From eb9ea23514f8e7539d2b3efe990b9fd104e350b7 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 6 Nov 2021 14:56:16 +0100 Subject: [PATCH] Add UniversalSignatureBuilder --- .../signature/builder/SignatureFactory.java | 17 ++++++ .../builder/UniversalSignatureBuilder.java | 57 +++++++++++++++++++ .../subpackets/BaseSignatureSubpackets.java | 10 ++++ 3 files changed, 84 insertions(+) create mode 100644 pgpainless-core/src/main/java/org/pgpainless/signature/builder/UniversalSignatureBuilder.java diff --git a/pgpainless-core/src/main/java/org/pgpainless/signature/builder/SignatureFactory.java b/pgpainless-core/src/main/java/org/pgpainless/signature/builder/SignatureFactory.java index 2f9162a9..6ee1ea4b 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/signature/builder/SignatureFactory.java +++ b/pgpainless-core/src/main/java/org/pgpainless/signature/builder/SignatureFactory.java @@ -11,8 +11,10 @@ import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSignature; import org.pgpainless.algorithm.KeyFlag; +import org.pgpainless.algorithm.SignatureType; import org.pgpainless.exception.WrongPassphraseException; import org.pgpainless.key.protection.SecretKeyRingProtector; +import org.pgpainless.signature.subpackets.BaseSignatureSubpackets; import org.pgpainless.signature.subpackets.SelfSignatureSubpackets; public final class SignatureFactory { @@ -109,6 +111,20 @@ public final class SignatureFactory { return certifier; } + public static UniversalSignatureBuilder universalSignature( + SignatureType signatureType, + PGPSecretKey signingKey, + SecretKeyRingProtector signingKeyProtector, + @Nullable BaseSignatureSubpackets.Callback callback) + throws WrongPassphraseException { + UniversalSignatureBuilder builder = + new UniversalSignatureBuilder(signatureType, signingKey, signingKeyProtector); + + builder.applyCallback(callback); + + return builder; + } + private static boolean hasSignDataFlag(KeyFlag... flags) { if (flags == null) { return false; @@ -120,4 +136,5 @@ public final class SignatureFactory { } return false; } + } diff --git a/pgpainless-core/src/main/java/org/pgpainless/signature/builder/UniversalSignatureBuilder.java b/pgpainless-core/src/main/java/org/pgpainless/signature/builder/UniversalSignatureBuilder.java new file mode 100644 index 00000000..b674cba9 --- /dev/null +++ b/pgpainless-core/src/main/java/org/pgpainless/signature/builder/UniversalSignatureBuilder.java @@ -0,0 +1,57 @@ +// SPDX-FileCopyrightText: 2021 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.signature.builder; + +import javax.annotation.Nullable; + +import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPSecretKey; +import org.bouncycastle.openpgp.PGPSignature; +import org.bouncycastle.openpgp.PGPSignatureGenerator; +import org.pgpainless.algorithm.SignatureType; +import org.pgpainless.exception.WrongPassphraseException; +import org.pgpainless.key.protection.SecretKeyRingProtector; +import org.pgpainless.signature.subpackets.BaseSignatureSubpackets; +import org.pgpainless.signature.subpackets.SignatureSubpacketGeneratorWrapper; + +/** + * Signature builder without restrictions on subpacket contents. + */ +public class UniversalSignatureBuilder extends AbstractSignatureBuilder { + + public UniversalSignatureBuilder(SignatureType signatureType, PGPSecretKey signingKey, SecretKeyRingProtector protector) + throws WrongPassphraseException { + super(signatureType, signingKey, protector); + } + + public UniversalSignatureBuilder(PGPSecretKey certificationKey, SecretKeyRingProtector protector, PGPSignature archetypeSignature) + throws WrongPassphraseException { + super(certificationKey, protector, archetypeSignature); + } + + @Override + protected boolean isValidSignatureType(SignatureType type) { + return true; + } + + public SignatureSubpacketGeneratorWrapper getHashedSubpackets() { + return hashedSubpackets; + } + + public SignatureSubpacketGeneratorWrapper getUnhashedSubpackets() { + return unhashedSubpackets; + } + + public void applyCallback(@Nullable BaseSignatureSubpackets.Callback callback) { + if (callback != null) { + callback.modifyHashedSubpackets(getHashedSubpackets()); + callback.modifyUnhashedSubpackets(getUnhashedSubpackets()); + } + } + + public PGPSignatureGenerator getSignatureGenerator() throws PGPException { + return buildAndInitSignatureGenerator(); + } +} diff --git a/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/BaseSignatureSubpackets.java b/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/BaseSignatureSubpackets.java index 3b824f11..0f05b0d5 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/BaseSignatureSubpackets.java +++ b/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/BaseSignatureSubpackets.java @@ -28,6 +28,16 @@ import org.pgpainless.algorithm.PublicKeyAlgorithm; public interface BaseSignatureSubpackets { + interface Callback { + default void modifyHashedSubpackets(SignatureSubpacketGeneratorWrapper subpackets) { + + } + + default void modifyUnhashedSubpackets(SignatureSubpacketGeneratorWrapper subpackets) { + + } + } + SignatureSubpacketGeneratorWrapper setIssuerFingerprintAndKeyId(PGPPublicKey key); SignatureSubpacketGeneratorWrapper setIssuerKeyId(long keyId);