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

ZMS-125 #617

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 22 additions & 10 deletions lib/dkim-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const ObjectId = require('mongodb').ObjectId;
const fingerprint = require('key-fingerprint').fingerprint;
const forge = require('node-forge');
const crypto = require('crypto');
const tools = require('./tools');
const { publish, DKIM_CREATED, DKIM_UPDATED, DKIM_DELETED } = require('./events');
Expand Down Expand Up @@ -47,6 +46,7 @@ class DkimHandler {

let privateKeyPem = options.privateKey;
let publicKeyPem;
let publicKeyDer;

if (!privateKeyPem) {
let keyPair = await this.generateKey();
Expand All @@ -61,12 +61,18 @@ class DkimHandler {
}

if (!publicKeyPem) {
// extract public key from private key using Forge
let privateKey = forge.pki.privateKeyFromPem(privateKeyPem);
let publicKey = forge.pki.setRsaPublicKey(privateKey.n, privateKey.e);
publicKeyPem = forge.pki.publicKeyToPem(publicKey);
// extract public key from private key

if (!publicKeyPem) {
const publicKey = crypto.createPublicKey({ key: privateKeyPem, format: 'pem' });

if (publicKey.asymmetricKeyType === 'ed25519') {
publicKeyPem = publicKey.export({ type: 'spki', format: 'pem' });
publicKeyDer = publicKey.export({ format: 'der', type: 'spki' });
} else if (publicKey.asymmetricKeyType === 'rsa') {
publicKeyPem = publicKey.export({ format: 'pem', type: 'spki' });
}

if (!publicKeyPem && !publicKeyDer) {
let err = new Error('Failed to generate public key');
err.responseCode = 500;
err.code = 'KeyGenereateError';
Expand All @@ -78,9 +84,11 @@ class DkimHandler {
try {
fp = fingerprint(privateKeyPem, 'sha256', true);

let ciphered = crypto.publicEncrypt(publicKeyPem, Buffer.from('secretvalue'));
let deciphered = crypto.privateDecrypt(privateKeyPem, ciphered);
if (deciphered.toString() !== 'secretvalue') {
const testData = Buffer.from('secretvalue');
const signature = crypto.sign(null, testData, privateKeyPem);
const verificationResult = crypto.verify(null, testData, publicKeyPem, signature);

if (!verificationResult) {
throw new Error('Was not able to use key for encryption');
}
} catch (E) {
Expand All @@ -107,6 +115,10 @@ class DkimHandler {
dkimData.description = description;
}

if (publicKeyDer) {
dkimData.publicKeyDer = publicKeyDer.toString('base64');
}

let r;

try {
Expand Down Expand Up @@ -170,7 +182,7 @@ class DkimHandler {
publicKey: dkimData.publicKey,
dnsTxt: {
name: dkimData.selector + '._domainkey.' + dkimData.domain,
value: 'v=DKIM1;t=s;p=' + dkimData.publicKey.replace(/^-.*-$/gm, '').replace(/\s/g, '')
value: 'v=DKIM1;t=s;p=' + (dkimData.publicKeyDer || dkimData.publicKey.replace(/^-.*-$/gm, '').replace(/\s/g, ''))
}
};
}
Expand Down