blob: 3d05d66b13c683d1169b0c157508c743201ff571 [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"
deadbeef1dcb1642017-03-29 21:08:16 -070023
24namespace webrtc {
25
deadbeef1dcb1642017-03-29 21:08:16 -070026// Number of tokens must be preset when TURN uri has transport param.
27static const size_t kTurnTransportTokensNum = 2;
28// The default stun port.
29static const int kDefaultStunPort = 3478;
30static const int kDefaultStunTlsPort = 5349;
31static const char kTransport[] = "transport";
32
33// NOTE: Must be in the same order as the ServiceType enum.
34static const char* kValidIceServiceTypes[] = {"stun", "stuns", "turn", "turns"};
35
36// NOTE: A loop below assumes that the first value of this enum is 0 and all
37// other values are incremental.
38enum ServiceType {
39 STUN = 0, // Indicates a STUN server.
40 STUNS, // Indicates a STUN server used with a TLS session.
41 TURN, // Indicates a TURN server
42 TURNS, // Indicates a TURN server used with a TLS session.
43 INVALID, // Unknown.
44};
45static_assert(INVALID == arraysize(kValidIceServiceTypes),
46 "kValidIceServiceTypes must have as many strings as ServiceType "
47 "has values.");
48
Niels Möllerdb4def92019-03-18 16:53:59 +010049// |in_str| should follow of RFC 7064/7065 syntax, but with an optional
50// "?transport=" already stripped. I.e.,
51// stunURI = scheme ":" host [ ":" port ]
52// scheme = "stun" / "stuns" / "turn" / "turns"
53// host = IP-literal / IPv4address / reg-name
54// port = *DIGIT
deadbeef1dcb1642017-03-29 21:08:16 -070055static bool GetServiceTypeAndHostnameFromUri(const std::string& in_str,
56 ServiceType* service_type,
57 std::string* hostname) {
58 const std::string::size_type colonpos = in_str.find(':');
59 if (colonpos == std::string::npos) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010060 RTC_LOG(LS_WARNING) << "Missing ':' in ICE URI: " << in_str;
deadbeef1dcb1642017-03-29 21:08:16 -070061 return false;
62 }
63 if ((colonpos + 1) == in_str.length()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010064 RTC_LOG(LS_WARNING) << "Empty hostname in ICE URI: " << in_str;
deadbeef1dcb1642017-03-29 21:08:16 -070065 return false;
66 }
67 *service_type = INVALID;
68 for (size_t i = 0; i < arraysize(kValidIceServiceTypes); ++i) {
69 if (in_str.compare(0, colonpos, kValidIceServiceTypes[i]) == 0) {
70 *service_type = static_cast<ServiceType>(i);
71 break;
72 }
73 }
74 if (*service_type == INVALID) {
75 return false;
76 }
77 *hostname = in_str.substr(colonpos + 1, std::string::npos);
78 return true;
79}
80
81static bool ParsePort(const std::string& in_str, int* port) {
82 // Make sure port only contains digits. FromString doesn't check this.
83 for (const char& c : in_str) {
84 if (!std::isdigit(c)) {
85 return false;
86 }
87 }
88 return rtc::FromString(in_str, port);
89}
90
91// This method parses IPv6 and IPv4 literal strings, along with hostnames in
92// standard hostname:port format.
93// Consider following formats as correct.
94// |hostname:port|, |[IPV6 address]:port|, |IPv4 address|:port,
95// |hostname|, |[IPv6 address]|, |IPv4 address|.
96static bool ParseHostnameAndPortFromString(const std::string& in_str,
97 std::string* host,
98 int* port) {
99 RTC_DCHECK(host->empty());
100 if (in_str.at(0) == '[') {
101 std::string::size_type closebracket = in_str.rfind(']');
102 if (closebracket != std::string::npos) {
103 std::string::size_type colonpos = in_str.find(':', closebracket);
104 if (std::string::npos != colonpos) {
105 if (!ParsePort(in_str.substr(closebracket + 2, std::string::npos),
106 port)) {
107 return false;
108 }
109 }
110 *host = in_str.substr(1, closebracket - 1);
111 } else {
112 return false;
113 }
114 } else {
115 std::string::size_type colonpos = in_str.find(':');
116 if (std::string::npos != colonpos) {
117 if (!ParsePort(in_str.substr(colonpos + 1, std::string::npos), port)) {
118 return false;
119 }
120 *host = in_str.substr(0, colonpos);
121 } else {
122 *host = in_str;
123 }
124 }
125 return !host->empty();
126}
127
128// Adds a STUN or TURN server to the appropriate list,
129// by parsing |url| and using the username/password in |server|.
130static RTCErrorType ParseIceServerUrl(
131 const PeerConnectionInterface::IceServer& server,
132 const std::string& url,
133 cricket::ServerAddresses* stun_servers,
134 std::vector<cricket::RelayServerConfig>* turn_servers) {
Niels Möllerdb4def92019-03-18 16:53:59 +0100135 // RFC 7064
136 // stunURI = scheme ":" host [ ":" port ]
deadbeef1dcb1642017-03-29 21:08:16 -0700137 // scheme = "stun" / "stuns"
deadbeef1dcb1642017-03-29 21:08:16 -0700138
Niels Möllerdb4def92019-03-18 16:53:59 +0100139 // RFC 7065
140 // turnURI = scheme ":" host [ ":" port ]
deadbeef1dcb1642017-03-29 21:08:16 -0700141 // [ "?transport=" transport ]
142 // scheme = "turn" / "turns"
143 // transport = "udp" / "tcp" / transport-ext
144 // transport-ext = 1*unreserved
Niels Möllerdb4def92019-03-18 16:53:59 +0100145
146 // RFC 3986
147 // host = IP-literal / IPv4address / reg-name
148 // port = *DIGIT
149
deadbeef1dcb1642017-03-29 21:08:16 -0700150 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
deadbeef1dcb1642017-03-29 21:08:16 -0700188 int port = kDefaultStunPort;
189 if (service_type == TURNS) {
190 port = kDefaultStunTlsPort;
191 turn_transport_type = cricket::PROTO_TLS;
192 }
193
Niels Möllerdb4def92019-03-18 16:53:59 +0100194 if (hoststring.find('@') != std::string::npos) {
195 RTC_LOG(WARNING) << "Invalid url: " << uri_without_transport;
196 RTC_LOG(WARNING)
197 << "Note that user-info@ in turn:-urls is long-deprecated.";
198 return RTCErrorType::SYNTAX_ERROR;
199 }
deadbeef1dcb1642017-03-29 21:08:16 -0700200 std::string address;
201 if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100202 RTC_LOG(WARNING) << "Invalid hostname format: " << uri_without_transport;
deadbeef1dcb1642017-03-29 21:08:16 -0700203 return RTCErrorType::SYNTAX_ERROR;
204 }
205
206 if (port <= 0 || port > 0xffff) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100207 RTC_LOG(WARNING) << "Invalid port: " << port;
deadbeef1dcb1642017-03-29 21:08:16 -0700208 return RTCErrorType::SYNTAX_ERROR;
209 }
210
211 switch (service_type) {
212 case STUN:
213 case STUNS:
214 stun_servers->insert(rtc::SocketAddress(address, port));
215 break;
216 case TURN:
217 case TURNS: {
Niels Möllerdb4def92019-03-18 16:53:59 +0100218 if (server.username.empty() || server.password.empty()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700219 // The WebRTC spec requires throwing an InvalidAccessError when username
220 // or credential are ommitted; this is the native equivalent.
Niels Möllerdb4def92019-03-18 16:53:59 +0100221 RTC_LOG(LS_ERROR) << "TURN server with empty username or password";
deadbeef1dcb1642017-03-29 21:08:16 -0700222 return RTCErrorType::INVALID_PARAMETER;
223 }
Emad Omaradab1d2d2017-06-16 15:43:11 -0700224 // If the hostname field is not empty, then the server address must be
225 // the resolved IP for that host, the hostname is needed later for TLS
226 // handshake (SNI and Certificate verification).
227 const std::string& hostname =
228 server.hostname.empty() ? address : server.hostname;
229 rtc::SocketAddress socket_address(hostname, port);
230 if (!server.hostname.empty()) {
231 rtc::IPAddress ip;
232 if (!IPFromString(address, &ip)) {
233 // When hostname is set, the server address must be a
234 // resolved ip address.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100235 RTC_LOG(LS_ERROR)
236 << "IceServer has hostname field set, but URI does not "
237 "contain an IP address.";
Emad Omaradab1d2d2017-06-16 15:43:11 -0700238 return RTCErrorType::INVALID_PARAMETER;
239 }
240 socket_address.SetResolvedIP(ip);
241 }
Niels Möllerdb4def92019-03-18 16:53:59 +0100242 cricket::RelayServerConfig config =
243 cricket::RelayServerConfig(socket_address, server.username,
244 server.password, turn_transport_type);
deadbeef1dcb1642017-03-29 21:08:16 -0700245 if (server.tls_cert_policy ==
246 PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck) {
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000247 config.tls_cert_policy =
248 cricket::TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK;
deadbeef1dcb1642017-03-29 21:08:16 -0700249 }
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000250 config.tls_alpn_protocols = server.tls_alpn_protocols;
251 config.tls_elliptic_curves = server.tls_elliptic_curves;
Diogo Real1dca9d52017-08-29 12:18:32 -0700252
deadbeef1dcb1642017-03-29 21:08:16 -0700253 turn_servers->push_back(config);
254 break;
255 }
256 default:
257 // We shouldn't get to this point with an invalid service_type, we should
258 // have returned an error already.
259 RTC_NOTREACHED() << "Unexpected service type";
260 return RTCErrorType::INTERNAL_ERROR;
261 }
262 return RTCErrorType::NONE;
263}
264
265RTCErrorType ParseIceServers(
266 const PeerConnectionInterface::IceServers& servers,
267 cricket::ServerAddresses* stun_servers,
268 std::vector<cricket::RelayServerConfig>* turn_servers) {
269 for (const PeerConnectionInterface::IceServer& server : servers) {
270 if (!server.urls.empty()) {
271 for (const std::string& url : server.urls) {
272 if (url.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100273 RTC_LOG(LS_ERROR) << "Empty uri.";
deadbeef1dcb1642017-03-29 21:08:16 -0700274 return RTCErrorType::SYNTAX_ERROR;
275 }
276 RTCErrorType err =
277 ParseIceServerUrl(server, url, stun_servers, turn_servers);
278 if (err != RTCErrorType::NONE) {
279 return err;
280 }
281 }
282 } else if (!server.uri.empty()) {
283 // Fallback to old .uri if new .urls isn't present.
284 RTCErrorType err =
285 ParseIceServerUrl(server, server.uri, stun_servers, turn_servers);
286 if (err != RTCErrorType::NONE) {
287 return err;
288 }
289 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100290 RTC_LOG(LS_ERROR) << "Empty uri.";
deadbeef1dcb1642017-03-29 21:08:16 -0700291 return RTCErrorType::SYNTAX_ERROR;
292 }
293 }
294 // Candidates must have unique priorities, so that connectivity checks
295 // are performed in a well-defined order.
296 int priority = static_cast<int>(turn_servers->size() - 1);
297 for (cricket::RelayServerConfig& turn_server : *turn_servers) {
298 // First in the list gets highest priority.
299 turn_server.priority = priority--;
300 }
301 return RTCErrorType::NONE;
302}
303
304} // namespace webrtc