This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27449-pgp-ocb in repository https://gitbox.apache.org/repos/asf/camel.git
commit 7750f15561445b31d024ad6a09c79fd8f578e886 Author: Claus Ibsen <[email protected]> AuthorDate: Mon Nov 23 12:18:10 2026 +0100 CAMEL-27449: camel-crypto-pgp - decrypt OCB (AEAD) messages of current GnuPG, which are integrity protected GnuPG 2.4 and later encrypts with the OCB encrypted data packet (packet tag 20) for every key it makes itself, since such keys announce AEAD support. unmarshal() only accepted a symmetrically encrypted integrity protected data packet as integrity protected (isIntegrityProtected()), so with requireIntegrityProtection on, the default since CAMEL-24441, it refused these messages with "PGP message is not integrity protected". OCB is authenticated encryption: BouncyCastle checks the tag of every chunk and the final tag while decrypting and fails on a mismatch, so the message is integrity protected. unmarshal() now accepts a message for which isAEAD() is true. PGPEncryptedData.verify() does not apply to the OCB packet (it throws for anything but the integrity protected packet), so the decrypted stream is read to its end instead, which makes sure the final tag is checked. Version 2 of the integrity protected packet (AEAD of RFC 9580) was already covered, as isIntegrityProtected() is true for it and verify() reads it to the end. A message without integrity protection is still refused, as tested by PGPRequireIntegrityProtectionTest. The new test decrypts a signed OCB message and a message in several OCB chunks made by GnuPG 2.5, with a throwaway key pair, and checks that an altered chunk or final tag is rejected even with requireIntegrityProtection off. The data format page said it implements only symmetric (shared-key) encryption, which is wrong for PGP; it now explains the public-key encryption and which messages count as integrity protected. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../apache/camel/catalog/docs/pgp-dataformat.adoc | 16 +- .../src/main/docs/pgp-dataformat.adoc | 16 +- .../converter/crypto/PGPKeyAccessDataFormat.java | 18 ++- .../crypto/PGPOcbEncryptedMessageTest.java | 174 +++++++++++++++++++++ .../camel/component/crypto/ocb-chunked-message.gpg | Bin 0 -> 691 bytes .../apache/camel/component/crypto/ocb-pubring.gpg | Bin 0 -> 477 bytes .../apache/camel/component/crypto/ocb-secring.gpg | Bin 0 -> 643 bytes .../camel/component/crypto/ocb-signed-message.gpg | Bin 0 -> 360 bytes 8 files changed, 216 insertions(+), 8 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/pgp-dataformat.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/pgp-dataformat.adoc index d3b906079a42..dc61a3e09aee 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/pgp-dataformat.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/pgp-dataformat.adoc @@ -14,8 +14,20 @@ Cryptographic Extension into Camel, allowing simple and flexible encryption and decryption of messages using Camel's familiar marshal and unmarshal formatting mechanism. It assumes marshalling to mean encryption to ciphertext and unmarshalling to mean decryption back to -the original plaintext. This data format implements only symmetric -(shared-key) encryption and decryption. +the original plaintext. + +PGP uses public-key encryption: the message is encrypted with a random +session key and a symmetric cipher, and the session key is encrypted with +the public key of each recipient, so only the holder of a matching private +key can decrypt the message. Optionally the message is signed with the +private key of the sender and the signature is verified with the public key +of the sender. + +When decrypting, the message must be integrity protected by default +(`requireIntegrityProtection`). Both a symmetrically encrypted integrity +protected data packet and an AEAD encrypted packet, such as the OCB +encrypted data packet that GnuPG 2.4 and later writes, are integrity +protected. == PGPDataFormat Options diff --git a/components/camel-crypto-pgp/src/main/docs/pgp-dataformat.adoc b/components/camel-crypto-pgp/src/main/docs/pgp-dataformat.adoc index d3b906079a42..dc61a3e09aee 100644 --- a/components/camel-crypto-pgp/src/main/docs/pgp-dataformat.adoc +++ b/components/camel-crypto-pgp/src/main/docs/pgp-dataformat.adoc @@ -14,8 +14,20 @@ Cryptographic Extension into Camel, allowing simple and flexible encryption and decryption of messages using Camel's familiar marshal and unmarshal formatting mechanism. It assumes marshalling to mean encryption to ciphertext and unmarshalling to mean decryption back to -the original plaintext. This data format implements only symmetric -(shared-key) encryption and decryption. +the original plaintext. + +PGP uses public-key encryption: the message is encrypted with a random +session key and a symmetric cipher, and the session key is encrypted with +the public key of each recipient, so only the holder of a matching private +key can decrypt the message. Optionally the message is signed with the +private key of the sender and the signature is verified with the public key +of the sender. + +When decrypting, the message must be integrity protected by default +(`requireIntegrityProtection`). Both a symmetrically encrypted integrity +protected data packet and an AEAD encrypted packet, such as the OCB +encrypted data packet that GnuPG 2.4 and later writes, are integrity +protected. == PGPDataFormat Options diff --git a/components/camel-crypto-pgp/src/main/java/org/apache/camel/converter/crypto/PGPKeyAccessDataFormat.java b/components/camel-crypto-pgp/src/main/java/org/apache/camel/converter/crypto/PGPKeyAccessDataFormat.java index 3f51111a400c..82b25727f756 100644 --- a/components/camel-crypto-pgp/src/main/java/org/apache/camel/converter/crypto/PGPKeyAccessDataFormat.java +++ b/components/camel-crypto-pgp/src/main/java/org/apache/camel/converter/crypto/PGPKeyAccessDataFormat.java @@ -417,9 +417,17 @@ public class PGPKeyAccessDataFormat extends ServiceSupport implements DataFormat verifySignature(pgpFactory, signature); PGPPublicKeyEncryptedData pbe = encDataAndPbe.getPbe(); if (pbe.isIntegrityProtected()) { + // symmetrically encrypted integrity protected data packet: version 1 carries a modification + // detection code, version 2 is AEAD; verify() reads the rest of the data and checks either if (!pbe.verify()) { throw new PGPException("Message failed integrity check"); } + } else if (pbe.isAEAD()) { + // OCB encrypted data packet (tag 20), written by GnuPG 2.4 and later. It is authenticated + // encryption: the decrypting stream checks the tag of every chunk and the final tag, and fails + // on a mismatch. verify() does not apply to this packet, so read the stream to its end, which + // makes sure the final tag is checked even if the literal data did not consume all of it. + encData.transferTo(OutputStream.nullOutputStream()); } else if (requireIntegrityProtection) { // Without a modification detection code the ciphertext is malleable, and whether the check runs // would otherwise be decided by the packet type the sender chose - so a sender or an intermediary @@ -708,10 +716,12 @@ public class PGPKeyAccessDataFormat extends ServiceSupport implements DataFormat * Whether a message must be integrity protected in order to be decrypted. * <p> * An OpenPGP symmetrically encrypted integrity protected data packet carries a modification detection code, which - * {@code unmarshal} verifies. The older symmetrically encrypted data packet carries none, and OpenPGP's CFB mode is - * malleable without it, so accepting such a message means accepting ciphertext that may have been altered. Since - * the packet type is chosen by whoever produced the message, leaving this off lets the sender - or anyone who can - * rewrite the message in transit - decide whether the check applies at all. + * {@code unmarshal} verifies. AEAD encrypted messages (version 2 of that packet, and the OCB encrypted data packet + * that GnuPG 2.4 and later writes) are authenticated by their encryption mode, so they are integrity protected as + * well. The older symmetrically encrypted data packet has no such protection, and OpenPGP's CFB mode is malleable + * without it, so accepting such a message means accepting ciphertext that may have been altered. Since the packet + * type is chosen by whoever produced the message, leaving this off lets the sender - or anyone who can rewrite the + * message in transit - decide whether the check applies at all. * <p> * Defaults to true. Set it to false only to interoperate with a sender that still emits the legacy packet. */ diff --git a/components/camel-crypto-pgp/src/test/java/org/apache/camel/converter/crypto/PGPOcbEncryptedMessageTest.java b/components/camel-crypto-pgp/src/test/java/org/apache/camel/converter/crypto/PGPOcbEncryptedMessageTest.java new file mode 100644 index 000000000000..0102a18c6f5f --- /dev/null +++ b/components/camel-crypto-pgp/src/test/java/org/apache/camel/converter/crypto/PGPOcbEncryptedMessageTest.java @@ -0,0 +1,174 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.converter.crypto; + +import java.io.IOException; +import java.io.InputStream; +import java.security.Security; + +import org.apache.camel.Exchange; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.support.DefaultExchange; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.openpgp.PGPException; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * GnuPG 2.4 and later encrypts with the OCB encrypted data packet (packet tag 20) for keys that announce AEAD support, + * which is every key it makes itself. OCB is authenticated encryption, so such a message is integrity protected and + * must decrypt with {@code requireIntegrityProtection} on. That a message without integrity protection is still refused + * is tested in {@link PGPRequireIntegrityProtectionTest}. + * <p> + * The key pair and the messages were made with GnuPG 2.5.24, in a throwaway GnuPG home directory: + * + * <pre> + * export GNUPGHOME=$(mktemp -d) + * gpg --batch --pinentry-mode loopback --passphrase camel-ocb-test \ + * --quick-gen-key 'Camel OCB Test <[email protected]>' default default never + * gpg --batch --export [email protected] > ocb-pubring.gpg + * gpg --batch --pinentry-mode loopback --passphrase camel-ocb-test \ + * --export-secret-keys [email protected] > ocb-secring.gpg + * + * printf 'Hello OCB' > plain.txt + * gpg --batch --pinentry-mode loopback --passphrase camel-ocb-test --trust-model always \ + * --recipient [email protected] --local-user [email protected] \ + * --sign --encrypt --output ocb-signed-message.gpg plain.txt + * + * perl -e 'print "Hello OCB in chunks. " x 20' > plain-chunks.txt + * gpg --batch --trust-model always --chunk-size 6 --compress-algo none \ + * --recipient [email protected] --encrypt --output ocb-chunked-message.gpg plain-chunks.txt + * </pre> + * + * {@code gpg --list-packets} shows the OCB packet in both messages, the second one in chunks of 64 bytes: + * + * <pre> + * # off=0 ctb=84 tag=1 hlen=2 plen=94 + * :pubkey enc packet: version 3, algo 18, keyid 1FB0A39556A87E1D + * # off=96 ctb=d4 tag=20 hlen=3 plen=261 new-ctb + * :ocb encrypted packet: cipher=9 aead=2 cb=16 + * # off=118 ctb=a3 tag=8 hlen=1 plen=0 indeterminate + * :compressed packet: algo=2 + * # off=120 ctb=90 tag=4 hlen=2 plen=13 + * :onepass_sig packet: keyid 670531E84BC7F033 + * # off=135 ctb=ac tag=11 hlen=2 plen=24 + * :literal data packet: + * # off=161 ctb=88 tag=2 hlen=2 plen=167 + * :signature packet: algo 22, keyid 670531E84BC7F033 + * + * # off=0 ctb=84 tag=1 hlen=2 plen=94 + * :pubkey enc packet: version 3, algo 18, keyid 1FB0A39556A87E1D + * # off=96 ctb=d4 tag=20 hlen=2 plen=0 partial new-ctb + * :ocb encrypted packet: cipher=9 aead=2 cb=0 + * # off=117 ctb=ad tag=11 hlen=3 plen=442 + * :literal data packet: + * </pre> + */ +class PGPOcbEncryptedMessageTest { + + private static final String PUB_KEY_RING = "org/apache/camel/component/crypto/ocb-pubring.gpg"; + private static final String SEC_KEY_RING = "org/apache/camel/component/crypto/ocb-secring.gpg"; + private static final String SIGNED_MESSAGE = "org/apache/camel/component/crypto/ocb-signed-message.gpg"; + private static final String CHUNKED_MESSAGE = "org/apache/camel/component/crypto/ocb-chunked-message.gpg"; + private static final String PASSPHRASE = "camel-ocb-test"; + + @BeforeAll + static void installProvider() { + Security.addProvider(new BouncyCastleProvider()); + } + + @Test + void aSignedOcbMessageOfGnuPGDecryptsWithIntegrityProtectionRequired() throws Exception { + PGPDataFormat decryptor = decryptor(); + assertTrue(decryptor.isRequireIntegrityProtection(), "expected the secure default"); + decryptor.setSignatureKeyFileName(PUB_KEY_RING); + decryptor.setSignatureVerificationOption(PGPKeyAccessDataFormat.SIGNATURE_VERIFICATION_OPTION_REQUIRED); + + assertEquals("Hello OCB", decrypt(decryptor, read(SIGNED_MESSAGE))); + } + + @Test + void anOcbMessageInSeveralChunksDecryptsWithIntegrityProtectionRequired() throws Exception { + assertEquals("Hello OCB in chunks. ".repeat(20), decrypt(decryptor(), read(CHUNKED_MESSAGE))); + } + + // the tampered messages are decrypted with requireIntegrityProtection off, so it is the authentication of OCB + // that rejects them, not the check of the packet type + + @Test + void anOcbMessageWithAnAlteredChunkIsRejected() throws Exception { + // the ciphertext begins after the 3 byte packet header and the 19 bytes of version, cipher, AEAD algorithm, + // chunk size and IV of the OCB packet at offset 96 + byte[] message = read(SIGNED_MESSAGE); + message[96 + 3 + 19 + 50] ^= 0x01; + + assertThrows(PGPException.class, () -> decrypt(lenientDecryptor(), message)); + } + + @Test + void anOcbMessageWithAnAlteredFinalTagIsRejected() throws Exception { + byte[] message = read(SIGNED_MESSAGE); + message[message.length - 1] ^= 0x01; + + assertThrows(PGPException.class, () -> decrypt(lenientDecryptor(), message)); + } + + @Test + void anOcbMessageInSeveralChunksWithAnAlteredFinalTagIsRejected() throws Exception { + byte[] message = read(CHUNKED_MESSAGE); + message[message.length - 1] ^= 0x01; + + // found while reading the literal data, when the last chunk is decrypted + assertThrows(IOException.class, () -> decrypt(lenientDecryptor(), message)); + } + + private static PGPDataFormat decryptor() { + PGPDataFormat decryptor = new PGPDataFormat(); + decryptor.setKeyFileName(SEC_KEY_RING); + decryptor.setPassword(PASSPHRASE); + return decryptor; + } + + private static PGPDataFormat lenientDecryptor() { + PGPDataFormat decryptor = decryptor(); + decryptor.setRequireIntegrityProtection(false); + return decryptor; + } + + private static byte[] read(String resource) throws IOException { + try (InputStream in = PGPOcbEncryptedMessageTest.class.getClassLoader().getResourceAsStream(resource)) { + assertNotNull(in, "missing test resource " + resource); + return in.readAllBytes(); + } + } + + private static String decrypt(PGPDataFormat decryptor, byte[] message) throws Exception { + try (DefaultCamelContext context = new DefaultCamelContext()) { + context.start(); + decryptor.start(); + + Exchange exchange = new DefaultExchange(context); + Object body = decryptor.unmarshal(exchange, message); + return context.getTypeConverter().convertTo(String.class, exchange, body); + } + } +} diff --git a/components/camel-crypto-pgp/src/test/resources/org/apache/camel/component/crypto/ocb-chunked-message.gpg b/components/camel-crypto-pgp/src/test/resources/org/apache/camel/component/crypto/ocb-chunked-message.gpg new file mode 100644 index 000000000000..6fbfdeba4c1a Binary files /dev/null and b/components/camel-crypto-pgp/src/test/resources/org/apache/camel/component/crypto/ocb-chunked-message.gpg differ diff --git a/components/camel-crypto-pgp/src/test/resources/org/apache/camel/component/crypto/ocb-pubring.gpg b/components/camel-crypto-pgp/src/test/resources/org/apache/camel/component/crypto/ocb-pubring.gpg new file mode 100644 index 000000000000..d7faae104d21 Binary files /dev/null and b/components/camel-crypto-pgp/src/test/resources/org/apache/camel/component/crypto/ocb-pubring.gpg differ diff --git a/components/camel-crypto-pgp/src/test/resources/org/apache/camel/component/crypto/ocb-secring.gpg b/components/camel-crypto-pgp/src/test/resources/org/apache/camel/component/crypto/ocb-secring.gpg new file mode 100644 index 000000000000..d737be9575b5 Binary files /dev/null and b/components/camel-crypto-pgp/src/test/resources/org/apache/camel/component/crypto/ocb-secring.gpg differ diff --git a/components/camel-crypto-pgp/src/test/resources/org/apache/camel/component/crypto/ocb-signed-message.gpg b/components/camel-crypto-pgp/src/test/resources/org/apache/camel/component/crypto/ocb-signed-message.gpg new file mode 100644 index 000000000000..1ceac191220d Binary files /dev/null and b/components/camel-crypto-pgp/src/test/resources/org/apache/camel/component/crypto/ocb-signed-message.gpg differ
