Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibrePGP V5 fingerprint calculation #1703

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public byte[] calculateFingerprint(PublicKeyPacket publicPk)
BCPGKey key = publicPk.getKey();
Digest digest;

if (publicPk.getVersion() <= 3)
if (publicPk.getVersion() <= PublicKeyPacket.VERSION_3)
{
RSAPublicBCPGKey rK = (RSAPublicBCPGKey)key;

Expand All @@ -42,7 +42,7 @@ public byte[] calculateFingerprint(PublicKeyPacket publicPk)
throw new PGPException("can't encode key components: " + e.getMessage(), e);
}
}
else if (publicPk.getVersion() == 4)
else if (publicPk.getVersion() == PublicKeyPacket.VERSION_4)
{
try
{
Expand All @@ -60,14 +60,14 @@ else if (publicPk.getVersion() == 4)
throw new PGPException("can't encode key components: " + e.getMessage(), e);
}
}
else if (publicPk.getVersion() == 6)
else if (publicPk.getVersion() == 5 || publicPk.getVersion() == PublicKeyPacket.VERSION_6)
{
try
{
byte[] kBytes = publicPk.getEncodedContents();
digest = new SHA256Digest();

digest.update((byte)0x9b);
digest.update((byte) (publicPk.getVersion() == PublicKeyPacket.VERSION_6 ? 0x9b : 0x9a));

digest.update((byte)(kBytes.length >> 24));
digest.update((byte)(kBytes.length >> 16));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public byte[] calculateFingerprint(PublicKeyPacket publicPk)
{
BCPGKey key = publicPk.getKey();

if (publicPk.getVersion() <= 3)
if (publicPk.getVersion() <= PublicKeyPacket.VERSION_3)
{
RSAPublicBCPGKey rK = (RSAPublicBCPGKey)key;

Expand Down Expand Up @@ -92,7 +92,7 @@ public byte[] calculateFingerprint(PublicKeyPacket publicPk)
throw new PGPException("can't encode key components: " + e.getMessage(), e);
}
}
else if (publicPk.getVersion() == 4)
else if (publicPk.getVersion() == PublicKeyPacket.VERSION_4)
{
try
{
Expand Down Expand Up @@ -120,15 +120,14 @@ else if (publicPk.getVersion() == 4)
throw new PGPException("can't encode key components: " + e.getMessage(), e);
}
}
else if (publicPk.getVersion() == 6)
else if (publicPk.getVersion() == 5 || publicPk.getVersion() == PublicKeyPacket.VERSION_6)
{
try
{
byte[] kBytes = publicPk.getEncodedContents();

MessageDigest digest = helper.createMessageDigest("SHA-256");

digest.update((byte)0x9b);
digest.update((byte) (publicPk.getVersion() == PublicKeyPacket.VERSION_6 ? 0x9b : 0x9a));

digest.update((byte)(kBytes.length >> 24));
digest.update((byte)(kBytes.length >> 16));
Expand Down
35 changes: 22 additions & 13 deletions pg/src/test/java/org/bouncycastle/openpgp/test/OperatorBcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,13 @@ public void testBcKeyFingerprintCalculator()
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
kpGen.initialize(1024);
KeyPair kp = kpGen.generateKeyPair();
Date creationTime = new Date(1000 * (new Date().getTime() / 1000));

JcaPGPKeyConverter converter = new JcaPGPKeyConverter().setProvider(new BouncyCastleProvider());
final PGPPublicKey pubKey = converter.getPGPPublicKey(PublicKeyAlgorithmTags.RSA_GENERAL, kp.getPublic(), new Date());
final PGPPublicKey pubKey = converter.getPGPPublicKey(PublicKeyAlgorithmTags.RSA_GENERAL, kp.getPublic(), creationTime);

PublicKeyPacket pubKeyPacket = new PublicKeyPacket(6, PublicKeyAlgorithmTags.RSA_GENERAL, new Date(), pubKey.getPublicKeyPacket().getKey());
byte[] output = calculator.calculateFingerprint(new PublicKeyPacket(6, PublicKeyAlgorithmTags.RSA_GENERAL, new Date(), pubKey.getPublicKeyPacket().getKey()));
PublicKeyPacket pubKeyPacket = new PublicKeyPacket(6, PublicKeyAlgorithmTags.RSA_GENERAL, creationTime, pubKey.getPublicKeyPacket().getKey());
byte[] output = calculator.calculateFingerprint(new PublicKeyPacket(6, PublicKeyAlgorithmTags.RSA_GENERAL, creationTime, pubKey.getPublicKeyPacket().getKey()));
byte[] kBytes = pubKeyPacket.getEncodedContents();
SHA256Digest digest = new SHA256Digest();

Expand All @@ -167,16 +168,24 @@ public void testBcKeyFingerprintCalculator()
digest.doFinal(digBuf, 0);
isTrue(areEqual(output, digBuf));

final PublicKeyPacket pubKeyPacket2 = new PublicKeyPacket(5, PublicKeyAlgorithmTags.RSA_GENERAL, new Date(), pubKey.getPublicKeyPacket().getKey());
testException("Unsupported PGP key version: ", "UnsupportedPacketVersionException", new TestExceptionOperation()
{
@Override
public void operation()
throws Exception
{
calculator.calculateFingerprint(pubKeyPacket2);
}
});
final PublicKeyPacket pubKeyPacket2 = new PublicKeyPacket(5, PublicKeyAlgorithmTags.RSA_GENERAL, creationTime, pubKey.getPublicKeyPacket().getKey());
kBytes = pubKeyPacket2.getEncodedContents();
output = calculator.calculateFingerprint(pubKeyPacket2);

digest = new SHA256Digest();

digest.update((byte)0x9a);

digest.update((byte)(kBytes.length >> 24));
digest.update((byte)(kBytes.length >> 16));
digest.update((byte)(kBytes.length >> 8));
digest.update((byte)kBytes.length);

digest.update(kBytes, 0, kBytes.length);
digBuf = new byte[digest.getDigestSize()];

digest.doFinal(digBuf, 0);
isTrue(areEqual(output, digBuf));
}

// public void testBcPBESecretKeyDecryptorBuilder()
Expand Down