Skip to content

Commit 8ff3e17

Browse files
author
gefeili
committed
Use PGPDataEncryptorBuilder as parameter in PGPKeyEncryptionMethodGenerator.generate
1 parent dbe7b7e commit 8ff3e17

File tree

4 files changed

+16
-49
lines changed

4 files changed

+16
-49
lines changed

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,16 @@
99
import org.bouncycastle.bcpg.AEADEncDataPacket;
1010
import org.bouncycastle.bcpg.BCPGHeaderObject;
1111
import org.bouncycastle.bcpg.BCPGOutputStream;
12-
import org.bouncycastle.bcpg.ContainedPacket;
1312
import org.bouncycastle.bcpg.HashAlgorithmTags;
1413
import org.bouncycastle.bcpg.PacketTags;
15-
import org.bouncycastle.bcpg.PublicKeyEncSessionPacket;
16-
import org.bouncycastle.bcpg.SymmetricEncDataPacket;
1714
import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket;
1815
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
19-
import org.bouncycastle.bcpg.SymmetricKeyEncSessionPacket;
2016
import org.bouncycastle.openpgp.operator.PBEKeyEncryptionMethodGenerator;
2117
import org.bouncycastle.openpgp.operator.PGPAEADDataEncryptor;
2218
import org.bouncycastle.openpgp.operator.PGPDataEncryptor;
2319
import org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder;
2420
import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
2521
import org.bouncycastle.openpgp.operator.PGPKeyEncryptionMethodGenerator;
26-
import org.bouncycastle.openpgp.operator.PublicKeyKeyEncryptionMethodGenerator;
2722
import org.bouncycastle.util.io.TeeOutputStream;
2823

2924
/**
@@ -259,26 +254,10 @@ else if (directS2K)
259254
PGPDataEncryptor dataEncryptor = dataEncryptorBuilder.build(messageKey);
260255
digestCalc = dataEncryptor.getIntegrityCalculator();
261256
BCPGHeaderObject encOut;
262-
int version = (dataEncryptor instanceof PGPAEADDataEncryptor ? (isV5StyleAEAD ? 5 : 6) : 4); // OpenPGP v4, v5 or v6
263257
for (int i = 0; i < methods.size(); i++)
264258
{
265259
PGPKeyEncryptionMethodGenerator method = methods.get(i);
266-
int packetVersion = 0;
267-
if (method instanceof PBEKeyEncryptionMethodGenerator)
268-
{
269-
PBEKeyEncryptionMethodGenerator mGen = (PBEKeyEncryptionMethodGenerator)method;
270-
mGen.setKekAlgorithm(mGen.getSessionKeyWrapperAlgorithm(defAlgorithm));
271-
if (version >= 5)
272-
{
273-
mGen.setAEADAlgorithm(dataEncryptorBuilder.getAeadAlgorithm());
274-
}
275-
packetVersion = version;
276-
}
277-
else if (method instanceof PublicKeyKeyEncryptionMethodGenerator)
278-
{
279-
packetVersion = version != 6 ? PublicKeyEncSessionPacket.VERSION_3 : PublicKeyEncSessionPacket.VERSION_6;
280-
}
281-
pOut.writePacket(method.generate(packetVersion, sessionInfo));
260+
pOut.writePacket(method.generate(dataEncryptorBuilder, sessionInfo));
282261
}
283262
try
284263
{
@@ -292,14 +271,14 @@ else if (method instanceof PublicKeyKeyEncryptionMethodGenerator)
292271
{
293272
byte[] iv = aeadDataEncryptor.getIV();
294273
encOut = new AEADEncDataPacket(
295-
dataEncryptorBuilder.getAlgorithm(), aeadDataEncryptor.getAEADAlgorithm(), aeadDataEncryptor.getChunkSize(), iv);
274+
defAlgorithm, aeadDataEncryptor.getAEADAlgorithm(), aeadDataEncryptor.getChunkSize(), iv);
296275
ivOrSaltLen = iv.length;
297276
}
298277
else // data is encrypted by v2 SEIPD (AEAD), so write v6 SKESK packet
299278
{
300279
//AEAD(HKDF(S2K(passphrase), info), secrets, packetprefix)
301280
encOut = SymmetricEncIntegrityPacket.createVersion2Packet(
302-
dataEncryptorBuilder.getAlgorithm(),
281+
defAlgorithm,
303282
aeadDataEncryptor.getAEADAlgorithm(),
304283
aeadDataEncryptor.getChunkSize(),
305284
salt);

pg/src/main/java/org/bouncycastle/openpgp/operator/PBEKeyEncryptionMethodGenerator.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,6 @@ public PBEKeyEncryptionMethodGenerator setSessionKeyWrapperAlgorithm(int wrapAlg
117117
return this;
118118
}
119119

120-
/**
121-
* the {@link SymmetricKeyAlgorithmTags encryption algorithm} being used to wrap the session key
122-
* */
123-
public PBEKeyEncryptionMethodGenerator setKekAlgorithm(int kekAlgorithm)
124-
{
125-
this.kekAlgorithm = kekAlgorithm;
126-
return this;
127-
}
128-
129-
public PBEKeyEncryptionMethodGenerator setAEADAlgorithm(int aeadAlgorithm)
130-
{
131-
this.aeadAlgorithm = aeadAlgorithm;
132-
return this;
133-
}
134-
135120
/**
136121
* Return the key wrapping algorithm this PBE key method is associated with.
137122
*
@@ -194,7 +179,8 @@ public byte[] getKey(int encAlgorithm)
194179
* SKESKv6 packets are used with Symmetrically-Encrypted Integrity-Protected Data (SEIPD) packets of
195180
* version 2 only.
196181
* A SKESKv6 packet MUST NOT precede a SEIPDv1, OED or SED packet.
197-
* @param sessionInfo session data generated by the encrypted data generator.
182+
*
183+
* @param sessionInfo session data generated by the encrypted data generator.
198184
* @return a packet encoding the provided information and the configuration of this instance.
199185
* @throws PGPException if an error occurs constructing the packet.
200186
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-version-4-symmetric-key-enc">
@@ -204,10 +190,11 @@ public byte[] getKey(int encAlgorithm)
204190
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-version-6-symmetric-key-enc">
205191
* RFC9580 - Symmetric-Key Encrypted Session-Key Packet version 6</a>
206192
*/
207-
public ContainedPacket generate(int version, byte[] sessionInfo)
193+
public ContainedPacket generate(PGPDataEncryptorBuilder dataEncryptorBuilder, byte[] sessionInfo)
208194
throws PGPException
209195
{
210-
if (version == SymmetricKeyEncSessionPacket.VERSION_4)
196+
int kekAlgorithm = getSessionKeyWrapperAlgorithm(dataEncryptorBuilder.getAlgorithm());
197+
if (dataEncryptorBuilder.getAeadAlgorithm() <= 0)
211198
{
212199
if (sessionInfo == null)
213200
{
@@ -224,8 +211,10 @@ public ContainedPacket generate(int version, byte[] sessionInfo)
224211

225212
return SymmetricKeyEncSessionPacket.createV4Packet(kekAlgorithm, s2k, encryptSessionInfo(kekAlgorithm, key, nSessionInfo));
226213
}
227-
else if (version == SymmetricKeyEncSessionPacket.VERSION_5 || version == SymmetricKeyEncSessionPacket.VERSION_6)
214+
else
228215
{
216+
int aeadAlgorithm = dataEncryptorBuilder.getAeadAlgorithm();
217+
int version = dataEncryptorBuilder.isV5StyleAEAD() ? SymmetricKeyEncSessionPacket.VERSION_5 : SymmetricKeyEncSessionPacket.VERSION_6;
229218
byte[] ikm = getKey(kekAlgorithm);
230219
byte[] info = new byte[]{
231220
(byte)0xC3,
@@ -257,7 +246,6 @@ else if (version == SymmetricKeyEncSessionPacket.VERSION_5 || version == Symmetr
257246
return SymmetricKeyEncSessionPacket.createV6Packet(kekAlgorithm, aeadAlgorithm, iv, s2k, esk, tag);
258247
}
259248
}
260-
throw new PGPException("Unexpected version number");
261249
}
262250

263251
protected byte[] getSessionKey(byte[] sessionInfo)

pg/src/main/java/org/bouncycastle/openpgp/operator/PGPKeyEncryptionMethodGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
public interface PGPKeyEncryptionMethodGenerator
1111
{
12-
ContainedPacket generate(int version, byte[] sessionInfo)
12+
13+
ContainedPacket generate(PGPDataEncryptorBuilder dataEncryptorBuilder, byte[] sessionInfo)
1314
throws PGPException;
1415
}

pg/src/main/java/org/bouncycastle/openpgp/operator/PublicKeyKeyEncryptionMethodGenerator.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ private byte[] convertToEncodedMPI(byte[] encryptedSessionInfo)
184184
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-version-3-public-key-encryp">
185185
* RFC9580 - Version 3 Public Key Encrypted Session Key Packet</a>
186186
*/
187-
public ContainedPacket generate(int version, byte[] sessionInfo)
187+
public ContainedPacket generate(PGPDataEncryptorBuilder dataEncryptorBuilder, byte[] sessionInfo)
188188
throws PGPException
189189
{
190-
if (version == PublicKeyEncSessionPacket.VERSION_3)
190+
if (dataEncryptorBuilder.getAeadAlgorithm() <= 0 || dataEncryptorBuilder.isV5StyleAEAD())
191191
{
192192
long keyId;
193193
if (useWildcardRecipient)
@@ -202,7 +202,7 @@ public ContainedPacket generate(int version, byte[] sessionInfo)
202202
byte[][] encodedEncSessionInfo = encodeEncryptedSessionInfo(encryptedSessionInfo);
203203
return PublicKeyEncSessionPacket.createV3PKESKPacket(keyId, pubKey.getAlgorithm(), encodedEncSessionInfo);
204204
}
205-
else if (version == PublicKeyEncSessionPacket.VERSION_6)
205+
else
206206
{
207207
byte[] keyFingerprint;
208208
int keyVersion;
@@ -224,7 +224,6 @@ else if (version == PublicKeyEncSessionPacket.VERSION_6)
224224
byte[][] encodedEncSessionInfo = encodeEncryptedSessionInfo(encryptedSessionInfo);
225225
return PublicKeyEncSessionPacket.createV6PKESKPacket(keyVersion, keyFingerprint, pubKey.getAlgorithm(), encodedEncSessionInfo);
226226
}
227-
throw new PGPException("Unexpected version number");
228227
}
229228

230229
/**

0 commit comments

Comments
 (0)