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