| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "crypto/rsa_private_key.h" | 5 #include "crypto/rsa_private_key.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 | 12 |
| 13 #pragma comment(lib, "crypt32.lib") | 13 #pragma comment(lib, "crypt32.lib") |
| 14 | 14 |
| 15 namespace { | |
| 16 // Helper for error handling during key import. | |
| 17 #define READ_ASSERT(truth) \ | |
| 18 if (!(truth)) { \ | |
| 19 NOTREACHED(); \ | |
| 20 return false; \ | |
| 21 } | |
| 22 } // namespace | |
| 23 | |
| 24 namespace crypto { | 15 namespace crypto { |
| 25 | 16 |
| 26 // static | 17 // static |
| 27 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { | 18 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { |
| 28 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 19 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
| 29 if (!result->InitProvider()) | 20 if (!result->InitProvider()) |
| 30 return NULL; | 21 return NULL; |
| 31 | 22 |
| 32 DWORD flags = CRYPT_EXPORTABLE; | 23 DWORD flags = CRYPT_EXPORTABLE; |
| 33 | 24 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 memcpy(dest, &pki.exponent1()->front(), pki.exponent1()->size()); | 86 memcpy(dest, &pki.exponent1()->front(), pki.exponent1()->size()); |
| 96 dest += pki.exponent1()->size(); | 87 dest += pki.exponent1()->size(); |
| 97 memcpy(dest, &pki.exponent2()->front(), pki.exponent2()->size()); | 88 memcpy(dest, &pki.exponent2()->front(), pki.exponent2()->size()); |
| 98 dest += pki.exponent2()->size(); | 89 dest += pki.exponent2()->size(); |
| 99 memcpy(dest, &pki.coefficient()->front(), pki.coefficient()->size()); | 90 memcpy(dest, &pki.coefficient()->front(), pki.coefficient()->size()); |
| 100 dest += pki.coefficient()->size(); | 91 dest += pki.coefficient()->size(); |
| 101 memcpy(dest, &pki.private_exponent()->front(), | 92 memcpy(dest, &pki.private_exponent()->front(), |
| 102 pki.private_exponent()->size()); | 93 pki.private_exponent()->size()); |
| 103 dest += pki.private_exponent()->size(); | 94 dest += pki.private_exponent()->size(); |
| 104 | 95 |
| 105 READ_ASSERT(dest == blob.get() + blob_size); | 96 if (dest != blob.get() + blob_size) { |
| 97 NOTREACHED(); |
| 98 return NULL; |
| 99 } |
| 106 if (!CryptImportKey(result->provider_, | 100 if (!CryptImportKey(result->provider_, |
| 107 reinterpret_cast<uint8*>(public_key_struc), blob_size, 0, | 101 reinterpret_cast<uint8*>(public_key_struc), blob_size, 0, |
| 108 CRYPT_EXPORTABLE, result->key_.receive())) | 102 CRYPT_EXPORTABLE, result->key_.receive())) |
| 109 return NULL; | 103 return NULL; |
| 110 | 104 |
| 111 return result.release(); | 105 return result.release(); |
| 112 } | 106 } |
| 113 | 107 |
| 114 // static | 108 // static |
| 115 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo( | 109 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo( |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 &encoded_length)) { | 226 &encoded_length)) { |
| 233 NOTREACHED(); | 227 NOTREACHED(); |
| 234 return false; | 228 return false; |
| 235 } | 229 } |
| 236 | 230 |
| 237 output->assign(encoded.get(), encoded.get() + encoded_length); | 231 output->assign(encoded.get(), encoded.get() + encoded_length); |
| 238 return true; | 232 return true; |
| 239 } | 233 } |
| 240 | 234 |
| 241 } // namespace crypto | 235 } // namespace crypto |
| OLD | NEW |