From 528591f906fe83d62dc57f22cc171f9b6f27aef3 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Thu, 1 Jun 2023 23:43:41 +0200 Subject: [PATCH] Key generation: Set default expiration periof of 5 years Can be changed by calling 'keyRingBuilder.setExpirationDate(null);' --- .../pgpainless/key/generation/KeyRingBuilder.java | 12 ++++++++++-- ...dSignatureSubpacketsArePreservedOnNewSigTest.java | 5 ++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java index 10970fc2..893036b1 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java @@ -54,6 +54,8 @@ import org.pgpainless.util.Passphrase; public class KeyRingBuilder implements KeyRingBuilderInterface { + private static final long YEAR_IN_SECONDS = 1000L * 60 * 60 * 24 * 365; + @SuppressWarnings("CharsetObjectCanBeUsed") private final Charset UTF8 = Charset.forName("UTF-8"); @@ -61,7 +63,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface { private final List subkeySpecs = new ArrayList<>(); private final Map userIds = new LinkedHashMap<>(); private Passphrase passphrase = Passphrase.emptyPassphrase(); - private Date expirationDate = null; + private Date expirationDate = new Date(new Date().getTime() + YEAR_IN_SECONDS * 5); // Expiration in 5 yeras @Override public KeyRingBuilder setPrimaryKey(@Nonnull KeySpec keySpec) { @@ -97,7 +99,13 @@ public class KeyRingBuilder implements KeyRingBuilderInterface { } @Override - public KeyRingBuilder setExpirationDate(@Nonnull Date expirationDate) { + public KeyRingBuilder setExpirationDate(@Nullable Date expirationDate) { + if (expirationDate == null) { + // No expiration + this.expirationDate = null; + return this; + } + Date now = new Date(); if (now.after(expirationDate)) { throw new IllegalArgumentException("Expiration date must be in the future."); diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/modification/OldSignatureSubpacketsArePreservedOnNewSigTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/modification/OldSignatureSubpacketsArePreservedOnNewSigTest.java index 54ed88d7..b8c3244e 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/modification/OldSignatureSubpacketsArePreservedOnNewSigTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/modification/OldSignatureSubpacketsArePreservedOnNewSigTest.java @@ -5,7 +5,6 @@ package org.pgpainless.key.modification; import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -38,7 +37,7 @@ public class OldSignatureSubpacketsArePreservedOnNewSigTest { assertNotNull(oldSignature); PGPSignatureSubpacketVector oldPackets = oldSignature.getHashedSubPackets(); - assertEquals(0, oldPackets.getKeyExpirationTime()); + long oldExpiration = oldPackets.getKeyExpirationTime(); Date now = new Date(); Date t1 = new Date(now.getTime() + millisInHour); @@ -51,7 +50,7 @@ public class OldSignatureSubpacketsArePreservedOnNewSigTest { assertNotNull(newSignature); PGPSignatureSubpacketVector newPackets = newSignature.getHashedSubPackets(); - assertNotEquals(0, newPackets.getKeyExpirationTime()); + assertNotEquals(oldExpiration, newPackets.getKeyExpirationTime()); assertArrayEquals(oldPackets.getPreferredHashAlgorithms(), newPackets.getPreferredHashAlgorithms()); }