ec_private_key.h 3.1 KB

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