cert_test_util_nss.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2014 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 "net/test/cert_test_util.h"
  5. #include <pk11pub.h>
  6. #include <secmodt.h>
  7. #include <string.h>
  8. #include <memory>
  9. #include "base/files/file_path.h"
  10. #include "base/files/file_util.h"
  11. #include "base/logging.h"
  12. #include "crypto/ec_private_key.h"
  13. #include "crypto/nss_key_util.h"
  14. #include "crypto/nss_util.h"
  15. #include "crypto/scoped_nss_types.h"
  16. #include "net/cert/cert_type.h"
  17. #include "net/cert/x509_util_nss.h"
  18. #include "third_party/boringssl/src/include/openssl/bytestring.h"
  19. #include "third_party/boringssl/src/include/openssl/ec.h"
  20. #include "third_party/boringssl/src/include/openssl/ec_key.h"
  21. #include "third_party/boringssl/src/include/openssl/evp.h"
  22. #include "third_party/boringssl/src/include/openssl/mem.h"
  23. namespace net {
  24. bool ImportSensitiveKeyFromFile(const base::FilePath& dir,
  25. base::StringPiece key_filename,
  26. PK11SlotInfo* slot) {
  27. base::FilePath key_path = dir.AppendASCII(key_filename);
  28. std::string key_pkcs8;
  29. bool success = base::ReadFileToString(key_path, &key_pkcs8);
  30. if (!success) {
  31. LOG(ERROR) << "Failed to read file " << key_path.value();
  32. return false;
  33. }
  34. std::vector<uint8_t> key_vector(key_pkcs8.begin(), key_pkcs8.end());
  35. // Prior to NSS 3.30, NSS cannot import unencrypted ECDSA private keys. Detect
  36. // such keys and encrypt with an empty password before importing. Once our
  37. // minimum version is raised to NSS 3.30, this logic can be removed. See
  38. // https://bugzilla.mozilla.org/show_bug.cgi?id=1295121
  39. CBS cbs;
  40. CBS_init(&cbs, key_vector.data(), key_vector.size());
  41. bssl::UniquePtr<EVP_PKEY> evp_pkey(EVP_parse_private_key(&cbs));
  42. if (!evp_pkey) {
  43. LOG(ERROR) << "Could not parse private key from file " << key_path.value();
  44. return false;
  45. }
  46. if (EVP_PKEY_id(evp_pkey.get()) == EVP_PKEY_EC) {
  47. std::unique_ptr<crypto::ECPrivateKey> ec_private_key =
  48. crypto::ECPrivateKey::CreateFromPrivateKeyInfo(key_vector);
  49. std::vector<uint8_t> encrypted;
  50. if (!ec_private_key ||
  51. !ec_private_key->ExportEncryptedPrivateKey(&encrypted)) {
  52. LOG(ERROR) << "Error importing private key from file "
  53. << key_path.value();
  54. return false;
  55. }
  56. SECItem encrypted_item = {siBuffer, encrypted.data(),
  57. static_cast<unsigned>(encrypted.size())};
  58. SECKEYEncryptedPrivateKeyInfo epki;
  59. memset(&epki, 0, sizeof(epki));
  60. crypto::ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
  61. if (SEC_QuickDERDecodeItem(
  62. arena.get(), &epki,
  63. SEC_ASN1_GET(SECKEY_EncryptedPrivateKeyInfoTemplate),
  64. &encrypted_item) != SECSuccess) {
  65. LOG(ERROR) << "Error importing private key from file "
  66. << key_path.value();
  67. return false;
  68. }
  69. // NSS uses the serialized public key in X9.62 form as the "public value"
  70. // for key ID purposes.
  71. EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(ec_private_key->key());
  72. bssl::ScopedCBB cbb;
  73. uint8_t* public_value;
  74. size_t public_value_len;
  75. if (!CBB_init(cbb.get(), 0) ||
  76. !EC_POINT_point2cbb(cbb.get(), EC_KEY_get0_group(ec_key),
  77. EC_KEY_get0_public_key(ec_key),
  78. POINT_CONVERSION_UNCOMPRESSED, nullptr) ||
  79. !CBB_finish(cbb.get(), &public_value, &public_value_len)) {
  80. LOG(ERROR) << "Error importing private key from file "
  81. << key_path.value();
  82. return false;
  83. }
  84. bssl::UniquePtr<uint8_t> scoped_public_value(public_value);
  85. SECItem public_item = {siBuffer, public_value,
  86. static_cast<unsigned>(public_value_len)};
  87. SECItem password_item = {siBuffer, nullptr, 0};
  88. if (PK11_ImportEncryptedPrivateKeyInfo(
  89. slot, &epki, &password_item, nullptr /* nickname */, &public_item,
  90. PR_TRUE /* permanent */, PR_TRUE /* private */, ecKey,
  91. KU_DIGITAL_SIGNATURE, nullptr /* wincx */) != SECSuccess) {
  92. LOG(ERROR) << "Error importing private key from file "
  93. << key_path.value();
  94. return false;
  95. }
  96. return true;
  97. }
  98. crypto::ScopedSECKEYPrivateKey private_key(
  99. crypto::ImportNSSKeyFromPrivateKeyInfo(slot, key_vector,
  100. true /* permanent */));
  101. LOG_IF(ERROR, !private_key) << "Could not create key from file "
  102. << key_path.value();
  103. return !!private_key;
  104. }
  105. bool ImportClientCertToSlot(CERTCertificate* nss_cert, PK11SlotInfo* slot) {
  106. std::string nickname =
  107. x509_util::GetDefaultUniqueNickname(nss_cert, USER_CERT, slot);
  108. SECStatus rv = PK11_ImportCert(slot, nss_cert, CK_INVALID_HANDLE,
  109. nickname.c_str(), PR_FALSE);
  110. if (rv != SECSuccess) {
  111. LOG(ERROR) << "Could not import cert";
  112. return false;
  113. }
  114. return true;
  115. }
  116. ScopedCERTCertificate ImportClientCertToSlot(
  117. const scoped_refptr<X509Certificate>& cert,
  118. PK11SlotInfo* slot) {
  119. ScopedCERTCertificate nss_cert =
  120. x509_util::CreateCERTCertificateFromX509Certificate(cert.get());
  121. if (!nss_cert)
  122. return nullptr;
  123. if (!ImportClientCertToSlot(nss_cert.get(), slot))
  124. return nullptr;
  125. return nss_cert;
  126. }
  127. scoped_refptr<X509Certificate> ImportClientCertAndKeyFromFile(
  128. const base::FilePath& dir,
  129. base::StringPiece cert_filename,
  130. base::StringPiece key_filename,
  131. PK11SlotInfo* slot,
  132. ScopedCERTCertificate* nss_cert) {
  133. if (!ImportSensitiveKeyFromFile(dir, key_filename, slot)) {
  134. LOG(ERROR) << "Could not import private key from file " << key_filename;
  135. return nullptr;
  136. }
  137. scoped_refptr<X509Certificate> cert(ImportCertFromFile(dir, cert_filename));
  138. if (!cert.get()) {
  139. LOG(ERROR) << "Failed to parse cert from file " << cert_filename;
  140. return nullptr;
  141. }
  142. *nss_cert = ImportClientCertToSlot(cert, slot);
  143. if (!*nss_cert)
  144. return nullptr;
  145. // |cert| continues to point to the original X509Certificate before the
  146. // import to |slot|. However this should not make a difference as NSS handles
  147. // state globally.
  148. return cert;
  149. }
  150. scoped_refptr<X509Certificate> ImportClientCertAndKeyFromFile(
  151. const base::FilePath& dir,
  152. base::StringPiece cert_filename,
  153. base::StringPiece key_filename,
  154. PK11SlotInfo* slot) {
  155. ScopedCERTCertificate nss_cert;
  156. return ImportClientCertAndKeyFromFile(dir, cert_filename, key_filename, slot,
  157. &nss_cert);
  158. }
  159. ScopedCERTCertificate ImportCERTCertificateFromFile(
  160. const base::FilePath& certs_dir,
  161. base::StringPiece cert_file) {
  162. scoped_refptr<X509Certificate> cert =
  163. ImportCertFromFile(certs_dir, cert_file);
  164. if (!cert)
  165. return nullptr;
  166. return x509_util::CreateCERTCertificateFromX509Certificate(cert.get());
  167. }
  168. ScopedCERTCertificateList CreateCERTCertificateListFromFile(
  169. const base::FilePath& certs_dir,
  170. base::StringPiece cert_file,
  171. int format) {
  172. CertificateList certs =
  173. CreateCertificateListFromFile(certs_dir, cert_file, format);
  174. ScopedCERTCertificateList nss_certs;
  175. for (const auto& cert : certs) {
  176. ScopedCERTCertificate nss_cert =
  177. x509_util::CreateCERTCertificateFromX509Certificate(cert.get());
  178. if (!nss_cert)
  179. return {};
  180. nss_certs.push_back(std::move(nss_cert));
  181. }
  182. return nss_certs;
  183. }
  184. } // namespace net