blob: 74d1ed2306fbb3067c1d538bd3c4b267075be70e [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/iceserverparsing.h"
deadbeef1dcb1642017-03-29 21:08:16 -070012
13#include <cctype> // For std::isdigit.
14#include <string>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/arraysize.h"
Diogo Real4f085432018-09-11 16:00:22 -070017#include "rtc_base/ssladapter.h"
deadbeef1dcb1642017-03-29 21:08:16 -070018
19namespace webrtc {
20
21// The min number of tokens must present in Turn host uri.
22// e.g. user@turn.example.org
23static const size_t kTurnHostTokensNum = 2;
24// Number of tokens must be preset when TURN uri has transport param.
25static const size_t kTurnTransportTokensNum = 2;
26// The default stun port.
27static const int kDefaultStunPort = 3478;
28static const int kDefaultStunTlsPort = 5349;
29static const char kTransport[] = "transport";
30
31// NOTE: Must be in the same order as the ServiceType enum.
32static const char* kValidIceServiceTypes[] = {"stun", "stuns", "turn", "turns"};
33
34// NOTE: A loop below assumes that the first value of this enum is 0 and all
35// other values are incremental.
36enum ServiceType {
37 STUN = 0, // Indicates a STUN server.
38 STUNS, // Indicates a STUN server used with a TLS session.
39 TURN, // Indicates a TURN server
40 TURNS, // Indicates a TURN server used with a TLS session.
41 INVALID, // Unknown.
42};
43static_assert(INVALID == arraysize(kValidIceServiceTypes),
44 "kValidIceServiceTypes must have as many strings as ServiceType "
45 "has values.");
46
47// |in_str| should be of format
48// stunURI = scheme ":" stun-host [ ":" stun-port ]
49// scheme = "stun" / "stuns"
50// stun-host = IP-literal / IPv4address / reg-name
51// stun-port = *DIGIT
52//
53// draft-petithuguenin-behave-turn-uris-01
54// turnURI = scheme ":" turn-host [ ":" turn-port ]
55// turn-host = username@IP-literal / IPv4address / reg-name
56static bool GetServiceTypeAndHostnameFromUri(const std::string& in_str,
57 ServiceType* service_type,
58 std::string* hostname) {
59 const std::string::size_type colonpos = in_str.find(':');
60 if (colonpos == std::string::npos) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010061 RTC_LOG(LS_WARNING) << "Missing ':' in ICE URI: " << in_str;
deadbeef1dcb1642017-03-29 21:08:16 -070062 return false;
63 }
64 if ((colonpos + 1) == in_str.length()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010065 RTC_LOG(LS_WARNING) << "Empty hostname in ICE URI: " << in_str;
deadbeef1dcb1642017-03-29 21:08:16 -070066 return false;
67 }
68 *service_type = INVALID;
69 for (size_t i = 0; i < arraysize(kValidIceServiceTypes); ++i) {
70 if (in_str.compare(0, colonpos, kValidIceServiceTypes[i]) == 0) {
71 *service_type = static_cast<ServiceType>(i);
72 break;
73 }
74 }
75 if (*service_type == INVALID) {
76 return false;
77 }
78 *hostname = in_str.substr(colonpos + 1, std::string::npos);
79 return true;
80}
81
82static bool ParsePort(const std::string& in_str, int* port) {
83 // Make sure port only contains digits. FromString doesn't check this.
84 for (const char& c : in_str) {
85 if (!std::isdigit(c)) {
86 return false;
87 }
88 }
89 return rtc::FromString(in_str, port);
90}
91
92// This method parses IPv6 and IPv4 literal strings, along with hostnames in
93// standard hostname:port format.
94// Consider following formats as correct.
95// |hostname:port|, |[IPV6 address]:port|, |IPv4 address|:port,
96// |hostname|, |[IPv6 address]|, |IPv4 address|.
97static bool ParseHostnameAndPortFromString(const std::string& in_str,
98 std::string* host,
99 int* port) {
100 RTC_DCHECK(host->empty());
101 if (in_str.at(0) == '[') {
102 std::string::size_type closebracket = in_str.rfind(']');
103 if (closebracket != std::string::npos) {
104 std::string::size_type colonpos = in_str.find(':', closebracket);
105 if (std::string::npos != colonpos) {
106 if (!ParsePort(in_str.substr(closebracket + 2, std::string::npos),
107 port)) {
108 return false;
109 }
110 }
111 *host = in_str.substr(1, closebracket - 1);
112 } else {
113 return false;
114 }
115 } else {
116 std::string::size_type colonpos = in_str.find(':');
117 if (std::string::npos != colonpos) {
118 if (!ParsePort(in_str.substr(colonpos + 1, std::string::npos), port)) {
119 return false;
120 }
121 *host = in_str.substr(0, colonpos);
122 } else {
123 *host = in_str;
124 }
125 }
126 return !host->empty();
127}
128
129// Adds a STUN or TURN server to the appropriate list,
130// by parsing |url| and using the username/password in |server|.
131static RTCErrorType ParseIceServerUrl(
132 const PeerConnectionInterface::IceServer& server,
133 const std::string& url,
134 cricket::ServerAddresses* stun_servers,
135 std::vector<cricket::RelayServerConfig>* turn_servers) {
136 // draft-nandakumar-rtcweb-stun-uri-01
137 // stunURI = scheme ":" stun-host [ ":" stun-port ]
138 // scheme = "stun" / "stuns"
139 // stun-host = IP-literal / IPv4address / reg-name
140 // stun-port = *DIGIT
141
142 // draft-petithuguenin-behave-turn-uris-01
143 // turnURI = scheme ":" turn-host [ ":" turn-port ]
144 // [ "?transport=" transport ]
145 // scheme = "turn" / "turns"
146 // transport = "udp" / "tcp" / transport-ext
147 // transport-ext = 1*unreserved
148 // turn-host = IP-literal / IPv4address / reg-name
149 // turn-port = *DIGIT
150 RTC_DCHECK(stun_servers != nullptr);
151 RTC_DCHECK(turn_servers != nullptr);
152 std::vector<std::string> tokens;
153 cricket::ProtocolType turn_transport_type = cricket::PROTO_UDP;
154 RTC_DCHECK(!url.empty());
155 rtc::tokenize_with_empty_tokens(url, '?', &tokens);
156 std::string uri_without_transport = tokens[0];
157 // Let's look into transport= param, if it exists.
158 if (tokens.size() == kTurnTransportTokensNum) { // ?transport= is present.
159 std::string uri_transport_param = tokens[1];
160 rtc::tokenize_with_empty_tokens(uri_transport_param, '=', &tokens);
161 if (tokens[0] != kTransport) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100162 RTC_LOG(LS_WARNING) << "Invalid transport parameter key.";
deadbeef1dcb1642017-03-29 21:08:16 -0700163 return RTCErrorType::SYNTAX_ERROR;
164 }
165 if (tokens.size() < 2) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100166 RTC_LOG(LS_WARNING) << "Transport parameter missing value.";
deadbeef1dcb1642017-03-29 21:08:16 -0700167 return RTCErrorType::SYNTAX_ERROR;
168 }
169 if (!cricket::StringToProto(tokens[1].c_str(), &turn_transport_type) ||
170 (turn_transport_type != cricket::PROTO_UDP &&
171 turn_transport_type != cricket::PROTO_TCP)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100172 RTC_LOG(LS_WARNING) << "Transport parameter should always be udp or tcp.";
deadbeef1dcb1642017-03-29 21:08:16 -0700173 return RTCErrorType::SYNTAX_ERROR;
174 }
175 }
176
177 std::string hoststring;
178 ServiceType service_type;
179 if (!GetServiceTypeAndHostnameFromUri(uri_without_transport, &service_type,
180 &hoststring)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100181 RTC_LOG(LS_WARNING) << "Invalid transport parameter in ICE URI: " << url;
deadbeef1dcb1642017-03-29 21:08:16 -0700182 return RTCErrorType::SYNTAX_ERROR;
183 }
184
185 // GetServiceTypeAndHostnameFromUri should never give an empty hoststring
186 RTC_DCHECK(!hoststring.empty());
187
188 // Let's break hostname.
189 tokens.clear();
190 rtc::tokenize_with_empty_tokens(hoststring, '@', &tokens);
191
192 std::string username(server.username);
193 if (tokens.size() > kTurnHostTokensNum) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100194 RTC_LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring;
deadbeef1dcb1642017-03-29 21:08:16 -0700195 return RTCErrorType::SYNTAX_ERROR;
196 }
197 if (tokens.size() == kTurnHostTokensNum) {
198 if (tokens[0].empty() || tokens[1].empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100199 RTC_LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring;
deadbeef1dcb1642017-03-29 21:08:16 -0700200 return RTCErrorType::SYNTAX_ERROR;
201 }
202 username.assign(rtc::s_url_decode(tokens[0]));
203 hoststring = tokens[1];
204 } else {
205 hoststring = tokens[0];
206 }
207
208 int port = kDefaultStunPort;
209 if (service_type == TURNS) {
210 port = kDefaultStunTlsPort;
211 turn_transport_type = cricket::PROTO_TLS;
212 }
213
214 std::string address;
215 if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100216 RTC_LOG(WARNING) << "Invalid hostname format: " << uri_without_transport;
deadbeef1dcb1642017-03-29 21:08:16 -0700217 return RTCErrorType::SYNTAX_ERROR;
218 }
219
220 if (port <= 0 || port > 0xffff) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100221 RTC_LOG(WARNING) << "Invalid port: " << port;
deadbeef1dcb1642017-03-29 21:08:16 -0700222 return RTCErrorType::SYNTAX_ERROR;
223 }
224
225 switch (service_type) {
226 case STUN:
227 case STUNS:
228 stun_servers->insert(rtc::SocketAddress(address, port));
229 break;
230 case TURN:
231 case TURNS: {
232 if (username.empty() || server.password.empty()) {
233 // The WebRTC spec requires throwing an InvalidAccessError when username
234 // or credential are ommitted; this is the native equivalent.
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200235 RTC_LOG(LS_ERROR) << "TURN URL without username, or password empty";
deadbeef1dcb1642017-03-29 21:08:16 -0700236 return RTCErrorType::INVALID_PARAMETER;
237 }
Emad Omaradab1d2d2017-06-16 15:43:11 -0700238 // If the hostname field is not empty, then the server address must be
239 // the resolved IP for that host, the hostname is needed later for TLS
240 // handshake (SNI and Certificate verification).
241 const std::string& hostname =
242 server.hostname.empty() ? address : server.hostname;
243 rtc::SocketAddress socket_address(hostname, port);
244 if (!server.hostname.empty()) {
245 rtc::IPAddress ip;
246 if (!IPFromString(address, &ip)) {
247 // When hostname is set, the server address must be a
248 // resolved ip address.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100249 RTC_LOG(LS_ERROR)
250 << "IceServer has hostname field set, but URI does not "
251 "contain an IP address.";
Emad Omaradab1d2d2017-06-16 15:43:11 -0700252 return RTCErrorType::INVALID_PARAMETER;
253 }
254 socket_address.SetResolvedIP(ip);
255 }
deadbeef1dcb1642017-03-29 21:08:16 -0700256 cricket::RelayServerConfig config = cricket::RelayServerConfig(
Emad Omaradab1d2d2017-06-16 15:43:11 -0700257 socket_address, username, server.password, turn_transport_type);
Diogo Real4f085432018-09-11 16:00:22 -0700258
259 config.ssl_config = server.ssl_config;
260
deadbeef1dcb1642017-03-29 21:08:16 -0700261 if (server.tls_cert_policy ==
262 PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck) {
Diogo Real4f085432018-09-11 16:00:22 -0700263 config.ssl_config.tls_cert_policy =
264 rtc::TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK;
deadbeef1dcb1642017-03-29 21:08:16 -0700265 }
Diogo Real4f085432018-09-11 16:00:22 -0700266 if (!server.ssl_config.tls_alpn_protocols.has_value() &&
267 !server.tls_alpn_protocols.empty()) {
268 config.ssl_config.tls_alpn_protocols = server.tls_alpn_protocols;
269 }
270 if (!server.ssl_config.tls_elliptic_curves.has_value() &&
271 !server.tls_elliptic_curves.empty()) {
272 config.ssl_config.tls_elliptic_curves = server.tls_elliptic_curves;
273 }
Diogo Real1dca9d52017-08-29 12:18:32 -0700274
deadbeef1dcb1642017-03-29 21:08:16 -0700275 turn_servers->push_back(config);
276 break;
277 }
278 default:
279 // We shouldn't get to this point with an invalid service_type, we should
280 // have returned an error already.
281 RTC_NOTREACHED() << "Unexpected service type";
282 return RTCErrorType::INTERNAL_ERROR;
283 }
284 return RTCErrorType::NONE;
285}
286
287RTCErrorType ParseIceServers(
288 const PeerConnectionInterface::IceServers& servers,
289 cricket::ServerAddresses* stun_servers,
290 std::vector<cricket::RelayServerConfig>* turn_servers) {
291 for (const PeerConnectionInterface::IceServer& server : servers) {
292 if (!server.urls.empty()) {
293 for (const std::string& url : server.urls) {
294 if (url.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100295 RTC_LOG(LS_ERROR) << "Empty uri.";
deadbeef1dcb1642017-03-29 21:08:16 -0700296 return RTCErrorType::SYNTAX_ERROR;
297 }
298 RTCErrorType err =
299 ParseIceServerUrl(server, url, stun_servers, turn_servers);
300 if (err != RTCErrorType::NONE) {
301 return err;
302 }
303 }
304 } else if (!server.uri.empty()) {
305 // Fallback to old .uri if new .urls isn't present.
306 RTCErrorType err =
307 ParseIceServerUrl(server, server.uri, stun_servers, turn_servers);
308 if (err != RTCErrorType::NONE) {
309 return err;
310 }
311 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100312 RTC_LOG(LS_ERROR) << "Empty uri.";
deadbeef1dcb1642017-03-29 21:08:16 -0700313 return RTCErrorType::SYNTAX_ERROR;
314 }
315 }
316 // Candidates must have unique priorities, so that connectivity checks
317 // are performed in a well-defined order.
318 int priority = static_cast<int>(turn_servers->size() - 1);
319 for (cricket::RelayServerConfig& turn_server : *turn_servers) {
320 // First in the list gets highest priority.
321 turn_server.priority = priority--;
322 }
323 return RTCErrorType::NONE;
324}
325
326} // namespace webrtc