You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by "Colm O hEigeartaigh (Jira)" <ji...@apache.org> on 2024/11/05 06:39:00 UTC

[jira] [Closed] (SANTUARIO-610) NullPointerException in XMLCipher with BC AES GCM NoPadding

     [ https://issues.apache.org/jira/browse/SANTUARIO-610?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Colm O hEigeartaigh closed SANTUARIO-610.
-----------------------------------------

> NullPointerException in XMLCipher with BC AES GCM NoPadding
> -----------------------------------------------------------
>
>                 Key: SANTUARIO-610
>                 URL: https://issues.apache.org/jira/browse/SANTUARIO-610
>             Project: Santuario
>          Issue Type: Bug
>          Components: Java
>    Affects Versions: Java 3.0.1, Java 4.0.2
>            Reporter: Benjamin Krahl
>            Assignee: Colm O hEigeartaigh
>            Priority: Critical
>             Fix For: Java 3.0.5, Java 4.0.3
>
>
> I encountered a {{NullPointerException}} when using the {{XMLCipher#encryptData(org.w3c.dom.Document, org.w3c.dom.Element, java.lang.String, java.io.InputStream)}} method in conjunction with BC AES GCM NoPadding.
> -If the input data is not a multiple of the AES block size- If the input data is 1-15 bytes larger than the initial buffer of 8192 bytes, the BC implementation returns {{{}null{}}}. This behavior is consistent with the JDK SPI {{{}javax.crypto.Cipher#update(byte[], int, int){}}}, which states:
> {quote}Returns:
> the new buffer with the result, or null if the underlying cipher is a block cipher and the input data is too short to result in a new block.
> {quote}
> However, this leads to a {{NullPointerException}} in {{{}XMLCipher{}}}.
> h4. Steps to Reproduce
>  # -Provide a byte array which is not a multiple of AES block size- Provide a byte array which is 1-15 bytes larger than the buffer of 8192 bytes
>  # Initialize {{XMLCipher}} 
>  # Try to encrypt data
> Code snippet
> {code:java}
> Security.insertProviderAt(new BouncyCastleProvider(), 1);
> KeyGenerator generator = KeyGenerator.getInstance("AES", BouncyCastleProvider.PROVIDER_NAME);
> generator.init(128);
> SecretKey sessionKey = generator.generateKey();
> XMLCipher xmlCipher = XMLCipher.getInstance(XMLCipher.AES_128_GCM);
> xmlCipher.init(XMLCipher.ENCRYPT_MODE, sessionKey);
> SOAPMessage soapMsg = loadSoapMsg();
> Document soapDoc = soapMsg.getSOAPPart().getOwnerDocument();
> EncryptedData data = xmlCipher.encryptData(soapDoc, null, new ByteArrayInputStream(dataToEncrypt));{code}
> *Expected Result:*
> The encrypted cipher text
> h4. Actual Result
> {{NullPointerException}}
> h4. Hints
> I debugged the code and found a potential issue here
> {code:java}
> try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
>     while ((numBytes = serializedData.read(buf)) != -1) {
>         byte[] data = c.update(buf, 0, numBytes);
>         baos.write(data);
>     }
>     baos.write(c.doFinal());
>     encryptedBytes = baos.toByteArray();
> }
> {code}
> I assume that one only needs to wrap {{baos.write(data)}} in an if statement and doFinal encrypts the remaining bytes:
> {code:java}
> if (data != null) {
>     baos.write(data);
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)