Skip to content

Commit cf490aa

Browse files
committed
relates to #1228 - cleaned up session key data, removed deprecated usages from test.
1 parent 48b5d70 commit cf490aa

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ public class SymmetricEncIntegrityPacket
1818

1919
version = in.read();
2020
}
21+
22+
public int getVersion()
23+
{
24+
return version;
25+
}
2126
}

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.io.InputStream;
44

5+
import org.bouncycastle.bcpg.AEADEncDataPacket;
56
import org.bouncycastle.bcpg.InputStreamPacket;
7+
import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket;
68
import org.bouncycastle.openpgp.operator.SessionKeyDataDecryptorFactory;
79

810
/**
@@ -11,30 +13,52 @@
1113
public class PGPSessionKeyEncryptedData
1214
extends PGPSymmetricKeyEncryptedData
1315
{
14-
private final PGPSessionKey sessionKey;
15-
1616
PGPSessionKeyEncryptedData(InputStreamPacket encData)
1717
{
1818
super(encData);
19-
this.sessionKey = null;
2019
}
2120

2221
@Override
2322
public int getAlgorithm()
2423
{
25-
return sessionKey.getAlgorithm();
24+
if (encData instanceof AEADEncDataPacket)
25+
{
26+
AEADEncDataPacket aeadData = (AEADEncDataPacket)encData;
27+
28+
return aeadData.getAlgorithm();
29+
}
30+
else
31+
{
32+
return -1; // unknown
33+
}
2634
}
2735

28-
public PGPSessionKey getSessionKey()
36+
@Override
37+
public int getVersion()
2938
{
30-
return sessionKey;
39+
if (encData instanceof AEADEncDataPacket)
40+
{
41+
AEADEncDataPacket aeadData = (AEADEncDataPacket)encData;
42+
43+
return aeadData.getVersion();
44+
}
45+
else if (encData instanceof SymmetricEncIntegrityPacket)
46+
{
47+
SymmetricEncIntegrityPacket symIntData = (SymmetricEncIntegrityPacket)encData;
48+
49+
return symIntData.getVersion();
50+
}
51+
else
52+
{
53+
return -1; // unmarked
54+
}
3155
}
3256

3357
public InputStream getDataStream(
3458
SessionKeyDataDecryptorFactory dataDecryptorFactory)
3559
throws PGPException
3660
{
37-
encStream = createDecryptionStream(dataDecryptorFactory, sessionKey);
61+
encStream = createDecryptionStream(dataDecryptorFactory, dataDecryptorFactory.getSessionKey());
3862

3963
return encStream;
4064
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private void verifyBcPublicKeyDecryptorFactoryFromSessionKeyCanDecryptDataSucces
150150
ArmoredInputStream msgArmorIn = new ArmoredInputStream(msgIn);
151151
PGPObjectFactory objectFactory = new BcPGPObjectFactory(msgArmorIn);
152152
PGPEncryptedDataList encryptedDataList = (PGPEncryptedDataList)objectFactory.nextObject();
153-
PGPPublicKeyEncryptedData encryptedData = (PGPPublicKeyEncryptedData)encryptedDataList.iterator().next();
153+
PGPSessionKeyEncryptedData encryptedData = encryptedDataList.extractSessionKeyEncryptedData();
154154

155155
SessionKeyDataDecryptorFactory decryptorFactory = new BcSessionKeyDataDecryptorFactory(new PGPSessionKey(PK_ENC_SESSIONKEY_ALG, Hex.decode(PK_ENC_SESSIONKEY)));
156156
InputStream decrypted = encryptedData.getDataStream(decryptorFactory);
@@ -172,7 +172,7 @@ private void verifyJcePublicKeyDecryptorFactoryFromSessionKeyCanDecryptDataSucce
172172
ArmoredInputStream msgArmorIn = new ArmoredInputStream(msgIn);
173173
PGPObjectFactory objectFactory = new BcPGPObjectFactory(msgArmorIn);
174174
PGPEncryptedDataList encryptedDataList = (PGPEncryptedDataList)objectFactory.nextObject();
175-
PGPPublicKeyEncryptedData encryptedData = (PGPPublicKeyEncryptedData)encryptedDataList.iterator().next();
175+
PGPSessionKeyEncryptedData encryptedData = encryptedDataList.extractSessionKeyEncryptedData();
176176

177177
SessionKeyDataDecryptorFactory decryptorFactory =
178178
new JceSessionKeyDataDecryptorFactoryBuilder().build(new PGPSessionKey(PK_ENC_SESSIONKEY_ALG, Hex.decode(PK_ENC_SESSIONKEY)));
@@ -240,7 +240,7 @@ private void verifyBcPBEDecryptorFactoryFromSessionKeyCanDecryptDataSuccessfully
240240

241241
PGPObjectFactory objectFactory = new BcPGPObjectFactory(msgArmorIn);
242242
PGPEncryptedDataList encryptedDataList = (PGPEncryptedDataList)objectFactory.nextObject();
243-
PGPPBEEncryptedData encryptedData = (PGPPBEEncryptedData)encryptedDataList.iterator().next();
243+
PGPSessionKeyEncryptedData encryptedData = encryptedDataList.extractSessionKeyEncryptedData();
244244

245245
SessionKeyDataDecryptorFactory decryptorFactory = new BcSessionKeyDataDecryptorFactory(new PGPSessionKey(PBE_ENC_SESSIONKEY_ALG, Hex.decode(PBE_ENC_SESSIONKEY)));
246246
InputStream decrypted = encryptedData.getDataStream(decryptorFactory);

0 commit comments

Comments
 (0)