dcrouter/test/suite/smtpclient_security/test.csec-03.dkim-signing.ts

138 lines
3.9 KiB
TypeScript
Raw Normal View History

2025-05-24 16:19:19 +00:00
import { tap, expect } from '@git.zone/tstest/tapbundle';
2025-05-26 14:50:55 +00:00
import { startTestServer, stopTestServer, type ITestServer } from '../../helpers/server.loader.js';
import { createTestSmtpClient } from '../../helpers/smtp.client.js';
2025-05-24 16:19:19 +00:00
import { Email } from '../../../ts/mail/core/classes.email.js';
import * as crypto from 'crypto';
2025-05-26 14:50:55 +00:00
let testServer: ITestServer;
2025-05-24 16:19:19 +00:00
tap.test('setup test SMTP server', async () => {
2025-05-26 14:50:55 +00:00
testServer = await startTestServer({
port: 2563,
tlsEnabled: false,
authRequired: false
});
2025-05-24 16:19:19 +00:00
expect(testServer).toBeTruthy();
expect(testServer.port).toBeGreaterThan(0);
});
tap.test('CSEC-03: Basic DKIM signature structure', async () => {
2025-05-26 14:50:55 +00:00
const smtpClient = createTestSmtpClient({
2025-05-24 16:19:19 +00:00
host: testServer.hostname,
port: testServer.port,
secure: false,
connectionTimeout: 5000,
debug: true
});
// Create email with DKIM configuration
const email = new Email({
from: 'sender@example.com',
to: ['recipient@example.com'],
subject: 'DKIM Signed Email',
2025-05-26 14:50:55 +00:00
text: 'This email should be DKIM signed'
2025-05-24 16:19:19 +00:00
});
2025-05-26 14:50:55 +00:00
// Note: DKIM signing would be handled by the Email class or SMTP client
// This test verifies the structure when it's implemented
2025-05-24 16:19:19 +00:00
const result = await smtpClient.sendMail(email);
2025-05-26 14:50:55 +00:00
expect(result.success).toBeTruthy();
2025-05-24 16:19:19 +00:00
2025-05-26 14:50:55 +00:00
console.log('Email sent successfully');
console.log('Note: DKIM signing functionality would be applied here');
2025-05-24 16:19:19 +00:00
await smtpClient.close();
});
tap.test('CSEC-03: DKIM with RSA key generation', async () => {
// Generate a test RSA key pair
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem'
}
});
console.log('Generated RSA key pair for DKIM:');
console.log('Public key (first line):', publicKey.split('\n')[1].substring(0, 50) + '...');
// Create DNS TXT record format
const publicKeyBase64 = publicKey
.replace(/-----BEGIN PUBLIC KEY-----/, '')
.replace(/-----END PUBLIC KEY-----/, '')
.replace(/\s/g, '');
console.log('\nDNS TXT record for default._domainkey.example.com:');
console.log(`v=DKIM1; k=rsa; p=${publicKeyBase64.substring(0, 50)}...`);
2025-05-26 14:50:55 +00:00
const smtpClient = createTestSmtpClient({
2025-05-24 16:19:19 +00:00
host: testServer.hostname,
port: testServer.port,
secure: false,
connectionTimeout: 5000,
debug: true
});
const email = new Email({
from: 'sender@example.com',
to: ['recipient@example.com'],
subject: 'DKIM with Real RSA Key',
2025-05-26 14:50:55 +00:00
text: 'This email is signed with a real RSA key'
2025-05-24 16:19:19 +00:00
});
const result = await smtpClient.sendMail(email);
2025-05-26 14:50:55 +00:00
expect(result.success).toBeTruthy();
2025-05-24 16:19:19 +00:00
await smtpClient.close();
});
tap.test('CSEC-03: DKIM body hash calculation', async () => {
2025-05-26 14:50:55 +00:00
const smtpClient = createTestSmtpClient({
2025-05-24 16:19:19 +00:00
host: testServer.hostname,
port: testServer.port,
secure: false,
connectionTimeout: 5000,
2025-05-26 14:50:55 +00:00
debug: false
2025-05-24 16:19:19 +00:00
});
// Test body hash with different content
const testBodies = [
2025-05-26 14:50:55 +00:00
{ name: 'Simple text', body: 'Hello World' },
{ name: 'Multi-line text', body: 'Line 1\r\nLine 2\r\nLine 3' },
{ name: 'Empty body', body: '' }
2025-05-24 16:19:19 +00:00
];
for (const test of testBodies) {
console.log(`\nTesting body hash for: ${test.name}`);
// Calculate expected body hash
const canonicalBody = test.body.replace(/\r\n/g, '\n').trimEnd() + '\n';
const bodyHash = crypto.createHash('sha256').update(canonicalBody).digest('base64');
console.log(` Expected hash: ${bodyHash.substring(0, 20)}...`);
const email = new Email({
from: 'sender@example.com',
to: ['recipient@example.com'],
subject: `Body Hash Test: ${test.name}`,
2025-05-26 14:50:55 +00:00
text: test.body
2025-05-24 16:19:19 +00:00
});
2025-05-26 14:50:55 +00:00
const result = await smtpClient.sendMail(email);
expect(result.success).toBeTruthy();
2025-05-24 16:19:19 +00:00
}
await smtpClient.close();
});
tap.test('cleanup test SMTP server', async () => {
if (testServer) {
2025-05-26 14:50:55 +00:00
await stopTestServer(testServer);
2025-05-24 16:19:19 +00:00
}
});
2025-05-26 14:50:55 +00:00
tap.start();