2021-10-07 15:48:52 +02:00
|
|
|
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-07-18 18:23:06 +02:00
|
|
|
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;
|
2022-03-07 11:08:59 +01:00
|
|
|
import java.util.Date;
|
2021-09-13 19:20:19 +02:00
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
import java.util.Set;
|
2018-07-31 20:09:16 +02:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2021-09-13 19:20:19 +02:00
|
|
|
import org.pgpainless.PGPainless;
|
2023-08-04 13:50:30 +02:00
|
|
|
import org.pgpainless.algorithm.AEADAlgorithmCombination;
|
2018-07-18 18:23:06 +02:00
|
|
|
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;
|
2021-11-20 20:27:36 +01:00
|
|
|
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;
|
2021-09-20 11:26:00 +02:00
|
|
|
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;
|
2021-11-20 20:27:36 +01:00
|
|
|
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();
|
2023-08-04 13:50:30 +02:00
|
|
|
private Set<AEADAlgorithmCombination> preferredAEADAlgorithms = algorithmSuite.getAEADAlgorithms();
|
2022-03-07 11:08:59 +01:00
|
|
|
private Date keyCreationDate;
|
2018-06-02 21:21:35 +02:00
|
|
|
|
2021-09-20 11:26:00 +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
|
|
|
}
|
2021-09-20 11:26:00 +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
|
2021-11-20 20:27:36 +01:00
|
|
|
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
|
2021-11-20 20:27:36 +01:00
|
|
|
public KeySpecBuilder overridePreferredHashAlgorithms(
|
|
|
|
@Nonnull HashAlgorithm... preferredHashAlgorithms) {
|
2021-09-13 19:20:19 +02:00
|
|
|
this.preferredHashAlgorithms = new LinkedHashSet<>(Arrays.asList(preferredHashAlgorithms));
|
|
|
|
return this;
|
2021-01-03 17:01:08 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 19:44:47 +02:00
|
|
|
@Override
|
2021-11-20 20:27:36 +01:00
|
|
|
public KeySpecBuilder overridePreferredSymmetricKeyAlgorithms(
|
|
|
|
@Nonnull SymmetricKeyAlgorithm... preferredSymmetricKeyAlgorithms) {
|
2022-07-15 14:19:45 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-08-04 13:50:30 +02:00
|
|
|
@Override
|
|
|
|
public KeySpecBuilder overridePreferredAEADAlgorithms(
|
|
|
|
@Nonnull AEADAlgorithmCombination... preferredAEADAlgorithms) {
|
|
|
|
this.preferredAEADAlgorithms = new LinkedHashSet<>(Arrays.asList(preferredAEADAlgorithms));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-03-07 11:08:59 +01:00
|
|
|
@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() {
|
2021-11-20 20:27:36 +01:00
|
|
|
this.hashedSubpackets.setKeyFlags(keyFlags);
|
|
|
|
this.hashedSubpackets.setPreferredCompressionAlgorithms(preferredCompressionAlgorithms);
|
|
|
|
this.hashedSubpackets.setPreferredHashAlgorithms(preferredHashAlgorithms);
|
|
|
|
this.hashedSubpackets.setPreferredSymmetricKeyAlgorithms(preferredSymmetricAlgorithms);
|
2023-08-04 13:50:30 +02:00
|
|
|
this.hashedSubpackets.setPreferredAEADCiphersuites(preferredAEADAlgorithms);
|
2021-11-20 20:27:36 +01:00
|
|
|
this.hashedSubpackets.setFeatures(Feature.MODIFICATION_DETECTION);
|
2021-09-13 19:20:19 +02:00
|
|
|
|
2022-03-07 11:08:59 +01:00
|
|
|
return new KeySpec(type, (SignatureSubpackets) hashedSubpackets, false, keyCreationDate);
|
2018-06-02 21:21:35 +02:00
|
|
|
}
|
|
|
|
}
|