diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/BaseSignatureSubpackets.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/BaseSignatureSubpackets.kt index 89b2d232..89b18a38 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/BaseSignatureSubpackets.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/BaseSignatureSubpackets.kt @@ -6,6 +6,7 @@ package org.pgpainless.signature.subpackets import java.io.IOException import java.net.URL +import java.time.Duration import java.util.* import openpgp.plusSeconds import org.bouncycastle.bcpg.sig.* @@ -73,6 +74,14 @@ interface BaseSignatureSubpackets { fun setSignatureExpirationTime(isCritical: Boolean, seconds: Long): BaseSignatureSubpackets + fun setSignatureExpirationTime( + isCritical: Boolean, + duration: Duration + ): BaseSignatureSubpackets { + require(!duration.isNegative) { "Signature Expiration Time cannot be negative." } + return setSignatureExpirationTime(isCritical, duration.seconds) + } + fun setSignatureExpirationTime( expirationTime: SignatureExpirationTime? ): BaseSignatureSubpackets diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.kt index d41f76be..43f61435 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.kt @@ -4,6 +4,7 @@ package org.pgpainless.signature.subpackets +import java.time.Duration import java.util.* import openpgp.plusSeconds import org.bouncycastle.bcpg.sig.Features @@ -74,6 +75,11 @@ interface SelfSignatureSubpackets : BaseSignatureSubpackets { secondsFromCreationToExpiration: Long ): SelfSignatureSubpackets + fun setKeyExpirationTime(isCritical: Boolean, duration: Duration): SelfSignatureSubpackets { + require(!duration.isNegative) { "Key Expiration Time cannot be negative." } + return setKeyExpirationTime(isCritical, duration.seconds) + } + fun setKeyExpirationTime(keyExpirationTime: KeyExpirationTime?): SelfSignatureSubpackets fun getKeyExpirationTime(keyCreationTime: Date): Date? = diff --git a/pgpainless-core/src/test/java/org/pgpainless/signature/subpackets/SignatureSubpacketsTest.java b/pgpainless-core/src/test/java/org/pgpainless/signature/subpackets/SignatureSubpacketsTest.java index 14eed4de..3cf9cce6 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/signature/subpackets/SignatureSubpacketsTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/signature/subpackets/SignatureSubpacketsTest.java @@ -13,10 +13,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; +import java.time.Duration; +import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.Iterator; import java.util.Random; +import kotlin.Unit; import org.bouncycastle.bcpg.SignatureSubpacket; import org.bouncycastle.bcpg.SignatureSubpacketTags; import org.bouncycastle.bcpg.sig.Exportable; @@ -533,4 +536,28 @@ public class SignatureSubpacketsTest { PreferredAlgorithms aeadAlgorithms = (PreferredAlgorithms) vector.getSubpacket(SignatureSubpacketTags.PREFERRED_AEAD_ALGORITHMS); assertArrayEquals(aead.getPreferences(), aeadAlgorithms.getPreferences()); } + + @Test + public void keyExpirationWithNegativeDurationFails() { + assertThrows(IllegalArgumentException.class, () -> + SelfSignatureSubpackets.applyHashed( + hashed -> { + hashed.setKeyExpirationTime(true, Duration.of(-1, ChronoUnit.DAYS)); + return Unit.INSTANCE; + } + ).modifyHashedSubpackets(new SignatureSubpackets()) + ); + } + + @Test + public void signatureExpirationWithNegativeDurationFails() { + assertThrows(IllegalArgumentException.class, () -> + SelfSignatureSubpackets.applyHashed( + hashed -> { + hashed.setSignatureExpirationTime(true, Duration.of(-1, ChronoUnit.DAYS)); + return Unit.INSTANCE; + } + ).modifyHashedSubpackets(new SignatureSubpackets()) + ); + } }