1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "WebTransportCertificateVerifier.h"
#include "ScopedNSSTypes.h"
#include "nss/mozpkix/pkixutil.h"
#include "nss/mozpkix/pkixcheck.h"
#include "hasht.h"
namespace mozilla::psm {
class ServerCertHashesTrustDomain : public mozilla::pkix::TrustDomain {
public:
ServerCertHashesTrustDomain() = default;
virtual mozilla::pkix::Result FindIssuer(
mozilla::pkix::Input encodedIssuerName, IssuerChecker& checker,
mozilla::pkix::Time time) override;
virtual mozilla::pkix::Result GetCertTrust(
mozilla::pkix::EndEntityOrCA endEntityOrCA,
const mozilla::pkix::CertPolicyId& policy,
mozilla::pkix::Input candidateCertDER,
/*out*/ mozilla::pkix::TrustLevel& trustLevel) override;
virtual mozilla::pkix::Result CheckSignatureDigestAlgorithm(
mozilla::pkix::DigestAlgorithm digestAlg,
mozilla::pkix::EndEntityOrCA endEntityOrCA,
mozilla::pkix::Time notBefore) override;
virtual mozilla::pkix::Result CheckRSAPublicKeyModulusSizeInBits(
mozilla::pkix::EndEntityOrCA endEntityOrCA,
unsigned int modulusSizeInBits) override;
virtual mozilla::pkix::Result VerifyRSAPKCS1SignedData(
mozilla::pkix::Input data, mozilla::pkix::DigestAlgorithm digestAlgorithm,
mozilla::pkix::Input signature,
mozilla::pkix::Input subjectPublicKeyInfo) override;
virtual mozilla::pkix::Result VerifyRSAPSSSignedData(
mozilla::pkix::Input data, mozilla::pkix::DigestAlgorithm digestAlgorithm,
mozilla::pkix::Input signature,
mozilla::pkix::Input subjectPublicKeyInfo) override;
virtual mozilla::pkix::Result CheckECDSACurveIsAcceptable(
mozilla::pkix::EndEntityOrCA endEntityOrCA,
mozilla::pkix::NamedCurve curve) override;
virtual mozilla::pkix::Result VerifyECDSASignedData(
mozilla::pkix::Input data, mozilla::pkix::DigestAlgorithm digestAlgorithm,
mozilla::pkix::Input signature,
mozilla::pkix::Input subjectPublicKeyInfo) override;
virtual mozilla::pkix::Result DigestBuf(
mozilla::pkix::Input item, mozilla::pkix::DigestAlgorithm digestAlg,
/*out*/ uint8_t* digestBuf, size_t digestBufLen) override;
virtual mozilla::pkix::Result CheckValidityIsAcceptable(
mozilla::pkix::Time notBefore, mozilla::pkix::Time notAfter,
mozilla::pkix::EndEntityOrCA endEntityOrCA,
mozilla::pkix::KeyPurposeId keyPurpose) override;
virtual mozilla::pkix::Result CheckRevocation(
mozilla::pkix::EndEntityOrCA endEntityOrCA,
const mozilla::pkix::CertID& certID, mozilla::pkix::Time time,
mozilla::pkix::Duration validityDuration,
/*optional*/ const mozilla::pkix::Input* stapledOCSPResponse,
/*optional*/ const mozilla::pkix::Input* aiaExtension) override;
virtual mozilla::pkix::Result IsChainValid(
const mozilla::pkix::DERArray& certChain, mozilla::pkix::Time time,
const mozilla::pkix::CertPolicyId& requiredPolicy) override;
virtual void NoteAuxiliaryExtension(
mozilla::pkix::AuxiliaryExtension extension,
mozilla::pkix::Input extensionData) override;
};
mozilla::pkix::Result ServerCertHashesTrustDomain::FindIssuer(
mozilla::pkix::Input encodedIssuerName, IssuerChecker& checker,
mozilla::pkix::Time time) {
MOZ_ASSERT_UNREACHABLE("not expecting this to be called");
return mozilla::pkix::Result::FATAL_ERROR_LIBRARY_FAILURE;
}
mozilla::pkix::Result ServerCertHashesTrustDomain::GetCertTrust(
mozilla::pkix::EndEntityOrCA endEntityOrCA,
const mozilla::pkix::CertPolicyId& policy,
mozilla::pkix::Input candidateCertDER,
/*out*/ mozilla::pkix::TrustLevel& trustLevel) {
MOZ_ASSERT_UNREACHABLE("not expecting this to be called");
return mozilla::pkix::Result::FATAL_ERROR_LIBRARY_FAILURE;
}
mozilla::pkix::Result
ServerCertHashesTrustDomain::CheckSignatureDigestAlgorithm(
mozilla::pkix::DigestAlgorithm digestAlg,
mozilla::pkix::EndEntityOrCA endEntityOrCA, mozilla::pkix::Time notBefore) {
MOZ_ASSERT_UNREACHABLE("not expecting this to be called");
return mozilla::pkix::Result::FATAL_ERROR_LIBRARY_FAILURE;
}
mozilla::pkix::Result
ServerCertHashesTrustDomain::CheckRSAPublicKeyModulusSizeInBits(
mozilla::pkix::EndEntityOrCA endEntityOrCA,
unsigned int modulusSizeInBits) {
return mozilla::pkix::Result::
ERROR_UNSUPPORTED_KEYALG; // RSA is not supported for
// serverCertificateHashes,
// Chromium does only support it for an intermediate period due to spec
// change, we do not support it.
}
mozilla::pkix::Result ServerCertHashesTrustDomain::VerifyRSAPKCS1SignedData(
mozilla::pkix::Input data, mozilla::pkix::DigestAlgorithm digestAlgorithm,
mozilla::pkix::Input signature, mozilla::pkix::Input subjectPublicKeyInfo) {
MOZ_ASSERT_UNREACHABLE("not expecting this to be called");
return mozilla::pkix::Result::FATAL_ERROR_LIBRARY_FAILURE;
}
mozilla::pkix::Result ServerCertHashesTrustDomain::VerifyRSAPSSSignedData(
mozilla::pkix::Input data, mozilla::pkix::DigestAlgorithm digestAlgorithm,
mozilla::pkix::Input signature, mozilla::pkix::Input subjectPublicKeyInfo) {
MOZ_ASSERT_UNREACHABLE("not expecting this to be called");
return mozilla::pkix::Result::FATAL_ERROR_LIBRARY_FAILURE;
}
mozilla::pkix::Result ServerCertHashesTrustDomain::CheckECDSACurveIsAcceptable(
mozilla::pkix::EndEntityOrCA endEntityOrCA,
mozilla::pkix::NamedCurve curve) {
return mozilla::pkix::Result::Success;
}
mozilla::pkix::Result ServerCertHashesTrustDomain::VerifyECDSASignedData(
mozilla::pkix::Input data, mozilla::pkix::DigestAlgorithm digestAlgorithm,
mozilla::pkix::Input signature, mozilla::pkix::Input subjectPublicKeyInfo) {
MOZ_ASSERT_UNREACHABLE("not expecting this to be called");
return mozilla::pkix::Result::FATAL_ERROR_LIBRARY_FAILURE;
}
mozilla::pkix::Result ServerCertHashesTrustDomain::DigestBuf(
mozilla::pkix::Input item, mozilla::pkix::DigestAlgorithm digestAlg,
/*out*/ uint8_t* digestBuf, size_t digestBufLen) {
MOZ_ASSERT_UNREACHABLE("not expecting this to be called");
return mozilla::pkix::Result::FATAL_ERROR_LIBRARY_FAILURE;
}
mozilla::pkix::Result ServerCertHashesTrustDomain::CheckValidityIsAcceptable(
mozilla::pkix::Time notBefore, mozilla::pkix::Time notAfter,
mozilla::pkix::EndEntityOrCA endEntityOrCA,
mozilla::pkix::KeyPurposeId keyPurpose) {
MOZ_ASSERT_UNREACHABLE("not expecting this to be called");
return mozilla::pkix::Result::FATAL_ERROR_LIBRARY_FAILURE;
}
mozilla::pkix::Result ServerCertHashesTrustDomain::CheckRevocation(
mozilla::pkix::EndEntityOrCA endEntityOrCA,
const mozilla::pkix::CertID& certID, mozilla::pkix::Time time,
mozilla::pkix::Duration validityDuration,
/*optional*/ const mozilla::pkix::Input* stapledOCSPResponse,
/*optional*/ const mozilla::pkix::Input* aiaExtension) {
MOZ_ASSERT_UNREACHABLE("not expecting this to be called");
return mozilla::pkix::Result::FATAL_ERROR_LIBRARY_FAILURE;
}
mozilla::pkix::Result ServerCertHashesTrustDomain::IsChainValid(
const mozilla::pkix::DERArray& certChain, mozilla::pkix::Time time,
const mozilla::pkix::CertPolicyId& requiredPolicy) {
MOZ_ASSERT_UNREACHABLE("not expecting this to be called");
return mozilla::pkix::Result::FATAL_ERROR_LIBRARY_FAILURE;
}
void ServerCertHashesTrustDomain::NoteAuxiliaryExtension(
mozilla::pkix::AuxiliaryExtension extension,
mozilla::pkix::Input extensionData) {
MOZ_ASSERT_UNREACHABLE("not expecting this to be called");
}
} // namespace mozilla::psm
namespace mozilla::net {
// Does certificate verificate as required for serverCertificateHashes
// This function is currently only used for Quic, but may be used later also for
// http/2
mozilla::pkix::Result AuthCertificateWithServerCertificateHashes(
nsTArray<uint8_t>& peerCert,
const nsTArray<RefPtr<nsIWebTransportHash>>& aServerCertHashes) {
using namespace mozilla::pkix;
Input certDER;
mozilla::pkix::Result rv =
certDER.Init(peerCert.Elements(), peerCert.Length());
if (rv != Success) {
return rv;
}
BackCert cert(certDER, EndEntityOrCA::MustBeEndEntity, nullptr);
rv = cert.Init();
if (rv != Success) {
return rv;
}
Time notBefore(Time::uninitialized);
Time notAfter(Time::uninitialized);
rv = ParseValidity(cert.GetValidity(), ¬Before, ¬After);
if (rv != Success) {
return rv;
}
// now we check that validity is not greater than 14 days
Duration certDuration(notBefore, notAfter);
if (certDuration > Duration(60 * 60 * 24 * 14)) {
return mozilla::pkix::Result::ERROR_VALIDITY_TOO_LONG;
}
Time now = Now();
// and if the certificate is actually valid?
rv = CheckValidity(now, notBefore, notAfter);
if (rv != Success) {
return rv;
}
mozilla::psm::ServerCertHashesTrustDomain trustDomain;
rv = CheckSubjectPublicKeyInfo(
cert.GetSubjectPublicKeyInfo(), trustDomain,
EndEntityOrCA::MustBeEndEntity /* should be ignored*/);
if (rv != Success) {
return rv;
}
// ok now the final check, calculate the hash and compare it:
// https://w3c.github.io/webtransport/#compute-a-certificate-hash
nsTArray<uint8_t> certHash;
if (NS_FAILED(Digest::DigestBuf(SEC_OID_SHA256, peerCert, certHash)) ||
certHash.Length() != SHA256_LENGTH) {
return mozilla::pkix::Result::ERROR_INVALID_ALGORITHM;
}
// https://w3c.github.io/webtransport/#verify-a-certificate-hash
for (const auto& hash : aServerCertHashes) {
nsCString algorithm;
if (NS_FAILED(hash->GetAlgorithm(algorithm)) || algorithm != "sha-256") {
continue;
}
nsTArray<uint8_t> value;
if (NS_FAILED(hash->GetValue(value))) {
continue;
}
if (certHash == value) {
return Success;
}
}
return mozilla::pkix::Result::ERROR_UNTRUSTED_CERT;
}
} // namespace mozilla::net
|