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