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