test_root_certs_mac.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 "net/cert/test_root_certs.h"
  5. #include <Security/Security.h>
  6. #include "build/build_config.h"
  7. #include "net/cert/pki/cert_errors.h"
  8. #include "net/cert/x509_certificate.h"
  9. #include "net/cert/x509_util.h"
  10. #include "net/cert/x509_util_apple.h"
  11. namespace net {
  12. bool TestRootCerts::AddImpl(X509Certificate* certificate) {
  13. base::ScopedCFTypeRef<SecCertificateRef> os_cert(
  14. x509_util::CreateSecCertificateFromX509Certificate(certificate));
  15. if (!os_cert)
  16. return false;
  17. if (CFArrayContainsValue(temporary_roots_,
  18. CFRangeMake(0, CFArrayGetCount(temporary_roots_)),
  19. os_cert.get()))
  20. return true;
  21. CFArrayAppendValue(temporary_roots_, os_cert.get());
  22. return true;
  23. }
  24. void TestRootCerts::ClearImpl() {
  25. CFArrayRemoveAllValues(temporary_roots_);
  26. }
  27. OSStatus TestRootCerts::FixupSecTrustRef(SecTrustRef trust_ref) const {
  28. if (IsEmpty())
  29. return noErr;
  30. OSStatus status = SecTrustSetAnchorCertificates(trust_ref, temporary_roots_);
  31. if (status)
  32. return status;
  33. // Trust system store in addition to trusting |temporary_roots_|.
  34. return SecTrustSetAnchorCertificatesOnly(trust_ref, false);
  35. }
  36. TestRootCerts::~TestRootCerts() = default;
  37. void TestRootCerts::Init() {
  38. temporary_roots_.reset(
  39. CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks));
  40. }
  41. } // namespace net