Skip to content

Commit

Permalink
relates to #1228 - cleaned up session key data, removed deprecated us…
Browse files Browse the repository at this point in the history
…ages from test.
  • Loading branch information
dghgit committed Oct 5, 2022
1 parent e053171 commit 0f2b0ea
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public class SymmetricEncIntegrityPacket

version = in.read();
}

public int getVersion()
{
return version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)));
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0f2b0ea

Please sign in to comment.