java.lang.Object
com.ssgllc.fish.service.util.published.PgpUtil

@Component public class PgpUtil extends Object
OpenPGP encryption, decryption, signing and verification of file contents from a Groovy script.

Called as the pgpUtil bean. It works on bytes, so it composes with bpmUtil.getFile(...) and bpmUtil.createFile(...).

Private keys and passphrases are never passed in directly. Methods take the name of an application.secrets entry, so key material stays out of process variables, configuration and logs. An environment variable maps to a secret name by dropping the APPLICATION_SECRETS_ prefix and lowercasing, with each underscore becoming a dot: APPLICATION_SECRETS_MIIS_PGP_KEY is the secret miis.pgp.key. Public keys are passed as ASCII-armored key text, for example read from an AppConfig.

Output uses AES-256 with an integrity packet, ZIP compression and SHA-256 signatures. Decryption accepts ASCII-armored or binary input, and rejects data without integrity protection, ciphers other than AES, signatures hashed with anything weaker than SHA-256, and output larger than 128 MB.

  • Constructor Summary

    Constructors
    Constructor
    Description
    PgpUtil(com.ssgllc.fish.config.ApplicationProperties applicationProperties)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    decrypt(byte[] data, String privateKeySecret, String passphraseSecret)
    Decrypts data without checking who signed it.
    byte[]
    decryptAndVerify(byte[] data, String privateKeySecret, String passphraseSecret, List<String> senderPublicKeys)
    Decrypts data and verifies that one of the expected senders signed it.
    byte[]
    encrypt(byte[] data, List<String> recipientPublicKeys, boolean armor)
    Encrypts data to one or more recipients without signing it.
    byte[]
    encryptAndSign(byte[] data, List<String> recipientPublicKeys, String signingKeySecret, String passphraseSecret, boolean armor)
    Signs data with a private key, then encrypts it to one or more recipients.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PgpUtil

      @Autowired public PgpUtil(com.ssgllc.fish.config.ApplicationProperties applicationProperties)
  • Method Details

    • encrypt

      public byte[] encrypt(byte[] data, List<String> recipientPublicKeys, boolean armor)
      Encrypts data to one or more recipients without signing it. The recipient cannot tell who sent it; prefer encryptAndSign(byte[], java.util.List<java.lang.String>, java.lang.String, java.lang.String, boolean) unless the recipient does not verify signatures.
      Groovy example:
      def plain = bpmUtil.getFile(bpmUtil.getResource("outbound/report.csv"))
      def encrypted = pgpUtil.encrypt(plain, [bpmUtil.getAppConfig("partnerPgpPublicKey")], true)
      bpmUtil.createFile("outbound/report.csv.asc", encrypted)
      Returns: the ASCII-armored encrypted file contents
      Parameters:
      data - the bytes to encrypt
      recipientPublicKeys - ASCII-armored public keys; each recipient can decrypt the result
      armor - true for ASCII-armored output, false for binary
      Returns:
      the encrypted bytes
    • encryptAndSign

      public byte[] encryptAndSign(byte[] data, List<String> recipientPublicKeys, String signingKeySecret, String passphraseSecret, boolean armor)
      Signs data with a private key, then encrypts it to one or more recipients.
      Groovy example:
      def plain = bpmUtil.getFile(bpmUtil.getResource("outbound/users.csv"))
      def encrypted = pgpUtil.encryptAndSign(plain, [bpmUtil.getAppConfig("partnerPgpPublicKey")],
          "pgp.private.key", "pgp.passphrase", false)
      bpmUtil.createFile("outbound/users.csv.gpg", encrypted)
      Returns: the binary signed and encrypted file contents
      Parameters:
      data - the bytes to sign and encrypt
      recipientPublicKeys - ASCII-armored public keys; each recipient can decrypt the result
      signingKeySecret - name of the application.secrets entry holding the ASCII-armored private key
      passphraseSecret - name of the application.secrets entry holding the key's passphrase, or null when the key has no passphrase
      armor - true for ASCII-armored output, false for binary
      Returns:
      the signed and encrypted bytes
    • decryptAndVerify

      public byte[] decryptAndVerify(byte[] data, String privateKeySecret, String passphraseSecret, List<String> senderPublicKeys)
      Decrypts data and verifies that one of the expected senders signed it. Throws CasetivityPgpException when the data is unsigned, signed by any other key, has a bad signature or fails its integrity check, so a returned value is always authentic.
      Groovy example:
      def encrypted = bpmUtil.getFile(bpmUtil.getResource("inbound/report.csv.gpg"))
      def plain = pgpUtil.decryptAndVerify(encrypted, "pgp.private.key", "pgp.passphrase",
          [bpmUtil.getAppConfig("partnerPgpPublicKey")])
      Returns: the decrypted file contents
      Parameters:
      data - the encrypted bytes, ASCII-armored or binary
      privateKeySecret - name of the application.secrets entry holding the ASCII-armored private key
      passphraseSecret - name of the application.secrets entry holding the key's passphrase, or null when the key has no passphrase
      senderPublicKeys - ASCII-armored public keys of the senders allowed to have signed the data
      Returns:
      the decrypted bytes
    • decrypt

      public byte[] decrypt(byte[] data, String privateKeySecret, String passphraseSecret)
      Decrypts data without checking who signed it. A signature in the data is skipped, not verified. This proves nothing about who sent the data; use decryptAndVerify(byte[], java.lang.String, java.lang.String, java.util.List<java.lang.String>) whenever the sender signs.
      Groovy example:
      def encrypted = bpmUtil.getFile(bpmUtil.getResource("inbound/report.csv.gpg"))
      def plain = pgpUtil.decrypt(encrypted, "pgp.private.key", "pgp.passphrase")
      Returns: the decrypted file contents
      Parameters:
      data - the encrypted bytes, ASCII-armored or binary
      privateKeySecret - name of the application.secrets entry holding the ASCII-armored private key
      passphraseSecret - name of the application.secrets entry holding the key's passphrase, or null when the key has no passphrase
      Returns:
      the decrypted bytes