1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-23 11:27:57 +01:00

Add UniversalSignatureBuilder

This commit is contained in:
Paul Schaub 2021-11-06 14:56:16 +01:00
parent 15d42c294e
commit eb9ea23514
3 changed files with 84 additions and 0 deletions

View file

@ -11,8 +11,10 @@ import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignature;
import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.algorithm.SignatureType;
import org.pgpainless.exception.WrongPassphraseException; import org.pgpainless.exception.WrongPassphraseException;
import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.signature.subpackets.BaseSignatureSubpackets;
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets; import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
public final class SignatureFactory { public final class SignatureFactory {
@ -109,6 +111,20 @@ public final class SignatureFactory {
return certifier; 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) { private static boolean hasSignDataFlag(KeyFlag... flags) {
if (flags == null) { if (flags == null) {
return false; return false;
@ -120,4 +136,5 @@ public final class SignatureFactory {
} }
return false; return false;
} }
} }

View file

@ -0,0 +1,57 @@
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// 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<UniversalSignatureBuilder> {
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();
}
}

View file

@ -28,6 +28,16 @@ import org.pgpainless.algorithm.PublicKeyAlgorithm;
public interface BaseSignatureSubpackets { public interface BaseSignatureSubpackets {
interface Callback {
default void modifyHashedSubpackets(SignatureSubpacketGeneratorWrapper subpackets) {
}
default void modifyUnhashedSubpackets(SignatureSubpacketGeneratorWrapper subpackets) {
}
}
SignatureSubpacketGeneratorWrapper setIssuerFingerprintAndKeyId(PGPPublicKey key); SignatureSubpacketGeneratorWrapper setIssuerFingerprintAndKeyId(PGPPublicKey key);
SignatureSubpacketGeneratorWrapper setIssuerKeyId(long keyId); SignatureSubpacketGeneratorWrapper setIssuerKeyId(long keyId);