Skip to content

Commit b49da0f

Browse files
author
gefeili
committed
Merge branch 'main' into pg-synchronize-bc_csharp
# Conflicts: # pg/src/main/java/org/bouncycastle/bcpg/BCPGOutputStream.java # pg/src/main/java/org/bouncycastle/bcpg/FingerprintUtil.java # pg/src/main/java/org/bouncycastle/bcpg/OnePassSignaturePacket.java # pg/src/main/java/org/bouncycastle/bcpg/SignaturePacket.java # pg/src/main/java/org/bouncycastle/bcpg/sig/IssuerKeyID.java
2 parents 5eb068a + 0588317 commit b49da0f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1386
-357
lines changed
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
11
package org.bouncycastle.bcpg;
22

3+
/**
4+
* AEAD Algorithm IDs.
5+
* Crypto-Refresh (OpenPGP) defines IDs 1 through 3, while LibrePGP only defines 1 and 2.
6+
* Further, the use of AEAD differs between C-R and LibrePGP.
7+
*
8+
* @see <a href="https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-13.html#name-aead-algorithms">
9+
* Crypto-Refresh: AEAD Algorithms</a>
10+
* @see <a href="https://www.ietf.org/archive/id/draft-koch-librepgp-00.html#name-encryption-modes">
11+
* LibrePGP - Encryption Modes</a>
12+
*/
313
public interface AEADAlgorithmTags
414
{
5-
int EAX = 1; // EAX (IV len: 16 octets, Tag len: 16 octets)
6-
int OCB = 2; // OCB (IV len: 15 octets, Tag len: 16 octets)
7-
int GCM = 3; // GCM (IV len: 12 octets, Tag len: 16 octets)
15+
/**
16+
* EAX with 16-bit nonce/IV and 16-bit auth tag length.
17+
*/
18+
int EAX = 1;
19+
/**
20+
* OCB with 15-bit nonce/IV and 16-bit auth tag length.
21+
* C-R compliant implementations MUST implement OCB.
22+
*/
23+
int OCB = 2;
24+
/**
25+
* GCM with 12-bit nonce/IV and 16-bit auth tag length.
26+
* OpenPGP only.
27+
*/
28+
int GCM = 3;
29+
30+
// 100 to 110: Experimental algorithms
831
}

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
@@ -246,52 +246,52 @@ public Packet readPacket()
246246
switch (tag)
247247
{
248248
case RESERVED:
249-
return new ReservedPacket(objStream);
249+
return new ReservedPacket(objStream, newPacket);
250250
case PUBLIC_KEY_ENC_SESSION:
251-
return new PublicKeyEncSessionPacket(objStream);
251+
return new PublicKeyEncSessionPacket(objStream, newPacket);
252252
case SIGNATURE:
253-
return new SignaturePacket(objStream);
253+
return new SignaturePacket(objStream, newPacket);
254254
case SYMMETRIC_KEY_ENC_SESSION:
255-
return new SymmetricKeyEncSessionPacket(objStream);
255+
return new SymmetricKeyEncSessionPacket(objStream, newPacket);
256256
case ONE_PASS_SIGNATURE:
257-
return new OnePassSignaturePacket(objStream);
257+
return new OnePassSignaturePacket(objStream, newPacket);
258258
case SECRET_KEY:
259-
return new SecretKeyPacket(objStream);
259+
return new SecretKeyPacket(objStream, newPacket);
260260
case PUBLIC_KEY:
261-
return new PublicKeyPacket(objStream);
261+
return new PublicKeyPacket(objStream, newPacket);
262262
case SECRET_SUBKEY:
263-
return new SecretSubkeyPacket(objStream);
263+
return new SecretSubkeyPacket(objStream, newPacket);
264264
case COMPRESSED_DATA:
265-
return new CompressedDataPacket(objStream);
265+
return new CompressedDataPacket(objStream, newPacket);
266266
case SYMMETRIC_KEY_ENC:
267-
return new SymmetricEncDataPacket(objStream);
267+
return new SymmetricEncDataPacket(objStream, newPacket);
268268
case MARKER:
269-
return new MarkerPacket(objStream);
269+
return new MarkerPacket(objStream, newPacket);
270270
case LITERAL_DATA:
271-
return new LiteralDataPacket(objStream);
271+
return new LiteralDataPacket(objStream, newPacket);
272272
case TRUST:
273-
return new TrustPacket(objStream);
273+
return new TrustPacket(objStream, newPacket);
274274
case USER_ID:
275-
return new UserIDPacket(objStream);
275+
return new UserIDPacket(objStream, newPacket);
276276
case USER_ATTRIBUTE:
277-
return new UserAttributePacket(objStream);
277+
return new UserAttributePacket(objStream, newPacket);
278278
case PUBLIC_SUBKEY:
279-
return new PublicSubkeyPacket(objStream);
279+
return new PublicSubkeyPacket(objStream, newPacket);
280280
case SYM_ENC_INTEGRITY_PRO:
281-
return new SymmetricEncIntegrityPacket(objStream);
281+
return new SymmetricEncIntegrityPacket(objStream, newPacket);
282282
case MOD_DETECTION_CODE:
283-
return new ModDetectionCodePacket(objStream);
283+
return new ModDetectionCodePacket(objStream, newPacket);
284284
case AEAD_ENC_DATA:
285-
return new AEADEncDataPacket(objStream);
285+
return new AEADEncDataPacket(objStream, newPacket);
286286
case PADDING:
287-
return new PaddingPacket(objStream);
287+
return new PaddingPacket(objStream, newPacket);
288288
case EXPERIMENTAL_1:
289289
case EXPERIMENTAL_2:
290290
case EXPERIMENTAL_3:
291291
case EXPERIMENTAL_4:
292-
return new ExperimentalPacket(tag, objStream);
292+
return new ExperimentalPacket(tag, objStream, newPacket);
293293
default:
294-
return new UnknownPacket(tag, objStream);
294+
return new UnknownPacket(tag, objStream, newPacket);
295295
}
296296
}
297297

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,13 +327,26 @@ 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 writeShort(short n)
327351
throws IOException
328352
{
@@ -344,10 +368,41 @@ void writePacket(
344368
byte[] body)
345369
throws IOException
346370
{
347-
this.writeHeader(tag, useOldFormat, false, body.length);
371+
this.writeHeader(tag, packetFormat == PacketFormat.LEGACY, false, body.length);
348372
this.write(body);
349373
}
350374

375+
/**
376+
* Write a packet.
377+
* The packet format will be chosen primarily based on {@link #packetFormat}.
378+
* If {@link #packetFormat} is {@link PacketFormat#CURRENT}, the packet will be encoded using the new format.
379+
* If it is {@link PacketFormat#LEGACY}, the packet will use old encoding format.
380+
* If it is {@link PacketFormat#ROUNDTRIP}, then the format will be determined by objectPrefersNewPacketFormat.
381+
*
382+
* @param objectPrefersNewPacketFormat whether the packet prefers to be encoded using the new packet format
383+
* @param tag packet tag
384+
* @param body packet body
385+
* @throws IOException
386+
*/
387+
void writePacket(
388+
boolean objectPrefersNewPacketFormat,
389+
int tag,
390+
byte[] body)
391+
throws IOException
392+
{
393+
boolean oldPacketFormat = packetFormat == PacketFormat.LEGACY ||
394+
(packetFormat == PacketFormat.ROUNDTRIP && !objectPrefersNewPacketFormat);
395+
this.writeHeader(tag, oldPacketFormat, false, body.length);
396+
this.write(body);
397+
}
398+
399+
/**
400+
* Write a packet, forcing the packet format to be either old or new.
401+
* @param tag packet tag
402+
* @param body packet body
403+
* @param oldFormat if true, old format is forced, else force new format
404+
* @throws IOException
405+
*/
351406
void writePacket(
352407
int tag,
353408
byte[] body,
@@ -395,4 +450,5 @@ public void close()
395450
out.flush();
396451
out.close();
397452
}
453+
398454
}

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/CompressionAlgorithmTags.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package org.bouncycastle.bcpg;
22

33
/**
4-
* Basic tags for compression algorithms
4+
* Basic tags for compression algorithms.
5+
*
6+
* @see <a href="https://www.rfc-editor.org/rfc/rfc4880.html#section-9.3">
7+
* RFC4880 - Compression Algorithms</a>
8+
* @see <a href="https://www.ietf.org/archive/id/draft-koch-librepgp-00.html#name-compression-algorithms">
9+
* LibrePGP - Compression Algorithms</a>
10+
* @see <a href="https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-13.html#name-compression-algorithms">
11+
* Crypto-Refresh - Compression Algorithms</a>
512
*/
613
public interface CompressionAlgorithmTags
714
{

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/ECDHPublicBCPGKey.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77
import org.bouncycastle.math.ec.ECPoint;
88

99
/**
10-
* base class for an ECDH Public Key.
10+
* Base class for an ECDH Public Key.
11+
* This type is for use with {@link PublicKeyAlgorithmTags#ECDH}.
12+
* The specific curve is identified by providing an OID.
13+
* Regarding X25519, X448, consider the following:
14+
* Modern implementations use dedicated key types {@link X25519PublicBCPGKey}, {@link X448PublicBCPGKey} along with
15+
* dedicated algorithm tags {@link PublicKeyAlgorithmTags#X25519}, {@link PublicKeyAlgorithmTags#X448}.
16+
* If you want to be compatible with legacy applications however, you should use this class instead.
17+
* Note though, that for v6 keys, {@link X25519PublicBCPGKey} or {@link X448PublicBCPGKey} MUST be used for X25519, X448.
18+
*
19+
* @see <a href="https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-13.html#name-algorithm-specific-part-for-ecd">
20+
* Crypto-Refresh - Algorithm-Specific Parts for ECDH Keys</a>
1121
*/
1222
public class ECDHPublicBCPGKey
1323
extends ECPublicBCPGKey

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
import org.bouncycastle.math.ec.ECPoint;
88

99
/**
10-
* base class for an ECDSA Public Key.
10+
* Base class for an ECDSA Public Key.
11+
* This type is used with {@link PublicKeyAlgorithmTags#ECDSA} and the curve is identified by providing an OID.
12+
*
13+
* @see <a href="https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-13.html#name-algorithm-specific-part-for-ec">
14+
* Crypto-Refresh - Algorithm-Specific Parts for ECDSA Keys</a>
1115
*/
1216
public class ECDSAPublicBCPGKey
1317
extends ECPublicBCPGKey

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import org.bouncycastle.math.ec.ECPoint;
99

1010
/**
11-
* base class for an EC Public Key.
11+
* Base class for an EC Public Key.
12+
* For subclasses, see {@link ECDHPublicBCPGKey}, {@link ECDSAPublicBCPGKey} or {@link EdDSAPublicBCPGKey}.
1213
*/
1314
public abstract class ECPublicBCPGKey
1415
extends BCPGObject

0 commit comments

Comments
 (0)