| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 // Handling of certificates and keypairs for SSLStreamAdapter's peer mode. | 11 // Handling of certificates and keypairs for SSLStreamAdapter's peer mode. |
| 12 #if HAVE_CONFIG_H | 12 #if HAVE_CONFIG_H |
| 13 #include "config.h" | 13 #include "config.h" |
| 14 #endif // HAVE_CONFIG_H | 14 #endif // HAVE_CONFIG_H |
| 15 | 15 |
| 16 #include "webrtc/base/sslidentity.h" | 16 #include "webrtc/base/sslidentity.h" |
| 17 | 17 |
| 18 #include <string> | 18 #include <string> |
| 19 | 19 |
| 20 #include "webrtc/base/base64.h" | 20 #include "webrtc/base/base64.h" |
| 21 #include "webrtc/base/checks.h" |
| 21 #include "webrtc/base/logging.h" | 22 #include "webrtc/base/logging.h" |
| 22 #include "webrtc/base/sslconfig.h" | 23 #include "webrtc/base/sslconfig.h" |
| 23 | 24 |
| 24 #if SSL_USE_OPENSSL | 25 #if SSL_USE_OPENSSL |
| 25 | 26 |
| 26 #include "webrtc/base/opensslidentity.h" | 27 #include "webrtc/base/opensslidentity.h" |
| 27 | 28 |
| 28 #endif // SSL_USE_OPENSSL | 29 #endif // SSL_USE_OPENSSL |
| 29 | 30 |
| 30 namespace rtc { | 31 namespace rtc { |
| 31 | 32 |
| 32 const char kPemTypeCertificate[] = "CERTIFICATE"; | 33 const char kPemTypeCertificate[] = "CERTIFICATE"; |
| 33 const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY"; | 34 const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY"; |
| 34 const char kPemTypeEcPrivateKey[] = "EC PRIVATE KEY"; | 35 const char kPemTypeEcPrivateKey[] = "EC PRIVATE KEY"; |
| 35 | 36 |
| 37 KeyParams::KeyParams(KeyType key_type) { |
| 38 if (key_type == KT_ECDSA) { |
| 39 type_ = KT_ECDSA; |
| 40 params_.curve = EC_NIST_P256; |
| 41 } else if (key_type == KT_RSA) { |
| 42 type_ = KT_RSA; |
| 43 params_.rsa.mod_size = kRsaDefaultModSize; |
| 44 params_.rsa.pub_exp = kRsaDefaultExponent; |
| 45 } else { |
| 46 RTC_NOTREACHED(); |
| 47 } |
| 48 } |
| 49 |
| 50 // static |
| 51 KeyParams KeyParams::RSA(int mod_size, int pub_exp) { |
| 52 KeyParams kt(KT_RSA); |
| 53 kt.params_.rsa.mod_size = mod_size; |
| 54 kt.params_.rsa.pub_exp = pub_exp; |
| 55 return kt; |
| 56 } |
| 57 |
| 58 // static |
| 59 KeyParams KeyParams::ECDSA(ECCurve curve) { |
| 60 KeyParams kt(KT_ECDSA); |
| 61 kt.params_.curve = curve; |
| 62 return kt; |
| 63 } |
| 64 |
| 65 bool KeyParams::IsValid() const { |
| 66 if (type_ == KT_RSA) { |
| 67 return (params_.rsa.mod_size >= kRsaMinModSize && |
| 68 params_.rsa.mod_size <= kRsaMaxModSize && |
| 69 params_.rsa.pub_exp > params_.rsa.mod_size); |
| 70 } else if (type_ == KT_ECDSA) { |
| 71 return (params_.curve == EC_NIST_P256); |
| 72 } |
| 73 return false; |
| 74 } |
| 75 |
| 76 RSAParams KeyParams::rsa_params() const { |
| 77 RTC_DCHECK(type_ == KT_RSA); |
| 78 return params_.rsa; |
| 79 } |
| 80 |
| 81 ECCurve KeyParams::ec_curve() const { |
| 82 RTC_DCHECK(type_ == KT_ECDSA); |
| 83 return params_.curve; |
| 84 } |
| 85 |
| 36 KeyType IntKeyTypeFamilyToKeyType(int key_type_family) { | 86 KeyType IntKeyTypeFamilyToKeyType(int key_type_family) { |
| 37 return static_cast<KeyType>(key_type_family); | 87 return static_cast<KeyType>(key_type_family); |
| 38 } | 88 } |
| 39 | 89 |
| 40 bool SSLIdentity::PemToDer(const std::string& pem_type, | 90 bool SSLIdentity::PemToDer(const std::string& pem_type, |
| 41 const std::string& pem_string, | 91 const std::string& pem_string, |
| 42 std::string* der) { | 92 std::string* der) { |
| 43 // Find the inner body. We need this to fulfill the contract of | 93 // Find the inner body. We need this to fulfill the contract of |
| 44 // returning pem_length. | 94 // returning pem_length. |
| 45 size_t header = pem_string.find("-----BEGIN " + pem_type + "-----"); | 95 size_t header = pem_string.find("-----BEGIN " + pem_type + "-----"); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 std::for_each(certs_.begin(), certs_.end(), DeleteCert); | 151 std::for_each(certs_.begin(), certs_.end(), DeleteCert); |
| 102 } | 152 } |
| 103 | 153 |
| 104 #if SSL_USE_OPENSSL | 154 #if SSL_USE_OPENSSL |
| 105 | 155 |
| 106 SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) { | 156 SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) { |
| 107 return OpenSSLCertificate::FromPEMString(pem_string); | 157 return OpenSSLCertificate::FromPEMString(pem_string); |
| 108 } | 158 } |
| 109 | 159 |
| 110 SSLIdentity* SSLIdentity::Generate(const std::string& common_name, | 160 SSLIdentity* SSLIdentity::Generate(const std::string& common_name, |
| 111 KeyType key_type) { | 161 const KeyParams& key_params) { |
| 112 return OpenSSLIdentity::Generate(common_name, key_type); | 162 return OpenSSLIdentity::Generate(common_name, key_params); |
| 113 } | 163 } |
| 114 | 164 |
| 115 SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) { | 165 SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) { |
| 116 return OpenSSLIdentity::GenerateForTest(params); | 166 return OpenSSLIdentity::GenerateForTest(params); |
| 117 } | 167 } |
| 118 | 168 |
| 119 SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key, | 169 SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key, |
| 120 const std::string& certificate) { | 170 const std::string& certificate) { |
| 121 return OpenSSLIdentity::FromPEMStrings(private_key, certificate); | 171 return OpenSSLIdentity::FromPEMStrings(private_key, certificate); |
| 122 } | 172 } |
| 123 | 173 |
| 124 #else // !SSL_USE_OPENSSL | 174 #else // !SSL_USE_OPENSSL |
| 125 | 175 |
| 126 #error "No SSL implementation" | 176 #error "No SSL implementation" |
| 127 | 177 |
| 128 #endif // SSL_USE_OPENSSL | 178 #endif // SSL_USE_OPENSSL |
| 129 | 179 |
| 130 } // namespace rtc | 180 } // namespace rtc |
| OLD | NEW |