From 11e7bc69fc032556951377ff924c36acd0bbaaa4 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 16 Oct 2020 13:11:46 +0200 Subject: [PATCH] Fix NPE by initializing the SignatureGenerator --- .../org/pgpainless/key/generation/KeyRingBuilder.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java index 075c5474..e7d384ef 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java @@ -27,10 +27,13 @@ import java.util.Iterator; import java.util.List; import javax.annotation.Nonnull; +import org.bouncycastle.asn1.ocsp.Signature; +import org.bouncycastle.bcpg.SignatureSubpacket; import org.bouncycastle.openpgp.PGPEncryptedData; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPKeyPair; import org.bouncycastle.openpgp.PGPKeyRingGenerator; +import org.bouncycastle.openpgp.PGPPrivateKey; import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPSecretKey; @@ -42,6 +45,7 @@ import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor; import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder; import org.bouncycastle.openpgp.operator.PGPDigestCalculator; +import org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair; @@ -49,6 +53,7 @@ import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder; import org.pgpainless.algorithm.HashAlgorithm; import org.pgpainless.algorithm.KeyFlag; +import org.pgpainless.decryption_verification.OpenPgpMetadata; import org.pgpainless.key.collection.PGPKeyRing; import org.pgpainless.key.generation.type.ECDH; import org.pgpainless.key.generation.type.ECDSA; @@ -270,8 +275,9 @@ public class KeyRingBuilder implements KeyRingBuilderInterface { // Attempt to add additional user-ids to the primary public key PGPPublicKey primaryPubKey = secretKeys.next().getPublicKey(); + PGPPrivateKey privateKey = secretKeyRing.getSecretKey().extractPrivateKey(secretKeyDecryptor); for (String additionalUserId : additionalUserIds) { - // This fails :( + signatureGenerator.init(0x13, privateKey); PGPSignature additionalUserIdSignature = signatureGenerator.generateCertification(additionalUserId, primaryPubKey); primaryPubKey = PGPPublicKey.addCertification(primaryPubKey, @@ -280,7 +286,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface { // "reassemble" secret key ring with modified primary key PGPSecretKey primarySecKey = new PGPSecretKey( - secretKeyRing.getSecretKey().extractPrivateKey(secretKeyDecryptor), + privateKey, primaryPubKey, digestCalculator, true, secretKeyEncryptor); List secretKeyList = new ArrayList<>(); secretKeyList.add(primarySecKey);