blob: 0ebf20bd6b3ba829bc4d58612ed67560fa3d665b [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);
ssarohabbfed522016-12-11 18:42:07 -080064
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
torbjorng4e572472015-10-08 09:42:49 -070071 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 Granlundb6d4ec42015-08-17 14:08:59 +020081 EVP_PKEY_free(pkey);
torbjorng4e572472015-10-08 09:42:49 -070082 LOG(LS_ERROR) << "ECDSA key requested for unknown curve";
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020083 return NULL;
84 }
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020085 } else {
86 EVP_PKEY_free(pkey);
87 LOG(LS_ERROR) << "Key type requested not understood";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088 return NULL;
89 }
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020090
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091 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.
97static 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 Granlund46c9cc02015-12-01 13:06:34 +0100102 time_t epoch_off = 0; // Time offset since epoch.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103
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
torbjorngf8160352016-03-29 07:57:43 -0700119 if (!X509_set_version(x509, 2L)) // version 3
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120 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 Granlund46c9cc02015-12-01 13:06:34 +0100137 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.orgf0488722014-05-13 18:00:26 +0000139 goto error;
140
Joachim Bauch1b794d52015-05-12 03:32:11 +0200141 if (!X509_sign(x509, pkey, EVP_sha256()))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000142 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.
157static 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
torbjorng4e572472015-10-08 09:42:49 -0700167OpenSSLKeyPair* OpenSSLKeyPair::Generate(const KeyParams& key_params) {
168 EVP_PKEY* pkey = MakeKey(key_params);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000169 if (!pkey) {
170 LogSSLErrors("Generating key pair");
171 return NULL;
172 }
173 return new OpenSSLKeyPair(pkey);
174}
175
hbos6b470a92016-04-28 05:14:21 -0700176OpenSSLKeyPair* 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.orgf0488722014-05-13 18:00:26 +0000199OpenSSLKeyPair::~OpenSSLKeyPair() {
200 EVP_PKEY_free(pkey_);
201}
202
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000203OpenSSLKeyPair* OpenSSLKeyPair::GetReference() {
204 AddReference();
205 return new OpenSSLKeyPair(pkey_);
206}
207
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000208void OpenSSLKeyPair::AddReference() {
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100209#if defined(OPENSSL_IS_BORINGSSL)
Jiayang Liu770cc382015-05-28 15:36:29 -0700210 EVP_PKEY_up_ref(pkey_);
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100211#else
212 CRYPTO_add(&pkey_->references, 1, CRYPTO_LOCK_EVP_PKEY);
213#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000214}
215
hbos6b470a92016-04-28 05:14:21 -0700216std::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
238std::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
259bool OpenSSLKeyPair::operator==(const OpenSSLKeyPair& other) const {
260 return EVP_PKEY_cmp(this->pkey_, other.pkey_) == 1;
261}
262
263bool OpenSSLKeyPair::operator!=(const OpenSSLKeyPair& other) const {
264 return !(*this == other);
265}
266
tfarinaa41ab932015-10-30 16:08:48 -0700267#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000268// Print a certificate to the log, for debugging.
269static 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
284OpenSSLCertificate* 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 }
tfarinaa41ab932015-10-30 16:08:48 -0700296#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297 PrintCert(x509);
298#endif
299 OpenSSLCertificate* ret = new OpenSSLCertificate(x509);
300 X509_free(x509);
301 return ret;
302}
303
304OpenSSLCertificate* 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 Granlundb6d4ec42015-08-17 14:08:59 +0200310 X509* x509 = PEM_read_bio_X509(bio, NULL, NULL, const_cast<char*>("\0"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000311 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.
323bool OpenSSLCertificate::GetSignatureDigestAlgorithm(
324 std::string* algorithm) const {
JiaYang (佳扬) Liu01aeaee2015-04-22 12:18:33 -0700325 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.orgf0488722014-05-13 18:00:26 +0000364}
365
jbauch555604a2016-04-26 03:13:22 -0700366std::unique_ptr<SSLCertChain> OpenSSLCertificate::GetChain() const {
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000367 // Chains are not yet supported when using OpenSSL.
368 // OpenSSLStreamAdapter::SSLVerifyCallback currently requires the remote
369 // certificate to be self-signed.
kwibergf5d47862016-03-15 12:53:24 -0700370 return nullptr;
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000371}
372
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000373bool 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
380bool OpenSSLCertificate::ComputeDigest(const X509* x509,
381 const std::string& algorithm,
382 unsigned char* digest,
383 size_t size,
384 size_t* length) {
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200385 const EVP_MD* md;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000386 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
401OpenSSLCertificate::~OpenSSLCertificate() {
402 X509_free(x509_);
403}
404
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000405OpenSSLCertificate* OpenSSLCertificate::GetReference() const {
406 return new OpenSSLCertificate(x509_);
407}
408
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000409std::string OpenSSLCertificate::ToPEMString() const {
410 BIO* bio = BIO_new(BIO_s_mem());
411 if (!bio) {
andrew@webrtc.orga5b78692014-08-28 16:28:26 +0000412 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000413 }
414 if (!PEM_write_bio_X509(bio, x509_)) {
415 BIO_free(bio);
andrew@webrtc.orga5b78692014-08-28 16:28:26 +0000416 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000417 }
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
426void OpenSSLCertificate::ToDER(Buffer* der_buffer) const {
427 // In case of failure, make sure to leave the buffer empty.
Karl Wiberg94784372015-04-20 14:03:07 +0200428 der_buffer->SetSize(0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000429
430 // Calculates the DER representation of the certificate, from scratch.
431 BIO* bio = BIO_new(BIO_s_mem());
432 if (!bio) {
andrew@webrtc.orga5b78692014-08-28 16:28:26 +0000433 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000434 }
435 if (!i2d_X509_bio(bio, x509_)) {
436 BIO_free(bio);
andrew@webrtc.orga5b78692014-08-28 16:28:26 +0000437 FATAL() << "unreachable code";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000438 }
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
445void OpenSSLCertificate::AddReference() const {
446 ASSERT(x509_ != NULL);
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100447#if defined(OPENSSL_IS_BORINGSSL)
Jiayang Liu770cc382015-05-28 15:36:29 -0700448 X509_up_ref(x509_);
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100449#else
450 CRYPTO_add(&x509_->references, 1, CRYPTO_LOCK_X509);
451#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000452}
453
hbos6b470a92016-04-28 05:14:21 -0700454bool OpenSSLCertificate::operator==(const OpenSSLCertificate& other) const {
455 return X509_cmp(this->x509_, other.x509_) == 0;
456}
457
458bool OpenSSLCertificate::operator!=(const OpenSSLCertificate& other) const {
459 return !(*this == other);
460}
461
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100462// Documented in sslidentity.h.
463int64_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.org67186fe2015-03-09 22:21:53 +0000478OpenSSLIdentity::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
485OpenSSLIdentity::~OpenSSLIdentity() = default;
486
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000487OpenSSLIdentity* OpenSSLIdentity::GenerateInternal(
488 const SSLIdentityParams& params) {
torbjorng4e572472015-10-08 09:42:49 -0700489 OpenSSLKeyPair* key_pair = OpenSSLKeyPair::Generate(params.key_params);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000490 if (key_pair) {
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200491 OpenSSLCertificate* certificate =
492 OpenSSLCertificate::Generate(key_pair, params);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000493 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 Granlund1d846b22016-03-31 16:21:04 +0200501OpenSSLIdentity* OpenSSLIdentity::GenerateWithExpiration(
502 const std::string& common_name,
503 const KeyParams& key_params,
504 time_t certificate_lifetime) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000505 SSLIdentityParams params;
torbjorng4e572472015-10-08 09:42:49 -0700506 params.key_params = key_params;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000507 params.common_name = common_name;
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100508 time_t now = time(NULL);
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200509 params.not_before = now + kCertificateWindowInSeconds;
torbjornge8dc0812016-02-15 09:35:54 -0800510 params.not_after = now + certificate_lifetime;
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200511 if (params.not_before > params.not_after)
512 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000513 return GenerateInternal(params);
514}
515
516OpenSSLIdentity* OpenSSLIdentity::GenerateForTest(
517 const SSLIdentityParams& params) {
518 return GenerateInternal(params);
519}
520
521SSLIdentity* OpenSSLIdentity::FromPEMStrings(
522 const std::string& private_key,
523 const std::string& certificate) {
jbauch555604a2016-04-26 03:13:22 -0700524 std::unique_ptr<OpenSSLCertificate> cert(
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000525 OpenSSLCertificate::FromPEMString(certificate));
526 if (!cert) {
527 LOG(LS_ERROR) << "Failed to create OpenSSLCertificate from PEM string.";
hbos6b470a92016-04-28 05:14:21 -0700528 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000529 }
530
hbos6b470a92016-04-28 05:14:21 -0700531 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.orgf0488722014-05-13 18:00:26 +0000536 }
537
hbos6b470a92016-04-28 05:14:21 -0700538 return new OpenSSLIdentity(key_pair,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000539 cert.release());
540}
541
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000542const OpenSSLCertificate& OpenSSLIdentity::certificate() const {
543 return *certificate_;
544}
545
546OpenSSLIdentity* OpenSSLIdentity::GetReference() const {
547 return new OpenSSLIdentity(key_pair_->GetReference(),
548 certificate_->GetReference());
549}
550
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000551bool 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
hbos6b470a92016-04-28 05:14:21 -0700561std::string OpenSSLIdentity::PrivateKeyToPEMString() const {
562 return key_pair_->PrivateKeyToPEMString();
563}
564
565std::string OpenSSLIdentity::PublicKeyToPEMString() const {
566 return key_pair_->PublicKeyToPEMString();
567}
568
569bool OpenSSLIdentity::operator==(const OpenSSLIdentity& other) const {
570 return *this->key_pair_ == *other.key_pair_ &&
571 *this->certificate_ == *other.certificate_;
572}
573
574bool OpenSSLIdentity::operator!=(const OpenSSLIdentity& other) const {
575 return !(*this == other);
576}
577
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000578} // namespace rtc
579
580#endif // HAVE_OPENSSL_SSL_H