pgpainless/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpecBuilder.java

103 lines
4.4 KiB
Java
Raw Normal View History

2021-10-07 15:48:52 +02:00
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.key.generation;
2018-06-02 21:21:35 +02:00
2021-09-13 19:20:19 +02:00
import java.util.Arrays;
import java.util.Date;
2021-09-13 19:20:19 +02:00
import java.util.LinkedHashSet;
import java.util.Set;
import javax.annotation.Nonnull;
2021-09-13 19:20:19 +02:00
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.AEADAlgorithmCombination;
import org.pgpainless.algorithm.AlgorithmSuite;
import org.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.algorithm.Feature;
import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.key.generation.type.KeyType;
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
import org.pgpainless.signature.subpackets.SignatureSubpackets;
2021-11-08 21:25:02 +01:00
import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil;
import org.pgpainless.util.CollectionUtils;
2018-06-02 21:21:35 +02:00
public class KeySpecBuilder implements KeySpecBuilderInterface {
2021-01-29 14:48:02 +01:00
private final KeyType type;
2021-09-13 19:20:19 +02:00
private final KeyFlag[] keyFlags;
private final SelfSignatureSubpackets hashedSubpackets = new SignatureSubpackets();
2021-09-13 19:20:19 +02:00
private final AlgorithmSuite algorithmSuite = PGPainless.getPolicy().getKeyGenerationAlgorithmSuite();
private Set<CompressionAlgorithm> preferredCompressionAlgorithms = algorithmSuite.getCompressionAlgorithms();
private Set<HashAlgorithm> preferredHashAlgorithms = algorithmSuite.getHashAlgorithms();
private Set<SymmetricKeyAlgorithm> preferredSymmetricAlgorithms = algorithmSuite.getSymmetricKeyAlgorithms();
private Set<AEADAlgorithmCombination> preferredAEADAlgorithms = algorithmSuite.getAEADAlgorithms();
private Date keyCreationDate;
2018-06-02 21:21:35 +02:00
KeySpecBuilder(@Nonnull KeyType type, KeyFlag flag, KeyFlag... flags) {
if (flag == null) {
throw new IllegalArgumentException("Key MUST carry at least one key flag");
2021-09-13 19:20:19 +02:00
}
if (flags == null) {
throw new IllegalArgumentException("List of additional flags MUST NOT be null.");
}
flags = CollectionUtils.concat(flag, flags);
2021-11-08 21:25:02 +01:00
SignatureSubpacketsUtil.assureKeyCanCarryFlags(type, flags);
2018-06-04 19:44:47 +02:00
this.type = type;
2021-09-13 19:20:19 +02:00
this.keyFlags = flags;
2018-06-02 21:21:35 +02:00
}
2018-06-04 19:44:47 +02:00
@Override
public KeySpecBuilder overridePreferredCompressionAlgorithms(
@Nonnull CompressionAlgorithm... compressionAlgorithms) {
2021-09-13 19:20:19 +02:00
this.preferredCompressionAlgorithms = new LinkedHashSet<>(Arrays.asList(compressionAlgorithms));
return this;
2018-06-04 19:44:47 +02:00
}
2018-06-02 21:21:35 +02:00
2021-09-13 19:20:19 +02:00
@Override
public KeySpecBuilder overridePreferredHashAlgorithms(
@Nonnull HashAlgorithm... preferredHashAlgorithms) {
2021-09-13 19:20:19 +02:00
this.preferredHashAlgorithms = new LinkedHashSet<>(Arrays.asList(preferredHashAlgorithms));
return this;
}
2018-06-04 19:44:47 +02:00
@Override
public KeySpecBuilder overridePreferredSymmetricKeyAlgorithms(
@Nonnull SymmetricKeyAlgorithm... preferredSymmetricKeyAlgorithms) {
for (SymmetricKeyAlgorithm algo : preferredSymmetricKeyAlgorithms) {
if (algo == SymmetricKeyAlgorithm.NULL) {
throw new IllegalArgumentException("NULL (unencrypted) is an invalid symmetric key algorithm preference.");
}
}
2021-09-13 19:20:19 +02:00
this.preferredSymmetricAlgorithms = new LinkedHashSet<>(Arrays.asList(preferredSymmetricKeyAlgorithms));
return this;
2018-06-02 21:21:35 +02:00
}
@Override
public KeySpecBuilder overridePreferredAEADAlgorithms(
@Nonnull AEADAlgorithmCombination... preferredAEADAlgorithms) {
this.preferredAEADAlgorithms = new LinkedHashSet<>(Arrays.asList(preferredAEADAlgorithms));
return this;
}
@Override
public KeySpecBuilder setKeyCreationDate(@Nonnull Date creationDate) {
this.keyCreationDate = creationDate;
return this;
}
2018-06-02 21:21:35 +02:00
2021-09-13 19:20:19 +02:00
@Override
public KeySpec build() {
this.hashedSubpackets.setKeyFlags(keyFlags);
this.hashedSubpackets.setPreferredCompressionAlgorithms(preferredCompressionAlgorithms);
this.hashedSubpackets.setPreferredHashAlgorithms(preferredHashAlgorithms);
this.hashedSubpackets.setPreferredSymmetricKeyAlgorithms(preferredSymmetricAlgorithms);
this.hashedSubpackets.setPreferredAEADCiphersuites(preferredAEADAlgorithms);
this.hashedSubpackets.setFeatures(Feature.MODIFICATION_DETECTION);
2021-09-13 19:20:19 +02:00
return new KeySpec(type, (SignatureSubpackets) hashedSubpackets, false, keyCreationDate);
2018-06-02 21:21:35 +02:00
}
}