[java-opensaml COMMIT] in /trunk/opensaml-security-api/src: main/java/org/opensaml/security/crypto/KeySupport.java te...
noreply at shibboleth.net
noreply at shibboleth.net
Tue Dec 16 20:00:23 EST 2014
Author: putmanb
Date: Tue Dec 16 20:00:23 2014
New Revision: 4191
URL: http://svn.shibboleth.net/view/java-opensaml?rev=4191&view=rev
Log:
OSJ-73: Implement decodeSecretKey() method in KeySupport
Modified:
trunk/opensaml-security-api/src/main/java/org/opensaml/security/crypto/KeySupport.java
trunk/opensaml-security-api/src/test/java/org/opensaml/security/crypto/KeySupportTest.java
Modified: trunk/opensaml-security-api/src/main/java/org/opensaml/security/crypto/KeySupport.java
URL: http://svn.shibboleth.net/view/java-opensaml/trunk/opensaml-security-api/src/main/java/org/opensaml/security/crypto/KeySupport.java?rev=4191&r1=4190&r2=4191&view=diff
==============================================================================
--- trunk/opensaml-security-api/src/main/java/org/opensaml/security/crypto/KeySupport.java (original)
+++ trunk/opensaml-security-api/src/main/java/org/opensaml/security/crypto/KeySupport.java Tue Dec 16 20:00:23 2014
@@ -52,6 +52,7 @@
import javax.annotation.Nullable;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
import net.shibboleth.utilities.java.support.codec.Base64Support;
import net.shibboleth.utilities.java.support.collection.LazyMap;
@@ -101,21 +102,48 @@
}
/**
- * Decodes secret keys in DER and PEM format.
- *
- * This method is not yet implemented.
- *
- * @param key secret key
- * @param password password if the key is encrypted or null if not
+ * Produces SecretKey instances specified as a raw byte[] plus a JCA key algorithm.
+ *
+ * @param key the raw secret key bytes
+ * @param algorithm the JCA key algorithm
*
* @return the decoded key
*
* @throws KeyException thrown if the key can not be decoded
*/
- @Nonnull public static SecretKey decodeSecretKey(@Nonnull final byte[] key, @Nullable final char[] password)
- throws KeyException {
- // TODO
- throw new UnsupportedOperationException("This method is not yet supported");
+ @Nonnull public static SecretKey decodeSecretKey(@Nonnull final byte[] key, @Nonnull final String algorithm)
+ throws KeyException {
+ Logger log = getLogger();
+ Constraint.isNotNull(key, "Secret key bytes can not be null");
+ Constraint.isNotNull(algorithm, "Secret key algorithm can not be null");
+ Constraint.isGreaterThanOrEqual(1, key.length, "Secret key bytes can not be empty");
+
+ int keyLengthBits = key.length*8;
+
+ switch(algorithm) {
+ case "AES":
+ if (keyLengthBits != 128 && keyLengthBits != 192 && keyLengthBits != 256) {
+ throw new KeyException(String.format("Saw invalid key length %d for algorithm %s",
+ keyLengthBits, "AES"));
+ }
+ break;
+ case "DES":
+ if (keyLengthBits != 64) {
+ throw new KeyException(String.format("Saw invalid key length %d for algorithm %s",
+ keyLengthBits, "DES"));
+ }
+ break;
+ case "DESede":
+ if (keyLengthBits != 192 && keyLengthBits != 168) {
+ throw new KeyException(String.format("Saw invalid key length %d for algorithm %s",
+ keyLengthBits, "DESede"));
+ }
+ break;
+ default:
+ log.debug("No length and sanity checking done for key with algorithm: {}", algorithm);
+ }
+
+ return new SecretKeySpec(key, algorithm);
}
/**
Modified: trunk/opensaml-security-api/src/test/java/org/opensaml/security/crypto/KeySupportTest.java
URL: http://svn.shibboleth.net/view/java-opensaml/trunk/opensaml-security-api/src/test/java/org/opensaml/security/crypto/KeySupportTest.java?rev=4191&r1=4190&r2=4191&view=diff
==============================================================================
--- trunk/opensaml-security-api/src/test/java/org/opensaml/security/crypto/KeySupportTest.java (original)
+++ trunk/opensaml-security-api/src/test/java/org/opensaml/security/crypto/KeySupportTest.java Tue Dec 16 20:00:23 2014
@@ -18,15 +18,19 @@
package org.opensaml.security.crypto;
import java.io.InputStream;
+import java.security.KeyException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
+import java.security.SecureRandom;
+
+import javax.crypto.SecretKey;
import org.opensaml.security.SecurityException;
-import org.opensaml.security.crypto.KeySupport;
import org.testng.Assert;
+import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
@@ -213,6 +217,29 @@
Assert.assertEquals(KeySupport.getKeyLength(KeySupport.generateKey("DESede", 112, null)), new Integer(192));
[... 29 lines stripped ...]
More information about the commits
mailing list