xref: /aosp_15_r20/external/cronet/net/cert/test_root_certs_ios.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/cert/test_root_certs.h"
6 
7 #include <Security/Security.h>
8 
9 #include "build/build_config.h"
10 #include "net/cert/x509_certificate.h"
11 #include "net/cert/x509_util.h"
12 #include "net/cert/x509_util_apple.h"
13 
14 namespace net {
15 
AddImpl(X509Certificate * certificate)16 bool TestRootCerts::AddImpl(X509Certificate* certificate) {
17   base::apple::ScopedCFTypeRef<SecCertificateRef> os_cert(
18       x509_util::CreateSecCertificateFromX509Certificate(certificate));
19   if (!os_cert) {
20     return false;
21   }
22 
23   if (CFArrayContainsValue(
24           temporary_roots_.get(),
25           CFRangeMake(0, CFArrayGetCount(temporary_roots_.get())),
26           os_cert.get())) {
27     return true;
28   }
29   CFArrayAppendValue(temporary_roots_.get(), os_cert.get());
30 
31   return true;
32 }
33 
ClearImpl()34 void TestRootCerts::ClearImpl() {
35   CFArrayRemoveAllValues(temporary_roots_.get());
36 }
37 
FixupSecTrustRef(SecTrustRef trust_ref) const38 OSStatus TestRootCerts::FixupSecTrustRef(SecTrustRef trust_ref) const {
39   if (IsEmpty()) {
40     return noErr;
41   }
42 
43   OSStatus status =
44       SecTrustSetAnchorCertificates(trust_ref, temporary_roots_.get());
45   if (status) {
46     return status;
47   }
48   // Trust system store in addition to trusting |temporary_roots_|.
49   return SecTrustSetAnchorCertificatesOnly(trust_ref, false);
50 }
51 
52 TestRootCerts::~TestRootCerts() = default;
53 
Init()54 void TestRootCerts::Init() {
55   temporary_roots_.reset(
56       CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks));
57 }
58 
59 }  // namespace net
60