mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-12-10 06:11:08 +01:00
Implement KeyRingInfo.getKeysWithFlag() and KeyRingInfo.getExpirationDateForUse()
This commit is contained in:
parent
8618d1faea
commit
1ad23366a7
2 changed files with 127 additions and 17 deletions
|
|
@ -25,6 +25,7 @@ import java.util.HashSet;
|
|||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
|
@ -587,24 +588,16 @@ public class KeyRingInfo {
|
|||
* @return expiration date
|
||||
*/
|
||||
public @Nullable Date getPrimaryKeyExpirationDate() {
|
||||
Date lastExpiration = null;
|
||||
if (getLatestDirectKeySelfSignature() != null) {
|
||||
lastExpiration = SignatureUtils.getKeyExpirationDate(getCreationDate(), getLatestDirectKeySelfSignature());
|
||||
PGPSignature primaryUserIdCertification = getLatestUserIdCertification(getPrimaryUserId());
|
||||
if (primaryUserIdCertification != null) {
|
||||
return SignatureSubpacketsUtil.getKeyExpirationTimeAsDate(primaryUserIdCertification, getPublicKey());
|
||||
}
|
||||
|
||||
for (String userId : getValidUserIds()) {
|
||||
|
||||
PGPSignature signature = getLatestUserIdCertification(userId);
|
||||
if (signature == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Date expiration = SignatureUtils.getKeyExpirationDate(getCreationDate(), signature);
|
||||
if (expiration != null && (lastExpiration == null || expiration.after(lastExpiration))) {
|
||||
lastExpiration = expiration;
|
||||
}
|
||||
PGPSignature directKeySig = getLatestDirectKeySelfSignature();
|
||||
if (directKeySig != null) {
|
||||
return SignatureSubpacketsUtil.getKeyExpirationTimeAsDate(directKeySig, getPublicKey());
|
||||
}
|
||||
return lastExpiration;
|
||||
throw new NoSuchElementException("No suitable signatures found on the key.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -620,15 +613,47 @@ public class KeyRingInfo {
|
|||
|
||||
PGPPublicKey subkey = getPublicKey(fingerprint.getKeyId());
|
||||
if (subkey == null) {
|
||||
throw new IllegalArgumentException("No subkey with fingerprint " + fingerprint + " found.");
|
||||
throw new NoSuchElementException("No subkey with fingerprint " + fingerprint + " found.");
|
||||
}
|
||||
|
||||
PGPSignature bindingSig = getCurrentSubkeyBindingSignature(fingerprint.getKeyId());
|
||||
if (bindingSig == null) {
|
||||
return null;
|
||||
throw new AssertionError("Subkey has no valid binding signature.");
|
||||
}
|
||||
|
||||
return SignatureUtils.getKeyExpirationDate(subkey.getCreationTime(), bindingSig);
|
||||
}
|
||||
|
||||
public Date getExpirationDateForUse(KeyFlag use) {
|
||||
if (use == KeyFlag.SPLIT || use == KeyFlag.SHARED) {
|
||||
throw new IllegalArgumentException("SPLIT and SHARED are not uses, but properties.");
|
||||
}
|
||||
|
||||
Date primaryExpiration = getPrimaryKeyExpirationDate();
|
||||
List<PGPPublicKey> nonExpiringSubkeys = new ArrayList<>();
|
||||
Date latestSubkeyExpirationDate = null;
|
||||
|
||||
List<PGPPublicKey> keysWithFlag = getKeysWithKeyFlag(use);
|
||||
for (PGPPublicKey key : keysWithFlag) {
|
||||
Date subkeyExpirationDate = getSubkeyExpirationDate(new OpenPgpV4Fingerprint(key));
|
||||
if (subkeyExpirationDate == null) {
|
||||
nonExpiringSubkeys.add(key);
|
||||
} else {
|
||||
if (latestSubkeyExpirationDate == null || subkeyExpirationDate.after(latestSubkeyExpirationDate)) {
|
||||
latestSubkeyExpirationDate = subkeyExpirationDate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (nonExpiringSubkeys.isEmpty()) {
|
||||
if (latestSubkeyExpirationDate.before(primaryExpiration)) {
|
||||
return latestSubkeyExpirationDate;
|
||||
}
|
||||
return primaryExpiration;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the key ring is a {@link PGPSecretKeyRing}.
|
||||
* If it is a {@link PGPPublicKeyRing} return false and if it is neither, throw an {@link AssertionError}.
|
||||
|
|
@ -727,6 +752,18 @@ public class KeyRingInfo {
|
|||
return encryptionKeys;
|
||||
}
|
||||
|
||||
public List<PGPPublicKey> getKeysWithKeyFlag(KeyFlag flag) {
|
||||
List<PGPPublicKey> keysWithFlag = new ArrayList<>();
|
||||
for (PGPPublicKey key : getPublicKeys()) {
|
||||
List<KeyFlag> keyFlags = getKeyFlagsOf(key.getKeyID());
|
||||
if (keyFlags.contains(flag)) {
|
||||
keysWithFlag.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
return keysWithFlag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of all subkeys that can be used for encryption with the given user-id.
|
||||
* This list does not include expired or revoked keys.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue