mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-12-09 05:41:07 +01:00
Fix CachingBcPublicKeyDataDecryptorFactory
This commit is contained in:
parent
705e36080c
commit
8c0d096fc6
2 changed files with 126 additions and 14 deletions
|
|
@ -5,9 +5,15 @@
|
|||
package org.bouncycastle;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.operator.PGPDataDecryptor;
|
||||
import org.bouncycastle.openpgp.PGPPrivateKey;
|
||||
import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory;
|
||||
import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory;
|
||||
import org.bouncycastle.util.encoders.Base64;
|
||||
import org.bouncycastle.util.encoders.Hex;
|
||||
import org.pgpainless.decryption_verification.CustomPublicKeyDataDecryptorFactory;
|
||||
import org.pgpainless.key.SubkeyIdentifier;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
|
@ -20,36 +26,46 @@ import java.util.Map;
|
|||
* cache hits.
|
||||
* If no hit is found, the method call is delegated to the underlying {@link PublicKeyDataDecryptorFactory}.
|
||||
* The result of that is then placed in the cache and returned.
|
||||
*
|
||||
* TODO: Do we also cache invalid session keys?
|
||||
*/
|
||||
public class CachingPublicKeyDataDecryptorFactory implements PublicKeyDataDecryptorFactory {
|
||||
public class CachingBcPublicKeyDataDecryptorFactory
|
||||
extends BcPublicKeyDataDecryptorFactory
|
||||
implements CustomPublicKeyDataDecryptorFactory {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CachingBcPublicKeyDataDecryptorFactory.class);
|
||||
|
||||
private final Map<String, byte[]> cachedSessionKeys = new HashMap<>();
|
||||
private final PublicKeyDataDecryptorFactory factory;
|
||||
private final SubkeyIdentifier decryptionKey;
|
||||
|
||||
public CachingPublicKeyDataDecryptorFactory(PublicKeyDataDecryptorFactory factory) {
|
||||
this.factory = factory;
|
||||
public CachingBcPublicKeyDataDecryptorFactory(PGPPrivateKey privateKey, SubkeyIdentifier decryptionKey) {
|
||||
super(privateKey);
|
||||
this.decryptionKey = decryptionKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] recoverSessionData(int keyAlgorithm, byte[][] secKeyData) throws PGPException {
|
||||
byte[] sessionKey = lookup(secKeyData);
|
||||
byte[] sessionKey = lookupSessionKeyData(secKeyData);
|
||||
if (sessionKey == null) {
|
||||
sessionKey = factory.recoverSessionData(keyAlgorithm, secKeyData);
|
||||
cache(secKeyData, sessionKey);
|
||||
LOGGER.debug("Cache miss for encrypted session key " + Hex.toHexString(secKeyData[0]));
|
||||
sessionKey = costlyRecoverSessionData(keyAlgorithm, secKeyData);
|
||||
cacheSessionKeyData(secKeyData, sessionKey);
|
||||
} else {
|
||||
LOGGER.debug("Cache hit for encrypted session key " + Hex.toHexString(secKeyData[0]));
|
||||
}
|
||||
return sessionKey;
|
||||
}
|
||||
|
||||
private byte[] lookup(byte[][] secKeyData) {
|
||||
public byte[] costlyRecoverSessionData(int keyAlgorithm, byte[][] secKeyData) throws PGPException {
|
||||
return super.recoverSessionData(keyAlgorithm, secKeyData);
|
||||
}
|
||||
|
||||
private byte[] lookupSessionKeyData(byte[][] secKeyData) {
|
||||
byte[] sk = secKeyData[0];
|
||||
String key = Base64.toBase64String(sk);
|
||||
byte[] sessionKey = cachedSessionKeys.get(key);
|
||||
return copy(sessionKey);
|
||||
}
|
||||
|
||||
private void cache(byte[][] secKeyData, byte[] sessionKey) {
|
||||
private void cacheSessionKeyData(byte[][] secKeyData, byte[] sessionKey) {
|
||||
byte[] sk = secKeyData[0];
|
||||
String key = Base64.toBase64String(sk);
|
||||
cachedSessionKeys.put(key, copy(sessionKey));
|
||||
|
|
@ -69,7 +85,7 @@ public class CachingPublicKeyDataDecryptorFactory implements PublicKeyDataDecryp
|
|||
}
|
||||
|
||||
@Override
|
||||
public PGPDataDecryptor createDataDecryptor(boolean withIntegrityPacket, int encAlgorithm, byte[] key) throws PGPException {
|
||||
return null;
|
||||
public SubkeyIdentifier getSubkeyIdentifier() {
|
||||
return decryptionKey;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue