| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.CertificateChainVerifier = void 0; |
| 4 |
exports.verifyCertificateChain = verifyCertificateChain; |
| 5 |
const error_1 = require("../error"); |
| 6 |
const trust_1 = require("../trust"); |
| 7 |
function verifyCertificateChain(timestamp, leaf, certificateAuthorities) { |
| 8 |
// Filter list of trusted CAs to those which are valid for the given |
| 9 |
// timestamp |
| 10 |
const cas = (0, trust_1.filterCertAuthorities)(certificateAuthorities, timestamp); |
| 11 |
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ |
| 12 |
let error; |
| 13 |
for (const ca of cas) { |
| 14 |
try { |
| 15 |
const verifier = new CertificateChainVerifier({ |
| 16 |
trustedCerts: ca.certChain, |
| 17 |
untrustedCert: leaf, |
| 18 |
timestamp, |
| 19 |
}); |
| 20 |
return verifier.verify(); |
| 21 |
} |
| 22 |
catch (err) { |
| 23 |
error = err; |
| 24 |
} |
| 25 |
} |
| 26 |
// If we failed to verify the certificate chain for all of the trusted |
| 27 |
// CAs, throw the last error we encountered. |
| 28 |
throw new error_1.VerificationError({ |
| 29 |
code: 'CERTIFICATE_ERROR', |
| 30 |
message: 'Failed to verify certificate chain', |
| 31 |
cause: error, |
| 32 |
}); |
| 33 |
} |
| 34 |
class CertificateChainVerifier { |
| 35 |
untrustedCert; |
| 36 |
trustedCerts; |
| 37 |
localCerts; |
| 38 |
timestamp; |
| 39 |
constructor(opts) { |
| 40 |
this.untrustedCert = opts.untrustedCert; |
| 41 |
this.trustedCerts = opts.trustedCerts; |
| 42 |
this.localCerts = dedupeCertificates([ |
| 43 |
...opts.trustedCerts, |
| 44 |
opts.untrustedCert, |
| 45 |
]); |
| 46 |
this.timestamp = opts.timestamp; |
| 47 |
} |
| 48 |
verify() { |
| 49 |
// Construct certificate path from leaf to root |
| 50 |
const certificatePath = this.sort(); |
| 51 |
// Perform validation checks on each certificate in the path |
| 52 |
this.checkPath(certificatePath); |
| 53 |
const validForDate = certificatePath.every((cert) => cert.validForDate(this.timestamp)); |
| 54 |
if (!validForDate) { |
| 55 |
throw new error_1.VerificationError({ |
| 56 |
code: 'CERTIFICATE_ERROR', |
| 57 |
message: 'certificate is not valid or expired at the specified date', |
| 58 |
}); |
| 59 |
} |
| 60 |
// Return verified certificate path |
| 61 |
return certificatePath; |
| 62 |
} |
| 63 |
sort() { |
| 64 |
const leafCert = this.untrustedCert; |
| 65 |
// Construct all possible paths from the leaf |
| 66 |
let paths = this.buildPaths(leafCert); |
| 67 |
// Filter for paths which contain a trusted certificate |
| 68 |
paths = paths.filter((path) => path.some((cert) => this.trustedCerts.includes(cert))); |
| 69 |
if (paths.length === 0) { |
| 70 |
throw new error_1.VerificationError({ |
| 71 |
code: 'CERTIFICATE_ERROR', |
| 72 |
message: 'no trusted certificate path found', |
| 73 |
}); |
| 74 |
} |
| 75 |
// Find the shortest of possible paths |
| 76 |
/* istanbul ignore next */ |
| 77 |
const path = paths.reduce((prev, curr) => prev.length < curr.length ? prev : curr); |
| 78 |
// Construct chain from shortest path |
| 79 |
// Removes the last certificate in the path, which will be a second copy |
| 80 |
// of the root certificate given that the root is self-signed. |
| 81 |
return [leafCert, ...path].slice(0, -1); |
| 82 |
} |
| 83 |
// Recursively build all possible paths from the leaf to the root |
| 84 |
buildPaths(certificate) { |
| 85 |
const paths = []; |
| 86 |
const issuers = this.findIssuer(certificate); |
| 87 |
if (issuers.length === 0) { |
| 88 |
throw new error_1.VerificationError({ |
| 89 |
code: 'CERTIFICATE_ERROR', |
| 90 |
message: 'no valid certificate path found', |
| 91 |
}); |
| 92 |
} |
| 93 |
for (let i = 0; i < issuers.length; i++) { |
| 94 |
const issuer = issuers[i]; |
| 95 |
// Base case - issuer is self |
| 96 |
if (issuer.equals(certificate)) { |
| 97 |
paths.push([certificate]); |
| 98 |
continue; |
| 99 |
} |
| 100 |
// Recursively build path for the issuer |
| 101 |
const subPaths = this.buildPaths(issuer); |
| 102 |
// Construct paths by appending the issuer to each subpath |
| 103 |
for (let j = 0; j < subPaths.length; j++) { |
| 104 |
paths.push([issuer, ...subPaths[j]]); |
| 105 |
} |
| 106 |
} |
| 107 |
return paths; |
| 108 |
} |
| 109 |
// Return all possible issuers for the given certificate |
| 110 |
findIssuer(certificate) { |
| 111 |
let issuers = []; |
| 112 |
let keyIdentifier; |
| 113 |
// Exit early if the certificate is self-signed |
| 114 |
if (certificate.subject.equals(certificate.issuer)) { |
| 115 |
if (certificate.verify()) { |
| 116 |
return [certificate]; |
| 117 |
} |
| 118 |
} |
| 119 |
// If the certificate has an authority key identifier, use that |
| 120 |
// to find the issuer |
| 121 |
if (certificate.extAuthorityKeyID) { |
| 122 |
keyIdentifier = certificate.extAuthorityKeyID.keyIdentifier; |
| 123 |
// TODO: Add support for authorityCertIssuer/authorityCertSerialNumber |
| 124 |
// though Fulcio doesn't appear to use these |
| 125 |
} |
| 126 |
// Find possible issuers by comparing the authorityKeyID/subjectKeyID |
| 127 |
// or issuer/subject. Potential issuers are added to the result array. |
| 128 |
this.localCerts.forEach((possibleIssuer) => { |
| 129 |
if (keyIdentifier) { |
| 130 |
/* istanbul ignore else */ |
| 131 |
if (possibleIssuer.extSubjectKeyID) { |
| 132 |
if (possibleIssuer.extSubjectKeyID.keyIdentifier.equals(keyIdentifier)) { |
| 133 |
issuers.push(possibleIssuer); |
| 134 |
} |
| 135 |
return; |
| 136 |
} |
| 137 |
} |
| 138 |
// Fallback to comparing certificate issuer and subject if |
| 139 |
// subjectKey/authorityKey extensions are not present |
| 140 |
if (possibleIssuer.subject.equals(certificate.issuer)) { |
| 141 |
issuers.push(possibleIssuer); |
| 142 |
} |
| 143 |
}); |
| 144 |
// Remove any issuers which fail to verify the certificate |
| 145 |
issuers = issuers.filter((issuer) => { |
| 146 |
try { |
| 147 |
return certificate.verify(issuer); |
| 148 |
} |
| 149 |
catch (ex) { |
| 150 |
/* istanbul ignore next - should never error */ |
| 151 |
return false; |
| 152 |
} |
| 153 |
}); |
| 154 |
return issuers; |
| 155 |
} |
| 156 |
checkPath(path) { |
| 157 |
/* istanbul ignore if */ |
| 158 |
if (path.length < 1) { |
| 159 |
throw new error_1.VerificationError({ |
| 160 |
code: 'CERTIFICATE_ERROR', |
| 161 |
message: 'certificate chain must contain at least one certificate', |
| 162 |
}); |
| 163 |
} |
| 164 |
// Ensure that all certificates beyond the leaf are CAs |
| 165 |
const validCAs = path.slice(1).every((cert) => cert.isCA); |
| 166 |
if (!validCAs) { |
| 167 |
throw new error_1.VerificationError({ |
| 168 |
code: 'CERTIFICATE_ERROR', |
| 169 |
message: 'intermediate certificate is not a CA', |
| 170 |
}); |
| 171 |
} |
| 172 |
// Certificate's issuer must match the subject of the next certificate |
| 173 |
// in the chain |
| 174 |
for (let i = path.length - 2; i >= 0; i--) { |
| 175 |
/* istanbul ignore if */ |
| 176 |
if (!path[i].issuer.equals(path[i + 1].subject)) { |
| 177 |
throw new error_1.VerificationError({ |
| 178 |
code: 'CERTIFICATE_ERROR', |
| 179 |
message: 'incorrect certificate name chaining', |
| 180 |
}); |
| 181 |
} |
| 182 |
} |
| 183 |
// Check pathlength constraints |
| 184 |
for (let i = 0; i < path.length; i++) { |
| 185 |
const cert = path[i]; |
| 186 |
// If the certificate is a CA, check the path length |
| 187 |
if (cert.extBasicConstraints?.isCA) { |
| 188 |
const pathLength = cert.extBasicConstraints.pathLenConstraint; |
| 189 |
// The path length, if set, indicates how many intermediate |
| 190 |
// certificates (NOT including the leaf) are allowed to follow. The |
| 191 |
// pathLength constraint of any intermediate CA certificate MUST be |
| 192 |
// greater than or equal to it's own depth in the chain (with an |
| 193 |
// adjustment for the leaf certificate) |
| 194 |
if (pathLength !== undefined && pathLength < i - 1) { |
| 195 |
throw new error_1.VerificationError({ |
| 196 |
code: 'CERTIFICATE_ERROR', |
| 197 |
message: 'path length constraint exceeded', |
| 198 |
}); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
exports.CertificateChainVerifier = CertificateChainVerifier; |
| 205 |
// Remove duplicate certificates from the array |
| 206 |
function dedupeCertificates(certs) { |
| 207 |
for (let i = 0; i < certs.length; i++) { |
| 208 |
for (let j = i + 1; j < certs.length; j++) { |
| 209 |
if (certs[i].equals(certs[j])) { |
| 210 |
certs.splice(j, 1); |
| 211 |
j--; |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
return certs; |
| 216 |
} |