[email protected] | bfde744 | 2012-05-02 20:47:47 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #include "crypto/rsa_private_key.h" |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 6 | |
| 7 | #include <list> |
| 8 | |
| 9 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | aeea229 | 2009-06-20 01:39:23 | [diff] [blame] | 11 | #include "base/string_util.h" |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 12 | |
[email protected] | e73acd5 | 2011-06-03 21:39:13 | [diff] [blame] | 13 | #pragma comment(lib, "crypt32.lib") |
| 14 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 15 | namespace crypto { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 16 | |
| 17 | // static |
| 18 | RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { |
| 19 | scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
| 20 | if (!result->InitProvider()) |
| 21 | return NULL; |
| 22 | |
| 23 | DWORD flags = CRYPT_EXPORTABLE; |
| 24 | |
| 25 | // The size is encoded as the upper 16 bits of the flags. :: sigh ::. |
| 26 | flags |= (num_bits << 16); |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 27 | if (!CryptGenKey(result->provider_, CALG_RSA_SIGN, flags, |
| 28 | result->key_.receive())) |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 29 | return NULL; |
| 30 | |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 31 | return result.release(); |
| 32 | } |
| 33 | |
| 34 | // static |
[email protected] | 7464805 | 2010-08-10 19:37:51 | [diff] [blame] | 35 | RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { |
| 36 | NOTIMPLEMENTED(); |
| 37 | return NULL; |
| 38 | } |
| 39 | |
| 40 | // static |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 41 | RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( |
| 42 | const std::vector<uint8>& input) { |
| 43 | scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
| 44 | if (!result->InitProvider()) |
| 45 | return NULL; |
| 46 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 47 | PrivateKeyInfoCodec pki(false); // Little-Endian |
[email protected] | b163bc1 | 2012-05-04 04:11:42 | [diff] [blame] | 48 | if (!pki.Import(input)) |
| 49 | return NULL; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 50 | |
[email protected] | b163bc1 | 2012-05-04 04:11:42 | [diff] [blame] | 51 | size_t blob_size = sizeof(PUBLICKEYSTRUC) + |
| 52 | sizeof(RSAPUBKEY) + |
| 53 | pki.modulus()->size() + |
| 54 | pki.prime1()->size() + |
| 55 | pki.prime2()->size() + |
| 56 | pki.exponent1()->size() + |
| 57 | pki.exponent2()->size() + |
| 58 | pki.coefficient()->size() + |
| 59 | pki.private_exponent()->size(); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 60 | scoped_array<BYTE> blob(new BYTE[blob_size]); |
| 61 | |
| 62 | uint8* dest = blob.get(); |
| 63 | PUBLICKEYSTRUC* public_key_struc = reinterpret_cast<PUBLICKEYSTRUC*>(dest); |
| 64 | public_key_struc->bType = PRIVATEKEYBLOB; |
| 65 | public_key_struc->bVersion = 0x02; |
| 66 | public_key_struc->reserved = 0; |
| 67 | public_key_struc->aiKeyAlg = CALG_RSA_SIGN; |
| 68 | dest += sizeof(PUBLICKEYSTRUC); |
| 69 | |
| 70 | RSAPUBKEY* rsa_pub_key = reinterpret_cast<RSAPUBKEY*>(dest); |
| 71 | rsa_pub_key->magic = 0x32415352; |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 72 | rsa_pub_key->bitlen = pki.modulus()->size() * 8; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 73 | int public_exponent_int = 0; |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 74 | for (size_t i = pki.public_exponent()->size(); i > 0; --i) { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 75 | public_exponent_int <<= 8; |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 76 | public_exponent_int |= (*pki.public_exponent())[i - 1]; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 77 | } |
| 78 | rsa_pub_key->pubexp = public_exponent_int; |
| 79 | dest += sizeof(RSAPUBKEY); |
| 80 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 81 | memcpy(dest, &pki.modulus()->front(), pki.modulus()->size()); |
| 82 | dest += pki.modulus()->size(); |
| 83 | memcpy(dest, &pki.prime1()->front(), pki.prime1()->size()); |
| 84 | dest += pki.prime1()->size(); |
| 85 | memcpy(dest, &pki.prime2()->front(), pki.prime2()->size()); |
| 86 | dest += pki.prime2()->size(); |
| 87 | memcpy(dest, &pki.exponent1()->front(), pki.exponent1()->size()); |
| 88 | dest += pki.exponent1()->size(); |
| 89 | memcpy(dest, &pki.exponent2()->front(), pki.exponent2()->size()); |
| 90 | dest += pki.exponent2()->size(); |
| 91 | memcpy(dest, &pki.coefficient()->front(), pki.coefficient()->size()); |
| 92 | dest += pki.coefficient()->size(); |
[email protected] | 08ce4d4 | 2010-10-21 00:31:19 | [diff] [blame] | 93 | memcpy(dest, &pki.private_exponent()->front(), |
| 94 | pki.private_exponent()->size()); |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 95 | dest += pki.private_exponent()->size(); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 96 | |
[email protected] | bfde744 | 2012-05-02 20:47:47 | [diff] [blame] | 97 | if (dest != blob.get() + blob_size) { |
| 98 | NOTREACHED(); |
| 99 | return NULL; |
| 100 | } |
[email protected] | 08ce4d4 | 2010-10-21 00:31:19 | [diff] [blame] | 101 | if (!CryptImportKey(result->provider_, |
[email protected] | b163bc1 | 2012-05-04 04:11:42 | [diff] [blame] | 102 | reinterpret_cast<uint8*>(public_key_struc), |
| 103 | static_cast<DWORD>(blob_size), 0, CRYPT_EXPORTABLE, |
| 104 | result->key_.receive())) { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 105 | return NULL; |
[email protected] | b163bc1 | 2012-05-04 04:11:42 | [diff] [blame] | 106 | } |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 107 | |
| 108 | return result.release(); |
| 109 | } |
| 110 | |
[email protected] | 7464805 | 2010-08-10 19:37:51 | [diff] [blame] | 111 | // static |
| 112 | RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo( |
| 113 | const std::vector<uint8>& input) { |
| 114 | NOTIMPLEMENTED(); |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | // static |
| 119 | RSAPrivateKey* RSAPrivateKey::FindFromPublicKeyInfo( |
| 120 | const std::vector<uint8>& input) { |
| 121 | NOTIMPLEMENTED(); |
| 122 | return NULL; |
| 123 | } |
| 124 | |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 125 | RSAPrivateKey::RSAPrivateKey() : provider_(NULL), key_(NULL) {} |
| 126 | |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 127 | RSAPrivateKey::~RSAPrivateKey() {} |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 128 | |
| 129 | bool RSAPrivateKey::InitProvider() { |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 130 | return FALSE != CryptAcquireContext(provider_.receive(), NULL, NULL, |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 131 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); |
| 132 | } |
| 133 | |
[email protected] | 5878288 | 2011-12-03 01:12:08 | [diff] [blame] | 134 | RSAPrivateKey* RSAPrivateKey::Copy() const { |
| 135 | scoped_ptr<RSAPrivateKey> copy(new RSAPrivateKey()); |
| 136 | if (!CryptContextAddRef(provider_, NULL, 0)) { |
| 137 | NOTREACHED(); |
| 138 | return NULL; |
| 139 | } |
| 140 | copy->provider_.reset(provider_.get()); |
| 141 | if (!CryptDuplicateKey(key_.get(), NULL, 0, copy->key_.receive())) |
| 142 | return NULL; |
| 143 | return copy.release(); |
| 144 | } |
| 145 | |
| 146 | bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 147 | // Export the key |
| 148 | DWORD blob_length = 0; |
[email protected] | 08ce4d4 | 2010-10-21 00:31:19 | [diff] [blame] | 149 | if (!CryptExportKey(key_, 0, PRIVATEKEYBLOB, 0, NULL, &blob_length)) { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 150 | NOTREACHED(); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | scoped_array<uint8> blob(new uint8[blob_length]); |
[email protected] | 08ce4d4 | 2010-10-21 00:31:19 | [diff] [blame] | 155 | if (!CryptExportKey(key_, 0, PRIVATEKEYBLOB, 0, blob.get(), &blob_length)) { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 156 | NOTREACHED(); |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | uint8* pos = blob.get(); |
| 161 | PUBLICKEYSTRUC *publickey_struct = reinterpret_cast<PUBLICKEYSTRUC*>(pos); |
| 162 | pos += sizeof(PUBLICKEYSTRUC); |
| 163 | |
| 164 | RSAPUBKEY *rsa_pub_key = reinterpret_cast<RSAPUBKEY*>(pos); |
| 165 | pos += sizeof(RSAPUBKEY); |
| 166 | |
| 167 | int mod_size = rsa_pub_key->bitlen / 8; |
| 168 | int primes_size = rsa_pub_key->bitlen / 16; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 169 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 170 | PrivateKeyInfoCodec pki(false); // Little-Endian |
| 171 | |
| 172 | pki.modulus()->assign(pos, pos + mod_size); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 173 | pos += mod_size; |
| 174 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 175 | pki.prime1()->assign(pos, pos + primes_size); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 176 | pos += primes_size; |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 177 | pki.prime2()->assign(pos, pos + primes_size); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 178 | pos += primes_size; |
| 179 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 180 | pki.exponent1()->assign(pos, pos + primes_size); |
[email protected] | fbc97c5 | 2009-06-22 17:17:26 | [diff] [blame] | 181 | pos += primes_size; |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 182 | pki.exponent2()->assign(pos, pos + primes_size); |
[email protected] | fbc97c5 | 2009-06-22 17:17:26 | [diff] [blame] | 183 | pos += primes_size; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 184 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 185 | pki.coefficient()->assign(pos, pos + primes_size); |
[email protected] | fbc97c5 | 2009-06-22 17:17:26 | [diff] [blame] | 186 | pos += primes_size; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 187 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 188 | pki.private_exponent()->assign(pos, pos + mod_size); |
[email protected] | fbc97c5 | 2009-06-22 17:17:26 | [diff] [blame] | 189 | pos += mod_size; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 190 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 191 | pki.public_exponent()->assign(reinterpret_cast<uint8*>(&rsa_pub_key->pubexp), |
| 192 | reinterpret_cast<uint8*>(&rsa_pub_key->pubexp) + 4); |
| 193 | |
[email protected] | 37199a8 | 2010-03-02 18:29:42 | [diff] [blame] | 194 | CHECK_EQ(pos - blob_length, reinterpret_cast<BYTE*>(publickey_struct)); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 195 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 196 | return pki.Export(output); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 197 | } |
| 198 | |
[email protected] | 5878288 | 2011-12-03 01:12:08 | [diff] [blame] | 199 | bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) const { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 200 | DWORD key_info_len; |
| 201 | if (!CryptExportPublicKeyInfo( |
| 202 | provider_, AT_SIGNATURE, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, |
| 203 | NULL, &key_info_len)) { |
| 204 | NOTREACHED(); |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | scoped_array<uint8> key_info(new uint8[key_info_len]); |
| 209 | if (!CryptExportPublicKeyInfo( |
| 210 | provider_, AT_SIGNATURE, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, |
| 211 | reinterpret_cast<CERT_PUBLIC_KEY_INFO*>(key_info.get()), &key_info_len)) { |
| 212 | NOTREACHED(); |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | DWORD encoded_length; |
| 217 | if (!CryptEncodeObject( |
| 218 | X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, X509_PUBLIC_KEY_INFO, |
| 219 | reinterpret_cast<CERT_PUBLIC_KEY_INFO*>(key_info.get()), NULL, |
| 220 | &encoded_length)) { |
| 221 | NOTREACHED(); |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | scoped_array<BYTE> encoded(new BYTE[encoded_length]); |
| 226 | if (!CryptEncodeObject( |
| 227 | X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, X509_PUBLIC_KEY_INFO, |
| 228 | reinterpret_cast<CERT_PUBLIC_KEY_INFO*>(key_info.get()), encoded.get(), |
| 229 | &encoded_length)) { |
| 230 | NOTREACHED(); |
| 231 | return false; |
| 232 | } |
| 233 | |
[email protected] | 9dd6a2d | 2011-11-15 04:45:16 | [diff] [blame] | 234 | output->assign(encoded.get(), encoded.get() + encoded_length); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 235 | return true; |
| 236 | } |
| 237 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 238 | } // namespace crypto |