001// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
002//
003// SPDX-License-Identifier: Apache-2.0
004
005package org.pgpainless.signature.builder;
006
007import javax.annotation.Nullable;
008
009import org.bouncycastle.openpgp.PGPException;
010import org.bouncycastle.openpgp.PGPPublicKey;
011import org.bouncycastle.openpgp.PGPSecretKey;
012import org.bouncycastle.openpgp.PGPSignature;
013import org.pgpainless.algorithm.SignatureType;
014import org.pgpainless.key.protection.SecretKeyRingProtector;
015import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
016
017public class DirectKeySignatureBuilder extends AbstractSignatureBuilder<DirectKeySignatureBuilder> {
018
019    public DirectKeySignatureBuilder(PGPSecretKey certificationKey, SecretKeyRingProtector protector, PGPSignature archetypeSignature)
020            throws PGPException {
021        super(certificationKey, protector, archetypeSignature);
022    }
023
024    public DirectKeySignatureBuilder(PGPSecretKey signingKey, SecretKeyRingProtector protector) throws PGPException {
025        super(SignatureType.DIRECT_KEY, signingKey, protector);
026    }
027
028    public SelfSignatureSubpackets getHashedSubpackets() {
029        return hashedSubpackets;
030    }
031
032    public SelfSignatureSubpackets getUnhashedSubpackets() {
033        return unhashedSubpackets;
034    }
035
036    public void applyCallback(@Nullable SelfSignatureSubpackets.Callback callback) {
037        if (callback != null) {
038            callback.modifyHashedSubpackets(getHashedSubpackets());
039            callback.modifyUnhashedSubpackets(getUnhashedSubpackets());
040        }
041    }
042
043    public PGPSignature build(PGPPublicKey key) throws PGPException {
044        return buildAndInitSignatureGenerator()
045                .generateCertification(key);
046    }
047
048    @Override
049    protected boolean isValidSignatureType(SignatureType type) {
050        return type == SignatureType.DIRECT_KEY;
051    }
052}