Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added reading of pem files #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions bin/stateless-dane.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Comment on lines +127 to +144
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation for supporting both PEM and JSON formats for public key data is well-executed. Using a try-catch block to attempt parsing the PEM format first and falling back to JSON if it fails is a practical approach that ensures backward compatibility. However, consider adding explicit error handling or logging within the catch block to aid in debugging potential parsing issues.

+ catch (e) {
+   console.error('Failed to parse PEM, attempting JSON:', e.message);
    const obj = JSON.parse(publicKeyData)
    parsedKey = {
      n: Buffer.from(obj.n, 'hex'),
      e: Buffer.from(obj.e, 'hex'),
    };
+ }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
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)
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) {
console.error('Failed to parse PEM, attempting JSON:', e.message);
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) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}