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