mirrored from https://www.bouncycastle.org/repositories/bc-java
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix unpacking time (unsigned int) from octets for large values
- Loading branch information
1 parent
7352222
commit f4f8638
Showing
6 changed files
with
94 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
pg/src/test/java/org/bouncycastle/bcpg/test/TimeEncodingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.bouncycastle.bcpg.test; | ||
|
||
import org.bouncycastle.bcpg.BCPGInputStream; | ||
import org.bouncycastle.bcpg.BCPGOutputStream; | ||
import org.bouncycastle.bcpg.PacketFormat; | ||
import org.bouncycastle.bcpg.PublicKeyPacket; | ||
import org.bouncycastle.bcpg.PublicSubkeyPacket; | ||
import org.bouncycastle.bcpg.UnknownBCPGKey; | ||
import org.bouncycastle.bcpg.sig.KeyExpirationTime; | ||
import org.bouncycastle.util.test.SimpleTest; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.Date; | ||
|
||
public class TimeEncodingTest | ||
extends SimpleTest | ||
{ | ||
@Override | ||
public String getName() | ||
{ | ||
return "UtilsTest"; | ||
} | ||
|
||
@Override | ||
public void performTest() | ||
throws Exception | ||
{ | ||
testRoundtrippingLargeUnsignedInt(); | ||
testKeyWithLargeCreationTime(); | ||
} | ||
|
||
private void testRoundtrippingLargeUnsignedInt() | ||
{ | ||
// Integer.MAX_VALUE < large < 0xffffffff | ||
long large = 2523592696L; // fits a 32-bit *unsigned* int, but overflows signed int | ||
// KeyExpirationTime packs the time into 4 octets | ||
KeyExpirationTime kexp = new KeyExpirationTime(false, large); | ||
// getTime() parses the time from 4 octets | ||
isEquals("Roundtripped unsigned int mismatches before packet parser pass", large, kexp.getTime()); | ||
|
||
// To be safe, do an additional packet encode/decode roundtrip | ||
KeyExpirationTime pKexp = new KeyExpirationTime(kexp.isCritical(), kexp.isLongLength(), kexp.getData()); | ||
isEquals("Roundtripped unsigned int mismatches after packet parser pass", large, pKexp.getTime()); | ||
} | ||
|
||
private void testKeyWithLargeCreationTime() | ||
throws IOException | ||
{ | ||
long maxSeconds = 0xFFFFFFFEL; // Fits 32 unsigned int, but not signed int | ||
Date maxPGPDate = new Date(maxSeconds * 1000); | ||
UnknownBCPGKey k = new UnknownBCPGKey(1, new byte[]{1}); // dummy | ||
PublicKeyPacket p = new PublicKeyPacket(PublicKeyPacket.VERSION_6, 99, maxPGPDate, k); | ||
isEquals("Key creation time mismatches before encoding", maxPGPDate, p.getTime()); | ||
|
||
ByteArrayOutputStream bOut = new ByteArrayOutputStream(); | ||
BCPGOutputStream pOut = new BCPGOutputStream(bOut, PacketFormat.CURRENT); | ||
p.encode(pOut); | ||
pOut.close(); | ||
ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray()); | ||
BCPGInputStream pIn = new BCPGInputStream(bIn); | ||
PublicKeyPacket parsed = (PublicKeyPacket) pIn.readPacket(); | ||
isEquals("Key creation time mismatches after encoding", maxPGPDate, parsed.getTime()); | ||
} | ||
|
||
public static void main(String[] args) | ||
{ | ||
runTest(new TimeEncodingTest()); | ||
} | ||
} |