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

Create applyCallback util methods

This commit is contained in:
Paul Schaub 2021-11-03 14:03:30 +01:00
parent 352f099d8a
commit 8212fe1cc7
7 changed files with 64 additions and 40 deletions

View file

@ -5,6 +5,7 @@
package org.pgpainless.signature.builder; package org.pgpainless.signature.builder;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
@ -34,6 +35,13 @@ public class CertificationSignatureBuilder extends AbstractSignatureBuilder<Cert
return unhashedSubpackets; return unhashedSubpackets;
} }
public void applyCallback(@Nullable SelfSignatureSubpackets.Callback callback) {
if (callback != null) {
callback.modifyHashedSubpackets(getHashedSubpackets());
callback.modifyUnhashedSubpackets(getUnhashedSubpackets());
}
}
public PGPSignature build(PGPPublicKey certifiedKey, String userId) throws PGPException { public PGPSignature build(PGPPublicKey certifiedKey, String userId) throws PGPException {
return buildAndInitSignatureGenerator().generateCertification(userId, certifiedKey); return buildAndInitSignatureGenerator().generateCertification(userId, certifiedKey);
} }

View file

@ -4,6 +4,8 @@
package org.pgpainless.signature.builder; package org.pgpainless.signature.builder;
import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
@ -31,6 +33,13 @@ public class DirectKeySignatureBuilder extends AbstractSignatureBuilder<DirectKe
return unhashedSubpackets; return unhashedSubpackets;
} }
public void applyCallback(@Nullable SelfSignatureSubpackets.Callback callback) {
if (callback != null) {
callback.modifyHashedSubpackets(getHashedSubpackets());
callback.modifyUnhashedSubpackets(getUnhashedSubpackets());
}
}
public PGPSignature build(PGPPublicKey key) throws PGPException { public PGPSignature build(PGPPublicKey key) throws PGPException {
return buildAndInitSignatureGenerator() return buildAndInitSignatureGenerator()
.generateCertification(key); .generateCertification(key);

View file

@ -4,6 +4,8 @@
package org.pgpainless.signature.builder; package org.pgpainless.signature.builder;
import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
@ -28,6 +30,13 @@ public class PrimaryKeyBindingSignatureBuilder extends AbstractSignatureBuilder<
return unhashedSubpackets; return unhashedSubpackets;
} }
public void applyCallback(@Nullable SelfSignatureSubpackets.Callback callback) {
if (callback != null) {
callback.modifyHashedSubpackets(getHashedSubpackets());
callback.modifyUnhashedSubpackets(getUnhashedSubpackets());
}
}
@Override @Override
protected boolean isValidSignatureType(SignatureType type) { protected boolean isValidSignatureType(SignatureType type) {
return type == SignatureType.PRIMARYKEY_BINDING; return type == SignatureType.PRIMARYKEY_BINDING;

View file

@ -4,12 +4,15 @@
package org.pgpainless.signature.builder; package org.pgpainless.signature.builder;
import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignature;
import org.pgpainless.algorithm.SignatureType; 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.RevocationSignatureSubpackets; import org.pgpainless.signature.subpackets.RevocationSignatureSubpackets;
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
public class RevocationSignatureBuilder extends AbstractSignatureBuilder<RevocationSignatureBuilder> { public class RevocationSignatureBuilder extends AbstractSignatureBuilder<RevocationSignatureBuilder> {
@ -37,6 +40,13 @@ public class RevocationSignatureBuilder extends AbstractSignatureBuilder<Revocat
return unhashedSubpackets; return unhashedSubpackets;
} }
public void applyCallback(@Nullable RevocationSignatureSubpackets.Callback callback) {
if (callback != null) {
callback.modifyHashedSubpackets(getHashedSubpackets());
callback.modifyUnhashedSubpackets(getUnhashedSubpackets());
}
}
public PGPSignature build() { public PGPSignature build() {
return null; return null;
} }

View file

@ -8,7 +8,6 @@ import java.io.IOException;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
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;
@ -48,8 +47,9 @@ public final class SignatureFactory {
SubkeyBindingSignatureBuilder subkeyBinder = bindSubkey(primaryKey, primaryKeyProtector, subkeyBindingSubpacketsCallback, flags); SubkeyBindingSignatureBuilder subkeyBinder = bindSubkey(primaryKey, primaryKeyProtector, subkeyBindingSubpacketsCallback, flags);
if (hasSignDataFlag(flags)) { if (hasSignDataFlag(flags)) {
PGPSignature backsig = createPrimaryKeyBinding( PGPSignature backsig = bindPrimaryKey(
subkey, subkeyProtector, primaryKeyBindingSubpacketsCallback, primaryKey.getPublicKey()); subkey, subkeyProtector, primaryKeyBindingSubpacketsCallback)
.build(primaryKey.getPublicKey());
subkeyBinder.getHashedSubpackets().addEmbeddedSignature(backsig); subkeyBinder.getHashedSubpackets().addEmbeddedSignature(backsig);
} }
@ -65,13 +65,9 @@ public final class SignatureFactory {
} }
SubkeyBindingSignatureBuilder subkeyBinder = new SubkeyBindingSignatureBuilder(primaryKey, primaryKeyProtector); SubkeyBindingSignatureBuilder subkeyBinder = new SubkeyBindingSignatureBuilder(primaryKey, primaryKeyProtector);
SelfSignatureSubpackets hashedSubpackets = subkeyBinder.getHashedSubpackets(); SelfSignatureSubpackets hashedSubpackets = subkeyBinder.getHashedSubpackets();
SelfSignatureSubpackets unhashedSubpackets = subkeyBinder.getUnhashedSubpackets();
hashedSubpackets.setKeyFlags(flags); hashedSubpackets.setKeyFlags(flags);
if (subkeyBindingSubpacketsCallback != null) { subkeyBinder.applyCallback(subkeyBindingSubpacketsCallback);
subkeyBindingSubpacketsCallback.modifyHashedSubpackets(hashedSubpackets);
subkeyBindingSubpacketsCallback.modifyUnhashedSubpackets(unhashedSubpackets);
}
return subkeyBinder; return subkeyBinder;
} }
@ -82,24 +78,11 @@ public final class SignatureFactory {
@Nullable SelfSignatureSubpackets.Callback primaryKeyBindingSubpacketsCallback) throws WrongPassphraseException { @Nullable SelfSignatureSubpackets.Callback primaryKeyBindingSubpacketsCallback) throws WrongPassphraseException {
PrimaryKeyBindingSignatureBuilder primaryKeyBinder = new PrimaryKeyBindingSignatureBuilder(subkey, subkeyProtector); PrimaryKeyBindingSignatureBuilder primaryKeyBinder = new PrimaryKeyBindingSignatureBuilder(subkey, subkeyProtector);
if (primaryKeyBindingSubpacketsCallback != null) { primaryKeyBinder.applyCallback(primaryKeyBindingSubpacketsCallback);
primaryKeyBindingSubpacketsCallback.modifyHashedSubpackets(primaryKeyBinder.getHashedSubpackets());
primaryKeyBindingSubpacketsCallback.modifyUnhashedSubpackets(primaryKeyBinder.getUnhashedSubpackets());
}
return primaryKeyBinder; return primaryKeyBinder;
} }
public static PGPSignature createPrimaryKeyBinding(
PGPSecretKey subkey,
SecretKeyRingProtector subkeyProtector,
@Nullable SelfSignatureSubpackets.Callback primaryKeyBindingSubpacketsCallback,
PGPPublicKey primaryKey)
throws PGPException {
return bindPrimaryKey(subkey, subkeyProtector, primaryKeyBindingSubpacketsCallback)
.build(primaryKey);
}
public static CertificationSignatureBuilder selfCertifyUserId( public static CertificationSignatureBuilder selfCertifyUserId(
PGPSecretKey primaryKey, PGPSecretKey primaryKey,
SecretKeyRingProtector primaryKeyProtector, SecretKeyRingProtector primaryKeyProtector,
@ -108,10 +91,8 @@ public final class SignatureFactory {
CertificationSignatureBuilder certifier = new CertificationSignatureBuilder(primaryKey, primaryKeyProtector); CertificationSignatureBuilder certifier = new CertificationSignatureBuilder(primaryKey, primaryKeyProtector);
certifier.getHashedSubpackets().setKeyFlags(flags); certifier.getHashedSubpackets().setKeyFlags(flags);
if (selfSignatureCallback != null) { certifier.applyCallback(selfSignatureCallback);
selfSignatureCallback.modifyHashedSubpackets(certifier.getHashedSubpackets());
selfSignatureCallback.modifyUnhashedSubpackets(certifier.getUnhashedSubpackets());
}
return certifier; return certifier;
} }
@ -120,22 +101,12 @@ public final class SignatureFactory {
SecretKeyRingProtector primaryKeyProtector, SecretKeyRingProtector primaryKeyProtector,
@Nullable SelfSignatureSubpackets.Callback selfSignatureCallback, @Nullable SelfSignatureSubpackets.Callback selfSignatureCallback,
PGPSignature oldCertification) throws WrongPassphraseException { PGPSignature oldCertification) throws WrongPassphraseException {
CertificationSignatureBuilder certifier = CertificationSignatureBuilder certifier = new CertificationSignatureBuilder(
new CertificationSignatureBuilder(primaryKey, primaryKeyProtector, oldCertification); primaryKey, primaryKeyProtector, oldCertification);
// TODO certifier.applyCallback(selfSignatureCallback);
return null;
}
public static PGPSignature createUserIdSelfCertification( return certifier;
String userId,
PGPSecretKey primaryKey,
SecretKeyRingProtector primaryKeyProtector,
@Nullable SelfSignatureSubpackets.Callback selfSignatureCallback,
KeyFlag... flags)
throws PGPException {
return selfCertifyUserId(primaryKey, primaryKeyProtector, selfSignatureCallback, flags)
.build(primaryKey.getPublicKey(), userId);
} }
private static boolean hasSignDataFlag(KeyFlag... flags) { private static boolean hasSignDataFlag(KeyFlag... flags) {

View file

@ -4,6 +4,8 @@
package org.pgpainless.signature.builder; package org.pgpainless.signature.builder;
import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
@ -33,6 +35,13 @@ public class SubkeyBindingSignatureBuilder extends AbstractSignatureBuilder<Subk
return unhashedSubpackets; return unhashedSubpackets;
} }
public void applyCallback(@Nullable SelfSignatureSubpackets.Callback callback) {
if (callback != null) {
callback.modifyHashedSubpackets(getHashedSubpackets());
callback.modifyUnhashedSubpackets(getUnhashedSubpackets());
}
}
public PGPSignature build(PGPPublicKey subkey) throws PGPException { public PGPSignature build(PGPPublicKey subkey) throws PGPException {
return buildAndInitSignatureGenerator() return buildAndInitSignatureGenerator()
.generateCertification(publicSigningKey, subkey); .generateCertification(publicSigningKey, subkey);

View file

@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
/**
* Classes related to OpenPGP signature verification.
*/
package org.pgpainless.signature.consumer;