Upload upstream chromium 67.0.3396
[platform/framework/web/chromium-efl.git] / crypto / ec_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_EC_PRIVATE_KEY_H_
6 #define CRYPTO_EC_PRIVATE_KEY_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <memory>
12 #include <string>
13 #include <vector>
14
15 #include "base/macros.h"
16 #include "build/build_config.h"
17 #include "crypto/crypto_export.h"
18 #include "third_party/boringssl/src/include/openssl/base.h"
19
20 namespace crypto {
21
22 // Encapsulates an elliptic curve (EC) private key. Can be used to generate new
23 // keys, export keys to other formats, or to extract a public key.
24 // TODO(mattm): make this and RSAPrivateKey implement some PrivateKey interface.
25 // (The difference in types of key() and public_key() make this a little
26 // tricky.)
27 class CRYPTO_EXPORT ECPrivateKey {
28  public:
29   ~ECPrivateKey();
30
31   // Creates a new random instance. Can return nullptr if initialization fails.
32   // The created key will use the NIST P-256 curve.
33   // TODO(mattm): Add a curve parameter.
34   static std::unique_ptr<ECPrivateKey> Create();
35
36   // Create a new instance by importing an existing private key. The format is
37   // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return
38   // nullptr if initialization fails.
39   static std::unique_ptr<ECPrivateKey> CreateFromPrivateKeyInfo(
40       const std::vector<uint8_t>& input);
41
42   // Creates a new instance by importing an existing key pair.
43   // The key pair is given as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo
44   // block with empty password and an X.509 SubjectPublicKeyInfo block.
45   // Returns nullptr if initialization fails.
46   //
47   // This function is deprecated. Use CreateFromPrivateKeyInfo for new code.
48   // See https://crbug.com/603319.
49   static std::unique_ptr<ECPrivateKey> CreateFromEncryptedPrivateKeyInfo(
50       const std::vector<uint8_t>& encrypted_private_key_info);
51
52   // Returns a copy of the object.
53   std::unique_ptr<ECPrivateKey> Copy() const;
54
55   EVP_PKEY* key() { return key_.get(); }
56
57   // Exports the private key to a PKCS #8 PrivateKeyInfo block.
58   bool ExportPrivateKey(std::vector<uint8_t>* output) const;
59
60   // Exports the private key as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo
61   // block wth empty password. This was historically used as a workaround for
62   // NSS API deficiencies and does not provide security.
63   //
64   // This function is deprecated. Use ExportPrivateKey for new code. See
65   // https://crbug.com/603319.
66   bool ExportEncryptedPrivateKey(std::vector<uint8_t>* output) const;
67
68   // Exports the public key to an X.509 SubjectPublicKeyInfo block.
69   bool ExportPublicKey(std::vector<uint8_t>* output) const;
70
71   // Exports the public key as an EC point in the uncompressed point format.
72   bool ExportRawPublicKey(std::string* output) const;
73
74  private:
75   // Constructor is private. Use one of the Create*() methods above instead.
76   ECPrivateKey();
77
78   bssl::UniquePtr<EVP_PKEY> key_;
79
80   DISALLOW_COPY_AND_ASSIGN(ECPrivateKey);
81 };
82
83
84 }  // namespace crypto
85
86 #endif  // CRYPTO_EC_PRIVATE_KEY_H_