forked from fahrPool/fahrPool
fix: GCM-Tag- und IV-Laenge beim SMTP-Passwort festschreiben
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2f886a24df
commit
9b90bf6335
|
|
@ -30,6 +30,18 @@ describe("smtp-crypto", () => {
|
|||
expect(() => entschluessele([v, iv, tag, kaputt.toString("base64")].join(":"))).toThrow();
|
||||
});
|
||||
|
||||
it("gekuerzter Auth-Tag scheitert", () => {
|
||||
const [v, iv, tag, ct] = verschluessele("geheim").split(":");
|
||||
const kurz = Buffer.from(tag, "base64").subarray(0, 4).toString("base64");
|
||||
expect(() => entschluessele([v, iv, kurz, ct].join(":"))).toThrow("Unbekanntes Format");
|
||||
});
|
||||
|
||||
it("falsche IV-Laenge scheitert", () => {
|
||||
const [v, iv, tag, ct] = verschluessele("geheim").split(":");
|
||||
const kurz = Buffer.from(iv, "base64").subarray(0, 8).toString("base64");
|
||||
expect(() => entschluessele([v, kurz, tag, ct].join(":"))).toThrow("Unbekanntes Format");
|
||||
});
|
||||
|
||||
it("unbekanntes Format scheitert", () => {
|
||||
expect(() => entschluessele("klartext")).toThrow("Unbekanntes Format");
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
|
||||
|
||||
const ALGORITHMUS = "aes-256-gcm";
|
||||
const IV_LAENGE = 12;
|
||||
const TAG_LAENGE = 16;
|
||||
|
||||
function schluessel(): Buffer {
|
||||
const roh = process.env.SMTP_VERSCHLUESSELUNG_KEY;
|
||||
|
|
@ -17,8 +19,8 @@ function schluessel(): Buffer {
|
|||
// Format v1:<iv>:<tag>:<ciphertext>, alles base64. Die Version erlaubt einen
|
||||
// spaeteren Schluesselwechsel ohne Raten.
|
||||
export function verschluessele(klartext: string): string {
|
||||
const iv = randomBytes(12);
|
||||
const cipher = createCipheriv(ALGORITHMUS, schluessel(), iv);
|
||||
const iv = randomBytes(IV_LAENGE);
|
||||
const cipher = createCipheriv(ALGORITHMUS, schluessel(), iv, { authTagLength: TAG_LAENGE });
|
||||
const ciphertext = Buffer.concat([cipher.update(klartext, "utf8"), cipher.final()]);
|
||||
const tag = cipher.getAuthTag();
|
||||
return ["v1", iv.toString("base64"), tag.toString("base64"), ciphertext.toString("base64")].join(":");
|
||||
|
|
@ -30,7 +32,13 @@ export function entschluessele(wert: string): string {
|
|||
throw new Error("Unbekanntes Format des verschluesselten SMTP-Passworts");
|
||||
}
|
||||
const [, iv, tag, ciphertext] = teile;
|
||||
const decipher = createDecipheriv(ALGORITHMUS, schluessel(), Buffer.from(iv, "base64"));
|
||||
decipher.setAuthTag(Buffer.from(tag, "base64"));
|
||||
const ivBytes = Buffer.from(iv, "base64");
|
||||
const tagBytes = Buffer.from(tag, "base64");
|
||||
// Node akzeptiert sonst gekuerzte GCM-Tags; Laengen daher fest vorgeben.
|
||||
if (ivBytes.length !== IV_LAENGE || tagBytes.length !== TAG_LAENGE) {
|
||||
throw new Error("Unbekanntes Format des verschluesselten SMTP-Passworts");
|
||||
}
|
||||
const decipher = createDecipheriv(ALGORITHMUS, schluessel(), ivBytes, { authTagLength: TAG_LAENGE });
|
||||
decipher.setAuthTag(tagBytes);
|
||||
return Buffer.concat([decipher.update(Buffer.from(ciphertext, "base64")), decipher.final()]).toString("utf8");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue