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*365; // one year, 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 char* common_name) {
100 LOG(LS_INFO) << "Making certificate for " << 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(name, NID_commonName, MBSTRING_UTF8,
132 (unsigned char*)common_name, -1, -1, 0) ||
133 !X509_set_subject_name(x509, name) ||
134 !X509_set_issuer_name(x509, name))
137 if (!X509_gmtime_adj(X509_get_notBefore(x509), CERTIFICATE_WINDOW) ||
138 !X509_gmtime_adj(X509_get_notAfter(x509), CERTIFICATE_LIFETIME))
141 if (!X509_sign(x509, pkey, EVP_sha1()))
144 BN_free(serial_number);
145 X509_NAME_free(name);
146 LOG(LS_INFO) << "Returning certificate";
150 BN_free(serial_number);
151 X509_NAME_free(name);
156 // This dumps the SSL error stack to the log.
157 static void LogSSLErrors(const std::string& prefix) {
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";
167 OpenSSLKeyPair* OpenSSLKeyPair::Generate() {
168 EVP_PKEY* pkey = MakeKey();
170 LogSSLErrors("Generating key pair");
173 return new OpenSSLKeyPair(pkey);
176 OpenSSLKeyPair::~OpenSSLKeyPair() {
177 EVP_PKEY_free(pkey_);
180 void OpenSSLKeyPair::AddReference() {
181 CRYPTO_add(&pkey_->references, 1, CRYPTO_LOCK_EVP_PKEY);
185 // Print a certificate to the log, for debugging.
186 static void PrintCert(X509* x509) {
187 BIO* temp_memory_bio = BIO_new(BIO_s_mem());
188 if (!temp_memory_bio) {
189 LOG_F(LS_ERROR) << "Failed to allocate temporary memory bio";
192 X509_print_ex(temp_memory_bio, x509, XN_FLAG_SEP_CPLUS_SPC, 0);
193 BIO_write(temp_memory_bio, "\0", 1);
195 BIO_get_mem_data(temp_memory_bio, &buffer);
196 LOG(LS_VERBOSE) << buffer;
197 BIO_free(temp_memory_bio);
201 OpenSSLCertificate* OpenSSLCertificate::Generate(
202 OpenSSLKeyPair* key_pair, const std::string& common_name) {
203 std::string actual_common_name = common_name;
204 if (actual_common_name.empty())
205 // Use a random string, arbitrarily 8chars long.
206 actual_common_name = CreateRandomString(8);
207 X509* x509 = MakeCertificate(key_pair->pkey(), actual_common_name.c_str());
209 LogSSLErrors("Generating certificate");
215 OpenSSLCertificate* ret = new OpenSSLCertificate(x509);
220 OpenSSLCertificate* OpenSSLCertificate::FromPEMString(
221 const std::string& pem_string) {
222 BIO* bio = BIO_new_mem_buf(const_cast<char*>(pem_string.c_str()), -1);
225 (void)BIO_set_close(bio, BIO_NOCLOSE);
226 BIO_set_mem_eof_return(bio, 0);
227 X509 *x509 = PEM_read_bio_X509(bio, NULL, NULL,
228 const_cast<char*>("\0"));
233 OpenSSLCertificate* ret = new OpenSSLCertificate(x509);
238 // NOTE: This implementation only functions correctly after InitializeSSL
239 // and before CleanupSSL.
240 bool OpenSSLCertificate::GetSignatureDigestAlgorithm(
241 std::string* algorithm) const {
242 return OpenSSLDigest::GetDigestName(
243 EVP_get_digestbyobj(x509_->sig_alg->algorithm), algorithm);
246 bool OpenSSLCertificate::ComputeDigest(const std::string &algorithm,
247 unsigned char *digest,
249 std::size_t *length) const {
250 return ComputeDigest(x509_, algorithm, digest, size, length);
253 bool OpenSSLCertificate::ComputeDigest(const X509 *x509,
254 const std::string &algorithm,
255 unsigned char *digest,
257 std::size_t *length) {
261 if (!OpenSSLDigest::GetDigestEVP(algorithm, &md))
264 if (size < static_cast<size_t>(EVP_MD_size(md)))
267 X509_digest(x509, md, digest, &n);
274 OpenSSLCertificate::~OpenSSLCertificate() {
278 std::string OpenSSLCertificate::ToPEMString() const {
279 BIO* bio = BIO_new(BIO_s_mem());
282 return std::string();
284 if (!PEM_write_bio_X509(bio, x509_)) {
287 return std::string();
289 BIO_write(bio, "\0", 1);
291 BIO_get_mem_data(bio, &buffer);
292 std::string ret(buffer);
297 void OpenSSLCertificate::ToDER(Buffer* der_buffer) const {
298 // In case of failure, make sure to leave the buffer empty.
299 der_buffer->SetData(NULL, 0);
301 // Calculates the DER representation of the certificate, from scratch.
302 BIO* bio = BIO_new(BIO_s_mem());
307 if (!i2d_X509_bio(bio, x509_)) {
313 size_t length = BIO_get_mem_data(bio, &data);
314 der_buffer->SetData(data, length);
318 void OpenSSLCertificate::AddReference() const {
319 ASSERT(x509_ != NULL);
320 CRYPTO_add(&x509_->references, 1, CRYPTO_LOCK_X509);
323 OpenSSLIdentity* OpenSSLIdentity::Generate(const std::string& common_name) {
324 OpenSSLKeyPair *key_pair = OpenSSLKeyPair::Generate();
326 OpenSSLCertificate *certificate =
327 OpenSSLCertificate::Generate(key_pair, common_name);
329 return new OpenSSLIdentity(key_pair, certificate);
332 LOG(LS_INFO) << "Identity generation failed";
336 SSLIdentity* OpenSSLIdentity::FromPEMStrings(
337 const std::string& private_key,
338 const std::string& certificate) {
339 scoped_ptr<OpenSSLCertificate> cert(
340 OpenSSLCertificate::FromPEMString(certificate));
342 LOG(LS_ERROR) << "Failed to create OpenSSLCertificate from PEM string.";
346 BIO* bio = BIO_new_mem_buf(const_cast<char*>(private_key.c_str()), -1);
348 LOG(LS_ERROR) << "Failed to create a new BIO buffer.";
351 (void)BIO_set_close(bio, BIO_NOCLOSE);
352 BIO_set_mem_eof_return(bio, 0);
353 EVP_PKEY *pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
354 const_cast<char*>("\0"));
358 LOG(LS_ERROR) << "Failed to create the private key from PEM string.";
362 return new OpenSSLIdentity(new OpenSSLKeyPair(pkey),
366 bool OpenSSLIdentity::ConfigureIdentity(SSL_CTX* ctx) {
367 // 1 is the documented success return code.
368 if (SSL_CTX_use_certificate(ctx, certificate_->x509()) != 1 ||
369 SSL_CTX_use_PrivateKey(ctx, key_pair_->pkey()) != 1) {
370 LogSSLErrors("Configuring key and certificate");
376 } // namespace talk_base
378 #endif // HAVE_OPENSSL_SSL_H