rsa_private_key_openssl.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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.
4
5#include "crypto/rsa_private_key.h"
6
7#include <openssl/evp.h>
8#include <openssl/pkcs12.h>
9#include <openssl/rsa.h>
10
11#include "base/logging.h"
12#include "base/memory/scoped_ptr.h"
13#include "crypto/openssl_util.h"
14#include "crypto/scoped_openssl_types.h"
15
16namespace crypto {
17
18namespace {
19
20// Function pointer definition, for injecting the required key export function
21// into ExportKey, below. The supplied function should export EVP_PKEY into
22// the supplied BIO, returning 1 on success or 0 on failure.
23typedef int (ExportFunction)(BIO*, EVP_PKEY*);
24
25// Helper to export |key| into |output| via the specified ExportFunction.
26bool ExportKey(EVP_PKEY* key,
27               ExportFunction export_fn,
28               std::vector<uint8>* output) {
29  if (!key)
30    return false;
31
32  OpenSSLErrStackTracer err_tracer(FROM_HERE);
33  ScopedBIO bio(BIO_new(BIO_s_mem()));
34
35  int res = export_fn(bio.get(), key);
36  if (!res)
37    return false;
38
39  char* data = NULL;
40  long len = BIO_get_mem_data(bio.get(), &data);
41  if (!data || len < 0)
42    return false;
43
44  output->assign(data, data + len);
45  return true;
46}
47
48}  // namespace
49
50// static
51RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) {
52  OpenSSLErrStackTracer err_tracer(FROM_HERE);
53
54  ScopedRSA rsa_key(RSA_new());
55  ScopedBIGNUM bn(BN_new());
56  if (!rsa_key.get() || !bn.get() || !BN_set_word(bn.get(), 65537L))
57    return NULL;
58
59  if (!RSA_generate_key_ex(rsa_key.get(), num_bits, bn.get(), NULL))
60    return NULL;
61
62  scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey);
63  result->key_ = EVP_PKEY_new();
64  if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get()))
65    return NULL;
66
67  return result.release();
68}
69
70// static
71RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo(
72    const std::vector<uint8>& input) {
73  if (input.empty())
74    return NULL;
75
76  OpenSSLErrStackTracer err_tracer(FROM_HERE);
77  // BIO_new_mem_buf is not const aware, but it does not modify the buffer.
78  char* data = reinterpret_cast<char*>(const_cast<uint8*>(&input[0]));
79  ScopedBIO bio(BIO_new_mem_buf(data, input.size()));
80  if (!bio.get())
81    return NULL;
82
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.
86  ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free>::Type p8inf(
87      d2i_PKCS8_PRIV_KEY_INFO_bio(bio.get(), NULL));
88  if (!p8inf.get())
89    return NULL;
90
91  scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey);
92  result->key_ = EVP_PKCS82PKEY(p8inf.get());
93  if (!result->key_)
94    return NULL;
95
96  return result.release();
97}
98
99RSAPrivateKey::RSAPrivateKey()
100    : key_(NULL) {
101}
102
103RSAPrivateKey::~RSAPrivateKey() {
104  if (key_)
105    EVP_PKEY_free(key_);
106}
107
108RSAPrivateKey* RSAPrivateKey::Copy() const {
109  scoped_ptr<RSAPrivateKey> copy(new RSAPrivateKey());
110  ScopedRSA rsa(EVP_PKEY_get1_RSA(key_));
111  if (!rsa)
112    return NULL;
113  copy->key_ = EVP_PKEY_new();
114  if (!EVP_PKEY_set1_RSA(copy->key_, rsa.get()))
115    return NULL;
116  return copy.release();
117}
118
119bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const {
120  return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output);
121}
122
123bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) const {
124  return ExportKey(key_, i2d_PUBKEY_bio, output);
125}
126
127}  // namespace crypto
128