Add SSLConfig object to IceServer.

This is a rollforward of https://webrtc-review.googlesource.com/c/src/+/96020,
with the addition of setting the old tlsCertPolicy, tlsAlpnProtocols and
tlsEllipticCurves in the RTCIceServer initializer, for backwards compatibility.

Bug: webrtc:9662
Change-Id: I28706ed4ff5abe3f7f913f105779f0e5412aeac5
Reviewed-on: https://webrtc-review.googlesource.com/98762
Commit-Queue: Diogo Real <diogor@google.com>
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Reviewed-by: Qingsi Wang <qingsi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24696}
diff --git a/sdk/objc/Framework/Headers/WebRTC/RTCSSLConfig.h b/sdk/objc/Framework/Headers/WebRTC/RTCSSLConfig.h
new file mode 100644
index 0000000..b572522
--- /dev/null
+++ b/sdk/objc/Framework/Headers/WebRTC/RTCSSLConfig.h
@@ -0,0 +1,11 @@
+/*
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import "api/peerconnection/RTCSSLConfig.h"
diff --git a/sdk/objc/api/peerconnection/RTCIceServer.h b/sdk/objc/api/peerconnection/RTCIceServer.h
index c2def04..a9760c7 100644
--- a/sdk/objc/api/peerconnection/RTCIceServer.h
+++ b/sdk/objc/api/peerconnection/RTCIceServer.h
@@ -11,11 +11,7 @@
 #import <Foundation/Foundation.h>
 
 #import "RTCMacros.h"
-
-typedef NS_ENUM(NSUInteger, RTCTlsCertPolicy) {
-  RTCTlsCertPolicySecure,
-  RTCTlsCertPolicyInsecureNoCheck
-};
+#import "RTCSSLConfig.h"
 
 NS_ASSUME_NONNULL_BEGIN
 
@@ -32,7 +28,8 @@
 @property(nonatomic, readonly, nullable) NSString *credential;
 
 /**
- * TLS certificate policy to use if this RTCIceServer object is a TURN server.
+  Deprecated. TODO(diogor, webrtc:9673): Remove from API.
+  TLS certificate policy to use if this RTCIceServer object is a TURN server.
  */
 @property(nonatomic, readonly) RTCTlsCertPolicy tlsCertPolicy;
 
@@ -43,15 +40,24 @@
  */
 @property(nonatomic, readonly, nullable) NSString *hostname;
 
-/** List of protocols to be used in the TLS ALPN extension. */
+/**
+  Deprecated. TODO(diogor, webrtc:9673): Remove from API.
+  List of protocols to be used in the TLS ALPN extension.
+  This field will be ignored if also set in RTCSSLConfig.
+ */
 @property(nonatomic, readonly) NSArray<NSString *> *tlsAlpnProtocols;
 
 /**
+  Deprecated. TODO(diogor, webrtc:9673): Remove from API.
   List elliptic curves to be used in the TLS elliptic curves extension.
   Only curve names supported by OpenSSL should be used (eg. "P-256","X25519").
+  This field will be ignored if also set in RTCSSLConfig.
   */
 @property(nonatomic, readonly) NSArray<NSString *> *tlsEllipticCurves;
 
+/** SSL configuration options for any SSL/TLS connections to this IceServer. */
+@property(nonatomic, readonly) RTCSSLConfig *sslConfig;
+
 - (nonnull instancetype)init NS_UNAVAILABLE;
 
 /** Convenience initializer for a server with no authentication (e.g. STUN). */
@@ -106,8 +112,17 @@
                      tlsCertPolicy:(RTCTlsCertPolicy)tlsCertPolicy
                           hostname:(nullable NSString *)hostname
                   tlsAlpnProtocols:(nullable NSArray<NSString *> *)tlsAlpnProtocols
-                 tlsEllipticCurves:(nullable NSArray<NSString *> *)tlsEllipticCurves
-    NS_DESIGNATED_INITIALIZER;
+                 tlsEllipticCurves:(nullable NSArray<NSString *> *)tlsEllipticCurves;
+
+/**
+ * Initialize an RTCIceServer with its associated URLs, optional
+ * username, optional credential, hostname and SSL config.
+ */
+- (instancetype)initWithURLStrings:(NSArray<NSString *> *)urlStrings
+                          username:(nullable NSString *)username
+                        credential:(nullable NSString *)credential
+                          hostname:(nullable NSString *)hostname
+                         sslConfig:(RTCSSLConfig *)sslConfig NS_DESIGNATED_INITIALIZER;
 
 @end
 
diff --git a/sdk/objc/api/peerconnection/RTCIceServer.mm b/sdk/objc/api/peerconnection/RTCIceServer.mm
index 2138e4c..d03fd81 100644
--- a/sdk/objc/api/peerconnection/RTCIceServer.mm
+++ b/sdk/objc/api/peerconnection/RTCIceServer.mm
@@ -9,6 +9,7 @@
  */
 
 #import "RTCIceServer+Private.h"
+#import "RTCSSLConfig+Native.h"
 
 #import "helpers/NSString+StdString.h"
 
@@ -21,6 +22,7 @@
 @synthesize hostname = _hostname;
 @synthesize tlsAlpnProtocols = _tlsAlpnProtocols;
 @synthesize tlsEllipticCurves = _tlsEllipticCurves;
+@synthesize sslConfig = _sslConfig;
 
 - (instancetype)initWithURLStrings:(NSArray<NSString *> *)urlStrings {
   return [self initWithURLStrings:urlStrings
@@ -83,28 +85,50 @@
                           hostname:(NSString *)hostname
                   tlsAlpnProtocols:(NSArray<NSString *> *)tlsAlpnProtocols
                  tlsEllipticCurves:(NSArray<NSString *> *)tlsEllipticCurves {
+  RTCSSLConfig *sslConfig = [[RTCSSLConfig alloc] init];
+  sslConfig.tlsCertPolicy = tlsCertPolicy;
+  sslConfig.tlsALPNProtocols = [[NSArray alloc] initWithArray:tlsAlpnProtocols copyItems:YES];
+  sslConfig.tlsEllipticCurves = [[NSArray alloc] initWithArray:tlsEllipticCurves copyItems:YES];
+  return [self initWithURLStrings:urlStrings
+                         username:username
+                       credential:credential
+                         hostname:hostname
+                        sslConfig:sslConfig];
+}
+
+- (instancetype)initWithURLStrings:(NSArray<NSString *> *)urlStrings
+                          username:(NSString *)username
+                        credential:(NSString *)credential
+                          hostname:(NSString *)hostname
+                         sslConfig:(RTCSSLConfig *)sslConfig {
   NSParameterAssert(urlStrings.count);
   if (self = [super init]) {
     _urlStrings = [[NSArray alloc] initWithArray:urlStrings copyItems:YES];
     _username = [username copy];
     _credential = [credential copy];
-    _tlsCertPolicy = tlsCertPolicy;
     _hostname = [hostname copy];
-    _tlsAlpnProtocols = [[NSArray alloc] initWithArray:tlsAlpnProtocols copyItems:YES];
-    _tlsEllipticCurves = [[NSArray alloc] initWithArray:tlsEllipticCurves copyItems:YES];
+    _sslConfig = sslConfig;
+
+    // TODO(diogor, webrtc:9673): Remove these duplicate assignments.
+    _tlsCertPolicy = sslConfig.tlsCertPolicy;
+    if (sslConfig.tlsALPNProtocols) {
+      _tlsAlpnProtocols = [[NSArray alloc] initWithArray:sslConfig.tlsALPNProtocols copyItems:YES];
+    }
+    if (sslConfig.tlsEllipticCurves) {
+      _tlsEllipticCurves =
+          [[NSArray alloc] initWithArray:sslConfig.tlsEllipticCurves copyItems:YES];
+    }
   }
   return self;
 }
 
 - (NSString *)description {
-  return [NSString stringWithFormat:@"RTCIceServer:\n%@\n%@\n%@\n%@\n%@\n%@\n%@",
+  return [NSString stringWithFormat:@"RTCIceServer:\n%@\n%@\n%@\n%@\n%@",
                                     _urlStrings,
                                     _username,
                                     _credential,
-                                    [self stringForTlsCertPolicy:_tlsCertPolicy],
                                     _hostname,
-                                    _tlsAlpnProtocols,
-                                    _tlsEllipticCurves];
+                                    _sslConfig];
 }
 
 #pragma mark - Private
@@ -149,6 +173,8 @@
           webrtc::PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck;
       break;
   }
+
+  iceServer.ssl_config = [_sslConfig nativeConfig];
   return iceServer;
 }
 
@@ -162,34 +188,38 @@
   NSString *username = [NSString stringForStdString:nativeServer.username];
   NSString *credential = [NSString stringForStdString:nativeServer.password];
   NSString *hostname = [NSString stringForStdString:nativeServer.hostname];
-  NSMutableArray *tlsAlpnProtocols =
-      [NSMutableArray arrayWithCapacity:nativeServer.tls_alpn_protocols.size()];
-  for (auto const &proto : nativeServer.tls_alpn_protocols) {
-    [tlsAlpnProtocols addObject:[NSString stringForStdString:proto]];
-  }
-  NSMutableArray *tlsEllipticCurves =
-      [NSMutableArray arrayWithCapacity:nativeServer.tls_elliptic_curves.size()];
-  for (auto const &curve : nativeServer.tls_elliptic_curves) {
-    [tlsEllipticCurves addObject:[NSString stringForStdString:curve]];
-  }
-  RTCTlsCertPolicy tlsCertPolicy;
+  RTCSSLConfig *sslConfig = [[RTCSSLConfig alloc] initWithNativeConfig:nativeServer.ssl_config];
 
-  switch (nativeServer.tls_cert_policy) {
-    case webrtc::PeerConnectionInterface::kTlsCertPolicySecure:
-      tlsCertPolicy = RTCTlsCertPolicySecure;
-      break;
-    case webrtc::PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck:
-      tlsCertPolicy = RTCTlsCertPolicyInsecureNoCheck;
-      break;
+  if (!nativeServer.ssl_config.tls_alpn_protocols.has_value() &&
+      !nativeServer.tls_alpn_protocols.empty()) {
+    NSMutableArray *tlsALPNProtocols =
+        [NSMutableArray arrayWithCapacity:nativeServer.tls_alpn_protocols.size()];
+    for (auto const &proto : nativeServer.tls_alpn_protocols) {
+      [tlsALPNProtocols addObject:[NSString stringForStdString:proto]];
+    }
+    sslConfig.tlsALPNProtocols = tlsALPNProtocols;
+  }
+
+  if (!nativeServer.ssl_config.tls_elliptic_curves.has_value() &&
+      !nativeServer.tls_elliptic_curves.empty()) {
+    NSMutableArray *tlsEllipticCurves =
+        [NSMutableArray arrayWithCapacity:nativeServer.tls_elliptic_curves.size()];
+    for (auto const &curve : nativeServer.tls_elliptic_curves) {
+      [tlsEllipticCurves addObject:[NSString stringForStdString:curve]];
+    }
+    sslConfig.tlsEllipticCurves = tlsEllipticCurves;
+  }
+
+  if (nativeServer.tls_cert_policy ==
+      webrtc::PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck) {
+    sslConfig.tlsCertPolicy = RTCTlsCertPolicyInsecureNoCheck;
   }
 
   self = [self initWithURLStrings:urls
                          username:username
                        credential:credential
-                    tlsCertPolicy:tlsCertPolicy
                          hostname:hostname
-                 tlsAlpnProtocols:tlsAlpnProtocols
-                tlsEllipticCurves:tlsEllipticCurves];
+                        sslConfig:sslConfig];
   return self;
 }
 
diff --git a/sdk/objc/api/peerconnection/RTCSSLConfig+Native.h b/sdk/objc/api/peerconnection/RTCSSLConfig+Native.h
new file mode 100644
index 0000000..7a38edc
--- /dev/null
+++ b/sdk/objc/api/peerconnection/RTCSSLConfig+Native.h
@@ -0,0 +1,27 @@
+/*
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import "RTCSSLConfig.h"
+
+#include "api/peerconnectioninterface.h"
+#include "rtc_base/ssladapter.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface RTCSSLConfig (Native)
+
+- (rtc::SSLConfig)nativeConfig;
+
+/** Initialize an RTCSSLConfig from a native SSLConfig. */
+- (instancetype)initWithNativeConfig:(const rtc::SSLConfig &)config;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/sdk/objc/api/peerconnection/RTCSSLConfig.h b/sdk/objc/api/peerconnection/RTCSSLConfig.h
new file mode 100644
index 0000000..5421609
--- /dev/null
+++ b/sdk/objc/api/peerconnection/RTCSSLConfig.h
@@ -0,0 +1,56 @@
+/*
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import <Foundation/Foundation.h>
+
+#import <WebRTC/RTCMacros.h>
+
+typedef NS_ENUM(NSUInteger, RTCTlsCertPolicy) {
+  RTCTlsCertPolicySecure,
+  RTCTlsCertPolicyInsecureNoCheck
+};
+
+NS_ASSUME_NONNULL_BEGIN
+
+RTC_EXPORT
+@interface RTCSSLConfig : NSObject
+
+/** Indicates whether to enable OCSP stapling in TLS. */
+@property(nonatomic) BOOL enableOCSPStapling;
+
+/** Indicates whether to enable the signed certificate timestamp extension in TLS. */
+@property(nonatomic) BOOL enableSignedCertTimestamp;
+
+/** Indicates whether to enable the TLS Channel ID extension. */
+@property(nonatomic) BOOL enableTlsChannelId;
+
+/** Indicates whether to enable the TLS GREASE extension. */
+@property(nonatomic) BOOL enableGrease;
+
+/** Indicates how to process TURN server certificates */
+@property(nonatomic) RTCTlsCertPolicy tlsCertPolicy;
+
+/** Highest supported SSL version, as defined in the supported_versions TLS extension. */
+@property(nonatomic, nullable) NSNumber *maxSSLVersion;
+
+/** List of protocols to be used in the TLS ALPN extension. */
+@property(nonatomic, copy, nullable) NSArray<NSString *> *tlsALPNProtocols;
+
+/**
+ List of elliptic curves to be used in the TLS elliptic curves extension.
+ Only curve names supported by OpenSSL should be used (eg. "P-256","X25519").
+ */
+@property(nonatomic, copy, nullable) NSArray<NSString *> *tlsEllipticCurves;
+
+- (instancetype)init;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/sdk/objc/api/peerconnection/RTCSSLConfig.mm b/sdk/objc/api/peerconnection/RTCSSLConfig.mm
new file mode 100644
index 0000000..60ff47c
--- /dev/null
+++ b/sdk/objc/api/peerconnection/RTCSSLConfig.mm
@@ -0,0 +1,134 @@
+/*
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import "RTCSSLConfig+Native.h"
+
+#import "helpers/NSString+StdString.h"
+
+@implementation RTCSSLConfig
+
+@synthesize enableOCSPStapling = _enableOCSPStapling;
+@synthesize enableSignedCertTimestamp = _enableSignedCertTimestamp;
+@synthesize enableTlsChannelId = _enableTlsChannelId;
+@synthesize enableGrease = _enableGrease;
+@synthesize tlsCertPolicy = _tlsCertPolicy;
+@synthesize maxSSLVersion = _maxSSLVersion;
+@synthesize tlsALPNProtocols = _tlsALPNProtocols;
+@synthesize tlsEllipticCurves = _tlsEllipticCurves;
+
+- (instancetype)init {
+  // Copy defaults
+  rtc::SSLConfig config;
+  return [self initWithNativeConfig:config];
+}
+
+- (instancetype)initWithNativeConfig:(const rtc::SSLConfig &)config {
+  if (self = [super init]) {
+    _enableOCSPStapling = config.enable_ocsp_stapling;
+    _enableSignedCertTimestamp = config.enable_signed_cert_timestamp;
+    _enableTlsChannelId = config.enable_tls_channel_id;
+    _enableGrease = config.enable_grease;
+
+    switch (config.tls_cert_policy) {
+      case rtc::TlsCertPolicy::TLS_CERT_POLICY_SECURE:
+        _tlsCertPolicy = RTCTlsCertPolicySecure;
+        break;
+      case rtc::TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK:
+        _tlsCertPolicy = RTCTlsCertPolicyInsecureNoCheck;
+        break;
+    }
+
+    if (config.max_ssl_version) {
+      _maxSSLVersion = [NSNumber numberWithInt:*config.max_ssl_version];
+    }
+    if (config.tls_alpn_protocols) {
+      NSMutableArray *tlsALPNProtocols =
+          [NSMutableArray arrayWithCapacity:config.tls_alpn_protocols.value().size()];
+      for (auto const &proto : config.tls_alpn_protocols.value()) {
+        [tlsALPNProtocols addObject:[NSString stringForStdString:proto]];
+      }
+      _tlsALPNProtocols = tlsALPNProtocols;
+    }
+    if (config.tls_elliptic_curves) {
+      NSMutableArray *tlsEllipticCurves =
+          [NSMutableArray arrayWithCapacity:config.tls_elliptic_curves.value().size()];
+      for (auto const &curve : config.tls_elliptic_curves.value()) {
+        [tlsEllipticCurves addObject:[NSString stringForStdString:curve]];
+      }
+      _tlsEllipticCurves = tlsEllipticCurves;
+    }
+  }
+  return self;
+}
+
+- (NSString *)description {
+  return [NSString stringWithFormat:@"RTCSSLConfig:\n%d\n%d\n%d\n%d\n%@\n%@\n%@\n%@",
+                                    _enableOCSPStapling,
+                                    _enableSignedCertTimestamp,
+                                    _enableTlsChannelId,
+                                    _enableGrease,
+                                    [self stringForTlsCertPolicy:_tlsCertPolicy],
+                                    _maxSSLVersion,
+                                    _tlsALPNProtocols,
+                                    _tlsEllipticCurves];
+}
+
+#pragma mark - Private
+
+- (NSString *)stringForTlsCertPolicy:(RTCTlsCertPolicy)tlsCertPolicy {
+  switch (tlsCertPolicy) {
+    case RTCTlsCertPolicySecure:
+      return @"RTCTlsCertPolicySecure";
+    case RTCTlsCertPolicyInsecureNoCheck:
+      return @"RTCTlsCertPolicyInsecureNoCheck";
+  }
+}
+
+- (rtc::SSLConfig)nativeConfig {
+  __block rtc::SSLConfig sslConfig;
+
+  sslConfig.enable_ocsp_stapling = _enableOCSPStapling;
+  sslConfig.enable_signed_cert_timestamp = _enableSignedCertTimestamp;
+  sslConfig.enable_tls_channel_id = _enableTlsChannelId;
+  sslConfig.enable_grease = _enableGrease;
+
+  switch (_tlsCertPolicy) {
+    case RTCTlsCertPolicySecure:
+      sslConfig.tls_cert_policy = rtc::TlsCertPolicy::TLS_CERT_POLICY_SECURE;
+      break;
+    case RTCTlsCertPolicyInsecureNoCheck:
+      sslConfig.tls_cert_policy = rtc::TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK;
+      break;
+  }
+
+  if (_maxSSLVersion != nil) {
+    sslConfig.max_ssl_version = absl::optional<int>(_maxSSLVersion.intValue);
+  }
+
+  if (_tlsALPNProtocols != nil) {
+    __block std::vector<std::string> alpn_protocols;
+    [_tlsALPNProtocols enumerateObjectsUsingBlock:^(NSString *proto, NSUInteger idx, BOOL *stop) {
+      alpn_protocols.push_back(proto.stdString);
+    }];
+    sslConfig.tls_alpn_protocols = absl::optional<std::vector<std::string>>(alpn_protocols);
+  }
+
+  if (_tlsEllipticCurves != nil) {
+    __block std::vector<std::string> elliptic_curves;
+    [_tlsEllipticCurves enumerateObjectsUsingBlock:^(NSString *curve, NSUInteger idx, BOOL *stop) {
+      elliptic_curves.push_back(curve.stdString);
+    }];
+    sslConfig.tls_elliptic_curves = absl::optional<std::vector<std::string>>(elliptic_curves);
+  }
+
+  return sslConfig;
+}
+
+@end
diff --git a/sdk/objc/unittests/RTCIceServerTest.mm b/sdk/objc/unittests/RTCIceServerTest.mm
index 8ef5195..7659196 100644
--- a/sdk/objc/unittests/RTCIceServerTest.mm
+++ b/sdk/objc/unittests/RTCIceServerTest.mm
@@ -89,7 +89,7 @@
   EXPECT_EQ("username", iceStruct.username);
   EXPECT_EQ("credential", iceStruct.password);
   EXPECT_EQ("hostname", iceStruct.hostname);
-  EXPECT_EQ(2u, iceStruct.tls_alpn_protocols.size());
+  EXPECT_EQ(2u, iceStruct.ssl_config.tls_alpn_protocols.value().size());
 }
 
 - (void)testTlsEllipticCurves {
@@ -106,8 +106,8 @@
   EXPECT_EQ("username", iceStruct.username);
   EXPECT_EQ("credential", iceStruct.password);
   EXPECT_EQ("hostname", iceStruct.hostname);
-  EXPECT_EQ(2u, iceStruct.tls_alpn_protocols.size());
-  EXPECT_EQ(2u, iceStruct.tls_elliptic_curves.size());
+  EXPECT_EQ(2u, iceStruct.ssl_config.tls_alpn_protocols.value().size());
+  EXPECT_EQ(2u, iceStruct.ssl_config.tls_elliptic_curves.value().size());
 }
 
 - (void)testInitFromNativeServer {
@@ -129,8 +129,8 @@
   EXPECT_EQ("username", [NSString stdStringForString:iceServer.username]);
   EXPECT_EQ("password", [NSString stdStringForString:iceServer.credential]);
   EXPECT_EQ("hostname", [NSString stdStringForString:iceServer.hostname]);
-  EXPECT_EQ(2u, iceServer.tlsAlpnProtocols.count);
-  EXPECT_EQ(2u, iceServer.tlsEllipticCurves.count);
+  EXPECT_EQ(2u, iceServer.sslConfig.tlsALPNProtocols.count);
+  EXPECT_EQ(2u, iceServer.sslConfig.tlsEllipticCurves.count);
 }
 
 @end