Skip to content

Commit 0237f75

Browse files
committed
Prevent ClassCastException for v3 OpenPGP keys with non-RSA public keys
v3 and earlier OpenPGP keys can only use RSA as key algorithm. This commit changes the behavior of BC to throw a PGPException instead of a ClassCastException when encountering a malformed key.
1 parent fe9de3e commit 0237f75

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

pg/src/main/java/org/bouncycastle/openpgp/operator/bc/BcKeyFingerprintCalculator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public byte[] calculateFingerprint(PublicKeyPacket publicPk)
2525

2626
if (publicPk.getVersion() <= PublicKeyPacket.VERSION_3)
2727
{
28+
if (!(key instanceof RSAPublicBCPGKey))
29+
{
30+
throw new PGPException("Version 3 OpenPGP keys can only use RSA. Found " + key.getClass().getName());
31+
}
2832
RSAPublicBCPGKey rK = (RSAPublicBCPGKey)key;
2933

3034
try

pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcaKeyFingerprintCalculator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public byte[] calculateFingerprint(PublicKeyPacket publicPk)
6666

6767
if (publicPk.getVersion() <= PublicKeyPacket.VERSION_3)
6868
{
69+
if (!(key instanceof RSAPublicBCPGKey))
70+
{
71+
throw new PGPException("Version 3 OpenPGP keys can only use RSA. Found " + key.getClass().getName());
72+
}
6973
RSAPublicBCPGKey rK = (RSAPublicBCPGKey)key;
7074

7175
try

0 commit comments

Comments
 (0)