Add SEIP.fromObject

To avoid defaulting to v1
This commit is contained in:
larabr
2023-08-28 15:31:00 +02:00
parent 1423bdd564
commit 278a61adab
3 changed files with 21 additions and 7 deletions

View File

@@ -397,12 +397,10 @@ export class Message {
const msg = await Message.encryptSessionKey(sessionKeyData, algorithmName, aeadAlgorithmName, encryptionKeys, passwords, wildcard, encryptionKeyIDs, date, userIDs, config);
const symEncryptedPacket = new SymEncryptedIntegrityProtectedDataPacket();
if (aeadAlgorithmName) {
symEncryptedPacket.version = 2;
symEncryptedPacket.aeadAlgorithm = enums.write(enums.aead, aeadAlgorithmName);
}
const symEncryptedPacket = SymEncryptedIntegrityProtectedDataPacket.fromObject({
version: aeadAlgorithmName ? 2 : 1,
aeadAlgorithm: aeadAlgorithmName ? enums.write(enums.aead, aeadAlgorithmName) : null
});
symEncryptedPacket.packets = this.packets;
const algorithm = enums.write(enums.symmetric, algorithmName);

View File

@@ -52,8 +52,22 @@ class SymEncryptedIntegrityProtectedDataPacket {
return enums.packet.symEncryptedIntegrityProtectedData;
}
static fromObject({ version, aeadAlgorithm }) {
if (version !== 1 && version !== 2) {
throw new Error('Unsupported SEIPD version');
}
const seip = new SymEncryptedIntegrityProtectedDataPacket();
seip.version = version;
if (version === 2) {
seip.aeadAlgorithm = aeadAlgorithm;
}
return seip;
}
constructor() {
this.version = 1;
this.version = null;
// The following 4 fields are for V2 only.
/** @type {enums.symmetric} */

View File

@@ -137,6 +137,7 @@ export default () => describe('Packet', function() {
const literal = new openpgp.LiteralDataPacket();
const enc = new openpgp.SymEncryptedIntegrityProtectedDataPacket();
enc.version = 1;
enc.packets = new openpgp.PacketList();
enc.packets.push(literal);
const msg = new openpgp.PacketList();
@@ -616,6 +617,7 @@ export default () => describe('Packet', function() {
literal.setText(testText);
const skesk = new openpgp.SymEncryptedSessionKeyPacket();
const seip = new openpgp.SymEncryptedIntegrityProtectedDataPacket();
seip.version = 1;
seip.packets = new openpgp.PacketList();
seip.packets.push(literal);
const msg = new openpgp.PacketList();