blob: 29127c5c91e20c599d7387017ab6da4f6d7f3322 [file] [log] [blame]
deadbeef1dcb1642017-03-29 21:08:16 -07001/*
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/ice_server_parsing.h"
deadbeef1dcb1642017-03-29 21:08:16 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
deadbeef1dcb1642017-03-29 21:08:16 -070015#include <cctype> // For std::isdigit.
16#include <string>
17
Steve Anton10542f22019-01-11 09:11:00 -080018#include "p2p/base/port_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/arraysize.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/ip_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/socket_address.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000024#include "rtc_base/string_encode.h"
deadbeef1dcb1642017-03-29 21:08:16 -070025
26namespace webrtc {
27
deadbeef1dcb1642017-03-29 21:08:16 -070028// Number of tokens must be preset when TURN uri has transport param.
29static const size_t kTurnTransportTokensNum = 2;
30// The default stun port.
31static const int kDefaultStunPort = 3478;
32static const int kDefaultStunTlsPort = 5349;
33static const char kTransport[] = "transport";
34
Harald Alvestranda3dd7722020-11-27 08:05:42 +000035// Allowed characters in hostname per RFC 3986 Appendix A "reg-name"
36static const char kRegNameCharacters[] =
37 "abcdefghijklmnopqrstuvwxyz"
38 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
39 "0123456789"
40 "-._~" // unreserved
41 "%" // pct-encoded
42 "!$&'()*+,;="; // sub-delims
43
deadbeef1dcb1642017-03-29 21:08:16 -070044// NOTE: Must be in the same order as the ServiceType enum.
45static const char* kValidIceServiceTypes[] = {"stun", "stuns", "turn", "turns"};
46
47// NOTE: A loop below assumes that the first value of this enum is 0 and all
48// other values are incremental.
49enum ServiceType {
50 STUN = 0, // Indicates a STUN server.
51 STUNS, // Indicates a STUN server used with a TLS session.
52 TURN, // Indicates a TURN server
53 TURNS, // Indicates a TURN server used with a TLS session.
54 INVALID, // Unknown.
55};
56static_assert(INVALID == arraysize(kValidIceServiceTypes),
57 "kValidIceServiceTypes must have as many strings as ServiceType "
58 "has values.");
59
Artem Titov880fa812021-07-30 22:30:23 +020060// `in_str` should follow of RFC 7064/7065 syntax, but with an optional
Niels Möllerdb4def92019-03-18 16:53:59 +010061// "?transport=" already stripped. I.e.,
62// stunURI = scheme ":" host [ ":" port ]
63// scheme = "stun" / "stuns" / "turn" / "turns"
64// host = IP-literal / IPv4address / reg-name
65// port = *DIGIT
deadbeef1dcb1642017-03-29 21:08:16 -070066static bool GetServiceTypeAndHostnameFromUri(const std::string& in_str,
67 ServiceType* service_type,
68 std::string* hostname) {
69 const std::string::size_type colonpos = in_str.find(':');
70 if (colonpos == std::string::npos) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010071 RTC_LOG(LS_WARNING) << "Missing ':' in ICE URI: " << in_str;
deadbeef1dcb1642017-03-29 21:08:16 -070072 return false;
73 }
74 if ((colonpos + 1) == in_str.length()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010075 RTC_LOG(LS_WARNING) << "Empty hostname in ICE URI: " << in_str;
deadbeef1dcb1642017-03-29 21:08:16 -070076 return false;
77 }
78 *service_type = INVALID;
79 for (size_t i = 0; i < arraysize(kValidIceServiceTypes); ++i) {
80 if (in_str.compare(0, colonpos, kValidIceServiceTypes[i]) == 0) {
81 *service_type = static_cast<ServiceType>(i);
82 break;
83 }
84 }
85 if (*service_type == INVALID) {
86 return false;
87 }
88 *hostname = in_str.substr(colonpos + 1, std::string::npos);
89 return true;
90}
91
92static bool ParsePort(const std::string& in_str, int* port) {
93 // Make sure port only contains digits. FromString doesn't check this.
94 for (const char& c : in_str) {
Niels Möllere66b83f2022-05-30 12:57:41 +020095 if (!std::isdigit(static_cast<unsigned char>(c))) {
deadbeef1dcb1642017-03-29 21:08:16 -070096 return false;
97 }
98 }
99 return rtc::FromString(in_str, port);
100}
101
102// This method parses IPv6 and IPv4 literal strings, along with hostnames in
103// standard hostname:port format.
104// Consider following formats as correct.
Artem Titovcfea2182021-08-10 01:22:31 +0200105// `hostname:port`, |[IPV6 address]:port|, |IPv4 address|:port,
Artem Titov880fa812021-07-30 22:30:23 +0200106// `hostname`, |[IPv6 address]|, |IPv4 address|.
deadbeef1dcb1642017-03-29 21:08:16 -0700107static bool ParseHostnameAndPortFromString(const std::string& in_str,
108 std::string* host,
109 int* port) {
110 RTC_DCHECK(host->empty());
111 if (in_str.at(0) == '[') {
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000112 // IP_literal syntax
deadbeef1dcb1642017-03-29 21:08:16 -0700113 std::string::size_type closebracket = in_str.rfind(']');
114 if (closebracket != std::string::npos) {
115 std::string::size_type colonpos = in_str.find(':', closebracket);
116 if (std::string::npos != colonpos) {
117 if (!ParsePort(in_str.substr(closebracket + 2, std::string::npos),
118 port)) {
119 return false;
120 }
121 }
122 *host = in_str.substr(1, closebracket - 1);
123 } else {
124 return false;
125 }
126 } else {
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000127 // IPv4address or reg-name syntax
deadbeef1dcb1642017-03-29 21:08:16 -0700128 std::string::size_type colonpos = in_str.find(':');
129 if (std::string::npos != colonpos) {
130 if (!ParsePort(in_str.substr(colonpos + 1, std::string::npos), port)) {
131 return false;
132 }
133 *host = in_str.substr(0, colonpos);
134 } else {
135 *host = in_str;
136 }
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000137 // RFC 3986 section 3.2.2 and Appendix A - "reg-name" syntax
138 if (host->find_first_not_of(kRegNameCharacters) != std::string::npos) {
139 return false;
140 }
deadbeef1dcb1642017-03-29 21:08:16 -0700141 }
142 return !host->empty();
143}
144
145// Adds a STUN or TURN server to the appropriate list,
Artem Titov880fa812021-07-30 22:30:23 +0200146// by parsing `url` and using the username/password in `server`.
deadbeef1dcb1642017-03-29 21:08:16 -0700147static RTCErrorType ParseIceServerUrl(
148 const PeerConnectionInterface::IceServer& server,
149 const std::string& url,
150 cricket::ServerAddresses* stun_servers,
151 std::vector<cricket::RelayServerConfig>* turn_servers) {
Niels Möllerdb4def92019-03-18 16:53:59 +0100152 // RFC 7064
153 // stunURI = scheme ":" host [ ":" port ]
deadbeef1dcb1642017-03-29 21:08:16 -0700154 // scheme = "stun" / "stuns"
deadbeef1dcb1642017-03-29 21:08:16 -0700155
Niels Möllerdb4def92019-03-18 16:53:59 +0100156 // RFC 7065
157 // turnURI = scheme ":" host [ ":" port ]
deadbeef1dcb1642017-03-29 21:08:16 -0700158 // [ "?transport=" transport ]
159 // scheme = "turn" / "turns"
160 // transport = "udp" / "tcp" / transport-ext
161 // transport-ext = 1*unreserved
Niels Möllerdb4def92019-03-18 16:53:59 +0100162
163 // RFC 3986
164 // host = IP-literal / IPv4address / reg-name
165 // port = *DIGIT
166
deadbeef1dcb1642017-03-29 21:08:16 -0700167 RTC_DCHECK(stun_servers != nullptr);
168 RTC_DCHECK(turn_servers != nullptr);
169 std::vector<std::string> tokens;
170 cricket::ProtocolType turn_transport_type = cricket::PROTO_UDP;
171 RTC_DCHECK(!url.empty());
Niels Möller634f2792021-09-07 16:06:57 +0200172 rtc::split(url, '?', &tokens);
deadbeef1dcb1642017-03-29 21:08:16 -0700173 std::string uri_without_transport = tokens[0];
174 // Let's look into transport= param, if it exists.
175 if (tokens.size() == kTurnTransportTokensNum) { // ?transport= is present.
176 std::string uri_transport_param = tokens[1];
Niels Möller634f2792021-09-07 16:06:57 +0200177 rtc::split(uri_transport_param, '=', &tokens);
deadbeef1dcb1642017-03-29 21:08:16 -0700178 if (tokens[0] != kTransport) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100179 RTC_LOG(LS_WARNING) << "Invalid transport parameter key.";
deadbeef1dcb1642017-03-29 21:08:16 -0700180 return RTCErrorType::SYNTAX_ERROR;
181 }
182 if (tokens.size() < 2) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100183 RTC_LOG(LS_WARNING) << "Transport parameter missing value.";
deadbeef1dcb1642017-03-29 21:08:16 -0700184 return RTCErrorType::SYNTAX_ERROR;
185 }
Niels Möller4662f532022-05-20 15:44:37 +0200186
187 absl::optional<cricket::ProtocolType> proto =
188 cricket::StringToProto(tokens[1]);
189 if (!proto ||
190 (*proto != cricket::PROTO_UDP && *proto != cricket::PROTO_TCP)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100191 RTC_LOG(LS_WARNING) << "Transport parameter should always be udp or tcp.";
deadbeef1dcb1642017-03-29 21:08:16 -0700192 return RTCErrorType::SYNTAX_ERROR;
193 }
Niels Möller4662f532022-05-20 15:44:37 +0200194 turn_transport_type = *proto;
deadbeef1dcb1642017-03-29 21:08:16 -0700195 }
196
197 std::string hoststring;
198 ServiceType service_type;
199 if (!GetServiceTypeAndHostnameFromUri(uri_without_transport, &service_type,
200 &hoststring)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100201 RTC_LOG(LS_WARNING) << "Invalid transport parameter in ICE URI: " << url;
deadbeef1dcb1642017-03-29 21:08:16 -0700202 return RTCErrorType::SYNTAX_ERROR;
203 }
204
205 // GetServiceTypeAndHostnameFromUri should never give an empty hoststring
206 RTC_DCHECK(!hoststring.empty());
207
deadbeef1dcb1642017-03-29 21:08:16 -0700208 int port = kDefaultStunPort;
209 if (service_type == TURNS) {
210 port = kDefaultStunTlsPort;
211 turn_transport_type = cricket::PROTO_TLS;
212 }
213
Niels Möllerdb4def92019-03-18 16:53:59 +0100214 if (hoststring.find('@') != std::string::npos) {
Harald Alvestrandef5b21e2021-11-27 21:31:08 +0000215 RTC_LOG(LS_WARNING) << "Invalid url: " << uri_without_transport;
216 RTC_LOG(LS_WARNING)
Niels Möllerdb4def92019-03-18 16:53:59 +0100217 << "Note that user-info@ in turn:-urls is long-deprecated.";
218 return RTCErrorType::SYNTAX_ERROR;
219 }
deadbeef1dcb1642017-03-29 21:08:16 -0700220 std::string address;
221 if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) {
Harald Alvestrandef5b21e2021-11-27 21:31:08 +0000222 RTC_LOG(LS_WARNING) << "Invalid hostname format: " << uri_without_transport;
deadbeef1dcb1642017-03-29 21:08:16 -0700223 return RTCErrorType::SYNTAX_ERROR;
224 }
225
226 if (port <= 0 || port > 0xffff) {
Harald Alvestrandef5b21e2021-11-27 21:31:08 +0000227 RTC_LOG(LS_WARNING) << "Invalid port: " << port;
deadbeef1dcb1642017-03-29 21:08:16 -0700228 return RTCErrorType::SYNTAX_ERROR;
229 }
230
231 switch (service_type) {
232 case STUN:
233 case STUNS:
234 stun_servers->insert(rtc::SocketAddress(address, port));
235 break;
236 case TURN:
237 case TURNS: {
Niels Möllerdb4def92019-03-18 16:53:59 +0100238 if (server.username.empty() || server.password.empty()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700239 // The WebRTC spec requires throwing an InvalidAccessError when username
240 // or credential are ommitted; this is the native equivalent.
Philipp Hancke05fadac2021-11-04 09:13:17 +0100241 RTC_LOG(LS_WARNING) << "TURN server with empty username or password";
deadbeef1dcb1642017-03-29 21:08:16 -0700242 return RTCErrorType::INVALID_PARAMETER;
243 }
Emad Omaradab1d2d2017-06-16 15:43:11 -0700244 // If the hostname field is not empty, then the server address must be
245 // the resolved IP for that host, the hostname is needed later for TLS
246 // handshake (SNI and Certificate verification).
247 const std::string& hostname =
248 server.hostname.empty() ? address : server.hostname;
249 rtc::SocketAddress socket_address(hostname, port);
250 if (!server.hostname.empty()) {
251 rtc::IPAddress ip;
252 if (!IPFromString(address, &ip)) {
253 // When hostname is set, the server address must be a
254 // resolved ip address.
Philipp Hancke05fadac2021-11-04 09:13:17 +0100255 RTC_LOG(LS_WARNING)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100256 << "IceServer has hostname field set, but URI does not "
257 "contain an IP address.";
Emad Omaradab1d2d2017-06-16 15:43:11 -0700258 return RTCErrorType::INVALID_PARAMETER;
259 }
260 socket_address.SetResolvedIP(ip);
261 }
Niels Möllerdb4def92019-03-18 16:53:59 +0100262 cricket::RelayServerConfig config =
263 cricket::RelayServerConfig(socket_address, server.username,
264 server.password, turn_transport_type);
deadbeef1dcb1642017-03-29 21:08:16 -0700265 if (server.tls_cert_policy ==
266 PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck) {
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000267 config.tls_cert_policy =
268 cricket::TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK;
deadbeef1dcb1642017-03-29 21:08:16 -0700269 }
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000270 config.tls_alpn_protocols = server.tls_alpn_protocols;
271 config.tls_elliptic_curves = server.tls_elliptic_curves;
Diogo Real1dca9d52017-08-29 12:18:32 -0700272
deadbeef1dcb1642017-03-29 21:08:16 -0700273 turn_servers->push_back(config);
274 break;
275 }
276 default:
277 // We shouldn't get to this point with an invalid service_type, we should
278 // have returned an error already.
Artem Titovd3251962021-11-15 16:57:07 +0100279 RTC_DCHECK_NOTREACHED() << "Unexpected service type";
deadbeef1dcb1642017-03-29 21:08:16 -0700280 return RTCErrorType::INTERNAL_ERROR;
281 }
282 return RTCErrorType::NONE;
283}
284
285RTCErrorType ParseIceServers(
286 const PeerConnectionInterface::IceServers& servers,
287 cricket::ServerAddresses* stun_servers,
288 std::vector<cricket::RelayServerConfig>* turn_servers) {
289 for (const PeerConnectionInterface::IceServer& server : servers) {
290 if (!server.urls.empty()) {
291 for (const std::string& url : server.urls) {
292 if (url.empty()) {
Philipp Hancke05fadac2021-11-04 09:13:17 +0100293 RTC_LOG(LS_WARNING) << "Empty uri.";
deadbeef1dcb1642017-03-29 21:08:16 -0700294 return RTCErrorType::SYNTAX_ERROR;
295 }
296 RTCErrorType err =
297 ParseIceServerUrl(server, url, stun_servers, turn_servers);
298 if (err != RTCErrorType::NONE) {
299 return err;
300 }
301 }
302 } else if (!server.uri.empty()) {
303 // Fallback to old .uri if new .urls isn't present.
304 RTCErrorType err =
305 ParseIceServerUrl(server, server.uri, stun_servers, turn_servers);
306 if (err != RTCErrorType::NONE) {
307 return err;
308 }
309 } else {
Philipp Hancke05fadac2021-11-04 09:13:17 +0100310 RTC_LOG(LS_WARNING) << "Empty uri.";
deadbeef1dcb1642017-03-29 21:08:16 -0700311 return RTCErrorType::SYNTAX_ERROR;
312 }
313 }
314 // Candidates must have unique priorities, so that connectivity checks
315 // are performed in a well-defined order.
316 int priority = static_cast<int>(turn_servers->size() - 1);
317 for (cricket::RelayServerConfig& turn_server : *turn_servers) {
318 // First in the list gets highest priority.
319 turn_server.priority = priority--;
320 }
321 return RTCErrorType::NONE;
322}
323
324} // namespace webrtc