Skip to content

Commit 77f3465

Browse files
committed
Merge branch 'pgpainless-roundtripPacketFormat'
2 parents 1bae99b + b25718a commit 77f3465

29 files changed

+692
-98
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ public class AEADEncDataPacket
2323
private final byte[] iv;
2424

2525
public AEADEncDataPacket(BCPGInputStream in)
26+
throws IOException
27+
{
28+
this(in, false);
29+
}
30+
31+
public AEADEncDataPacket(BCPGInputStream in,
32+
boolean newPacketFormat)
2633
throws IOException
2734
{
28-
super(in, AEAD_ENC_DATA);
35+
super(in, AEAD_ENC_DATA, newPacketFormat);
2936

3037
version = (byte)in.read();
3138
if (version != VERSION_1)

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -264,52 +264,52 @@ else if (l == 255)
264264
switch (tag)
265265
{
266266
case RESERVED:
267-
return new ReservedPacket(objStream);
267+
return new ReservedPacket(objStream, newPacket);
268268
case PUBLIC_KEY_ENC_SESSION:
269-
return new PublicKeyEncSessionPacket(objStream);
269+
return new PublicKeyEncSessionPacket(objStream, newPacket);
270270
case SIGNATURE:
271-
return new SignaturePacket(objStream);
271+
return new SignaturePacket(objStream, newPacket);
272272
case SYMMETRIC_KEY_ENC_SESSION:
273-
return new SymmetricKeyEncSessionPacket(objStream);
273+
return new SymmetricKeyEncSessionPacket(objStream, newPacket);
274274
case ONE_PASS_SIGNATURE:
275-
return new OnePassSignaturePacket(objStream);
275+
return new OnePassSignaturePacket(objStream, newPacket);
276276
case SECRET_KEY:
277-
return new SecretKeyPacket(objStream);
277+
return new SecretKeyPacket(objStream, newPacket);
278278
case PUBLIC_KEY:
279-
return new PublicKeyPacket(objStream);
279+
return new PublicKeyPacket(objStream, newPacket);
280280
case SECRET_SUBKEY:
281-
return new SecretSubkeyPacket(objStream);
281+
return new SecretSubkeyPacket(objStream, newPacket);
282282
case COMPRESSED_DATA:
283-
return new CompressedDataPacket(objStream);
283+
return new CompressedDataPacket(objStream, newPacket);
284284
case SYMMETRIC_KEY_ENC:
285-
return new SymmetricEncDataPacket(objStream);
285+
return new SymmetricEncDataPacket(objStream, newPacket);
286286
case MARKER:
287-
return new MarkerPacket(objStream);
287+
return new MarkerPacket(objStream, newPacket);
288288
case LITERAL_DATA:
289-
return new LiteralDataPacket(objStream);
289+
return new LiteralDataPacket(objStream, newPacket);
290290
case TRUST:
291-
return new TrustPacket(objStream);
291+
return new TrustPacket(objStream, newPacket);
292292
case USER_ID:
293-
return new UserIDPacket(objStream);
293+
return new UserIDPacket(objStream, newPacket);
294294
case USER_ATTRIBUTE:
295-
return new UserAttributePacket(objStream);
295+
return new UserAttributePacket(objStream, newPacket);
296296
case PUBLIC_SUBKEY:
297-
return new PublicSubkeyPacket(objStream);
297+
return new PublicSubkeyPacket(objStream, newPacket);
298298
case SYM_ENC_INTEGRITY_PRO:
299-
return new SymmetricEncIntegrityPacket(objStream);
299+
return new SymmetricEncIntegrityPacket(objStream, newPacket);
300300
case MOD_DETECTION_CODE:
301-
return new ModDetectionCodePacket(objStream);
301+
return new ModDetectionCodePacket(objStream, newPacket);
302302
case AEAD_ENC_DATA:
303-
return new AEADEncDataPacket(objStream);
303+
return new AEADEncDataPacket(objStream, newPacket);
304304
case PADDING:
305-
return new PaddingPacket(objStream);
305+
return new PaddingPacket(objStream, newPacket);
306306
case EXPERIMENTAL_1:
307307
case EXPERIMENTAL_2:
308308
case EXPERIMENTAL_3:
309309
case EXPERIMENTAL_4:
310-
return new ExperimentalPacket(tag, objStream);
310+
return new ExperimentalPacket(tag, objStream, newPacket);
311311
default:
312-
return new UnknownPacket(tag, objStream);
312+
return new UnknownPacket(tag, objStream, newPacket);
313313
}
314314
}
315315

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

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static BCPGOutputStream wrap(OutputStream out)
2929
}
3030

3131
OutputStream out;
32-
private boolean useOldFormat;
32+
private PacketFormat packetFormat;
3333
private byte[] partialBuffer;
3434
private int partialBufferLength;
3535
private int partialPower;
@@ -46,11 +46,11 @@ public static BCPGOutputStream wrap(OutputStream out)
4646
public BCPGOutputStream(
4747
OutputStream out)
4848
{
49-
this(out, false);
49+
this(out, PacketFormat.ROUNDTRIP);
5050
}
5151

5252
/**
53-
* Base constructor specifying whether or not to use packets in the new format
53+
* Base constructor specifying whether to use packets in the new format
5454
* wherever possible.
5555
*
5656
* @param out output stream to write encoded data to.
@@ -59,9 +59,16 @@ public BCPGOutputStream(
5959
public BCPGOutputStream(
6060
OutputStream out,
6161
boolean newFormatOnly)
62+
{
63+
this(out, newFormatOnly ? PacketFormat.CURRENT : PacketFormat.ROUNDTRIP);
64+
}
65+
66+
public BCPGOutputStream(
67+
OutputStream out,
68+
PacketFormat packetFormat)
6269
{
6370
this.out = out;
64-
this.useOldFormat = !newFormatOnly;
71+
this.packetFormat = packetFormat;
6572
}
6673

6774
/**
@@ -75,6 +82,7 @@ public BCPGOutputStream(
7582
throws IOException
7683
{
7784
this.out = out;
85+
this.packetFormat = PacketFormat.LEGACY;
7886
this.writeHeader(tag, true, true, 0);
7987
}
8088

@@ -95,6 +103,7 @@ public BCPGOutputStream(
95103
throws IOException
96104
{
97105
this.out = out;
106+
this.packetFormat = oldFormat ? PacketFormat.LEGACY : PacketFormat.CURRENT;
98107

99108
if (length > 0xFFFFFFFFL)
100109
{
@@ -122,6 +131,7 @@ public BCPGOutputStream(
122131
throws IOException
123132
{
124133
this.out = out;
134+
this.packetFormat = PacketFormat.CURRENT;
125135

126136
this.writeHeader(tag, false, false, length);
127137
}
@@ -141,6 +151,7 @@ public BCPGOutputStream(
141151
throws IOException
142152
{
143153
this.out = out;
154+
this.packetFormat = PacketFormat.CURRENT;
144155
this.writeHeader(tag, false, true, 0);
145156

146157
this.partialBuffer = buffer;
@@ -316,22 +327,66 @@ public void write(
316327
}
317328
}
318329

330+
/**
331+
* Write a packet to the stream.
332+
* @param p packet
333+
* @throws IOException
334+
*/
319335
public void writePacket(
320336
ContainedPacket p)
321337
throws IOException
322338
{
323339
p.encode(this);
324340
}
325341

342+
/**
343+
* Write a packet to the stream.
344+
* The packet will use the old encoding format if {@link #packetFormat} is {@link PacketFormat#LEGACY}, otherwise
345+
* it will be encoded using the new packet format.
346+
* @param tag packet tag
347+
* @param body packet body
348+
* @throws IOException
349+
*/
326350
void writePacket(
327351
int tag,
328352
byte[] body)
329353
throws IOException
330354
{
331-
this.writeHeader(tag, useOldFormat, false, body.length);
355+
this.writeHeader(tag, packetFormat == PacketFormat.LEGACY, false, body.length);
332356
this.write(body);
333357
}
334358

359+
/**
360+
* Write a packet.
361+
* The packet format will be chosen primarily based on {@link #packetFormat}.
362+
* If {@link #packetFormat} is {@link PacketFormat#CURRENT}, the packet will be encoded using the new format.
363+
* If it is {@link PacketFormat#LEGACY}, the packet will use old encoding format.
364+
* If it is {@link PacketFormat#ROUNDTRIP}, then the format will be determined by objectPrefersNewPacketFormat.
365+
*
366+
* @param objectPrefersNewPacketFormat whether the packet prefers to be encoded using the new packet format
367+
* @param tag packet tag
368+
* @param body packet body
369+
* @throws IOException
370+
*/
371+
void writePacket(
372+
boolean objectPrefersNewPacketFormat,
373+
int tag,
374+
byte[] body)
375+
throws IOException
376+
{
377+
boolean oldPacketFormat = packetFormat == PacketFormat.LEGACY ||
378+
(packetFormat == PacketFormat.ROUNDTRIP && !objectPrefersNewPacketFormat);
379+
this.writeHeader(tag, oldPacketFormat, false, body.length);
380+
this.write(body);
381+
}
382+
383+
/**
384+
* Write a packet, forcing the packet format to be either old or new.
385+
* @param tag packet tag
386+
* @param body packet body
387+
* @param oldFormat if true, old format is forced, else force new format
388+
* @throws IOException
389+
*/
335390
void writePacket(
336391
int tag,
337392
byte[] body,
@@ -379,4 +434,5 @@ public void close()
379434
out.flush();
380435
out.close();
381436
}
437+
382438
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ public class CompressedDataPacket
1111
int algorithm;
1212

1313
CompressedDataPacket(
14-
BCPGInputStream in)
14+
BCPGInputStream in)
15+
throws IOException
16+
{
17+
this(in, false);
18+
}
19+
20+
CompressedDataPacket(
21+
BCPGInputStream in,
22+
boolean newPacketFormat)
1523
throws IOException
1624
{
17-
super(in, COMPRESSED_DATA);
25+
super(in, COMPRESSED_DATA, newPacketFormat);
1826

1927
algorithm = in.read();
2028
}

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,30 @@ public abstract class ContainedPacket
1212
extends Packet
1313
implements Encodable
1414
{
15+
1516
ContainedPacket(int packetTag)
1617
{
17-
super(packetTag);
18+
this(packetTag, false);
19+
}
20+
21+
ContainedPacket(int packetTag, boolean newPacketFormat)
22+
{
23+
super(packetTag, newPacketFormat);
1824
}
1925

2026
public byte[] getEncoded()
2127
throws IOException
2228
{
23-
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
24-
BCPGOutputStream pOut = new BCPGOutputStream(bOut);
25-
26-
pOut.writePacket(this);
29+
return getEncoded(PacketFormat.ROUNDTRIP);
30+
}
2731

32+
public byte[] getEncoded(PacketFormat format)
33+
throws IOException
34+
{
35+
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
36+
BCPGOutputStream pOut = new BCPGOutputStream(bOut, format);
37+
pOut.writePacket(this);
2838
pOut.close();
29-
3039
return bOut.toByteArray();
3140
}
3241

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,32 @@ public class ExperimentalPacket
1111
extends ContainedPacket implements PublicKeyAlgorithmTags
1212
{
1313
private byte[] contents;
14-
14+
15+
/**
16+
*
17+
* @param in
18+
* @throws IOException
19+
*/
20+
ExperimentalPacket(
21+
int tag,
22+
BCPGInputStream in)
23+
throws IOException
24+
{
25+
this(tag, in, false);
26+
}
27+
1528
/**
1629
*
1730
* @param in
1831
* @throws IOException
1932
*/
2033
ExperimentalPacket(
2134
int tag,
22-
BCPGInputStream in)
35+
BCPGInputStream in,
36+
boolean newPacketFormat)
2337
throws IOException
2438
{
25-
super(tag);
39+
super(tag, newPacketFormat);
2640

2741
this.contents = in.readAll();
2842
}
@@ -44,6 +58,6 @@ public void encode(
4458
BCPGOutputStream out)
4559
throws IOException
4660
{
47-
out.writePacket(getPacketTag(), contents);
61+
out.writePacket(hasNewPacketFormat(), getPacketTag(), contents);
4862
}
4963
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@ public InputStreamPacket(
2222
BCPGInputStream in,
2323
int packetTag)
2424
{
25-
super(packetTag);
26-
25+
this(in, packetTag, false);
26+
}
27+
28+
InputStreamPacket(
29+
BCPGInputStream in,
30+
int packetTag,
31+
boolean newPacketFormat)
32+
{
33+
super(packetTag, newPacketFormat);
2734
this.in = in;
2835
}
2936

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ public class LiteralDataPacket
1616
long modDate;
1717

1818
LiteralDataPacket(
19-
BCPGInputStream in)
19+
BCPGInputStream in)
20+
throws IOException
21+
{
22+
this(in, false);
23+
}
24+
25+
LiteralDataPacket(
26+
BCPGInputStream in,
27+
boolean newPacketFormat)
2028
throws IOException
2129
{
22-
super(in, LITERAL_DATA);
30+
super(in, LITERAL_DATA, newPacketFormat);
2331

2432
format = in.read();
2533
int l = in.read();

0 commit comments

Comments
 (0)