From f0e59ecef5da6c50487b2568daef9c37832369dc Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Thu, 3 Aug 2023 14:48:57 +0200 Subject: [PATCH] EncryptionOptions: Allow overriding evaluation date for recipient keys --- .../encryption_signing/EncryptionOptions.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionOptions.java b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionOptions.java index e2e8cf5a..bb937a1a 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionOptions.java +++ b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionOptions.java @@ -68,6 +68,7 @@ public class EncryptionOptions { private final Map keyViews = new HashMap<>(); private final EncryptionKeySelector encryptionKeySelector = encryptToAllCapableSubkeys(); private boolean allowEncryptionWithMissingKeyFlags = false; + private Date evaluationDate = new Date(); private SymmetricKeyAlgorithm encryptionAlgorithmOverride = null; @@ -96,6 +97,17 @@ public class EncryptionOptions { return new EncryptionOptions(); } + /** + * Override the evaluation date for recipient keys with the given date. + * + * @param evaluationDate new evaluation date + * @return this + */ + public EncryptionOptions setEvaluationDate(@Nonnull Date evaluationDate) { + this.evaluationDate = evaluationDate; + return this; + } + /** * Factory method to create an {@link EncryptionOptions} object which will encrypt for keys * which carry the flag {@link org.pgpainless.algorithm.KeyFlag#ENCRYPT_COMMS}. @@ -141,7 +153,7 @@ public class EncryptionOptions { * @return encryption options */ public EncryptionOptions addAuthenticatableRecipients(String userId, boolean email, CertificateAuthority authority, int targetAmount) { - List identifiedCertificates = authority.lookupByUserId(userId, email, new Date(), targetAmount); + List identifiedCertificates = authority.lookupByUserId(userId, email, evaluationDate, targetAmount); boolean foundAcceptable = false; for (CertificateAuthenticity candidate : identifiedCertificates) { if (candidate.isAuthenticated()) { @@ -213,7 +225,7 @@ public class EncryptionOptions { public EncryptionOptions addRecipient(@Nonnull PGPPublicKeyRing key, @Nonnull CharSequence userId, @Nonnull EncryptionKeySelector encryptionKeySelectionStrategy) { - KeyRingInfo info = new KeyRingInfo(key, new Date()); + KeyRingInfo info = new KeyRingInfo(key, evaluationDate); List encryptionSubkeys = encryptionKeySelectionStrategy .selectEncryptionSubkeys(info.getEncryptionSubkeys(userId.toString(), purpose)); @@ -277,7 +289,6 @@ public class EncryptionOptions { } private EncryptionOptions addAsRecipient(PGPPublicKeyRing key, EncryptionKeySelector encryptionKeySelectionStrategy, boolean wildcardKeyId) { - Date evaluationDate = new Date(); KeyRingInfo info = new KeyRingInfo(key, evaluationDate); Date primaryKeyExpiration;