electron/script/codesign/gen-trust.js

39 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
const cp = require('child_process');
const fs = require('fs');
const path = require('path');
2020-03-20 20:28:31 +00:00
const certificatePath = process.argv[2];
const outPath = process.argv[3];
const templatePath = path.resolve(__dirname, 'trust.xml');
2020-03-20 20:28:31 +00:00
const template = fs.readFileSync(templatePath, 'utf8');
2020-03-20 20:28:31 +00:00
const fingerprintResult = cp.spawnSync('openssl', ['x509', '-noout', '-fingerprint', '-sha1', '-in', certificatePath]);
if (fingerprintResult.status !== 0) {
2020-03-20 20:28:31 +00:00
console.error(fingerprintResult.stderr.toString());
process.exit(1);
}
2020-03-20 20:28:31 +00:00
const fingerprint = fingerprintResult.stdout.toString().replace(/^SHA1 Fingerprint=/, '').replace(/:/g, '').trim();
2020-03-20 20:28:31 +00:00
const serialResult = cp.spawnSync('openssl', ['x509', '-serial', '-noout', '-in', certificatePath]);
if (serialResult.status !== 0) {
2020-03-20 20:28:31 +00:00
console.error(serialResult.stderr.toString());
process.exit(1);
}
2020-03-20 20:28:31 +00:00
let serialHex = serialResult.stdout.toString().replace(/^serial=/, '').trim();
// Pad the serial number out to 18 hex chars
while (serialHex.length < 18) {
2020-03-20 20:28:31 +00:00
serialHex = `0${serialHex}`;
}
2020-03-20 20:28:31 +00:00
const serialB64 = Buffer.from(serialHex, 'hex').toString('base64');
const trust = template
.replace(/{{FINGERPRINT}}/g, fingerprint)
2020-03-20 20:28:31 +00:00
.replace(/{{SERIAL_BASE64}}/g, serialB64);
2020-03-20 20:28:31 +00:00
fs.writeFileSync(outPath, trust);
2020-03-20 20:28:31 +00:00
console.log('Generated Trust Settings');