mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-24 11:57:59 +01:00
SignatureSubpackets: Add set*Expiration() methods taking Duration arguments
This commit is contained in:
parent
b27a8b71ec
commit
31eda327c6
3 changed files with 42 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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? =
|
||||
|
|
|
@ -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())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue