Skip to content

Commit 7e05e3e

Browse files
author
gefeili
committed
Fix the issue in StreamUtil.readTime
1 parent 88c3a55 commit 7e05e3e

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

pg/src/main/java/org/bouncycastle/bcpg/StreamUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static void writeTime(BCPGOutputStream pOut, long time)
117117
static long readTime(BCPGInputStream in)
118118
throws IOException
119119
{
120-
return (long)read4OctetLength(in) * 1000L;
120+
return (((long) in.read() << 24 | in.read() << 16 | in.read() << 8 | in.read()) & 0xFFFFFFFFL) * 1000L;
121121
}
122122

123123
static void write2OctetLength(OutputStream pOut, int len)

pg/src/test/java/org/bouncycastle/openpgp/test/BcpgGeneralTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import java.io.ByteArrayInputStream;
44
import java.io.ByteArrayOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
57
import java.security.SecureRandom;
68
import java.security.Security;
9+
import java.util.Calendar;
710
import java.util.Date;
811
import java.util.Iterator;
912

@@ -31,6 +34,7 @@
3134
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
3235
import org.bouncycastle.openpgp.operator.bc.BcPGPKeyPair;
3336
import org.bouncycastle.util.Arrays;
37+
import org.bouncycastle.util.Pack;
3438
import org.bouncycastle.util.Strings;
3539
import org.bouncycastle.util.test.SimpleTest;
3640

@@ -54,13 +58,46 @@ public String getName()
5458
public void performTest()
5559
throws Exception
5660
{
61+
testReadTime();
5762
//testS2K();
5863
testExceptions();
5964
testECDHPublicBCPGKey();
6065
// Tests for PreferredAEADCiphersuites
6166
testPreferredAEADCiphersuites();
6267
}
6368

69+
// StreamUtil.readTime
70+
static long readTime(BCPGInputStream in)
71+
throws IOException
72+
{
73+
return (((long) in.read() << 24 | in.read() << 16 | in.read() << 8 | in.read()) & 0xFFFFFFFFL) * 1000L;
74+
}
75+
76+
public void testReadTime()
77+
throws IOException
78+
{
79+
Calendar calendar = Calendar.getInstance();
80+
calendar.set(2074, Calendar.JANUARY, 1, 0, 0, 0);
81+
calendar.set(Calendar.MILLISECOND, 0);
82+
83+
Date tmp = calendar.getTime();
84+
long time = tmp.getTime() / 1000L * 1000L;
85+
byte[] date = Pack.intToBigEndian((int)(time / 1000L));
86+
87+
ByteArrayInputStream bs = new ByteArrayInputStream(date);
88+
BCPGInputStream stream = new BCPGInputStream(bs);
89+
long rlt = readTime(stream);
90+
isTrue(rlt == time);
91+
92+
time = Long.MAX_VALUE / 1000L * 1000L;
93+
date = Pack.intToBigEndian((int)(time / 1000L));
94+
bs = new ByteArrayInputStream(date);
95+
stream = new BCPGInputStream(bs);
96+
rlt = readTime(stream);
97+
byte[] date2 = Pack.intToBigEndian((int)(rlt / 1000L));
98+
isTrue(Arrays.areEqual(date, date2));
99+
}
100+
64101
public void testPreferredAEADCiphersuites()
65102
throws Exception
66103
{

0 commit comments

Comments
 (0)