Class PGPSignatureGenerator

java.lang.Object
org.bouncycastle.openpgp.PGPSignatureGenerator

public class PGPSignatureGenerator extends Object
Generator for PGP Signatures.
  • Field Details

    • lastb

      protected byte lastb
    • sigOut

      protected OutputStream sigOut
    • sigType

      protected int sigType
    • version

      protected final int version
    • legacyTextSigOut

      protected OutputStream legacyTextSigOut
      Verification only: a second stream fed the pre-1.87 canonical text form, in which a bare CR was hashed as CR LF, or null if no such fallback is wanted. See update(byte).
    • sawBareCR

      protected boolean sawBareCR
      Set once the text-mode input has contained a CR not followed by LF, the only input on which the current and the legacy canonical text forms differ.
  • Constructor Details

    • PGPSignatureGenerator

      public PGPSignatureGenerator(PGPContentSignerBuilder contentSignerBuilder)
      Create a version 4 signature generator built on the passed in contentSignerBuilder.
      Parameters:
      contentSignerBuilder - builder to produce PGPContentSigner objects for generating signatures.
    • PGPSignatureGenerator

      public PGPSignatureGenerator(PGPContentSignerBuilder contentSignerBuilder, PGPPublicKey signingKey)
      Create a signature generator built on the passed in contentSignerBuilder. The produces signature version will match the version of the passed in signing key.
      Parameters:
      contentSignerBuilder - builder to produce PGPContentSigner objects for generating signatures
      signingKey - signing key
    • PGPSignatureGenerator

      public PGPSignatureGenerator(PGPContentSignerBuilder contentSignerBuilder, PGPPublicKey signingKey, int signatureVersion)
  • Method Details

    • init

      public void init(int signatureType, PGPPrivateKey key) throws PGPException
      Initialize the generator for signing.
      Parameters:
      signatureType - type of signature
      key - private signing key
      Throws:
      PGPException
    • setHashedSubpackets

      public void setHashedSubpackets(PGPSignatureSubpacketVector hashedPcks)
      Set the hashed signature subpackets. Hashed signature subpackets are covered by the signature.
      Parameters:
      hashedPcks - hashed signature subpackets
    • setUnhashedSubpackets

      public void setUnhashedSubpackets(PGPSignatureSubpacketVector unhashedPcks)
      Set the unhashed signature subpackets. Unhashed signature subpackets are not covered by the signature.
      Parameters:
      unhashedPcks - unhashed signature subpackets
    • generateOnePassVersion

      public PGPOnePassSignature generateOnePassVersion(boolean isNested) throws PGPException
      Return the one pass header associated with the current signature.
      Parameters:
      isNested - true if the signature is nested, false otherwise.
      Returns:
      PGPOnePassSignature
      Throws:
      PGPException
    • generate

      public PGPSignature generate() throws PGPException
      Return a signature object containing the current signature state.
      Returns:
      PGPSignature
      Throws:
      PGPException
    • prepareSignatureSubpackets

      protected void prepareSignatureSubpackets() throws PGPException
      Throws:
      PGPException
    • generateCertification

      public PGPSignature generateCertification(String id, PGPPublicKey pubKey) throws PGPException
      Generate a certification for the passed in id and key.
      Parameters:
      id - the id we are certifying against the public key.
      pubKey - the key we are certifying against the id.
      Returns:
      the certification.
      Throws:
      PGPException
    • generateCertification

      public PGPSignature generateCertification(PGPUserAttributeSubpacketVector userAttributes, PGPPublicKey pubKey) throws PGPException
      Generate a certification for the passed in userAttributes
      Parameters:
      userAttributes - the id we are certifying against the public key.
      pubKey - the key we are certifying against the id.
      Returns:
      the certification.
      Throws:
      PGPException
    • generateCertification

      public PGPSignature generateCertification(PGPPublicKey masterKey, PGPPublicKey pubKey) throws PGPException
      Generate a certification for the passed in key against the passed in master key.
      Parameters:
      masterKey - the key we are certifying against.
      pubKey - the key we are certifying.
      Returns:
      the certification.
      Throws:
      PGPException
    • generateCertification

      public PGPSignature generateCertification(PGPPublicKey pubKey) throws PGPException
      Generate a certification, such as a revocation, for the passed in key.
      Parameters:
      pubKey - the key we are certifying.
      Returns:
      the certification.
      Throws:
      PGPException
    • resetTextState

      protected void resetTextState()
      Reset the text canonicalisation state for a new signature calculation.
    • update

      public void update(byte b)
      Update the signature with a byte of the signed data.

      For a text signature (type 0x01) the data is hashed in canonical text form, as RFC 9580 sec. 5.2.1.2 describes: a LF not preceded by a CR is hashed as CR LF, and every other byte, a CR not followed by a LF included, is hashed unaltered. A bare CR is therefore not a line ending, which is what GnuPG, GopenPGP, OpenPGP.js, rpgpie, RNP and PGPy do (github #2553). Releases before 1.87 hashed a bare CR as CR LF; when verifying, that form is also accepted unless Properties.OPENPGP_TEXT_SIG_ALLOW_LEGACY_CR is set to false.

    • isLegacyTextFormDifferent

      protected boolean isLegacyTextFormDifferent()
      Return true if the text-mode data hashed so far is one the legacy canonical text form hashes differently - that is, it holds a bare CR, a trailing CR included.
    • isLegacyTextFormAllowed

      protected static boolean isLegacyTextFormAllowed()
      Return true if a text signature should also be checked against the pre-1.87 canonical text form (see update(byte)).
    • verifyLegacyTextForm

      protected boolean verifyLegacyTextForm(PGPContentVerifier legacyVerifier, byte[] trailer, byte[] signature) throws PGPException
      Second chance for a text signature that did not verify over the current canonical form: check it over the legacy form, if a legacy hash was kept and the data is one on which the two forms differ (on any other data they are the same bytes, so the answer cannot change).
      Throws:
      PGPException
    • update

      public void update(byte[] b)
    • update

      public void update(byte[] b, int off, int len)
    • blockUpdate

      protected void blockUpdate(byte[] block, int off, int len)
    • updateWithIdData

      protected void updateWithIdData(int header, byte[] idBytes)
    • updateWithPublicKey

      protected void updateWithPublicKey(PGPPublicKey key) throws PGPException
      Throws:
      PGPException
    • getAttributesHash

      protected void getAttributesHash(PGPUserAttributeSubpacketVector userAttributes) throws PGPException
      Throws:
      PGPException