From dd342a93b94a36f5c473e2640e2f4b3c3d02489b Mon Sep 17 00:00:00 2001 From: Alexander Shevtsov Date: Sun, 3 Mar 2024 17:57:15 +0100 Subject: [PATCH 1/2] added reading of pem files --- bin/stateless-dane.js | 29 +++++++++++++++++++++-------- package.json | 3 ++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/bin/stateless-dane.js b/bin/stateless-dane.js index bec9d7d..ab0d939 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'); @@ -89,9 +90,9 @@ Examples: const sign = config.bool('sign', true); 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); @@ -121,12 +122,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 +} From 8739628cda378d3a0def41a84136a0dfc223ab67 Mon Sep 17 00:00:00 2001 From: Alexander Shevtsov Date: Mon, 4 Mar 2024 04:25:05 +0100 Subject: [PATCH 2/2] port --- bin/stateless-dane.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bin/stateless-dane.js b/bin/stateless-dane.js index ab0d939..1ea1fe7 100755 --- a/bin/stateless-dane.js +++ b/bin/stateless-dane.js @@ -89,6 +89,7 @@ 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 publicKeyData if (publicKeyFile) { @@ -99,6 +100,7 @@ Examples: const options = { resolverIP: config.str('resolver-ip') || undefined, resolverPort: config.str('resolver-port') || undefined, + port: config.uint('port') || undefined, } switch (command) {