Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: base/crypto/rsa_private_key_openssl.cc

Issue 5047003: Implements RSAPrivateKey for openssl. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: git diff trunk Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/crypto/rsa_private_key.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/crypto/rsa_private_key.h" 5 #include "base/crypto/rsa_private_key.h"
6 6
7 #include <openssl/evp.h>
8 #include <openssl/pkcs12.h>
9 #include <openssl/rsa.h>
10
7 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/openssl_util.h"
13 #include "base/scoped_ptr.h"
14 #include "base/stl_util-inl.h"
8 15
9 namespace base { 16 namespace base {
10 17
11 // static 18 namespace {
12 RSAPrivateKey* RSAPrivateKey::CreateWithParams(uint16 num_bits, 19
13 bool permanent, 20 // Function pointer definition, for injecting the required key export function
14 bool sensitive) { 21 // into ExportKey, below. The supplied function should export EVP_PKEY into
15 NOTIMPLEMENTED(); 22 // the supplied BIO, returning 1 on success or 0 on failure.
16 return NULL; 23 typedef int (ExportFunction)(BIO*, EVP_PKEY*);
24
25 // Helper to export |key| into |output| via the specified ExportFunction.
26 bool ExportKey(EVP_PKEY* key,
27 ExportFunction export_fn,
28 std::vector<uint8>* output) {
29 if (!key)
30 return false;
31
32 ScopedOpenSSL<BIO, BIO_free_all> bio(BIO_new(BIO_s_mem()));
33
34 int res = export_fn(bio.get(), key);
35 ClearOpenSSLERRStack();
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 STLAssignToVector(output, reinterpret_cast<const uint8*>(data), len);
45 return true;
17 } 46 }
18 47
48 } // namespace
49
19 // static 50 // static
20 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { 51 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) {
21 return CreateWithParams(num_bits, 52 EnsureOpenSSLInit();
22 false /* not permanent */, 53
23 false /* not sensitive */); 54 ScopedOpenSSL<RSA, RSA_free> rsa_key(RSA_generate_key(num_bits, 65537L,
55 NULL, NULL));
56 ClearOpenSSLERRStack();
57 if (!rsa_key.get())
58 return NULL;
59
60 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey);
61 result->key_ = EVP_PKEY_new();
62 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get()))
63 return NULL;
64
65 return result.release();
24 } 66 }
25 67
26 // static 68 // static
27 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { 69 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) {
28 return CreateWithParams(num_bits,
29 true /* permanent */,
30 true /* sensitive */);
31 }
32
33 // static
34 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfoWithParams(
35 const std::vector<uint8>& input, bool permanent, bool sensitive) {
36 NOTIMPLEMENTED(); 70 NOTIMPLEMENTED();
37 return NULL; 71 return NULL;
38 } 72 }
39 73
40 // static 74 // static
41 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( 75 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo(
42 const std::vector<uint8>& input) { 76 const std::vector<uint8>& input) {
43 return CreateFromPrivateKeyInfoWithParams(input, 77 EnsureOpenSSLInit();
44 false /* not permanent */, 78
45 false /* not sensitive */); 79 // BIO_new_mem_buf is not const aware, but it does not modify the buffer.
80 char* data = reinterpret_cast<char*>(const_cast<uint8*>(input.data()));
81 ScopedOpenSSL<BIO, BIO_free_all> bio(BIO_new_mem_buf(data, input.size()));
82 if (!bio.get())
83 return NULL;
84
85 // Importing is a little more involved than exporting, as we must first
86 // PKCS#8 decode the input, and then import the EVP_PKEY from Private Key
87 // Info structure returned.
88 ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free> p8inf(
89 d2i_PKCS8_PRIV_KEY_INFO_bio(bio.get(), NULL));
90 ClearOpenSSLERRStack();
91 if (!p8inf.get())
92 return NULL;
93
94 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey);
95 result->key_ = EVP_PKCS82PKEY(p8inf.get());
96 ClearOpenSSLERRStack();
97 if (!result->key_)
98 return NULL;
99
100 return result.release();
46 } 101 }
47 102
48 // static 103 // static
49 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo( 104 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo(
50 const std::vector<uint8>& input) { 105 const std::vector<uint8>& input) {
51 return CreateFromPrivateKeyInfoWithParams(input, 106 NOTIMPLEMENTED();
52 true /* permanent */, 107 return NULL;
53 true /* seneitive */);
54 } 108 }
55 109
56 // static 110 // static
57 RSAPrivateKey* RSAPrivateKey::FindFromPublicKeyInfo( 111 RSAPrivateKey* RSAPrivateKey::FindFromPublicKeyInfo(
58 const std::vector<uint8>& input) { 112 const std::vector<uint8>& input) {
59 NOTIMPLEMENTED(); 113 NOTIMPLEMENTED();
60 return NULL; 114 return NULL;
61 } 115 }
62 116
63 RSAPrivateKey::RSAPrivateKey() { 117 RSAPrivateKey::RSAPrivateKey()
118 : key_(NULL) {
64 } 119 }
65 120
66 RSAPrivateKey::~RSAPrivateKey() { 121 RSAPrivateKey::~RSAPrivateKey() {
122 if (key_)
123 EVP_PKEY_free(key_);
67 } 124 }
68 125
69 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { 126 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) {
70 NOTIMPLEMENTED(); 127 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output);
71 return false;
72 } 128 }
73 129
74 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) { 130 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) {
75 NOTIMPLEMENTED(); 131 return ExportKey(key_, i2d_PUBKEY_bio, output);
76 return false;
77 } 132 }
78 133
79 } // namespace base 134 } // namespace base
OLDNEW
« no previous file with comments | « base/crypto/rsa_private_key.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698