Port test_root_certs_nss to iOS.
[chromium-blink-merge.git] / net / base / test_root_certs.h
blob4d52dbcda5c088fd520785659413b42dfad1bbb3
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.
5 #ifndef NET_BASE_TEST_ROOT_CERTS_H_
6 #define NET_BASE_TEST_ROOT_CERTS_H_
8 #include "base/lazy_instance.h"
9 #include "base/memory/ref_counted.h"
10 #include "build/build_config.h"
11 #include "net/base/net_export.h"
13 #if defined(USE_NSS) || defined(OS_IOS)
14 #include <list>
15 #elif defined(OS_WIN)
16 #include <windows.h>
17 #include <wincrypt.h>
18 #elif defined(OS_MACOSX)
19 #include <CoreFoundation/CFArray.h>
20 #include <Security/SecTrust.h>
21 #include "base/mac/scoped_cftyperef.h"
22 #endif
24 class FilePath;
26 namespace net {
28 class X509Certificate;
30 // TestRootCerts is a helper class for unit tests that is used to
31 // artificially mark a certificate as trusted, independent of the local
32 // machine configuration.
33 class NET_EXPORT_PRIVATE TestRootCerts {
34 public:
35 // Obtains the Singleton instance to the trusted certificates.
36 static TestRootCerts* GetInstance();
38 // Returns true if an instance exists, without forcing an initialization.
39 static bool HasInstance();
41 // Marks |certificate| as trusted for X509Certificate::Verify(). Returns
42 // false if the certificate could not be marked trusted.
43 bool Add(X509Certificate* certificate);
45 // Reads a single certificate from |file| and marks it as trusted. Returns
46 // false if an error is encountered, such as being unable to read |file|
47 // or more than one certificate existing in |file|.
48 bool AddFromFile(const FilePath& file);
50 // Clears the trusted status of any certificates that were previously
51 // marked trusted via Add().
52 void Clear();
54 // Returns true if there are no certificates that have been marked trusted.
55 bool IsEmpty() const;
57 #if defined(OS_MACOSX) && !defined(OS_IOS)
58 CFArrayRef temporary_roots() const { return temporary_roots_; }
60 // Modifies the root certificates of |trust_ref| to include the
61 // certificates stored in |temporary_roots_|. If IsEmpty() is true, this
62 // does not modify |trust_ref|.
63 OSStatus FixupSecTrustRef(SecTrustRef trust_ref) const;
64 #elif defined(OS_WIN)
65 HCERTSTORE temporary_roots() const { return temporary_roots_; }
67 // Returns an HCERTCHAINENGINE suitable to be used for certificate
68 // validation routines, or NULL to indicate that the default system chain
69 // engine is appropriate. The caller is responsible for freeing the
70 // returned HCERTCHAINENGINE.
71 HCERTCHAINENGINE GetChainEngine() const;
72 #endif
74 private:
75 friend struct base::DefaultLazyInstanceTraits<TestRootCerts>;
77 TestRootCerts();
78 ~TestRootCerts();
80 // Performs platform-dependent initialization.
81 void Init();
83 #if defined(USE_NSS) || defined(OS_IOS)
84 // It is necessary to maintain a cache of the original certificate trust
85 // settings, in order to restore them when Clear() is called.
86 class TrustEntry;
87 std::list<TrustEntry*> trust_cache_;
88 #elif defined(OS_WIN)
89 HCERTSTORE temporary_roots_;
90 #elif defined(OS_MACOSX)
91 base::mac::ScopedCFTypeRef<CFMutableArrayRef> temporary_roots_;
92 #endif
94 #if defined(OS_WIN) || defined(USE_OPENSSL)
95 // True if there are no temporarily trusted root certificates.
96 bool empty_;
97 #endif
99 DISALLOW_COPY_AND_ASSIGN(TestRootCerts);
102 // Scoped helper for unittests to handle safely managing trusted roots.
103 class NET_EXPORT_PRIVATE ScopedTestRoot {
104 public:
105 ScopedTestRoot();
106 // Creates a ScopedTestRoot that will adds|cert| to the TestRootCerts store.
107 explicit ScopedTestRoot(X509Certificate* cert);
108 ~ScopedTestRoot();
110 // Assigns |cert| to be the new test root cert. If |cert| is NULL, undoes
111 // any work the ScopedTestRoot may have previously done.
112 // If |cert_| contains a certificate (due to a prior call to Reset or due to
113 // a cert being passed at construction), the existing TestRootCerts store is
114 // cleared.
115 void Reset(X509Certificate* cert);
117 private:
118 scoped_refptr<X509Certificate> cert_;
120 DISALLOW_COPY_AND_ASSIGN(ScopedTestRoot);
123 } // namespace net
125 #endif // NET_BASE_TEST_ROOT_CERTS_H_