Chromium Code Reviews| 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 KeyParams KeyParams::RSA(int mod_size, int pub_exp) { | |
|
Henrik Grunell WebRTC
2015/10/08 13:45:32
Nit: Add
// static
above the static functions.
torbjorng (webrtc)
2015/10/08 14:01:46
Done.
| |
| 51 KeyParams kt(KT_RSA); | |
| 52 kt.params_.rsa.mod_size = mod_size; | |
| 53 kt.params_.rsa.pub_exp = pub_exp; | |
| 54 return kt; | |
| 55 } | |
| 56 | |
| 57 KeyParams KeyParams::ECDSA(ECCurve curve) { | |
| 58 KeyParams kt(KT_ECDSA); | |
| 59 kt.params_.curve = curve; | |
| 60 return kt; | |
| 61 } | |
| 62 | |
| 63 bool KeyParams::IsValid() const { | |
| 64 if (type_ == KT_RSA) { | |
| 65 return (params_.rsa.mod_size >= kRsaMinModSize && | |
| 66 params_.rsa.mod_size <= kRsaMaxModSize && | |
| 67 params_.rsa.pub_exp > params_.rsa.mod_size); | |
| 68 } else if (type_ == KT_ECDSA) { | |
| 69 return (params_.curve == EC_NIST_P256); | |
| 70 } | |
| 71 return false; | |
| 72 } | |
| 73 | |
| 74 RSAParams KeyParams::rsa_params() const { | |
| 75 RTC_DCHECK(type_ == KT_RSA); | |
| 76 return params_.rsa; | |
| 77 } | |
| 78 | |
| 79 ECCurve KeyParams::ec_curve() const { | |
| 80 RTC_DCHECK(type_ == KT_ECDSA); | |
| 81 return params_.curve; | |
| 82 } | |
| 83 | |
| 36 KeyType IntKeyTypeFamilyToKeyType(int key_type_family) { | 84 KeyType IntKeyTypeFamilyToKeyType(int key_type_family) { |
| 37 return static_cast<KeyType>(key_type_family); | 85 return static_cast<KeyType>(key_type_family); |
| 38 } | 86 } |
| 39 | 87 |
| 40 bool SSLIdentity::PemToDer(const std::string& pem_type, | 88 bool SSLIdentity::PemToDer(const std::string& pem_type, |
| 41 const std::string& pem_string, | 89 const std::string& pem_string, |
| 42 std::string* der) { | 90 std::string* der) { |
| 43 // Find the inner body. We need this to fulfill the contract of | 91 // Find the inner body. We need this to fulfill the contract of |
| 44 // returning pem_length. | 92 // returning pem_length. |
| 45 size_t header = pem_string.find("-----BEGIN " + pem_type + "-----"); | 93 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); | 149 std::for_each(certs_.begin(), certs_.end(), DeleteCert); |
| 102 } | 150 } |
| 103 | 151 |
| 104 #if SSL_USE_OPENSSL | 152 #if SSL_USE_OPENSSL |
| 105 | 153 |
| 106 SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) { | 154 SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) { |
| 107 return OpenSSLCertificate::FromPEMString(pem_string); | 155 return OpenSSLCertificate::FromPEMString(pem_string); |
| 108 } | 156 } |
| 109 | 157 |
| 110 SSLIdentity* SSLIdentity::Generate(const std::string& common_name, | 158 SSLIdentity* SSLIdentity::Generate(const std::string& common_name, |
| 111 KeyType key_type) { | 159 const KeyParams& key_params) { |
| 112 return OpenSSLIdentity::Generate(common_name, key_type); | 160 return OpenSSLIdentity::Generate(common_name, key_params); |
| 113 } | 161 } |
| 114 | 162 |
| 115 SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) { | 163 SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) { |
| 116 return OpenSSLIdentity::GenerateForTest(params); | 164 return OpenSSLIdentity::GenerateForTest(params); |
| 117 } | 165 } |
| 118 | 166 |
| 119 SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key, | 167 SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key, |
| 120 const std::string& certificate) { | 168 const std::string& certificate) { |
| 121 return OpenSSLIdentity::FromPEMStrings(private_key, certificate); | 169 return OpenSSLIdentity::FromPEMStrings(private_key, certificate); |
| 122 } | 170 } |
| 123 | 171 |
| 124 #else // !SSL_USE_OPENSSL | 172 #else // !SSL_USE_OPENSSL |
| 125 | 173 |
| 126 #error "No SSL implementation" | 174 #error "No SSL implementation" |
| 127 | 175 |
| 128 #endif // SSL_USE_OPENSSL | 176 #endif // SSL_USE_OPENSSL |
| 129 | 177 |
| 130 } // namespace rtc | 178 } // namespace rtc |
| OLD | NEW |