| OLD | NEW | 
|---|
| 1 /* | 1 /* | 
| 2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved. | 
| 3  * | 3  * | 
| 4  *  Use of this source code is governed by a BSD-style license | 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 | 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 | 6  *  tree. An additional intellectual property rights grant can be found | 
| 7  *  in the file PATENTS.  All contributing project authors may | 7  *  in the file PATENTS.  All contributing project authors may | 
| 8  *  be found in the AUTHORS file in the root of the source tree. | 8  *  be found in the AUTHORS file in the root of the source tree. | 
| 9  */ | 9  */ | 
| 10 | 10 | 
| (...skipping 15 matching lines...) Expand all  Loading... | 
| 26 #include "webrtc/base/helpers.h" | 26 #include "webrtc/base/helpers.h" | 
| 27 #include "webrtc/base/logging.h" | 27 #include "webrtc/base/logging.h" | 
| 28 #include "webrtc/base/openssl.h" | 28 #include "webrtc/base/openssl.h" | 
| 29 #include "webrtc/base/openssldigest.h" | 29 #include "webrtc/base/openssldigest.h" | 
| 30 | 30 | 
| 31 namespace rtc { | 31 namespace rtc { | 
| 32 | 32 | 
| 33 // We could have exposed a myriad of parameters for the crypto stuff, | 33 // We could have exposed a myriad of parameters for the crypto stuff, | 
| 34 // but keeping it simple seems best. | 34 // but keeping it simple seems best. | 
| 35 | 35 | 
| 36 // Strength of generated keys. Those are RSA. |  | 
| 37 static const int KEY_LENGTH = 1024; |  | 
| 38 |  | 
| 39 // Random bits for certificate serial number | 36 // Random bits for certificate serial number | 
| 40 static const int SERIAL_RAND_BITS = 64; | 37 static const int SERIAL_RAND_BITS = 64; | 
| 41 | 38 | 
| 42 // Certificate validity lifetime | 39 // Certificate validity lifetime | 
| 43 static const int CERTIFICATE_LIFETIME = 60*60*24*30;  // 30 days, arbitrarily | 40 static const int CERTIFICATE_LIFETIME = 60*60*24*30;  // 30 days, arbitrarily | 
| 44 // Certificate validity window. | 41 // Certificate validity window. | 
| 45 // This is to compensate for slightly incorrect system clocks. | 42 // This is to compensate for slightly incorrect system clocks. | 
| 46 static const int CERTIFICATE_WINDOW = -60*60*24; | 43 static const int CERTIFICATE_WINDOW = -60*60*24; | 
| 47 | 44 | 
| 48 // Generate a key pair. Caller is responsible for freeing the returned object. | 45 // Generate a key pair. Caller is responsible for freeing the returned object. | 
| 49 static EVP_PKEY* MakeKey(KeyType key_type) { | 46 static EVP_PKEY* MakeKey(const KeyParams& key_params) { | 
| 50   LOG(LS_INFO) << "Making key pair"; | 47   LOG(LS_INFO) << "Making key pair"; | 
| 51   EVP_PKEY* pkey = EVP_PKEY_new(); | 48   EVP_PKEY* pkey = EVP_PKEY_new(); | 
| 52   if (key_type == KT_RSA) { | 49   if (key_params.type() == KT_RSA) { | 
|  | 50     int key_length = key_params.rsa_params().mod_size; | 
| 53     BIGNUM* exponent = BN_new(); | 51     BIGNUM* exponent = BN_new(); | 
| 54     RSA* rsa = RSA_new(); | 52     RSA* rsa = RSA_new(); | 
| 55     if (!pkey || !exponent || !rsa || | 53     if (!pkey || !exponent || !rsa || | 
| 56         !BN_set_word(exponent, 0x10001) ||  // 65537 RSA exponent | 54         !BN_set_word(exponent, key_params.rsa_params().pub_exp) || | 
| 57         !RSA_generate_key_ex(rsa, KEY_LENGTH, exponent, NULL) || | 55         !RSA_generate_key_ex(rsa, key_length, exponent, NULL) || | 
| 58         !EVP_PKEY_assign_RSA(pkey, rsa)) { | 56         !EVP_PKEY_assign_RSA(pkey, rsa)) { | 
| 59       EVP_PKEY_free(pkey); | 57       EVP_PKEY_free(pkey); | 
| 60       BN_free(exponent); | 58       BN_free(exponent); | 
| 61       RSA_free(rsa); | 59       RSA_free(rsa); | 
| 62       LOG(LS_ERROR) << "Failed to make RSA key pair"; | 60       LOG(LS_ERROR) << "Failed to make RSA key pair"; | 
| 63       return NULL; | 61       return NULL; | 
| 64     } | 62     } | 
| 65     // ownership of rsa struct was assigned, don't free it. | 63     // ownership of rsa struct was assigned, don't free it. | 
| 66     BN_free(exponent); | 64     BN_free(exponent); | 
| 67   } else if (key_type == KT_ECDSA) { | 65   } else if (key_params.type() == KT_ECDSA) { | 
| 68     EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); | 66     if (key_params.ec_curve() == EC_NIST_P256) { | 
| 69     if (!pkey || !ec_key || !EC_KEY_generate_key(ec_key) || | 67       EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); | 
| 70         !EVP_PKEY_assign_EC_KEY(pkey, ec_key)) { | 68       if (!pkey || !ec_key || !EC_KEY_generate_key(ec_key) || | 
|  | 69           !EVP_PKEY_assign_EC_KEY(pkey, ec_key)) { | 
|  | 70         EVP_PKEY_free(pkey); | 
|  | 71         EC_KEY_free(ec_key); | 
|  | 72         LOG(LS_ERROR) << "Failed to make EC key pair"; | 
|  | 73         return NULL; | 
|  | 74       } | 
|  | 75       // ownership of ec_key struct was assigned, don't free it. | 
|  | 76     } else { | 
|  | 77       // Add generation of any other curves here. | 
| 71       EVP_PKEY_free(pkey); | 78       EVP_PKEY_free(pkey); | 
| 72       EC_KEY_free(ec_key); | 79       LOG(LS_ERROR) << "ECDSA key requested for unknown curve"; | 
| 73       LOG(LS_ERROR) << "Failed to make EC key pair"; |  | 
| 74       return NULL; | 80       return NULL; | 
| 75     } | 81     } | 
| 76     // ownership of ec_key struct was assigned, don't free it. |  | 
| 77   } else { | 82   } else { | 
| 78     EVP_PKEY_free(pkey); | 83     EVP_PKEY_free(pkey); | 
| 79     LOG(LS_ERROR) << "Key type requested not understood"; | 84     LOG(LS_ERROR) << "Key type requested not understood"; | 
| 80     return NULL; | 85     return NULL; | 
| 81   } | 86   } | 
| 82 | 87 | 
| 83   LOG(LS_INFO) << "Returning key pair"; | 88   LOG(LS_INFO) << "Returning key pair"; | 
| 84   return pkey; | 89   return pkey; | 
| 85 } | 90 } | 
| 86 | 91 | 
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 148 static void LogSSLErrors(const std::string& prefix) { | 153 static void LogSSLErrors(const std::string& prefix) { | 
| 149   char error_buf[200]; | 154   char error_buf[200]; | 
| 150   unsigned long err; | 155   unsigned long err; | 
| 151 | 156 | 
| 152   while ((err = ERR_get_error()) != 0) { | 157   while ((err = ERR_get_error()) != 0) { | 
| 153     ERR_error_string_n(err, error_buf, sizeof(error_buf)); | 158     ERR_error_string_n(err, error_buf, sizeof(error_buf)); | 
| 154     LOG(LS_ERROR) << prefix << ": " << error_buf << "\n"; | 159     LOG(LS_ERROR) << prefix << ": " << error_buf << "\n"; | 
| 155   } | 160   } | 
| 156 } | 161 } | 
| 157 | 162 | 
| 158 OpenSSLKeyPair* OpenSSLKeyPair::Generate(KeyType key_type) { | 163 OpenSSLKeyPair* OpenSSLKeyPair::Generate(const KeyParams& key_params) { | 
| 159   EVP_PKEY* pkey = MakeKey(key_type); | 164   EVP_PKEY* pkey = MakeKey(key_params); | 
| 160   if (!pkey) { | 165   if (!pkey) { | 
| 161     LogSSLErrors("Generating key pair"); | 166     LogSSLErrors("Generating key pair"); | 
| 162     return NULL; | 167     return NULL; | 
| 163   } | 168   } | 
| 164   return new OpenSSLKeyPair(pkey); | 169   return new OpenSSLKeyPair(pkey); | 
| 165 } | 170 } | 
| 166 | 171 | 
| 167 OpenSSLKeyPair::~OpenSSLKeyPair() { | 172 OpenSSLKeyPair::~OpenSSLKeyPair() { | 
| 168   EVP_PKEY_free(pkey_); | 173   EVP_PKEY_free(pkey_); | 
| 169 } | 174 } | 
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 372                                  OpenSSLCertificate* certificate) | 377                                  OpenSSLCertificate* certificate) | 
| 373     : key_pair_(key_pair), certificate_(certificate) { | 378     : key_pair_(key_pair), certificate_(certificate) { | 
| 374   ASSERT(key_pair != NULL); | 379   ASSERT(key_pair != NULL); | 
| 375   ASSERT(certificate != NULL); | 380   ASSERT(certificate != NULL); | 
| 376 } | 381 } | 
| 377 | 382 | 
| 378 OpenSSLIdentity::~OpenSSLIdentity() = default; | 383 OpenSSLIdentity::~OpenSSLIdentity() = default; | 
| 379 | 384 | 
| 380 OpenSSLIdentity* OpenSSLIdentity::GenerateInternal( | 385 OpenSSLIdentity* OpenSSLIdentity::GenerateInternal( | 
| 381     const SSLIdentityParams& params) { | 386     const SSLIdentityParams& params) { | 
| 382   OpenSSLKeyPair* key_pair = OpenSSLKeyPair::Generate(params.key_type); | 387   OpenSSLKeyPair* key_pair = OpenSSLKeyPair::Generate(params.key_params); | 
| 383   if (key_pair) { | 388   if (key_pair) { | 
| 384     OpenSSLCertificate* certificate = | 389     OpenSSLCertificate* certificate = | 
| 385         OpenSSLCertificate::Generate(key_pair, params); | 390         OpenSSLCertificate::Generate(key_pair, params); | 
| 386     if (certificate) | 391     if (certificate) | 
| 387       return new OpenSSLIdentity(key_pair, certificate); | 392       return new OpenSSLIdentity(key_pair, certificate); | 
| 388     delete key_pair; | 393     delete key_pair; | 
| 389   } | 394   } | 
| 390   LOG(LS_INFO) << "Identity generation failed"; | 395   LOG(LS_INFO) << "Identity generation failed"; | 
| 391   return NULL; | 396   return NULL; | 
| 392 } | 397 } | 
| 393 | 398 | 
| 394 OpenSSLIdentity* OpenSSLIdentity::Generate(const std::string& common_name, | 399 OpenSSLIdentity* OpenSSLIdentity::Generate(const std::string& common_name, | 
| 395                                            KeyType key_type) { | 400                                            const KeyParams& key_params) { | 
| 396   SSLIdentityParams params; | 401   SSLIdentityParams params; | 
|  | 402   params.key_params = key_params; | 
| 397   params.common_name = common_name; | 403   params.common_name = common_name; | 
| 398   params.not_before = CERTIFICATE_WINDOW; | 404   params.not_before = CERTIFICATE_WINDOW; | 
| 399   params.not_after = CERTIFICATE_LIFETIME; | 405   params.not_after = CERTIFICATE_LIFETIME; | 
| 400   params.key_type = key_type; |  | 
| 401   return GenerateInternal(params); | 406   return GenerateInternal(params); | 
| 402 } | 407 } | 
| 403 | 408 | 
| 404 OpenSSLIdentity* OpenSSLIdentity::GenerateForTest( | 409 OpenSSLIdentity* OpenSSLIdentity::GenerateForTest( | 
| 405     const SSLIdentityParams& params) { | 410     const SSLIdentityParams& params) { | 
| 406   return GenerateInternal(params); | 411   return GenerateInternal(params); | 
| 407 } | 412 } | 
| 408 | 413 | 
| 409 SSLIdentity* OpenSSLIdentity::FromPEMStrings( | 414 SSLIdentity* OpenSSLIdentity::FromPEMStrings( | 
| 410     const std::string& private_key, | 415     const std::string& private_key, | 
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 450      SSL_CTX_use_PrivateKey(ctx, key_pair_->pkey()) != 1) { | 455      SSL_CTX_use_PrivateKey(ctx, key_pair_->pkey()) != 1) { | 
| 451     LogSSLErrors("Configuring key and certificate"); | 456     LogSSLErrors("Configuring key and certificate"); | 
| 452     return false; | 457     return false; | 
| 453   } | 458   } | 
| 454   return true; | 459   return true; | 
| 455 } | 460 } | 
| 456 | 461 | 
| 457 }  // namespace rtc | 462 }  // namespace rtc | 
| 458 | 463 | 
| 459 #endif  // HAVE_OPENSSL_SSL_H | 464 #endif  // HAVE_OPENSSL_SSL_H | 
| OLD | NEW | 
|---|