From d2202dcb0f4afbfa4b90f502e42d9226cefb7110 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 15 Feb 2021 23:37:03 +0100 Subject: [PATCH] Retry key generation on invalid private key encoding length Workaround for #70 --- .../pgpainless/key/generation/KeyRingBuilder.java | 15 +++++++++++++-- 1 file changed, 13 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 7822fc45..601ee995 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,6 +27,8 @@ import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.annotation.Nonnull; import org.bouncycastle.openpgp.PGPException; @@ -60,6 +62,8 @@ import org.pgpainless.util.SignatureSubpacketGeneratorUtil; public class KeyRingBuilder implements KeyRingBuilderInterface { + private static final Logger LOGGER = Logger.getLogger(KeyRingBuilder.class.getName()); + private final Charset UTF8 = Charset.forName("UTF-8"); private final List keySpecs = new ArrayList<>(); @@ -454,8 +458,15 @@ public class KeyRingBuilder implements KeyRingBuilderInterface { KeyPair keyPair = certKeyGenerator.generateKeyPair(); // Form PGP key pair - PGPKeyPair pgpKeyPair = ImplementationFactory.getInstance().getPGPKeyPair(type.getAlgorithm(), keyPair, new Date()); - + PGPKeyPair pgpKeyPair; + try { + pgpKeyPair = ImplementationFactory.getInstance().getPGPKeyPair(type.getAlgorithm(), keyPair, new Date()); + } catch (PGPException e) { + // When generating EdDSA keys, the private key has an encoding length of 33 instead of 34, which results + // in an exception. Therefore we just try again as a workaround. + LOGGER.log(Level.INFO, "Private key has wrong length. Try again.", e); + pgpKeyPair = generateKeyPair(spec); + } return pgpKeyPair; } }