diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyInfo.java b/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyInfo.java index adb7e1ae..7288a9d1 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyInfo.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyInfo.java @@ -19,6 +19,7 @@ import org.bouncycastle.bcpg.ECDHPublicBCPGKey; import org.bouncycastle.bcpg.ECDSAPublicBCPGKey; import org.bouncycastle.bcpg.ECPublicBCPGKey; import org.bouncycastle.bcpg.EdDSAPublicBCPGKey; +import org.bouncycastle.bcpg.S2K; import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil; import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPSecretKey; @@ -39,6 +40,22 @@ public class KeyInfo { this.secretKey = null; } + public String getCurveName() { return getCurveName(publicKey); } + + /** + * Returns indication that a contained secret key is encrypted. + * + * @return true if secret key is encrypted, false if secret key is not encrypted or there is public key only. + */ + public boolean isEncrypted() { return secretKey != null && isEncrypted(secretKey); } + + /** + * Returns indication that a contained secret key is not encrypted. + * + * @return true if secret key is not encrypted or there is public key only, false if secret key is encrypted. + */ + public boolean isDecrypted() { return secretKey == null || isDecrypted(secretKey); } + public static String getCurveName(PGPPublicKey publicKey) { PublicKeyAlgorithm algorithm = PublicKeyAlgorithm.fromId(publicKey.getAlgorithm()); ECPublicBCPGKey key; @@ -64,4 +81,24 @@ public class KeyInfo { public static String getCurveName(ECPublicBCPGKey key) { return ECUtil.getCurveName(key.getCurveOID()); } + + /** + * Returns indication that a secret key is encrypted. + * + * @param secretKey A secret key to examine. + * @return true if secret key is encrypted, false otherwise. + */ + public static boolean isEncrypted(PGPSecretKey secretKey) { + return secretKey.getS2KUsage() != 0 && secretKey.getS2K().getType() != S2K.GNU_DUMMY_S2K; + } + + /** + * Returns indication that a secret key is not encrypted. + * + * @param secretKey A secret key to examine. + * @return true if secret key is encrypted, false otherwise. + */ + public static boolean isDecrypted(PGPSecretKey secretKey) { + return secretKey.getS2KUsage() == 0 || secretKey.getS2K().getType() == S2K.GNU_DUMMY_S2K; + } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyRingInfo.java b/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyRingInfo.java index 09e4fcaa..c0ab9759 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyRingInfo.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyRingInfo.java @@ -306,7 +306,7 @@ public class KeyRingInfo { public boolean isFullyDecrypted() { if (isSecretKey()) { for (PGPSecretKey secretKey : getSecretKeys()) { - if (secretKey.getS2KUsage() != 0) { + if (KeyInfo.isEncrypted(secretKey)) { return false; } } @@ -324,7 +324,7 @@ public class KeyRingInfo { public boolean isFullyEncrypted() { if (isSecretKey()) { for (PGPSecretKey secretKey : getSecretKeys()) { - if (secretKey.getS2KUsage() == 0) { + if (KeyInfo.isDecrypted(secretKey)) { return false; } }