From b6fbab044350bcba999b0e8eaca74c327f7518f6 Mon Sep 17 00:00:00 2001 From: larabr <7375870+larabr@users.noreply.github.com> Date: Fri, 29 Sep 2023 13:00:44 +0200 Subject: [PATCH] Internally use `createSignaturePacket` helper whenever possible --- src/key/factory.js | 34 +++++++++++++++++----------------- src/key/helper.js | 17 +++++++---------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/key/factory.js b/src/key/factory.js index b2477960..44d7462f 100644 --- a/src/key/factory.js +++ b/src/key/factory.js @@ -197,50 +197,50 @@ async function wrapKeyObject(secretKeyPacket, secretSubkeyPackets, options, conf const dataToSign = {}; dataToSign.userID = userIDPacket; dataToSign.key = secretKeyPacket; - const signaturePacket = new SignaturePacket(); - signaturePacket.signatureType = enums.signature.certGeneric; - signaturePacket.publicKeyAlgorithm = secretKeyPacket.algorithm; - signaturePacket.hashAlgorithm = await helper.getPreferredHashAlgo(null, secretKeyPacket, undefined, undefined, config); - signaturePacket.keyFlags = [enums.keyFlags.certifyKeys | enums.keyFlags.signData]; - signaturePacket.preferredSymmetricAlgorithms = createPreferredAlgos([ + + const signatureProperties = {}; + signatureProperties.signatureType = enums.signature.certGeneric; + signatureProperties.keyFlags = [enums.keyFlags.certifyKeys | enums.keyFlags.signData]; + signatureProperties.preferredSymmetricAlgorithms = createPreferredAlgos([ // prefer aes256, aes128, then aes192 (no WebCrypto support: https://www.chromium.org/blink/webcrypto#TOC-AES-support) enums.symmetric.aes256, enums.symmetric.aes128, enums.symmetric.aes192 ], config.preferredSymmetricAlgorithm); if (config.aeadProtect) { - signaturePacket.preferredAEADAlgorithms = createPreferredAlgos([ + signatureProperties.preferredAEADAlgorithms = createPreferredAlgos([ enums.aead.eax, enums.aead.ocb ], config.preferredAEADAlgorithm); } - signaturePacket.preferredHashAlgorithms = createPreferredAlgos([ + signatureProperties.preferredHashAlgorithms = createPreferredAlgos([ // prefer fast asm.js implementations (SHA-256) enums.hash.sha256, enums.hash.sha512 ], config.preferredHashAlgorithm); - signaturePacket.preferredCompressionAlgorithms = createPreferredAlgos([ + signatureProperties.preferredCompressionAlgorithms = createPreferredAlgos([ enums.compression.zlib, enums.compression.zip, enums.compression.uncompressed ], config.preferredCompressionAlgorithm); if (index === 0) { - signaturePacket.isPrimaryUserID = true; + signatureProperties.isPrimaryUserID = true; } // integrity protection always enabled - signaturePacket.features = [0]; - signaturePacket.features[0] |= enums.features.modificationDetection; + signatureProperties.features = [0]; + signatureProperties.features[0] |= enums.features.modificationDetection; if (config.aeadProtect) { - signaturePacket.features[0] |= enums.features.aead; + signatureProperties.features[0] |= enums.features.aead; } if (config.v5Keys) { - signaturePacket.features[0] |= enums.features.v5Keys; + signatureProperties.features[0] |= enums.features.v5Keys; } if (options.keyExpirationTime > 0) { - signaturePacket.keyExpirationTime = options.keyExpirationTime; - signaturePacket.keyNeverExpires = false; + signatureProperties.keyExpirationTime = options.keyExpirationTime; + signatureProperties.keyNeverExpires = false; } - await signaturePacket.sign(secretKeyPacket, dataToSign, options.date); + + const signaturePacket = await helper.createSignaturePacket(dataToSign, null, secretKeyPacket, signatureProperties, options.date, undefined, undefined, undefined, config); return { userIDPacket, signaturePacket }; })).then(list => { diff --git a/src/key/helper.js b/src/key/helper.js index 00ae8554..77507151 100644 --- a/src/key/helper.js +++ b/src/key/helper.js @@ -86,23 +86,20 @@ export async function createBindingSignature(subkey, primaryKey, options, config const dataToSign = {}; dataToSign.key = primaryKey; dataToSign.bind = subkey; - const subkeySignaturePacket = new SignaturePacket(); - subkeySignaturePacket.signatureType = enums.signature.subkeyBinding; - subkeySignaturePacket.publicKeyAlgorithm = primaryKey.algorithm; - subkeySignaturePacket.hashAlgorithm = await getPreferredHashAlgo(null, primaryKey, undefined, undefined, config); + const signatureProperties = { signatureType: enums.signature.subkeyBinding }; if (options.sign) { - subkeySignaturePacket.keyFlags = [enums.keyFlags.signData]; - subkeySignaturePacket.embeddedSignature = await createSignaturePacket(dataToSign, null, subkey, { + signatureProperties.keyFlags = [enums.keyFlags.signData]; + signatureProperties.embeddedSignature = await createSignaturePacket(dataToSign, null, subkey, { signatureType: enums.signature.keyBinding }, options.date, undefined, undefined, undefined, config); } else { - subkeySignaturePacket.keyFlags = [enums.keyFlags.encryptCommunication | enums.keyFlags.encryptStorage]; + signatureProperties.keyFlags = [enums.keyFlags.encryptCommunication | enums.keyFlags.encryptStorage]; } if (options.keyExpirationTime > 0) { - subkeySignaturePacket.keyExpirationTime = options.keyExpirationTime; - subkeySignaturePacket.keyNeverExpires = false; + signatureProperties.keyExpirationTime = options.keyExpirationTime; + signatureProperties.keyNeverExpires = false; } - await subkeySignaturePacket.sign(primaryKey, dataToSign, options.date); + const subkeySignaturePacket = await createSignaturePacket(dataToSign, null, primaryKey, signatureProperties, options.date, undefined, undefined, undefined, config); return subkeySignaturePacket; }