1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-26 14:22:05 +01:00

Adopt new PublicKeyDataDecryptorFactory API

This commit is contained in:
Paul Schaub 2024-08-11 13:40:49 +02:00
parent 0196e3ce65
commit 4911816b7d
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 11 additions and 7 deletions

View file

@ -27,10 +27,11 @@ class CachingBcPublicKeyDataDecryptorFactory(
override fun recoverSessionData( override fun recoverSessionData(
keyAlgorithm: Int, keyAlgorithm: Int,
secKeyData: Array<out ByteArray> secKeyData: Array<out ByteArray>,
pkeskVersion: Int
): ByteArray = ): ByteArray =
lookupSessionKeyData(secKeyData) lookupSessionKeyData(secKeyData)
?: costlyRecoverSessionData(keyAlgorithm, secKeyData).also { ?: costlyRecoverSessionData(keyAlgorithm, secKeyData, pkeskVersion).also {
cacheSessionKeyData(secKeyData, it) cacheSessionKeyData(secKeyData, it)
} }
@ -39,8 +40,9 @@ class CachingBcPublicKeyDataDecryptorFactory(
private fun costlyRecoverSessionData( private fun costlyRecoverSessionData(
keyAlgorithm: Int, keyAlgorithm: Int,
secKeyData: Array<out ByteArray> secKeyData: Array<out ByteArray>,
): ByteArray = super.recoverSessionData(keyAlgorithm, secKeyData) pkeskVersion: Int
): ByteArray = super.recoverSessionData(keyAlgorithm, secKeyData, pkeskVersion)
private fun cacheSessionKeyData(secKeyData: Array<out ByteArray>, sessionKey: ByteArray) { private fun cacheSessionKeyData(secKeyData: Array<out ByteArray>, sessionKey: ByteArray) {
cachedSessions[toKey(secKeyData)] = sessionKey.clone() cachedSessions[toKey(secKeyData)] = sessionKey.clone()

View file

@ -9,6 +9,7 @@ import org.bouncycastle.bcpg.AEADEncDataPacket
import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket
import org.bouncycastle.openpgp.PGPException import org.bouncycastle.openpgp.PGPException
import org.bouncycastle.openpgp.PGPSessionKey import org.bouncycastle.openpgp.PGPSessionKey
import org.bouncycastle.openpgp.operator.AbstractPublicKeyDataDecryptorFactory
import org.bouncycastle.openpgp.operator.PGPDataDecryptor import org.bouncycastle.openpgp.operator.PGPDataDecryptor
import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory
import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory
@ -44,7 +45,7 @@ class HardwareSecurity {
class HardwareDataDecryptorFactory( class HardwareDataDecryptorFactory(
override val subkeyIdentifier: SubkeyIdentifier, override val subkeyIdentifier: SubkeyIdentifier,
private val callback: DecryptionCallback, private val callback: DecryptionCallback,
) : CustomPublicKeyDataDecryptorFactory { ) : AbstractPublicKeyDataDecryptorFactory(), CustomPublicKeyDataDecryptorFactory {
// luckily we can instantiate the BcPublicKeyDataDecryptorFactory with null as argument. // luckily we can instantiate the BcPublicKeyDataDecryptorFactory with null as argument.
private val factory: PublicKeyDataDecryptorFactory = BcPublicKeyDataDecryptorFactory(null) private val factory: PublicKeyDataDecryptorFactory = BcPublicKeyDataDecryptorFactory(null)
@ -73,7 +74,8 @@ class HardwareSecurity {
override fun recoverSessionData( override fun recoverSessionData(
keyAlgorithm: Int, keyAlgorithm: Int,
secKeyData: Array<out ByteArray> secKeyData: Array<out ByteArray>,
pkeskVersion: Int
): ByteArray { ): ByteArray {
return try { return try {
callback.decryptSessionKey(subkeyIdentifier.subkeyId, keyAlgorithm, secKeyData[0]) callback.decryptSessionKey(subkeyIdentifier.subkeyId, keyAlgorithm, secKeyData[0])

View file

@ -62,7 +62,7 @@ public class CustomPublicKeyDataDecryptorFactoryTest {
PGPSecretKey decryptionKey = secretKey.getSecretKey(encryptionKey.getKeyID()); PGPSecretKey decryptionKey = secretKey.getSecretKey(encryptionKey.getKeyID());
PGPPrivateKey privateKey = UnlockSecretKey.unlockSecretKey(decryptionKey, Passphrase.emptyPassphrase()); PGPPrivateKey privateKey = UnlockSecretKey.unlockSecretKey(decryptionKey, Passphrase.emptyPassphrase());
PublicKeyDataDecryptorFactory internal = new BcPublicKeyDataDecryptorFactory(privateKey); PublicKeyDataDecryptorFactory internal = new BcPublicKeyDataDecryptorFactory(privateKey);
return internal.recoverSessionData(keyAlgorithm, new byte[][] {sessionKeyData}); return internal.recoverSessionData(keyAlgorithm, new byte[][] {sessionKeyData}, 3);
} catch (PGPException e) { } catch (PGPException e) {
throw new HardwareSecurity.HardwareSecurityException(); throw new HardwareSecurity.HardwareSecurityException();
} }