blob: 707994922ed45ddf6cadc3758d2ed3d9e1723903 [file] [log] [blame]
[email protected]cf211882012-07-11 07:19:141// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]28ae8fe2009-06-05 18:25:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#ifndef CRYPTO_RSA_PRIVATE_KEY_H_
6#define CRYPTO_RSA_PRIVATE_KEY_H_
[email protected]28ae8fe2009-06-05 18:25:067
avidd373b8b2015-12-21 21:34:438#include <stddef.h>
9#include <stdint.h>
10
rsleevid1afa1e2016-06-22 04:00:4811#include <memory>
[email protected]28ae8fe2009-06-05 18:25:0612#include <vector>
13
Maksim Ivanovbc9778222020-06-29 17:53:1614#include "base/containers/span.h"
avidd373b8b2015-12-21 21:34:4315#include "base/macros.h"
thestig92619caa2015-09-24 19:53:2116#include "build/build_config.h"
[email protected]d613a9902011-08-05 20:59:1117#include "crypto/crypto_export.h"
tfarina29a3a1742016-10-28 18:47:3318#include "third_party/boringssl/src/include/openssl/base.h"
[email protected]28ae8fe2009-06-05 18:25:0619
[email protected]4b559b4d2011-04-14 17:37:1420namespace crypto {
[email protected]28ae8fe2009-06-05 18:25:0621
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]f61c3972010-12-23 09:54:1524// TODO(hclam): This class should be ref-counted so it can be reused easily.
[email protected]d613a9902011-08-05 20:59:1125class CRYPTO_EXPORT RSAPrivateKey {
[email protected]28ae8fe2009-06-05 18:25:0626 public:
[email protected]a502bbe72011-01-07 18:06:4527 ~RSAPrivateKey();
28
[email protected]28ae8fe2009-06-05 18:25:0629 // Create a new random instance. Can return NULL if initialization fails.
rsleevid1afa1e2016-06-22 04:00:4830 static std::unique_ptr<RSAPrivateKey> Create(uint16_t num_bits);
[email protected]28ae8fe2009-06-05 18:25:0631
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.
rsleevid1afa1e2016-06-22 04:00:4835 static std::unique_ptr<RSAPrivateKey> CreateFromPrivateKeyInfo(
Maksim Ivanovbc9778222020-06-29 17:53:1636 base::span<const uint8_t> input);
[email protected]28ae8fe2009-06-05 18:25:0637
davidben2bcbc6b2015-04-22 02:36:4138 // 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.
rsleevid1afa1e2016-06-22 04:00:4841 static std::unique_ptr<RSAPrivateKey> CreateFromKey(EVP_PKEY* key);
davidben2bcbc6b2015-04-22 02:36:4142
Matt Mueller90240682020-02-18 23:21:2143 EVP_PKEY* key() const { return key_.get(); }
[email protected]28ae8fe2009-06-05 18:25:0644
[email protected]58782882011-12-03 01:12:0845 // Creates a copy of the object.
rsleevid1afa1e2016-06-22 04:00:4846 std::unique_ptr<RSAPrivateKey> Copy() const;
[email protected]58782882011-12-03 01:12:0847
davidben8131e3e2016-01-26 00:28:5848 // Exports the private key to a PKCS #8 PrivateKeyInfo block.
avidd373b8b2015-12-21 21:34:4349 bool ExportPrivateKey(std::vector<uint8_t>* output) const;
[email protected]28ae8fe2009-06-05 18:25:0650
51 // Exports the public key to an X509 SubjectPublicKeyInfo block.
avidd373b8b2015-12-21 21:34:4352 bool ExportPublicKey(std::vector<uint8_t>* output) const;
[email protected]28ae8fe2009-06-05 18:25:0653
[email protected]1f923942010-11-17 14:39:2254 private:
davidben85bad9e2015-05-11 20:20:1055 // Constructor is private. Use one of the Create*() methods above instead.
[email protected]28ae8fe2009-06-05 18:25:0656 RSAPrivateKey();
57
davidben74f67442016-10-01 01:45:2258 bssl::UniquePtr<EVP_PKEY> key_;
[email protected]28ae8fe2009-06-05 18:25:0659
60 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey);
61};
62
[email protected]4b559b4d2011-04-14 17:37:1463} // namespace crypto
[email protected]28ae8fe2009-06-05 18:25:0664
[email protected]4b559b4d2011-04-14 17:37:1465#endif // CRYPTO_RSA_PRIVATE_KEY_H_