blob: b70322eeb92403e71a8717e32d7ab285139108e5 [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
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000015#include <algorithm>
deadbeef1dcb1642017-03-29 21:08:16 -070016#include <cctype> // For std::isdigit.
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000017#include <memory>
deadbeef1dcb1642017-03-29 21:08:16 -070018#include <string>
19
Steve Anton10542f22019-01-11 09:11:00 -080020#include "p2p/base/port_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/arraysize.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/ip_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/socket_address.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000026#include "rtc_base/string_encode.h"
deadbeef1dcb1642017-03-29 21:08:16 -070027
28namespace webrtc {
29
deadbeef1dcb1642017-03-29 21:08:16 -070030// Number of tokens must be preset when TURN uri has transport param.
31static const size_t kTurnTransportTokensNum = 2;
32// The default stun port.
33static const int kDefaultStunPort = 3478;
34static const int kDefaultStunTlsPort = 5349;
35static const char kTransport[] = "transport";
36
Harald Alvestranda3dd7722020-11-27 08:05:42 +000037// Allowed characters in hostname per RFC 3986 Appendix A "reg-name"
38static const char kRegNameCharacters[] =
39 "abcdefghijklmnopqrstuvwxyz"
40 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
41 "0123456789"
42 "-._~" // unreserved
43 "%" // pct-encoded
44 "!$&'()*+,;="; // sub-delims
45
deadbeef1dcb1642017-03-29 21:08:16 -070046// NOTE: Must be in the same order as the ServiceType enum.
47static const char* kValidIceServiceTypes[] = {"stun", "stuns", "turn", "turns"};
48
49// NOTE: A loop below assumes that the first value of this enum is 0 and all
50// other values are incremental.
51enum ServiceType {
52 STUN = 0, // Indicates a STUN server.
53 STUNS, // Indicates a STUN server used with a TLS session.
54 TURN, // Indicates a TURN server
55 TURNS, // Indicates a TURN server used with a TLS session.
56 INVALID, // Unknown.
57};
58static_assert(INVALID == arraysize(kValidIceServiceTypes),
59 "kValidIceServiceTypes must have as many strings as ServiceType "
60 "has values.");
61
Artem Titov880fa812021-07-30 22:30:23 +020062// `in_str` should follow of RFC 7064/7065 syntax, but with an optional
Niels Möllerdb4def92019-03-18 16:53:59 +010063// "?transport=" already stripped. I.e.,
64// stunURI = scheme ":" host [ ":" port ]
65// scheme = "stun" / "stuns" / "turn" / "turns"
66// host = IP-literal / IPv4address / reg-name
67// port = *DIGIT
deadbeef1dcb1642017-03-29 21:08:16 -070068static bool GetServiceTypeAndHostnameFromUri(const std::string& in_str,
69 ServiceType* service_type,
70 std::string* hostname) {
71 const std::string::size_type colonpos = in_str.find(':');
72 if (colonpos == std::string::npos) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010073 RTC_LOG(LS_WARNING) << "Missing ':' in ICE URI: " << in_str;
deadbeef1dcb1642017-03-29 21:08:16 -070074 return false;
75 }
76 if ((colonpos + 1) == in_str.length()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010077 RTC_LOG(LS_WARNING) << "Empty hostname in ICE URI: " << in_str;
deadbeef1dcb1642017-03-29 21:08:16 -070078 return false;
79 }
80 *service_type = INVALID;
81 for (size_t i = 0; i < arraysize(kValidIceServiceTypes); ++i) {
82 if (in_str.compare(0, colonpos, kValidIceServiceTypes[i]) == 0) {
83 *service_type = static_cast<ServiceType>(i);
84 break;
85 }
86 }
87 if (*service_type == INVALID) {
88 return false;
89 }
90 *hostname = in_str.substr(colonpos + 1, std::string::npos);
91 return true;
92}
93
94static bool ParsePort(const std::string& in_str, int* port) {
95 // Make sure port only contains digits. FromString doesn't check this.
96 for (const char& c : in_str) {
97 if (!std::isdigit(c)) {
98 return false;
99 }
100 }
101 return rtc::FromString(in_str, port);
102}
103
104// This method parses IPv6 and IPv4 literal strings, along with hostnames in
105// standard hostname:port format.
106// Consider following formats as correct.
Artem Titovcfea2182021-08-10 01:22:31 +0200107// `hostname:port`, |[IPV6 address]:port|, |IPv4 address|:port,
Artem Titov880fa812021-07-30 22:30:23 +0200108// `hostname`, |[IPv6 address]|, |IPv4 address|.
deadbeef1dcb1642017-03-29 21:08:16 -0700109static bool ParseHostnameAndPortFromString(const std::string& in_str,
110 std::string* host,
111 int* port) {
112 RTC_DCHECK(host->empty());
113 if (in_str.at(0) == '[') {
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000114 // IP_literal syntax
deadbeef1dcb1642017-03-29 21:08:16 -0700115 std::string::size_type closebracket = in_str.rfind(']');
116 if (closebracket != std::string::npos) {
117 std::string::size_type colonpos = in_str.find(':', closebracket);
118 if (std::string::npos != colonpos) {
119 if (!ParsePort(in_str.substr(closebracket + 2, std::string::npos),
120 port)) {
121 return false;
122 }
123 }
124 *host = in_str.substr(1, closebracket - 1);
125 } else {
126 return false;
127 }
128 } else {
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000129 // IPv4address or reg-name syntax
deadbeef1dcb1642017-03-29 21:08:16 -0700130 std::string::size_type colonpos = in_str.find(':');
131 if (std::string::npos != colonpos) {
132 if (!ParsePort(in_str.substr(colonpos + 1, std::string::npos), port)) {
133 return false;
134 }
135 *host = in_str.substr(0, colonpos);
136 } else {
137 *host = in_str;
138 }
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000139 // RFC 3986 section 3.2.2 and Appendix A - "reg-name" syntax
140 if (host->find_first_not_of(kRegNameCharacters) != std::string::npos) {
141 return false;
142 }
deadbeef1dcb1642017-03-29 21:08:16 -0700143 }
144 return !host->empty();
145}
146
147// Adds a STUN or TURN server to the appropriate list,
Artem Titov880fa812021-07-30 22:30:23 +0200148// by parsing `url` and using the username/password in `server`.
deadbeef1dcb1642017-03-29 21:08:16 -0700149static RTCErrorType ParseIceServerUrl(
150 const PeerConnectionInterface::IceServer& server,
151 const std::string& url,
152 cricket::ServerAddresses* stun_servers,
153 std::vector<cricket::RelayServerConfig>* turn_servers) {
Niels Möllerdb4def92019-03-18 16:53:59 +0100154 // RFC 7064
155 // stunURI = scheme ":" host [ ":" port ]
deadbeef1dcb1642017-03-29 21:08:16 -0700156 // scheme = "stun" / "stuns"
deadbeef1dcb1642017-03-29 21:08:16 -0700157
Niels Möllerdb4def92019-03-18 16:53:59 +0100158 // RFC 7065
159 // turnURI = scheme ":" host [ ":" port ]
deadbeef1dcb1642017-03-29 21:08:16 -0700160 // [ "?transport=" transport ]
161 // scheme = "turn" / "turns"
162 // transport = "udp" / "tcp" / transport-ext
163 // transport-ext = 1*unreserved
Niels Möllerdb4def92019-03-18 16:53:59 +0100164
165 // RFC 3986
166 // host = IP-literal / IPv4address / reg-name
167 // port = *DIGIT
168
deadbeef1dcb1642017-03-29 21:08:16 -0700169 RTC_DCHECK(stun_servers != nullptr);
170 RTC_DCHECK(turn_servers != nullptr);
171 std::vector<std::string> tokens;
172 cricket::ProtocolType turn_transport_type = cricket::PROTO_UDP;
173 RTC_DCHECK(!url.empty());
Niels Möller634f2792021-09-07 16:06:57 +0200174 rtc::split(url, '?', &tokens);
deadbeef1dcb1642017-03-29 21:08:16 -0700175 std::string uri_without_transport = tokens[0];
176 // Let's look into transport= param, if it exists.
177 if (tokens.size() == kTurnTransportTokensNum) { // ?transport= is present.
178 std::string uri_transport_param = tokens[1];
Niels Möller634f2792021-09-07 16:06:57 +0200179 rtc::split(uri_transport_param, '=', &tokens);
deadbeef1dcb1642017-03-29 21:08:16 -0700180 if (tokens[0] != kTransport) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100181 RTC_LOG(LS_WARNING) << "Invalid transport parameter key.";
deadbeef1dcb1642017-03-29 21:08:16 -0700182 return RTCErrorType::SYNTAX_ERROR;
183 }
184 if (tokens.size() < 2) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100185 RTC_LOG(LS_WARNING) << "Transport parameter missing value.";
deadbeef1dcb1642017-03-29 21:08:16 -0700186 return RTCErrorType::SYNTAX_ERROR;
187 }
188 if (!cricket::StringToProto(tokens[1].c_str(), &turn_transport_type) ||
189 (turn_transport_type != cricket::PROTO_UDP &&
190 turn_transport_type != 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 }
194 }
195
196 std::string hoststring;
197 ServiceType service_type;
198 if (!GetServiceTypeAndHostnameFromUri(uri_without_transport, &service_type,
199 &hoststring)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100200 RTC_LOG(LS_WARNING) << "Invalid transport parameter in ICE URI: " << url;
deadbeef1dcb1642017-03-29 21:08:16 -0700201 return RTCErrorType::SYNTAX_ERROR;
202 }
203
204 // GetServiceTypeAndHostnameFromUri should never give an empty hoststring
205 RTC_DCHECK(!hoststring.empty());
206
deadbeef1dcb1642017-03-29 21:08:16 -0700207 int port = kDefaultStunPort;
208 if (service_type == TURNS) {
209 port = kDefaultStunTlsPort;
210 turn_transport_type = cricket::PROTO_TLS;
211 }
212
Niels Möllerdb4def92019-03-18 16:53:59 +0100213 if (hoststring.find('@') != std::string::npos) {
214 RTC_LOG(WARNING) << "Invalid url: " << uri_without_transport;
215 RTC_LOG(WARNING)
216 << "Note that user-info@ in turn:-urls is long-deprecated.";
217 return RTCErrorType::SYNTAX_ERROR;
218 }
deadbeef1dcb1642017-03-29 21:08:16 -0700219 std::string address;
220 if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100221 RTC_LOG(WARNING) << "Invalid hostname format: " << uri_without_transport;
deadbeef1dcb1642017-03-29 21:08:16 -0700222 return RTCErrorType::SYNTAX_ERROR;
223 }
224
225 if (port <= 0 || port > 0xffff) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100226 RTC_LOG(WARNING) << "Invalid port: " << port;
deadbeef1dcb1642017-03-29 21:08:16 -0700227 return RTCErrorType::SYNTAX_ERROR;
228 }
229
230 switch (service_type) {
231 case STUN:
232 case STUNS:
233 stun_servers->insert(rtc::SocketAddress(address, port));
234 break;
235 case TURN:
236 case TURNS: {
Niels Möllerdb4def92019-03-18 16:53:59 +0100237 if (server.username.empty() || server.password.empty()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700238 // The WebRTC spec requires throwing an InvalidAccessError when username
239 // or credential are ommitted; this is the native equivalent.
Philipp Hancke05fadac2021-11-04 09:13:17 +0100240 RTC_LOG(LS_WARNING) << "TURN server with empty username or password";
deadbeef1dcb1642017-03-29 21:08:16 -0700241 return RTCErrorType::INVALID_PARAMETER;
242 }
Emad Omaradab1d2d2017-06-16 15:43:11 -0700243 // If the hostname field is not empty, then the server address must be
244 // the resolved IP for that host, the hostname is needed later for TLS
245 // handshake (SNI and Certificate verification).
246 const std::string& hostname =
247 server.hostname.empty() ? address : server.hostname;
248 rtc::SocketAddress socket_address(hostname, port);
249 if (!server.hostname.empty()) {
250 rtc::IPAddress ip;
251 if (!IPFromString(address, &ip)) {
252 // When hostname is set, the server address must be a
253 // resolved ip address.
Philipp Hancke05fadac2021-11-04 09:13:17 +0100254 RTC_LOG(LS_WARNING)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100255 << "IceServer has hostname field set, but URI does not "
256 "contain an IP address.";
Emad Omaradab1d2d2017-06-16 15:43:11 -0700257 return RTCErrorType::INVALID_PARAMETER;
258 }
259 socket_address.SetResolvedIP(ip);
260 }
Niels Möllerdb4def92019-03-18 16:53:59 +0100261 cricket::RelayServerConfig config =
262 cricket::RelayServerConfig(socket_address, server.username,
263 server.password, turn_transport_type);
deadbeef1dcb1642017-03-29 21:08:16 -0700264 if (server.tls_cert_policy ==
265 PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck) {
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000266 config.tls_cert_policy =
267 cricket::TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK;
deadbeef1dcb1642017-03-29 21:08:16 -0700268 }
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000269 config.tls_alpn_protocols = server.tls_alpn_protocols;
270 config.tls_elliptic_curves = server.tls_elliptic_curves;
Diogo Real1dca9d52017-08-29 12:18:32 -0700271
deadbeef1dcb1642017-03-29 21:08:16 -0700272 turn_servers->push_back(config);
273 break;
274 }
275 default:
276 // We shouldn't get to this point with an invalid service_type, we should
277 // have returned an error already.
Artem Titovd3251962021-11-15 16:57:07 +0100278 RTC_DCHECK_NOTREACHED() << "Unexpected service type";
deadbeef1dcb1642017-03-29 21:08:16 -0700279 return RTCErrorType::INTERNAL_ERROR;
280 }
281 return RTCErrorType::NONE;
282}
283
284RTCErrorType ParseIceServers(
285 const PeerConnectionInterface::IceServers& servers,
286 cricket::ServerAddresses* stun_servers,
287 std::vector<cricket::RelayServerConfig>* turn_servers) {
288 for (const PeerConnectionInterface::IceServer& server : servers) {
289 if (!server.urls.empty()) {
290 for (const std::string& url : server.urls) {
291 if (url.empty()) {
Philipp Hancke05fadac2021-11-04 09:13:17 +0100292 RTC_LOG(LS_WARNING) << "Empty uri.";
deadbeef1dcb1642017-03-29 21:08:16 -0700293 return RTCErrorType::SYNTAX_ERROR;
294 }
295 RTCErrorType err =
296 ParseIceServerUrl(server, url, stun_servers, turn_servers);
297 if (err != RTCErrorType::NONE) {
298 return err;
299 }
300 }
301 } else if (!server.uri.empty()) {
302 // Fallback to old .uri if new .urls isn't present.
303 RTCErrorType err =
304 ParseIceServerUrl(server, server.uri, stun_servers, turn_servers);
305 if (err != RTCErrorType::NONE) {
306 return err;
307 }
308 } else {
Philipp Hancke05fadac2021-11-04 09:13:17 +0100309 RTC_LOG(LS_WARNING) << "Empty uri.";
deadbeef1dcb1642017-03-29 21:08:16 -0700310 return RTCErrorType::SYNTAX_ERROR;
311 }
312 }
313 // Candidates must have unique priorities, so that connectivity checks
314 // are performed in a well-defined order.
315 int priority = static_cast<int>(turn_servers->size() - 1);
316 for (cricket::RelayServerConfig& turn_server : *turn_servers) {
317 // First in the list gets highest priority.
318 turn_server.priority = priority--;
319 }
320 return RTCErrorType::NONE;
321}
322
323} // namespace webrtc