Help with assertion encryption ?
Brent Putman
putmanb at georgetown.edu
Tue May 20 21:15:10 EDT 2014
On 5/20/14 6:49 PM, Brent Putman wrote:
>
> AlgorithmParameterSpec algParamSpec = new GCMParameterSpec(128, iv);
>
>
> So that looks promising. If that is the case, then the issue is with
> Santuario. It probably needs to special case for GCM. I just tested
> that mechanism with BC on Java 7 and it works also. So perhaps that
> is the new "proper" way to do GCM, but BC doesn't require it for
> historical reasons and Santuario is just lagging behind and written to
> the older more lax BC way. At least I hope so.
I'm pretty convinced that this is the way you're supposed to do it. I
wrote a quick and dirty patch to XMLCipher that does this (attached),
and successfully encrypted and decrypted using AES 128 GCM with Java 8
and no BC.
However... turns out GCMParameterSpec only showed up in the API with
Java 1.7. AFAICT Santuario is still, even in their latest 2.0.0
release, targeting 1.6. So... I don't think this will necessarily be a
straightforward. They may in fact be aware of this issue already, and
haven't fixed it for the versioning reason. Tomorrow when I'm more sure
of my facts, I'll post something to the xmlsec list.
I did find this:
http://mail.openjdk.java.net/pipermail/security-dev/2013-July/008079.html
so Sean Mullan may already be aware of this.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://shibboleth.net/pipermail/dev/attachments/20140520/76cd059d/attachment-0001.html
-------------- next part --------------
Index: src/main/java/org/apache/xml/security/encryption/XMLCipher.java
===================================================================
--- src/main/java/org/apache/xml/security/encryption/XMLCipher.java (revision 1596418)
+++ src/main/java/org/apache/xml/security/encryption/XMLCipher.java (working copy)
@@ -28,6 +28,7 @@
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
+import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.MGF1ParameterSpec;
import java.util.ArrayList;
import java.util.HashMap;
@@ -40,6 +41,7 @@
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
+import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource;
@@ -1125,7 +1127,7 @@
try {
int ivLen = JCEMapper.getIVLengthFromURI(algorithm) / 8;
byte[] temp = XMLSecurityConstants.generateBytes(ivLen);
- IvParameterSpec paramSpec = new IvParameterSpec(temp);
+ AlgorithmParameterSpec paramSpec = constructBlockCipherParameters(algorithm, temp);
c.init(cipherMode, key, paramSpec);
} catch (InvalidKeyException ike) {
throw new XMLEncryptionException("empty", ike);
@@ -1196,6 +1198,25 @@
}
/**
+ * Build an <code>AlgorithmParameterSpec</code> instance used to initialize a <code>Cipher</code> instance
+ * for block cipher encryption and decryption.
+ *
+ * @param algorithm the XML encryption algorithm URI
+ * @param iv the initialization vector
+ * @return the newly constructed AlgorithmParameterSpec instance, appropriate for the
+ * specified algorithm
+ */
+ private AlgorithmParameterSpec constructBlockCipherParameters(String algorithm, byte[] iv) {
+ if (EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128_GCM.equals(algorithm)
+ || EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES192_GCM.equals(algorithm)
+ || EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES256_GCM.equals(algorithm)) {
+ return new GCMParameterSpec(128, iv);
+ } else {
+ return new IvParameterSpec(iv);
+ }
+ }
+
+ /**
* Returns an <code>EncryptedData</code> interface. Use this operation if
* you want to load an <code>EncryptedData</code> structure from a DOM
* structure and manipulate the contents.
@@ -1731,10 +1752,10 @@
// necessary bytes into a dedicated array.
System.arraycopy(encryptedBytes, 0, ivBytes, 0, ivLen);
- IvParameterSpec iv = new IvParameterSpec(ivBytes);
+ AlgorithmParameterSpec paramSpec = constructBlockCipherParameters(algorithm, ivBytes);
try {
- c.init(cipherMode, key, iv);
+ c.init(cipherMode, key, paramSpec);
} catch (InvalidKeyException ike) {
throw new XMLEncryptionException("empty", ike);
} catch (InvalidAlgorithmParameterException iape) {
More information about the dev
mailing list