blob: cb4145be1af8a5c2bd6ddaef7ce7d9be274c81eb [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) {
95 if (!std::isdigit(c)) {
96 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 }
186 if (!cricket::StringToProto(tokens[1].c_str(), &turn_transport_type) ||
187 (turn_transport_type != cricket::PROTO_UDP &&
188 turn_transport_type != cricket::PROTO_TCP)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100189 RTC_LOG(LS_WARNING) << "Transport parameter should always be udp or tcp.";
deadbeef1dcb1642017-03-29 21:08:16 -0700190 return RTCErrorType::SYNTAX_ERROR;
191 }
192 }
193
194 std::string hoststring;
195 ServiceType service_type;
196 if (!GetServiceTypeAndHostnameFromUri(uri_without_transport, &service_type,
197 &hoststring)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100198 RTC_LOG(LS_WARNING) << "Invalid transport parameter in ICE URI: " << url;
deadbeef1dcb1642017-03-29 21:08:16 -0700199 return RTCErrorType::SYNTAX_ERROR;
200 }
201
202 // GetServiceTypeAndHostnameFromUri should never give an empty hoststring
203 RTC_DCHECK(!hoststring.empty());
204
deadbeef1dcb1642017-03-29 21:08:16 -0700205 int port = kDefaultStunPort;
206 if (service_type == TURNS) {
207 port = kDefaultStunTlsPort;
208 turn_transport_type = cricket::PROTO_TLS;
209 }
210
Niels Möllerdb4def92019-03-18 16:53:59 +0100211 if (hoststring.find('@') != std::string::npos) {
Harald Alvestrandef5b21e2021-11-27 21:31:08 +0000212 RTC_LOG(LS_WARNING) << "Invalid url: " << uri_without_transport;
213 RTC_LOG(LS_WARNING)
Niels Möllerdb4def92019-03-18 16:53:59 +0100214 << "Note that user-info@ in turn:-urls is long-deprecated.";
215 return RTCErrorType::SYNTAX_ERROR;
216 }
deadbeef1dcb1642017-03-29 21:08:16 -0700217 std::string address;
218 if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) {
Harald Alvestrandef5b21e2021-11-27 21:31:08 +0000219 RTC_LOG(LS_WARNING) << "Invalid hostname format: " << uri_without_transport;
deadbeef1dcb1642017-03-29 21:08:16 -0700220 return RTCErrorType::SYNTAX_ERROR;
221 }
222
223 if (port <= 0 || port > 0xffff) {
Harald Alvestrandef5b21e2021-11-27 21:31:08 +0000224 RTC_LOG(LS_WARNING) << "Invalid port: " << port;
deadbeef1dcb1642017-03-29 21:08:16 -0700225 return RTCErrorType::SYNTAX_ERROR;
226 }
227
228 switch (service_type) {
229 case STUN:
230 case STUNS:
231 stun_servers->insert(rtc::SocketAddress(address, port));
232 break;
233 case TURN:
234 case TURNS: {
Niels Möllerdb4def92019-03-18 16:53:59 +0100235 if (server.username.empty() || server.password.empty()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700236 // The WebRTC spec requires throwing an InvalidAccessError when username
237 // or credential are ommitted; this is the native equivalent.
Philipp Hancke05fadac2021-11-04 09:13:17 +0100238 RTC_LOG(LS_WARNING) << "TURN server with empty username or password";
deadbeef1dcb1642017-03-29 21:08:16 -0700239 return RTCErrorType::INVALID_PARAMETER;
240 }
Emad Omaradab1d2d2017-06-16 15:43:11 -0700241 // If the hostname field is not empty, then the server address must be
242 // the resolved IP for that host, the hostname is needed later for TLS
243 // handshake (SNI and Certificate verification).
244 const std::string& hostname =
245 server.hostname.empty() ? address : server.hostname;
246 rtc::SocketAddress socket_address(hostname, port);
247 if (!server.hostname.empty()) {
248 rtc::IPAddress ip;
249 if (!IPFromString(address, &ip)) {
250 // When hostname is set, the server address must be a
251 // resolved ip address.
Philipp Hancke05fadac2021-11-04 09:13:17 +0100252 RTC_LOG(LS_WARNING)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100253 << "IceServer has hostname field set, but URI does not "
254 "contain an IP address.";
Emad Omaradab1d2d2017-06-16 15:43:11 -0700255 return RTCErrorType::INVALID_PARAMETER;
256 }
257 socket_address.SetResolvedIP(ip);
258 }
Niels Möllerdb4def92019-03-18 16:53:59 +0100259 cricket::RelayServerConfig config =
260 cricket::RelayServerConfig(socket_address, server.username,
261 server.password, turn_transport_type);
deadbeef1dcb1642017-03-29 21:08:16 -0700262 if (server.tls_cert_policy ==
263 PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck) {
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000264 config.tls_cert_policy =
265 cricket::TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK;
deadbeef1dcb1642017-03-29 21:08:16 -0700266 }
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000267 config.tls_alpn_protocols = server.tls_alpn_protocols;
268 config.tls_elliptic_curves = server.tls_elliptic_curves;
Diogo Real1dca9d52017-08-29 12:18:32 -0700269
deadbeef1dcb1642017-03-29 21:08:16 -0700270 turn_servers->push_back(config);
271 break;
272 }
273 default:
274 // We shouldn't get to this point with an invalid service_type, we should
275 // have returned an error already.
Artem Titovd3251962021-11-15 16:57:07 +0100276 RTC_DCHECK_NOTREACHED() << "Unexpected service type";
deadbeef1dcb1642017-03-29 21:08:16 -0700277 return RTCErrorType::INTERNAL_ERROR;
278 }
279 return RTCErrorType::NONE;
280}
281
282RTCErrorType ParseIceServers(
283 const PeerConnectionInterface::IceServers& servers,
284 cricket::ServerAddresses* stun_servers,
285 std::vector<cricket::RelayServerConfig>* turn_servers) {
286 for (const PeerConnectionInterface::IceServer& server : servers) {
287 if (!server.urls.empty()) {
288 for (const std::string& url : server.urls) {
289 if (url.empty()) {
Philipp Hancke05fadac2021-11-04 09:13:17 +0100290 RTC_LOG(LS_WARNING) << "Empty uri.";
deadbeef1dcb1642017-03-29 21:08:16 -0700291 return RTCErrorType::SYNTAX_ERROR;
292 }
293 RTCErrorType err =
294 ParseIceServerUrl(server, url, stun_servers, turn_servers);
295 if (err != RTCErrorType::NONE) {
296 return err;
297 }
298 }
299 } else if (!server.uri.empty()) {
300 // Fallback to old .uri if new .urls isn't present.
301 RTCErrorType err =
302 ParseIceServerUrl(server, server.uri, stun_servers, turn_servers);
303 if (err != RTCErrorType::NONE) {
304 return err;
305 }
306 } else {
Philipp Hancke05fadac2021-11-04 09:13:17 +0100307 RTC_LOG(LS_WARNING) << "Empty uri.";
deadbeef1dcb1642017-03-29 21:08:16 -0700308 return RTCErrorType::SYNTAX_ERROR;
309 }
310 }
311 // Candidates must have unique priorities, so that connectivity checks
312 // are performed in a well-defined order.
313 int priority = static_cast<int>(turn_servers->size() - 1);
314 for (cricket::RelayServerConfig& turn_server : *turn_servers) {
315 // First in the list gets highest priority.
316 turn_server.priority = priority--;
317 }
318 return RTCErrorType::NONE;
319}
320
321} // namespace webrtc