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

More progress!

This commit is contained in:
Paul Schaub 2021-11-06 17:37:01 +01:00
parent 8c49d37e1f
commit 44169ecf64
5 changed files with 117 additions and 16 deletions

View file

@ -15,27 +15,37 @@ import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector;
import org.pgpainless.algorithm.SignatureType;
import org.pgpainless.exception.WrongPassphraseException;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
import org.pgpainless.signature.subpackets.CertificationSubpackets;
public class CertificationSignatureBuilder extends AbstractSignatureBuilder<CertificationSignatureBuilder> {
public CertificationSignatureBuilder(PGPSecretKey certificationKey, SecretKeyRingProtector protector) throws WrongPassphraseException {
super(SignatureType.GENERIC_CERTIFICATION, certificationKey, protector);
public CertificationSignatureBuilder(PGPSecretKey certificationKey, SecretKeyRingProtector protector)
throws WrongPassphraseException {
this(SignatureType.GENERIC_CERTIFICATION, certificationKey, protector);
}
public CertificationSignatureBuilder(PGPSecretKey certificationKey, SecretKeyRingProtector protector, PGPSignature archetypeSignature) throws WrongPassphraseException {
public CertificationSignatureBuilder(SignatureType signatureType, PGPSecretKey signingKey, SecretKeyRingProtector protector)
throws WrongPassphraseException {
super(signatureType, signingKey, protector);
}
public CertificationSignatureBuilder(
PGPSecretKey certificationKey,
SecretKeyRingProtector protector,
PGPSignature archetypeSignature)
throws WrongPassphraseException {
super(certificationKey, protector, archetypeSignature);
}
public SelfSignatureSubpackets getHashedSubpackets() {
public CertificationSubpackets getHashedSubpackets() {
return hashedSubpackets;
}
public SelfSignatureSubpackets getUnhashedSubpackets() {
public CertificationSubpackets getUnhashedSubpackets() {
return unhashedSubpackets;
}
public void applyCallback(@Nullable SelfSignatureSubpackets.Callback callback) {
public void applyCallback(@Nullable CertificationSubpackets.Callback callback) {
if (callback != null) {
callback.modifyHashedSubpackets(getHashedSubpackets());
callback.modifyUnhashedSubpackets(getUnhashedSubpackets());
@ -46,7 +56,8 @@ public class CertificationSignatureBuilder extends AbstractSignatureBuilder<Cert
return buildAndInitSignatureGenerator().generateCertification(userId, certifiedKey);
}
public PGPSignature build(PGPPublicKey certifiedKey, PGPUserAttributeSubpacketVector userAttribute) throws PGPException {
public PGPSignature build(PGPPublicKey certifiedKey, PGPUserAttributeSubpacketVector userAttribute)
throws PGPException {
return buildAndInitSignatureGenerator().generateCertification(userAttribute, certifiedKey);
}

View file

@ -0,0 +1,75 @@
// 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.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector;
import org.pgpainless.algorithm.SignatureType;
import org.pgpainless.exception.WrongPassphraseException;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
public class SelfSignatureBuilder extends AbstractSignatureBuilder<SelfSignatureBuilder> {
public SelfSignatureBuilder(PGPSecretKey signingKey, SecretKeyRingProtector protector) throws WrongPassphraseException {
this(SignatureType.GENERIC_CERTIFICATION, signingKey, protector);
}
public SelfSignatureBuilder(SignatureType signatureType, PGPSecretKey signingKey, SecretKeyRingProtector protector)
throws WrongPassphraseException {
super(signatureType, signingKey, protector);
}
public SelfSignatureBuilder(
PGPSecretKey primaryKey,
SecretKeyRingProtector primaryKeyProtector,
PGPSignature oldCertification)
throws WrongPassphraseException {
super(primaryKey, primaryKeyProtector, oldCertification);
}
public SelfSignatureSubpackets getHashedSubpackets() {
return hashedSubpackets;
}
public SelfSignatureSubpackets getUnhashedSubpackets() {
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);
}
public PGPSignature build(PGPPublicKey certifiedKey, PGPUserAttributeSubpacketVector userAttribute)
throws PGPException {
return buildAndInitSignatureGenerator().generateCertification(userAttribute, certifiedKey);
}
@Override
protected boolean isValidSignatureType(SignatureType type) {
switch (type) {
case GENERIC_CERTIFICATION:
case NO_CERTIFICATION:
case CASUAL_CERTIFICATION:
case POSITIVE_CERTIFICATION:
case DIRECT_KEY:
return true;
default:
return false;
}
}
}

View file

@ -15,6 +15,7 @@ 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.CertificationSubpackets;
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
public final class SignatureFactory {
@ -29,8 +30,8 @@ public final class SignatureFactory {
@Nullable SelfSignatureSubpackets.Callback subkeyBindingSubpacketsCallback,
KeyFlag... flags) throws WrongPassphraseException {
if (hasSignDataFlag(flags)) {
throw new IllegalArgumentException("Binding a subkey with SIGN_DATA flag requires primary key backsig." +
"Please use the method bindSigningSubkey().");
throw new IllegalArgumentException("Binding a subkey with SIGN_DATA flag requires primary key backsig.\n" +
"Please use the method bindSigningSubkey() instead.");
}
return bindSubkey(primaryKey, primaryKeyProtector, subkeyBindingSubpacketsCallback, flags);
@ -85,25 +86,26 @@ public final class SignatureFactory {
return primaryKeyBinder;
}
public static CertificationSignatureBuilder selfCertifyUserId(
public static SelfSignatureBuilder selfCertifyUserId(
PGPSecretKey primaryKey,
SecretKeyRingProtector primaryKeyProtector,
@Nullable SelfSignatureSubpackets.Callback selfSignatureCallback,
KeyFlag... flags) throws WrongPassphraseException {
CertificationSignatureBuilder certifier = new CertificationSignatureBuilder(primaryKey, primaryKeyProtector);
SelfSignatureBuilder certifier = new SelfSignatureBuilder(SignatureType.POSITIVE_CERTIFICATION, primaryKey, primaryKeyProtector);
certifier.getHashedSubpackets().setKeyFlags(flags);
certifier.applyCallback(selfSignatureCallback);
return certifier;
}
public static CertificationSignatureBuilder renewSelfCertification(
public static SelfSignatureBuilder renewSelfCertification(
PGPSecretKey primaryKey,
SecretKeyRingProtector primaryKeyProtector,
@Nullable SelfSignatureSubpackets.Callback selfSignatureCallback,
PGPSignature oldCertification) throws WrongPassphraseException {
CertificationSignatureBuilder certifier = new CertificationSignatureBuilder(
SelfSignatureBuilder certifier = new SelfSignatureBuilder(
primaryKey, primaryKeyProtector, oldCertification);
certifier.applyCallback(selfSignatureCallback);
@ -111,6 +113,18 @@ public final class SignatureFactory {
return certifier;
}
public static CertificationSignatureBuilder certifyUserId(
PGPSecretKey signingKey,
SecretKeyRingProtector signingKeyProtector,
@Nullable CertificationSubpackets.Callback subpacketsCallback)
throws WrongPassphraseException {
CertificationSignatureBuilder certifier = new CertificationSignatureBuilder(signingKey, signingKeyProtector);
certifier.applyCallback(subpacketsCallback);
return certifier;
}
public static UniversalSignatureBuilder universalSignature(
SignatureType signatureType,
PGPSecretKey signingKey,

View file

@ -24,6 +24,7 @@ import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.signature.builder.CertificationSignatureBuilder;
import org.pgpainless.signature.builder.DirectKeySignatureBuilder;
import org.pgpainless.signature.builder.SelfSignatureBuilder;
public class ProofUtil {
@ -73,7 +74,7 @@ public class ProofUtil {
throw new NoSuchElementException("No previous valid user-id certification found.");
}
CertificationSignatureBuilder sigBuilder = new CertificationSignatureBuilder(certificationKey, protector, previousCertification);
SelfSignatureBuilder sigBuilder = new SelfSignatureBuilder(certificationKey, protector, previousCertification);
for (Proof proof : proofs) {
sigBuilder.getHashedSubpackets().addNotationData(false, proof.getNotationName(), proof.getNotationValue());
}

View file

@ -49,7 +49,7 @@ import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.key.util.RevocationAttributes;
public class SignatureSubpacketGeneratorWrapper
implements BaseSignatureSubpackets, SelfSignatureSubpackets, RevocationSignatureSubpackets {
implements BaseSignatureSubpackets, SelfSignatureSubpackets, CertificationSubpackets, RevocationSignatureSubpackets {
private SignatureCreationTime signatureCreationTime;
private SignatureExpirationTime signatureExpirationTime;