2026-08-04 11:53:47 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const Zga = require("../lib/zganode.js");
|
|
|
|
|
const forge = Zga.forge;
|
|
|
|
|
|
2026-08-04 13:40:37 +02:00
|
|
|
/** @type {Map<number, {privateKey: *, publicKey: *, certificate: *}>} key cache */
|
|
|
|
|
const keyCache = new Map();
|
2026-08-04 11:53:47 +02:00
|
|
|
|
|
|
|
|
/**
|
2026-08-04 13:40:37 +02:00
|
|
|
* Generate (and cache) a self-signed RSA key pair + certificate of the given
|
|
|
|
|
* modulus length, as raw node-forge objects.
|
2026-08-04 11:53:47 +02:00
|
|
|
*
|
|
|
|
|
* @param {number} bits RSA modulus length in bits.
|
2026-08-04 13:40:37 +02:00
|
|
|
* @return {{privateKey: *, publicKey: *, certificate: *}}
|
2026-08-04 11:53:47 +02:00
|
|
|
*/
|
2026-08-04 13:40:37 +02:00
|
|
|
function makeKeyCert(bits){
|
|
|
|
|
if(keyCache.has(bits)){
|
|
|
|
|
return keyCache.get(bits);
|
2026-08-04 11:53:47 +02:00
|
|
|
}
|
|
|
|
|
const keys = forge.pki.rsa.generateKeyPair({bits: bits, e: 0x10001});
|
|
|
|
|
const cert = forge.pki.createCertificate();
|
|
|
|
|
cert.publicKey = keys.publicKey;
|
|
|
|
|
cert.serialNumber = "01";
|
|
|
|
|
cert.validity.notBefore = new Date(2020, 0, 1);
|
|
|
|
|
cert.validity.notAfter = new Date(2030, 0, 1);
|
|
|
|
|
const attrs = [{name: "commonName", value: "zgapdfsigner-test"}];
|
|
|
|
|
cert.setSubject(attrs);
|
|
|
|
|
cert.setIssuer(attrs);
|
|
|
|
|
cert.sign(keys.privateKey, forge.md.sha256.create());
|
2026-08-04 13:40:37 +02:00
|
|
|
const entry = {privateKey: keys.privateKey, publicKey: keys.publicKey, certificate: cert};
|
|
|
|
|
keyCache.set(bits, entry);
|
|
|
|
|
return entry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build a self-signed PKCS#12 (returned as a DER binary string) carrying an
|
|
|
|
|
* RSA key of the given modulus length. Used to exercise the CCN-STIC-221
|
|
|
|
|
* key-length guard in loadP12cert without shipping binary fixtures.
|
|
|
|
|
*
|
|
|
|
|
* @param {number} bits RSA modulus length in bits.
|
|
|
|
|
* @param {string} pwd PKCS#12 password.
|
|
|
|
|
* @return {string} DER-encoded PKCS#12 as a binary string.
|
|
|
|
|
*/
|
|
|
|
|
function makeP12(bits, pwd){
|
|
|
|
|
const kc = makeKeyCert(bits);
|
|
|
|
|
const asn1 = forge.pkcs12.toPkcs12Asn1(kc.privateKey, [kc.certificate], pwd, {algorithm: "3des"});
|
|
|
|
|
return forge.asn1.toDer(asn1).getBytes();
|
2026-08-04 11:53:47 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-04 13:40:37 +02:00
|
|
|
module.exports = {makeKeyCert: makeKeyCert, makeP12: makeP12};
|