You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2024/03/11 10:43:05 UTC

(santuario-xml-security-java) branch main updated: SANTUARIO-610: Fix NullPointerException in XMLCipher with BC AES GCM (#288)

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

coheigea pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/santuario-xml-security-java.git


The following commit(s) were added to refs/heads/main by this push:
     new 0a15380d SANTUARIO-610: Fix NullPointerException in XMLCipher with BC AES GCM (#288)
0a15380d is described below

commit 0a15380d6941ac2a8aa111b6ef05d12d5ca56614
Author: Benjamin Krahl <be...@bertelsmann.de>
AuthorDate: Mon Mar 11 11:43:00 2024 +0100

    SANTUARIO-610: Fix NullPointerException in XMLCipher with BC AES GCM (#288)
    
    * SANTUARIO-610: Fix NullPointerException in XMLCipher with BC AES GCM NoPadding
    
    * Update XMLCipherTest.java
    
    ---------
    
    Co-authored-by: Benjamin Krahl <kr...@users.noreply.github.com>
    Co-authored-by: Colm O hEigeartaigh <co...@users.noreply.github.com>
---
 .../apache/xml/security/encryption/XMLCipher.java  |  4 ++-
 .../test/dom/encryption/XMLCipherTest.java         | 41 ++++++++++++++++++++++
 2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/xml/security/encryption/XMLCipher.java b/src/main/java/org/apache/xml/security/encryption/XMLCipher.java
index 75075f90..3682d220 100644
--- a/src/main/java/org/apache/xml/security/encryption/XMLCipher.java
+++ b/src/main/java/org/apache/xml/security/encryption/XMLCipher.java
@@ -1150,7 +1150,9 @@ public final class XMLCipher {
                 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                     while ((numBytes = serializedData.read(buf)) != -1) {
                         byte[] data = c.update(buf, 0, numBytes);
-                        baos.write(data);
+                        if (data != null) {
+                            baos.write(data);
+                        }
                     }
                     baos.write(c.doFinal());
                     encryptedBytes = baos.toByteArray();
diff --git a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java
index b187f9de..13f033eb 100644
--- a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java
+++ b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java
@@ -75,6 +75,7 @@ import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
@@ -1012,6 +1013,46 @@ class XMLCipherTest {
         xmlCipher.decryptToByteArray(encryptedData);
     }
 
+    @Test
+    void testEncryptForDataExceeding8192bytes() throws Exception {
+        boolean bcAtFirstPosition = false;
+        if (Security.getProvider("BC") == null) {
+            // Use reflection to add new BouncyCastleProvider
+            try {
+                Class<?> bouncyCastleProviderClass = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
+                Provider bouncyCastleProvider = (Provider)bouncyCastleProviderClass.getConstructor().newInstance();
+                Security.insertProviderAt(bouncyCastleProvider, 1);
+                bcAtFirstPosition = true;
+            } catch (ReflectiveOperationException e) {
+                // BouncyCastle not installed, ignore
+            }
+        }
+        Assumptions.assumeTrue(bcAtFirstPosition);
+
+        KeyGenerator generator = KeyGenerator.getInstance("AES", "BC");
+        generator.init(128);
+        SecretKey key = generator.generateKey();
+
+        XMLCipher cipher = XMLCipher.getInstance(XMLCipher.AES_128_GCM);
+        cipher.init(XMLCipher.ENCRYPT_MODE, key);
+
+        Document document = document();
+        byte[] dataToEncrypt = new byte[8193];
+        for (int i = 0; i < 8193; i++) {
+            dataToEncrypt[i] = (byte) i;
+        }
+        EncryptedData encryptedData = cipher.encryptData(document, null, new ByteArrayInputStream(dataToEncrypt));
+
+        XMLCipher decipher = XMLCipher.getInstance(XMLCipher.AES_128);
+        decipher.init(XMLCipher.DECRYPT_MODE, key);
+        String algorithm = encryptedData.getEncryptionMethod().getAlgorithm();
+        assertEquals(XMLCipher.AES_128_GCM, algorithm);
+        byte[] decryptedByteArray = decipher.decryptToByteArray(decipher.martial(encryptedData));
+        assertArrayEquals(dataToEncrypt, decryptedByteArray);
+
+        Security.removeProvider("BC");
+    }
+
     @Test
     void testMultipleKEKs() throws Exception {