Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: webrtc/base/opensslidentity.cc

Issue 1329493005: Provide RSA2048 as per RFC (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Address hbos' concerns Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 &&
66 key_params.ec_curve() == EC_NIST_P256) {
juberti 2015/10/07 06:35:22 I think you want to check the curve inside this if
torbjorng (webrtc) 2015/10/07 13:30:03 Done.
68 EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); 67 EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
69 if (!pkey || !ec_key || !EC_KEY_generate_key(ec_key) || 68 if (!pkey || !ec_key || !EC_KEY_generate_key(ec_key) ||
70 !EVP_PKEY_assign_EC_KEY(pkey, ec_key)) { 69 !EVP_PKEY_assign_EC_KEY(pkey, ec_key)) {
71 EVP_PKEY_free(pkey); 70 EVP_PKEY_free(pkey);
72 EC_KEY_free(ec_key); 71 EC_KEY_free(ec_key);
73 LOG(LS_ERROR) << "Failed to make EC key pair"; 72 LOG(LS_ERROR) << "Failed to make EC key pair";
74 return NULL; 73 return NULL;
75 } 74 }
76 // ownership of ec_key struct was assigned, don't free it. 75 // ownership of ec_key struct was assigned, don't free it.
77 } else { 76 } else {
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 static void LogSSLErrors(const std::string& prefix) { 147 static void LogSSLErrors(const std::string& prefix) {
149 char error_buf[200]; 148 char error_buf[200];
150 unsigned long err; 149 unsigned long err;
151 150
152 while ((err = ERR_get_error()) != 0) { 151 while ((err = ERR_get_error()) != 0) {
153 ERR_error_string_n(err, error_buf, sizeof(error_buf)); 152 ERR_error_string_n(err, error_buf, sizeof(error_buf));
154 LOG(LS_ERROR) << prefix << ": " << error_buf << "\n"; 153 LOG(LS_ERROR) << prefix << ": " << error_buf << "\n";
155 } 154 }
156 } 155 }
157 156
158 OpenSSLKeyPair* OpenSSLKeyPair::Generate(KeyType key_type) { 157 OpenSSLKeyPair* OpenSSLKeyPair::Generate(const KeyParams& key_params) {
159 EVP_PKEY* pkey = MakeKey(key_type); 158 EVP_PKEY* pkey = MakeKey(key_params);
160 if (!pkey) { 159 if (!pkey) {
161 LogSSLErrors("Generating key pair"); 160 LogSSLErrors("Generating key pair");
162 return NULL; 161 return NULL;
163 } 162 }
164 return new OpenSSLKeyPair(pkey); 163 return new OpenSSLKeyPair(pkey);
165 } 164 }
166 165
166 OpenSSLKeyPair* OpenSSLKeyPair::Generate(KeyType key_type) {
167 return OpenSSLKeyPair::Generate(KeyParams(key_type));
168 }
169
167 OpenSSLKeyPair::~OpenSSLKeyPair() { 170 OpenSSLKeyPair::~OpenSSLKeyPair() {
168 EVP_PKEY_free(pkey_); 171 EVP_PKEY_free(pkey_);
169 } 172 }
170 173
171 OpenSSLKeyPair* OpenSSLKeyPair::GetReference() { 174 OpenSSLKeyPair* OpenSSLKeyPair::GetReference() {
172 AddReference(); 175 AddReference();
173 return new OpenSSLKeyPair(pkey_); 176 return new OpenSSLKeyPair(pkey_);
174 } 177 }
175 178
176 void OpenSSLKeyPair::AddReference() { 179 void OpenSSLKeyPair::AddReference() {
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 OpenSSLCertificate* certificate) 375 OpenSSLCertificate* certificate)
373 : key_pair_(key_pair), certificate_(certificate) { 376 : key_pair_(key_pair), certificate_(certificate) {
374 ASSERT(key_pair != NULL); 377 ASSERT(key_pair != NULL);
375 ASSERT(certificate != NULL); 378 ASSERT(certificate != NULL);
376 } 379 }
377 380
378 OpenSSLIdentity::~OpenSSLIdentity() = default; 381 OpenSSLIdentity::~OpenSSLIdentity() = default;
379 382
380 OpenSSLIdentity* OpenSSLIdentity::GenerateInternal( 383 OpenSSLIdentity* OpenSSLIdentity::GenerateInternal(
381 const SSLIdentityParams& params) { 384 const SSLIdentityParams& params) {
382 OpenSSLKeyPair* key_pair = OpenSSLKeyPair::Generate(params.key_type); 385 OpenSSLKeyPair* key_pair = OpenSSLKeyPair::Generate(params.key_params);
383 if (key_pair) { 386 if (key_pair) {
384 OpenSSLCertificate* certificate = 387 OpenSSLCertificate* certificate =
385 OpenSSLCertificate::Generate(key_pair, params); 388 OpenSSLCertificate::Generate(key_pair, params);
386 if (certificate) 389 if (certificate)
387 return new OpenSSLIdentity(key_pair, certificate); 390 return new OpenSSLIdentity(key_pair, certificate);
388 delete key_pair; 391 delete key_pair;
389 } 392 }
390 LOG(LS_INFO) << "Identity generation failed"; 393 LOG(LS_INFO) << "Identity generation failed";
391 return NULL; 394 return NULL;
392 } 395 }
393 396
394 OpenSSLIdentity* OpenSSLIdentity::Generate(const std::string& common_name, 397 OpenSSLIdentity* OpenSSLIdentity::Generate(const std::string& common_name,
395 KeyType key_type) { 398 const KeyParams& key_params) {
396 SSLIdentityParams params; 399 SSLIdentityParams params(key_params);
397 params.common_name = common_name; 400 params.common_name = common_name;
398 params.not_before = CERTIFICATE_WINDOW; 401 params.not_before = CERTIFICATE_WINDOW;
399 params.not_after = CERTIFICATE_LIFETIME; 402 params.not_after = CERTIFICATE_LIFETIME;
400 params.key_type = key_type;
401 return GenerateInternal(params); 403 return GenerateInternal(params);
402 } 404 }
403 405
404 OpenSSLIdentity* OpenSSLIdentity::GenerateForTest( 406 OpenSSLIdentity* OpenSSLIdentity::GenerateForTest(
405 const SSLIdentityParams& params) { 407 const SSLIdentityParams& params) {
406 return GenerateInternal(params); 408 return GenerateInternal(params);
407 } 409 }
408 410
409 SSLIdentity* OpenSSLIdentity::FromPEMStrings( 411 SSLIdentity* OpenSSLIdentity::FromPEMStrings(
410 const std::string& private_key, 412 const std::string& private_key,
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 SSL_CTX_use_PrivateKey(ctx, key_pair_->pkey()) != 1) { 452 SSL_CTX_use_PrivateKey(ctx, key_pair_->pkey()) != 1) {
451 LogSSLErrors("Configuring key and certificate"); 453 LogSSLErrors("Configuring key and certificate");
452 return false; 454 return false;
453 } 455 }
454 return true; 456 return true;
455 } 457 }
456 458
457 } // namespace rtc 459 } // namespace rtc
458 460
459 #endif // HAVE_OPENSSL_SSL_H 461 #endif // HAVE_OPENSSL_SSL_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698