1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-15 17:02:06 +01:00

Change return values of signature subpackets subclasses

This commit is contained in:
Paul Schaub 2021-11-06 15:40:10 +01:00
parent ed96bcd109
commit 8c49d37e1f
6 changed files with 135 additions and 103 deletions

View file

@ -44,10 +44,12 @@ public abstract class AbstractSignatureBuilder<B extends AbstractSignatureBuilde
this.hashAlgorithm = negotiateHashAlgorithm(publicSigningKey); this.hashAlgorithm = negotiateHashAlgorithm(publicSigningKey);
unhashedSubpackets = SignatureSubpacketGeneratorWrapper.createEmptySubpackets(); unhashedSubpackets = SignatureSubpacketGeneratorWrapper.createEmptySubpackets();
// Prepopulate hashed subpackets with default values (key-id etc.)
hashedSubpackets = SignatureSubpacketGeneratorWrapper.createHashedSubpackets(publicSigningKey); hashedSubpackets = SignatureSubpacketGeneratorWrapper.createHashedSubpackets(publicSigningKey);
} }
public AbstractSignatureBuilder(PGPSecretKey certificationKey, SecretKeyRingProtector protector, PGPSignature archetypeSignature) throws WrongPassphraseException { public AbstractSignatureBuilder(PGPSecretKey certificationKey, SecretKeyRingProtector protector, PGPSignature archetypeSignature)
throws WrongPassphraseException {
SignatureType type = SignatureType.valueOf(archetypeSignature.getSignatureType()); SignatureType type = SignatureType.valueOf(archetypeSignature.getSignatureType());
if (!isValidSignatureType(type)) { if (!isValidSignatureType(type)) {
throw new IllegalArgumentException("Invalid signature type."); throw new IllegalArgumentException("Invalid signature type.");
@ -61,12 +63,26 @@ public abstract class AbstractSignatureBuilder<B extends AbstractSignatureBuilde
hashedSubpackets = SignatureSubpacketGeneratorWrapper.refreshHashedSubpackets(publicSigningKey, archetypeSignature); hashedSubpackets = SignatureSubpacketGeneratorWrapper.refreshHashedSubpackets(publicSigningKey, archetypeSignature);
} }
/**
* Negotiate a {@link HashAlgorithm} to be used when creating the signature.
*
* @param publicKey signing public key
* @return hash algorithm
*/
protected HashAlgorithm negotiateHashAlgorithm(PGPPublicKey publicKey) { protected HashAlgorithm negotiateHashAlgorithm(PGPPublicKey publicKey) {
Set<HashAlgorithm> hashAlgorithmPreferences = OpenPgpKeyAttributeUtil.getOrGuessPreferredHashAlgorithms(publicKey); Set<HashAlgorithm> hashAlgorithmPreferences = OpenPgpKeyAttributeUtil.getOrGuessPreferredHashAlgorithms(publicKey);
return HashAlgorithmNegotiator.negotiateSignatureHashAlgorithm(PGPainless.getPolicy()) return HashAlgorithmNegotiator.negotiateSignatureHashAlgorithm(PGPainless.getPolicy())
.negotiateHashAlgorithm(hashAlgorithmPreferences); .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) { public B setSignatureType(SignatureType type) {
if (!isValidSignatureType(type)) { if (!isValidSignatureType(type)) {
throw new IllegalArgumentException("Invalid signature type: " + type); throw new IllegalArgumentException("Invalid signature type: " + type);
@ -75,6 +91,13 @@ public abstract class AbstractSignatureBuilder<B extends AbstractSignatureBuilde
return (B) this; return (B) this;
} }
/**
* Build an instance of {@link PGPSignatureGenerator} initialized with the signing key
* and with hashed and unhashed subpackets.
*
* @return pgp signature generator
* @throws PGPException
*/
protected PGPSignatureGenerator buildAndInitSignatureGenerator() throws PGPException { protected PGPSignatureGenerator buildAndInitSignatureGenerator() throws PGPException {
PGPSignatureGenerator generator = new PGPSignatureGenerator( PGPSignatureGenerator generator = new PGPSignatureGenerator(
ImplementationFactory.getInstance().getPGPContentSignerBuilder( ImplementationFactory.getInstance().getPGPContentSignerBuilder(
@ -87,5 +110,12 @@ public abstract class AbstractSignatureBuilder<B extends AbstractSignatureBuilde
return generator; return generator;
} }
/**
* Return true if the given {@link SignatureType} is a valid choice for the concrete implementation
* of {@link AbstractSignatureBuilder}.
*
* @param type type
* @return return true if valid, false otherwise
*/
protected abstract boolean isValidSignatureType(SignatureType type); protected abstract boolean isValidSignatureType(SignatureType type);
} }

View file

@ -28,91 +28,85 @@ import org.pgpainless.algorithm.PublicKeyAlgorithm;
public interface BaseSignatureSubpackets { public interface BaseSignatureSubpackets {
interface Callback { interface Callback extends SignatureSubpacketCallback<SignatureSubpacketGeneratorWrapper> {
default void modifyHashedSubpackets(SignatureSubpacketGeneratorWrapper subpackets) {
} }
default void modifyUnhashedSubpackets(SignatureSubpacketGeneratorWrapper subpackets) { BaseSignatureSubpackets setIssuerFingerprintAndKeyId(PGPPublicKey key);
} BaseSignatureSubpackets setIssuerKeyId(long keyId);
}
BaseSignatureSubpackets setIssuerKeyId(boolean isCritical, long keyId);
SignatureSubpacketGeneratorWrapper setIssuerFingerprintAndKeyId(PGPPublicKey key);
BaseSignatureSubpackets setIssuerKeyId(@Nullable IssuerKeyID issuerKeyID);
SignatureSubpacketGeneratorWrapper setIssuerKeyId(long keyId);
BaseSignatureSubpackets setIssuerFingerprint(@Nonnull PGPPublicKey key);
SignatureSubpacketGeneratorWrapper setIssuerKeyId(boolean isCritical, long keyId);
BaseSignatureSubpackets setIssuerFingerprint(boolean isCritical, @Nonnull PGPPublicKey key);
SignatureSubpacketGeneratorWrapper setIssuerKeyId(@Nullable IssuerKeyID issuerKeyID);
BaseSignatureSubpackets setIssuerFingerprint(@Nullable IssuerFingerprint fingerprint);
SignatureSubpacketGeneratorWrapper setIssuerFingerprint(@Nonnull PGPPublicKey key);
BaseSignatureSubpackets setSignatureCreationTime(@Nonnull Date creationTime);
SignatureSubpacketGeneratorWrapper setIssuerFingerprint(boolean isCritical, @Nonnull PGPPublicKey key);
BaseSignatureSubpackets setSignatureCreationTime(boolean isCritical, @Nonnull Date creationTime);
SignatureSubpacketGeneratorWrapper setIssuerFingerprint(@Nullable IssuerFingerprint fingerprint);
BaseSignatureSubpackets setSignatureCreationTime(@Nullable SignatureCreationTime signatureCreationTime);
SignatureSubpacketGeneratorWrapper setSignatureCreationTime(@Nonnull Date creationTime);
BaseSignatureSubpackets setSignatureExpirationTime(@Nonnull Date creationTime, @Nonnull Date expirationTime);
SignatureSubpacketGeneratorWrapper setSignatureCreationTime(boolean isCritical, @Nonnull Date creationTime);
BaseSignatureSubpackets setSignatureExpirationTime(boolean isCritical, @Nonnull Date creationTime, @Nonnull Date expirationTime);
SignatureSubpacketGeneratorWrapper setSignatureCreationTime(@Nullable SignatureCreationTime signatureCreationTime);
BaseSignatureSubpackets setSignatureExpirationTime(boolean isCritical, long seconds);
SignatureSubpacketGeneratorWrapper setSignatureExpirationTime(@Nonnull Date creationTime, @Nonnull Date expirationTime);
BaseSignatureSubpackets setSignatureExpirationTime(@Nullable SignatureExpirationTime expirationTime);
SignatureSubpacketGeneratorWrapper setSignatureExpirationTime(boolean isCritical, @Nonnull Date creationTime, @Nonnull Date expirationTime);
BaseSignatureSubpackets setSignerUserId(@Nonnull String userId);
SignatureSubpacketGeneratorWrapper setSignatureExpirationTime(boolean isCritical, long seconds);
BaseSignatureSubpackets setSignerUserId(boolean isCritical, @Nonnull String userId);
SignatureSubpacketGeneratorWrapper setSignatureExpirationTime(@Nullable SignatureExpirationTime expirationTime);
BaseSignatureSubpackets setSignerUserId(@Nullable SignerUserID signerUserId);
SignatureSubpacketGeneratorWrapper setSignerUserId(@Nonnull String userId);
BaseSignatureSubpackets addNotationData(boolean isCritical, @Nonnull String notationName, @Nonnull String notationValue);
SignatureSubpacketGeneratorWrapper setSignerUserId(boolean isCritical, @Nonnull String userId);
BaseSignatureSubpackets addNotationData(boolean isCritical, boolean isHumanReadable, @Nonnull String notationName, @Nonnull String notationValue);
SignatureSubpacketGeneratorWrapper setSignerUserId(@Nullable SignerUserID signerUserId);
BaseSignatureSubpackets addNotationData(@Nonnull NotationData notationData);
SignatureSubpacketGeneratorWrapper addNotationData(boolean isCritical, @Nonnull String notationName, @Nonnull String notationValue);
BaseSignatureSubpackets clearNotationData();
SignatureSubpacketGeneratorWrapper addNotationData(boolean isCritical, boolean isHumanReadable, @Nonnull String notationName, @Nonnull String notationValue);
BaseSignatureSubpackets addIntendedRecipientFingerprint(@Nonnull PGPPublicKey recipient);
SignatureSubpacketGeneratorWrapper addNotationData(@Nonnull NotationData notationData);
BaseSignatureSubpackets addIntendedRecipientFingerprint(boolean isCritical, @Nonnull PGPPublicKey recipient);
SignatureSubpacketGeneratorWrapper clearNotationData();
BaseSignatureSubpackets addIntendedRecipientFingerprint(IntendedRecipientFingerprint intendedRecipientFingerprint);
SignatureSubpacketGeneratorWrapper addIntendedRecipientFingerprint(@Nonnull PGPPublicKey recipient);
BaseSignatureSubpackets clearIntendedRecipientFingerprints();
SignatureSubpacketGeneratorWrapper addIntendedRecipientFingerprint(boolean isCritical, @Nonnull PGPPublicKey recipient);
BaseSignatureSubpackets setExportable(boolean isCritical, boolean isExportable);
SignatureSubpacketGeneratorWrapper addIntendedRecipientFingerprint(IntendedRecipientFingerprint intendedRecipientFingerprint);
BaseSignatureSubpackets setExportable(@Nullable Exportable exportable);
SignatureSubpacketGeneratorWrapper clearIntendedRecipientFingerprints();
BaseSignatureSubpackets setRevocable(boolean isCritical, boolean isRevocable);
SignatureSubpacketGeneratorWrapper setExportable(boolean isCritical, boolean isExportable);
BaseSignatureSubpackets setRevocable(@Nullable Revocable revocable);
SignatureSubpacketGeneratorWrapper setExportable(@Nullable Exportable exportable);
BaseSignatureSubpackets setSignatureTarget(@Nonnull PublicKeyAlgorithm keyAlgorithm, @Nonnull HashAlgorithm hashAlgorithm, @Nonnull byte[] hashData);
SignatureSubpacketGeneratorWrapper setRevocable(boolean isCritical, boolean isRevocable);
BaseSignatureSubpackets setSignatureTarget(boolean isCritical, @Nonnull PublicKeyAlgorithm keyAlgorithm, @Nonnull HashAlgorithm hashAlgorithm, @Nonnull byte[] hashData);
SignatureSubpacketGeneratorWrapper setRevocable(@Nullable Revocable revocable);
BaseSignatureSubpackets setSignatureTarget(@Nullable SignatureTarget signatureTarget);
SignatureSubpacketGeneratorWrapper setSignatureTarget(@Nonnull PublicKeyAlgorithm keyAlgorithm, @Nonnull HashAlgorithm hashAlgorithm, @Nonnull byte[] hashData);
BaseSignatureSubpackets setTrust(int depth, int amount);
SignatureSubpacketGeneratorWrapper setSignatureTarget(boolean isCritical, @Nonnull PublicKeyAlgorithm keyAlgorithm, @Nonnull HashAlgorithm hashAlgorithm, @Nonnull byte[] hashData);
BaseSignatureSubpackets setTrust(boolean isCritical, int depth, int amount);
SignatureSubpacketGeneratorWrapper setSignatureTarget(@Nullable SignatureTarget signatureTarget);
BaseSignatureSubpackets setTrust(@Nullable TrustSignature trust);
SignatureSubpacketGeneratorWrapper setTrust(int depth, int amount);
BaseSignatureSubpackets addEmbeddedSignature(@Nonnull PGPSignature signature) throws IOException;
SignatureSubpacketGeneratorWrapper setTrust(boolean isCritical, int depth, int amount);
BaseSignatureSubpackets addEmbeddedSignature(boolean isCritical, @Nonnull PGPSignature signature) throws IOException;
SignatureSubpacketGeneratorWrapper setTrust(@Nullable TrustSignature trust);
BaseSignatureSubpackets addEmbeddedSignature(@Nonnull EmbeddedSignature embeddedSignature);
SignatureSubpacketGeneratorWrapper addEmbeddedSignature(@Nonnull PGPSignature signature) throws IOException;
BaseSignatureSubpackets clearEmbeddedSignatures();
SignatureSubpacketGeneratorWrapper addEmbeddedSignature(boolean isCritical, @Nonnull PGPSignature signature) throws IOException;
SignatureSubpacketGeneratorWrapper addEmbeddedSignature(@Nonnull EmbeddedSignature embeddedSignature);
SignatureSubpacketGeneratorWrapper clearEmbeddedSignatures();
} }

View file

@ -6,13 +6,7 @@ package org.pgpainless.signature.subpackets;
public interface CertificationSubpackets extends BaseSignatureSubpackets { public interface CertificationSubpackets extends BaseSignatureSubpackets {
interface Callback { interface Callback extends SignatureSubpacketCallback<BaseSignatureSubpackets> {
default void modifyHashedSubpackets(CertificationSubpackets subpackets) {
}
default void modifyUnhashedSubpackets(CertificationSubpackets subpackets) {
} }
} }
}

View file

@ -12,21 +12,15 @@ import org.pgpainless.key.util.RevocationAttributes;
public interface RevocationSignatureSubpackets extends BaseSignatureSubpackets { public interface RevocationSignatureSubpackets extends BaseSignatureSubpackets {
interface Callback { interface Callback extends SignatureSubpacketCallback<RevocationSignatureSubpackets> {
default void modifyHashedSubpackets(RevocationSignatureSubpackets subpackets) {
} }
default void modifyUnhashedSubpackets(RevocationSignatureSubpackets subpackets) { RevocationSignatureSubpackets setRevocationReason(RevocationAttributes revocationAttributes);
} RevocationSignatureSubpackets setRevocationReason(boolean isCritical, RevocationAttributes revocationAttributes);
}
RevocationSignatureSubpackets setRevocationReason(boolean isCritical, RevocationAttributes.Reason reason, @Nonnull String description);
SignatureSubpacketGeneratorWrapper setRevocationReason(RevocationAttributes revocationAttributes);
RevocationSignatureSubpackets setRevocationReason(@Nullable RevocationReason reason);
SignatureSubpacketGeneratorWrapper setRevocationReason(boolean isCritical, RevocationAttributes revocationAttributes);
SignatureSubpacketGeneratorWrapper setRevocationReason(boolean isCritical, RevocationAttributes.Reason reason, @Nonnull String description);
SignatureSubpacketGeneratorWrapper setRevocationReason(@Nullable RevocationReason reason);
} }

View file

@ -24,75 +24,69 @@ import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
public interface SelfSignatureSubpackets extends BaseSignatureSubpackets { public interface SelfSignatureSubpackets extends BaseSignatureSubpackets {
interface Callback { interface Callback extends SignatureSubpacketCallback<SelfSignatureSubpackets> {
default void modifyHashedSubpackets(SelfSignatureSubpackets subpackets) {
} }
default void modifyUnhashedSubpackets(SelfSignatureSubpackets subpackets) { SelfSignatureSubpackets setKeyFlags(KeyFlag... keyFlags);
} SelfSignatureSubpackets setKeyFlags(boolean isCritical, KeyFlag... keyFlags);
}
SelfSignatureSubpackets setKeyFlags(@Nullable KeyFlags keyFlags);
SignatureSubpacketGeneratorWrapper setKeyFlags(KeyFlag... keyFlags);
SelfSignatureSubpackets setPrimaryUserId();
SignatureSubpacketGeneratorWrapper setKeyFlags(boolean isCritical, KeyFlag... keyFlags);
SelfSignatureSubpackets setPrimaryUserId(boolean isCritical);
SignatureSubpacketGeneratorWrapper setKeyFlags(@Nullable KeyFlags keyFlags);
SelfSignatureSubpackets setPrimaryUserId(@Nullable PrimaryUserID primaryUserId);
SignatureSubpacketGeneratorWrapper setPrimaryUserId();
SelfSignatureSubpackets setKeyExpirationTime(@Nonnull PGPPublicKey key, @Nonnull Date keyExpirationTime);
SignatureSubpacketGeneratorWrapper setPrimaryUserId(boolean isCritical);
SelfSignatureSubpackets setKeyExpirationTime(@Nonnull Date keyCreationTime, @Nonnull Date keyExpirationTime);
SignatureSubpacketGeneratorWrapper setPrimaryUserId(@Nullable PrimaryUserID primaryUserId);
SelfSignatureSubpackets setKeyExpirationTime(boolean isCritical, @Nonnull Date keyCreationTime, @Nonnull Date keyExpirationTime);
SignatureSubpacketGeneratorWrapper setKeyExpirationTime(@Nonnull PGPPublicKey key, @Nonnull Date keyExpirationTime);
SelfSignatureSubpackets setKeyExpirationTime(boolean isCritical, long secondsFromCreationToExpiration);
SignatureSubpacketGeneratorWrapper setKeyExpirationTime(@Nonnull Date keyCreationTime, @Nonnull Date keyExpirationTime);
SelfSignatureSubpackets setKeyExpirationTime(@Nullable KeyExpirationTime keyExpirationTime);
SignatureSubpacketGeneratorWrapper setKeyExpirationTime(boolean isCritical, @Nonnull Date keyCreationTime, @Nonnull Date keyExpirationTime);
SelfSignatureSubpackets setPreferredCompressionAlgorithms(CompressionAlgorithm... algorithms);
SignatureSubpacketGeneratorWrapper setKeyExpirationTime(boolean isCritical, long secondsFromCreationToExpiration);
SelfSignatureSubpackets setPreferredCompressionAlgorithms(Set<CompressionAlgorithm> algorithms);
SignatureSubpacketGeneratorWrapper setKeyExpirationTime(@Nullable KeyExpirationTime keyExpirationTime);
SelfSignatureSubpackets setPreferredCompressionAlgorithms(boolean isCritical, Set<CompressionAlgorithm> algorithms);
SignatureSubpacketGeneratorWrapper setPreferredCompressionAlgorithms(CompressionAlgorithm... algorithms);
SelfSignatureSubpackets setPreferredCompressionAlgorithms(@Nullable PreferredAlgorithms algorithms);
SignatureSubpacketGeneratorWrapper setPreferredCompressionAlgorithms(Set<CompressionAlgorithm> algorithms);
SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(SymmetricKeyAlgorithm... algorithms);
SignatureSubpacketGeneratorWrapper setPreferredCompressionAlgorithms(boolean isCritical, Set<CompressionAlgorithm> algorithms);
SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(Set<SymmetricKeyAlgorithm> algorithms);
SignatureSubpacketGeneratorWrapper setPreferredCompressionAlgorithms(@Nullable PreferredAlgorithms algorithms);
SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(boolean isCritical, Set<SymmetricKeyAlgorithm> algorithms);
SignatureSubpacketGeneratorWrapper setPreferredSymmetricKeyAlgorithms(SymmetricKeyAlgorithm... algorithms);
SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(@Nullable PreferredAlgorithms algorithms);
SignatureSubpacketGeneratorWrapper setPreferredSymmetricKeyAlgorithms(Set<SymmetricKeyAlgorithm> algorithms);
SelfSignatureSubpackets setPreferredHashAlgorithms(HashAlgorithm... algorithms);
SignatureSubpacketGeneratorWrapper setPreferredSymmetricKeyAlgorithms(boolean isCritical, Set<SymmetricKeyAlgorithm> algorithms);
SelfSignatureSubpackets setPreferredHashAlgorithms(Set<HashAlgorithm> algorithms);
SignatureSubpacketGeneratorWrapper setPreferredSymmetricKeyAlgorithms(@Nullable PreferredAlgorithms algorithms);
SelfSignatureSubpackets setPreferredHashAlgorithms(boolean isCritical, Set<HashAlgorithm> algorithms);
SignatureSubpacketGeneratorWrapper setPreferredHashAlgorithms(HashAlgorithm... algorithms);
SelfSignatureSubpackets setPreferredHashAlgorithms(@Nullable PreferredAlgorithms algorithms);
SignatureSubpacketGeneratorWrapper setPreferredHashAlgorithms(Set<HashAlgorithm> algorithms);
SelfSignatureSubpackets addRevocationKey(@Nonnull PGPPublicKey revocationKey);
SignatureSubpacketGeneratorWrapper setPreferredHashAlgorithms(boolean isCritical, Set<HashAlgorithm> algorithms);
SelfSignatureSubpackets addRevocationKey(boolean isCritical, @Nonnull PGPPublicKey revocationKey);
SignatureSubpacketGeneratorWrapper setPreferredHashAlgorithms(@Nullable PreferredAlgorithms algorithms);
SelfSignatureSubpackets addRevocationKey(boolean isCritical, boolean isSensitive, @Nonnull PGPPublicKey revocationKey);
SignatureSubpacketGeneratorWrapper addRevocationKey(@Nonnull PGPPublicKey revocationKey);
SelfSignatureSubpackets addRevocationKey(@Nonnull RevocationKey revocationKey);
SignatureSubpacketGeneratorWrapper addRevocationKey(boolean isCritical, @Nonnull PGPPublicKey revocationKey);
SelfSignatureSubpackets clearRevocationKeys();
SignatureSubpacketGeneratorWrapper addRevocationKey(boolean isCritical, boolean isSensitive, @Nonnull PGPPublicKey revocationKey);
SelfSignatureSubpackets setFeatures(Feature... features);
SignatureSubpacketGeneratorWrapper addRevocationKey(@Nonnull RevocationKey revocationKey);
SelfSignatureSubpackets setFeatures(boolean isCritical, Feature... features);
SignatureSubpacketGeneratorWrapper clearRevocationKeys();
SelfSignatureSubpackets setFeatures(@Nullable Features features);
SignatureSubpacketGeneratorWrapper setFeatures(Feature... features);
SignatureSubpacketGeneratorWrapper setFeatures(boolean isCritical, Feature... features);
SignatureSubpacketGeneratorWrapper setFeatures(@Nullable Features features);
} }

View file

@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.signature.subpackets;
public interface SignatureSubpacketCallback<S extends BaseSignatureSubpackets> {
/**
* 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) {
}
}