henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 | |
| 11 | // Handling of certificates and keypairs for SSLStreamAdapter's peer mode. |
| 12 | #if HAVE_CONFIG_H |
| 13 | #include "config.h" |
| 14 | #endif // HAVE_CONFIG_H |
| 15 | |
| 16 | #include "webrtc/base/sslidentity.h" |
| 17 | |
| 18 | #include <string> |
| 19 | |
| 20 | #include "webrtc/base/base64.h" |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame^] | 21 | #include "webrtc/base/checks.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | #include "webrtc/base/logging.h" |
| 23 | #include "webrtc/base/sslconfig.h" |
| 24 | |
torbjorng | 172f009 | 2015-10-07 04:57:55 -0700 | [diff] [blame] | 25 | #if SSL_USE_OPENSSL |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | |
| 27 | #include "webrtc/base/opensslidentity.h" |
| 28 | |
torbjorng | 172f009 | 2015-10-07 04:57:55 -0700 | [diff] [blame] | 29 | #endif // SSL_USE_OPENSSL |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | |
| 31 | namespace rtc { |
| 32 | |
| 33 | const char kPemTypeCertificate[] = "CERTIFICATE"; |
| 34 | const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY"; |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 35 | const char kPemTypeEcPrivateKey[] = "EC PRIVATE KEY"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame^] | 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 | |
Henrik Boström | 9b5476d | 2015-09-22 14:12:57 +0200 | [diff] [blame] | 86 | KeyType IntKeyTypeFamilyToKeyType(int key_type_family) { |
| 87 | return static_cast<KeyType>(key_type_family); |
| 88 | } |
| 89 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 90 | bool SSLIdentity::PemToDer(const std::string& pem_type, |
| 91 | const std::string& pem_string, |
| 92 | std::string* der) { |
| 93 | // Find the inner body. We need this to fulfill the contract of |
| 94 | // returning pem_length. |
| 95 | size_t header = pem_string.find("-----BEGIN " + pem_type + "-----"); |
| 96 | if (header == std::string::npos) |
| 97 | return false; |
| 98 | |
| 99 | size_t body = pem_string.find("\n", header); |
| 100 | if (body == std::string::npos) |
| 101 | return false; |
| 102 | |
| 103 | size_t trailer = pem_string.find("-----END " + pem_type + "-----"); |
| 104 | if (trailer == std::string::npos) |
| 105 | return false; |
| 106 | |
| 107 | std::string inner = pem_string.substr(body + 1, trailer - (body + 1)); |
| 108 | |
| 109 | *der = Base64::Decode(inner, Base64::DO_PARSE_WHITE | |
| 110 | Base64::DO_PAD_ANY | |
| 111 | Base64::DO_TERM_BUFFER); |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | std::string SSLIdentity::DerToPem(const std::string& pem_type, |
| 116 | const unsigned char* data, |
| 117 | size_t length) { |
| 118 | std::stringstream result; |
| 119 | |
| 120 | result << "-----BEGIN " << pem_type << "-----\n"; |
| 121 | |
| 122 | std::string b64_encoded; |
| 123 | Base64::EncodeFromArray(data, length, &b64_encoded); |
| 124 | |
| 125 | // Divide the Base-64 encoded data into 64-character chunks, as per |
| 126 | // 4.3.2.4 of RFC 1421. |
| 127 | static const size_t kChunkSize = 64; |
| 128 | size_t chunks = (b64_encoded.size() + (kChunkSize - 1)) / kChunkSize; |
| 129 | for (size_t i = 0, chunk_offset = 0; i < chunks; |
| 130 | ++i, chunk_offset += kChunkSize) { |
| 131 | result << b64_encoded.substr(chunk_offset, kChunkSize); |
| 132 | result << "\n"; |
| 133 | } |
| 134 | |
| 135 | result << "-----END " << pem_type << "-----\n"; |
| 136 | |
| 137 | return result.str(); |
| 138 | } |
| 139 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 140 | SSLCertChain::SSLCertChain(const std::vector<SSLCertificate*>& certs) { |
| 141 | ASSERT(!certs.empty()); |
| 142 | certs_.resize(certs.size()); |
| 143 | std::transform(certs.begin(), certs.end(), certs_.begin(), DupCert); |
| 144 | } |
| 145 | |
| 146 | SSLCertChain::SSLCertChain(const SSLCertificate* cert) { |
| 147 | certs_.push_back(cert->GetReference()); |
| 148 | } |
| 149 | |
| 150 | SSLCertChain::~SSLCertChain() { |
| 151 | std::for_each(certs_.begin(), certs_.end(), DeleteCert); |
| 152 | } |
| 153 | |
torbjorng | 172f009 | 2015-10-07 04:57:55 -0700 | [diff] [blame] | 154 | #if SSL_USE_OPENSSL |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 155 | |
| 156 | SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) { |
| 157 | return OpenSSLCertificate::FromPEMString(pem_string); |
| 158 | } |
| 159 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 160 | SSLIdentity* SSLIdentity::Generate(const std::string& common_name, |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame^] | 161 | const KeyParams& key_params) { |
| 162 | return OpenSSLIdentity::Generate(common_name, key_params); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) { |
| 166 | return OpenSSLIdentity::GenerateForTest(params); |
| 167 | } |
| 168 | |
| 169 | SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key, |
| 170 | const std::string& certificate) { |
| 171 | return OpenSSLIdentity::FromPEMStrings(private_key, certificate); |
| 172 | } |
| 173 | |
torbjorng | 172f009 | 2015-10-07 04:57:55 -0700 | [diff] [blame] | 174 | #else // !SSL_USE_OPENSSL |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | |
| 176 | #error "No SSL implementation" |
| 177 | |
torbjorng | 172f009 | 2015-10-07 04:57:55 -0700 | [diff] [blame] | 178 | #endif // SSL_USE_OPENSSL |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 179 | |
| 180 | } // namespace rtc |