From dff47d17d1d522740f793761937a52c6b6e7458b Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 28 Jun 2021 21:14:40 +0200 Subject: [PATCH] Remove more unused methods and fix method name --- .../signature/SignatureValidator.java | 2 +- .../main/java/org/pgpainless/util/BCUtil.java | 26 +++++++------------ .../BrainpoolKeyGenerationTest.java | 8 +++--- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/signature/SignatureValidator.java b/pgpainless-core/src/main/java/org/pgpainless/signature/SignatureValidator.java index ff6237f2..2b64b368 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/signature/SignatureValidator.java +++ b/pgpainless-core/src/main/java/org/pgpainless/signature/SignatureValidator.java @@ -276,7 +276,7 @@ public abstract class SignatureValidator { @Override public void verify(PGPSignature signature) throws SignatureValidationException { PublicKeyAlgorithm algorithm = PublicKeyAlgorithm.fromId(signingKey.getAlgorithm()); - int bitStrength = BCUtil.getBitStrenght(signingKey); + int bitStrength = BCUtil.getBitStrength(signingKey); if (!policy.getPublicKeyAlgorithmPolicy().isAcceptable(algorithm, bitStrength)) { throw new SignatureValidationException("Signature was made using unacceptable key. " + algorithm + " (" + bitStrength + " bits) is not acceptable according to the public key algorithm policy."); diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/BCUtil.java b/pgpainless-core/src/main/java/org/pgpainless/util/BCUtil.java index 72b24385..b43236ff 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/BCUtil.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/BCUtil.java @@ -15,29 +15,21 @@ */ package org.pgpainless.util; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import javax.annotation.Nonnull; - import org.bouncycastle.asn1.ASN1ObjectIdentifier; import org.bouncycastle.bcpg.ECPublicBCPGKey; import org.bouncycastle.openpgp.PGPPublicKey; -import org.bouncycastle.openpgp.PGPUtil; public class BCUtil { - public static InputStream getPgpDecoderInputStream(@Nonnull byte[] bytes) - throws IOException { - return getPgpDecoderInputStream(new ByteArrayInputStream(bytes)); - } - - public static InputStream getPgpDecoderInputStream(@Nonnull InputStream inputStream) - throws IOException { - return PGPUtil.getDecoderStream(inputStream); - } - - public static int getBitStrenght(PGPPublicKey key) { + /** + * Utility method to get the bit strength of OpenPGP keys. + * Bouncycastle is lacking support for some keys (eg. EdDSA, X25519), so this method + * manually derives the bit strength from the keys curves OID. + * + * @param key key + * @return bit strength + */ + public static int getBitStrength(PGPPublicKey key) { int bitStrength = key.getBitStrength(); if (bitStrength == -1) { diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/generation/BrainpoolKeyGenerationTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/generation/BrainpoolKeyGenerationTest.java index be574c1f..fcfae805 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/generation/BrainpoolKeyGenerationTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/generation/BrainpoolKeyGenerationTest.java @@ -116,22 +116,22 @@ public class BrainpoolKeyGenerationTest { PGPSecretKey ecdsaPrim = iterator.next(); KeyInfo ecdsaInfo = new KeyInfo(ecdsaPrim); assertEquals(EllipticCurve._BRAINPOOLP384R1.getName(), ecdsaInfo.getCurveName()); - assertEquals(384, BCUtil.getBitStrenght(ecdsaPrim.getPublicKey())); + assertEquals(384, BCUtil.getBitStrength(ecdsaPrim.getPublicKey())); PGPSecretKey eddsaSub = iterator.next(); KeyInfo eddsaInfo = new KeyInfo(eddsaSub); assertEquals(EdDSACurve._Ed25519.getName(), eddsaInfo.getCurveName()); - assertEquals(256, BCUtil.getBitStrenght(eddsaSub.getPublicKey())); + assertEquals(256, BCUtil.getBitStrength(eddsaSub.getPublicKey())); PGPSecretKey xdhSub = iterator.next(); KeyInfo xdhInfo = new KeyInfo(xdhSub); assertEquals(XDHSpec._X25519.getCurveName(), xdhInfo.getCurveName()); - assertEquals(256, BCUtil.getBitStrenght(xdhSub.getPublicKey())); + assertEquals(256, BCUtil.getBitStrength(xdhSub.getPublicKey())); PGPSecretKey rsaSub = iterator.next(); KeyInfo rsaInfo = new KeyInfo(rsaSub); assertThrows(IllegalArgumentException.class, rsaInfo::getCurveName, "RSA is not a curve-based encryption system"); - assertEquals(3072, BCUtil.getBitStrenght(rsaSub.getPublicKey())); + assertEquals(3072, BCUtil.getBitStrength(rsaSub.getPublicKey())); } public PGPSecretKeyRing generateKey(KeySpec primaryKey, KeySpec subKey, String userId) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {