Move getKeyLifetimeInSeconds to SignatureSubpacketsUtil and make public

This commit is contained in:
Paul Schaub 2021-11-28 13:09:10 +01:00
parent b09858e186
commit b874aee6bb
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
2 changed files with 21 additions and 19 deletions

View File

@ -92,28 +92,10 @@ public final class SignatureSubpacketGeneratorUtil {
@Nonnull Date creationDate,
PGPSignatureSubpacketGenerator subpacketGenerator) {
removeAllPacketsOfType(SignatureSubpacketTags.KEY_EXPIRE_TIME, subpacketGenerator);
long secondsToExpire = getKeyLifetimeInSeconds(expirationDate, creationDate);
long secondsToExpire = SignatureSubpacketsUtil.getKeyLifetimeInSeconds(expirationDate, creationDate);
subpacketGenerator.setKeyExpirationTime(true, secondsToExpire);
}
/**
* Calculate the duration in seconds until the key expires after creation.
*
* @param expirationDate new expiration date
* @param creationTime key creation time
* @return life time of the key in seconds
*/
private static long getKeyLifetimeInSeconds(Date expirationDate, @Nonnull Date creationTime) {
long secondsToExpire = 0; // 0 means "no expiration"
if (expirationDate != null) {
if (creationTime.after(expirationDate)) {
throw new IllegalArgumentException("Key MUST NOT expire before being created. (creation: " + creationTime + ", expiration: " + expirationDate + ")");
}
secondsToExpire = (expirationDate.getTime() - creationTime.getTime()) / 1000;
}
return secondsToExpire;
}
/**
* Return true, if the subpacket generator has a {@link KeyFlags} subpacket which carries the given key flag.
* Returns false, if no {@link KeyFlags} subpacket is present.

View File

@ -11,6 +11,7 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bouncycastle.bcpg.sig.Exportable;
@ -204,6 +205,25 @@ public final class SignatureSubpacketsUtil {
return SignatureUtils.datePlusSeconds(signingKey.getCreationTime(), subpacket.getTime());
}
/**
* Calculate the duration in seconds until the key expires after creation.
*
* @param expirationDate new expiration date
* @param creationDate key creation time
* @return life time of the key in seconds
*/
public static long getKeyLifetimeInSeconds(@Nullable Date expirationDate, @Nonnull Date creationDate) {
long secondsToExpire = 0; // 0 means "no expiration"
if (expirationDate != null) {
if (creationDate.after(expirationDate)) {
throw new IllegalArgumentException("Key MUST NOT expire before being created. " +
"(creation: " + creationDate + ", expiration: " + expirationDate + ")");
}
secondsToExpire = (expirationDate.getTime() - creationDate.getTime()) / 1000;
}
return secondsToExpire;
}
/**
* Return the revocable subpacket of this signature.
* We only look for it in the hashed area of the signature.