This is an automated email from the ASF dual-hosted git repository.

chibenwa pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-mime4j.git

commit 527a881ec43d2ae5e445a5828120c565c2890386
Author: Benoit TELLIER <[email protected]>
AuthorDate: Tue Nov 10 10:58:30 2026 +0100

    [FIX] CipherStorageProvider: encrypt with segmented AES-256/GCM
    
    The provider defaulted to "Blowfish" and built its ciphers from the bare
    algorithm name, which SunJCE reads as <alg>/ECB/PKCS5Padding: identical
    plaintext blocks gave identical ciphertext blocks, no IV was used, and
    nothing authenticated the ciphertext. Whoever could read the spool learnt
    the structure of each attachment, which the provider exists to hide, and
    whoever could write it changed the bytes the parser read back undetected.
    
    Each storage now gets a random AES-256 key and a random nonce prefix, kept
    in memory only. The data is cut into 64 KB segments, each encrypted and
    authenticated with AES/GCM under a nonce made of that prefix, the segment
    number and a last-segment flag: a modified, reordered or truncated content
    fails the read with an IOException before any altered byte is returned.
    Segments keep the memory of a read bounded, which a single GCM operation
    through CipherInputStream would not, as it buffers the whole part to check
    its tag. The two-argument constructor accepts only "AES" and
    "AES/GCM/NoPadding" and refuses other algorithms with an
    IllegalArgumentException, ECB and bare names such as "Blowfish" included.
---
 .../mime4j/storage/CipherStorageProvider.java      | 280 +++++++++++++++++----
 .../mime4j/storage/CipherStorageProviderTest.java  | 196 +++++++++++++++
 2 files changed, 430 insertions(+), 46 deletions(-)

diff --git 
a/storage/src/main/java/org/apache/james/mime4j/storage/CipherStorageProvider.java
 
b/storage/src/main/java/org/apache/james/mime4j/storage/CipherStorageProvider.java
index 24216c7b..148de064 100644
--- 
a/storage/src/main/java/org/apache/james/mime4j/storage/CipherStorageProvider.java
+++ 
b/storage/src/main/java/org/apache/james/mime4j/storage/CipherStorageProvider.java
@@ -23,18 +23,29 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.security.GeneralSecurityException;
 import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.util.Locale;
 
 import javax.crypto.Cipher;
-import javax.crypto.CipherInputStream;
-import javax.crypto.CipherOutputStream;
 import javax.crypto.KeyGenerator;
-import javax.crypto.spec.SecretKeySpec;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.GCMParameterSpec;
 
 /**
- * A {@link StorageProvider} that transparently scrambles and unscrambles the
+ * A {@link StorageProvider} that transparently encrypts and decrypts the
  * data stored by another <code>StorageProvider</code>.
  *
  * <p>
+ * Each storage gets its own random AES-256 key and nonce prefix, kept in 
memory
+ * only. The data is cut into segments of {@value #SEGMENT_SIZE} bytes, each
+ * encrypted and authenticated with AES/GCM under a distinct nonce that also
+ * tells whether it is the last segment: the back-end learns nothing but the
+ * length of the data, and a modified, reordered or truncated content makes the
+ * read fail with an {@link IOException} before any altered byte is returned.
+ * A read buffers one segment at a time, whatever the size of the data.
+ * </p>
+ *
+ * <p>
  * Example usage:
  *
  * <pre>
@@ -46,19 +57,32 @@ import javax.crypto.spec.SecretKeySpec;
  */
 public class CipherStorageProvider extends AbstractStorageProvider {
 
+    private static final String KEY_ALGORITHM = "AES";
+    private static final String TRANSFORMATION = "AES/GCM/NoPadding";
+    private static final int KEY_SIZE = 256;
+    private static final int TAG_BITS = 128;
+    private static final int TAG_LEN = TAG_BITS / 8;
+    private static final int NONCE_LEN = 12;
+    // nonce = random prefix | segment counter (4 bytes) | last segment flag 
(1 byte)
+    private static final int NONCE_PREFIX_LEN = NONCE_LEN - 5;
+
+    /** Plaintext bytes per segment. */
+    static final int SEGMENT_SIZE = 64 * 1024;
+
+    private static final SecureRandom RANDOM = new SecureRandom();
+
     private final StorageProvider backend;
-    private final String algorithm;
     private final KeyGenerator keygen;
 
     /**
-     * Creates a new <code>CipherStorageProvider</code> for the given back-end
-     * using the Blowfish cipher algorithm.
+     * Creates a new <code>CipherStorageProvider</code> for the given back-end,
+     * using AES-256 in GCM mode.
      *
      * @param backend
      *            back-end storage strategy to encrypt.
      */
     public CipherStorageProvider(StorageProvider backend) {
-        this(backend, "Blowfish");
+        this(backend, KEY_ALGORITHM);
     }
 
     /**
@@ -68,87 +92,144 @@ public class CipherStorageProvider extends 
AbstractStorageProvider {
      * @param backend
      *            back-end storage strategy to encrypt.
      * @param algorithm
-     *            the name of the symmetric block cipher algorithm such as
-     *            "Blowfish", "AES" or "RC2".
+     *            <code>"AES"</code> or <code>"AES/GCM/NoPadding"</code>, which
+     *            both mean AES-256 in GCM mode. Other algorithms are refused: 
a
+     *            bare name such as <code>"Blowfish"</code> means ECB mode, 
which
+     *            leaks the patterns of the data and authenticates nothing.
+     * @throws IllegalArgumentException
+     *            if the algorithm is not supported.
      */
     public CipherStorageProvider(StorageProvider backend, String algorithm) {
         if (backend == null)
             throw new IllegalArgumentException();
+        if (algorithm == null) {
+            throw new IllegalArgumentException("algorithm may not be null");
+        }
+        String normalized = algorithm.trim().toUpperCase(Locale.ROOT);
+        if (!normalized.equals(KEY_ALGORITHM) && 
!normalized.equals(TRANSFORMATION.toUpperCase(Locale.ROOT))) {
+            throw new IllegalArgumentException("Unsupported algorithm " + 
algorithm
+                    + ": only " + TRANSFORMATION + " is supported");
+        }
 
         try {
             this.backend = backend;
-            this.algorithm = algorithm;
-            this.keygen = KeyGenerator.getInstance(algorithm);
+            this.keygen = KeyGenerator.getInstance(KEY_ALGORITHM);
+            this.keygen.init(KEY_SIZE, RANDOM);
         } catch (NoSuchAlgorithmException e) {
             throw new IllegalArgumentException(e);
         }
     }
 
     public StorageOutputStream createStorageOutputStream() throws IOException {
-        SecretKeySpec skeySpec = getSecretKeySpec();
+        SecretKey key;
+        synchronized (keygen) {
+            key = keygen.generateKey();
+        }
+        byte[] noncePrefix = new byte[NONCE_PREFIX_LEN];
+        RANDOM.nextBytes(noncePrefix);
+
+        return new 
CipherStorageOutputStream(backend.createStorageOutputStream(), key, 
noncePrefix);
+    }
 
-        return new CipherStorageOutputStream(backend
-                .createStorageOutputStream(), algorithm, skeySpec);
+    private static GCMParameterSpec segmentSpec(byte[] noncePrefix, int 
segment, boolean last) {
+        byte[] nonce = new byte[NONCE_LEN];
+        System.arraycopy(noncePrefix, 0, nonce, 0, NONCE_PREFIX_LEN);
+        nonce[NONCE_PREFIX_LEN] = (byte) (segment >>> 24);
+        nonce[NONCE_PREFIX_LEN + 1] = (byte) (segment >>> 16);
+        nonce[NONCE_PREFIX_LEN + 2] = (byte) (segment >>> 8);
+        nonce[NONCE_PREFIX_LEN + 3] = (byte) segment;
+        nonce[NONCE_LEN - 1] = (byte) (last ? 1 : 0);
+        return new GCMParameterSpec(TAG_BITS, nonce);
     }
 
-    private SecretKeySpec getSecretKeySpec() {
-        byte[] raw = keygen.generateKey().getEncoded();
-        return new SecretKeySpec(raw, algorithm);
+    private static IOException ioException(String message, 
GeneralSecurityException cause) {
+        return (IOException) new IOException(message).initCause(cause);
     }
 
     private static final class CipherStorageOutputStream extends
             StorageOutputStream {
         private final StorageOutputStream storageOut;
-        private final String algorithm;
-        private final SecretKeySpec skeySpec;
-        private final CipherOutputStream cipherOut;
+        private final SecretKey key;
+        private final byte[] noncePrefix;
+        private final Cipher cipher;
+        private final byte[] segment = new byte[SEGMENT_SIZE];
+        private int segmentLen;
+        private int segmentCount;
+        private boolean closed;
 
         public CipherStorageOutputStream(StorageOutputStream out,
-                String algorithm, SecretKeySpec skeySpec) throws IOException {
+                SecretKey key, byte[] noncePrefix) throws IOException {
+            this.storageOut = out;
+            this.key = key;
+            this.noncePrefix = noncePrefix;
             try {
-                this.storageOut = out;
-                this.algorithm = algorithm;
-                this.skeySpec = skeySpec;
-
-                Cipher cipher = Cipher.getInstance(algorithm);
-                cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
+                this.cipher = Cipher.getInstance(TRANSFORMATION);
+            } catch (GeneralSecurityException e) {
+                throw ioException("Cannot create the cipher", e);
+            }
+        }
 
-                this.cipherOut = new CipherOutputStream(out, cipher);
+        private void flushSegment(boolean last) throws IOException {
+            if (segmentCount == Integer.MAX_VALUE) {
+                throw new IOException("Too much data to encrypt");
+            }
+            try {
+                cipher.init(Cipher.ENCRYPT_MODE, key, segmentSpec(noncePrefix, 
segmentCount, last));
+                byte[] encrypted = cipher.doFinal(segment, 0, segmentLen);
+                storageOut.write(encrypted);
             } catch (GeneralSecurityException e) {
-                throw (IOException) new IOException().initCause(e);
+                throw ioException("Cannot encrypt", e);
             }
+            segmentCount++;
+            segmentLen = 0;
         }
 
         @Override
         public void close() throws IOException {
             super.close();
-            cipherOut.close();
+            if (!closed) {
+                closed = true;
+                try {
+                    flushSegment(true);
+                } finally {
+                    storageOut.close();
+                }
+            }
         }
 
         @Override
         protected void write0(byte[] buffer, int offset, int length)
                 throws IOException {
-            cipherOut.write(buffer, offset, length);
+            while (length > 0) {
+                if (segmentLen == SEGMENT_SIZE) {
+                    // More data follows: this segment is not the last one
+                    flushSegment(false);
+                }
+                int chunk = Math.min(length, SEGMENT_SIZE - segmentLen);
+                System.arraycopy(buffer, offset, segment, segmentLen, chunk);
+                segmentLen += chunk;
+                offset += chunk;
+                length -= chunk;
+            }
         }
 
         @Override
         protected Storage toStorage0() throws IOException {
-            // cipherOut has already been closed because toStorage calls close
+            // close() has already been called because toStorage calls close
             Storage encrypted = storageOut.toStorage();
-            return new CipherStorage(encrypted, algorithm, skeySpec);
+            return new CipherStorage(encrypted, key, noncePrefix);
         }
     }
 
     private static final class CipherStorage implements Storage {
         private Storage encrypted;
-        private final String algorithm;
-        private final SecretKeySpec skeySpec;
+        private final SecretKey key;
+        private final byte[] noncePrefix;
 
-        public CipherStorage(Storage encrypted, String algorithm,
-                SecretKeySpec skeySpec) {
+        public CipherStorage(Storage encrypted, SecretKey key, byte[] 
noncePrefix) {
             this.encrypted = encrypted;
-            this.algorithm = algorithm;
-            this.skeySpec = skeySpec;
+            this.key = key;
+            this.noncePrefix = noncePrefix;
         }
 
         public void delete() {
@@ -162,16 +243,123 @@ public class CipherStorageProvider extends 
AbstractStorageProvider {
             if (encrypted == null)
                 throw new IllegalStateException("storage has been deleted");
 
+            Cipher cipher;
             try {
-                Cipher cipher = Cipher.getInstance(algorithm);
-                cipher.init(Cipher.DECRYPT_MODE, skeySpec);
-
-                InputStream in = encrypted.getInputStream();
-                return new CipherInputStream(in, cipher);
+                cipher = Cipher.getInstance(TRANSFORMATION);
             } catch (GeneralSecurityException e) {
-                throw (IOException) new IOException().initCause(e);
+                throw ioException("Cannot create the cipher", e);
             }
+            return new 
SegmentDecryptingInputStream(encrypted.getInputStream(), cipher, key, 
noncePrefix);
         }
     }
 
+    /**
+     * Decrypts one segment at a time, and returns its bytes only once its tag 
is verified.
+     */
+    private static final class SegmentDecryptingInputStream extends 
InputStream {
+        private static final int ENCRYPTED_SEGMENT_SIZE = SEGMENT_SIZE + 
TAG_LEN;
+
+        private final InputStream in;
+        private final Cipher cipher;
+        private final SecretKey key;
+        private final byte[] noncePrefix;
+        // one encrypted segment, plus one byte read ahead to tell whether it 
is the last one
+        private final byte[] encryptedSegment = new 
byte[ENCRYPTED_SEGMENT_SIZE + 1];
+        private int readAhead = -1;
+        private byte[] plain = new byte[0];
+        private int plainPos;
+        private int segmentCount;
+        private boolean lastDone;
+
+        SegmentDecryptingInputStream(InputStream in, Cipher cipher, SecretKey 
key, byte[] noncePrefix) {
+            this.in = in;
+            this.cipher = cipher;
+            this.key = key;
+            this.noncePrefix = noncePrefix;
+        }
+
+        private int readFully(byte[] b, int off, int len) throws IOException {
+            int total = 0;
+            while (total < len) {
+                int n = in.read(b, off + total, len - total);
+                if (n == -1) {
+                    break;
+                }
+                total += n;
+            }
+            return total;
+        }
+
+        /**
+         * @return false at the end of the data, once the last segment has 
been verified
+         */
+        private boolean nextSegment() throws IOException {
+            if (lastDone) {
+                return false;
+            }
+            int len = 0;
+            if (readAhead != -1) {
+                encryptedSegment[0] = (byte) readAhead;
+                len = 1;
+                readAhead = -1;
+            }
+            len += readFully(encryptedSegment, len, ENCRYPTED_SEGMENT_SIZE - 
len);
+            boolean last;
+            if (len < ENCRYPTED_SEGMENT_SIZE) {
+                last = true;
+            } else {
+                readAhead = in.read();
+                last = readAhead == -1;
+            }
+            if (segmentCount == Integer.MAX_VALUE) {
+                throw new IOException("Too many encrypted segments");
+            }
+            try {
+                cipher.init(Cipher.DECRYPT_MODE, key, segmentSpec(noncePrefix, 
segmentCount, last));
+                plain = cipher.doFinal(encryptedSegment, 0, len);
+            } catch (GeneralSecurityException e) {
+                throw ioException("Encrypted storage has been altered or 
truncated", e);
+            }
+            plainPos = 0;
+            segmentCount++;
+            lastDone = last;
+            return true;
+        }
+
+        @Override
+        public int read() throws IOException {
+            byte[] one = new byte[1];
+            int n = read(one, 0, 1);
+            return n == -1 ? -1 : one[0] & 0xff;
+        }
+
+        @Override
+        public int read(byte[] b, int off, int len) throws IOException {
+            if (off < 0 || len < 0 || len > b.length - off) {
+                throw new IndexOutOfBoundsException();
+            }
+            if (len == 0) {
+                return 0;
+            }
+            while (plainPos == plain.length) {
+                if (!nextSegment()) {
+                    return -1;
+                }
+            }
+            int n = Math.min(len, plain.length - plainPos);
+            System.arraycopy(plain, plainPos, b, off, n);
+            plainPos += n;
+            return n;
+        }
+
+        @Override
+        public int available() {
+            return plain.length - plainPos;
+        }
+
+        @Override
+        public void close() throws IOException {
+            in.close();
+        }
+    }
 }
diff --git 
a/storage/src/test/java/org/apache/james/mime4j/storage/CipherStorageProviderTest.java
 
b/storage/src/test/java/org/apache/james/mime4j/storage/CipherStorageProviderTest.java
new file mode 100644
index 00000000..3e144daf
--- /dev/null
+++ 
b/storage/src/test/java/org/apache/james/mime4j/storage/CipherStorageProviderTest.java
@@ -0,0 +1,196 @@
+/****************************************************************
+ * 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.james.mime4j.storage;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CipherStorageProviderTest {
+
+    /**
+     * A back-end whose stored bytes the test can read and alter.
+     */
+    private static final class InspectableBackend extends 
AbstractStorageProvider {
+        byte[] stored;
+
+        public StorageOutputStream createStorageOutputStream() {
+            return new StorageOutputStream() {
+                private final ByteArrayOutputStream out = new 
ByteArrayOutputStream();
+
+                @Override
+                protected void write0(byte[] buffer, int offset, int length) {
+                    out.write(buffer, offset, length);
+                }
+
+                @Override
+                protected Storage toStorage0() {
+                    stored = out.toByteArray();
+                    return new Storage() {
+                        public InputStream getInputStream() {
+                            return new ByteArrayInputStream(stored);
+                        }
+
+                        public void delete() {
+                        }
+                    };
+                }
+            };
+        }
+    }
+
+    private static byte[] data(int size) {
+        byte[] data = new byte[size];
+        for (int i = 0; i < size; i++) {
+            data[i] = (byte) (i * 31 + i / 7);
+        }
+        return data;
+    }
+
+    private static byte[] read(Storage storage) throws IOException {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        try (InputStream in = storage.getInputStream()) {
+            byte[] buf = new byte[1000];
+            int n;
+            while ((n = in.read(buf)) != -1) {
+                out.write(buf, 0, n);
+            }
+        }
+        return out.toByteArray();
+    }
+
+    @Test
+    public void shouldReadBackWhatWasWritten() throws Exception {
+        int segment = CipherStorageProvider.SEGMENT_SIZE;
+        int[] sizes = {0, 1, 15, 16, 17, segment - 1, segment, segment + 1, 2 
* segment, 3 * segment + 5};
+        CipherStorageProvider provider = new CipherStorageProvider(new 
InspectableBackend());
+        for (int size : sizes) {
+            byte[] data = data(size);
+            Storage storage = provider.store(new ByteArrayInputStream(data));
+            Assert.assertArrayEquals("size " + size, data, read(storage));
+            // read byte per byte
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            try (InputStream in = storage.getInputStream()) {
+                int b;
+                while ((b = in.read()) != -1) {
+                    out.write(b);
+                }
+            }
+            Assert.assertArrayEquals("size " + size, data, out.toByteArray());
+        }
+    }
+
+    @Test
+    public void repeatedPlaintextBlocksShouldNotGiveRepeatedCiphertextBlocks() 
throws Exception {
+        InspectableBackend backend = new InspectableBackend();
+        byte[] data = new byte[16 * 10000];
+        Arrays.fill(data, (byte) 'A');
+        new CipherStorageProvider(backend).store(new 
ByteArrayInputStream(data));
+
+        Set<ByteBuffer> blocks = new HashSet<>();
+        for (int i = 0; i + 16 <= backend.stored.length; i += 16) {
+            Assert.assertTrue("ciphertext block " + i / 16 + " repeats",
+                    
blocks.add(ByteBuffer.wrap(Arrays.copyOfRange(backend.stored, i, i + 16))));
+        }
+    }
+
+    @Test
+    public void twoStoragesOfTheSameDataShouldNotShareCiphertext() throws 
Exception {
+        InspectableBackend backend = new InspectableBackend();
+        CipherStorageProvider provider = new CipherStorageProvider(backend);
+        byte[] data = data(100);
+        provider.store(new ByteArrayInputStream(data));
+        byte[] first = backend.stored;
+        provider.store(new ByteArrayInputStream(data));
+        Assert.assertFalse(Arrays.equals(first, backend.stored));
+    }
+
+    private static void assertReadFails(Storage storage) {
+        try {
+            read(storage);
+            Assert.fail("Expected IOException");
+        } catch (IOException expected) {
+            // expected
+        }
+    }
+
+    @Test
+    public void aFlippedCiphertextByteShouldMakeTheReadFail() throws Exception 
{
+        int segment = CipherStorageProvider.SEGMENT_SIZE;
+        int[] positions = {0, 100, segment + 10, 2 * segment + 20};
+        for (int position : positions) {
+            InspectableBackend backend = new InspectableBackend();
+            Storage storage = new CipherStorageProvider(backend).store(new 
ByteArrayInputStream(data(2 * segment + 100)));
+            backend.stored[position] ^= 1;
+            assertReadFails(storage);
+        }
+    }
+
+    @Test
+    public void aTruncatedCiphertextShouldMakeTheReadFail() throws Exception {
+        int segment = CipherStorageProvider.SEGMENT_SIZE;
+        int encryptedSegment = segment + 16;
+        // dropping the last segment, part of it, or the tag of a lone segment
+        int[][] cases = {{2 * segment, encryptedSegment}, {2 * segment + 100, 
encryptedSegment + 50}, {10, 20}};
+        for (int[] c : cases) {
+            InspectableBackend backend = new InspectableBackend();
+            Storage storage = new CipherStorageProvider(backend).store(new 
ByteArrayInputStream(data(c[0])));
+            backend.stored = Arrays.copyOf(backend.stored, c[1]);
+            assertReadFails(storage);
+        }
+    }
+
+    @Test
+    public void swappedSegmentsShouldMakeTheReadFail() throws Exception {
+        int encryptedSegment = CipherStorageProvider.SEGMENT_SIZE + 16;
+        InspectableBackend backend = new InspectableBackend();
+        Storage storage = new CipherStorageProvider(backend)
+                .store(new ByteArrayInputStream(data(3 * 
CipherStorageProvider.SEGMENT_SIZE)));
+        byte[] swapped = backend.stored.clone();
+        System.arraycopy(backend.stored, 0, swapped, encryptedSegment, 
encryptedSegment);
+        System.arraycopy(backend.stored, encryptedSegment, swapped, 0, 
encryptedSegment);
+        backend.stored = swapped;
+        assertReadFails(storage);
+    }
+
+    @Test
+    public void aesShouldBeAccepted() {
+        new CipherStorageProvider(new MemoryStorageProvider(), "AES");
+        new CipherStorageProvider(new MemoryStorageProvider(), 
"aes/gcm/nopadding");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void aBareBlowfishShouldBeRefused() {
+        new CipherStorageProvider(new MemoryStorageProvider(), "Blowfish");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void ecbShouldBeRefused() {
+        new CipherStorageProvider(new MemoryStorageProvider(), 
"AES/ECB/PKCS5Padding");
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to