Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "rtc_base/openssl_certificate.h" |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 12 | |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 13 | #if defined(WEBRTC_WIN) |
| 14 | // Must be included first before openssl headers. |
| 15 | #include "rtc_base/win32.h" // NOLINT |
| 16 | #endif // WEBRTC_WIN |
| 17 | |
| 18 | #include <openssl/bio.h> |
| 19 | #include <openssl/bn.h> |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 20 | #include <openssl/pem.h> |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 21 | #include <time.h> |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 22 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 23 | #include <memory> |
| 24 | |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
Philipp Hancke | 4158678 | 2024-06-06 14:31:07 | [diff] [blame] | 26 | #include "rtc_base/crypto_random.h" |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 27 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 28 | #include "rtc_base/message_digest.h" |
| 29 | #include "rtc_base/openssl_digest.h" |
| 30 | #include "rtc_base/openssl_identity.h" |
| 31 | #include "rtc_base/openssl_utility.h" |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 32 | |
| 33 | namespace rtc { |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 34 | namespace { |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 35 | |
| 36 | // Random bits for certificate serial number |
| 37 | static const int SERIAL_RAND_BITS = 64; |
| 38 | |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 39 | #if !defined(NDEBUG) |
| 40 | // Print a certificate to the log, for debugging. |
| 41 | static void PrintCert(X509* x509) { |
| 42 | BIO* temp_memory_bio = BIO_new(BIO_s_mem()); |
| 43 | if (!temp_memory_bio) { |
| 44 | RTC_DLOG_F(LS_ERROR) << "Failed to allocate temporary memory bio"; |
| 45 | return; |
| 46 | } |
| 47 | X509_print_ex(temp_memory_bio, x509, XN_FLAG_SEP_CPLUS_SPC, 0); |
| 48 | BIO_write(temp_memory_bio, "\0", 1); |
| 49 | char* buffer; |
| 50 | BIO_get_mem_data(temp_memory_bio, &buffer); |
| 51 | RTC_DLOG(LS_VERBOSE) << buffer; |
| 52 | BIO_free(temp_memory_bio); |
| 53 | } |
| 54 | #endif |
| 55 | |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 56 | // Generate a self-signed certificate, with the public key from the |
| 57 | // given key pair. Caller is responsible for freeing the returned object. |
| 58 | static X509* MakeCertificate(EVP_PKEY* pkey, const SSLIdentityParams& params) { |
Philipp Hancke | cfd8374 | 2024-08-09 18:56:33 | [diff] [blame] | 59 | RTC_DCHECK(pkey != nullptr); |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 60 | RTC_LOG(LS_INFO) << "Making certificate for " << params.common_name; |
| 61 | |
| 62 | ASN1_INTEGER* asn1_serial_number = nullptr; |
Vojin Ilic | 06a88fe | 2021-06-23 09:53:43 | [diff] [blame] | 63 | std::unique_ptr<BIGNUM, decltype(&::BN_free)> serial_number{nullptr, |
| 64 | ::BN_free}; |
| 65 | std::unique_ptr<X509, decltype(&::X509_free)> x509{nullptr, ::X509_free}; |
| 66 | std::unique_ptr<X509_NAME, decltype(&::X509_NAME_free)> name{ |
| 67 | nullptr, ::X509_NAME_free}; |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 68 | time_t epoch_off = 0; // Time offset since epoch. |
Vojin Ilic | 06a88fe | 2021-06-23 09:53:43 | [diff] [blame] | 69 | x509.reset(X509_new()); |
| 70 | if (x509 == nullptr) { |
| 71 | return nullptr; |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 72 | } |
Vojin Ilic | 06a88fe | 2021-06-23 09:53:43 | [diff] [blame] | 73 | if (!X509_set_pubkey(x509.get(), pkey)) { |
| 74 | return nullptr; |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 75 | } |
| 76 | // serial number - temporary reference to serial number inside x509 struct |
Vojin Ilic | 06a88fe | 2021-06-23 09:53:43 | [diff] [blame] | 77 | serial_number.reset(BN_new()); |
| 78 | if (serial_number == nullptr || |
| 79 | !BN_pseudo_rand(serial_number.get(), SERIAL_RAND_BITS, 0, 0) || |
| 80 | (asn1_serial_number = X509_get_serialNumber(x509.get())) == nullptr || |
| 81 | !BN_to_ASN1_INTEGER(serial_number.get(), asn1_serial_number)) { |
| 82 | return nullptr; |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 83 | } |
| 84 | // Set version to X509.V3 |
Vojin Ilic | 06a88fe | 2021-06-23 09:53:43 | [diff] [blame] | 85 | if (!X509_set_version(x509.get(), 2L)) { |
| 86 | return nullptr; |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // There are a lot of possible components for the name entries. In |
| 90 | // our P2P SSL mode however, the certificates are pre-exchanged |
| 91 | // (through the secure XMPP channel), and so the certificate |
| 92 | // identification is arbitrary. It can't be empty, so we set some |
| 93 | // arbitrary common_name. Note that this certificate goes out in |
| 94 | // clear during SSL negotiation, so there may be a privacy issue in |
| 95 | // putting anything recognizable here. |
Vojin Ilic | 06a88fe | 2021-06-23 09:53:43 | [diff] [blame] | 96 | name.reset(X509_NAME_new()); |
| 97 | if (name == nullptr || |
| 98 | !X509_NAME_add_entry_by_NID(name.get(), NID_commonName, MBSTRING_UTF8, |
Philipp Hancke | cfd8374 | 2024-08-09 18:56:33 | [diff] [blame] | 99 | (unsigned char*)params.common_name.data(), -1, |
| 100 | -1, 0) || |
Vojin Ilic | 06a88fe | 2021-06-23 09:53:43 | [diff] [blame] | 101 | !X509_set_subject_name(x509.get(), name.get()) || |
| 102 | !X509_set_issuer_name(x509.get(), name.get())) { |
| 103 | return nullptr; |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 104 | } |
Vojin Ilic | 06a88fe | 2021-06-23 09:53:43 | [diff] [blame] | 105 | if (!X509_time_adj(X509_get_notBefore(x509.get()), params.not_before, |
| 106 | &epoch_off) || |
| 107 | !X509_time_adj(X509_get_notAfter(x509.get()), params.not_after, |
| 108 | &epoch_off)) { |
| 109 | return nullptr; |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 110 | } |
Vojin Ilic | 06a88fe | 2021-06-23 09:53:43 | [diff] [blame] | 111 | if (!X509_sign(x509.get(), pkey, EVP_sha256())) { |
| 112 | return nullptr; |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 113 | } |
| 114 | |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 115 | RTC_LOG(LS_INFO) << "Returning certificate"; |
Vojin Ilic | 06a88fe | 2021-06-23 09:53:43 | [diff] [blame] | 116 | return x509.release(); |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | } // namespace |
| 120 | |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 121 | OpenSSLCertificate::OpenSSLCertificate(X509* x509) : x509_(x509) { |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 122 | RTC_DCHECK(x509_ != nullptr); |
| 123 | X509_up_ref(x509_); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 124 | } |
| 125 | |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 126 | std::unique_ptr<OpenSSLCertificate> OpenSSLCertificate::Generate( |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 127 | OpenSSLKeyPair* key_pair, |
| 128 | const SSLIdentityParams& params) { |
| 129 | SSLIdentityParams actual_params(params); |
| 130 | if (actual_params.common_name.empty()) { |
| 131 | // Use a random string, arbitrarily 8chars long. |
| 132 | actual_params.common_name = CreateRandomString(8); |
| 133 | } |
| 134 | X509* x509 = MakeCertificate(key_pair->pkey(), actual_params); |
| 135 | if (!x509) { |
| 136 | openssl::LogSSLErrors("Generating certificate"); |
| 137 | return nullptr; |
| 138 | } |
| 139 | #if !defined(NDEBUG) |
| 140 | PrintCert(x509); |
| 141 | #endif |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 142 | auto ret = std::make_unique<OpenSSLCertificate>(x509); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 143 | X509_free(x509); |
| 144 | return ret; |
| 145 | } |
| 146 | |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 147 | std::unique_ptr<OpenSSLCertificate> OpenSSLCertificate::FromPEMString( |
Ali Tofigh | 58d861c | 2022-03-24 23:51:20 | [diff] [blame] | 148 | absl::string_view pem_string) { |
| 149 | BIO* bio = BIO_new_mem_buf(const_cast<char*>(pem_string.data()), -1); |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 150 | if (!bio) { |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 151 | return nullptr; |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 152 | } |
| 153 | |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 154 | BIO_set_mem_eof_return(bio, 0); |
| 155 | X509* x509 = |
| 156 | PEM_read_bio_X509(bio, nullptr, nullptr, const_cast<char*>("\0")); |
| 157 | BIO_free(bio); // Frees the BIO, but not the pointed-to string. |
| 158 | |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 159 | if (!x509) { |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 160 | return nullptr; |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 161 | } |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 162 | auto ret = std::make_unique<OpenSSLCertificate>(x509); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 163 | X509_free(x509); |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | // NOTE: This implementation only functions correctly after InitializeSSL |
| 168 | // and before CleanupSSL. |
| 169 | bool OpenSSLCertificate::GetSignatureDigestAlgorithm( |
| 170 | std::string* algorithm) const { |
| 171 | int nid = X509_get_signature_nid(x509_); |
| 172 | switch (nid) { |
| 173 | case NID_md5WithRSA: |
| 174 | case NID_md5WithRSAEncryption: |
| 175 | *algorithm = DIGEST_MD5; |
| 176 | break; |
| 177 | case NID_ecdsa_with_SHA1: |
| 178 | case NID_dsaWithSHA1: |
| 179 | case NID_dsaWithSHA1_2: |
| 180 | case NID_sha1WithRSA: |
| 181 | case NID_sha1WithRSAEncryption: |
| 182 | *algorithm = DIGEST_SHA_1; |
| 183 | break; |
| 184 | case NID_ecdsa_with_SHA224: |
| 185 | case NID_sha224WithRSAEncryption: |
| 186 | case NID_dsa_with_SHA224: |
| 187 | *algorithm = DIGEST_SHA_224; |
| 188 | break; |
| 189 | case NID_ecdsa_with_SHA256: |
| 190 | case NID_sha256WithRSAEncryption: |
| 191 | case NID_dsa_with_SHA256: |
| 192 | *algorithm = DIGEST_SHA_256; |
| 193 | break; |
| 194 | case NID_ecdsa_with_SHA384: |
| 195 | case NID_sha384WithRSAEncryption: |
| 196 | *algorithm = DIGEST_SHA_384; |
| 197 | break; |
| 198 | case NID_ecdsa_with_SHA512: |
| 199 | case NID_sha512WithRSAEncryption: |
| 200 | *algorithm = DIGEST_SHA_512; |
| 201 | break; |
| 202 | default: |
| 203 | // Unknown algorithm. There are several unhandled options that are less |
| 204 | // common and more complex. |
| 205 | RTC_LOG(LS_ERROR) << "Unknown signature algorithm NID: " << nid; |
| 206 | algorithm->clear(); |
| 207 | return false; |
| 208 | } |
| 209 | return true; |
| 210 | } |
| 211 | |
Ali Tofigh | 58d861c | 2022-03-24 23:51:20 | [diff] [blame] | 212 | bool OpenSSLCertificate::ComputeDigest(absl::string_view algorithm, |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 213 | unsigned char* digest, |
| 214 | size_t size, |
| 215 | size_t* length) const { |
| 216 | return ComputeDigest(x509_, algorithm, digest, size, length); |
| 217 | } |
| 218 | |
| 219 | bool OpenSSLCertificate::ComputeDigest(const X509* x509, |
Ali Tofigh | 58d861c | 2022-03-24 23:51:20 | [diff] [blame] | 220 | absl::string_view algorithm, |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 221 | unsigned char* digest, |
| 222 | size_t size, |
| 223 | size_t* length) { |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 224 | const EVP_MD* md = nullptr; |
| 225 | unsigned int n = 0; |
| 226 | if (!OpenSSLDigest::GetDigestEVP(algorithm, &md)) { |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 227 | return false; |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 228 | } |
| 229 | if (size < static_cast<size_t>(EVP_MD_size(md))) { |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 230 | return false; |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 231 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 232 | X509_digest(x509, md, digest, &n); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 233 | *length = n; |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 234 | return true; |
| 235 | } |
| 236 | |
| 237 | OpenSSLCertificate::~OpenSSLCertificate() { |
| 238 | X509_free(x509_); |
| 239 | } |
| 240 | |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 241 | std::unique_ptr<SSLCertificate> OpenSSLCertificate::Clone() const { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 242 | return std::make_unique<OpenSSLCertificate>(x509_); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | std::string OpenSSLCertificate::ToPEMString() const { |
| 246 | BIO* bio = BIO_new(BIO_s_mem()); |
Karl Wiberg | c95b939 | 2020-11-07 23:49:37 | [diff] [blame] | 247 | RTC_CHECK(bio); |
| 248 | RTC_CHECK(PEM_write_bio_X509(bio, x509_)); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 249 | BIO_write(bio, "\0", 1); |
| 250 | char* buffer; |
| 251 | BIO_get_mem_data(bio, &buffer); |
| 252 | std::string ret(buffer); |
| 253 | BIO_free(bio); |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | void OpenSSLCertificate::ToDER(Buffer* der_buffer) const { |
| 258 | // In case of failure, make sure to leave the buffer empty. |
| 259 | der_buffer->SetSize(0); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 260 | // Calculates the DER representation of the certificate, from scratch. |
| 261 | BIO* bio = BIO_new(BIO_s_mem()); |
Karl Wiberg | c95b939 | 2020-11-07 23:49:37 | [diff] [blame] | 262 | RTC_CHECK(bio); |
| 263 | RTC_CHECK(i2d_X509_bio(bio, x509_)); |
Benjamin Wright | 428320c | 2018-10-29 01:22:53 | [diff] [blame] | 264 | char* data = nullptr; |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 265 | size_t length = BIO_get_mem_data(bio, &data); |
| 266 | der_buffer->SetData(data, length); |
| 267 | BIO_free(bio); |
| 268 | } |
| 269 | |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 270 | bool OpenSSLCertificate::operator==(const OpenSSLCertificate& other) const { |
| 271 | return X509_cmp(x509_, other.x509_) == 0; |
| 272 | } |
| 273 | |
| 274 | bool OpenSSLCertificate::operator!=(const OpenSSLCertificate& other) const { |
| 275 | return !(*this == other); |
| 276 | } |
| 277 | |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 278 | int64_t OpenSSLCertificate::CertificateExpirationTime() const { |
| 279 | ASN1_TIME* expire_time = X509_get_notAfter(x509_); |
| 280 | bool long_format; |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 281 | if (expire_time->type == V_ASN1_UTCTIME) { |
| 282 | long_format = false; |
| 283 | } else if (expire_time->type == V_ASN1_GENERALIZEDTIME) { |
| 284 | long_format = true; |
| 285 | } else { |
| 286 | return -1; |
| 287 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 288 | return ASN1TimeToSec(expire_time->data, expire_time->length, long_format); |
| 289 | } |
| 290 | |
| 291 | } // namespace rtc |