blob: 533d59787dee0f6f2f34e6115de7d6fbbf95173e [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>
deadbeef1dcb1642017-03-29 21:08:16 -070014#include <cctype> // For std::isdigit.
15#include <string>
16
Steve Anton10542f22019-01-11 09:11:00 -080017#include "p2p/base/port_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/arraysize.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/ip_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/socket_address.h"
23#include "rtc_base/string_encode.h"
deadbeef1dcb1642017-03-29 21:08:16 -070024
25namespace webrtc {
26
27// The min number of tokens must present in Turn host uri.
28// e.g. user@turn.example.org
29static const size_t kTurnHostTokensNum = 2;
30// 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
37// NOTE: Must be in the same order as the ServiceType enum.
38static const char* kValidIceServiceTypes[] = {"stun", "stuns", "turn", "turns"};
39
40// NOTE: A loop below assumes that the first value of this enum is 0 and all
41// other values are incremental.
42enum ServiceType {
43 STUN = 0, // Indicates a STUN server.
44 STUNS, // Indicates a STUN server used with a TLS session.
45 TURN, // Indicates a TURN server
46 TURNS, // Indicates a TURN server used with a TLS session.
47 INVALID, // Unknown.
48};
49static_assert(INVALID == arraysize(kValidIceServiceTypes),
50 "kValidIceServiceTypes must have as many strings as ServiceType "
51 "has values.");
52
53// |in_str| should be of format
54// stunURI = scheme ":" stun-host [ ":" stun-port ]
55// scheme = "stun" / "stuns"
56// stun-host = IP-literal / IPv4address / reg-name
57// stun-port = *DIGIT
58//
59// draft-petithuguenin-behave-turn-uris-01
60// turnURI = scheme ":" turn-host [ ":" turn-port ]
61// turn-host = username@IP-literal / IPv4address / reg-name
62static bool GetServiceTypeAndHostnameFromUri(const std::string& in_str,
63 ServiceType* service_type,
64 std::string* hostname) {
65 const std::string::size_type colonpos = in_str.find(':');
66 if (colonpos == std::string::npos) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010067 RTC_LOG(LS_WARNING) << "Missing ':' in ICE URI: " << in_str;
deadbeef1dcb1642017-03-29 21:08:16 -070068 return false;
69 }
70 if ((colonpos + 1) == in_str.length()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010071 RTC_LOG(LS_WARNING) << "Empty hostname in ICE URI: " << in_str;
deadbeef1dcb1642017-03-29 21:08:16 -070072 return false;
73 }
74 *service_type = INVALID;
75 for (size_t i = 0; i < arraysize(kValidIceServiceTypes); ++i) {
76 if (in_str.compare(0, colonpos, kValidIceServiceTypes[i]) == 0) {
77 *service_type = static_cast<ServiceType>(i);
78 break;
79 }
80 }
81 if (*service_type == INVALID) {
82 return false;
83 }
84 *hostname = in_str.substr(colonpos + 1, std::string::npos);
85 return true;
86}
87
88static bool ParsePort(const std::string& in_str, int* port) {
89 // Make sure port only contains digits. FromString doesn't check this.
90 for (const char& c : in_str) {
91 if (!std::isdigit(c)) {
92 return false;
93 }
94 }
95 return rtc::FromString(in_str, port);
96}
97
98// This method parses IPv6 and IPv4 literal strings, along with hostnames in
99// standard hostname:port format.
100// Consider following formats as correct.
101// |hostname:port|, |[IPV6 address]:port|, |IPv4 address|:port,
102// |hostname|, |[IPv6 address]|, |IPv4 address|.
103static bool ParseHostnameAndPortFromString(const std::string& in_str,
104 std::string* host,
105 int* port) {
106 RTC_DCHECK(host->empty());
107 if (in_str.at(0) == '[') {
108 std::string::size_type closebracket = in_str.rfind(']');
109 if (closebracket != std::string::npos) {
110 std::string::size_type colonpos = in_str.find(':', closebracket);
111 if (std::string::npos != colonpos) {
112 if (!ParsePort(in_str.substr(closebracket + 2, std::string::npos),
113 port)) {
114 return false;
115 }
116 }
117 *host = in_str.substr(1, closebracket - 1);
118 } else {
119 return false;
120 }
121 } else {
122 std::string::size_type colonpos = in_str.find(':');
123 if (std::string::npos != colonpos) {
124 if (!ParsePort(in_str.substr(colonpos + 1, std::string::npos), port)) {
125 return false;
126 }
127 *host = in_str.substr(0, colonpos);
128 } else {
129 *host = in_str;
130 }
131 }
132 return !host->empty();
133}
134
135// Adds a STUN or TURN server to the appropriate list,
136// by parsing |url| and using the username/password in |server|.
137static RTCErrorType ParseIceServerUrl(
138 const PeerConnectionInterface::IceServer& server,
139 const std::string& url,
140 cricket::ServerAddresses* stun_servers,
141 std::vector<cricket::RelayServerConfig>* turn_servers) {
142 // draft-nandakumar-rtcweb-stun-uri-01
143 // stunURI = scheme ":" stun-host [ ":" stun-port ]
144 // scheme = "stun" / "stuns"
145 // stun-host = IP-literal / IPv4address / reg-name
146 // stun-port = *DIGIT
147
148 // draft-petithuguenin-behave-turn-uris-01
149 // turnURI = scheme ":" turn-host [ ":" turn-port ]
150 // [ "?transport=" transport ]
151 // scheme = "turn" / "turns"
152 // transport = "udp" / "tcp" / transport-ext
153 // transport-ext = 1*unreserved
154 // turn-host = IP-literal / IPv4address / reg-name
155 // turn-port = *DIGIT
156 RTC_DCHECK(stun_servers != nullptr);
157 RTC_DCHECK(turn_servers != nullptr);
158 std::vector<std::string> tokens;
159 cricket::ProtocolType turn_transport_type = cricket::PROTO_UDP;
160 RTC_DCHECK(!url.empty());
161 rtc::tokenize_with_empty_tokens(url, '?', &tokens);
162 std::string uri_without_transport = tokens[0];
163 // Let's look into transport= param, if it exists.
164 if (tokens.size() == kTurnTransportTokensNum) { // ?transport= is present.
165 std::string uri_transport_param = tokens[1];
166 rtc::tokenize_with_empty_tokens(uri_transport_param, '=', &tokens);
167 if (tokens[0] != kTransport) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100168 RTC_LOG(LS_WARNING) << "Invalid transport parameter key.";
deadbeef1dcb1642017-03-29 21:08:16 -0700169 return RTCErrorType::SYNTAX_ERROR;
170 }
171 if (tokens.size() < 2) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100172 RTC_LOG(LS_WARNING) << "Transport parameter missing value.";
deadbeef1dcb1642017-03-29 21:08:16 -0700173 return RTCErrorType::SYNTAX_ERROR;
174 }
175 if (!cricket::StringToProto(tokens[1].c_str(), &turn_transport_type) ||
176 (turn_transport_type != cricket::PROTO_UDP &&
177 turn_transport_type != cricket::PROTO_TCP)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100178 RTC_LOG(LS_WARNING) << "Transport parameter should always be udp or tcp.";
deadbeef1dcb1642017-03-29 21:08:16 -0700179 return RTCErrorType::SYNTAX_ERROR;
180 }
181 }
182
183 std::string hoststring;
184 ServiceType service_type;
185 if (!GetServiceTypeAndHostnameFromUri(uri_without_transport, &service_type,
186 &hoststring)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100187 RTC_LOG(LS_WARNING) << "Invalid transport parameter in ICE URI: " << url;
deadbeef1dcb1642017-03-29 21:08:16 -0700188 return RTCErrorType::SYNTAX_ERROR;
189 }
190
191 // GetServiceTypeAndHostnameFromUri should never give an empty hoststring
192 RTC_DCHECK(!hoststring.empty());
193
194 // Let's break hostname.
195 tokens.clear();
196 rtc::tokenize_with_empty_tokens(hoststring, '@', &tokens);
197
198 std::string username(server.username);
199 if (tokens.size() > kTurnHostTokensNum) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100200 RTC_LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring;
deadbeef1dcb1642017-03-29 21:08:16 -0700201 return RTCErrorType::SYNTAX_ERROR;
202 }
203 if (tokens.size() == kTurnHostTokensNum) {
204 if (tokens[0].empty() || tokens[1].empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100205 RTC_LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring;
deadbeef1dcb1642017-03-29 21:08:16 -0700206 return RTCErrorType::SYNTAX_ERROR;
207 }
208 username.assign(rtc::s_url_decode(tokens[0]));
209 hoststring = tokens[1];
210 } else {
211 hoststring = tokens[0];
212 }
213
214 int port = kDefaultStunPort;
215 if (service_type == TURNS) {
216 port = kDefaultStunTlsPort;
217 turn_transport_type = cricket::PROTO_TLS;
218 }
219
220 std::string address;
221 if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100222 RTC_LOG(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) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100227 RTC_LOG(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: {
238 if (username.empty() || server.password.empty()) {
239 // The WebRTC spec requires throwing an InvalidAccessError when username
240 // or credential are ommitted; this is the native equivalent.
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200241 RTC_LOG(LS_ERROR) << "TURN URL without username, or password empty";
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.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100255 RTC_LOG(LS_ERROR)
256 << "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 }
deadbeef1dcb1642017-03-29 21:08:16 -0700262 cricket::RelayServerConfig config = cricket::RelayServerConfig(
Emad Omaradab1d2d2017-06-16 15:43:11 -0700263 socket_address, username, 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.
278 RTC_NOTREACHED() << "Unexpected service type";
279 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()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100292 RTC_LOG(LS_ERROR) << "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 {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100309 RTC_LOG(LS_ERROR) << "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