Skip to content

Commit dc12e70

Browse files
committed
Merge branch 'main' of gitlab.cryptoworkshop.com:root/bc-java
2 parents dc75309 + 1fd1d9a commit dc12e70

5 files changed

Lines changed: 58 additions & 43 deletions

File tree

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

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ public class SymmetricKeyEncSessionPacket
2929
*/
3030
public static final int VERSION_6 = 6;
3131

32-
private int version; // V4, V5, V6
33-
private int encAlgorithm; // V4, V5, V6
34-
private S2K s2k; // V4,
35-
// array for exposing raw S2K parameters. Useful for forwards compat.
36-
private byte[] s2kBytes; // Makes only sense for v6, since there we have a counter
37-
private byte[] secKeyData; // V4, V5, V6
38-
private int aeadAlgorithm; // V5, V6
39-
private byte[] iv; // V5, V6
40-
private byte[] authTag; // V5, V6
32+
private final int version; // V4, V5, V6
33+
private final int encAlgorithm; // V4, V5, V6
34+
private final int aeadAlgorithm; // V5, V6
35+
private S2K s2k; // V4, V5, V6
36+
private byte[] secKeyData; // V4, V5, V6
37+
private byte[] iv; // V5, V6
38+
private byte[] authTag; // V5, V6
4139

4240
public SymmetricKeyEncSessionPacket(
4341
BCPGInputStream in)
@@ -57,6 +55,7 @@ public SymmetricKeyEncSessionPacket(
5755
if (version == VERSION_4)
5856
{
5957
encAlgorithm = in.read();
58+
aeadAlgorithm = 0;
6059

6160
s2k = new S2K(in);
6261

@@ -207,6 +206,7 @@ public SymmetricKeyEncSessionPacket(
207206

208207
this.version = VERSION_4;
209208
this.encAlgorithm = encAlgorithm;
209+
this.aeadAlgorithm = 0;
210210
this.s2k = s2k;
211211
this.secKeyData = secKeyData;
212212
}
@@ -339,51 +339,63 @@ public void encode(
339339
BCPGOutputStream out)
340340
throws IOException
341341
{
342+
PacketFormat packetFormat = version > 4 ? PacketFormat.CURRENT : PacketFormat.ROUNDTRIP;
343+
342344
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
343-
BCPGOutputStream pOut;
344-
if (version == 4)
345-
{
346-
pOut = new BCPGOutputStream(bOut);
347-
}
348-
else
349-
{
350-
pOut = new BCPGOutputStream(bOut, true);
351-
}
345+
BCPGOutputStream pOut = new BCPGOutputStream(bOut, packetFormat);
352346

353347
pOut.write(version);
354-
if (version == VERSION_4)
348+
349+
switch (version)
350+
{
351+
case VERSION_4:
355352
{
356353
pOut.write(encAlgorithm);
357-
pOut.writeObject(s2k);
354+
s2k.encode(pOut);
358355

359356
if (secKeyData != null && secKeyData.length > 0)
360357
{
361358
pOut.write(secKeyData);
362359
}
360+
break;
363361
}
364-
else
362+
case VERSION_5:
365363
{
366-
int s2kLen = 0;
367-
if (version == VERSION_6)
368-
{
369-
s2kLen = s2k.getEncoded().length;
370-
int count = 1 + 1 + 1 + s2kLen + iv.length;
371-
pOut.write(count); // len of 5 following fields
372-
}
373364
pOut.write(encAlgorithm);
374365
pOut.write(aeadAlgorithm);
375-
if (version == VERSION_6)
366+
s2k.encode(pOut);
367+
pOut.write(iv);
368+
369+
if (secKeyData != null && secKeyData.length > 0)
376370
{
377-
pOut.write(s2kLen);
371+
pOut.write(secKeyData);
378372
}
379-
pOut.writeObject(s2k);
373+
374+
pOut.write(authTag);
375+
break;
376+
}
377+
case VERSION_6:
378+
{
379+
byte[] s2kEncoded = s2k.getEncoded();
380+
int count = 1 + 1 + 1 + s2kEncoded.length + iv.length; // len of 5 following fields
381+
382+
pOut.write(count);
383+
pOut.write(encAlgorithm);
384+
pOut.write(aeadAlgorithm);
385+
pOut.write(s2kEncoded.length);
386+
pOut.write(s2kEncoded);
380387
pOut.write(iv);
381388

382389
if (secKeyData != null && secKeyData.length > 0)
383390
{
384391
pOut.write(secKeyData);
385392
}
393+
386394
pOut.write(authTag);
395+
break;
396+
}
397+
default:
398+
throw new IllegalStateException();
387399
}
388400

389401
pOut.close();

pg/src/main/java/org/bouncycastle/openpgp/PGPObjectFactory.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,8 @@ public PGPObjectFactory(
8383
public Object nextObject()
8484
throws IOException
8585
{
86-
List l;
87-
88-
switch (in.nextPacketTag())
86+
int tag = in.nextPacketTag();
87+
switch (tag)
8988
{
9089
case -1:
9190
return null;
@@ -96,7 +95,8 @@ public Object nextObject()
9695
case PacketTags.EXPERIMENTAL_4:
9796
return in.readPacket();
9897
case PacketTags.SIGNATURE:
99-
l = new ArrayList();
98+
{
99+
ArrayList l = new ArrayList();
100100

101101
while (in.nextPacketTag() == PacketTags.SIGNATURE)
102102
{
@@ -117,6 +117,7 @@ public Object nextObject()
117117
}
118118

119119
return new PGPSignatureList((PGPSignature[])l.toArray(new PGPSignature[l.size()]));
120+
}
120121
case PacketTags.SECRET_KEY:
121122
try
122123
{
@@ -150,7 +151,8 @@ public Object nextObject()
150151
case PacketTags.AEAD_ENC_DATA:
151152
return new PGPEncryptedDataList(in);
152153
case PacketTags.ONE_PASS_SIGNATURE:
153-
l = new ArrayList();
154+
{
155+
ArrayList l = new ArrayList();
154156

155157
while (in.nextPacketTag() == PacketTags.ONE_PASS_SIGNATURE)
156158
{
@@ -165,24 +167,23 @@ public Object nextObject()
165167
}
166168

167169
return new PGPOnePassSignatureList((PGPOnePassSignature[])l.toArray(new PGPOnePassSignature[l.size()]));
170+
}
168171
case PacketTags.MARKER:
169172
return new PGPMarker(in);
170173
case PacketTags.PADDING:
171174
return new PGPPadding(in);
172175
case PacketTags.MOD_DETECTION_CODE:
173-
return new UnknownPacket(PacketTags.MOD_DETECTION_CODE, in);
174176
case PacketTags.USER_ID:
175-
return new UnknownPacket(PacketTags.USER_ID, in);
176177
case PacketTags.USER_ATTRIBUTE:
177-
return new UnknownPacket(PacketTags.USER_ATTRIBUTE, in);
178+
return new UnknownPacket(tag, in);
178179
}
179180

180-
int tag = in.nextPacketTag();
181+
int nextTag = in.nextPacketTag();
181182
UnknownPacket unknownPacket = (UnknownPacket)in.readPacket();
182183
if (throwForUnknownCriticalPackets && unknownPacket.isCritical())
183184
{
184185
// Leave the error message intact for backwards compatibility
185-
throw new IOException("unknown object in stream: " + tag);
186+
throw new IOException("unknown object in stream: " + nextTag);
186187
}
187188
return unknownPacket;
188189
}

pg/src/main/java/org/bouncycastle/openpgp/PGPPadding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public PGPPadding(int len)
9595
*/
9696
public PGPPadding(int len, SecureRandom random)
9797
{
98-
this.p = new PaddingPacket(len, random);
98+
this.p = new PaddingPacket(len, CryptoServicesRegistrar.getSecureRandom(random));
9999
}
100100

101101
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ private void knownV5TestVectorDecryptionTests()
183183
private void knownV6TestVectorDecryptionTests()
184184
throws IOException, PGPException
185185
{
186-
// Test known-good V6 test vectors TODO: decryption tests
186+
// Test known-good V6 test vectors
187187
testBcDecryption(V6_EAX_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
188188
testBcDecryption(V6_OCB_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
189189
testBcDecryption(V6_GCM_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);

pkix/src/test/java/org/bouncycastle/openssl/test/AllTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public void testPKCS8Encrypted()
8484

8585
PrivateKey key = kpGen.generateKeyPair().getPrivate();
8686

87+
encryptedTestNew(key, PKCS8Generator.AES_128_CBC);
88+
encryptedTestNew(key, PKCS8Generator.AES_192_CBC);
8789
encryptedTestNew(key, PKCS8Generator.AES_256_CBC);
8890
encryptedTestNew(key, PKCS8Generator.DES3_CBC);
8991
encryptedTestNew(key, PKCS8Generator.PBE_SHA1_3DES);

0 commit comments

Comments
 (0)