1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-07 04:41:12 +01:00

Add method to allow for encryption for keys with missing keyflags.

There are legacy keys around, which do not carry any key flags.
This commit adds a method to EncryptionOptions that allow PGPainless to encrypt
for such keys.

Fixes #400
This commit is contained in:
Paul Schaub 2023-08-03 14:04:40 +02:00
parent 0d8db24b1a
commit 975d59c5a9
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 220 additions and 3 deletions

View file

@ -67,6 +67,7 @@ public class EncryptionOptions {
private final Map<SubkeyIdentifier, KeyRingInfo> keyRingInfo = new HashMap<>();
private final Map<SubkeyIdentifier, KeyAccessor> keyViews = new HashMap<>();
private final EncryptionKeySelector encryptionKeySelector = encryptToAllCapableSubkeys();
private boolean allowEncryptionWithMissingKeyFlags = false;
private SymmetricKeyAlgorithm encryptionAlgorithmOverride = null;
@ -277,8 +278,7 @@ public class EncryptionOptions {
private EncryptionOptions addAsRecipient(PGPPublicKeyRing key, EncryptionKeySelector encryptionKeySelectionStrategy, boolean wildcardKeyId) {
Date evaluationDate = new Date();
KeyRingInfo info;
info = new KeyRingInfo(key, evaluationDate);
KeyRingInfo info = new KeyRingInfo(key, evaluationDate);
Date primaryKeyExpiration;
try {
@ -292,6 +292,23 @@ public class EncryptionOptions {
List<PGPPublicKey> encryptionSubkeys = encryptionKeySelectionStrategy
.selectEncryptionSubkeys(info.getEncryptionSubkeys(purpose));
// There are some legacy keys around without key flags.
// If we allow encryption for those keys, we add valid keys without any key flags, if they are
// capable of encryption by means of their algorithm
if (encryptionSubkeys.isEmpty() && allowEncryptionWithMissingKeyFlags) {
List<PGPPublicKey> validSubkeys = info.getValidSubkeys();
for (PGPPublicKey validSubkey : validSubkeys) {
if (!validSubkey.isEncryptionKey()) {
continue;
}
// only add encryption keys with no key flags.
if (info.getKeyFlagsOf(validSubkey.getKeyID()).isEmpty()) {
encryptionSubkeys.add(validSubkey);
}
}
}
if (encryptionSubkeys.isEmpty()) {
throw new KeyException.UnacceptableEncryptionKeyException(OpenPgpFingerprint.of(key));
}
@ -386,6 +403,19 @@ public class EncryptionOptions {
return this;
}
/**
* If this method is called, subsequent calls to {@link #addRecipient(PGPPublicKeyRing)} will allow encryption
* for subkeys that do not carry any {@link org.pgpainless.algorithm.KeyFlag} subpacket.
* This is a workaround for dealing with legacy keys that have no key flags subpacket but rely on the key algorithm
* type to convey the subkeys use.
*
* @return this
*/
public EncryptionOptions setAllowEncryptionWithMissingKeyFlags() {
this.allowEncryptionWithMissingKeyFlags = true;
return this;
}
/**
* Return <pre>true</pre> iff the user specified at least one encryption method,
* <pre>false</pre> otherwise.

View file

@ -60,7 +60,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface<KeyRingBuilder> {
private final List<KeySpec> subkeySpecs = new ArrayList<>();
private final Map<String, SelfSignatureSubpackets.Callback> userIds = new LinkedHashMap<>();
private Passphrase passphrase = Passphrase.emptyPassphrase();
private Date expirationDate = new Date(System.currentTimeMillis() + YEAR_IN_SECONDS * 5); // Expiration in 5 yeras
private Date expirationDate = new Date(System.currentTimeMillis() + YEAR_IN_SECONDS * 5); // Expiration in 5 years
@Override
public KeyRingBuilder setPrimaryKey(@Nonnull KeySpec keySpec) {