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 SubkeyBindingSignatureBuilder extends AbstractSignatureBuilder<SubkeyBindingSignatureBuilder> { 018 019 public SubkeyBindingSignatureBuilder(PGPSecretKey signingKey, SecretKeyRingProtector protector) 020 throws PGPException { 021 super(SignatureType.SUBKEY_BINDING, signingKey, protector); 022 } 023 024 public SubkeyBindingSignatureBuilder( 025 PGPSecretKey signingKey, 026 SecretKeyRingProtector protector, 027 PGPSignature oldSubkeyBinding) 028 throws PGPException { 029 super(signingKey, protector, requireValidSignatureType(oldSubkeyBinding)); 030 } 031 032 private static PGPSignature requireValidSignatureType(PGPSignature signature) { 033 if (signature.getSignatureType() == SignatureType.SUBKEY_BINDING.getCode()) { 034 return signature; 035 } 036 throw new IllegalArgumentException("Invalid signature type."); 037 } 038 039 @Override 040 protected boolean isValidSignatureType(SignatureType type) { 041 return type == SignatureType.SUBKEY_BINDING; 042 } 043 044 public SelfSignatureSubpackets getHashedSubpackets() { 045 return hashedSubpackets; 046 } 047 048 public SelfSignatureSubpackets getUnhashedSubpackets() { 049 return unhashedSubpackets; 050 } 051 052 public void applyCallback(@Nullable SelfSignatureSubpackets.Callback callback) { 053 if (callback != null) { 054 callback.modifyHashedSubpackets(getHashedSubpackets()); 055 callback.modifyUnhashedSubpackets(getUnhashedSubpackets()); 056 } 057 } 058 059 public PGPSignature build(PGPPublicKey subkey) throws PGPException { 060 return buildAndInitSignatureGenerator() 061 .generateCertification(publicSigningKey, subkey); 062 } 063}