blob: 64a627e5fbfa293591952fdd68aa1194df5e7be5 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]70372d42010-10-22 13:12:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#include "crypto/rsa_private_key.h"
[email protected]70372d42010-10-22 13:12:346
[email protected]1f923942010-11-17 14:39:227#include <openssl/evp.h>
8#include <openssl/pkcs12.h>
9#include <openssl/rsa.h>
10
[email protected]70372d42010-10-22 13:12:3411#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/scoped_ptr.h"
[email protected]4b559b4d2011-04-14 17:37:1413#include "crypto/openssl_util.h"
[email protected]70372d42010-10-22 13:12:3414
[email protected]4b559b4d2011-04-14 17:37:1415namespace crypto {
[email protected]70372d42010-10-22 13:12:3416
[email protected]1f923942010-11-17 14:39:2217namespace {
18
19// Function pointer definition, for injecting the required key export function
20// into ExportKey, below. The supplied function should export EVP_PKEY into
21// the supplied BIO, returning 1 on success or 0 on failure.
22typedef int (ExportFunction)(BIO*, EVP_PKEY*);
23
24// Helper to export |key| into |output| via the specified ExportFunction.
25bool ExportKey(EVP_PKEY* key,
26 ExportFunction export_fn,
27 std::vector<uint8>* output) {
28 if (!key)
29 return false;
30
[email protected]be796bb2010-11-18 15:43:4331 OpenSSLErrStackTracer err_tracer(FROM_HERE);
[email protected]1f923942010-11-17 14:39:2232 ScopedOpenSSL<BIO, BIO_free_all> bio(BIO_new(BIO_s_mem()));
33
34 int res = export_fn(bio.get(), key);
[email protected]1f923942010-11-17 14:39:2235 if (!res)
36 return false;
37
38 char* data = NULL;
39 long len = BIO_get_mem_data(bio.get(), &data);
40 if (!data || len < 0)
41 return false;
42
[email protected]d641b6e2011-07-20 11:07:1643 output->assign(data, data + len);
[email protected]1f923942010-11-17 14:39:2244 return true;
[email protected]70372d42010-10-22 13:12:3445}
46
[email protected]1f923942010-11-17 14:39:2247} // namespace
48
[email protected]70372d42010-10-22 13:12:3449// static
50RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) {
[email protected]be796bb2010-11-18 15:43:4351 OpenSSLErrStackTracer err_tracer(FROM_HERE);
[email protected]fed406f82010-12-08 15:40:4552
53 ScopedOpenSSL<RSA, RSA_free> rsa_key(RSA_new());
54 ScopedOpenSSL<BIGNUM, BN_free> bn(BN_new());
55 if (!rsa_key.get() || !bn.get() || !BN_set_word(bn.get(), 65537L))
56 return NULL;
57
58 if (!RSA_generate_key_ex(rsa_key.get(), num_bits, bn.get(), NULL))
[email protected]1f923942010-11-17 14:39:2259 return NULL;
60
61 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey);
62 result->key_ = EVP_PKEY_new();
63 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get()))
64 return NULL;
65
66 return result.release();
[email protected]70372d42010-10-22 13:12:3467}
68
69// static
70RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) {
[email protected]70372d42010-10-22 13:12:3471 NOTIMPLEMENTED();
72 return NULL;
73}
74
75// static
76RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo(
77 const std::vector<uint8>& input) {
[email protected]d641b6e2011-07-20 11:07:1678 if (input.empty())
79 return NULL;
[email protected]1f923942010-11-17 14:39:2280
[email protected]d641b6e2011-07-20 11:07:1681 OpenSSLErrStackTracer err_tracer(FROM_HERE);
[email protected]1f923942010-11-17 14:39:2282 // BIO_new_mem_buf is not const aware, but it does not modify the buffer.
[email protected]d641b6e2011-07-20 11:07:1683 char* data = reinterpret_cast<char*>(const_cast<uint8*>(&input[0]));
[email protected]1f923942010-11-17 14:39:2284 ScopedOpenSSL<BIO, BIO_free_all> bio(BIO_new_mem_buf(data, input.size()));
85 if (!bio.get())
86 return NULL;
87
88 // Importing is a little more involved than exporting, as we must first
89 // PKCS#8 decode the input, and then import the EVP_PKEY from Private Key
90 // Info structure returned.
91 ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free> p8inf(
92 d2i_PKCS8_PRIV_KEY_INFO_bio(bio.get(), NULL));
[email protected]1f923942010-11-17 14:39:2293 if (!p8inf.get())
94 return NULL;
95
96 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey);
97 result->key_ = EVP_PKCS82PKEY(p8inf.get());
[email protected]1f923942010-11-17 14:39:2298 if (!result->key_)
99 return NULL;
100
101 return result.release();
[email protected]70372d42010-10-22 13:12:34102}
103
104// static
105RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo(
106 const std::vector<uint8>& input) {
[email protected]1f923942010-11-17 14:39:22107 NOTIMPLEMENTED();
108 return NULL;
[email protected]70372d42010-10-22 13:12:34109}
110
111// static
112RSAPrivateKey* RSAPrivateKey::FindFromPublicKeyInfo(
113 const std::vector<uint8>& input) {
114 NOTIMPLEMENTED();
115 return NULL;
116}
117
[email protected]1f923942010-11-17 14:39:22118RSAPrivateKey::RSAPrivateKey()
119 : key_(NULL) {
[email protected]70372d42010-10-22 13:12:34120}
121
122RSAPrivateKey::~RSAPrivateKey() {
[email protected]1f923942010-11-17 14:39:22123 if (key_)
124 EVP_PKEY_free(key_);
[email protected]70372d42010-10-22 13:12:34125}
126
[email protected]58782882011-12-03 01:12:08127RSAPrivateKey* RSAPrivateKey::Copy() const {
128 scoped_ptr<RSAPrivateKey> copy(new RSAPrivateKey());
129 RSA* rsa = EVP_PKEY_get1_RSA(key_);
130 if (!rsa)
131 return NULL;
132 copy->key_ = EVP_PKEY_new();
133 if (!EVP_PKEY_set1_RSA(copy->key_, rsa))
134 return NULL;
135 return copy.release();
136}
137
138bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const {
[email protected]1f923942010-11-17 14:39:22139 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output);
[email protected]70372d42010-10-22 13:12:34140}
141
[email protected]58782882011-12-03 01:12:08142bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) const {
[email protected]1f923942010-11-17 14:39:22143 return ExportKey(key_, i2d_PUBKEY_bio, output);
[email protected]70372d42010-10-22 13:12:34144}
145
[email protected]4b559b4d2011-04-14 17:37:14146} // namespace crypto