1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-10 06:11:08 +01:00

Allow custom key creation dates during generation

This commit is contained in:
Paul Schaub 2022-03-07 11:08:59 +01:00
parent a6dcf027c0
commit 10e72f6773
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
5 changed files with 73 additions and 3 deletions

View file

@ -305,9 +305,11 @@ public class KeyRingBuilder implements KeyRingBuilderInterface<KeyRingBuilder> {
// Create raw Key Pair
KeyPair keyPair = certKeyGenerator.generateKeyPair();
Date keyCreationDate = spec.getKeyCreationDate() != null ? spec.getKeyCreationDate() : new Date();
// Form PGP key pair
PGPKeyPair pgpKeyPair = ImplementationFactory.getInstance()
.getPGPKeyPair(type.getAlgorithm(), keyPair, new Date());
.getPGPKeyPair(type.getAlgorithm(), keyPair, keyCreationDate);
return pgpKeyPair;
}
}

View file

@ -5,6 +5,7 @@
package org.pgpainless.key.generation;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPSignatureSubpacketVector;
import org.pgpainless.algorithm.KeyFlag;
@ -12,18 +13,23 @@ import org.pgpainless.key.generation.type.KeyType;
import org.pgpainless.signature.subpackets.SignatureSubpackets;
import org.pgpainless.signature.subpackets.SignatureSubpacketsHelper;
import java.util.Date;
public class KeySpec {
private final KeyType keyType;
private final SignatureSubpackets subpacketGenerator;
private final boolean inheritedSubPackets;
private final Date keyCreationDate;
KeySpec(@Nonnull KeyType type,
@Nonnull SignatureSubpackets subpacketGenerator,
boolean inheritedSubPackets) {
boolean inheritedSubPackets,
@Nullable Date keyCreationDate) {
this.keyType = type;
this.subpacketGenerator = subpacketGenerator;
this.inheritedSubPackets = inheritedSubPackets;
this.keyCreationDate = keyCreationDate;
}
@Nonnull
@ -45,6 +51,11 @@ public class KeySpec {
return inheritedSubPackets;
}
@Nullable
public Date getKeyCreationDate() {
return keyCreationDate;
}
public static KeySpecBuilder getBuilder(KeyType type, KeyFlag flag, KeyFlag... flags) {
return new KeySpecBuilder(type, flag, flags);
}

View file

@ -5,6 +5,7 @@
package org.pgpainless.key.generation;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.annotation.Nonnull;
@ -31,6 +32,7 @@ public class KeySpecBuilder implements KeySpecBuilderInterface {
private Set<CompressionAlgorithm> preferredCompressionAlgorithms = algorithmSuite.getCompressionAlgorithms();
private Set<HashAlgorithm> preferredHashAlgorithms = algorithmSuite.getHashAlgorithms();
private Set<SymmetricKeyAlgorithm> preferredSymmetricAlgorithms = algorithmSuite.getSymmetricKeyAlgorithms();
private Date keyCreationDate;
KeySpecBuilder(@Nonnull KeyType type, KeyFlag flag, KeyFlag... flags) {
if (flag == null) {
@ -66,6 +68,11 @@ public class KeySpecBuilder implements KeySpecBuilderInterface {
return this;
}
@Override
public KeySpecBuilder setKeyCreationDate(@Nonnull Date creationDate) {
this.keyCreationDate = creationDate;
return this;
}
@Override
public KeySpec build() {
@ -75,6 +82,6 @@ public class KeySpecBuilder implements KeySpecBuilderInterface {
this.hashedSubpackets.setPreferredSymmetricKeyAlgorithms(preferredSymmetricAlgorithms);
this.hashedSubpackets.setFeatures(Feature.MODIFICATION_DETECTION);
return new KeySpec(type, (SignatureSubpackets) hashedSubpackets, false);
return new KeySpec(type, (SignatureSubpackets) hashedSubpackets, false, keyCreationDate);
}
}

View file

@ -10,6 +10,8 @@ import org.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import java.util.Date;
public interface KeySpecBuilderInterface {
KeySpecBuilder overridePreferredCompressionAlgorithms(@Nonnull CompressionAlgorithm... compressionAlgorithms);
@ -18,5 +20,7 @@ public interface KeySpecBuilderInterface {
KeySpecBuilder overridePreferredSymmetricKeyAlgorithms(@Nonnull SymmetricKeyAlgorithm... preferredSymmetricKeyAlgorithms);
KeySpecBuilder setKeyCreationDate(@Nonnull Date creationDate);
KeySpec build();
}