keychain_test_util_mac.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. #ifndef NET_TEST_KEYCHAIN_TEST_UTIL_MAC_H_
  5. #define NET_TEST_KEYCHAIN_TEST_UTIL_MAC_H_
  6. #include <Security/SecKeychain.h>
  7. #include <string>
  8. #include "base/files/scoped_temp_dir.h"
  9. #include "base/mac/scoped_cftyperef.h"
  10. namespace net {
  11. class X509Certificate;
  12. // Manages a temporary keychain.
  13. class ScopedTestKeychain {
  14. public:
  15. ScopedTestKeychain();
  16. ~ScopedTestKeychain();
  17. // Initializes the temp dir and keychain, returning true on success.
  18. bool Initialize();
  19. // Returns the SecKeychainRef. Initialize() must have been called first.
  20. SecKeychainRef keychain() const { return keychain_.get(); }
  21. private:
  22. base::ScopedTempDir keychain_dir_;
  23. base::ScopedCFTypeRef<SecKeychainRef> keychain_;
  24. };
  25. // Import the |cert| and matching key in unencrypted |pkcs8| into |keychain|
  26. // and return the SecIdentityRef for |cert| and its key.
  27. base::ScopedCFTypeRef<SecIdentityRef> ImportCertAndKeyToKeychain(
  28. const X509Certificate* cert,
  29. const std::string pkcs8,
  30. SecKeychainRef keychain);
  31. } // namespace net
  32. #endif // NET_TEST_KEYCHAIN_TEST_UTIL_MAC_H_