Skip to content

Commit

Permalink
fix dkim issue
Browse files Browse the repository at this point in the history
  • Loading branch information
NickOvt committed Apr 2, 2024
1 parent 2d08b61 commit 6718e12
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
42 changes: 20 additions & 22 deletions lib/dkim-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,34 +62,32 @@ class DkimHandler {
publicKeyPem = keyPair.publicKey;
}

if (!publicKeyPem) {
// extract public key from private key

// 1) check that privateKeyPem is ED25519 raw key, which length is 44
if (privateKeyPem.length === 44) {
// privateKeyPem is actually a raw ED25519 base64 string with length of 44
// convert raw ED25519 key to PEM formatted private key
privateKeyPem = `-----BEGIN PRIVATE KEY-----
// extract public key from private key

// 1) check that privateKeyPem is ED25519 raw key, which length is 44
if (privateKeyPem.length === 44) {
// privateKeyPem is actually a raw ED25519 base64 string with length of 44
// convert raw ED25519 key to PEM formatted private key
privateKeyPem = `-----BEGIN PRIVATE KEY-----
${Buffer.concat([Buffer.from(ASN1_PADDING, 'base64'), Buffer.from(privateKeyPem, 'base64')]).toString('base64')}
-----END PRIVATE KEY-----`;
}
}

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

publicKeyPem = publicKey.export({ type: 'spki', format: 'pem' });
publicKeyPem = publicKey.export({ type: 'spki', format: 'pem' });

if (publicKey.asymmetricKeyType === 'ed25519') {
publicKeyDer = publicKey.export({ format: 'der', type: 'spki' }).subarray(12).toString('base64');
} else if (publicKey.asymmetricKeyType === 'rsa') {
publicKeyDer = publicKey.export({ format: 'der', type: 'spki' }).toString('base64');
}
if (publicKey.asymmetricKeyType === 'ed25519') {
publicKeyDer = publicKey.export({ format: 'der', type: 'spki' }).subarray(12).toString('base64');
} else if (publicKey.asymmetricKeyType === 'rsa') {
publicKeyDer = publicKey.export({ format: 'der', type: 'spki' }).toString('base64');
}

if (!publicKeyPem && !publicKeyDer) {
let err = new Error('Failed to generate public key');
err.responseCode = 500;
err.code = 'KeyGenereateError';
throw err;
}
if (!publicKeyPem && !publicKeyDer) {
let err = new Error('Failed to generate public key');
err.responseCode = 500;
err.code = 'KeyGenereateError';
throw err;
}

let fp;
Expand Down
16 changes: 16 additions & 0 deletions test/api/dkim-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ describe('API DKIM', function () {

this.timeout(10000); // eslint-disable-line no-invalid-this

it('should POST /dkim expect success / key empty', async () => {
const response = await server
.post('/dkim')
.send({
domain: 'example.com',
selector: 'wildduck',
description: 'Some text about this DKIM certificate',
sess: '12345',
ip: '127.0.0.1'
})
.expect(200);
expect(response.body.success).to.be.true;
expect(/^[0-9a-f]{24}$/.test(response.body.id)).to.be.true;
dkim = response.body.id;
});

it('should POST /dkim expect success / RSA pem', async () => {
const response = await server
.post('/dkim')
Expand Down

0 comments on commit 6718e12

Please sign in to comment.