mirror of
https://github.com/nodemailer/wildduck.git
synced 2024-09-20 07:16:05 +08:00
fix(api-dkim): Fix empty p value in dnsText.value if no private key initially given ZMS-142 (#664)
* fix dkim issue * make better test
This commit is contained in:
parent
1f27778ae8
commit
d9834776f3
|
@ -62,34 +62,32 @@ class DkimHandler {
|
|||
publicKeyPem = keyPair.publicKey;
|
||||
}
|
||||
|
||||
if (!publicKeyPem) {
|
||||
// extract public key from 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-----
|
||||
// 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;
|
||||
|
|
|
@ -16,6 +16,25 @@ 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;
|
||||
expect(response.body.dnsTxt.value).to.not.be.undefined;
|
||||
expect(response.body.dnsTxt.value.split('p=').length).to.be.equal(2); // check that splitting is correct
|
||||
expect(response.body.dnsTxt.value.split('p=')[1]).to.be.not.empty; // check that we actually have the key part and it is not an empty string
|
||||
});
|
||||
|
||||
it('should POST /dkim expect success / RSA pem', async () => {
|
||||
const response = await server
|
||||
.post('/dkim')
|
||||
|
|
Loading…
Reference in a new issue