ec_private_key.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include "crypto/ec_private_key.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <utility>
  8. #include "base/check_op.h"
  9. #include "crypto/openssl_util.h"
  10. #include "third_party/boringssl/src/include/openssl/bn.h"
  11. #include "third_party/boringssl/src/include/openssl/bytestring.h"
  12. #include "third_party/boringssl/src/include/openssl/ec.h"
  13. #include "third_party/boringssl/src/include/openssl/ec_key.h"
  14. #include "third_party/boringssl/src/include/openssl/evp.h"
  15. #include "third_party/boringssl/src/include/openssl/mem.h"
  16. #include "third_party/boringssl/src/include/openssl/pkcs8.h"
  17. namespace crypto {
  18. ECPrivateKey::~ECPrivateKey() = default;
  19. // static
  20. std::unique_ptr<ECPrivateKey> ECPrivateKey::Create() {
  21. OpenSSLErrStackTracer err_tracer(FROM_HERE);
  22. bssl::UniquePtr<EC_KEY> ec_key(
  23. EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  24. if (!ec_key || !EC_KEY_generate_key(ec_key.get()))
  25. return nullptr;
  26. std::unique_ptr<ECPrivateKey> result(new ECPrivateKey());
  27. result->key_.reset(EVP_PKEY_new());
  28. if (!result->key_ || !EVP_PKEY_set1_EC_KEY(result->key_.get(), ec_key.get()))
  29. return nullptr;
  30. CHECK_EQ(EVP_PKEY_EC, EVP_PKEY_id(result->key_.get()));
  31. return result;
  32. }
  33. // static
  34. std::unique_ptr<ECPrivateKey> ECPrivateKey::CreateFromPrivateKeyInfo(
  35. base::span<const uint8_t> input) {
  36. OpenSSLErrStackTracer err_tracer(FROM_HERE);
  37. CBS cbs;
  38. CBS_init(&cbs, input.data(), input.size());
  39. bssl::UniquePtr<EVP_PKEY> pkey(EVP_parse_private_key(&cbs));
  40. if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_EC)
  41. return nullptr;
  42. std::unique_ptr<ECPrivateKey> result(new ECPrivateKey());
  43. result->key_ = std::move(pkey);
  44. return result;
  45. }
  46. // static
  47. std::unique_ptr<ECPrivateKey> ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
  48. base::span<const uint8_t> encrypted_private_key_info) {
  49. OpenSSLErrStackTracer err_tracer(FROM_HERE);
  50. CBS cbs;
  51. CBS_init(&cbs, encrypted_private_key_info.data(),
  52. encrypted_private_key_info.size());
  53. bssl::UniquePtr<EVP_PKEY> pkey(
  54. PKCS8_parse_encrypted_private_key(&cbs, "", 0));
  55. // Hack for reading keys generated by an older version of the OpenSSL code.
  56. // Some implementations encode the empty password as "\0\0" (passwords are
  57. // normally encoded in big-endian UCS-2 with a NUL terminator) and some
  58. // encode as the empty string. PKCS8_parse_encrypted_private_key
  59. // distinguishes the two by whether the password is nullptr.
  60. if (!pkey) {
  61. CBS_init(&cbs, encrypted_private_key_info.data(),
  62. encrypted_private_key_info.size());
  63. pkey.reset(PKCS8_parse_encrypted_private_key(&cbs, nullptr, 0));
  64. }
  65. if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_EC)
  66. return nullptr;
  67. std::unique_ptr<ECPrivateKey> result(new ECPrivateKey());
  68. result->key_ = std::move(pkey);
  69. return result;
  70. }
  71. std::unique_ptr<ECPrivateKey> ECPrivateKey::Copy() const {
  72. std::unique_ptr<ECPrivateKey> copy(new ECPrivateKey());
  73. copy->key_ = bssl::UpRef(key_);
  74. return copy;
  75. }
  76. bool ECPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const {
  77. OpenSSLErrStackTracer err_tracer(FROM_HERE);
  78. uint8_t* der;
  79. size_t der_len;
  80. bssl::ScopedCBB cbb;
  81. if (!CBB_init(cbb.get(), 0) ||
  82. !EVP_marshal_private_key(cbb.get(), key_.get()) ||
  83. !CBB_finish(cbb.get(), &der, &der_len)) {
  84. return false;
  85. }
  86. output->assign(der, der + der_len);
  87. OPENSSL_free(der);
  88. return true;
  89. }
  90. bool ECPrivateKey::ExportEncryptedPrivateKey(
  91. std::vector<uint8_t>* output) const {
  92. OpenSSLErrStackTracer err_tracer(FROM_HERE);
  93. // Encrypt the object.
  94. // NOTE: NSS uses SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_3KEY_TRIPLE_DES_CBC
  95. // so use NID_pbe_WithSHA1And3_Key_TripleDES_CBC which should be the OpenSSL
  96. // equivalent.
  97. uint8_t* der;
  98. size_t der_len;
  99. bssl::ScopedCBB cbb;
  100. if (!CBB_init(cbb.get(), 0) ||
  101. !PKCS8_marshal_encrypted_private_key(
  102. cbb.get(), NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  103. nullptr /* cipher */, nullptr /* no password */, 0 /* pass_len */,
  104. nullptr /* salt */, 0 /* salt_len */, 1 /* iterations */,
  105. key_.get()) ||
  106. !CBB_finish(cbb.get(), &der, &der_len)) {
  107. return false;
  108. }
  109. output->assign(der, der + der_len);
  110. OPENSSL_free(der);
  111. return true;
  112. }
  113. bool ECPrivateKey::ExportPublicKey(std::vector<uint8_t>* output) const {
  114. OpenSSLErrStackTracer err_tracer(FROM_HERE);
  115. uint8_t* der;
  116. size_t der_len;
  117. bssl::ScopedCBB cbb;
  118. if (!CBB_init(cbb.get(), 0) ||
  119. !EVP_marshal_public_key(cbb.get(), key_.get()) ||
  120. !CBB_finish(cbb.get(), &der, &der_len)) {
  121. return false;
  122. }
  123. output->assign(der, der + der_len);
  124. OPENSSL_free(der);
  125. return true;
  126. }
  127. bool ECPrivateKey::ExportRawPublicKey(std::string* output) const {
  128. OpenSSLErrStackTracer err_tracer(FROM_HERE);
  129. std::array<uint8_t, 65> buf;
  130. EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(key_.get());
  131. if (!EC_POINT_point2oct(EC_KEY_get0_group(ec_key),
  132. EC_KEY_get0_public_key(ec_key),
  133. POINT_CONVERSION_UNCOMPRESSED, buf.data(), buf.size(),
  134. /*ctx=*/nullptr)) {
  135. return false;
  136. }
  137. output->assign(buf.begin(), buf.end());
  138. return true;
  139. }
  140. ECPrivateKey::ECPrivateKey() = default;
  141. } // namespace crypto