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 PrimaryKeyBindingSignatureBuilder extends AbstractSignatureBuilder<PrimaryKeyBindingSignatureBuilder> {
018
019    public PrimaryKeyBindingSignatureBuilder(PGPSecretKey subkey, SecretKeyRingProtector subkeyProtector)
020            throws PGPException {
021        super(SignatureType.PRIMARYKEY_BINDING, subkey, subkeyProtector);
022    }
023
024    public SelfSignatureSubpackets getHashedSubpackets() {
025        return hashedSubpackets;
026    }
027
028    public SelfSignatureSubpackets getUnhashedSubpackets() {
029        return unhashedSubpackets;
030    }
031
032    public void applyCallback(@Nullable SelfSignatureSubpackets.Callback callback) {
033        if (callback != null) {
034            callback.modifyHashedSubpackets(getHashedSubpackets());
035            callback.modifyUnhashedSubpackets(getUnhashedSubpackets());
036        }
037    }
038
039    @Override
040    protected boolean isValidSignatureType(SignatureType type) {
041        return type == SignatureType.PRIMARYKEY_BINDING;
042    }
043
044    public PGPSignature build(PGPPublicKey primaryKey) throws PGPException {
045        return buildAndInitSignatureGenerator()
046                .generateCertification(primaryKey, publicSigningKey);
047    }
048}