|
| 1 | +import { promises } from 'fs' |
| 2 | +import * as tls from 'tls' |
| 3 | + |
| 4 | +import { JWS } from 'jose' |
| 5 | +import * as forge from 'node-forge' |
| 6 | + |
| 7 | +import { getJwkFromRecipient, isX5C } from './keys' |
| 8 | + |
| 9 | +import certificateFromPem = forge.pki.certificateFromPem |
| 10 | + |
| 11 | +/** |
| 12 | + * Service to validate the certificate chain (using web PKI) for Verifiable PayIDs |
| 13 | + * signed with a a server key. |
| 14 | + */ |
| 15 | +export default class CertificateChainValidator { |
| 16 | + public caStore: forge.pki.CAStore |
| 17 | + |
| 18 | + public constructor() { |
| 19 | + this.caStore = forge.pki.createCaStore([]) |
| 20 | + this.importNodeRootCertficates() |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Verifies the certificate chain for any x5c certificates inside the JWS protected section. |
| 25 | + * |
| 26 | + * @param jws - The JWS to verify. |
| 27 | + * @returns True if verified. |
| 28 | + */ |
| 29 | + public verifyCertificateChainJWS(jws: JWS.GeneralJWS): boolean { |
| 30 | + return jws.signatures |
| 31 | + .map((recipient) => this.verifyCertificateChainRecipient(recipient)) |
| 32 | + .every((val) => val) |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Verifies the chain within the recipient. |
| 37 | + * |
| 38 | + * @param recipient - The recipient to verify. |
| 39 | + * @returns True if verified. |
| 40 | + */ |
| 41 | + public verifyCertificateChainRecipient(recipient: JWS.JWSRecipient): boolean { |
| 42 | + return this.verifyCertificateChain(extractX5CCertificates(recipient)) |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Verifies the chain within the list of certificates. |
| 47 | + * |
| 48 | + * @param chain - The list of certificates. |
| 49 | + * @returns True if verified. |
| 50 | + */ |
| 51 | + public verifyCertificateChain(chain: forge.pki.Certificate[]): boolean { |
| 52 | + if (chain.length === 0) { |
| 53 | + return true |
| 54 | + } |
| 55 | + try { |
| 56 | + return forge.pki.verifyCertificateChain(this.caStore, chain) |
| 57 | + } catch { |
| 58 | + return false |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Adds a root certificate to be included in certificate chain validation. |
| 64 | + * |
| 65 | + * @param certificate - The certificate text in PEM format. |
| 66 | + */ |
| 67 | + public addRootCertificate(certificate: string): void { |
| 68 | + const cert = forge.pki.certificateFromPem(certificate) |
| 69 | + this.caStore.addCertificate(cert) |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Adds a root certificate to be included in certificate chain validation. |
| 74 | + * |
| 75 | + * @param path - Path to certificate file in PEM format. |
| 76 | + */ |
| 77 | + public async addRootCertificateFile(path: string): Promise<void> { |
| 78 | + this.addRootCertificate(await promises.readFile(path, 'ascii')) |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Imports the Root certificates from Node into the CA store used for certificate chain validation. |
| 83 | + */ |
| 84 | + private importNodeRootCertficates(): void { |
| 85 | + for (const rootCert of tls.rootCertificates) { |
| 86 | + try { |
| 87 | + this.addRootCertificate(rootCert) |
| 88 | + } catch { |
| 89 | + // unsupported cert. just skip. |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Extract X5C certificates from the JWS protected section. |
| 97 | + * |
| 98 | + * @param recipient - The recipient to parse. |
| 99 | + * @returns List of certificates. |
| 100 | + */ |
| 101 | +export function extractX5CCertificates( |
| 102 | + recipient: JWS.JWSRecipient, |
| 103 | +): forge.pki.Certificate[] { |
| 104 | + const jwk = getJwkFromRecipient(recipient) |
| 105 | + if (jwk && isX5C(jwk) && jwk.x5c) { |
| 106 | + return jwk.x5c.map((pem) => |
| 107 | + certificateFromPem( |
| 108 | + `-----BEGIN CERTIFICATE-----${pem}-----END CERTIFICATE-----`, |
| 109 | + ), |
| 110 | + ) |
| 111 | + } |
| 112 | + return [] |
| 113 | +} |
0 commit comments