[email protected] | cf21188 | 2012-07-11 07:19:14 | [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 | #ifndef CRYPTO_RSA_PRIVATE_KEY_H_ |
6 | #define CRYPTO_RSA_PRIVATE_KEY_H_ | ||||
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 7 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 8 | #include <stddef.h> |
9 | #include <stdint.h> | ||||
10 | |||||
rsleevi | d1afa1e | 2016-06-22 04:00:48 | [diff] [blame] | 11 | #include <memory> |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 12 | #include <vector> |
13 | |||||
Maksim Ivanov | bc977822 | 2020-06-29 17:53:16 | [diff] [blame] | 14 | #include "base/containers/span.h" |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 15 | #include "base/macros.h" |
thestig | 92619caa | 2015-09-24 19:53:21 | [diff] [blame] | 16 | #include "build/build_config.h" |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 17 | #include "crypto/crypto_export.h" |
tfarina | 29a3a174 | 2016-10-28 18:47:33 | [diff] [blame] | 18 | #include "third_party/boringssl/src/include/openssl/base.h" |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 19 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 20 | namespace crypto { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 21 | |
22 | // Encapsulates an RSA private key. Can be used to generate new keys, export | ||||
23 | // keys to other formats, or to extract a public key. | ||||
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 24 | // TODO(hclam): This class should be ref-counted so it can be reused easily. |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 25 | class CRYPTO_EXPORT RSAPrivateKey { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 26 | public: |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 27 | ~RSAPrivateKey(); |
28 | |||||
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 29 | // Create a new random instance. Can return NULL if initialization fails. |
rsleevi | d1afa1e | 2016-06-22 04:00:48 | [diff] [blame] | 30 | static std::unique_ptr<RSAPrivateKey> Create(uint16_t num_bits); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 31 | |
32 | // Create a new instance by importing an existing private key. The format is | ||||
33 | // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if | ||||
34 | // initialization fails. | ||||
rsleevi | d1afa1e | 2016-06-22 04:00:48 | [diff] [blame] | 35 | static std::unique_ptr<RSAPrivateKey> CreateFromPrivateKeyInfo( |
Maksim Ivanov | bc977822 | 2020-06-29 17:53:16 | [diff] [blame] | 36 | base::span<const uint8_t> input); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 37 | |
davidben | 2bcbc6b | 2015-04-22 02:36:41 | [diff] [blame] | 38 | // Create a new instance from an existing EVP_PKEY, taking a |
39 | // reference to it. |key| must be an RSA key. Returns NULL on | ||||
40 | // failure. | ||||
rsleevi | d1afa1e | 2016-06-22 04:00:48 | [diff] [blame] | 41 | static std::unique_ptr<RSAPrivateKey> CreateFromKey(EVP_PKEY* key); |
davidben | 2bcbc6b | 2015-04-22 02:36:41 | [diff] [blame] | 42 | |
Matt Mueller | 9024068 | 2020-02-18 23:21:21 | [diff] [blame] | 43 | EVP_PKEY* key() const { return key_.get(); } |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 44 | |
[email protected] | 5878288 | 2011-12-03 01:12:08 | [diff] [blame] | 45 | // Creates a copy of the object. |
rsleevi | d1afa1e | 2016-06-22 04:00:48 | [diff] [blame] | 46 | std::unique_ptr<RSAPrivateKey> Copy() const; |
[email protected] | 5878288 | 2011-12-03 01:12:08 | [diff] [blame] | 47 | |
davidben | 8131e3e | 2016-01-26 00:28:58 | [diff] [blame] | 48 | // Exports the private key to a PKCS #8 PrivateKeyInfo block. |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 49 | bool ExportPrivateKey(std::vector<uint8_t>* output) const; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 50 | |
51 | // Exports the public key to an X509 SubjectPublicKeyInfo block. | ||||
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 52 | bool ExportPublicKey(std::vector<uint8_t>* output) const; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 53 | |
[email protected] | 1f92394 | 2010-11-17 14:39:22 | [diff] [blame] | 54 | private: |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 55 | // Constructor is private. Use one of the Create*() methods above instead. |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 56 | RSAPrivateKey(); |
57 | |||||
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 58 | bssl::UniquePtr<EVP_PKEY> key_; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 59 | |
60 | DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); | ||||
61 | }; | ||||
62 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 63 | } // namespace crypto |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 64 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 65 | #endif // CRYPTO_RSA_PRIVATE_KEY_H_ |