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 | #if HAVE_OPENSSL_SSL_H |
| 12 | |
| 13 | #include "webrtc/base/opensslidentity.h" |
| 14 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 15 | #include <memory> |
| 16 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | // Must be included first before openssl headers. |
| 18 | #include "webrtc/base/win32.h" // NOLINT |
| 19 | |
| 20 | #include <openssl/bio.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/pem.h> |
| 23 | #include <openssl/bn.h> |
| 24 | #include <openssl/rsa.h> |
| 25 | #include <openssl/crypto.h> |
| 26 | |
| 27 | #include "webrtc/base/checks.h" |
| 28 | #include "webrtc/base/helpers.h" |
| 29 | #include "webrtc/base/logging.h" |
| 30 | #include "webrtc/base/openssl.h" |
| 31 | #include "webrtc/base/openssldigest.h" |
| 32 | |
| 33 | namespace rtc { |
| 34 | |
| 35 | // We could have exposed a myriad of parameters for the crypto stuff, |
| 36 | // but keeping it simple seems best. |
| 37 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | // Random bits for certificate serial number |
| 39 | static const int SERIAL_RAND_BITS = 64; |
| 40 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | // Generate a key pair. Caller is responsible for freeing the returned object. |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 42 | static EVP_PKEY* MakeKey(const KeyParams& key_params) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | LOG(LS_INFO) << "Making key pair"; |
| 44 | EVP_PKEY* pkey = EVP_PKEY_new(); |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 45 | if (key_params.type() == KT_RSA) { |
| 46 | int key_length = key_params.rsa_params().mod_size; |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 47 | BIGNUM* exponent = BN_new(); |
| 48 | RSA* rsa = RSA_new(); |
| 49 | if (!pkey || !exponent || !rsa || |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 50 | !BN_set_word(exponent, key_params.rsa_params().pub_exp) || |
| 51 | !RSA_generate_key_ex(rsa, key_length, exponent, NULL) || |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 52 | !EVP_PKEY_assign_RSA(pkey, rsa)) { |
| 53 | EVP_PKEY_free(pkey); |
| 54 | BN_free(exponent); |
| 55 | RSA_free(rsa); |
| 56 | LOG(LS_ERROR) << "Failed to make RSA key pair"; |
| 57 | return NULL; |
| 58 | } |
| 59 | // ownership of rsa struct was assigned, don't free it. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 60 | BN_free(exponent); |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 61 | } else if (key_params.type() == KT_ECDSA) { |
| 62 | if (key_params.ec_curve() == EC_NIST_P256) { |
| 63 | EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
ssaroha | bbfed52 | 2016-12-11 18:42:07 -0800 | [diff] [blame^] | 64 | |
| 65 | // Ensure curve name is included when EC key is serialized. |
| 66 | // Without this call, OpenSSL versions before 1.1.0 will create |
| 67 | // certificates that don't work for TLS. |
| 68 | // This is a no-op for BoringSSL and OpenSSL 1.1.0+ |
| 69 | EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE); |
| 70 | |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 71 | if (!pkey || !ec_key || !EC_KEY_generate_key(ec_key) || |
| 72 | !EVP_PKEY_assign_EC_KEY(pkey, ec_key)) { |
| 73 | EVP_PKEY_free(pkey); |
| 74 | EC_KEY_free(ec_key); |
| 75 | LOG(LS_ERROR) << "Failed to make EC key pair"; |
| 76 | return NULL; |
| 77 | } |
| 78 | // ownership of ec_key struct was assigned, don't free it. |
| 79 | } else { |
| 80 | // Add generation of any other curves here. |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 81 | EVP_PKEY_free(pkey); |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 82 | LOG(LS_ERROR) << "ECDSA key requested for unknown curve"; |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 83 | return NULL; |
| 84 | } |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 85 | } else { |
| 86 | EVP_PKEY_free(pkey); |
| 87 | LOG(LS_ERROR) << "Key type requested not understood"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 88 | return NULL; |
| 89 | } |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 90 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 91 | LOG(LS_INFO) << "Returning key pair"; |
| 92 | return pkey; |
| 93 | } |
| 94 | |
| 95 | // Generate a self-signed certificate, with the public key from the |
| 96 | // given key pair. Caller is responsible for freeing the returned object. |
| 97 | static X509* MakeCertificate(EVP_PKEY* pkey, const SSLIdentityParams& params) { |
| 98 | LOG(LS_INFO) << "Making certificate for " << params.common_name; |
| 99 | X509* x509 = NULL; |
| 100 | BIGNUM* serial_number = NULL; |
| 101 | X509_NAME* name = NULL; |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 102 | time_t epoch_off = 0; // Time offset since epoch. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 103 | |
| 104 | if ((x509=X509_new()) == NULL) |
| 105 | goto error; |
| 106 | |
| 107 | if (!X509_set_pubkey(x509, pkey)) |
| 108 | goto error; |
| 109 | |
| 110 | // serial number |
| 111 | // temporary reference to serial number inside x509 struct |
| 112 | ASN1_INTEGER* asn1_serial_number; |
| 113 | if ((serial_number = BN_new()) == NULL || |
| 114 | !BN_pseudo_rand(serial_number, SERIAL_RAND_BITS, 0, 0) || |
| 115 | (asn1_serial_number = X509_get_serialNumber(x509)) == NULL || |
| 116 | !BN_to_ASN1_INTEGER(serial_number, asn1_serial_number)) |
| 117 | goto error; |
| 118 | |
torbjorng | f816035 | 2016-03-29 07:57:43 -0700 | [diff] [blame] | 119 | if (!X509_set_version(x509, 2L)) // version 3 |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 120 | goto error; |
| 121 | |
| 122 | // There are a lot of possible components for the name entries. In |
| 123 | // our P2P SSL mode however, the certificates are pre-exchanged |
| 124 | // (through the secure XMPP channel), and so the certificate |
| 125 | // identification is arbitrary. It can't be empty, so we set some |
| 126 | // arbitrary common_name. Note that this certificate goes out in |
| 127 | // clear during SSL negotiation, so there may be a privacy issue in |
| 128 | // putting anything recognizable here. |
| 129 | if ((name = X509_NAME_new()) == NULL || |
| 130 | !X509_NAME_add_entry_by_NID( |
| 131 | name, NID_commonName, MBSTRING_UTF8, |
| 132 | (unsigned char*)params.common_name.c_str(), -1, -1, 0) || |
| 133 | !X509_set_subject_name(x509, name) || |
| 134 | !X509_set_issuer_name(x509, name)) |
| 135 | goto error; |
| 136 | |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 137 | if (!X509_time_adj(X509_get_notBefore(x509), params.not_before, &epoch_off) || |
| 138 | !X509_time_adj(X509_get_notAfter(x509), params.not_after, &epoch_off)) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 139 | goto error; |
| 140 | |
Joachim Bauch | 1b794d5 | 2015-05-12 03:32:11 +0200 | [diff] [blame] | 141 | if (!X509_sign(x509, pkey, EVP_sha256())) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 142 | goto error; |
| 143 | |
| 144 | BN_free(serial_number); |
| 145 | X509_NAME_free(name); |
| 146 | LOG(LS_INFO) << "Returning certificate"; |
| 147 | return x509; |
| 148 | |
| 149 | error: |
| 150 | BN_free(serial_number); |
| 151 | X509_NAME_free(name); |
| 152 | X509_free(x509); |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | // This dumps the SSL error stack to the log. |
| 157 | static void LogSSLErrors(const std::string& prefix) { |
| 158 | char error_buf[200]; |
| 159 | unsigned long err; |
| 160 | |
| 161 | while ((err = ERR_get_error()) != 0) { |
| 162 | ERR_error_string_n(err, error_buf, sizeof(error_buf)); |
| 163 | LOG(LS_ERROR) << prefix << ": " << error_buf << "\n"; |
| 164 | } |
| 165 | } |
| 166 | |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 167 | OpenSSLKeyPair* OpenSSLKeyPair::Generate(const KeyParams& key_params) { |
| 168 | EVP_PKEY* pkey = MakeKey(key_params); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 169 | if (!pkey) { |
| 170 | LogSSLErrors("Generating key pair"); |
| 171 | return NULL; |
| 172 | } |
| 173 | return new OpenSSLKeyPair(pkey); |
| 174 | } |
| 175 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 176 | OpenSSLKeyPair* OpenSSLKeyPair::FromPrivateKeyPEMString( |
| 177 | const std::string& pem_string) { |
| 178 | BIO* bio = BIO_new_mem_buf(const_cast<char*>(pem_string.c_str()), -1); |
| 179 | if (!bio) { |
| 180 | LOG(LS_ERROR) << "Failed to create a new BIO buffer."; |
| 181 | return nullptr; |
| 182 | } |
| 183 | BIO_set_mem_eof_return(bio, 0); |
| 184 | EVP_PKEY* pkey = |
| 185 | PEM_read_bio_PrivateKey(bio, nullptr, nullptr, const_cast<char*>("\0")); |
| 186 | BIO_free(bio); // Frees the BIO, but not the pointed-to string. |
| 187 | if (!pkey) { |
| 188 | LOG(LS_ERROR) << "Failed to create the private key from PEM string."; |
| 189 | return nullptr; |
| 190 | } |
| 191 | if (EVP_PKEY_missing_parameters(pkey) != 0) { |
| 192 | LOG(LS_ERROR) << "The resulting key pair is missing public key parameters."; |
| 193 | EVP_PKEY_free(pkey); |
| 194 | return nullptr; |
| 195 | } |
| 196 | return new OpenSSLKeyPair(pkey); |
| 197 | } |
| 198 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 199 | OpenSSLKeyPair::~OpenSSLKeyPair() { |
| 200 | EVP_PKEY_free(pkey_); |
| 201 | } |
| 202 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 203 | OpenSSLKeyPair* OpenSSLKeyPair::GetReference() { |
| 204 | AddReference(); |
| 205 | return new OpenSSLKeyPair(pkey_); |
| 206 | } |
| 207 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 208 | void OpenSSLKeyPair::AddReference() { |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 209 | #if defined(OPENSSL_IS_BORINGSSL) |
Jiayang Liu | 770cc38 | 2015-05-28 15:36:29 -0700 | [diff] [blame] | 210 | EVP_PKEY_up_ref(pkey_); |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 211 | #else |
| 212 | CRYPTO_add(&pkey_->references, 1, CRYPTO_LOCK_EVP_PKEY); |
| 213 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 214 | } |
| 215 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 216 | std::string OpenSSLKeyPair::PrivateKeyToPEMString() const { |
| 217 | BIO* temp_memory_bio = BIO_new(BIO_s_mem()); |
| 218 | if (!temp_memory_bio) { |
| 219 | LOG_F(LS_ERROR) << "Failed to allocate temporary memory bio"; |
| 220 | RTC_NOTREACHED(); |
| 221 | return ""; |
| 222 | } |
| 223 | if (!PEM_write_bio_PrivateKey( |
| 224 | temp_memory_bio, pkey_, nullptr, nullptr, 0, nullptr, nullptr)) { |
| 225 | LOG_F(LS_ERROR) << "Failed to write private key"; |
| 226 | BIO_free(temp_memory_bio); |
| 227 | RTC_NOTREACHED(); |
| 228 | return ""; |
| 229 | } |
| 230 | BIO_write(temp_memory_bio, "\0", 1); |
| 231 | char* buffer; |
| 232 | BIO_get_mem_data(temp_memory_bio, &buffer); |
| 233 | std::string priv_key_str = buffer; |
| 234 | BIO_free(temp_memory_bio); |
| 235 | return priv_key_str; |
| 236 | } |
| 237 | |
| 238 | std::string OpenSSLKeyPair::PublicKeyToPEMString() const { |
| 239 | BIO* temp_memory_bio = BIO_new(BIO_s_mem()); |
| 240 | if (!temp_memory_bio) { |
| 241 | LOG_F(LS_ERROR) << "Failed to allocate temporary memory bio"; |
| 242 | RTC_NOTREACHED(); |
| 243 | return ""; |
| 244 | } |
| 245 | if (!PEM_write_bio_PUBKEY(temp_memory_bio, pkey_)) { |
| 246 | LOG_F(LS_ERROR) << "Failed to write public key"; |
| 247 | BIO_free(temp_memory_bio); |
| 248 | RTC_NOTREACHED(); |
| 249 | return ""; |
| 250 | } |
| 251 | BIO_write(temp_memory_bio, "\0", 1); |
| 252 | char* buffer; |
| 253 | BIO_get_mem_data(temp_memory_bio, &buffer); |
| 254 | std::string pub_key_str = buffer; |
| 255 | BIO_free(temp_memory_bio); |
| 256 | return pub_key_str; |
| 257 | } |
| 258 | |
| 259 | bool OpenSSLKeyPair::operator==(const OpenSSLKeyPair& other) const { |
| 260 | return EVP_PKEY_cmp(this->pkey_, other.pkey_) == 1; |
| 261 | } |
| 262 | |
| 263 | bool OpenSSLKeyPair::operator!=(const OpenSSLKeyPair& other) const { |
| 264 | return !(*this == other); |
| 265 | } |
| 266 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 267 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 268 | // Print a certificate to the log, for debugging. |
| 269 | static void PrintCert(X509* x509) { |
| 270 | BIO* temp_memory_bio = BIO_new(BIO_s_mem()); |
| 271 | if (!temp_memory_bio) { |
| 272 | LOG_F(LS_ERROR) << "Failed to allocate temporary memory bio"; |
| 273 | return; |
| 274 | } |
| 275 | X509_print_ex(temp_memory_bio, x509, XN_FLAG_SEP_CPLUS_SPC, 0); |
| 276 | BIO_write(temp_memory_bio, "\0", 1); |
| 277 | char* buffer; |
| 278 | BIO_get_mem_data(temp_memory_bio, &buffer); |
| 279 | LOG(LS_VERBOSE) << buffer; |
| 280 | BIO_free(temp_memory_bio); |
| 281 | } |
| 282 | #endif |
| 283 | |
| 284 | OpenSSLCertificate* OpenSSLCertificate::Generate( |
| 285 | OpenSSLKeyPair* key_pair, const SSLIdentityParams& params) { |
| 286 | SSLIdentityParams actual_params(params); |
| 287 | if (actual_params.common_name.empty()) { |
| 288 | // Use a random string, arbitrarily 8chars long. |
| 289 | actual_params.common_name = CreateRandomString(8); |
| 290 | } |
| 291 | X509* x509 = MakeCertificate(key_pair->pkey(), actual_params); |
| 292 | if (!x509) { |
| 293 | LogSSLErrors("Generating certificate"); |
| 294 | return NULL; |
| 295 | } |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 296 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 297 | PrintCert(x509); |
| 298 | #endif |
| 299 | OpenSSLCertificate* ret = new OpenSSLCertificate(x509); |
| 300 | X509_free(x509); |
| 301 | return ret; |
| 302 | } |
| 303 | |
| 304 | OpenSSLCertificate* OpenSSLCertificate::FromPEMString( |
| 305 | const std::string& pem_string) { |
| 306 | BIO* bio = BIO_new_mem_buf(const_cast<char*>(pem_string.c_str()), -1); |
| 307 | if (!bio) |
| 308 | return NULL; |
| 309 | BIO_set_mem_eof_return(bio, 0); |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 310 | X509* x509 = PEM_read_bio_X509(bio, NULL, NULL, const_cast<char*>("\0")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 311 | BIO_free(bio); // Frees the BIO, but not the pointed-to string. |
| 312 | |
| 313 | if (!x509) |
| 314 | return NULL; |
| 315 | |
| 316 | OpenSSLCertificate* ret = new OpenSSLCertificate(x509); |
| 317 | X509_free(x509); |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | // NOTE: This implementation only functions correctly after InitializeSSL |
| 322 | // and before CleanupSSL. |
| 323 | bool OpenSSLCertificate::GetSignatureDigestAlgorithm( |
| 324 | std::string* algorithm) const { |
JiaYang (佳扬) Liu | 01aeaee | 2015-04-22 12:18:33 -0700 | [diff] [blame] | 325 | int nid = OBJ_obj2nid(x509_->sig_alg->algorithm); |
| 326 | switch (nid) { |
| 327 | case NID_md5WithRSA: |
| 328 | case NID_md5WithRSAEncryption: |
| 329 | *algorithm = DIGEST_MD5; |
| 330 | break; |
| 331 | case NID_ecdsa_with_SHA1: |
| 332 | case NID_dsaWithSHA1: |
| 333 | case NID_dsaWithSHA1_2: |
| 334 | case NID_sha1WithRSA: |
| 335 | case NID_sha1WithRSAEncryption: |
| 336 | *algorithm = DIGEST_SHA_1; |
| 337 | break; |
| 338 | case NID_ecdsa_with_SHA224: |
| 339 | case NID_sha224WithRSAEncryption: |
| 340 | case NID_dsa_with_SHA224: |
| 341 | *algorithm = DIGEST_SHA_224; |
| 342 | break; |
| 343 | case NID_ecdsa_with_SHA256: |
| 344 | case NID_sha256WithRSAEncryption: |
| 345 | case NID_dsa_with_SHA256: |
| 346 | *algorithm = DIGEST_SHA_256; |
| 347 | break; |
| 348 | case NID_ecdsa_with_SHA384: |
| 349 | case NID_sha384WithRSAEncryption: |
| 350 | *algorithm = DIGEST_SHA_384; |
| 351 | break; |
| 352 | case NID_ecdsa_with_SHA512: |
| 353 | case NID_sha512WithRSAEncryption: |
| 354 | *algorithm = DIGEST_SHA_512; |
| 355 | break; |
| 356 | default: |
| 357 | // Unknown algorithm. There are several unhandled options that are less |
| 358 | // common and more complex. |
| 359 | LOG(LS_ERROR) << "Unknown signature algorithm NID: " << nid; |
| 360 | algorithm->clear(); |
| 361 | return false; |
| 362 | } |
| 363 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 364 | } |
| 365 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 366 | std::unique_ptr<SSLCertChain> OpenSSLCertificate::GetChain() const { |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 367 | // Chains are not yet supported when using OpenSSL. |
| 368 | // OpenSSLStreamAdapter::SSLVerifyCallback currently requires the remote |
| 369 | // certificate to be self-signed. |
kwiberg | f5d4786 | 2016-03-15 12:53:24 -0700 | [diff] [blame] | 370 | return nullptr; |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 371 | } |
| 372 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 373 | bool OpenSSLCertificate::ComputeDigest(const std::string& algorithm, |
| 374 | unsigned char* digest, |
| 375 | size_t size, |
| 376 | size_t* length) const { |
| 377 | return ComputeDigest(x509_, algorithm, digest, size, length); |
| 378 | } |
| 379 | |
| 380 | bool OpenSSLCertificate::ComputeDigest(const X509* x509, |
| 381 | const std::string& algorithm, |
| 382 | unsigned char* digest, |
| 383 | size_t size, |
| 384 | size_t* length) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 385 | const EVP_MD* md; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 386 | unsigned int n; |
| 387 | |
| 388 | if (!OpenSSLDigest::GetDigestEVP(algorithm, &md)) |
| 389 | return false; |
| 390 | |
| 391 | if (size < static_cast<size_t>(EVP_MD_size(md))) |
| 392 | return false; |
| 393 | |
| 394 | X509_digest(x509, md, digest, &n); |
| 395 | |
| 396 | *length = n; |
| 397 | |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | OpenSSLCertificate::~OpenSSLCertificate() { |
| 402 | X509_free(x509_); |
| 403 | } |
| 404 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 405 | OpenSSLCertificate* OpenSSLCertificate::GetReference() const { |
| 406 | return new OpenSSLCertificate(x509_); |
| 407 | } |
| 408 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 409 | std::string OpenSSLCertificate::ToPEMString() const { |
| 410 | BIO* bio = BIO_new(BIO_s_mem()); |
| 411 | if (!bio) { |
andrew@webrtc.org | a5b7869 | 2014-08-28 16:28:26 +0000 | [diff] [blame] | 412 | FATAL() << "unreachable code"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 413 | } |
| 414 | if (!PEM_write_bio_X509(bio, x509_)) { |
| 415 | BIO_free(bio); |
andrew@webrtc.org | a5b7869 | 2014-08-28 16:28:26 +0000 | [diff] [blame] | 416 | FATAL() << "unreachable code"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 417 | } |
| 418 | BIO_write(bio, "\0", 1); |
| 419 | char* buffer; |
| 420 | BIO_get_mem_data(bio, &buffer); |
| 421 | std::string ret(buffer); |
| 422 | BIO_free(bio); |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | void OpenSSLCertificate::ToDER(Buffer* der_buffer) const { |
| 427 | // In case of failure, make sure to leave the buffer empty. |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 428 | der_buffer->SetSize(0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 429 | |
| 430 | // Calculates the DER representation of the certificate, from scratch. |
| 431 | BIO* bio = BIO_new(BIO_s_mem()); |
| 432 | if (!bio) { |
andrew@webrtc.org | a5b7869 | 2014-08-28 16:28:26 +0000 | [diff] [blame] | 433 | FATAL() << "unreachable code"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 434 | } |
| 435 | if (!i2d_X509_bio(bio, x509_)) { |
| 436 | BIO_free(bio); |
andrew@webrtc.org | a5b7869 | 2014-08-28 16:28:26 +0000 | [diff] [blame] | 437 | FATAL() << "unreachable code"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 438 | } |
| 439 | char* data; |
| 440 | size_t length = BIO_get_mem_data(bio, &data); |
| 441 | der_buffer->SetData(data, length); |
| 442 | BIO_free(bio); |
| 443 | } |
| 444 | |
| 445 | void OpenSSLCertificate::AddReference() const { |
| 446 | ASSERT(x509_ != NULL); |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 447 | #if defined(OPENSSL_IS_BORINGSSL) |
Jiayang Liu | 770cc38 | 2015-05-28 15:36:29 -0700 | [diff] [blame] | 448 | X509_up_ref(x509_); |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 449 | #else |
| 450 | CRYPTO_add(&x509_->references, 1, CRYPTO_LOCK_X509); |
| 451 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 452 | } |
| 453 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 454 | bool OpenSSLCertificate::operator==(const OpenSSLCertificate& other) const { |
| 455 | return X509_cmp(this->x509_, other.x509_) == 0; |
| 456 | } |
| 457 | |
| 458 | bool OpenSSLCertificate::operator!=(const OpenSSLCertificate& other) const { |
| 459 | return !(*this == other); |
| 460 | } |
| 461 | |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 462 | // Documented in sslidentity.h. |
| 463 | int64_t OpenSSLCertificate::CertificateExpirationTime() const { |
| 464 | ASN1_TIME* expire_time = X509_get_notAfter(x509_); |
| 465 | bool long_format; |
| 466 | |
| 467 | if (expire_time->type == V_ASN1_UTCTIME) { |
| 468 | long_format = false; |
| 469 | } else if (expire_time->type == V_ASN1_GENERALIZEDTIME) { |
| 470 | long_format = true; |
| 471 | } else { |
| 472 | return -1; |
| 473 | } |
| 474 | |
| 475 | return ASN1TimeToSec(expire_time->data, expire_time->length, long_format); |
| 476 | } |
| 477 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 478 | OpenSSLIdentity::OpenSSLIdentity(OpenSSLKeyPair* key_pair, |
| 479 | OpenSSLCertificate* certificate) |
| 480 | : key_pair_(key_pair), certificate_(certificate) { |
| 481 | ASSERT(key_pair != NULL); |
| 482 | ASSERT(certificate != NULL); |
| 483 | } |
| 484 | |
| 485 | OpenSSLIdentity::~OpenSSLIdentity() = default; |
| 486 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 487 | OpenSSLIdentity* OpenSSLIdentity::GenerateInternal( |
| 488 | const SSLIdentityParams& params) { |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 489 | OpenSSLKeyPair* key_pair = OpenSSLKeyPair::Generate(params.key_params); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 490 | if (key_pair) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 491 | OpenSSLCertificate* certificate = |
| 492 | OpenSSLCertificate::Generate(key_pair, params); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 493 | if (certificate) |
| 494 | return new OpenSSLIdentity(key_pair, certificate); |
| 495 | delete key_pair; |
| 496 | } |
| 497 | LOG(LS_INFO) << "Identity generation failed"; |
| 498 | return NULL; |
| 499 | } |
| 500 | |
Torbjorn Granlund | 1d846b2 | 2016-03-31 16:21:04 +0200 | [diff] [blame] | 501 | OpenSSLIdentity* OpenSSLIdentity::GenerateWithExpiration( |
| 502 | const std::string& common_name, |
| 503 | const KeyParams& key_params, |
| 504 | time_t certificate_lifetime) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 505 | SSLIdentityParams params; |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 506 | params.key_params = key_params; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 507 | params.common_name = common_name; |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 508 | time_t now = time(NULL); |
Torbjorn Granlund | 1d846b2 | 2016-03-31 16:21:04 +0200 | [diff] [blame] | 509 | params.not_before = now + kCertificateWindowInSeconds; |
torbjorng | e8dc081 | 2016-02-15 09:35:54 -0800 | [diff] [blame] | 510 | params.not_after = now + certificate_lifetime; |
Torbjorn Granlund | 1d846b2 | 2016-03-31 16:21:04 +0200 | [diff] [blame] | 511 | if (params.not_before > params.not_after) |
| 512 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 513 | return GenerateInternal(params); |
| 514 | } |
| 515 | |
| 516 | OpenSSLIdentity* OpenSSLIdentity::GenerateForTest( |
| 517 | const SSLIdentityParams& params) { |
| 518 | return GenerateInternal(params); |
| 519 | } |
| 520 | |
| 521 | SSLIdentity* OpenSSLIdentity::FromPEMStrings( |
| 522 | const std::string& private_key, |
| 523 | const std::string& certificate) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 524 | std::unique_ptr<OpenSSLCertificate> cert( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 525 | OpenSSLCertificate::FromPEMString(certificate)); |
| 526 | if (!cert) { |
| 527 | LOG(LS_ERROR) << "Failed to create OpenSSLCertificate from PEM string."; |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 528 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 529 | } |
| 530 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 531 | OpenSSLKeyPair* key_pair = |
| 532 | OpenSSLKeyPair::FromPrivateKeyPEMString(private_key); |
| 533 | if (!key_pair) { |
| 534 | LOG(LS_ERROR) << "Failed to create key pair from PEM string."; |
| 535 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 536 | } |
| 537 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 538 | return new OpenSSLIdentity(key_pair, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 539 | cert.release()); |
| 540 | } |
| 541 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 542 | const OpenSSLCertificate& OpenSSLIdentity::certificate() const { |
| 543 | return *certificate_; |
| 544 | } |
| 545 | |
| 546 | OpenSSLIdentity* OpenSSLIdentity::GetReference() const { |
| 547 | return new OpenSSLIdentity(key_pair_->GetReference(), |
| 548 | certificate_->GetReference()); |
| 549 | } |
| 550 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 551 | bool OpenSSLIdentity::ConfigureIdentity(SSL_CTX* ctx) { |
| 552 | // 1 is the documented success return code. |
| 553 | if (SSL_CTX_use_certificate(ctx, certificate_->x509()) != 1 || |
| 554 | SSL_CTX_use_PrivateKey(ctx, key_pair_->pkey()) != 1) { |
| 555 | LogSSLErrors("Configuring key and certificate"); |
| 556 | return false; |
| 557 | } |
| 558 | return true; |
| 559 | } |
| 560 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 561 | std::string OpenSSLIdentity::PrivateKeyToPEMString() const { |
| 562 | return key_pair_->PrivateKeyToPEMString(); |
| 563 | } |
| 564 | |
| 565 | std::string OpenSSLIdentity::PublicKeyToPEMString() const { |
| 566 | return key_pair_->PublicKeyToPEMString(); |
| 567 | } |
| 568 | |
| 569 | bool OpenSSLIdentity::operator==(const OpenSSLIdentity& other) const { |
| 570 | return *this->key_pair_ == *other.key_pair_ && |
| 571 | *this->certificate_ == *other.certificate_; |
| 572 | } |
| 573 | |
| 574 | bool OpenSSLIdentity::operator!=(const OpenSSLIdentity& other) const { |
| 575 | return !(*this == other); |
| 576 | } |
| 577 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 578 | } // namespace rtc |
| 579 | |
| 580 | #endif // HAVE_OPENSSL_SSL_H |