From 76e19359b42f739c99193c8f94c31828f9ea33b6 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 20 Nov 2021 20:27:36 +0100 Subject: [PATCH] Replace subpacket generator in key spec classes --- .../pgpainless/key/generation/KeySpec.java | 10 ---- .../key/generation/KeySpecBuilder.java | 54 ++++++------------- 2 files changed, 15 insertions(+), 49 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpec.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpec.java index 5032c83a..6bffde16 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpec.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpec.java @@ -6,7 +6,6 @@ package org.pgpainless.key.generation; import javax.annotation.Nonnull; -import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator; import org.bouncycastle.openpgp.PGPSignatureSubpacketVector; import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.key.generation.type.KeyType; @@ -19,15 +18,6 @@ public class KeySpec { private final SignatureSubpackets subpacketGenerator; private final boolean inheritedSubPackets; - KeySpec(@Nonnull KeyType type, - @Nonnull PGPSignatureSubpacketGenerator subpacketGenerator, - boolean inheritedSubPackets) { - this( - type, - SignatureSubpackets.createSubpacketsFrom(subpacketGenerator.generate()), - inheritedSubPackets); - } - KeySpec(@Nonnull KeyType type, @Nonnull SignatureSubpackets subpacketGenerator, boolean inheritedSubPackets) { diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpecBuilder.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpecBuilder.java index 3a99bb94..07b53383 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpecBuilder.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpecBuilder.java @@ -5,12 +5,10 @@ package org.pgpainless.key.generation; import java.util.Arrays; -import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Set; import javax.annotation.Nonnull; -import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator; import org.pgpainless.PGPainless; import org.pgpainless.algorithm.AlgorithmSuite; import org.pgpainless.algorithm.CompressionAlgorithm; @@ -19,6 +17,8 @@ import org.pgpainless.algorithm.HashAlgorithm; import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.SymmetricKeyAlgorithm; import org.pgpainless.key.generation.type.KeyType; +import org.pgpainless.signature.subpackets.SelfSignatureSubpackets; +import org.pgpainless.signature.subpackets.SignatureSubpackets; import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil; import org.pgpainless.util.CollectionUtils; @@ -26,7 +26,7 @@ public class KeySpecBuilder implements KeySpecBuilderInterface { private final KeyType type; private final KeyFlag[] keyFlags; - private final PGPSignatureSubpacketGenerator hashedSubPackets = new PGPSignatureSubpacketGenerator(); + private final SelfSignatureSubpackets hashedSubpackets = new SignatureSubpackets(); private final AlgorithmSuite algorithmSuite = PGPainless.getPolicy().getKeyGenerationAlgorithmSuite(); private Set preferredCompressionAlgorithms = algorithmSuite.getCompressionAlgorithms(); private Set preferredHashAlgorithms = algorithmSuite.getHashAlgorithms(); @@ -46,19 +46,22 @@ public class KeySpecBuilder implements KeySpecBuilderInterface { } @Override - public KeySpecBuilder overridePreferredCompressionAlgorithms(@Nonnull CompressionAlgorithm... compressionAlgorithms) { + public KeySpecBuilder overridePreferredCompressionAlgorithms( + @Nonnull CompressionAlgorithm... compressionAlgorithms) { this.preferredCompressionAlgorithms = new LinkedHashSet<>(Arrays.asList(compressionAlgorithms)); return this; } @Override - public KeySpecBuilder overridePreferredHashAlgorithms(@Nonnull HashAlgorithm... preferredHashAlgorithms) { + public KeySpecBuilder overridePreferredHashAlgorithms( + @Nonnull HashAlgorithm... preferredHashAlgorithms) { this.preferredHashAlgorithms = new LinkedHashSet<>(Arrays.asList(preferredHashAlgorithms)); return this; } @Override - public KeySpecBuilder overridePreferredSymmetricKeyAlgorithms(@Nonnull SymmetricKeyAlgorithm... preferredSymmetricKeyAlgorithms) { + public KeySpecBuilder overridePreferredSymmetricKeyAlgorithms( + @Nonnull SymmetricKeyAlgorithm... preferredSymmetricKeyAlgorithms) { this.preferredSymmetricAlgorithms = new LinkedHashSet<>(Arrays.asList(preferredSymmetricKeyAlgorithms)); return this; } @@ -66,39 +69,12 @@ public class KeySpecBuilder implements KeySpecBuilderInterface { @Override public KeySpec build() { - this.hashedSubPackets.setKeyFlags(false, KeyFlag.toBitmask(keyFlags)); - this.hashedSubPackets.setPreferredCompressionAlgorithms(false, getPreferredCompressionAlgorithmIDs()); - this.hashedSubPackets.setPreferredHashAlgorithms(false, getPreferredHashAlgorithmIDs()); - this.hashedSubPackets.setPreferredSymmetricAlgorithms(false, getPreferredSymmetricKeyAlgorithmIDs()); - this.hashedSubPackets.setFeature(false, Feature.MODIFICATION_DETECTION.getFeatureId()); + this.hashedSubpackets.setKeyFlags(keyFlags); + this.hashedSubpackets.setPreferredCompressionAlgorithms(preferredCompressionAlgorithms); + this.hashedSubpackets.setPreferredHashAlgorithms(preferredHashAlgorithms); + this.hashedSubpackets.setPreferredSymmetricKeyAlgorithms(preferredSymmetricAlgorithms); + this.hashedSubpackets.setFeatures(Feature.MODIFICATION_DETECTION); - return new KeySpec(type, hashedSubPackets, false); - } - - private int[] getPreferredCompressionAlgorithmIDs() { - int[] ids = new int[preferredCompressionAlgorithms.size()]; - Iterator iterator = preferredCompressionAlgorithms.iterator(); - for (int i = 0; i < ids.length; i++) { - ids[i] = iterator.next().getAlgorithmId(); - } - return ids; - } - - private int[] getPreferredHashAlgorithmIDs() { - int[] ids = new int[preferredHashAlgorithms.size()]; - Iterator iterator = preferredHashAlgorithms.iterator(); - for (int i = 0; i < ids.length; i++) { - ids[i] = iterator.next().getAlgorithmId(); - } - return ids; - } - - private int[] getPreferredSymmetricKeyAlgorithmIDs() { - int[] ids = new int[preferredSymmetricAlgorithms.size()]; - Iterator iterator = preferredSymmetricAlgorithms.iterator(); - for (int i = 0; i < ids.length; i++) { - ids[i] = iterator.next().getAlgorithmId(); - } - return ids; + return new KeySpec(type, (SignatureSubpackets) hashedSubpackets, false); } }