diff --git a/pg/src/main/java/org/bouncycastle/bcpg/SymmetricEncIntegrityPacket.java b/pg/src/main/java/org/bouncycastle/bcpg/SymmetricEncIntegrityPacket.java index 8e8fac6653..0d82e7f1c8 100644 --- a/pg/src/main/java/org/bouncycastle/bcpg/SymmetricEncIntegrityPacket.java +++ b/pg/src/main/java/org/bouncycastle/bcpg/SymmetricEncIntegrityPacket.java @@ -18,4 +18,9 @@ public class SymmetricEncIntegrityPacket version = in.read(); } + + public int getVersion() + { + return version; + } } diff --git a/pg/src/main/java/org/bouncycastle/openpgp/PGPSessionKeyEncryptedData.java b/pg/src/main/java/org/bouncycastle/openpgp/PGPSessionKeyEncryptedData.java index ae885469b2..40c2cfaf36 100644 --- a/pg/src/main/java/org/bouncycastle/openpgp/PGPSessionKeyEncryptedData.java +++ b/pg/src/main/java/org/bouncycastle/openpgp/PGPSessionKeyEncryptedData.java @@ -2,7 +2,9 @@ import java.io.InputStream; +import org.bouncycastle.bcpg.AEADEncDataPacket; import org.bouncycastle.bcpg.InputStreamPacket; +import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket; import org.bouncycastle.openpgp.operator.SessionKeyDataDecryptorFactory; /** @@ -11,30 +13,52 @@ public class PGPSessionKeyEncryptedData extends PGPSymmetricKeyEncryptedData { - private final PGPSessionKey sessionKey; - PGPSessionKeyEncryptedData(InputStreamPacket encData) { super(encData); - this.sessionKey = null; } @Override public int getAlgorithm() { - return sessionKey.getAlgorithm(); + if (encData instanceof AEADEncDataPacket) + { + AEADEncDataPacket aeadData = (AEADEncDataPacket)encData; + + return aeadData.getAlgorithm(); + } + else + { + return -1; // unknown + } } - public PGPSessionKey getSessionKey() + @Override + public int getVersion() { - return sessionKey; + if (encData instanceof AEADEncDataPacket) + { + AEADEncDataPacket aeadData = (AEADEncDataPacket)encData; + + return aeadData.getVersion(); + } + else if (encData instanceof SymmetricEncIntegrityPacket) + { + SymmetricEncIntegrityPacket symIntData = (SymmetricEncIntegrityPacket)encData; + + return symIntData.getVersion(); + } + else + { + return -1; // unmarked + } } public InputStream getDataStream( SessionKeyDataDecryptorFactory dataDecryptorFactory) throws PGPException { - encStream = createDecryptionStream(dataDecryptorFactory, sessionKey); + encStream = createDecryptionStream(dataDecryptorFactory, dataDecryptorFactory.getSessionKey()); return encStream; } diff --git a/pg/src/test/java/org/bouncycastle/openpgp/test/PGPSessionKeyTest.java b/pg/src/test/java/org/bouncycastle/openpgp/test/PGPSessionKeyTest.java index 24081d9070..821fe02c2e 100644 --- a/pg/src/test/java/org/bouncycastle/openpgp/test/PGPSessionKeyTest.java +++ b/pg/src/test/java/org/bouncycastle/openpgp/test/PGPSessionKeyTest.java @@ -150,7 +150,7 @@ private void verifyBcPublicKeyDecryptorFactoryFromSessionKeyCanDecryptDataSucces ArmoredInputStream msgArmorIn = new ArmoredInputStream(msgIn); PGPObjectFactory objectFactory = new BcPGPObjectFactory(msgArmorIn); PGPEncryptedDataList encryptedDataList = (PGPEncryptedDataList)objectFactory.nextObject(); - PGPPublicKeyEncryptedData encryptedData = (PGPPublicKeyEncryptedData)encryptedDataList.iterator().next(); + PGPSessionKeyEncryptedData encryptedData = encryptedDataList.extractSessionKeyEncryptedData(); SessionKeyDataDecryptorFactory decryptorFactory = new BcSessionKeyDataDecryptorFactory(new PGPSessionKey(PK_ENC_SESSIONKEY_ALG, Hex.decode(PK_ENC_SESSIONKEY))); InputStream decrypted = encryptedData.getDataStream(decryptorFactory); @@ -172,7 +172,7 @@ private void verifyJcePublicKeyDecryptorFactoryFromSessionKeyCanDecryptDataSucce ArmoredInputStream msgArmorIn = new ArmoredInputStream(msgIn); PGPObjectFactory objectFactory = new BcPGPObjectFactory(msgArmorIn); PGPEncryptedDataList encryptedDataList = (PGPEncryptedDataList)objectFactory.nextObject(); - PGPPublicKeyEncryptedData encryptedData = (PGPPublicKeyEncryptedData)encryptedDataList.iterator().next(); + PGPSessionKeyEncryptedData encryptedData = encryptedDataList.extractSessionKeyEncryptedData(); SessionKeyDataDecryptorFactory decryptorFactory = new JceSessionKeyDataDecryptorFactoryBuilder().build(new PGPSessionKey(PK_ENC_SESSIONKEY_ALG, Hex.decode(PK_ENC_SESSIONKEY))); @@ -240,7 +240,7 @@ private void verifyBcPBEDecryptorFactoryFromSessionKeyCanDecryptDataSuccessfully PGPObjectFactory objectFactory = new BcPGPObjectFactory(msgArmorIn); PGPEncryptedDataList encryptedDataList = (PGPEncryptedDataList)objectFactory.nextObject(); - PGPPBEEncryptedData encryptedData = (PGPPBEEncryptedData)encryptedDataList.iterator().next(); + PGPSessionKeyEncryptedData encryptedData = encryptedDataList.extractSessionKeyEncryptedData(); SessionKeyDataDecryptorFactory decryptorFactory = new BcSessionKeyDataDecryptorFactory(new PGPSessionKey(PBE_ENC_SESSIONKEY_ALG, Hex.decode(PBE_ENC_SESSIONKEY))); InputStream decrypted = encryptedData.getDataStream(decryptorFactory);