keychain_test_util_mac.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2017 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/keychain_test_util_mac.h"
  5. #include <Security/SecCertificate.h>
  6. #include <Security/SecImportExport.h>
  7. #include "base/mac/mac_logging.h"
  8. #include "net/cert/x509_util_apple.h"
  9. #include "third_party/boringssl/src/include/openssl/bytestring.h"
  10. #include "third_party/boringssl/src/include/openssl/ec_key.h"
  11. #include "third_party/boringssl/src/include/openssl/evp.h"
  12. #include "third_party/boringssl/src/include/openssl/mem.h"
  13. #include "third_party/boringssl/src/include/openssl/rsa.h"
  14. namespace net {
  15. namespace {
  16. base::ScopedCFTypeRef<SecIdentityRef> GetSecIdentityRefForCertificate(
  17. SecCertificateRef cert,
  18. SecKeychainRef keychain) {
  19. OSStatus status;
  20. base::ScopedCFTypeRef<SecIdentityRef> identity;
  21. status = SecIdentityCreateWithCertificate(keychain, cert,
  22. identity.InitializeInto());
  23. if (status != noErr) {
  24. OSSTATUS_LOG(WARNING, status);
  25. return base::ScopedCFTypeRef<SecIdentityRef>();
  26. }
  27. return identity;
  28. }
  29. } // namespace
  30. ScopedTestKeychain::ScopedTestKeychain() = default;
  31. ScopedTestKeychain::~ScopedTestKeychain() = default;
  32. // Much of the Keychain API was marked deprecated as of the macOS 13 SDK.
  33. // Removal of its use is tracked in https://crbug.com/1348251 but deprecation
  34. // warnings are disabled in the meanwhile.
  35. #pragma clang diagnostic push
  36. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  37. bool ScopedTestKeychain::Initialize() {
  38. if (!keychain_dir_.CreateUniqueTempDir())
  39. return false;
  40. base::FilePath keychain_path =
  41. keychain_dir_.GetPath().AppendASCII("test_keychain.keychain");
  42. return SecKeychainCreate(keychain_path.value().c_str(), 0, "", FALSE, nullptr,
  43. keychain_.InitializeInto()) == noErr;
  44. }
  45. #pragma clang diagnostic pop
  46. base::ScopedCFTypeRef<SecIdentityRef> ImportCertAndKeyToKeychain(
  47. const X509Certificate* cert,
  48. const std::string pkcs8,
  49. SecKeychainRef keychain) {
  50. // Insert the certificate into the keychain.
  51. base::ScopedCFTypeRef<SecCertificateRef> sec_cert(
  52. x509_util::CreateSecCertificateFromX509Certificate(cert));
  53. if (!sec_cert)
  54. return base::ScopedCFTypeRef<SecIdentityRef>();
  55. if (noErr != SecCertificateAddToKeychain(sec_cert, keychain))
  56. return base::ScopedCFTypeRef<SecIdentityRef>();
  57. // Import the key into the keychain. Apple doesn't accept unencrypted PKCS#8,
  58. // but it accepts the low-level RSAPrivateKey and ECPrivateKey types as
  59. // "kSecFormatOpenSSL", so produce those. There doesn't appear to be a way to
  60. // tell it which key type we have, so leave this unspecified and have it
  61. // guess.
  62. CBS cbs;
  63. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(pkcs8.data()), pkcs8.size());
  64. bssl::UniquePtr<EVP_PKEY> openssl_key(EVP_parse_private_key(&cbs));
  65. if (!openssl_key || CBS_len(&cbs) != 0)
  66. return base::ScopedCFTypeRef<SecIdentityRef>();
  67. bssl::ScopedCBB cbb;
  68. if (!CBB_init(cbb.get(), 0))
  69. return base::ScopedCFTypeRef<SecIdentityRef>();
  70. if (EVP_PKEY_id(openssl_key.get()) == EVP_PKEY_RSA) {
  71. if (!RSA_marshal_private_key(cbb.get(),
  72. EVP_PKEY_get0_RSA(openssl_key.get())))
  73. return base::ScopedCFTypeRef<SecIdentityRef>();
  74. } else if (EVP_PKEY_id(openssl_key.get()) == EVP_PKEY_EC) {
  75. if (!EC_KEY_marshal_private_key(cbb.get(),
  76. EVP_PKEY_get0_EC_KEY(openssl_key.get()), 0))
  77. return base::ScopedCFTypeRef<SecIdentityRef>();
  78. } else {
  79. return base::ScopedCFTypeRef<SecIdentityRef>();
  80. }
  81. uint8_t* encoded;
  82. size_t encoded_len;
  83. if (!CBB_finish(cbb.get(), &encoded, &encoded_len))
  84. return base::ScopedCFTypeRef<SecIdentityRef>();
  85. bssl::UniquePtr<uint8_t> scoped_encoded(encoded);
  86. base::ScopedCFTypeRef<CFDataRef> encoded_ref(
  87. CFDataCreate(kCFAllocatorDefault, encoded, encoded_len));
  88. SecExternalFormat format = kSecFormatOpenSSL;
  89. SecExternalItemType item_type = kSecItemTypePrivateKey;
  90. if (noErr != SecItemImport(encoded_ref, nullptr, &format, &item_type, 0,
  91. nullptr, keychain, nullptr)) {
  92. return base::ScopedCFTypeRef<SecIdentityRef>();
  93. }
  94. base::ScopedCFTypeRef<SecIdentityRef> sec_identity =
  95. GetSecIdentityRefForCertificate(sec_cert, keychain);
  96. return sec_identity;
  97. }
  98. } // namespace net