blob: 58a0cd8adeb2b2f48b7851cc02493c7918108fca [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
jbauch555604a2016-04-26 03:13:22 -070015#include <memory>
16
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017// 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
33namespace 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.orgf0488722014-05-13 18:00:26 +000038// Random bits for certificate serial number
39static const int SERIAL_RAND_BITS = 64;
40
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041// Generate a key pair. Caller is responsible for freeing the returned object.
torbjorng4e572472015-10-08 09:42:49 -070042static EVP_PKEY* MakeKey(const KeyParams& key_params) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043 LOG(LS_INFO) << "Making key pair";
44 EVP_PKEY* pkey = EVP_PKEY_new();
torbjorng4e572472015-10-08 09:42:49 -070045 if (key_params.type() == KT_RSA) {
46 int key_length = key_params.rsa_params().mod_size;
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020047 BIGNUM* exponent = BN_new();
48 RSA* rsa = RSA_new();
49 if (!pkey || !exponent || !rsa ||
torbjorng4e572472015-10-08 09:42:49 -070050 !BN_set_word(exponent, key_params.rsa_params().pub_exp) ||
51 !RSA_generate_key_ex(rsa, key_length, exponent, NULL) ||
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020052 !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.orgf0488722014-05-13 18:00:26 +000060 BN_free(exponent);
torbjorng4e572472015-10-08 09:42:49 -070061 } 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 Granlundb6d4ec42015-08-17 14:08:59 +020074 EVP_PKEY_free(pkey);
torbjorng4e572472015-10-08 09:42:49 -070075 LOG(LS_ERROR) << "ECDSA key requested for unknown curve";
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020076 return NULL;
77 }
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020078 } else {
79 EVP_PKEY_free(pkey);
80 LOG(LS_ERROR) << "Key type requested not understood";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 return NULL;
82 }
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020083
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084 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.
90static 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 Granlund46c9cc02015-12-01 13:06:34 +010095 time_t epoch_off = 0; // Time offset since epoch.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096
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
torbjorngf8160352016-03-29 07:57:43 -0700112 if (!X509_set_version(x509, 2L)) // version 3
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113 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 Granlund46c9cc02015-12-01 13:06:34 +0100130 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.orgf0488722014-05-13 18:00:26 +0000132 goto error;
133
Joachim Bauch1b794d52015-05-12 03:32:11 +0200134 if (!X509_sign(x509, pkey, EVP_sha256()))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135 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.
150static 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
torbjorng4e572472015-10-08 09:42:49 -0700160OpenSSLKeyPair* OpenSSLKeyPair::Generate(const KeyParams& key_params) {
161 EVP_PKEY* pkey = MakeKey(key_params);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000162 if (!pkey) {
163 LogSSLErrors("Generating key pair");
164 return NULL;
165 }
166 return new OpenSSLKeyPair(pkey);
167}
168
hbos6b470a92016-04-28 05:14:21 -0700169OpenSSLKeyPair* 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.orgf0488722014-05-13 18:00:26 +0000192OpenSSLKeyPair::~OpenSSLKeyPair() {
193 EVP_PKEY_free(pkey_);
194}
195
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000196OpenSSLKeyPair* OpenSSLKeyPair::GetReference() {
197 AddReference();
198 return new OpenSSLKeyPair(pkey_);
199}
200
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000201void OpenSSLKeyPair::AddReference() {
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100202#if defined(OPENSSL_IS_BORINGSSL)
Jiayang Liu770cc382015-05-28 15:36:29 -0700203 EVP_PKEY_up_ref(pkey_);
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100204#else
205 CRYPTO_add(&pkey_->references, 1, CRYPTO_LOCK_EVP_PKEY);
206#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000207}
208
hbos6b470a92016-04-28 05:14:21 -0700209std::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
231std::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
252bool OpenSSLKeyPair::operator==(const OpenSSLKeyPair& other) const {
253 return EVP_PKEY_cmp(this->pkey_, other.pkey_) == 1;
254}
255
256bool OpenSSLKeyPair::operator!=(const OpenSSLKeyPair& other) const {
257 return !(*this == other);
258}
259
tfarinaa41ab932015-10-30 16:08:48 -0700260#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000261// Print a certificate to the log, for debugging.
262static 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
277OpenSSLCertificate* 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 }
tfarinaa41ab932015-10-30 16:08:48 -0700289#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000290 PrintCert(x509);
291#endif
292 OpenSSLCertificate* ret = new OpenSSLCertificate(x509);
293 X509_free(x509);
294 return ret;
295}
296
297OpenSSLCertificate* 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 Granlundb6d4ec42015-08-17 14:08:59 +0200303 X509* x509 = PEM_read_bio_X509(bio, NULL, NULL, const_cast<char*>("\0"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000304 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.
316bool OpenSSLCertificate::GetSignatureDigestAlgorithm(
317 std::string* algorithm) const {
JiaYang (佳扬) Liu01aeaee2015-04-22 12:18:33 -0700318 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.orgf0488722014-05-13 18:00:26 +0000357}
358
jbauch555604a2016-04-26 03:13:22 -0700359std::unique_ptr<SSLCertChain> OpenSSLCertificate::GetChain() const {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000360 // Chains are not yet supported when using OpenSSL.
361 // OpenSSLStreamAdapter::SSLVerifyCallback currently requires the remote
362 // certificate to be self-signed.
kwibergf5d47862016-03-15 12:53:24 -0700363 return nullptr;
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000364}
365
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000366bool 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
373bool OpenSSLCertificate::ComputeDigest(const X509* x509,
374 const std::string& algorithm,
375 unsigned char* digest,
376 size_t size,
377 size_t* length) {
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200378 const EVP_MD* md;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000379 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
394OpenSSLCertificate::~OpenSSLCertificate() {
395 X509_free(x509_);
396}
397
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000398OpenSSLCertificate* OpenSSLCertificate::GetReference() const {
399 return new OpenSSLCertificate(x509_);
400}
401
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000402std::string OpenSSLCertificate::ToPEMString() const {
403 BIO* bio = BIO_new(BIO_s_mem());
404 if (!bio) {
andrew@webrtc.orga5b78692014-08-28 16:28:26 +0000405 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000406 }
407 if (!PEM_write_bio_X509(bio, x509_)) {
408 BIO_free(bio);
andrew@webrtc.orga5b78692014-08-28 16:28:26 +0000409 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000410 }
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
419void OpenSSLCertificate::ToDER(Buffer* der_buffer) const {
420 // In case of failure, make sure to leave the buffer empty.
Karl Wiberg94784372015-04-20 14:03:07 +0200421 der_buffer->SetSize(0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000422
423 // Calculates the DER representation of the certificate, from scratch.
424 BIO* bio = BIO_new(BIO_s_mem());
425 if (!bio) {
andrew@webrtc.orga5b78692014-08-28 16:28:26 +0000426 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000427 }
428 if (!i2d_X509_bio(bio, x509_)) {
429 BIO_free(bio);
andrew@webrtc.orga5b78692014-08-28 16:28:26 +0000430 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000431 }
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
438void OpenSSLCertificate::AddReference() const {
439 ASSERT(x509_ != NULL);
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100440#if defined(OPENSSL_IS_BORINGSSL)
Jiayang Liu770cc382015-05-28 15:36:29 -0700441 X509_up_ref(x509_);
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100442#else
443 CRYPTO_add(&x509_->references, 1, CRYPTO_LOCK_X509);
444#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000445}
446
hbos6b470a92016-04-28 05:14:21 -0700447bool OpenSSLCertificate::operator==(const OpenSSLCertificate& other) const {
448 return X509_cmp(this->x509_, other.x509_) == 0;
449}
450
451bool OpenSSLCertificate::operator!=(const OpenSSLCertificate& other) const {
452 return !(*this == other);
453}
454
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100455// Documented in sslidentity.h.
456int64_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.org67186fe2015-03-09 22:21:53 +0000471OpenSSLIdentity::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
478OpenSSLIdentity::~OpenSSLIdentity() = default;
479
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000480OpenSSLIdentity* OpenSSLIdentity::GenerateInternal(
481 const SSLIdentityParams& params) {
torbjorng4e572472015-10-08 09:42:49 -0700482 OpenSSLKeyPair* key_pair = OpenSSLKeyPair::Generate(params.key_params);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000483 if (key_pair) {
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200484 OpenSSLCertificate* certificate =
485 OpenSSLCertificate::Generate(key_pair, params);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000486 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 Granlund1d846b22016-03-31 16:21:04 +0200494OpenSSLIdentity* OpenSSLIdentity::GenerateWithExpiration(
495 const std::string& common_name,
496 const KeyParams& key_params,
497 time_t certificate_lifetime) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000498 SSLIdentityParams params;
torbjorng4e572472015-10-08 09:42:49 -0700499 params.key_params = key_params;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000500 params.common_name = common_name;
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100501 time_t now = time(NULL);
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200502 params.not_before = now + kCertificateWindowInSeconds;
torbjornge8dc0812016-02-15 09:35:54 -0800503 params.not_after = now + certificate_lifetime;
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200504 if (params.not_before > params.not_after)
505 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000506 return GenerateInternal(params);
507}
508
509OpenSSLIdentity* OpenSSLIdentity::GenerateForTest(
510 const SSLIdentityParams& params) {
511 return GenerateInternal(params);
512}
513
514SSLIdentity* OpenSSLIdentity::FromPEMStrings(
515 const std::string& private_key,
516 const std::string& certificate) {
jbauch555604a2016-04-26 03:13:22 -0700517 std::unique_ptr<OpenSSLCertificate> cert(
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000518 OpenSSLCertificate::FromPEMString(certificate));
519 if (!cert) {
520 LOG(LS_ERROR) << "Failed to create OpenSSLCertificate from PEM string.";
hbos6b470a92016-04-28 05:14:21 -0700521 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000522 }
523
hbos6b470a92016-04-28 05:14:21 -0700524 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.orgf0488722014-05-13 18:00:26 +0000529 }
530
hbos6b470a92016-04-28 05:14:21 -0700531 return new OpenSSLIdentity(key_pair,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000532 cert.release());
533}
534
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000535const OpenSSLCertificate& OpenSSLIdentity::certificate() const {
536 return *certificate_;
537}
538
539OpenSSLIdentity* OpenSSLIdentity::GetReference() const {
540 return new OpenSSLIdentity(key_pair_->GetReference(),
541 certificate_->GetReference());
542}
543
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000544bool 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
hbos6b470a92016-04-28 05:14:21 -0700554std::string OpenSSLIdentity::PrivateKeyToPEMString() const {
555 return key_pair_->PrivateKeyToPEMString();
556}
557
558std::string OpenSSLIdentity::PublicKeyToPEMString() const {
559 return key_pair_->PublicKeyToPEMString();
560}
561
562bool OpenSSLIdentity::operator==(const OpenSSLIdentity& other) const {
563 return *this->key_pair_ == *other.key_pair_ &&
564 *this->certificate_ == *other.certificate_;
565}
566
567bool OpenSSLIdentity::operator!=(const OpenSSLIdentity& other) const {
568 return !(*this == other);
569}
570
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000571} // namespace rtc
572
573#endif // HAVE_OPENSSL_SSL_H