diff --git a/bin/stateless-dane.js b/bin/stateless-dane.js index bec9d7d..1ea1fe7 100755 --- a/bin/stateless-dane.js +++ b/bin/stateless-dane.js @@ -5,6 +5,7 @@ const fs = require('node:fs'); const Config = require('bcfg'); const { NodeClient } = require('hs-client'); const rsa = require('bcrypto/lib/rsa'); +const forge = require('node-forge') const { StatelessDANECertificate } = require('../lib'); const pkg = require('../package.json'); @@ -88,16 +89,18 @@ Examples: const command = config.str(0); const sign = config.bool('sign', true); + const port = config.uint('port', 443); const publicKeyFile = config.str('public-key-file'); - var publicKeyJson + var publicKeyData if (publicKeyFile) { - publicKeyJson = JSON.parse(fs.readFileSync(publicKeyFile, 'utf8')); + publicKeyData = fs.readFileSync(publicKeyFile, 'utf8') } const parsed = config.bool('parsed', true); const options = { resolverIP: config.str('resolver-ip') || undefined, resolverPort: config.str('resolver-port') || undefined, + port: config.uint('port') || undefined, } switch (command) { @@ -121,12 +124,24 @@ Examples: return; } const cert = new StatelessDANECertificate(nodeClient, name, options); - if (publicKeyJson) { - const parsed = { - n: Buffer.from(publicKeyJson.n, 'hex'), - e: Buffer.from(publicKeyJson.e, 'hex'), - }; - cert.publicKey = rsa.publicKeyImport(parsed); + if (publicKeyData) { + let parsedKey + try { + const publicKey = forge.pki.publicKeyFromPem(publicKeyData); + parsedKey = { + n: Buffer.from(publicKey.n.toByteArray()), // modulus + e: Buffer.from(publicKey.e.toByteArray()), // exponent + }; + + } + catch (e) { + const obj = JSON.parse(publicKeyData) + parsedKey = { + n: Buffer.from(obj.n, 'hex'), + e: Buffer.from(obj.e, 'hex'), + }; + } + cert.publicKey = rsa.publicKeyImport(parsedKey) } await cert.create(); if (sign) { diff --git a/package.json b/package.json index 109ab57..f7ad3a8 100644 --- a/package.json +++ b/package.json @@ -35,9 +35,10 @@ "bns": "^0.15.0", "bsert": "^0.0.10", "hs-client": "^0.0.13", + "node-forge": "^1.3.1", "urkel": "^1.0.3" }, "devDependencies": { "bmocha": "^2.1.8" } -} \ No newline at end of file +}