From 8f8b8f534b99db2d1134b0af194b814047232d7d Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Tue, 1 Aug 2023 16:21:46 +0200 Subject: [PATCH] Fix test and add temporary workaround --- .../subpackets/SignatureSubpacketsHelper.java | 16 ++++++++++++++++ .../subpackets/SignatureSubpacketsTest.java | 18 +++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/SignatureSubpacketsHelper.java b/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/SignatureSubpacketsHelper.java index 2cb98d47..013dc088 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/SignatureSubpacketsHelper.java +++ b/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/SignatureSubpacketsHelper.java @@ -32,6 +32,9 @@ import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.PublicKeyAlgorithm; import org.pgpainless.key.util.RevocationAttributes; +import java.util.ArrayList; +import java.util.List; + public class SignatureSubpacketsHelper { public static SignatureSubpackets applyFrom(PGPSignatureSubpacketVector vector, SignatureSubpackets subpackets) { @@ -81,6 +84,19 @@ public class SignatureSubpacketsHelper { subpackets.setPreferredCompressionAlgorithms((PreferredAlgorithms) subpacket); break; case preferredAEADAlgorithms: + // Workaround for https://github.com/pgpainless/pgpainless/pull/399 + // TODO: Remove when BC 1.77 is released + if (subpacket instanceof PreferredAlgorithms) { + List combinationList = new ArrayList<>(); + int[] algorithms = ((PreferredAlgorithms) subpacket).getPreferences(); + for (int i = 0; i < algorithms.length; i += 2) { + combinationList.add(new PreferredAEADCiphersuites.Combination(algorithms[i], algorithms[i + 1])); + } + PreferredAEADCiphersuites aead = new PreferredAEADCiphersuites( + subpacket.isCritical(), combinationList.toArray(new PreferredAEADCiphersuites.Combination[0])); + subpackets.setPreferredAEADCiphersuites(aead); + break; + } subpackets.setPreferredAEADCiphersuites((PreferredAEADCiphersuites) subpacket); break; case primaryUserId: diff --git a/pgpainless-core/src/test/java/org/pgpainless/signature/subpackets/SignatureSubpacketsTest.java b/pgpainless-core/src/test/java/org/pgpainless/signature/subpackets/SignatureSubpacketsTest.java index beb87ec5..34177ec0 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/signature/subpackets/SignatureSubpacketsTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/signature/subpackets/SignatureSubpacketsTest.java @@ -17,12 +17,15 @@ import java.util.Date; import java.util.Iterator; import java.util.Random; +import org.bouncycastle.bcpg.AEADAlgorithmTags; import org.bouncycastle.bcpg.SignatureSubpacket; import org.bouncycastle.bcpg.SignatureSubpacketTags; +import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; import org.bouncycastle.bcpg.sig.Exportable; import org.bouncycastle.bcpg.sig.Features; import org.bouncycastle.bcpg.sig.IssuerFingerprint; import org.bouncycastle.bcpg.sig.NotationData; +import org.bouncycastle.bcpg.sig.PreferredAEADCiphersuites; import org.bouncycastle.bcpg.sig.PreferredAlgorithms; import org.bouncycastle.bcpg.sig.Revocable; import org.bouncycastle.bcpg.sig.RevocationKey; @@ -481,9 +484,14 @@ public class SignatureSubpacketsTest { new Random().nextBytes(hash); subpackets.setSignatureTarget(false, publicKeys.getPublicKey().getAlgorithm(), HashAlgorithm.SHA512.getAlgorithmId(), hash); subpackets.addIntendedRecipientFingerprint(true, publicKeys.getPublicKey()); - PreferredAlgorithms aead = new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_AEAD_ALGORITHMS, false, new int[] {2}); - subpackets.addCustomSubpacket(aead); - + PreferredAEADCiphersuites.Combination[] aead = new PreferredAEADCiphersuites.Combination[] { + new PreferredAEADCiphersuites.Combination(SymmetricKeyAlgorithmTags.AES_256, AEADAlgorithmTags.OCB), + new PreferredAEADCiphersuites.Combination(SymmetricKeyAlgorithmTags.AES_128, AEADAlgorithmTags.EAX) + }; + subpackets.setPreferredAEADAlgorithms(false, new int[] { + aead[0].getSymmetricAlgorithm(), aead[0].getAeadAlgorithm(), + aead[1].getSymmetricAlgorithm(), aead[1].getAeadAlgorithm() + }); SignatureSubpackets wrapper = SignatureSubpackets.createSubpacketsFrom(subpackets.generate()); PGPSignatureSubpacketVector vector = SignatureSubpacketsHelper.toVector(wrapper); @@ -530,7 +538,7 @@ public class SignatureSubpacketsTest { assertEquals(HashAlgorithm.SHA512.getAlgorithmId(), signatureTarget.getHashAlgorithm()); assertArrayEquals(hash, signatureTarget.getHashData()); assertArrayEquals(publicKeys.getPublicKey().getFingerprint(), vector.getIntendedRecipientFingerprint().getFingerprint()); - PreferredAlgorithms aeadAlgorithms = (PreferredAlgorithms) vector.getSubpacket(SignatureSubpacketTags.PREFERRED_AEAD_ALGORITHMS); - assertArrayEquals(aead.getPreferences(), aeadAlgorithms.getPreferences()); + PreferredAEADCiphersuites aeadAlgorithms = (PreferredAEADCiphersuites) vector.getSubpacket(SignatureSubpacketTags.PREFERRED_AEAD_ALGORITHMS); + assertArrayEquals(aead, aeadAlgorithms.getRawAlgorithms()); } }