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;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
@ -34,6 +35,13 @@ public class CertificationSignatureBuilder extends AbstractSignatureBuilder<Cert
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 {
return buildAndInitSignatureGenerator().generateCertification(userId, certifiedKey);
}

View file

@ -4,6 +4,8 @@
package org.pgpainless.signature.builder;
import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
@ -31,6 +33,13 @@ public class DirectKeySignatureBuilder extends AbstractSignatureBuilder<DirectKe
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 {
return buildAndInitSignatureGenerator()
.generateCertification(key);

View file

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

View file

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

View file

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

View file

@ -4,6 +4,8 @@
package org.pgpainless.signature.builder;
import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
@ -33,6 +35,13 @@ public class SubkeyBindingSignatureBuilder extends AbstractSignatureBuilder<Subk
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 {
return buildAndInitSignatureGenerator()
.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;