From 8c49d37e1f2d7b9116aeef85abeed29638cc44be Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 6 Nov 2021 15:40:10 +0100 Subject: [PATCH] Change return values of signature subpackets subclasses --- .../builder/AbstractSignatureBuilder.java | 32 ++++++- .../subpackets/BaseSignatureSubpackets.java | 86 +++++++++---------- .../subpackets/CertificationSubpackets.java | 8 +- .../RevocationSignatureSubpackets.java | 16 ++-- .../subpackets/SelfSignatureSubpackets.java | 70 +++++++-------- .../SignatureSubpacketCallback.java | 26 ++++++ 6 files changed, 135 insertions(+), 103 deletions(-) create mode 100644 pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/SignatureSubpacketCallback.java diff --git a/pgpainless-core/src/main/java/org/pgpainless/signature/builder/AbstractSignatureBuilder.java b/pgpainless-core/src/main/java/org/pgpainless/signature/builder/AbstractSignatureBuilder.java index b99693bc..219e7d22 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/signature/builder/AbstractSignatureBuilder.java +++ b/pgpainless-core/src/main/java/org/pgpainless/signature/builder/AbstractSignatureBuilder.java @@ -44,10 +44,12 @@ public abstract class AbstractSignatureBuilder hashAlgorithmPreferences = OpenPgpKeyAttributeUtil.getOrGuessPreferredHashAlgorithms(publicKey); return HashAlgorithmNegotiator.negotiateSignatureHashAlgorithm(PGPainless.getPolicy()) .negotiateHashAlgorithm(hashAlgorithmPreferences); } + /** + * Set the builders {@link SignatureType}. + * Note that only those types who are valid for the concrete subclass of this {@link AbstractSignatureBuilder} + * are allowed. Invalid choices result in an {@link IllegalArgumentException} to be thrown. + * + * @param type signature type + * @return builder + */ public B setSignatureType(SignatureType type) { if (!isValidSignatureType(type)) { throw new IllegalArgumentException("Invalid signature type: " + type); @@ -75,6 +91,13 @@ public abstract class AbstractSignatureBuilder { - } - - default void modifyUnhashedSubpackets(SignatureSubpacketGeneratorWrapper subpackets) { - - } } - SignatureSubpacketGeneratorWrapper setIssuerFingerprintAndKeyId(PGPPublicKey key); + BaseSignatureSubpackets setIssuerFingerprintAndKeyId(PGPPublicKey key); - SignatureSubpacketGeneratorWrapper setIssuerKeyId(long keyId); + BaseSignatureSubpackets setIssuerKeyId(long keyId); - SignatureSubpacketGeneratorWrapper setIssuerKeyId(boolean isCritical, long keyId); + BaseSignatureSubpackets setIssuerKeyId(boolean isCritical, long keyId); - SignatureSubpacketGeneratorWrapper setIssuerKeyId(@Nullable IssuerKeyID issuerKeyID); + BaseSignatureSubpackets setIssuerKeyId(@Nullable IssuerKeyID issuerKeyID); - SignatureSubpacketGeneratorWrapper setIssuerFingerprint(@Nonnull PGPPublicKey key); + BaseSignatureSubpackets setIssuerFingerprint(@Nonnull PGPPublicKey key); - SignatureSubpacketGeneratorWrapper setIssuerFingerprint(boolean isCritical, @Nonnull PGPPublicKey key); + BaseSignatureSubpackets setIssuerFingerprint(boolean isCritical, @Nonnull PGPPublicKey key); - SignatureSubpacketGeneratorWrapper setIssuerFingerprint(@Nullable IssuerFingerprint fingerprint); + BaseSignatureSubpackets setIssuerFingerprint(@Nullable IssuerFingerprint fingerprint); - SignatureSubpacketGeneratorWrapper setSignatureCreationTime(@Nonnull Date creationTime); + BaseSignatureSubpackets setSignatureCreationTime(@Nonnull Date creationTime); - SignatureSubpacketGeneratorWrapper setSignatureCreationTime(boolean isCritical, @Nonnull Date creationTime); + BaseSignatureSubpackets setSignatureCreationTime(boolean isCritical, @Nonnull Date creationTime); - SignatureSubpacketGeneratorWrapper setSignatureCreationTime(@Nullable SignatureCreationTime signatureCreationTime); + BaseSignatureSubpackets setSignatureCreationTime(@Nullable SignatureCreationTime signatureCreationTime); - SignatureSubpacketGeneratorWrapper setSignatureExpirationTime(@Nonnull Date creationTime, @Nonnull Date expirationTime); + BaseSignatureSubpackets setSignatureExpirationTime(@Nonnull Date creationTime, @Nonnull Date expirationTime); - SignatureSubpacketGeneratorWrapper setSignatureExpirationTime(boolean isCritical, @Nonnull Date creationTime, @Nonnull Date expirationTime); + BaseSignatureSubpackets setSignatureExpirationTime(boolean isCritical, @Nonnull Date creationTime, @Nonnull Date expirationTime); - SignatureSubpacketGeneratorWrapper setSignatureExpirationTime(boolean isCritical, long seconds); + BaseSignatureSubpackets setSignatureExpirationTime(boolean isCritical, long seconds); - SignatureSubpacketGeneratorWrapper setSignatureExpirationTime(@Nullable SignatureExpirationTime expirationTime); + BaseSignatureSubpackets setSignatureExpirationTime(@Nullable SignatureExpirationTime expirationTime); - SignatureSubpacketGeneratorWrapper setSignerUserId(@Nonnull String userId); + BaseSignatureSubpackets setSignerUserId(@Nonnull String userId); - SignatureSubpacketGeneratorWrapper setSignerUserId(boolean isCritical, @Nonnull String userId); + BaseSignatureSubpackets setSignerUserId(boolean isCritical, @Nonnull String userId); - SignatureSubpacketGeneratorWrapper setSignerUserId(@Nullable SignerUserID signerUserId); + BaseSignatureSubpackets setSignerUserId(@Nullable SignerUserID signerUserId); - SignatureSubpacketGeneratorWrapper addNotationData(boolean isCritical, @Nonnull String notationName, @Nonnull String notationValue); + BaseSignatureSubpackets addNotationData(boolean isCritical, @Nonnull String notationName, @Nonnull String notationValue); - SignatureSubpacketGeneratorWrapper addNotationData(boolean isCritical, boolean isHumanReadable, @Nonnull String notationName, @Nonnull String notationValue); + BaseSignatureSubpackets addNotationData(boolean isCritical, boolean isHumanReadable, @Nonnull String notationName, @Nonnull String notationValue); - SignatureSubpacketGeneratorWrapper addNotationData(@Nonnull NotationData notationData); + BaseSignatureSubpackets addNotationData(@Nonnull NotationData notationData); - SignatureSubpacketGeneratorWrapper clearNotationData(); + BaseSignatureSubpackets clearNotationData(); - SignatureSubpacketGeneratorWrapper addIntendedRecipientFingerprint(@Nonnull PGPPublicKey recipient); + BaseSignatureSubpackets addIntendedRecipientFingerprint(@Nonnull PGPPublicKey recipient); - SignatureSubpacketGeneratorWrapper addIntendedRecipientFingerprint(boolean isCritical, @Nonnull PGPPublicKey recipient); + BaseSignatureSubpackets addIntendedRecipientFingerprint(boolean isCritical, @Nonnull PGPPublicKey recipient); - SignatureSubpacketGeneratorWrapper addIntendedRecipientFingerprint(IntendedRecipientFingerprint intendedRecipientFingerprint); + BaseSignatureSubpackets addIntendedRecipientFingerprint(IntendedRecipientFingerprint intendedRecipientFingerprint); - SignatureSubpacketGeneratorWrapper clearIntendedRecipientFingerprints(); + BaseSignatureSubpackets clearIntendedRecipientFingerprints(); - SignatureSubpacketGeneratorWrapper setExportable(boolean isCritical, boolean isExportable); + BaseSignatureSubpackets setExportable(boolean isCritical, boolean isExportable); - SignatureSubpacketGeneratorWrapper setExportable(@Nullable Exportable exportable); + BaseSignatureSubpackets setExportable(@Nullable Exportable exportable); - SignatureSubpacketGeneratorWrapper setRevocable(boolean isCritical, boolean isRevocable); + BaseSignatureSubpackets setRevocable(boolean isCritical, boolean isRevocable); - SignatureSubpacketGeneratorWrapper setRevocable(@Nullable Revocable revocable); + BaseSignatureSubpackets setRevocable(@Nullable Revocable revocable); - SignatureSubpacketGeneratorWrapper setSignatureTarget(@Nonnull PublicKeyAlgorithm keyAlgorithm, @Nonnull HashAlgorithm hashAlgorithm, @Nonnull byte[] hashData); + BaseSignatureSubpackets setSignatureTarget(@Nonnull PublicKeyAlgorithm keyAlgorithm, @Nonnull HashAlgorithm hashAlgorithm, @Nonnull byte[] hashData); - SignatureSubpacketGeneratorWrapper setSignatureTarget(boolean isCritical, @Nonnull PublicKeyAlgorithm keyAlgorithm, @Nonnull HashAlgorithm hashAlgorithm, @Nonnull byte[] hashData); + BaseSignatureSubpackets setSignatureTarget(boolean isCritical, @Nonnull PublicKeyAlgorithm keyAlgorithm, @Nonnull HashAlgorithm hashAlgorithm, @Nonnull byte[] hashData); - SignatureSubpacketGeneratorWrapper setSignatureTarget(@Nullable SignatureTarget signatureTarget); + BaseSignatureSubpackets setSignatureTarget(@Nullable SignatureTarget signatureTarget); - SignatureSubpacketGeneratorWrapper setTrust(int depth, int amount); + BaseSignatureSubpackets setTrust(int depth, int amount); - SignatureSubpacketGeneratorWrapper setTrust(boolean isCritical, int depth, int amount); + BaseSignatureSubpackets setTrust(boolean isCritical, int depth, int amount); - SignatureSubpacketGeneratorWrapper setTrust(@Nullable TrustSignature trust); + BaseSignatureSubpackets setTrust(@Nullable TrustSignature trust); - SignatureSubpacketGeneratorWrapper addEmbeddedSignature(@Nonnull PGPSignature signature) throws IOException; + BaseSignatureSubpackets addEmbeddedSignature(@Nonnull PGPSignature signature) throws IOException; - SignatureSubpacketGeneratorWrapper addEmbeddedSignature(boolean isCritical, @Nonnull PGPSignature signature) throws IOException; + BaseSignatureSubpackets addEmbeddedSignature(boolean isCritical, @Nonnull PGPSignature signature) throws IOException; - SignatureSubpacketGeneratorWrapper addEmbeddedSignature(@Nonnull EmbeddedSignature embeddedSignature); + BaseSignatureSubpackets addEmbeddedSignature(@Nonnull EmbeddedSignature embeddedSignature); - SignatureSubpacketGeneratorWrapper clearEmbeddedSignatures(); + BaseSignatureSubpackets clearEmbeddedSignatures(); } diff --git a/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/CertificationSubpackets.java b/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/CertificationSubpackets.java index e6597ca9..59356bba 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/CertificationSubpackets.java +++ b/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/CertificationSubpackets.java @@ -6,13 +6,7 @@ package org.pgpainless.signature.subpackets; public interface CertificationSubpackets extends BaseSignatureSubpackets { - interface Callback { - default void modifyHashedSubpackets(CertificationSubpackets subpackets) { + interface Callback extends SignatureSubpacketCallback { - } - - default void modifyUnhashedSubpackets(CertificationSubpackets subpackets) { - - } } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/RevocationSignatureSubpackets.java b/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/RevocationSignatureSubpackets.java index d3ffd9d5..358437dc 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/RevocationSignatureSubpackets.java +++ b/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/RevocationSignatureSubpackets.java @@ -12,21 +12,15 @@ import org.pgpainless.key.util.RevocationAttributes; public interface RevocationSignatureSubpackets extends BaseSignatureSubpackets { - interface Callback { - default void modifyHashedSubpackets(RevocationSignatureSubpackets subpackets) { + interface Callback extends SignatureSubpacketCallback { - } - - default void modifyUnhashedSubpackets(RevocationSignatureSubpackets subpackets) { - - } } - SignatureSubpacketGeneratorWrapper setRevocationReason(RevocationAttributes revocationAttributes); + RevocationSignatureSubpackets setRevocationReason(RevocationAttributes revocationAttributes); - SignatureSubpacketGeneratorWrapper setRevocationReason(boolean isCritical, RevocationAttributes revocationAttributes); + RevocationSignatureSubpackets setRevocationReason(boolean isCritical, RevocationAttributes revocationAttributes); - SignatureSubpacketGeneratorWrapper setRevocationReason(boolean isCritical, RevocationAttributes.Reason reason, @Nonnull String description); + RevocationSignatureSubpackets setRevocationReason(boolean isCritical, RevocationAttributes.Reason reason, @Nonnull String description); - SignatureSubpacketGeneratorWrapper setRevocationReason(@Nullable RevocationReason reason); + RevocationSignatureSubpackets setRevocationReason(@Nullable RevocationReason reason); } diff --git a/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.java b/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.java index fa4eb719..1ff45ae4 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.java +++ b/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.java @@ -24,75 +24,69 @@ import org.pgpainless.algorithm.SymmetricKeyAlgorithm; public interface SelfSignatureSubpackets extends BaseSignatureSubpackets { - interface Callback { - default void modifyHashedSubpackets(SelfSignatureSubpackets subpackets) { + interface Callback extends SignatureSubpacketCallback { - } - - default void modifyUnhashedSubpackets(SelfSignatureSubpackets subpackets) { - - } } - SignatureSubpacketGeneratorWrapper setKeyFlags(KeyFlag... keyFlags); + SelfSignatureSubpackets setKeyFlags(KeyFlag... keyFlags); - SignatureSubpacketGeneratorWrapper setKeyFlags(boolean isCritical, KeyFlag... keyFlags); + SelfSignatureSubpackets setKeyFlags(boolean isCritical, KeyFlag... keyFlags); - SignatureSubpacketGeneratorWrapper setKeyFlags(@Nullable KeyFlags keyFlags); + SelfSignatureSubpackets setKeyFlags(@Nullable KeyFlags keyFlags); - SignatureSubpacketGeneratorWrapper setPrimaryUserId(); + SelfSignatureSubpackets setPrimaryUserId(); - SignatureSubpacketGeneratorWrapper setPrimaryUserId(boolean isCritical); + SelfSignatureSubpackets setPrimaryUserId(boolean isCritical); - SignatureSubpacketGeneratorWrapper setPrimaryUserId(@Nullable PrimaryUserID primaryUserId); + SelfSignatureSubpackets setPrimaryUserId(@Nullable PrimaryUserID primaryUserId); - SignatureSubpacketGeneratorWrapper setKeyExpirationTime(@Nonnull PGPPublicKey key, @Nonnull Date keyExpirationTime); + SelfSignatureSubpackets setKeyExpirationTime(@Nonnull PGPPublicKey key, @Nonnull Date keyExpirationTime); - SignatureSubpacketGeneratorWrapper setKeyExpirationTime(@Nonnull Date keyCreationTime, @Nonnull Date keyExpirationTime); + SelfSignatureSubpackets setKeyExpirationTime(@Nonnull Date keyCreationTime, @Nonnull Date keyExpirationTime); - SignatureSubpacketGeneratorWrapper setKeyExpirationTime(boolean isCritical, @Nonnull Date keyCreationTime, @Nonnull Date keyExpirationTime); + SelfSignatureSubpackets setKeyExpirationTime(boolean isCritical, @Nonnull Date keyCreationTime, @Nonnull Date keyExpirationTime); - SignatureSubpacketGeneratorWrapper setKeyExpirationTime(boolean isCritical, long secondsFromCreationToExpiration); + SelfSignatureSubpackets setKeyExpirationTime(boolean isCritical, long secondsFromCreationToExpiration); - SignatureSubpacketGeneratorWrapper setKeyExpirationTime(@Nullable KeyExpirationTime keyExpirationTime); + SelfSignatureSubpackets setKeyExpirationTime(@Nullable KeyExpirationTime keyExpirationTime); - SignatureSubpacketGeneratorWrapper setPreferredCompressionAlgorithms(CompressionAlgorithm... algorithms); + SelfSignatureSubpackets setPreferredCompressionAlgorithms(CompressionAlgorithm... algorithms); - SignatureSubpacketGeneratorWrapper setPreferredCompressionAlgorithms(Set algorithms); + SelfSignatureSubpackets setPreferredCompressionAlgorithms(Set algorithms); - SignatureSubpacketGeneratorWrapper setPreferredCompressionAlgorithms(boolean isCritical, Set algorithms); + SelfSignatureSubpackets setPreferredCompressionAlgorithms(boolean isCritical, Set algorithms); - SignatureSubpacketGeneratorWrapper setPreferredCompressionAlgorithms(@Nullable PreferredAlgorithms algorithms); + SelfSignatureSubpackets setPreferredCompressionAlgorithms(@Nullable PreferredAlgorithms algorithms); - SignatureSubpacketGeneratorWrapper setPreferredSymmetricKeyAlgorithms(SymmetricKeyAlgorithm... algorithms); + SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(SymmetricKeyAlgorithm... algorithms); - SignatureSubpacketGeneratorWrapper setPreferredSymmetricKeyAlgorithms(Set algorithms); + SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(Set algorithms); - SignatureSubpacketGeneratorWrapper setPreferredSymmetricKeyAlgorithms(boolean isCritical, Set algorithms); + SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(boolean isCritical, Set algorithms); - SignatureSubpacketGeneratorWrapper setPreferredSymmetricKeyAlgorithms(@Nullable PreferredAlgorithms algorithms); + SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(@Nullable PreferredAlgorithms algorithms); - SignatureSubpacketGeneratorWrapper setPreferredHashAlgorithms(HashAlgorithm... algorithms); + SelfSignatureSubpackets setPreferredHashAlgorithms(HashAlgorithm... algorithms); - SignatureSubpacketGeneratorWrapper setPreferredHashAlgorithms(Set algorithms); + SelfSignatureSubpackets setPreferredHashAlgorithms(Set algorithms); - SignatureSubpacketGeneratorWrapper setPreferredHashAlgorithms(boolean isCritical, Set algorithms); + SelfSignatureSubpackets setPreferredHashAlgorithms(boolean isCritical, Set algorithms); - SignatureSubpacketGeneratorWrapper setPreferredHashAlgorithms(@Nullable PreferredAlgorithms algorithms); + SelfSignatureSubpackets setPreferredHashAlgorithms(@Nullable PreferredAlgorithms algorithms); - SignatureSubpacketGeneratorWrapper addRevocationKey(@Nonnull PGPPublicKey revocationKey); + SelfSignatureSubpackets addRevocationKey(@Nonnull PGPPublicKey revocationKey); - SignatureSubpacketGeneratorWrapper addRevocationKey(boolean isCritical, @Nonnull PGPPublicKey revocationKey); + SelfSignatureSubpackets addRevocationKey(boolean isCritical, @Nonnull PGPPublicKey revocationKey); - SignatureSubpacketGeneratorWrapper addRevocationKey(boolean isCritical, boolean isSensitive, @Nonnull PGPPublicKey revocationKey); + SelfSignatureSubpackets addRevocationKey(boolean isCritical, boolean isSensitive, @Nonnull PGPPublicKey revocationKey); - SignatureSubpacketGeneratorWrapper addRevocationKey(@Nonnull RevocationKey revocationKey); + SelfSignatureSubpackets addRevocationKey(@Nonnull RevocationKey revocationKey); - SignatureSubpacketGeneratorWrapper clearRevocationKeys(); + SelfSignatureSubpackets clearRevocationKeys(); - SignatureSubpacketGeneratorWrapper setFeatures(Feature... features); + SelfSignatureSubpackets setFeatures(Feature... features); - SignatureSubpacketGeneratorWrapper setFeatures(boolean isCritical, Feature... features); + SelfSignatureSubpackets setFeatures(boolean isCritical, Feature... features); - SignatureSubpacketGeneratorWrapper setFeatures(@Nullable Features features); + SelfSignatureSubpackets setFeatures(@Nullable Features features); } diff --git a/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/SignatureSubpacketCallback.java b/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/SignatureSubpacketCallback.java new file mode 100644 index 00000000..11650ea3 --- /dev/null +++ b/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/SignatureSubpacketCallback.java @@ -0,0 +1,26 @@ +// SPDX-FileCopyrightText: 2021 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.signature.subpackets; + +public interface SignatureSubpacketCallback { + + /** + * Callback method that can be used to modify the hashed subpackets of a signature. + * + * @param hashedSubpackets hashed subpackets + */ + default void modifyHashedSubpackets(S hashedSubpackets) { + + } + + /** + * Callback method that can be used to modify the unhashed subpackets of a signature. + * + * @param unhashedSubpackets unhashed subpackets + */ + default void modifyUnhashedSubpackets(S unhashedSubpackets) { + + } +}