gen-trust.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. const cp = require('child_process');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const certificatePath = process.argv[2];
  5. const outPath = process.argv[3];
  6. const templatePath = path.resolve(__dirname, 'trust.xml');
  7. const template = fs.readFileSync(templatePath, 'utf8');
  8. const fingerprintResult = cp.spawnSync('openssl', ['x509', '-noout', '-fingerprint', '-sha1', '-in', certificatePath]);
  9. if (fingerprintResult.status !== 0) {
  10. console.error(fingerprintResult.stderr.toString());
  11. process.exit(1);
  12. }
  13. const fingerprint = fingerprintResult.stdout.toString().replace(/^SHA1 Fingerprint=/, '').replace(/:/g, '').trim();
  14. const serialResult = cp.spawnSync('openssl', ['x509', '-serial', '-noout', '-in', certificatePath]);
  15. if (serialResult.status !== 0) {
  16. console.error(serialResult.stderr.toString());
  17. process.exit(1);
  18. }
  19. let serialHex = serialResult.stdout.toString().replace(/^serial=/, '').trim();
  20. // Pad the serial number out to 18 hex chars
  21. while (serialHex.length < 18) {
  22. serialHex = `0${serialHex}`;
  23. }
  24. const serialB64 = Buffer.from(serialHex, 'hex').toString('base64');
  25. const trust = template
  26. .replace(/{{FINGERPRINT}}/g, fingerprint)
  27. .replace(/{{SERIAL_BASE64}}/g, serialB64);
  28. fs.writeFileSync(outPath, trust);
  29. console.log('Generated Trust Settings');