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