| OLD | NEW |
| 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 #ifndef BASE_CRYPTO_RSA_PRIVATE_KEY_H_ | 5 #ifndef BASE_CRYPTO_RSA_PRIVATE_KEY_H_ |
| 6 #define BASE_CRYPTO_RSA_PRIVATE_KEY_H_ | 6 #define BASE_CRYPTO_RSA_PRIVATE_KEY_H_ |
| 7 | 7 |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 | 9 |
| 10 #if defined(USE_NSS) | 10 #if defined(OS_WIN) |
| 11 #include <cryptoht.h> | |
| 12 #include <keythi.h> | |
| 13 #elif defined(OS_MACOSX) | |
| 14 // TODO(port); | |
| 15 #elif defined(OS_WIN) | |
| 16 #include <windows.h> | 11 #include <windows.h> |
| 17 #include <wincrypt.h> | 12 #include <wincrypt.h> |
| 13 #else |
| 14 // TODO(port) |
| 18 #endif | 15 #endif |
| 19 | 16 |
| 20 #include <vector> | 17 #include <vector> |
| 21 | 18 |
| 22 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
| 23 | 20 |
| 24 namespace base { | 21 namespace base { |
| 25 | 22 |
| 26 // Encapsulates an RSA private key. Can be used to generate new keys, export | 23 // Encapsulates an RSA private key. Can be used to generate new keys, export |
| 27 // keys to other formats, or to extract a public key. | 24 // keys to other formats, or to extract a public key. |
| 28 class RSAPrivateKey { | 25 class RSAPrivateKey { |
| 29 public: | 26 public: |
| 30 // Create a new random instance. Can return NULL if initialization fails. | 27 // Create a new random instance. Can return NULL if initialization fails. |
| 31 static RSAPrivateKey* Create(uint16 num_bits); | 28 static RSAPrivateKey* Create(uint16 num_bits); |
| 32 | 29 |
| 33 // Create a new instance by importing an existing private key. The format is | 30 // Create a new instance by importing an existing private key. The format is |
| 34 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if | 31 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if |
| 35 // initialization fails. | 32 // initialization fails. |
| 36 static RSAPrivateKey* CreateFromPrivateKeyInfo( | 33 static RSAPrivateKey* CreateFromPrivateKeyInfo( |
| 37 const std::vector<uint8>& input); | 34 const std::vector<uint8>& input); |
| 38 | 35 |
| 39 ~RSAPrivateKey(); | 36 ~RSAPrivateKey(); |
| 40 | 37 |
| 41 #if defined(USE_NSS) | 38 #if defined(OS_WIN) |
| 42 SECKEYPrivateKey* key() { return key_; } | |
| 43 #elif defined(OS_WIN) | |
| 44 HCRYPTPROV provider() { return provider_; } | 39 HCRYPTPROV provider() { return provider_; } |
| 45 HCRYPTKEY key() { return key_; } | 40 HCRYPTKEY key() { return key_; } |
| 46 #endif | 41 #endif |
| 47 | 42 |
| 48 // Exports the private key to a PKCS #1 PrivateKey block. | 43 // Exports the private key to a PKCS #1 PrivateKey block. |
| 49 bool ExportPrivateKey(std::vector<uint8>* output); | 44 bool ExportPrivateKey(std::vector<uint8>* output); |
| 50 | 45 |
| 51 // Exports the public key to an X509 SubjectPublicKeyInfo block. | 46 // Exports the public key to an X509 SubjectPublicKeyInfo block. |
| 52 bool ExportPublicKey(std::vector<uint8>* output); | 47 bool ExportPublicKey(std::vector<uint8>* output); |
| 53 | 48 |
| 54 private: | 49 private: |
| 55 // Constructor is private. Use Create() or CreateFromPrivateKeyInfo() | 50 // Constructor is private. Use Create() or CreateFromPrivateKeyInfo() |
| 56 // instead. | 51 // instead. |
| 57 RSAPrivateKey(); | 52 RSAPrivateKey(); |
| 58 | 53 |
| 59 #if defined(USE_NSS) | 54 #if defined(OS_WIN) |
| 60 SECKEYPrivateKey* key_; | |
| 61 SECKEYPublicKey* public_key_; | |
| 62 #elif defined(OS_WIN) | |
| 63 bool InitProvider(); | 55 bool InitProvider(); |
| 64 | 56 |
| 65 HCRYPTPROV provider_; | 57 HCRYPTPROV provider_; |
| 66 HCRYPTKEY key_; | 58 HCRYPTKEY key_; |
| 67 #endif | 59 #endif |
| 68 | 60 |
| 69 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); | 61 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); |
| 70 }; | 62 }; |
| 71 | 63 |
| 72 } // namespace base | 64 } // namespace base |
| 73 | 65 |
| 74 #endif // BASE_CRYPTO_RSA_PRIVATE_KEY_H_ | 66 #endif // BASE_CRYPTO_RSA_PRIVATE_KEY_H_ |
| OLD | NEW |