2026-09-15 13:00:32 +02:00
|
|
|
"use strict";
|
2020-02-29 23:01:03 +01:00
|
|
|
// FIXME:
|
|
|
|
|
// replace this Transform mess with a method that pipes input argument to output argument
|
2026-09-15 13:00:32 +02:00
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
|
};
|
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
|
const message_parser_js_1 = __importDefault(require("./message-parser.js"));
|
|
|
|
|
const relaxed_body_js_1 = __importDefault(require("./relaxed-body.js"));
|
|
|
|
|
const sign_js_1 = __importDefault(require("./sign.js"));
|
|
|
|
|
const node_stream_1 = require("node:stream");
|
|
|
|
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
|
|
|
const node_path_1 = __importDefault(require("node:path"));
|
|
|
|
|
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
|
|
|
const objects_js_1 = require("../shared/objects.js");
|
2020-02-29 23:01:03 +01:00
|
|
|
const DKIM_ALGO = 'sha256';
|
2026-09-15 13:00:32 +02:00
|
|
|
const MAX_MESSAGE_SIZE = 10 * 1024 * 1024; // buffer messages larger than this to disk
|
2020-02-29 23:01:03 +01:00
|
|
|
class DKIMSigner {
|
|
|
|
|
constructor(options, keys, input, output) {
|
|
|
|
|
this.options = options || {};
|
|
|
|
|
this.keys = keys;
|
|
|
|
|
this.cacheTreshold = Number(this.options.cacheTreshold) || MAX_MESSAGE_SIZE;
|
|
|
|
|
this.hashAlgo = this.options.hashAlgo || DKIM_ALGO;
|
|
|
|
|
this.cacheDir = this.options.cacheDir || false;
|
|
|
|
|
this.chunks = [];
|
|
|
|
|
this.chunklen = 0;
|
|
|
|
|
this.readPos = 0;
|
2025-12-25 10:58:28 +01:00
|
|
|
this.cachePath = this.cacheDir
|
2026-09-15 13:00:32 +02:00
|
|
|
? node_path_1.default.join(this.cacheDir, 'message.' + Date.now() + '-' + node_crypto_1.default.randomBytes(14).toString('hex'))
|
2025-12-25 10:58:28 +01:00
|
|
|
: false;
|
2020-02-29 23:01:03 +01:00
|
|
|
this.cache = false;
|
|
|
|
|
this.headers = false;
|
|
|
|
|
this.bodyHash = false;
|
|
|
|
|
this.parser = false;
|
|
|
|
|
this.relaxedBody = false;
|
|
|
|
|
this.input = input;
|
|
|
|
|
this.output = output;
|
|
|
|
|
this.output.usingCache = false;
|
2022-10-21 16:52:45 +02:00
|
|
|
this.hasErrored = false;
|
2020-02-29 23:01:03 +01:00
|
|
|
this.input.on('error', err => {
|
2022-10-21 16:52:45 +02:00
|
|
|
this.hasErrored = true;
|
2020-02-29 23:01:03 +01:00
|
|
|
this.cleanup();
|
|
|
|
|
output.emit('error', err);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
cleanup() {
|
|
|
|
|
if (!this.cache || !this.cachePath) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-09-15 13:00:32 +02:00
|
|
|
node_fs_1.default.unlink(this.cachePath, () => false);
|
2020-02-29 23:01:03 +01:00
|
|
|
}
|
|
|
|
|
createReadCache() {
|
|
|
|
|
// pipe remainings to cache file
|
2026-09-15 13:00:32 +02:00
|
|
|
this.cache = node_fs_1.default.createReadStream(this.cachePath);
|
2020-02-29 23:01:03 +01:00
|
|
|
this.cache.once('error', err => {
|
|
|
|
|
this.cleanup();
|
|
|
|
|
this.output.emit('error', err);
|
|
|
|
|
});
|
|
|
|
|
this.cache.once('close', () => {
|
|
|
|
|
this.cleanup();
|
|
|
|
|
});
|
|
|
|
|
this.cache.pipe(this.output);
|
|
|
|
|
}
|
|
|
|
|
sendNextChunk() {
|
2022-10-21 16:52:45 +02:00
|
|
|
if (this.hasErrored) {
|
2020-02-29 23:01:03 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (this.readPos >= this.chunks.length) {
|
|
|
|
|
if (!this.cache) {
|
2026-09-15 13:00:32 +02:00
|
|
|
this.output.end();
|
|
|
|
|
return;
|
2020-02-29 23:01:03 +01:00
|
|
|
}
|
|
|
|
|
return this.createReadCache();
|
|
|
|
|
}
|
2026-04-28 12:50:45 +02:00
|
|
|
const chunk = this.chunks[this.readPos++];
|
2020-02-29 23:01:03 +01:00
|
|
|
if (this.output.write(chunk) === false) {
|
2026-09-15 13:00:32 +02:00
|
|
|
this.output.once('drain', () => {
|
2020-02-29 23:01:03 +01:00
|
|
|
this.sendNextChunk();
|
|
|
|
|
});
|
2026-09-15 13:00:32 +02:00
|
|
|
return;
|
2020-02-29 23:01:03 +01:00
|
|
|
}
|
|
|
|
|
setImmediate(() => this.sendNextChunk());
|
|
|
|
|
}
|
|
|
|
|
sendSignedOutput() {
|
|
|
|
|
let keyPos = 0;
|
2026-04-28 12:50:45 +02:00
|
|
|
const signNextKey = () => {
|
2020-02-29 23:01:03 +01:00
|
|
|
if (keyPos >= this.keys.length) {
|
|
|
|
|
this.output.write(this.parser.rawHeaders);
|
2026-09-15 13:00:32 +02:00
|
|
|
setImmediate(() => this.sendNextChunk());
|
|
|
|
|
return;
|
2020-02-29 23:01:03 +01:00
|
|
|
}
|
2026-04-28 12:50:45 +02:00
|
|
|
const key = this.keys[keyPos++];
|
2026-09-15 13:00:32 +02:00
|
|
|
const dkimField = (0, sign_js_1.default)(this.headers, this.hashAlgo, this.bodyHash, {
|
2020-02-29 23:01:03 +01:00
|
|
|
domainName: key.domainName,
|
|
|
|
|
keySelector: key.keySelector,
|
|
|
|
|
privateKey: key.privateKey,
|
|
|
|
|
headerFieldNames: this.options.headerFieldNames,
|
|
|
|
|
skipFields: this.options.skipFields
|
|
|
|
|
});
|
|
|
|
|
if (dkimField) {
|
|
|
|
|
this.output.write(Buffer.from(dkimField + '\r\n'));
|
|
|
|
|
}
|
2026-09-15 13:00:32 +02:00
|
|
|
setImmediate(signNextKey);
|
2020-02-29 23:01:03 +01:00
|
|
|
};
|
|
|
|
|
if (this.bodyHash && this.headers) {
|
|
|
|
|
return signNextKey();
|
|
|
|
|
}
|
|
|
|
|
this.output.write(this.parser.rawHeaders);
|
|
|
|
|
this.sendNextChunk();
|
|
|
|
|
}
|
|
|
|
|
createWriteCache() {
|
|
|
|
|
this.output.usingCache = true;
|
|
|
|
|
// pipe remainings to cache file
|
2026-09-15 13:00:32 +02:00
|
|
|
this.cache = node_fs_1.default.createWriteStream(this.cachePath);
|
2020-02-29 23:01:03 +01:00
|
|
|
this.cache.once('error', err => {
|
|
|
|
|
this.cleanup();
|
|
|
|
|
// drain input
|
|
|
|
|
this.relaxedBody.unpipe(this.cache);
|
|
|
|
|
this.relaxedBody.on('readable', () => {
|
|
|
|
|
while (this.relaxedBody.read() !== null) {
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-10-21 16:52:45 +02:00
|
|
|
this.hasErrored = true;
|
2020-02-29 23:01:03 +01:00
|
|
|
// emit error
|
|
|
|
|
this.output.emit('error', err);
|
|
|
|
|
});
|
|
|
|
|
this.cache.once('close', () => {
|
|
|
|
|
this.sendSignedOutput();
|
|
|
|
|
});
|
|
|
|
|
this.relaxedBody.removeAllListeners('readable');
|
|
|
|
|
this.relaxedBody.pipe(this.cache);
|
|
|
|
|
}
|
|
|
|
|
signStream() {
|
2026-09-15 13:00:32 +02:00
|
|
|
this.parser = new message_parser_js_1.default();
|
|
|
|
|
this.relaxedBody = new relaxed_body_js_1.default({
|
2020-02-29 23:01:03 +01:00
|
|
|
hashAlgo: this.hashAlgo
|
|
|
|
|
});
|
|
|
|
|
this.parser.on('headers', value => {
|
|
|
|
|
this.headers = value;
|
|
|
|
|
});
|
|
|
|
|
this.relaxedBody.on('hash', value => {
|
|
|
|
|
this.bodyHash = value;
|
|
|
|
|
});
|
|
|
|
|
this.relaxedBody.on('readable', () => {
|
|
|
|
|
let chunk;
|
|
|
|
|
if (this.cache) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
while ((chunk = this.relaxedBody.read()) !== null) {
|
|
|
|
|
this.chunks.push(chunk);
|
|
|
|
|
this.chunklen += chunk.length;
|
|
|
|
|
if (this.chunklen >= this.cacheTreshold && this.cachePath) {
|
|
|
|
|
return this.createWriteCache();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
this.relaxedBody.on('end', () => {
|
|
|
|
|
if (this.cache) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.sendSignedOutput();
|
|
|
|
|
});
|
|
|
|
|
this.parser.pipe(this.relaxedBody);
|
|
|
|
|
setImmediate(() => this.input.pipe(this.parser));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
class DKIM {
|
|
|
|
|
constructor(options) {
|
|
|
|
|
this.options = options || {};
|
2026-09-15 13:00:32 +02:00
|
|
|
this.keys = [].concat(this.options.keys || {
|
|
|
|
|
domainName: options.domainName,
|
|
|
|
|
keySelector: options.keySelector,
|
|
|
|
|
privateKey: options.privateKey
|
|
|
|
|
});
|
2020-02-29 23:01:03 +01:00
|
|
|
}
|
|
|
|
|
sign(input, extraOptions) {
|
2026-09-15 13:00:32 +02:00
|
|
|
const output = new node_stream_1.PassThrough();
|
2020-02-29 23:01:03 +01:00
|
|
|
let inputStream = input;
|
|
|
|
|
let writeValue = false;
|
|
|
|
|
if (Buffer.isBuffer(input)) {
|
|
|
|
|
writeValue = input;
|
2026-09-15 13:00:32 +02:00
|
|
|
inputStream = new node_stream_1.PassThrough();
|
|
|
|
|
}
|
|
|
|
|
else if (typeof input === 'string') {
|
2020-02-29 23:01:03 +01:00
|
|
|
writeValue = Buffer.from(input);
|
2026-09-15 13:00:32 +02:00
|
|
|
inputStream = new node_stream_1.PassThrough();
|
2020-02-29 23:01:03 +01:00
|
|
|
}
|
|
|
|
|
let options = this.options;
|
|
|
|
|
if (extraOptions && Object.keys(extraOptions).length) {
|
2026-09-07 07:05:39 +02:00
|
|
|
// extraOptions is mail.data._dkim, caller supplied message data. An own
|
|
|
|
|
// "__proto__" key there would let every option this signer reads and the
|
|
|
|
|
// transport did not set, such as skipFields, answer from the caller
|
2026-09-15 13:00:32 +02:00
|
|
|
options = (0, objects_js_1.copyOwnKeys)({}, extraOptions);
|
|
|
|
|
(0, objects_js_1.copyOwnKeys)(options, this.options);
|
2020-02-29 23:01:03 +01:00
|
|
|
}
|
2026-04-28 12:50:45 +02:00
|
|
|
const signer = new DKIMSigner(options, this.keys, inputStream, output);
|
2020-02-29 23:01:03 +01:00
|
|
|
setImmediate(() => {
|
|
|
|
|
signer.signStream();
|
|
|
|
|
if (writeValue) {
|
|
|
|
|
setImmediate(() => {
|
|
|
|
|
inputStream.end(writeValue);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-15 13:00:32 +02:00
|
|
|
exports.default = DKIM;
|
|
|
|
|
module.exports = exports.default;
|
|
|
|
|
Object.defineProperty(module.exports, 'default', { value: exports.default, enumerable: false, writable: true, configurable: true });
|