| 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 |
| 11 | #include "pc/ice_server_parsing.h" |
| 12 |
| 13 | #include <stddef.h> |
| 14 |
| 15 | #include <cctype> // For std::isdigit. |
| 16 | #include <string> |
| 17 | #include <tuple> |
| 18 |
| 19 | #include "p2p/base/port_interface.h" |
| 20 | #include "rtc_base/arraysize.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/ip_address.h" |
| 23 | #include "rtc_base/logging.h" |
| 24 | #include "rtc_base/socket_address.h" |
| 25 | #include "rtc_base/string_encode.h" |
| 26 | #include "rtc_base/string_to_number.h" |
| 27 |
| 28 | namespace webrtc { |
| 29 |
| 30 | namespace { |
| 31 | // Number of tokens must be preset when TURN uri has transport param. |
| 32 | const size_t kTurnTransportTokensNum = 2; |
| 33 | // The default stun port. |
| 34 | const int kDefaultStunPort = 3478; |
| 35 | const int kDefaultStunTlsPort = 5349; |
| 36 | const char kTransport[] = "transport"; |
| 37 |
| 38 | // Allowed characters in hostname per RFC 3986 Appendix A "reg-name" |
| 39 | const char kRegNameCharacters[] = |
| 40 | "abcdefghijklmnopqrstuvwxyz" |
| 41 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 42 | "0123456789" |
| 43 | "-._~" // unreserved |
| 44 | "%" // pct-encoded |
| 45 | "!$&'()*+,;="; // sub-delims |
| 46 |
| 47 | // NOTE: Must be in the same order as the ServiceType enum. |
| 48 | const char* kValidIceServiceTypes[] = {"stun", "stuns", "turn", "turns"}; |
| 49 |
| 50 | // NOTE: A loop below assumes that the first value of this enum is 0 and all |
| 51 | // other values are incremental. |
| 52 | enum class ServiceType { |
| 53 | STUN = 0, // Indicates a STUN server. |
| 54 | STUNS, // Indicates a STUN server used with a TLS session. |
| 55 | TURN, // Indicates a TURN server |
| 56 | TURNS, // Indicates a TURN server used with a TLS session. |
| 57 | INVALID, // Unknown. |
| 58 | }; |
| 59 | static_assert(static_cast<size_t>(ServiceType::INVALID) == |
| 60 | arraysize(kValidIceServiceTypes), |
| 61 | "kValidIceServiceTypes must have as many strings as ServiceType " |
| 62 | "has values."); |
| 63 |
| 64 | // `in_str` should follow of RFC 7064/7065 syntax, but with an optional |
| 65 | // "?transport=" already stripped. I.e., |
| 66 | // stunURI = scheme ":" host [ ":" port ] |
| 67 | // scheme = "stun" / "stuns" / "turn" / "turns" |
| 68 | // host = IP-literal / IPv4address / reg-name |
| 69 | // port = *DIGIT |
| 70 |
| 71 | // Return tuple is service_type, host, with service_type == ServiceType::INVALID |
| 72 | // on failure. |
| 73 | std::tuple<ServiceType, absl::string_view> GetServiceTypeAndHostnameFromUri( |
| 74 | absl::string_view in_str) { |
| 75 | const auto colonpos = in_str.find(':'); |
| 76 | if (colonpos == absl::string_view::npos) { |
| 77 | RTC_LOG(LS_WARNING) << "Missing ':' in ICE URI: " << in_str; |
| 78 | return {ServiceType::INVALID, ""}; |
| 79 | } |
| 80 | if ((colonpos + 1) == in_str.length()) { |
| 81 | RTC_LOG(LS_WARNING) << "Empty hostname in ICE URI: " << in_str; |
| 82 | return {ServiceType::INVALID, ""}; |
| 83 | } |
| 84 | for (size_t i = 0; i < arraysize(kValidIceServiceTypes); ++i) { |
| 85 | if (in_str.compare(0, colonpos, kValidIceServiceTypes[i]) == 0) { |
| 86 | return {static_cast<ServiceType>(i), in_str.substr(colonpos + 1)}; |
| 87 | } |
| 88 | } |
| 89 | return {ServiceType::INVALID, ""}; |
| 90 | } |
| 91 |
| 92 | absl::optional<int> ParsePort(absl::string_view in_str) { |
| 93 | // Make sure port only contains digits. StringToNumber doesn't check this. |
| 94 | for (const char& c : in_str) { |
| 95 | if (!std::isdigit(static_cast<unsigned char>(c))) { |
| 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | return rtc::StringToNumber<int>(in_str); |
| 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. |
| 105 | // `hostname:port`, |[IPV6 address]:port|, |IPv4 address|:port, |
| 106 | // `hostname`, |[IPv6 address]|, |IPv4 address|. |
| 107 |
| 108 | // Return tuple is success, host, port. |
| 109 | std::tuple<bool, absl::string_view, int> ParseHostnameAndPortFromString( |
| 110 | absl::string_view in_str, |
| 111 | int default_port) { |
| 112 | if (in_str.empty()) { |
| 113 | return {false, "", 0}; |
| 114 | } |
| 115 | absl::string_view host; |
| 116 | int port = default_port; |
| 117 |
| 118 | if (in_str.at(0) == '[') { |
| 119 | // IP_literal syntax |
| 120 | auto closebracket = in_str.rfind(']'); |
| 121 | if (closebracket == absl::string_view::npos) { |
| 122 | return {false, "", 0}; |
| 123 | } |
| 124 | auto colonpos = in_str.find(':', closebracket); |
| 125 | if (absl::string_view::npos != colonpos) { |
| 126 | if (absl::optional<int> opt_port = |
| 127 | ParsePort(in_str.substr(closebracket + 2))) { |
| 128 | port = *opt_port; |
| 129 | } else { |
| 130 | return {false, "", 0}; |
| 131 | } |
| 132 | } |
| 133 | host = in_str.substr(1, closebracket - 1); |
| 134 | } else { |
| 135 | // IPv4address or reg-name syntax |
| 136 | auto colonpos = in_str.find(':'); |
| 137 | if (absl::string_view::npos != colonpos) { |
| 138 | if (absl::optional<int> opt_port = |
| 139 | ParsePort(in_str.substr(colonpos + 1))) { |
| 140 | port = *opt_port; |
| 141 | } else { |
| 142 | return {false, "", 0}; |
| 143 | } |
| 144 | host = in_str.substr(0, colonpos); |
| 145 | } else { |
| 146 | host = in_str; |
| 147 | } |
| 148 | // RFC 3986 section 3.2.2 and Appendix A - "reg-name" syntax |
| 149 | if (host.find_first_not_of(kRegNameCharacters) != absl::string_view::npos) { |
| 150 | return {false, "", 0}; |
| 151 | } |
| 152 | } |
| 153 | return {!host.empty(), host, port}; |
| 154 | } |
| 155 |
| 156 | // Adds a STUN or TURN server to the appropriate list, |
| 157 | // by parsing `url` and using the username/password in `server`. |
| 158 | RTCError ParseIceServerUrl( |
| 159 | const PeerConnectionInterface::IceServer& server, |
| 160 | absl::string_view url, |
| 161 | cricket::ServerAddresses* stun_servers, |
| 162 | std::vector<cricket::RelayServerConfig>* turn_servers) { |
| 163 | // RFC 7064 |
| 164 | // stunURI = scheme ":" host [ ":" port ] |
| 165 | // scheme = "stun" / "stuns" |
| 166 |
| 167 | // RFC 7065 |
| 168 | // turnURI = scheme ":" host [ ":" port ] |
| 169 | // [ "?transport=" transport ] |
| 170 | // scheme = "turn" / "turns" |
| 171 | // transport = "udp" / "tcp" / transport-ext |
| 172 | // transport-ext = 1*unreserved |
| 173 |
| 174 | // RFC 3986 |
| 175 | // host = IP-literal / IPv4address / reg-name |
| 176 | // port = *DIGIT |
| 177 |
| 178 | RTC_DCHECK(stun_servers != nullptr); |
| 179 | RTC_DCHECK(turn_servers != nullptr); |
| 180 | cricket::ProtocolType turn_transport_type = cricket::PROTO_UDP; |
| 181 | RTC_DCHECK(!url.empty()); |
| 182 | std::vector<absl::string_view> tokens = rtc::split(url, '?'); |
| 183 | absl::string_view uri_without_transport = tokens[0]; |
| 184 | // Let's look into transport= param, if it exists. |
| 185 | if (tokens.size() == kTurnTransportTokensNum) { // ?transport= is present. |
| 186 | std::vector<absl::string_view> transport_tokens = |
| 187 | rtc::split(tokens[1], '='); |
| 188 | if (transport_tokens[0] != kTransport) { |
| 189 | LOG_AND_RETURN_ERROR( |
| 190 | RTCErrorType::SYNTAX_ERROR, |
| 191 | "ICE server parsing failed: Invalid transport parameter key."); |
| 192 | } |
| 193 | if (transport_tokens.size() < 2) { |
| 194 | LOG_AND_RETURN_ERROR( |
| 195 | RTCErrorType::SYNTAX_ERROR, |
| 196 | "ICE server parsing failed: Transport parameter missing value."); |
| 197 | } |
| 198 |
| 199 | absl::optional<cricket::ProtocolType> proto = |
| 200 | cricket::StringToProto(transport_tokens[1]); |
| 201 | if (!proto || |
| 202 | (*proto != cricket::PROTO_UDP && *proto != cricket::PROTO_TCP)) { |
| 203 | LOG_AND_RETURN_ERROR( |
| 204 | RTCErrorType::SYNTAX_ERROR, |
| 205 | "ICE server parsing failed: Transport parameter should " |
| 206 | "always be udp or tcp."); |
| 207 | } |
| 208 | turn_transport_type = *proto; |
| 209 | } |
| 210 |
| 211 | auto [service_type, hoststring] = |
| 212 | GetServiceTypeAndHostnameFromUri(uri_without_transport); |
| 213 | if (service_type == ServiceType::INVALID) { |
| 214 | RTC_LOG(LS_ERROR) << "Invalid transport parameter in ICE URI: " << url; |
| 215 | LOG_AND_RETURN_ERROR( |
| 216 | RTCErrorType::SYNTAX_ERROR, |
| 217 | "ICE server parsing failed: Invalid transport parameter in ICE URI"); |
| 218 | } |
| 219 |
| 220 | // GetServiceTypeAndHostnameFromUri should never give an empty hoststring |
| 221 | RTC_DCHECK(!hoststring.empty()); |
| 222 |
| 223 | // stun with ?transport (or any ?) is not valid. |
| 224 | if ((service_type == ServiceType::STUN || |
| 225 | service_type == ServiceType::STUNS) && |
| 226 | tokens.size() > 1) { |
| 227 | LOG_AND_RETURN_ERROR( |
| 228 | RTCErrorType::SYNTAX_ERROR, |
| 229 | "ICE server parsing failed: Invalid stun url with query parameters"); |
| 230 | } |
| 231 |
| 232 | int default_port = kDefaultStunPort; |
| 233 | if (service_type == ServiceType::TURNS) { |
| 234 | default_port = kDefaultStunTlsPort; |
| 235 | turn_transport_type = cricket::PROTO_TLS; |
| 236 | } |
| 237 |
| 238 | if (hoststring.find('@') != absl::string_view::npos) { |
| 239 | RTC_LOG(LS_ERROR) << "Invalid url with long deprecated user@host syntax: " |
| 240 | << uri_without_transport; |
| 241 | LOG_AND_RETURN_ERROR(RTCErrorType::SYNTAX_ERROR, |
| 242 | "ICE server parsing failed: Invalid url with long " |
| 243 | "deprecated user@host syntax"); |
| 244 | } |
| 245 |
| 246 | auto [success, address, port] = |
| 247 | ParseHostnameAndPortFromString(hoststring, default_port); |
| 248 | if (!success) { |
| 249 | RTC_LOG(LS_ERROR) << "Invalid hostname format: " << uri_without_transport; |
| 250 | LOG_AND_RETURN_ERROR(RTCErrorType::SYNTAX_ERROR, |
| 251 | "ICE server parsing failed: Invalid hostname format"); |
| 252 | } |
| 253 |
| 254 | if (port <= 0 || port > 0xffff) { |
| 255 | RTC_LOG(LS_ERROR) << "Invalid port: " << port; |
| 256 | LOG_AND_RETURN_ERROR(RTCErrorType::SYNTAX_ERROR, |
| 257 | "ICE server parsing failed: Invalid port"); |
| 258 | } |
| 259 |
| 260 | switch (service_type) { |
| 261 | case ServiceType::STUN: |
| 262 | case ServiceType::STUNS: |
| 263 | stun_servers->insert(rtc::SocketAddress(address, port)); |
| 264 | break; |
| 265 | case ServiceType::TURN: |
| 266 | case ServiceType::TURNS: { |
| 267 | if (server.username.empty() || server.password.empty()) { |
| 268 | // The WebRTC spec requires throwing an InvalidAccessError when username |
| 269 | // or credential are ommitted; this is the native equivalent. |
| 270 | LOG_AND_RETURN_ERROR( |
| 271 | RTCErrorType::INVALID_PARAMETER, |
| 272 | "ICE server parsing failed: TURN server with empty " |
| 273 | "username or password"); |
| 274 | } |
| 275 | // If the hostname field is not empty, then the server address must be |
| 276 | // the resolved IP for that host, the hostname is needed later for TLS |
| 277 | // handshake (SNI and Certificate verification). |
| 278 | absl::string_view hostname = |
| 279 | server.hostname.empty() ? address : server.hostname; |
| 280 | rtc::SocketAddress socket_address(hostname, port); |
| 281 | if (!server.hostname.empty()) { |
| 282 | rtc::IPAddress ip; |
| 283 | if (!IPFromString(address, &ip)) { |
| 284 | // When hostname is set, the server address must be a |
| 285 | // resolved ip address. |
| 286 | LOG_AND_RETURN_ERROR( |
| 287 | RTCErrorType::INVALID_PARAMETER, |
| 288 | "ICE server parsing failed: " |
| 289 | "IceServer has hostname field set, but URI does not " |
| 290 | "contain an IP address."); |
| 291 | } |
| 292 | socket_address.SetResolvedIP(ip); |
| 293 | } |
| 294 | cricket::RelayServerConfig config = |
| 295 | cricket::RelayServerConfig(socket_address, server.username, |
| 296 | server.password, turn_transport_type); |
| 297 | if (server.tls_cert_policy == |
| 298 | PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck) { |
| 299 | config.tls_cert_policy = |
| 300 | cricket::TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK; |
| 301 | } |
| 302 | config.tls_alpn_protocols = server.tls_alpn_protocols; |
| 303 | config.tls_elliptic_curves = server.tls_elliptic_curves; |
| 304 |
| 305 | turn_servers->push_back(config); |
| 306 | break; |
| 307 | } |
| 308 | default: |
| 309 | // We shouldn't get to this point with an invalid service_type, we should |
| 310 | // have returned an error already. |
| 311 | LOG_AND_RETURN_ERROR( |
| 312 | RTCErrorType::INTERNAL_ERROR, |
| 313 | "ICE server parsing failed: Unexpected service type"); |
| 314 | } |
| 315 | return RTCError::OK(); |
| 316 | } |
| 317 |
| 318 | } // namespace |
| 319 |
| 320 | RTCError ParseIceServersOrError( |
| 321 | const PeerConnectionInterface::IceServers& servers, |
| 322 | cricket::ServerAddresses* stun_servers, |
| 323 | std::vector<cricket::RelayServerConfig>* turn_servers) { |
| 324 | for (const PeerConnectionInterface::IceServer& server : servers) { |
| 325 | if (!server.urls.empty()) { |
| 326 | for (const std::string& url : server.urls) { |
| 327 | if (url.empty()) { |
| 328 | LOG_AND_RETURN_ERROR(RTCErrorType::SYNTAX_ERROR, |
| 329 | "ICE server parsing failed: Empty uri."); |
| 330 | } |
| 331 | RTCError err = |
| 332 | ParseIceServerUrl(server, url, stun_servers, turn_servers); |
| 333 | if (!err.ok()) { |
| 334 | return err; |
| 335 | } |
| 336 | } |
| 337 | } else if (!server.uri.empty()) { |
| 338 | // Fallback to old .uri if new .urls isn't present. |
| 339 | RTCError err = |
| 340 | ParseIceServerUrl(server, server.uri, stun_servers, turn_servers); |
| 341 |
| 342 | if (!err.ok()) { |
| 343 | return err; |
| 344 | } |
| 345 | } else { |
| 346 | LOG_AND_RETURN_ERROR(RTCErrorType::SYNTAX_ERROR, |
| 347 | "ICE server parsing failed: Empty uri."); |
| 348 | } |
| 349 | } |
| 350 | return RTCError::OK(); |
| 351 | } |
| 352 |
| 353 | RTCErrorType ParseIceServers( |
| 354 | const PeerConnectionInterface::IceServers& servers, |
| 355 | cricket::ServerAddresses* stun_servers, |
| 356 | std::vector<cricket::RelayServerConfig>* turn_servers) { |
| 357 | return ParseIceServersOrError(servers, stun_servers, turn_servers).type(); |
| 358 | } |
| 359 |
| 360 | } // namespace webrtc |
| 361 |