1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef CRYPTO_RSA_PRIVATE_KEY_H_ 6*635a8641SAndroid Build Coastguard Worker #define CRYPTO_RSA_PRIVATE_KEY_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <stddef.h> 9*635a8641SAndroid Build Coastguard Worker #include <stdint.h> 10*635a8641SAndroid Build Coastguard Worker #include <openssl/base.h> 11*635a8641SAndroid Build Coastguard Worker 12*635a8641SAndroid Build Coastguard Worker #include <memory> 13*635a8641SAndroid Build Coastguard Worker #include <vector> 14*635a8641SAndroid Build Coastguard Worker 15*635a8641SAndroid Build Coastguard Worker #include "base/macros.h" 16*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h" 17*635a8641SAndroid Build Coastguard Worker #include "crypto/crypto_export.h" 18*635a8641SAndroid Build Coastguard Worker 19*635a8641SAndroid Build Coastguard Worker namespace crypto { 20*635a8641SAndroid Build Coastguard Worker 21*635a8641SAndroid Build Coastguard Worker // Encapsulates an RSA private key. Can be used to generate new keys, export 22*635a8641SAndroid Build Coastguard Worker // keys to other formats, or to extract a public key. 23*635a8641SAndroid Build Coastguard Worker // TODO(hclam): This class should be ref-counted so it can be reused easily. 24*635a8641SAndroid Build Coastguard Worker class CRYPTO_EXPORT RSAPrivateKey { 25*635a8641SAndroid Build Coastguard Worker public: 26*635a8641SAndroid Build Coastguard Worker ~RSAPrivateKey(); 27*635a8641SAndroid Build Coastguard Worker 28*635a8641SAndroid Build Coastguard Worker // Create a new random instance. Can return NULL if initialization fails. 29*635a8641SAndroid Build Coastguard Worker static std::unique_ptr<RSAPrivateKey> Create(uint16_t num_bits); 30*635a8641SAndroid Build Coastguard Worker 31*635a8641SAndroid Build Coastguard Worker // Create a new instance by importing an existing private key. The format is 32*635a8641SAndroid Build Coastguard Worker // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if 33*635a8641SAndroid Build Coastguard Worker // initialization fails. 34*635a8641SAndroid Build Coastguard Worker static std::unique_ptr<RSAPrivateKey> CreateFromPrivateKeyInfo( 35*635a8641SAndroid Build Coastguard Worker const std::vector<uint8_t>& input); 36*635a8641SAndroid Build Coastguard Worker 37*635a8641SAndroid Build Coastguard Worker // Create a new instance from an existing EVP_PKEY, taking a 38*635a8641SAndroid Build Coastguard Worker // reference to it. |key| must be an RSA key. Returns NULL on 39*635a8641SAndroid Build Coastguard Worker // failure. 40*635a8641SAndroid Build Coastguard Worker static std::unique_ptr<RSAPrivateKey> CreateFromKey(EVP_PKEY* key); 41*635a8641SAndroid Build Coastguard Worker key()42*635a8641SAndroid Build Coastguard Worker EVP_PKEY* key() { return key_.get(); } 43*635a8641SAndroid Build Coastguard Worker 44*635a8641SAndroid Build Coastguard Worker // Creates a copy of the object. 45*635a8641SAndroid Build Coastguard Worker std::unique_ptr<RSAPrivateKey> Copy() const; 46*635a8641SAndroid Build Coastguard Worker 47*635a8641SAndroid Build Coastguard Worker // Exports the private key to a PKCS #8 PrivateKeyInfo block. 48*635a8641SAndroid Build Coastguard Worker bool ExportPrivateKey(std::vector<uint8_t>* output) const; 49*635a8641SAndroid Build Coastguard Worker 50*635a8641SAndroid Build Coastguard Worker // Exports the public key to an X509 SubjectPublicKeyInfo block. 51*635a8641SAndroid Build Coastguard Worker bool ExportPublicKey(std::vector<uint8_t>* output) const; 52*635a8641SAndroid Build Coastguard Worker 53*635a8641SAndroid Build Coastguard Worker private: 54*635a8641SAndroid Build Coastguard Worker // Constructor is private. Use one of the Create*() methods above instead. 55*635a8641SAndroid Build Coastguard Worker RSAPrivateKey(); 56*635a8641SAndroid Build Coastguard Worker 57*635a8641SAndroid Build Coastguard Worker bssl::UniquePtr<EVP_PKEY> key_; 58*635a8641SAndroid Build Coastguard Worker 59*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); 60*635a8641SAndroid Build Coastguard Worker }; 61*635a8641SAndroid Build Coastguard Worker 62*635a8641SAndroid Build Coastguard Worker } // namespace crypto 63*635a8641SAndroid Build Coastguard Worker 64*635a8641SAndroid Build Coastguard Worker #endif // CRYPTO_RSA_PRIVATE_KEY_H_ 65