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