Bug 1850713: remove duplicated setting of early hint preloader id in `ScriptLoader...
[gecko.git] / security / certverifier / NSSCertDBTrustDomain.h
blob438e9bec10d94093873c9bc776810a686a396088
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef NSSCertDBTrustDomain_h
8 #define NSSCertDBTrustDomain_h
10 #include "CertVerifier.h"
11 #include "CRLiteTimestamp.h"
12 #include "ScopedNSSTypes.h"
13 #include "mozilla/BasePrincipal.h"
14 #include "mozilla/TimeStamp.h"
15 #include "mozpkix/pkixtypes.h"
16 #include "nsICertStorage.h"
17 #include "nsString.h"
18 #include "secmodt.h"
20 namespace mozilla {
21 namespace psm {
23 enum class ValidityCheckingMode {
24 CheckingOff = 0,
25 CheckForEV = 1,
28 enum class NSSDBConfig {
29 ReadWrite = 0,
30 ReadOnly = 1,
33 enum class PKCS11DBConfig {
34 DoNotLoadModules = 0,
35 LoadModules = 1,
38 // Policy options for matching id-Netscape-stepUp with id-kp-serverAuth (for CA
39 // certificates only):
40 // * Always match: the step-up OID is considered equivalent to serverAuth
41 // * Match before 23 August 2016: the OID is considered equivalent if the
42 // certificate's notBefore is before 23 August 2016
43 // * Match before 23 August 2015: similarly, but for 23 August 2015
44 // * Never match: the OID is never considered equivalent to serverAuth
45 enum class NetscapeStepUpPolicy : uint32_t {
46 AlwaysMatch = 0,
47 MatchBefore23August2016 = 1,
48 MatchBefore23August2015 = 2,
49 NeverMatch = 3,
52 enum class OCSPFetchStatus : uint16_t {
53 NotFetched = 0,
54 Fetched = 1,
57 SECStatus InitializeNSS(const nsACString& dir, NSSDBConfig nssDbConfig,
58 PKCS11DBConfig pkcs11DbConfig);
60 void DisableMD5();
62 /**
63 * Loads root certificates from a module.
65 * @param dir
66 * The path to the directory containing the NSS builtin roots module.
67 * Usually the same as the path to the other NSS shared libraries.
68 * If empty, the (library) path will be searched.
69 * @return true if the roots were successfully loaded, false otherwise.
71 bool LoadLoadableRoots(const nsCString& dir);
73 /**
74 * Loads the OS client certs module.
76 * @param dir
77 * The path to the directory containing the module. This should be the
78 * same as where all of the other gecko libraries live.
79 * @return true if the module was successfully loaded, false otherwise.
81 bool LoadOSClientCertsModule(const nsCString& dir);
83 extern const char* kOSClientCertsModuleName;
85 /**
86 * Loads the IPC client certs module.
88 * @param dir
89 * The path to the directory containing the module. This should be the
90 * same as where all of the other gecko libraries live.
91 * @return true if the module was successfully loaded, false otherwise.
93 bool LoadIPCClientCertsModule(const nsCString& dir);
95 extern const char* kIPCClientCertsModuleName;
97 /**
98 * Unloads the loadable roots module and os client certs module, if loaded.
100 void UnloadUserModules();
102 nsresult DefaultServerNicknameForCert(const CERTCertificate* cert,
103 /*out*/ nsCString& nickname);
106 * Build nsTArray<uint8_t>s out of the issuer, serial, subject and public key
107 * data from the supplied certificate for use in revocation checks.
109 * @param certDER
110 * The Input that references the encoded bytes of the certificate.
111 * @param endEntityOrCA
112 * Whether the certificate is an end-entity or CA.
113 * @param out encIssuer
114 * The array to populate with issuer data.
115 * @param out encSerial
116 * The array to populate with serial number data.
117 * @param out encSubject
118 * The array to populate with subject data.
119 * @param out encPubKey
120 * The array to populate with public key data.
121 * @return
122 * Result::Success, unless there's a problem decoding the certificate.
124 pkix::Result BuildRevocationCheckArrays(pkix::Input certDER,
125 pkix::EndEntityOrCA endEntityOrCA,
126 /*out*/ nsTArray<uint8_t>& issuerBytes,
127 /*out*/ nsTArray<uint8_t>& serialBytes,
128 /*out*/ nsTArray<uint8_t>& subjectBytes,
129 /*out*/ nsTArray<uint8_t>& pubKeyBytes);
131 void SaveIntermediateCerts(const nsTArray<nsTArray<uint8_t>>& certList);
133 class NSSCertDBTrustDomain : public mozilla::pkix::TrustDomain {
134 public:
135 typedef mozilla::pkix::Result Result;
137 enum OCSPFetching {
138 NeverFetchOCSP = 0,
139 FetchOCSPForDVSoftFail = 1,
140 FetchOCSPForDVHardFail = 2,
141 FetchOCSPForEV = 3,
142 LocalOnlyOCSPForEV = 4,
145 NSSCertDBTrustDomain(
146 SECTrustType certDBTrustType, OCSPFetching ocspFetching,
147 OCSPCache& ocspCache, void* pinArg, mozilla::TimeDuration ocspTimeoutSoft,
148 mozilla::TimeDuration ocspTimeoutHard, uint32_t certShortLifetimeInDays,
149 unsigned int minRSABits, ValidityCheckingMode validityCheckingMode,
150 NetscapeStepUpPolicy netscapeStepUpPolicy, CRLiteMode crliteMode,
151 const OriginAttributes& originAttributes,
152 const Vector<mozilla::pkix::Input>& thirdPartyRootInputs,
153 const Vector<mozilla::pkix::Input>& thirdPartyIntermediateInputs,
154 const Maybe<nsTArray<nsTArray<uint8_t>>>& extraCertificates,
155 /*out*/ nsTArray<nsTArray<uint8_t>>& builtChain,
156 /*optional*/ PinningTelemetryInfo* pinningTelemetryInfo = nullptr,
157 /*optional*/ const char* hostname = nullptr);
159 virtual Result FindIssuer(mozilla::pkix::Input encodedIssuerName,
160 IssuerChecker& checker,
161 mozilla::pkix::Time time) override;
163 virtual Result GetCertTrust(
164 mozilla::pkix::EndEntityOrCA endEntityOrCA,
165 const mozilla::pkix::CertPolicyId& policy,
166 mozilla::pkix::Input candidateCertDER,
167 /*out*/ mozilla::pkix::TrustLevel& trustLevel) override;
169 virtual Result CheckSignatureDigestAlgorithm(
170 mozilla::pkix::DigestAlgorithm digestAlg,
171 mozilla::pkix::EndEntityOrCA endEntityOrCA,
172 mozilla::pkix::Time notBefore) override;
174 virtual Result CheckRSAPublicKeyModulusSizeInBits(
175 mozilla::pkix::EndEntityOrCA endEntityOrCA,
176 unsigned int modulusSizeInBits) override;
178 virtual Result VerifyRSAPKCS1SignedData(
179 mozilla::pkix::Input data, mozilla::pkix::DigestAlgorithm digestAlgorithm,
180 mozilla::pkix::Input signature,
181 mozilla::pkix::Input subjectPublicKeyInfo) override;
183 virtual Result VerifyRSAPSSSignedData(
184 mozilla::pkix::Input data, mozilla::pkix::DigestAlgorithm digestAlgorithm,
185 mozilla::pkix::Input signature,
186 mozilla::pkix::Input subjectPublicKeyInfo) override;
188 virtual Result CheckECDSACurveIsAcceptable(
189 mozilla::pkix::EndEntityOrCA endEntityOrCA,
190 mozilla::pkix::NamedCurve curve) override;
192 virtual Result VerifyECDSASignedData(
193 mozilla::pkix::Input data, mozilla::pkix::DigestAlgorithm digestAlgorithm,
194 mozilla::pkix::Input signature,
195 mozilla::pkix::Input subjectPublicKeyInfo) override;
197 virtual Result DigestBuf(mozilla::pkix::Input item,
198 mozilla::pkix::DigestAlgorithm digestAlg,
199 /*out*/ uint8_t* digestBuf,
200 size_t digestBufLen) override;
202 virtual Result CheckValidityIsAcceptable(
203 mozilla::pkix::Time notBefore, mozilla::pkix::Time notAfter,
204 mozilla::pkix::EndEntityOrCA endEntityOrCA,
205 mozilla::pkix::KeyPurposeId keyPurpose) override;
207 virtual Result NetscapeStepUpMatchesServerAuth(
208 mozilla::pkix::Time notBefore,
209 /*out*/ bool& matches) override;
211 virtual Result CheckRevocation(
212 mozilla::pkix::EndEntityOrCA endEntityOrCA,
213 const mozilla::pkix::CertID& certID, mozilla::pkix::Time time,
214 mozilla::pkix::Duration validityDuration,
215 /*optional*/ const mozilla::pkix::Input* stapledOCSPResponse,
216 /*optional*/ const mozilla::pkix::Input* aiaExtension,
217 /*optional*/ const mozilla::pkix::Input* sctExtension) override;
219 virtual Result IsChainValid(
220 const mozilla::pkix::DERArray& certChain, mozilla::pkix::Time time,
221 const mozilla::pkix::CertPolicyId& requiredPolicy) override;
223 virtual void NoteAuxiliaryExtension(
224 mozilla::pkix::AuxiliaryExtension extension,
225 mozilla::pkix::Input extensionData) override;
227 // Resets the OCSP stapling status and SCT lists accumulated during
228 // the chain building.
229 void ResetAccumulatedState();
231 CertVerifier::OCSPStaplingStatus GetOCSPStaplingStatus() const {
232 return mOCSPStaplingStatus;
235 // SCT lists (see Certificate Transparency) extracted during
236 // certificate verification. Note that the returned Inputs are invalidated
237 // the next time a chain is built and by ResetAccumulatedState method
238 // (and when the TrustDomain object is destroyed).
240 mozilla::pkix::Input GetSCTListFromCertificate() const;
241 mozilla::pkix::Input GetSCTListFromOCSPStapling() const;
243 bool GetIsBuiltChainRootBuiltInRoot() const;
245 bool GetIsErrorDueToDistrustedCAPolicy() const;
247 OCSPFetchStatus GetOCSPFetchStatus() { return mOCSPFetchStatus; }
249 private:
250 Result CheckCRLiteStash(
251 const nsTArray<uint8_t>& issuerSubjectPublicKeyInfoBytes,
252 const nsTArray<uint8_t>& serialNumberBytes);
253 Result CheckCRLite(
254 const nsTArray<uint8_t>& issuerBytes,
255 const nsTArray<uint8_t>& issuerSubjectPublicKeyInfoBytes,
256 const nsTArray<uint8_t>& serialNumberBytes,
257 const nsTArray<RefPtr<nsICRLiteTimestamp>>& crliteTimestamps,
258 bool& filterCoversCertificate);
260 enum EncodedResponseSource {
261 ResponseIsFromNetwork = 1,
262 ResponseWasStapled = 2
264 Result VerifyAndMaybeCacheEncodedOCSPResponse(
265 const mozilla::pkix::CertID& certID, mozilla::pkix::Time time,
266 uint16_t maxLifetimeInDays, mozilla::pkix::Input encodedResponse,
267 EncodedResponseSource responseSource, /*out*/ bool& expired,
268 /*out*/ uint32_t& ageInHours);
269 TimeDuration GetOCSPTimeout() const;
271 Result CheckRevocationByCRLite(const mozilla::pkix::CertID& certID,
272 const mozilla::pkix::Input& sctExtension,
273 /*out*/ bool& crliteCoversCertificate);
275 Result CheckRevocationByOCSP(
276 const mozilla::pkix::CertID& certID, mozilla::pkix::Time time,
277 mozilla::pkix::Duration validityDuration, const nsCString& aiaLocation,
278 const bool crliteCoversCertificate, const Result crliteResult,
279 /*optional*/ const mozilla::pkix::Input* stapledOCSPResponse,
280 /*out*/ bool& softFailure);
282 Result SynchronousCheckRevocationWithServer(
283 const mozilla::pkix::CertID& certID, const nsCString& aiaLocation,
284 mozilla::pkix::Time time, uint16_t maxOCSPLifetimeInDays,
285 const Result cachedResponseResult, const Result stapledOCSPResponseResult,
286 const bool crliteFilterCoversCertificate, const Result crliteResult,
287 /*out*/ bool& softFailure);
288 Result HandleOCSPFailure(const Result cachedResponseResult,
289 const Result stapledOCSPResponseResult,
290 const Result error,
291 /*out*/ bool& softFailure);
293 const SECTrustType mCertDBTrustType;
294 const OCSPFetching mOCSPFetching;
295 OCSPCache& mOCSPCache; // non-owning!
296 void* mPinArg; // non-owning!
297 const mozilla::TimeDuration mOCSPTimeoutSoft;
298 const mozilla::TimeDuration mOCSPTimeoutHard;
299 const uint32_t mCertShortLifetimeInDays;
300 const unsigned int mMinRSABits;
301 ValidityCheckingMode mValidityCheckingMode;
302 NetscapeStepUpPolicy mNetscapeStepUpPolicy;
303 CRLiteMode mCRLiteMode;
304 bool mSawDistrustedCAByPolicyError;
305 const OriginAttributes& mOriginAttributes;
306 const Vector<mozilla::pkix::Input>& mThirdPartyRootInputs; // non-owning
307 const Vector<mozilla::pkix::Input>&
308 mThirdPartyIntermediateInputs; // non-owning
309 const Maybe<nsTArray<nsTArray<uint8_t>>>& mExtraCertificates; // non-owning
310 nsTArray<nsTArray<uint8_t>>& mBuiltChain; // non-owning
311 bool mIsBuiltChainRootBuiltInRoot;
312 PinningTelemetryInfo* mPinningTelemetryInfo;
313 const char* mHostname; // non-owning - only used for pinning checks
314 nsCOMPtr<nsICertStorage> mCertStorage;
315 CertVerifier::OCSPStaplingStatus mOCSPStaplingStatus;
316 // Certificate Transparency data extracted during certificate verification
317 UniqueSECItem mSCTListFromCertificate;
318 UniqueSECItem mSCTListFromOCSPStapling;
320 // The built-in roots module, if available.
321 UniqueSECMODModule mBuiltInRootsModule;
323 OCSPFetchStatus mOCSPFetchStatus;
326 } // namespace psm
327 } // namespace mozilla
329 #endif // NSSCertDBTrustDomain_h