Move more WebContentsObserver methods from RVH to RFH.
[chromium-blink-merge.git] / crypto / rsa_private_key_openssl.cc
blob0df173093ad9237faac9b94e30369fbd054e5ffe
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "crypto/rsa_private_key.h"
7 #include <openssl/bio.h>
8 #include <openssl/bn.h>
9 #include <openssl/evp.h>
10 #include <openssl/pkcs12.h>
11 #include <openssl/rsa.h>
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "crypto/openssl_util.h"
16 #include "crypto/scoped_openssl_types.h"
18 namespace crypto {
20 namespace {
22 typedef ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free>::Type
23 ScopedPKCS8_PRIV_KEY_INFO;
25 // Function pointer definition, for injecting the required key export function
26 // into ExportKey, below. The supplied function should export EVP_PKEY into
27 // the supplied BIO, returning 1 on success or 0 on failure.
28 typedef int (ExportFunction)(BIO*, EVP_PKEY*);
30 // Helper to export |key| into |output| via the specified ExportFunction.
31 bool ExportKey(EVP_PKEY* key,
32 ExportFunction export_fn,
33 std::vector<uint8>* output) {
34 if (!key)
35 return false;
37 OpenSSLErrStackTracer err_tracer(FROM_HERE);
38 ScopedBIO bio(BIO_new(BIO_s_mem()));
40 int res = export_fn(bio.get(), key);
41 if (!res)
42 return false;
44 char* data = NULL;
45 long len = BIO_get_mem_data(bio.get(), &data);
46 if (!data || len < 0)
47 return false;
49 output->assign(data, data + len);
50 return true;
53 } // namespace
55 // static
56 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) {
57 OpenSSLErrStackTracer err_tracer(FROM_HERE);
59 ScopedRSA rsa_key(RSA_new());
60 ScopedBIGNUM bn(BN_new());
61 if (!rsa_key.get() || !bn.get() || !BN_set_word(bn.get(), 65537L))
62 return NULL;
64 if (!RSA_generate_key_ex(rsa_key.get(), num_bits, bn.get(), NULL))
65 return NULL;
67 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey);
68 result->key_ = EVP_PKEY_new();
69 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get()))
70 return NULL;
72 return result.release();
75 // static
76 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo(
77 const std::vector<uint8>& input) {
78 if (input.empty())
79 return NULL;
81 OpenSSLErrStackTracer err_tracer(FROM_HERE);
83 // Importing is a little more involved than exporting, as we must first
84 // PKCS#8 decode the input, and then import the EVP_PKEY from Private Key
85 // Info structure returned.
87 // TODO(davidben): This should check that |ptr| advanced to the end of |input|
88 // to ensure there is no trailing data.
89 const uint8_t* ptr = &input[0];
90 ScopedPKCS8_PRIV_KEY_INFO p8inf(
91 d2i_PKCS8_PRIV_KEY_INFO(nullptr, &ptr, input.size()));
92 if (!p8inf.get())
93 return NULL;
95 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey);
96 result->key_ = EVP_PKCS82PKEY(p8inf.get());
97 if (!result->key_)
98 return NULL;
100 return result.release();
103 // static
104 RSAPrivateKey* RSAPrivateKey::CreateFromKey(EVP_PKEY* key) {
105 DCHECK(key);
106 if (EVP_PKEY_type(key->type) != EVP_PKEY_RSA)
107 return NULL;
108 RSAPrivateKey* copy = new RSAPrivateKey();
109 copy->key_ = EVP_PKEY_dup(key);
110 return copy;
113 RSAPrivateKey::RSAPrivateKey()
114 : key_(NULL) {
117 RSAPrivateKey::~RSAPrivateKey() {
118 if (key_)
119 EVP_PKEY_free(key_);
122 RSAPrivateKey* RSAPrivateKey::Copy() const {
123 scoped_ptr<RSAPrivateKey> copy(new RSAPrivateKey());
124 ScopedRSA rsa(EVP_PKEY_get1_RSA(key_));
125 if (!rsa)
126 return NULL;
127 copy->key_ = EVP_PKEY_new();
128 if (!EVP_PKEY_set1_RSA(copy->key_, rsa.get()))
129 return NULL;
130 return copy.release();
133 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const {
134 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output);
137 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) const {
138 return ExportKey(key_, i2d_PUBKEY_bio, output);
141 } // namespace crypto