Skip to content

Commit 49c371e

Browse files
committed
relates to #1228 - simplified use of PGPSessionKeyEncryptedData
1 parent cf9ef0d commit 49c371e

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,10 @@ public Iterator<PGPEncryptedData> iterator()
191191
* Create a decryption method using a {@link PGPSessionKey}. This method can be used to decrypt messages which do not
192192
* contain a SKESK or PKESK packet using a session key.
193193
*
194-
* @param sessionKey session key for message decryption
195194
* @return session key encrypted data
196195
*/
197-
public PGPSessionKeyEncryptedData makeSessionKeyEncryptedData(PGPSessionKey sessionKey)
196+
public PGPSessionKeyEncryptedData extractSessionKeyEncryptedData()
198197
{
199-
return new PGPSessionKeyEncryptedData(sessionKey, data);
198+
return new PGPSessionKeyEncryptedData(data);
200199
}
201200
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ public InputStream getDataStream(
112112
}
113113
}
114114

115+
/**
116+
* @deprecated will be removed in 1.74, use PGPEncryptedDataList.extractSessionKeyEncryptedData() and then apply the dataDecryptorFactory.
117+
*/
115118
public InputStream getDataStream(
116119
SessionKeyDataDecryptorFactory dataDecryptorFactory)
117120
throws PGPException

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ public InputStream getDataStream(
108108
return getDataStream(dataDecryptorFactory, getSessionKey(dataDecryptorFactory));
109109
}
110110

111+
/**
112+
* @deprecated will be removed in 1.74, use PGPEncryptedDataList.extractSessionKeyEncryptedData() and then apply the dataDecryptorFactory.
113+
*/
111114
public InputStream getDataStream(
112115
SessionKeyDataDecryptorFactory dataDecryptorFactory)
113116
throws PGPException

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
import org.bouncycastle.bcpg.InputStreamPacket;
66
import org.bouncycastle.openpgp.operator.SessionKeyDataDecryptorFactory;
77

8+
/**
9+
* The basis of PGP encrypted data - encrypted data encrypted using a symmetric session key.
10+
*/
811
public class PGPSessionKeyEncryptedData
912
extends PGPSymmetricEncryptedData
1013
{
1114
private final PGPSessionKey sessionKey;
1215

13-
PGPSessionKeyEncryptedData(PGPSessionKey sessionKey, InputStreamPacket encData)
16+
PGPSessionKeyEncryptedData(InputStreamPacket encData)
1417
{
1518
super(encData);
16-
this.sessionKey = sessionKey;
19+
this.sessionKey = null;
1720
}
1821

1922
@Override

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,9 @@ private void verifyJcePBEDecryptorFactoryFromSessionKeyCanDecryptDataSuccessfull
214214

215215
PGPObjectFactory objectFactory = new BcPGPObjectFactory(msgArmorIn);
216216
PGPEncryptedDataList encryptedDataList = (PGPEncryptedDataList)objectFactory.nextObject();
217-
PGPPBEEncryptedData encryptedData = (PGPPBEEncryptedData)encryptedDataList.iterator().next();
217+
PGPSessionKeyEncryptedData encryptedData = encryptedDataList.extractSessionKeyEncryptedData();
218218

219-
SessionKeyDataDecryptorFactory decryptorFactory = new JceSessionKeyDataDecryptorFactoryBuilder().build(
220-
new PGPSessionKey(PBE_ENC_SESSIONKEY_ALG, Hex.decode(PBE_ENC_SESSIONKEY)));
219+
SessionKeyDataDecryptorFactory decryptorFactory = new JceSessionKeyDataDecryptorFactoryBuilder().build(new PGPSessionKey(PBE_ENC_SESSIONKEY_ALG, Hex.decode(PBE_ENC_SESSIONKEY)));
221220
InputStream decrypted = encryptedData.getDataStream(decryptorFactory);
222221

223222
objectFactory = new BcPGPObjectFactory(decrypted);
@@ -292,9 +291,10 @@ private void decryptMessageWithoutEskUsingSessionKey()
292291
isEquals(0, encryptedData.size()); // there is no encrypted session key packet
293292

294293
// Add decryption method using a session key
295-
PGPSessionKeyEncryptedData sessionKeyEncData = encryptedData.makeSessionKeyEncryptedData(sessionKey);
294+
PGPSessionKeyEncryptedData sessionKeyEncData = encryptedData.extractSessionKeyEncryptedData();
296295

297296
SessionKeyDataDecryptorFactory decryptorFactory = new BcSessionKeyDataDecryptorFactory(sessionKey);
297+
298298
InputStream decrypted = sessionKeyEncData.getDataStream(decryptorFactory);
299299

300300
objectFactory = new BcPGPObjectFactory(decrypted);

0 commit comments

Comments
 (0)