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

Convert KeyRingBuilder fields to local variables

This commit is contained in:
Paul Schaub 2021-11-11 13:07:49 +01:00
parent 3d5a005ec7
commit a6181218a2

View file

@ -53,10 +53,6 @@ public class KeyRingBuilder implements KeyRingBuilderInterface<KeyRingBuilder> {
private final Charset UTF8 = Charset.forName("UTF-8"); private final Charset UTF8 = Charset.forName("UTF-8");
private PGPSignatureGenerator signatureGenerator;
private PGPDigestCalculator keyFingerprintCalculator;
private PBESecretKeyEncryptor secretKeyEncryptor;
private KeySpec primaryKeySpec; private KeySpec primaryKeySpec;
private final List<KeySpec> subkeySpecs = new ArrayList<>(); private final List<KeySpec> subkeySpecs = new ArrayList<>();
private final Set<String> userIds = new LinkedHashSet<>(); private final Set<String> userIds = new LinkedHashSet<>();
@ -127,8 +123,8 @@ public class KeyRingBuilder implements KeyRingBuilderInterface<KeyRingBuilder> {
if (userIds.isEmpty()) { if (userIds.isEmpty()) {
throw new IllegalStateException("At least one user-id is required."); throw new IllegalStateException("At least one user-id is required.");
} }
keyFingerprintCalculator = ImplementationFactory.getInstance().getV4FingerprintCalculator(); PGPDigestCalculator keyFingerprintCalculator = ImplementationFactory.getInstance().getV4FingerprintCalculator();
secretKeyEncryptor = buildSecretKeyEncryptor(); PBESecretKeyEncryptor secretKeyEncryptor = buildSecretKeyEncryptor(keyFingerprintCalculator);
PBESecretKeyDecryptor secretKeyDecryptor = buildSecretKeyDecryptor(); PBESecretKeyDecryptor secretKeyDecryptor = buildSecretKeyDecryptor();
passphrase.clear(); passphrase.clear();
@ -136,7 +132,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface<KeyRingBuilder> {
// Generate Primary Key // Generate Primary Key
PGPKeyPair certKey = generateKeyPair(primaryKeySpec); PGPKeyPair certKey = generateKeyPair(primaryKeySpec);
PGPContentSignerBuilder signer = buildContentSigner(certKey); PGPContentSignerBuilder signer = buildContentSigner(certKey);
signatureGenerator = new PGPSignatureGenerator(signer); PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(signer);
SignatureSubpackets hashedSubPacketGenerator = primaryKeySpec.getSubpacketGenerator(); SignatureSubpackets hashedSubPacketGenerator = primaryKeySpec.getSubpacketGenerator();
hashedSubPacketGenerator.setPrimaryUserId(); hashedSubPacketGenerator.setPrimaryUserId();
if (expirationDate != null) { if (expirationDate != null) {
@ -147,7 +143,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface<KeyRingBuilder> {
PGPSignatureSubpacketVector hashedSubPackets = generator.generate(); PGPSignatureSubpacketVector hashedSubPackets = generator.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, keyFingerprintCalculator, hashedSubPackets, secretKeyEncryptor);
addSubKeys(certKey, ringGenerator); addSubKeys(certKey, ringGenerator);
@ -185,7 +181,9 @@ public class KeyRingBuilder implements KeyRingBuilderInterface<KeyRingBuilder> {
private PGPKeyRingGenerator buildRingGenerator(PGPKeyPair certKey, private PGPKeyRingGenerator buildRingGenerator(PGPKeyPair certKey,
PGPContentSignerBuilder signer, PGPContentSignerBuilder signer,
PGPSignatureSubpacketVector hashedSubPackets) PGPDigestCalculator keyFingerprintCalculator,
PGPSignatureSubpacketVector hashedSubPackets,
PBESecretKeyEncryptor secretKeyEncryptor)
throws PGPException { throws PGPException {
String primaryUserId = userIds.iterator().next(); String primaryUserId = userIds.iterator().next();
return new PGPKeyRingGenerator( return new PGPKeyRingGenerator(
@ -212,7 +210,8 @@ public class KeyRingBuilder implements KeyRingBuilderInterface<KeyRingBuilder> {
} }
} }
private PGPSignatureSubpacketVector addPrimaryKeyBindingSignatureIfNecessary(PGPKeyPair primaryKey, PGPKeyPair subKey, PGPSignatureSubpacketVector hashedSubpackets) throws PGPException, IOException { private PGPSignatureSubpacketVector addPrimaryKeyBindingSignatureIfNecessary(PGPKeyPair primaryKey, PGPKeyPair subKey, PGPSignatureSubpacketVector hashedSubpackets)
throws PGPException, IOException {
int keyFlagMask = hashedSubpackets.getKeyFlags(); int keyFlagMask = hashedSubpackets.getKeyFlags();
if (!KeyFlag.hasKeyFlag(keyFlagMask, KeyFlag.SIGN_DATA) && !KeyFlag.hasKeyFlag(keyFlagMask, KeyFlag.CERTIFY_OTHER)) { if (!KeyFlag.hasKeyFlag(keyFlagMask, KeyFlag.SIGN_DATA) && !KeyFlag.hasKeyFlag(keyFlagMask, KeyFlag.CERTIFY_OTHER)) {
return hashedSubpackets; return hashedSubpackets;
@ -233,7 +232,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface<KeyRingBuilder> {
hashAlgorithm.getAlgorithmId()); hashAlgorithm.getAlgorithmId());
} }
private PBESecretKeyEncryptor buildSecretKeyEncryptor() { private PBESecretKeyEncryptor buildSecretKeyEncryptor(PGPDigestCalculator keyFingerprintCalculator) {
SymmetricKeyAlgorithm keyEncryptionAlgorithm = PGPainless.getPolicy().getSymmetricKeyEncryptionAlgorithmPolicy() SymmetricKeyAlgorithm keyEncryptionAlgorithm = PGPainless.getPolicy().getSymmetricKeyEncryptionAlgorithmPolicy()
.getDefaultSymmetricKeyAlgorithm(); .getDefaultSymmetricKeyAlgorithm();
if (!passphrase.isValid()) { if (!passphrase.isValid()) {
@ -268,29 +267,30 @@ public class KeyRingBuilder implements KeyRingBuilderInterface<KeyRingBuilder> {
return pgpKeyPair; return pgpKeyPair;
} }
private static SecretKeyRingProtector protectorFromPassphrase(@Nonnull Passphrase passphrase, long keyId) {
if (!passphrase.isValid()) {
throw new IllegalStateException("Passphrase has been cleared.");
}
if (passphrase.isEmpty()) {
return SecretKeyRingProtector.unprotectedKeys();
} else {
return SecretKeyRingProtector.unlockSingleKeyWith(passphrase, keyId);
}
}
public static PGPSecretKey generatePGPSecretKey(KeySpec keySpec, @Nonnull Passphrase passphrase, boolean isPrimary) public static PGPSecretKey generatePGPSecretKey(KeySpec keySpec, @Nonnull Passphrase passphrase, boolean isPrimary)
throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException { throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
PGPDigestCalculator keyFingerprintCalculator = ImplementationFactory.getInstance() PGPDigestCalculator keyFingerprintCalculator = ImplementationFactory.getInstance()
.getV4FingerprintCalculator(); .getV4FingerprintCalculator();
PGPKeyPair keyPair = generateKeyPair(keySpec); PGPKeyPair keyPair = generateKeyPair(keySpec);
SecretKeyRingProtector protector;
synchronized (passphrase.lock) { SecretKeyRingProtector protector = protectorFromPassphrase(passphrase, keyPair.getKeyID());
if (!passphrase.isValid()) {
throw new IllegalStateException("Passphrase has been cleared.");
}
if (!passphrase.isEmpty()) {
protector = SecretKeyRingProtector.unlockSingleKeyWith(passphrase, keyPair.getKeyID());
} else {
protector = SecretKeyRingProtector.unprotectedKeys();
}
return new PGPSecretKey( return new PGPSecretKey(
keyPair.getPrivateKey(), keyPair.getPrivateKey(),
keyPair.getPublicKey(), keyPair.getPublicKey(),
keyFingerprintCalculator, keyFingerprintCalculator,
isPrimary, isPrimary,
protector.getEncryptor(keyPair.getKeyID())); protector.getEncryptor(keyPair.getKeyID()));
}
} }
} }