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

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

Issue 1558018: Implements support for PBKDF2-based key derivation, random key generation, an... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Style fixup Created 10 years, 8 months 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') | base/crypto/scoped_capi_types.h » ('j') | 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) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 <iostream> 7 #include <iostream>
8 #include <list> 8 #include <list>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 14 matching lines...) Expand all
25 // static 25 // static
26 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { 26 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) {
27 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); 27 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey);
28 if (!result->InitProvider()) 28 if (!result->InitProvider())
29 return NULL; 29 return NULL;
30 30
31 DWORD flags = CRYPT_EXPORTABLE; 31 DWORD flags = CRYPT_EXPORTABLE;
32 32
33 // The size is encoded as the upper 16 bits of the flags. :: sigh ::. 33 // The size is encoded as the upper 16 bits of the flags. :: sigh ::.
34 flags |= (num_bits << 16); 34 flags |= (num_bits << 16);
35 if (!CryptGenKey(result->provider_, CALG_RSA_SIGN, flags, &result->key_)) 35 if (!CryptGenKey(result->provider_, CALG_RSA_SIGN, flags,
36 result->key_.receive()))
36 return NULL; 37 return NULL;
37 38
38 return result.release(); 39 return result.release();
39 } 40 }
40 41
41 // static 42 // static
42 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( 43 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo(
43 const std::vector<uint8>& input) { 44 const std::vector<uint8>& input) {
44 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); 45 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey);
45 if (!result->InitProvider()) 46 if (!result->InitProvider())
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 dest += pki.exponent1()->size(); 89 dest += pki.exponent1()->size();
89 memcpy(dest, &pki.exponent2()->front(), pki.exponent2()->size()); 90 memcpy(dest, &pki.exponent2()->front(), pki.exponent2()->size());
90 dest += pki.exponent2()->size(); 91 dest += pki.exponent2()->size();
91 memcpy(dest, &pki.coefficient()->front(), pki.coefficient()->size()); 92 memcpy(dest, &pki.coefficient()->front(), pki.coefficient()->size());
92 dest += pki.coefficient()->size(); 93 dest += pki.coefficient()->size();
93 memcpy(dest, &pki.private_exponent()->front(), pki.private_exponent()->size()) ; 94 memcpy(dest, &pki.private_exponent()->front(), pki.private_exponent()->size()) ;
94 dest += pki.private_exponent()->size(); 95 dest += pki.private_exponent()->size();
95 96
96 READ_ASSERT(dest == blob.get() + blob_size); 97 READ_ASSERT(dest == blob.get() + blob_size);
97 if (!CryptImportKey( 98 if (!CryptImportKey(
98 result->provider_, reinterpret_cast<uint8*>(public_key_struc), blob_size, 99 result->provider_, reinterpret_cast<uint8*>(public_key_struc),
99 NULL, CRYPT_EXPORTABLE, &result->key_)) { 100 blob_size, NULL, CRYPT_EXPORTABLE, result->key_.receive())) {
100 return NULL; 101 return NULL;
101 } 102 }
102 103
103 return result.release(); 104 return result.release();
104 } 105 }
105 106
106 RSAPrivateKey::RSAPrivateKey() : provider_(NULL), key_(NULL) {} 107 RSAPrivateKey::RSAPrivateKey() : provider_(NULL), key_(NULL) {}
107 108
108 RSAPrivateKey::~RSAPrivateKey() { 109 RSAPrivateKey::~RSAPrivateKey() {
109 if (key_) {
110 if (!CryptDestroyKey(key_))
111 NOTREACHED();
112 }
113
114 if (provider_) {
115 if (!CryptReleaseContext(provider_, 0))
116 NOTREACHED();
117 }
118 } 110 }
119 111
120 bool RSAPrivateKey::InitProvider() { 112 bool RSAPrivateKey::InitProvider() {
121 return FALSE != CryptAcquireContext(&provider_, NULL, NULL, 113 return FALSE != CryptAcquireContext(provider_.receive(), NULL, NULL,
122 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); 114 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
123 } 115 }
124 116
125 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { 117 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) {
126 // Export the key 118 // Export the key
127 DWORD blob_length = 0; 119 DWORD blob_length = 0;
128 if (!CryptExportKey(key_, NULL, PRIVATEKEYBLOB, 0, NULL, &blob_length)) { 120 if (!CryptExportKey(key_, NULL, PRIVATEKEYBLOB, 0, NULL, &blob_length)) {
129 NOTREACHED(); 121 NOTREACHED();
130 return false; 122 return false;
131 } 123 }
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 return false; 203 return false;
212 } 204 }
213 205
214 for (size_t i = 0; i < encoded_length; ++i) 206 for (size_t i = 0; i < encoded_length; ++i)
215 output->push_back(encoded[i]); 207 output->push_back(encoded[i]);
216 208
217 return true; 209 return true;
218 } 210 }
219 211
220 } // namespace base 212 } // namespace base
OLDNEW
« no previous file with comments | « base/crypto/rsa_private_key.h ('k') | base/crypto/scoped_capi_types.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698