3 * Copyright 2004--2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #if HAVE_OPENSSL_SSL_H
30 #include "talk/base/opensslidentity.h"
32 // Must be included first before openssl headers.
33 #include "talk/base/win32.h" // NOLINT
35 #include <openssl/ssl.h>
36 #include <openssl/bio.h>
37 #include <openssl/err.h>
38 #include <openssl/pem.h>
39 #include <openssl/bn.h>
40 #include <openssl/rsa.h>
41 #include <openssl/crypto.h>
43 #include "talk/base/checks.h"
44 #include "talk/base/helpers.h"
45 #include "talk/base/logging.h"
46 #include "talk/base/openssldigest.h"
50 // We could have exposed a myriad of parameters for the crypto stuff,
51 // but keeping it simple seems best.
53 // Strength of generated keys. Those are RSA.
54 static const int KEY_LENGTH = 1024;
56 // Random bits for certificate serial number
57 static const int SERIAL_RAND_BITS = 64;
59 // Certificate validity lifetime
60 static const int CERTIFICATE_LIFETIME = 60*60*24*30; // 30 days, arbitrarily
61 // Certificate validity window.
62 // This is to compensate for slightly incorrect system clocks.
63 static const int CERTIFICATE_WINDOW = -60*60*24;
65 // Generate a key pair. Caller is responsible for freeing the returned object.
66 static EVP_PKEY* MakeKey() {
67 LOG(LS_INFO) << "Making key pair";
68 EVP_PKEY* pkey = EVP_PKEY_new();
69 #if OPENSSL_VERSION_NUMBER < 0x00908000l
70 // Only RSA_generate_key is available. Use that.
71 RSA* rsa = RSA_generate_key(KEY_LENGTH, 0x10001, NULL, NULL);
72 if (!EVP_PKEY_assign_RSA(pkey, rsa)) {
78 // RSA_generate_key is deprecated. Use _ex version.
79 BIGNUM* exponent = BN_new();
81 if (!pkey || !exponent || !rsa ||
82 !BN_set_word(exponent, 0x10001) || // 65537 RSA exponent
83 !RSA_generate_key_ex(rsa, KEY_LENGTH, exponent, NULL) ||
84 !EVP_PKEY_assign_RSA(pkey, rsa)) {
90 // ownership of rsa struct was assigned, don't free it.
93 LOG(LS_INFO) << "Returning key pair";
97 // Generate a self-signed certificate, with the public key from the
98 // given key pair. Caller is responsible for freeing the returned object.
99 static X509* MakeCertificate(EVP_PKEY* pkey, const SSLIdentityParams& params) {
100 LOG(LS_INFO) << "Making certificate for " << params.common_name;
102 BIGNUM* serial_number = NULL;
103 X509_NAME* name = NULL;
105 if ((x509=X509_new()) == NULL)
108 if (!X509_set_pubkey(x509, pkey))
112 // temporary reference to serial number inside x509 struct
113 ASN1_INTEGER* asn1_serial_number;
114 if ((serial_number = BN_new()) == NULL ||
115 !BN_pseudo_rand(serial_number, SERIAL_RAND_BITS, 0, 0) ||
116 (asn1_serial_number = X509_get_serialNumber(x509)) == NULL ||
117 !BN_to_ASN1_INTEGER(serial_number, asn1_serial_number))
120 if (!X509_set_version(x509, 0L)) // version 1
123 // There are a lot of possible components for the name entries. In
124 // our P2P SSL mode however, the certificates are pre-exchanged
125 // (through the secure XMPP channel), and so the certificate
126 // identification is arbitrary. It can't be empty, so we set some
127 // arbitrary common_name. Note that this certificate goes out in
128 // clear during SSL negotiation, so there may be a privacy issue in
129 // putting anything recognizable here.
130 if ((name = X509_NAME_new()) == NULL ||
131 !X509_NAME_add_entry_by_NID(
132 name, NID_commonName, MBSTRING_UTF8,
133 (unsigned char*)params.common_name.c_str(), -1, -1, 0) ||
134 !X509_set_subject_name(x509, name) ||
135 !X509_set_issuer_name(x509, name))
138 if (!X509_gmtime_adj(X509_get_notBefore(x509), params.not_before) ||
139 !X509_gmtime_adj(X509_get_notAfter(x509), params.not_after))
142 if (!X509_sign(x509, pkey, EVP_sha1()))
145 BN_free(serial_number);
146 X509_NAME_free(name);
147 LOG(LS_INFO) << "Returning certificate";
151 BN_free(serial_number);
152 X509_NAME_free(name);
157 // This dumps the SSL error stack to the log.
158 static void LogSSLErrors(const std::string& prefix) {
162 while ((err = ERR_get_error()) != 0) {
163 ERR_error_string_n(err, error_buf, sizeof(error_buf));
164 LOG(LS_ERROR) << prefix << ": " << error_buf << "\n";
168 OpenSSLKeyPair* OpenSSLKeyPair::Generate() {
169 EVP_PKEY* pkey = MakeKey();
171 LogSSLErrors("Generating key pair");
174 return new OpenSSLKeyPair(pkey);
177 OpenSSLKeyPair::~OpenSSLKeyPair() {
178 EVP_PKEY_free(pkey_);
181 void OpenSSLKeyPair::AddReference() {
182 CRYPTO_add(&pkey_->references, 1, CRYPTO_LOCK_EVP_PKEY);
186 // Print a certificate to the log, for debugging.
187 static void PrintCert(X509* x509) {
188 BIO* temp_memory_bio = BIO_new(BIO_s_mem());
189 if (!temp_memory_bio) {
190 LOG_F(LS_ERROR) << "Failed to allocate temporary memory bio";
193 X509_print_ex(temp_memory_bio, x509, XN_FLAG_SEP_CPLUS_SPC, 0);
194 BIO_write(temp_memory_bio, "\0", 1);
196 BIO_get_mem_data(temp_memory_bio, &buffer);
197 LOG(LS_VERBOSE) << buffer;
198 BIO_free(temp_memory_bio);
202 OpenSSLCertificate* OpenSSLCertificate::Generate(
203 OpenSSLKeyPair* key_pair, const SSLIdentityParams& params) {
204 SSLIdentityParams actual_params(params);
205 if (actual_params.common_name.empty()) {
206 // Use a random string, arbitrarily 8chars long.
207 actual_params.common_name = CreateRandomString(8);
209 X509* x509 = MakeCertificate(key_pair->pkey(), actual_params);
211 LogSSLErrors("Generating certificate");
217 OpenSSLCertificate* ret = new OpenSSLCertificate(x509);
222 OpenSSLCertificate* OpenSSLCertificate::FromPEMString(
223 const std::string& pem_string) {
224 BIO* bio = BIO_new_mem_buf(const_cast<char*>(pem_string.c_str()), -1);
227 (void)BIO_set_close(bio, BIO_NOCLOSE);
228 BIO_set_mem_eof_return(bio, 0);
229 X509 *x509 = PEM_read_bio_X509(bio, NULL, NULL,
230 const_cast<char*>("\0"));
235 OpenSSLCertificate* ret = new OpenSSLCertificate(x509);
240 // NOTE: This implementation only functions correctly after InitializeSSL
241 // and before CleanupSSL.
242 bool OpenSSLCertificate::GetSignatureDigestAlgorithm(
243 std::string* algorithm) const {
244 return OpenSSLDigest::GetDigestName(
245 EVP_get_digestbyobj(x509_->sig_alg->algorithm), algorithm);
248 bool OpenSSLCertificate::ComputeDigest(const std::string &algorithm,
249 unsigned char *digest,
251 std::size_t *length) const {
252 return ComputeDigest(x509_, algorithm, digest, size, length);
255 bool OpenSSLCertificate::ComputeDigest(const X509 *x509,
256 const std::string &algorithm,
257 unsigned char *digest,
259 std::size_t *length) {
263 if (!OpenSSLDigest::GetDigestEVP(algorithm, &md))
266 if (size < static_cast<size_t>(EVP_MD_size(md)))
269 X509_digest(x509, md, digest, &n);
276 OpenSSLCertificate::~OpenSSLCertificate() {
280 std::string OpenSSLCertificate::ToPEMString() const {
281 BIO* bio = BIO_new(BIO_s_mem());
284 return std::string();
286 if (!PEM_write_bio_X509(bio, x509_)) {
289 return std::string();
291 BIO_write(bio, "\0", 1);
293 BIO_get_mem_data(bio, &buffer);
294 std::string ret(buffer);
299 void OpenSSLCertificate::ToDER(Buffer* der_buffer) const {
300 // In case of failure, make sure to leave the buffer empty.
301 der_buffer->SetData(NULL, 0);
303 // Calculates the DER representation of the certificate, from scratch.
304 BIO* bio = BIO_new(BIO_s_mem());
309 if (!i2d_X509_bio(bio, x509_)) {
315 size_t length = BIO_get_mem_data(bio, &data);
316 der_buffer->SetData(data, length);
320 void OpenSSLCertificate::AddReference() const {
321 ASSERT(x509_ != NULL);
322 CRYPTO_add(&x509_->references, 1, CRYPTO_LOCK_X509);
325 OpenSSLIdentity* OpenSSLIdentity::GenerateInternal(
326 const SSLIdentityParams& params) {
327 OpenSSLKeyPair *key_pair = OpenSSLKeyPair::Generate();
329 OpenSSLCertificate *certificate = OpenSSLCertificate::Generate(
332 return new OpenSSLIdentity(key_pair, certificate);
335 LOG(LS_INFO) << "Identity generation failed";
339 OpenSSLIdentity* OpenSSLIdentity::Generate(const std::string& common_name) {
340 SSLIdentityParams params;
341 params.common_name = common_name;
342 params.not_before = CERTIFICATE_WINDOW;
343 params.not_after = CERTIFICATE_LIFETIME;
344 return GenerateInternal(params);
347 OpenSSLIdentity* OpenSSLIdentity::GenerateForTest(
348 const SSLIdentityParams& params) {
349 return GenerateInternal(params);
352 SSLIdentity* OpenSSLIdentity::FromPEMStrings(
353 const std::string& private_key,
354 const std::string& certificate) {
355 scoped_ptr<OpenSSLCertificate> cert(
356 OpenSSLCertificate::FromPEMString(certificate));
358 LOG(LS_ERROR) << "Failed to create OpenSSLCertificate from PEM string.";
362 BIO* bio = BIO_new_mem_buf(const_cast<char*>(private_key.c_str()), -1);
364 LOG(LS_ERROR) << "Failed to create a new BIO buffer.";
367 (void)BIO_set_close(bio, BIO_NOCLOSE);
368 BIO_set_mem_eof_return(bio, 0);
369 EVP_PKEY *pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
370 const_cast<char*>("\0"));
374 LOG(LS_ERROR) << "Failed to create the private key from PEM string.";
378 return new OpenSSLIdentity(new OpenSSLKeyPair(pkey),
382 bool OpenSSLIdentity::ConfigureIdentity(SSL_CTX* ctx) {
383 // 1 is the documented success return code.
384 if (SSL_CTX_use_certificate(ctx, certificate_->x509()) != 1 ||
385 SSL_CTX_use_PrivateKey(ctx, key_pair_->pkey()) != 1) {
386 LogSSLErrors("Configuring key and certificate");
392 } // namespace talk_base
394 #endif // HAVE_OPENSSL_SSL_H