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

Use FingerprintUtil for deriving key-ID from fingerprint #1645

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 50 additions & 21 deletions pg/src/main/java/org/bouncycastle/bcpg/FingerprintUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.bouncycastle.bcpg;

import org.bouncycastle.util.Pack;

public class FingerprintUtil
{

Expand Down Expand Up @@ -47,18 +49,7 @@ public static long keyIdFromV4Fingerprint(byte[] v4Fingerprint)
*/
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));
return readKeyID(bytes);
}

/**
Expand All @@ -68,19 +59,57 @@ public static long longFromLeftMostBytes(byte[] bytes)
* @return long
*/
public static long longFromRightMostBytes(byte[] bytes)
{
return readKeyID(bytes, bytes.length - 8);
}

/**
* Read a key-ID from the first 8 octets of the given byte array.
* @param bytes byte array
* @return key-ID
*/
public static long readKeyID(byte[] bytes)
{
return readKeyID(bytes, 0);
}

/**
* Read a key-ID from 8 octets of the given byte array starting at offset.
* @param bytes byte array
* @param offset offset
* @return key-ID
*/
public static long readKeyID(byte[] bytes, int offset)
{
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));
return Pack.bigEndianToLong(bytes, offset);
}

/**
* Write the key-ID encoded as 8 octets to the given byte array, starting at index offset.
* @param keyID keyID
* @param bytes byte array
* @param offset starting offset
*/
public static void writeKeyID(long keyID, byte[] bytes, int offset)
{
if (bytes.length - offset < 8)
{
throw new IllegalArgumentException("Not enough space to write key-ID to byte array.");
}
Pack.longToBigEndian(keyID, bytes, offset);
}

/**
* Write the key-ID to the first 8 octets of the given byte array.
* @param keyID keyID
* @param bytes byte array
*/
public static void writeKeyID(long keyID, byte[] bytes)
{
writeKeyID(keyID, bytes, 0);
}
}
20 changes: 2 additions & 18 deletions pg/src/main/java/org/bouncycastle/bcpg/OnePassSignaturePacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,7 @@ else if (version == VERSION_6)
fingerprint = new byte[32];
in.readFully(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));
keyID = FingerprintUtil.keyIdFromV6Fingerprint(fingerprint);
}
else
{
Expand Down Expand Up @@ -154,15 +146,7 @@ public OnePassSignaturePacket(
this.salt = salt;
this.fingerprint = fingerprint;
this.isContaining = (isNested) ? 0 : 1;
// 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));
keyID = FingerprintUtil.keyIdFromV6Fingerprint(fingerprint);
}

/**
Expand Down
17 changes: 3 additions & 14 deletions pg/src/main/java/org/bouncycastle/bcpg/sig/IssuerKeyID.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bouncycastle.bcpg.sig;

import org.bouncycastle.bcpg.FingerprintUtil;
import org.bouncycastle.bcpg.SignatureSubpacket;
import org.bouncycastle.bcpg.SignatureSubpacketTags;

Expand All @@ -13,16 +14,7 @@ protected static byte[] keyIDToBytes(
long keyId)
{
byte[] data = new byte[8];

data[0] = (byte)(keyId >> 56);
data[1] = (byte)(keyId >> 48);
data[2] = (byte)(keyId >> 40);
data[3] = (byte)(keyId >> 32);
data[4] = (byte)(keyId >> 24);
data[5] = (byte)(keyId >> 16);
data[6] = (byte)(keyId >> 8);
data[7] = (byte)keyId;

FingerprintUtil.writeKeyID(keyId, data);
return data;
}

Expand All @@ -43,9 +35,6 @@ public IssuerKeyID(

public long getKeyID()
{
long keyID = ((long)(data[0] & 0xff) << 56) | ((long)(data[1] & 0xff) << 48) | ((long)(data[2] & 0xff) << 40) | ((long)(data[3] & 0xff) << 32)
| ((long)(data[4] & 0xff) << 24) | ((data[5] & 0xff) << 16) | ((data[6] & 0xff) << 8) | (data[7] & 0xff);

return keyID;
return FingerprintUtil.readKeyID(data);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.bouncycastle.bcpg.test;

import org.bouncycastle.bcpg.FingerprintUtil;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.util.test.SimpleTest;

Expand Down Expand Up @@ -46,6 +47,38 @@ private void testLibrePgpKeyIdFromFingerprint()
-3812177997909612905L, FingerprintUtil.keyIdFromLibrePgpFingerprint(decoded));
}

private void testLeftMostEqualsRightMostFor8Bytes()
{
byte[] bytes = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
isEquals(
FingerprintUtil.longFromLeftMostBytes(bytes),
FingerprintUtil.longFromRightMostBytes(bytes));
byte[] b = new byte[8];
FingerprintUtil.writeKeyID(FingerprintUtil.longFromLeftMostBytes(bytes), b);
isTrue(Arrays.areEqual(bytes, b));
}

private void testWriteKeyIdToBytes()
{
byte[] bytes = new byte[12];
long keyId = 72623859790382856L;
FingerprintUtil.writeKeyID(keyId, bytes, 2);
isTrue(Arrays.areEqual(
new byte[] {0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00},
bytes));

try
{
byte[] b = new byte[7];
FingerprintUtil.writeKeyID(0, b);
fail("Expected IllegalArgumentException for too short byte array.");
}
catch (IllegalArgumentException e)
{
// Expected
}
}

@Override
public String getName()
{
Expand All @@ -60,6 +93,8 @@ public void performTest()
testV6KeyIdFromFingerprint();
testKeyIdFromTooShortFails();
testLibrePgpKeyIdFromFingerprint();
testLeftMostEqualsRightMostFor8Bytes();
testWriteKeyIdToBytes();
}

public static void main(String[] args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,7 @@ private void roundtripV6Packet()
{
byte[] salt = new byte[32];
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));
long keyID = FingerprintUtil.keyIdFromV6Fingerprint(fingerprint);

new SecureRandom().nextBytes(salt);
OnePassSignaturePacket before = new OnePassSignaturePacket(
Expand Down