Skip to content

Commit 013d141

Browse files
committed
PGPEncryptedDataGenerator: Change forceSessionKey default to true and prevent direct-s2k with SEIPDv2 packets
Fixes #1913
1 parent db6161f commit 013d141

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public class PGPEncryptedDataGenerator
8888
private SecureRandom rand;
8989
// If true, force generation of a session key, even if we only have a single password-based encryption method
9090
// and could therefore use the S2K output as session key directly.
91-
private boolean forceSessionKey = false;
91+
private boolean forceSessionKey = true;
9292

9393
/**
9494
* Base constructor.
@@ -121,7 +121,9 @@ public PGPEncryptedDataGenerator(PGPDataEncryptorBuilder encryptorBuilder, boole
121121
* Some versions of PGP always expect a session key, this will force use
122122
* of a session key even if a single PBE encryptor is provided.
123123
*
124-
* @param forceSessionKey true if a session key should always be used, default is false.
124+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#section-5.3.1-4">
125+
* RFC9580 - Description of the optional encrypted session key field</a>
126+
* @param forceSessionKey true if a session key should always be used, default is true.
125127
*/
126128
public void setForceSessionKey(boolean forceSessionKey)
127129
{
@@ -223,7 +225,9 @@ private OutputStream open(
223225

224226
boolean directS2K = !forceSessionKey && methods.size() == 1 &&
225227
methods.get(0) instanceof PBEKeyEncryptionMethodGenerator;
226-
if (directS2K)
228+
boolean isV5StyleAEAD = dataEncryptorBuilder.isV5StyleAEAD();
229+
boolean isSEIPv2 = dataEncryptorBuilder.getAeadAlgorithm() != -1 && !isV5StyleAEAD;
230+
if (directS2K && !isSEIPv2)
227231
{
228232
sessionKey = ((PBEKeyEncryptionMethodGenerator)methods.get(0)).getKey(defAlgorithm);
229233
sessionInfo = null; // null indicates direct use of S2K output as sessionKey/messageKey
@@ -238,8 +242,7 @@ private OutputStream open(
238242

239243
// In OpenPGP v6, we need an additional step to derive a message key and IV from the session info.
240244
// Since we cannot inject the IV into the data encryptor, we append it to the message key.
241-
boolean isV5StyleAEAD = dataEncryptorBuilder.isV5StyleAEAD();
242-
if (dataEncryptorBuilder.getAeadAlgorithm() != -1 && !isV5StyleAEAD)
245+
if (isSEIPv2)
243246
{
244247
byte[] info = SymmetricEncIntegrityPacket.createAAData(
245248
SymmetricEncIntegrityPacket.VERSION_2,

0 commit comments

Comments
 (0)