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. |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 12 | #include "rtc_base/sslidentity.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 14 | #include <ctime> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | #include <string> |
David Benjamin | 3df76b1 | 2017-09-29 12:27:18 -0400 | [diff] [blame] | 16 | #include <utility> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/base64.h" |
| 19 | #include "rtc_base/checks.h" |
| 20 | #include "rtc_base/logging.h" |
| 21 | #include "rtc_base/opensslidentity.h" |
David Benjamin | 3df76b1 | 2017-09-29 12:27:18 -0400 | [diff] [blame] | 22 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/sslfingerprint.h" |
deadbeef | f33491e | 2017-01-20 17:01:45 -0800 | [diff] [blame] | 24 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | namespace rtc { |
| 26 | |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 27 | ////////////////////////////////////////////////////////////////////// |
| 28 | // KeyParams |
| 29 | ////////////////////////////////////////////////////////////////////// |
| 30 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 31 | const char kPemTypeCertificate[] = "CERTIFICATE"; |
| 32 | const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY"; |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 33 | const char kPemTypeEcPrivateKey[] = "EC PRIVATE KEY"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 35 | KeyParams::KeyParams(KeyType key_type) { |
| 36 | if (key_type == KT_ECDSA) { |
| 37 | type_ = KT_ECDSA; |
| 38 | params_.curve = EC_NIST_P256; |
| 39 | } else if (key_type == KT_RSA) { |
| 40 | type_ = KT_RSA; |
| 41 | params_.rsa.mod_size = kRsaDefaultModSize; |
| 42 | params_.rsa.pub_exp = kRsaDefaultExponent; |
| 43 | } else { |
| 44 | RTC_NOTREACHED(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // static |
| 49 | KeyParams KeyParams::RSA(int mod_size, int pub_exp) { |
| 50 | KeyParams kt(KT_RSA); |
| 51 | kt.params_.rsa.mod_size = mod_size; |
| 52 | kt.params_.rsa.pub_exp = pub_exp; |
| 53 | return kt; |
| 54 | } |
| 55 | |
| 56 | // static |
| 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 | |
Henrik Boström | 9b5476d | 2015-09-22 14:12:57 +0200 | [diff] [blame] | 84 | KeyType IntKeyTypeFamilyToKeyType(int key_type_family) { |
| 85 | return static_cast<KeyType>(key_type_family); |
| 86 | } |
| 87 | |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 88 | ////////////////////////////////////////////////////////////////////// |
| 89 | // SSLIdentity |
| 90 | ////////////////////////////////////////////////////////////////////// |
| 91 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 92 | bool SSLIdentity::PemToDer(const std::string& pem_type, |
| 93 | const std::string& pem_string, |
| 94 | std::string* der) { |
| 95 | // Find the inner body. We need this to fulfill the contract of |
| 96 | // returning pem_length. |
| 97 | size_t header = pem_string.find("-----BEGIN " + pem_type + "-----"); |
| 98 | if (header == std::string::npos) |
| 99 | return false; |
| 100 | |
| 101 | size_t body = pem_string.find("\n", header); |
| 102 | if (body == std::string::npos) |
| 103 | return false; |
| 104 | |
| 105 | size_t trailer = pem_string.find("-----END " + pem_type + "-----"); |
| 106 | if (trailer == std::string::npos) |
| 107 | return false; |
| 108 | |
| 109 | std::string inner = pem_string.substr(body + 1, trailer - (body + 1)); |
| 110 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 111 | *der = Base64::Decode(inner, Base64::DO_PARSE_WHITE | Base64::DO_PAD_ANY | |
| 112 | Base64::DO_TERM_BUFFER); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 113 | return true; |
| 114 | } |
| 115 | |
| 116 | std::string SSLIdentity::DerToPem(const std::string& pem_type, |
| 117 | const unsigned char* data, |
| 118 | size_t length) { |
| 119 | std::stringstream result; |
| 120 | |
| 121 | result << "-----BEGIN " << pem_type << "-----\n"; |
| 122 | |
| 123 | std::string b64_encoded; |
| 124 | Base64::EncodeFromArray(data, length, &b64_encoded); |
| 125 | |
| 126 | // Divide the Base-64 encoded data into 64-character chunks, as per |
| 127 | // 4.3.2.4 of RFC 1421. |
| 128 | static const size_t kChunkSize = 64; |
| 129 | size_t chunks = (b64_encoded.size() + (kChunkSize - 1)) / kChunkSize; |
| 130 | for (size_t i = 0, chunk_offset = 0; i < chunks; |
| 131 | ++i, chunk_offset += kChunkSize) { |
| 132 | result << b64_encoded.substr(chunk_offset, kChunkSize); |
| 133 | result << "\n"; |
| 134 | } |
| 135 | |
| 136 | result << "-----END " << pem_type << "-----\n"; |
| 137 | |
| 138 | return result.str(); |
| 139 | } |
| 140 | |
Torbjorn Granlund | a3dc79e | 2016-02-16 13:33:53 +0100 | [diff] [blame] | 141 | // static |
Torbjorn Granlund | 1d846b2 | 2016-03-31 16:21:04 +0200 | [diff] [blame] | 142 | SSLIdentity* SSLIdentity::GenerateWithExpiration(const std::string& common_name, |
| 143 | const KeyParams& key_params, |
| 144 | time_t certificate_lifetime) { |
| 145 | return OpenSSLIdentity::GenerateWithExpiration(common_name, key_params, |
| 146 | certificate_lifetime); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Torbjorn Granlund | a3dc79e | 2016-02-16 13:33:53 +0100 | [diff] [blame] | 149 | // static |
| 150 | SSLIdentity* SSLIdentity::Generate(const std::string& common_name, |
| 151 | const KeyParams& key_params) { |
Torbjorn Granlund | 1d846b2 | 2016-03-31 16:21:04 +0200 | [diff] [blame] | 152 | return OpenSSLIdentity::GenerateWithExpiration( |
| 153 | common_name, key_params, kDefaultCertificateLifetimeInSeconds); |
Torbjorn Granlund | a3dc79e | 2016-02-16 13:33:53 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // static |
| 157 | SSLIdentity* SSLIdentity::Generate(const std::string& common_name, |
| 158 | KeyType key_type) { |
Torbjorn Granlund | 1d846b2 | 2016-03-31 16:21:04 +0200 | [diff] [blame] | 159 | return OpenSSLIdentity::GenerateWithExpiration( |
| 160 | common_name, KeyParams(key_type), kDefaultCertificateLifetimeInSeconds); |
Torbjorn Granlund | a3dc79e | 2016-02-16 13:33:53 +0100 | [diff] [blame] | 161 | } |
| 162 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 163 | SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) { |
| 164 | return OpenSSLIdentity::GenerateForTest(params); |
| 165 | } |
| 166 | |
Torbjorn Granlund | a3dc79e | 2016-02-16 13:33:53 +0100 | [diff] [blame] | 167 | // static |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 168 | SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key, |
| 169 | const std::string& certificate) { |
| 170 | return OpenSSLIdentity::FromPEMStrings(private_key, certificate); |
| 171 | } |
| 172 | |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 173 | // static |
| 174 | SSLIdentity* SSLIdentity::FromPEMChainStrings( |
| 175 | const std::string& private_key, |
| 176 | const std::string& certificate_chain) { |
| 177 | return OpenSSLIdentity::FromPEMChainStrings(private_key, certificate_chain); |
| 178 | } |
| 179 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 180 | bool operator==(const SSLIdentity& a, const SSLIdentity& b) { |
| 181 | return static_cast<const OpenSSLIdentity&>(a) == |
| 182 | static_cast<const OpenSSLIdentity&>(b); |
| 183 | } |
| 184 | bool operator!=(const SSLIdentity& a, const SSLIdentity& b) { |
| 185 | return !(a == b); |
| 186 | } |
| 187 | |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 188 | ////////////////////////////////////////////////////////////////////// |
| 189 | // Helper Functions |
| 190 | ////////////////////////////////////////////////////////////////////// |
| 191 | |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 192 | // Read |n| bytes from ASN1 number string at *|pp| and return the numeric value. |
| 193 | // Update *|pp| and *|np| to reflect number of read bytes. |
| 194 | static inline int ASN1ReadInt(const unsigned char** pp, size_t* np, size_t n) { |
| 195 | const unsigned char* p = *pp; |
| 196 | int x = 0; |
| 197 | for (size_t i = 0; i < n; i++) |
| 198 | x = 10 * x + p[i] - '0'; |
| 199 | *pp = p + n; |
| 200 | *np = *np - n; |
| 201 | return x; |
| 202 | } |
| 203 | |
| 204 | int64_t ASN1TimeToSec(const unsigned char* s, size_t length, bool long_format) { |
| 205 | size_t bytes_left = length; |
| 206 | |
| 207 | // Make sure the string ends with Z. Doing it here protects the strspn call |
| 208 | // from running off the end of the string in Z's absense. |
| 209 | if (length == 0 || s[length - 1] != 'Z') |
| 210 | return -1; |
| 211 | |
| 212 | // Make sure we only have ASCII digits so that we don't need to clutter the |
| 213 | // code below and ASN1ReadInt with error checking. |
| 214 | size_t n = strspn(reinterpret_cast<const char*>(s), "0123456789"); |
| 215 | if (n + 1 != length) |
| 216 | return -1; |
| 217 | |
| 218 | int year; |
| 219 | |
| 220 | // Read out ASN1 year, in either 2-char "UTCTIME" or 4-char "GENERALIZEDTIME" |
| 221 | // format. Both format use UTC in this context. |
| 222 | if (long_format) { |
| 223 | // ASN1 format: yyyymmddhh[mm[ss[.fff]]]Z where the Z is literal, but |
| 224 | // RFC 5280 requires us to only support exactly yyyymmddhhmmssZ. |
| 225 | |
| 226 | if (bytes_left < 11) |
| 227 | return -1; |
| 228 | |
| 229 | year = ASN1ReadInt(&s, &bytes_left, 4); |
| 230 | year -= 1900; |
| 231 | } else { |
| 232 | // ASN1 format: yymmddhhmm[ss]Z where the Z is literal, but RFC 5280 |
| 233 | // requires us to only support exactly yymmddhhmmssZ. |
| 234 | |
| 235 | if (bytes_left < 9) |
| 236 | return -1; |
| 237 | |
| 238 | year = ASN1ReadInt(&s, &bytes_left, 2); |
| 239 | if (year < 50) // Per RFC 5280 4.1.2.5.1 |
| 240 | year += 100; |
| 241 | } |
| 242 | |
| 243 | std::tm tm; |
| 244 | tm.tm_year = year; |
| 245 | |
| 246 | // Read out remaining ASN1 time data and store it in |tm| in documented |
| 247 | // std::tm format. |
| 248 | tm.tm_mon = ASN1ReadInt(&s, &bytes_left, 2) - 1; |
| 249 | tm.tm_mday = ASN1ReadInt(&s, &bytes_left, 2); |
| 250 | tm.tm_hour = ASN1ReadInt(&s, &bytes_left, 2); |
| 251 | tm.tm_min = ASN1ReadInt(&s, &bytes_left, 2); |
| 252 | tm.tm_sec = ASN1ReadInt(&s, &bytes_left, 2); |
| 253 | |
| 254 | if (bytes_left != 1) { |
| 255 | // Now just Z should remain. Its existence was asserted above. |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | return TmToSeconds(tm); |
| 260 | } |
| 261 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 262 | } // namespace rtc |