Class PgpUtil
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 -
Method Summary
Modifier and TypeMethodDescriptionbyte[]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[]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.
-
Constructor Details
-
PgpUtil
@Autowired public PgpUtil(com.ssgllc.fish.config.ApplicationProperties applicationProperties)
-
-
Method Details
-
encrypt
Encrypts data to one or more recipients without signing it. The recipient cannot tell who sent it; preferencryptAndSign(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"))Returns: the ASCII-armored encrypted file contents
def encrypted = pgpUtil.encrypt(plain, [bpmUtil.getAppConfig("partnerPgpPublicKey")], true)
bpmUtil.createFile("outbound/report.csv.asc", encrypted)
- Parameters:
data- the bytes to encryptrecipientPublicKeys- ASCII-armored public keys; each recipient can decrypt the resultarmor-truefor ASCII-armored output,falsefor 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"))Returns: the binary signed and encrypted file contents
def encrypted = pgpUtil.encryptAndSign(plain, [bpmUtil.getAppConfig("partnerPgpPublicKey")],
"pgp.private.key", "pgp.passphrase", false)
bpmUtil.createFile("outbound/users.csv.gpg", encrypted)
- Parameters:
data- the bytes to sign and encryptrecipientPublicKeys- ASCII-armored public keys; each recipient can decrypt the resultsigningKeySecret- name of theapplication.secretsentry holding the ASCII-armored private keypassphraseSecret- name of theapplication.secretsentry holding the key's passphrase, ornullwhen the key has no passphrasearmor-truefor ASCII-armored output,falsefor 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. ThrowsCasetivityPgpExceptionwhen 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"))Returns: the decrypted file contents
def plain = pgpUtil.decryptAndVerify(encrypted, "pgp.private.key", "pgp.passphrase",
[bpmUtil.getAppConfig("partnerPgpPublicKey")])
- Parameters:
data- the encrypted bytes, ASCII-armored or binaryprivateKeySecret- name of theapplication.secretsentry holding the ASCII-armored private keypassphraseSecret- name of theapplication.secretsentry holding the key's passphrase, ornullwhen the key has no passphrasesenderPublicKeys- ASCII-armored public keys of the senders allowed to have signed the data- Returns:
- the decrypted bytes
-
decrypt
Decrypts data without checking who signed it. A signature in the data is skipped, not verified. This proves nothing about who sent the data; usedecryptAndVerify(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"))Returns: the decrypted file contents
def plain = pgpUtil.decrypt(encrypted, "pgp.private.key", "pgp.passphrase")
- Parameters:
data- the encrypted bytes, ASCII-armored or binaryprivateKeySecret- name of theapplication.secretsentry holding the ASCII-armored private keypassphraseSecret- name of theapplication.secretsentry holding the key's passphrase, ornullwhen the key has no passphrase- Returns:
- the decrypted bytes
-