| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.verifySubjectAlternativeName = verifySubjectAlternativeName; |
| 4 |
exports.verifyExtensions = verifyExtensions; |
| 5 |
exports.verifyOIDs = verifyOIDs; |
| 6 |
const error_1 = require("./error"); |
| 7 |
// Verifies that the signer's SAN matches the policy identity. The |
| 8 |
// policyIdentity is treated as a JavaScript regular expression pattern and |
| 9 |
// tested against the full signerIdentity string. For exact matching, use |
| 10 |
// anchored patterns (e.g. '^user@example\\.com$'). |
| 11 |
function verifySubjectAlternativeName(policyIdentity, signerIdentity) { |
| 12 |
if (signerIdentity === undefined || !signerIdentity.match(policyIdentity)) { |
| 13 |
throw new error_1.PolicyError({ |
| 14 |
code: 'UNTRUSTED_SIGNER_ERROR', |
| 15 |
message: `certificate identity error - expected ${policyIdentity}, got ${signerIdentity}`, |
| 16 |
}); |
| 17 |
} |
| 18 |
} |
| 19 |
function verifyExtensions(policyExtensions, signerExtensions = {}) { |
| 20 |
let key; |
| 21 |
for (key in policyExtensions) { |
| 22 |
if (signerExtensions[key] !== policyExtensions[key]) { |
| 23 |
throw new error_1.PolicyError({ |
| 24 |
code: 'UNTRUSTED_SIGNER_ERROR', |
| 25 |
message: `invalid certificate extension - expected ${key}=${policyExtensions[key]}, got ${key}=${signerExtensions[key]}`, |
| 26 |
}); |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
function verifyOIDs(policyOIDs, signerOIDs = []) { |
| 31 |
for (const policyOID of policyOIDs) { |
| 32 |
const match = signerOIDs.find((signerOID) => oidEquals(policyOID.oid?.id, signerOID.oid?.id) && |
| 33 |
policyOID.value.equals(signerOID.value)); |
| 34 |
if (!match) { |
| 35 |
/* istanbul ignore next */ |
| 36 |
const oid = policyOID.oid?.id.join('.') ?? '<unknown>'; |
| 37 |
throw new error_1.PolicyError({ |
| 38 |
code: 'UNTRUSTED_SIGNER_ERROR', |
| 39 |
message: `invalid certificate extension - missing OID ${oid}`, |
| 40 |
}); |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
function oidEquals(a, b) { |
| 45 |
/* istanbul ignore if */ |
| 46 |
if (a === undefined || b === undefined) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
return a.length === b.length && a.every((v, i) => v === b[i]); |
| 50 |
} |