From 6718e12be8c254f19ab4768a004fbab267b7e551 Mon Sep 17 00:00:00 2001 From: Nikolai Ovtsinnikov Date: Tue, 2 Apr 2024 15:48:40 +0300 Subject: [PATCH] fix dkim issue --- lib/dkim-handler.js | 42 ++++++++++++++++++++---------------------- test/api/dkim-test.js | 16 ++++++++++++++++ 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/lib/dkim-handler.js b/lib/dkim-handler.js index 47173975..267775f8 100644 --- a/lib/dkim-handler.js +++ b/lib/dkim-handler.js @@ -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; diff --git a/test/api/dkim-test.js b/test/api/dkim-test.js index ce61a5a5..06cf6484 100644 --- a/test/api/dkim-test.js +++ b/test/api/dkim-test.js @@ -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')