diff --git a/pg/src/main/java/org/bouncycastle/bcpg/FingerprintUtil.java b/pg/src/main/java/org/bouncycastle/bcpg/FingerprintUtil.java deleted file mode 100644 index d2e49b9e93..0000000000 --- a/pg/src/main/java/org/bouncycastle/bcpg/FingerprintUtil.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.bouncycastle.bcpg; - -public class FingerprintUtil { - - /** - * Derive a 64 bit key-id from a version 6 OpenPGP fingerprint. - * For v6 keys, the key-id corresponds to the left-most 8 octets of the fingerprint. - * @param v6Fingerprint 32 byte fingerprint - * @return key-id - */ - public static long keyIdFromV6Fingerprint(byte[] v6Fingerprint) { - return longFromLeftMostBytes(v6Fingerprint); - } - - /** - * Derive a 64 bit key-id from a version 5 LibrePGP fingerprint. - * For such keys, the key-id corresponds to the left-most 8 octets of the fingerprint. - * @param v5Fingerprint 32 byte fingerprint - * @return key-id - */ - public static long keyIdFromLibrePgpFingerprint(byte[] v5Fingerprint) { - return longFromLeftMostBytes(v5Fingerprint); - } - - /** - * Derive a 64 bit key-id from a version 4 OpenPGP fingerprint. - * For v4 keys, the key-id corresponds to the right-most 8 octets of the fingerprint. - * @param v4Fingerprint 20 byte fingerprint - * @return key-id - */ - public static long keyIdFromV4Fingerprint(byte[] v4Fingerprint) { - return longFromRightMostBytes(v4Fingerprint); - } - - /** - * Convert the left-most 8 bytes from the given array to a long. - * @param bytes bytes - * @return long - */ - public static long longFromLeftMostBytes(byte[] bytes) { - if (bytes.length < 8) { - throw new IllegalArgumentException("Byte array MUST contain at least 8 bytes"); - } - return ((bytes[0] & 0xffL) << 56) | - ((bytes[1] & 0xffL) << 48) | - ((bytes[2] & 0xffL) << 40) | - ((bytes[3] & 0xffL) << 32) | - ((bytes[4] & 0xffL) << 24) | - ((bytes[5] & 0xffL) << 16) | - ((bytes[6] & 0xffL) << 8) | - ((bytes[7] & 0xffL)); - } - - /** - * Convert the right-most 8 bytes from the given array to a long. - * @param bytes bytes - * @return long - */ - public static long longFromRightMostBytes(byte[] bytes) { - if (bytes.length < 8) { - throw new IllegalArgumentException("Byte array MUST contain at least 8 bytes"); - } - int i = bytes.length; - return ((bytes[i - 8] & 0xffL) << 56) | - ((bytes[i - 7] & 0xffL) << 48) | - ((bytes[i - 6] & 0xffL) << 40) | - ((bytes[i - 5] & 0xffL) << 32) | - ((bytes[i - 4] & 0xffL) << 24) | - ((bytes[i - 3] & 0xffL) << 16) | - ((bytes[i - 2] & 0xffL) << 8) | - ((bytes[i - 1] & 0xffL)); - } -} diff --git a/pg/src/main/java/org/bouncycastle/bcpg/OnePassSignaturePacket.java b/pg/src/main/java/org/bouncycastle/bcpg/OnePassSignaturePacket.java index 29183e0e54..b71766bc3f 100644 --- a/pg/src/main/java/org/bouncycastle/bcpg/OnePassSignaturePacket.java +++ b/pg/src/main/java/org/bouncycastle/bcpg/OnePassSignaturePacket.java @@ -72,7 +72,15 @@ else if (version == VERSION_6) fingerprint = new byte[32]; in.readFully(fingerprint); - keyID = FingerprintUtil.keyIdFromV6Fingerprint(fingerprint); + // TODO: Replace with FingerprintUtil + keyID = ((fingerprint[0] & 0xffL) << 56) | + ((fingerprint[1] & 0xffL) << 48) | + ((fingerprint[2] & 0xffL) << 40) | + ((fingerprint[3] & 0xffL) << 32) | + ((fingerprint[4] & 0xffL) << 24) | + ((fingerprint[5] & 0xffL) << 16) | + ((fingerprint[6] & 0xffL) << 8) | + ((fingerprint[7] & 0xffL)); } else { @@ -145,7 +153,15 @@ public OnePassSignaturePacket( this.salt = salt; this.fingerprint = fingerprint; this.isContaining = (isNested) ? 0 : 1; - this.keyID = FingerprintUtil.keyIdFromV6Fingerprint(fingerprint); + // TODO: Replace with FingerprintUtil + keyID = ((fingerprint[0] & 0xffL) << 56) | + ((fingerprint[1] & 0xffL) << 48) | + ((fingerprint[2] & 0xffL) << 40) | + ((fingerprint[3] & 0xffL) << 32) | + ((fingerprint[4] & 0xffL) << 24) | + ((fingerprint[5] & 0xffL) << 16) | + ((fingerprint[6] & 0xffL) << 8) | + ((fingerprint[7] & 0xffL)); } /** diff --git a/pg/src/test/java/org/bouncycastle/bcpg/test/FingerprintUtilTest.java b/pg/src/test/java/org/bouncycastle/bcpg/test/FingerprintUtilTest.java deleted file mode 100644 index 48eb0dee5c..0000000000 --- a/pg/src/test/java/org/bouncycastle/bcpg/test/FingerprintUtilTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.bouncycastle.bcpg.test; - -import org.bouncycastle.bcpg.FingerprintUtil; -import org.bouncycastle.util.encoders.Hex; -import org.bouncycastle.util.test.SimpleTest; - -public class FingerprintUtilTest extends SimpleTest { - - private void testKeyIdFromTooShortFails() { - byte[] decoded = new byte[1]; - try { - FingerprintUtil.keyIdFromV4Fingerprint(decoded); - fail("Expected exception"); - } catch (IllegalArgumentException e) { - // expected - } - } - - private void testV4KeyIdFromFingerprint() { - String fingerprint = "1D018C772DF8C5EF86A1DCC9B4B509CB5936E03E"; - byte[] decoded = Hex.decode(fingerprint); - isEquals("v4 key-id from fingerprint mismatch", - -5425419407118114754L, FingerprintUtil.keyIdFromV4Fingerprint(decoded)); - } - - private void testV6KeyIdFromFingerprint() { - String fingerprint = "cb186c4f0609a697e4d52dfa6c722b0c1f1e27c18a56708f6525ec27bad9acc9"; - byte[] decoded = Hex.decode(fingerprint); - isEquals("v6 key-id from fingerprint mismatch", - -3812177997909612905L, FingerprintUtil.keyIdFromV6Fingerprint(decoded)); - } - - private void testLibrePgpKeyIdFromFingerprint() { - // v6 key-ids are derived from fingerprints the same way as LibrePGP does - String fingerprint = "cb186c4f0609a697e4d52dfa6c722b0c1f1e27c18a56708f6525ec27bad9acc9"; - byte[] decoded = Hex.decode(fingerprint); - isEquals("LibrePGP key-id from fingerprint mismatch", - -3812177997909612905L, FingerprintUtil.keyIdFromLibrePgpFingerprint(decoded)); - } - - @Override - public String getName() { - return "FingerprintUtilTest"; - } - - @Override - public void performTest() throws Exception { - testV4KeyIdFromFingerprint(); - testV6KeyIdFromFingerprint(); - testKeyIdFromTooShortFails(); - testLibrePgpKeyIdFromFingerprint(); - } - - public static void main(String[] args) { - runTest(new FingerprintUtilTest()); - } -} diff --git a/pg/src/test/java/org/bouncycastle/bcpg/test/OnePassSignaturePacketTest.java b/pg/src/test/java/org/bouncycastle/bcpg/test/OnePassSignaturePacketTest.java index c89c16c406..205624a679 100644 --- a/pg/src/test/java/org/bouncycastle/bcpg/test/OnePassSignaturePacketTest.java +++ b/pg/src/test/java/org/bouncycastle/bcpg/test/OnePassSignaturePacketTest.java @@ -108,6 +108,14 @@ private void roundtripV3Packet() throws IOException { private void roundtripV6Packet() throws IOException { byte[] salt = new byte[HashUtils.getV6SignatureSaltSizeInBytes(HashAlgorithmTags.SHA512)]; byte[] fingerprint = Hex.decode("CB186C4F0609A697E4D52DFA6C722B0C1F1E27C18A56708F6525EC27BAD9ACC9"); + long keyID = ((fingerprint[0] & 0xffL) << 56) | + ((fingerprint[1] & 0xffL) << 48) | + ((fingerprint[2] & 0xffL) << 40) | + ((fingerprint[3] & 0xffL) << 32) | + ((fingerprint[4] & 0xffL) << 24) | + ((fingerprint[5] & 0xffL) << 16) | + ((fingerprint[6] & 0xffL) << 8) | + ((fingerprint[7] & 0xffL)); new SecureRandom().nextBytes(salt); OnePassSignaturePacket before = new OnePassSignaturePacket( @@ -131,7 +139,7 @@ private void roundtripV6Packet() throws IOException { isEncodingEqual("Fingerprint mismatch", fingerprint, before.getFingerprint()); isEquals("Derived key-ID mismatch", - FingerprintUtil.keyIdFromV6Fingerprint(fingerprint), before.getKeyID()); + keyID, before.getKeyID()); isTrue("non-nested OPS is expected to be containing", before.isContaining());