Skip to content

Commit 27eb23c

Browse files
committed
Merge branch '1906-readtime' into 'main'
1906 readtime See merge request root/bc-java!41
2 parents 47539e2 + 13d0d41 commit 27eb23c

File tree

3 files changed

+95
-19
lines changed

3 files changed

+95
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public class PublicKeyPacket
136136
throw new UnsupportedPacketVersionException("Unsupported Public Key Packet version encountered: " + version);
137137
}
138138

139-
time = StreamUtil.read4OctetLength(in);
139+
time = StreamUtil.read4OctetLength(in) & 0xFFFFFFFFL;
140140

141141
if (version == 2 || version == VERSION_3)
142142
{

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static int read2OctetLength(InputStream in)
134134
}
135135

136136
static void write4OctetLength(OutputStream pOut, int len)
137-
throws IOException
137+
throws IOException
138138
{
139139
pOut.write(len >> 24);
140140
pOut.write(len >> 16);
@@ -151,7 +151,6 @@ static int read4OctetLength(InputStream in)
151151
static int flag_eof = 0;
152152
static int flag_isLongLength = 1;
153153
static int flag_partial = 2;
154-
155154
/**
156155
* Note: flags is an array of three boolean values:
157156
* flags[0] indicates l is negative, flag for eof
@@ -192,27 +191,27 @@ else if (l == 255)
192191
static void write8OctetLength(OutputStream pOut, long len)
193192
throws IOException
194193
{
195-
pOut.write((int)(len >> 56));
196-
pOut.write((int)(len >> 48));
197-
pOut.write((int)(len >> 40));
198-
pOut.write((int)(len >> 32));
199-
pOut.write((int)(len >> 24));
200-
pOut.write((int)(len >> 16));
201-
pOut.write((int)(len >> 8));
202-
pOut.write((int)len);
194+
pOut.write((int) (len >> 56));
195+
pOut.write((int) (len >> 48));
196+
pOut.write((int) (len >> 40));
197+
pOut.write((int) (len >> 32));
198+
pOut.write((int) (len >> 24));
199+
pOut.write((int) (len >> 16));
200+
pOut.write((int) (len >> 8));
201+
pOut.write((int) len);
203202
}
204203

205204
static long read8OctetLength(InputStream in)
206205
throws IOException
207206
{
208-
return ((long)in.read() << 56) |
209-
((long)in.read() << 48) |
210-
((long)in.read() << 40) |
211-
((long)in.read() << 32) |
212-
((long)in.read() << 24) |
213-
((long)in.read() << 16) |
214-
((long)in.read() << 8) |
215-
((long)in.read());
207+
return ((long) in.read() << 56) |
208+
((long) in.read() << 48) |
209+
((long) in.read() << 40) |
210+
((long) in.read() << 32) |
211+
((long) in.read() << 24) |
212+
((long) in.read() << 16) |
213+
((long) in.read() << 8) |
214+
((long) in.read());
216215
}
217216

218217
}

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
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;
12+
import java.util.TimeZone;
913

1014
import org.bouncycastle.bcpg.AEADAlgorithmTags;
1115
import org.bouncycastle.bcpg.ArmoredInputStream;
@@ -25,18 +29,35 @@
2529
import org.bouncycastle.crypto.params.X25519KeyGenerationParameters;
2630
import org.bouncycastle.jce.provider.BouncyCastleProvider;
2731
import org.bouncycastle.openpgp.PGPEncryptedDataList;
32+
import org.bouncycastle.openpgp.PGPLiteralData;
2833
import org.bouncycastle.openpgp.PGPObjectFactory;
2934
import org.bouncycastle.openpgp.PGPPBEEncryptedData;
3035
import org.bouncycastle.openpgp.PGPPublicKey;
3136
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
3237
import org.bouncycastle.openpgp.operator.bc.BcPGPKeyPair;
38+
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
3339
import org.bouncycastle.util.Arrays;
40+
import org.bouncycastle.util.Pack;
3441
import org.bouncycastle.util.Strings;
3542
import org.bouncycastle.util.test.SimpleTest;
3643

3744
public class BcpgGeneralTest
3845
extends SimpleTest
3946
{
47+
/*
48+
* Format: Binary data
49+
Filename: "hello.txt"
50+
Timestamp: 2104-06-26 14:42:55 UTC
51+
Content: "Hello, world!\n"
52+
* */
53+
byte[] message = Strings.toUTF8ByteArray("-----BEGIN PGP MESSAGE-----\n" +
54+
"\n" +
55+
"yx1iCWhlbGxvLnR4dPz1TW9IZWxsbywgd29ybGQhCg==\n" +
56+
"=3swl\n" +
57+
"-----END PGP MESSAGE-----");
58+
59+
60+
4061
public static void main(String[] args)
4162
{
4263
Security.addProvider(new BouncyCastleProvider());
@@ -54,13 +75,69 @@ public String getName()
5475
public void performTest()
5576
throws Exception
5677
{
78+
testReadTime();
79+
testReadTime2();
5780
//testS2K();
5881
testExceptions();
5982
testECDHPublicBCPGKey();
6083
// Tests for PreferredAEADCiphersuites
6184
testPreferredAEADCiphersuites();
6285
}
6386

87+
static int read4OctetLength(InputStream in)
88+
throws IOException
89+
{
90+
return (in.read() << 24) | (in.read() << 16) | (in.read() << 8) | in.read();
91+
}
92+
93+
// StreamUtil.readTime
94+
static long readTime(BCPGInputStream in)
95+
throws IOException
96+
{
97+
return ((long)read4OctetLength(in) & 0xFFFFFFFFL) * 1000L;
98+
}
99+
100+
public void testReadTime()
101+
throws IOException
102+
{
103+
Calendar calendar = Calendar.getInstance();
104+
calendar.set(2074, Calendar.JANUARY, 1, 0, 0, 0);
105+
calendar.set(Calendar.MILLISECOND, 0);
106+
107+
Date tmp = calendar.getTime();
108+
long time = tmp.getTime() / 1000L * 1000L;
109+
byte[] date = Pack.intToBigEndian((int)(time / 1000L));
110+
111+
ByteArrayInputStream bs = new ByteArrayInputStream(date);
112+
BCPGInputStream stream = new BCPGInputStream(bs);
113+
long rlt = readTime(stream);
114+
isTrue(rlt == time);
115+
116+
time = Long.MAX_VALUE / 1000L * 1000L;
117+
date = Pack.intToBigEndian((int)(time / 1000L));
118+
bs = new ByteArrayInputStream(date);
119+
stream = new BCPGInputStream(bs);
120+
rlt = readTime(stream);
121+
byte[] date2 = Pack.intToBigEndian((int)(rlt / 1000L));
122+
isTrue(Arrays.areEqual(date, date2));
123+
}
124+
125+
public void testReadTime2()
126+
throws Exception
127+
{
128+
PGPObjectFactory pgpObjectFactoryOfTestFile = new PGPObjectFactory(
129+
new ArmoredInputStream(new ByteArrayInputStream(message)), new JcaKeyFingerprintCalculator());
130+
PGPLiteralData ld = (PGPLiteralData)pgpObjectFactoryOfTestFile.nextObject();
131+
Date date = ld.getModificationTime();
132+
133+
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
134+
calendar.set(2104, Calendar.JUNE, 26, 14, 42, 55);
135+
calendar.set(Calendar.MILLISECOND, 0);
136+
Date expected = calendar.getTime();
137+
138+
isTrue(date.equals(expected));
139+
}
140+
64141
public void testPreferredAEADCiphersuites()
65142
throws Exception
66143
{

0 commit comments

Comments
 (0)