1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-26 22:32:07 +01:00

Replace subpacket generator in key spec classes

This commit is contained in:
Paul Schaub 2021-11-20 20:27:36 +01:00
parent 91080f411d
commit 76e19359b4
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 15 additions and 49 deletions

View file

@ -6,7 +6,6 @@ package org.pgpainless.key.generation;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
import org.bouncycastle.openpgp.PGPSignatureSubpacketVector; import org.bouncycastle.openpgp.PGPSignatureSubpacketVector;
import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.key.generation.type.KeyType; import org.pgpainless.key.generation.type.KeyType;
@ -19,15 +18,6 @@ public class KeySpec {
private final SignatureSubpackets subpacketGenerator; private final SignatureSubpackets subpacketGenerator;
private final boolean inheritedSubPackets; private final boolean inheritedSubPackets;
KeySpec(@Nonnull KeyType type,
@Nonnull PGPSignatureSubpacketGenerator subpacketGenerator,
boolean inheritedSubPackets) {
this(
type,
SignatureSubpackets.createSubpacketsFrom(subpacketGenerator.generate()),
inheritedSubPackets);
}
KeySpec(@Nonnull KeyType type, KeySpec(@Nonnull KeyType type,
@Nonnull SignatureSubpackets subpacketGenerator, @Nonnull SignatureSubpackets subpacketGenerator,
boolean inheritedSubPackets) { boolean inheritedSubPackets) {

View file

@ -5,12 +5,10 @@
package org.pgpainless.key.generation; package org.pgpainless.key.generation;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.AlgorithmSuite; import org.pgpainless.algorithm.AlgorithmSuite;
import org.pgpainless.algorithm.CompressionAlgorithm; import org.pgpainless.algorithm.CompressionAlgorithm;
@ -19,6 +17,8 @@ import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm; import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.key.generation.type.KeyType; 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.signature.subpackets.SignatureSubpacketsUtil;
import org.pgpainless.util.CollectionUtils; import org.pgpainless.util.CollectionUtils;
@ -26,7 +26,7 @@ public class KeySpecBuilder implements KeySpecBuilderInterface {
private final KeyType type; private final KeyType type;
private final KeyFlag[] keyFlags; private final KeyFlag[] keyFlags;
private final PGPSignatureSubpacketGenerator hashedSubPackets = new PGPSignatureSubpacketGenerator(); private final SelfSignatureSubpackets hashedSubpackets = new SignatureSubpackets();
private final AlgorithmSuite algorithmSuite = PGPainless.getPolicy().getKeyGenerationAlgorithmSuite(); private final AlgorithmSuite algorithmSuite = PGPainless.getPolicy().getKeyGenerationAlgorithmSuite();
private Set<CompressionAlgorithm> preferredCompressionAlgorithms = algorithmSuite.getCompressionAlgorithms(); private Set<CompressionAlgorithm> preferredCompressionAlgorithms = algorithmSuite.getCompressionAlgorithms();
private Set<HashAlgorithm> preferredHashAlgorithms = algorithmSuite.getHashAlgorithms(); private Set<HashAlgorithm> preferredHashAlgorithms = algorithmSuite.getHashAlgorithms();
@ -46,19 +46,22 @@ public class KeySpecBuilder implements KeySpecBuilderInterface {
} }
@Override @Override
public KeySpecBuilder overridePreferredCompressionAlgorithms(@Nonnull CompressionAlgorithm... compressionAlgorithms) { public KeySpecBuilder overridePreferredCompressionAlgorithms(
@Nonnull CompressionAlgorithm... compressionAlgorithms) {
this.preferredCompressionAlgorithms = new LinkedHashSet<>(Arrays.asList(compressionAlgorithms)); this.preferredCompressionAlgorithms = new LinkedHashSet<>(Arrays.asList(compressionAlgorithms));
return this; return this;
} }
@Override @Override
public KeySpecBuilder overridePreferredHashAlgorithms(@Nonnull HashAlgorithm... preferredHashAlgorithms) { public KeySpecBuilder overridePreferredHashAlgorithms(
@Nonnull HashAlgorithm... preferredHashAlgorithms) {
this.preferredHashAlgorithms = new LinkedHashSet<>(Arrays.asList(preferredHashAlgorithms)); this.preferredHashAlgorithms = new LinkedHashSet<>(Arrays.asList(preferredHashAlgorithms));
return this; return this;
} }
@Override @Override
public KeySpecBuilder overridePreferredSymmetricKeyAlgorithms(@Nonnull SymmetricKeyAlgorithm... preferredSymmetricKeyAlgorithms) { public KeySpecBuilder overridePreferredSymmetricKeyAlgorithms(
@Nonnull SymmetricKeyAlgorithm... preferredSymmetricKeyAlgorithms) {
this.preferredSymmetricAlgorithms = new LinkedHashSet<>(Arrays.asList(preferredSymmetricKeyAlgorithms)); this.preferredSymmetricAlgorithms = new LinkedHashSet<>(Arrays.asList(preferredSymmetricKeyAlgorithms));
return this; return this;
} }
@ -66,39 +69,12 @@ public class KeySpecBuilder implements KeySpecBuilderInterface {
@Override @Override
public KeySpec build() { public KeySpec build() {
this.hashedSubPackets.setKeyFlags(false, KeyFlag.toBitmask(keyFlags)); this.hashedSubpackets.setKeyFlags(keyFlags);
this.hashedSubPackets.setPreferredCompressionAlgorithms(false, getPreferredCompressionAlgorithmIDs()); this.hashedSubpackets.setPreferredCompressionAlgorithms(preferredCompressionAlgorithms);
this.hashedSubPackets.setPreferredHashAlgorithms(false, getPreferredHashAlgorithmIDs()); this.hashedSubpackets.setPreferredHashAlgorithms(preferredHashAlgorithms);
this.hashedSubPackets.setPreferredSymmetricAlgorithms(false, getPreferredSymmetricKeyAlgorithmIDs()); this.hashedSubpackets.setPreferredSymmetricKeyAlgorithms(preferredSymmetricAlgorithms);
this.hashedSubPackets.setFeature(false, Feature.MODIFICATION_DETECTION.getFeatureId()); this.hashedSubpackets.setFeatures(Feature.MODIFICATION_DETECTION);
return new KeySpec(type, hashedSubPackets, false); return new KeySpec(type, (SignatureSubpackets) hashedSubpackets, false);
}
private int[] getPreferredCompressionAlgorithmIDs() {
int[] ids = new int[preferredCompressionAlgorithms.size()];
Iterator<CompressionAlgorithm> 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<HashAlgorithm> 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<SymmetricKeyAlgorithm> iterator = preferredSymmetricAlgorithms.iterator();
for (int i = 0; i < ids.length; i++) {
ids[i] = iterator.next().getAlgorithmId();
}
return ids;
} }
} }