1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-27 06:42:05 +01:00

Expose keySpec.getSubpacketGenerator() and move setPrimaryUserId to builder

This commit is contained in:
Paul Schaub 2021-01-29 14:51:24 +01:00
parent 4e7c1c023c
commit 4ddbca4908
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 32 additions and 5 deletions

View file

@ -38,6 +38,7 @@ import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureGenerator; import org.bouncycastle.openpgp.PGPSignatureGenerator;
import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
import org.bouncycastle.openpgp.PGPSignatureSubpacketVector; import org.bouncycastle.openpgp.PGPSignatureSubpacketVector;
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor; import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
@ -54,6 +55,7 @@ import org.pgpainless.key.generation.type.rsa.RsaLength;
import org.pgpainless.key.util.UserId; import org.pgpainless.key.util.UserId;
import org.pgpainless.provider.ProviderFactory; import org.pgpainless.provider.ProviderFactory;
import org.pgpainless.util.Passphrase; import org.pgpainless.util.Passphrase;
import org.pgpainless.util.SignatureSubpacketGeneratorUtil;
public class KeyRingBuilder implements KeyRingBuilderInterface { public class KeyRingBuilder implements KeyRingBuilderInterface {
@ -192,8 +194,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
} }
private boolean hasCertifyOthersFlag(KeySpec keySpec) { private boolean hasCertifyOthersFlag(KeySpec keySpec) {
int flags = keySpec.getSubpackets().getKeyFlags(); return SignatureSubpacketGeneratorUtil.hasKeyFlag(KeyFlag.CERTIFY_OTHER, keySpec.getSubpacketGenerator());
return KeyFlag.hasKeyFlag(flags, KeyFlag.CERTIFY_OTHER);
} }
private boolean keyIsCertificationCapable(KeySpec keySpec) { private boolean keyIsCertificationCapable(KeySpec keySpec) {
@ -267,7 +268,9 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
PGPKeyPair certKey = generateKeyPair(certKeySpec); PGPKeyPair certKey = generateKeyPair(certKeySpec);
PGPContentSignerBuilder signer = buildContentSigner(certKey); PGPContentSignerBuilder signer = buildContentSigner(certKey);
signatureGenerator = new PGPSignatureGenerator(signer); signatureGenerator = new PGPSignatureGenerator(signer);
PGPSignatureSubpacketVector hashedSubPackets = certKeySpec.getSubpackets(); PGPSignatureSubpacketGenerator hashedSubPacketGenerator = certKeySpec.getSubpacketGenerator();
hashedSubPacketGenerator.setPrimaryUserID(false, true);
PGPSignatureSubpacketVector hashedSubPackets = hashedSubPacketGenerator.generate();
// Generator which the user can get the key pair from // Generator which the user can get the key pair from
PGPKeyRingGenerator ringGenerator = buildRingGenerator(certKey, signer, hashedSubPackets); PGPKeyRingGenerator ringGenerator = buildRingGenerator(certKey, signer, hashedSubPackets);

View file

@ -46,6 +46,10 @@ public class KeySpec {
return subpacketGenerator != null ? subpacketGenerator.generate() : null; return subpacketGenerator != null ? subpacketGenerator.generate() : null;
} }
PGPSignatureSubpacketGenerator getSubpacketGenerator() {
return subpacketGenerator;
}
boolean isInheritedSubPackets() { boolean isInheritedSubPackets() {
return inheritedSubPackets; return inheritedSubPackets;
} }

View file

@ -34,7 +34,6 @@ public class KeySpecBuilder implements KeySpecBuilderInterface {
KeySpecBuilder(@Nonnull KeyType type) { KeySpecBuilder(@Nonnull KeyType type) {
this.type = type; this.type = type;
hashedSubPackets.setPrimaryUserID(false, true);
} }
@Override @Override

View file

@ -15,19 +15,32 @@
*/ */
package org.pgpainless.util; package org.pgpainless.util;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import org.bouncycastle.bcpg.SignatureSubpacket; import org.bouncycastle.bcpg.SignatureSubpacket;
import org.bouncycastle.bcpg.SignatureSubpacketTags; import org.bouncycastle.bcpg.SignatureSubpacketTags;
import org.bouncycastle.bcpg.sig.KeyFlags;
import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator; import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
import org.pgpainless.algorithm.KeyFlag;
/** /**
* Utility class that helps dealing with BCs SignatureSubpacketGenerator class. * Utility class that helps dealing with BCs SignatureSubpacketGenerator class.
*/ */
public class SignatureSubpacketGeneratorUtil { public class SignatureSubpacketGeneratorUtil {
public static <P extends SignatureSubpacket> List<P> getSubpacketsOfType(org.pgpainless.algorithm.SignatureSubpacket type,
PGPSignatureSubpacketGenerator generator) {
SignatureSubpacket[] subpackets = generator.getSubpackets(type.getCode());
List<P> list = new ArrayList<>();
for (SignatureSubpacket p : subpackets) {
list.add((P) p);
}
return list;
}
public static void removeAllPacketsOfType(org.pgpainless.algorithm.SignatureSubpacket subpacketType, public static void removeAllPacketsOfType(org.pgpainless.algorithm.SignatureSubpacket subpacketType,
PGPSignatureSubpacketGenerator subpacketGenerator) { PGPSignatureSubpacketGenerator subpacketGenerator) {
removeAllPacketsOfType(subpacketType.getCode(), subpacketGenerator); removeAllPacketsOfType(subpacketType.getCode(), subpacketGenerator);
@ -84,4 +97,12 @@ public class SignatureSubpacketGeneratorUtil {
} }
return secondsToExpire; return secondsToExpire;
} }
public static boolean hasKeyFlag(KeyFlag keyFlag, PGPSignatureSubpacketGenerator generator) {
List<KeyFlags> keyFlagPackets = getSubpacketsOfType(org.pgpainless.algorithm.SignatureSubpacket.keyFlags, generator);
if (keyFlagPackets.isEmpty()) {
return false;
}
return KeyFlag.hasKeyFlag(keyFlagPackets.get(0).getFlags(), keyFlag);
}
} }