|
|
762105 |
# HG changeset patch
|
|
|
762105 |
# User Jack Magne <jmagne@redhat.com>
|
|
|
762105 |
# Date 1504307754 25200
|
|
|
762105 |
# Fri Sep 01 16:15:54 2017 -0700
|
|
|
762105 |
# Node ID eec15518fd61f1d988c25b4de589555796f9e65f
|
|
|
762105 |
# Parent 17d1d7b740ca5777fbcf8ee817a2f26b9c93593a
|
|
|
762105 |
unwrapping of HMAC-SHA1 secret keys using AES wrapping and unwrapping
|
|
|
762105 |
cfu on behalf of jmagne
|
|
|
762105 |
|
|
|
762105 |
diff -r 17d1d7b740ca -r eec15518fd61 org/mozilla/jss/pkcs11/PK11KeyWrapper.java
|
|
|
762105 |
--- a/org/mozilla/jss/pkcs11/PK11KeyWrapper.java Mon May 01 10:39:50 2017 -0700
|
|
|
762105 |
+++ b/org/mozilla/jss/pkcs11/PK11KeyWrapper.java Fri Sep 01 16:15:54 2017 -0700
|
|
|
762105 |
@@ -588,6 +588,8 @@
|
|
|
762105 |
return EncryptionAlgorithm.RC4;
|
|
|
762105 |
} else if( type == SymmetricKey.AES ) {
|
|
|
762105 |
return EncryptionAlgorithm.AES_128_ECB;
|
|
|
762105 |
+ } else if( type == SymmetricKey.SHA1_HMAC) {
|
|
|
762105 |
+ return HMACAlgorithm.SHA1;
|
|
|
762105 |
} else {
|
|
|
762105 |
Assert._assert( type == SymmetricKey.RC2 );
|
|
|
762105 |
return EncryptionAlgorithm.RC2_CBC;
|
|
|
762105 |
diff -r 17d1d7b740ca -r eec15518fd61 org/mozilla/jss/pkcs11/PK11MessageDigest.c
|
|
|
762105 |
--- a/org/mozilla/jss/pkcs11/PK11MessageDigest.c Mon May 01 10:39:50 2017 -0700
|
|
|
762105 |
+++ b/org/mozilla/jss/pkcs11/PK11MessageDigest.c Fri Sep 01 16:15:54 2017 -0700
|
|
|
762105 |
@@ -67,19 +67,19 @@
|
|
|
762105 |
}
|
|
|
762105 |
|
|
|
762105 |
/* copy the key, setting the CKA_SIGN attribute */
|
|
|
762105 |
- /*
|
|
|
762105 |
+
|
|
|
762105 |
newKey = PK11_CopySymKeyForSigning(origKey, mech);
|
|
|
762105 |
+
|
|
|
762105 |
+ /* For some key on the hsm, this call could fail, but the key may work anyway */
|
|
|
762105 |
+
|
|
|
762105 |
if( newKey == NULL ) {
|
|
|
762105 |
- JSS_throwMsg(env, DIGEST_EXCEPTION,
|
|
|
762105 |
- "Unable to set CKA_SIGN attribute on symmetric key");
|
|
|
762105 |
- goto finish;
|
|
|
762105 |
+ newKey = origKey;
|
|
|
762105 |
}
|
|
|
762105 |
- */
|
|
|
762105 |
|
|
|
762105 |
param.data = NULL;
|
|
|
762105 |
param.len = 0;
|
|
|
762105 |
|
|
|
762105 |
- context = PK11_CreateContextBySymKey(mech, CKA_SIGN, origKey, ¶m;;
|
|
|
762105 |
+ context = PK11_CreateContextBySymKey(mech, CKA_SIGN, newKey, ¶m;;
|
|
|
762105 |
if( context == NULL ) {
|
|
|
762105 |
JSS_throwMsg(env, DIGEST_EXCEPTION,
|
|
|
762105 |
"Unable to initialize digest context");
|
|
|
762105 |
@@ -88,7 +88,7 @@
|
|
|
762105 |
|
|
|
762105 |
contextObj = JSS_PK11_wrapCipherContextProxy(env, &context);
|
|
|
762105 |
finish:
|
|
|
762105 |
- if(newKey) {
|
|
|
762105 |
+ if(newKey && (newKey != origKey)) {
|
|
|
762105 |
/* SymKeys are ref counted, and the context will free it's ref
|
|
|
762105 |
* when it is destroyed */
|
|
|
762105 |
PK11_FreeSymKey(newKey);
|
|
|
762105 |
diff -r 17d1d7b740ca -r eec15518fd61 org/mozilla/jss/tests/HmacTest.java
|
|
|
762105 |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
|
|
|
762105 |
+++ b/org/mozilla/jss/tests/HmacTest.java Fri Sep 01 16:15:54 2017 -0700
|
|
|
762105 |
@@ -0,0 +1,119 @@
|
|
|
762105 |
+
|
|
|
762105 |
+package org.mozilla.jss.tests;
|
|
|
762105 |
+
|
|
|
762105 |
+
|
|
|
762105 |
+import java.security.Key;
|
|
|
762105 |
+import javax.crypto.Cipher;
|
|
|
762105 |
+import javax.crypto.KeyGenerator;
|
|
|
762105 |
+import javax.crypto.Mac;
|
|
|
762105 |
+import javax.crypto.SecretKey;
|
|
|
762105 |
+import javax.crypto.spec.IvParameterSpec;
|
|
|
762105 |
+
|
|
|
762105 |
+import org.mozilla.jss.CryptoManager;
|
|
|
762105 |
+import org.mozilla.jss.crypto.CryptoToken;
|
|
|
762105 |
+import org.mozilla.jss.crypto.SymmetricKey;
|
|
|
762105 |
+
|
|
|
762105 |
+
|
|
|
762105 |
+public class HmacTest {
|
|
|
762105 |
+
|
|
|
762105 |
+ private static final String INTERNAL_KEY_STORAGE_TOKEN =
|
|
|
762105 |
+ new CryptoManager.InitializationValues("").getInternalKeyStorageTokenDescription().trim();
|
|
|
762105 |
+
|
|
|
762105 |
+ private static final String NSS_DATABASE_DIR = "sql:data";
|
|
|
762105 |
+ private static final String PROVIDER = "Mozilla-JSS";
|
|
|
762105 |
+
|
|
|
762105 |
+
|
|
|
762105 |
+ public static void main(String[] args)
|
|
|
762105 |
+ {
|
|
|
762105 |
+
|
|
|
762105 |
+ String algorithm = "hmac-sha1";
|
|
|
762105 |
+
|
|
|
762105 |
+ try {
|
|
|
762105 |
+ configureCrypto(args);
|
|
|
762105 |
+
|
|
|
762105 |
+ Mac mac = Mac.getInstance(algorithm, PROVIDER);
|
|
|
762105 |
+
|
|
|
762105 |
+ byte[] keyData = new byte[16];
|
|
|
762105 |
+ Key key = importHmacSha1Key(keyData);
|
|
|
762105 |
+
|
|
|
762105 |
+ mac.init(key);
|
|
|
762105 |
+
|
|
|
762105 |
+ doHMAC(mac,"Dogtag rules!");
|
|
|
762105 |
+
|
|
|
762105 |
+ System.out.println("Done");
|
|
|
762105 |
+
|
|
|
762105 |
+ System.exit(0);
|
|
|
762105 |
+ } catch (Exception e) {
|
|
|
762105 |
+ System.exit(1);
|
|
|
762105 |
+ }
|
|
|
762105 |
+ }
|
|
|
762105 |
+
|
|
|
762105 |
+ private static void configureCrypto(String[] args)
|
|
|
762105 |
+ throws Exception {
|
|
|
762105 |
+
|
|
|
762105 |
+ CryptoManager.InitializationValues initializationValues =
|
|
|
762105 |
+ new CryptoManager.InitializationValues(args[0]);
|
|
|
762105 |
+
|
|
|
762105 |
+ CryptoManager.initialize(initializationValues);
|
|
|
762105 |
+
|
|
|
762105 |
+ CryptoManager cryptoManager = CryptoManager.getInstance();
|
|
|
762105 |
+
|
|
|
762105 |
+ CryptoToken cryptoToken =
|
|
|
762105 |
+ cryptoManager.getTokenByName(INTERNAL_KEY_STORAGE_TOKEN);
|
|
|
762105 |
+
|
|
|
762105 |
+ cryptoManager.setThreadToken(cryptoToken);
|
|
|
762105 |
+ }
|
|
|
762105 |
+
|
|
|
762105 |
+ private static Key importHmacSha1Key(byte[] key)
|
|
|
762105 |
+ throws Exception {
|
|
|
762105 |
+
|
|
|
762105 |
+ final String WRAPPING_ALGORITHM = "AES/CBC/PKCS5Padding";
|
|
|
762105 |
+
|
|
|
762105 |
+ Key wrappingKey = getWrappingKey();
|
|
|
762105 |
+
|
|
|
762105 |
+ byte[] iv = new byte[16];
|
|
|
762105 |
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
|
|
|
762105 |
+
|
|
|
762105 |
+ Cipher wrappingCipher = Cipher.getInstance(WRAPPING_ALGORITHM, PROVIDER);
|
|
|
762105 |
+ wrappingCipher.init(Cipher.ENCRYPT_MODE, wrappingKey, ivParameterSpec);
|
|
|
762105 |
+
|
|
|
762105 |
+ byte[] wrappedKeyData = wrappingCipher.doFinal(key);
|
|
|
762105 |
+
|
|
|
762105 |
+ Cipher unwrappingCipher = Cipher.getInstance(WRAPPING_ALGORITHM, PROVIDER);
|
|
|
762105 |
+ unwrappingCipher.init(Cipher.UNWRAP_MODE, wrappingKey, ivParameterSpec);
|
|
|
762105 |
+
|
|
|
762105 |
+ return (SecretKey) unwrappingCipher.unwrap(wrappedKeyData,
|
|
|
762105 |
+ SymmetricKey.SHA1_HMAC.toString(),
|
|
|
762105 |
+ Cipher.SECRET_KEY);
|
|
|
762105 |
+ }
|
|
|
762105 |
+
|
|
|
762105 |
+ private static synchronized Key getWrappingKey()
|
|
|
762105 |
+ throws Exception {
|
|
|
762105 |
+
|
|
|
762105 |
+ final String keyGenAlgorithm = "AES";
|
|
|
762105 |
+ final int wrappingKeyLength = 256;
|
|
|
762105 |
+
|
|
|
762105 |
+ KeyGenerator keyGen = KeyGenerator.getInstance(keyGenAlgorithm, PROVIDER);
|
|
|
762105 |
+ keyGen.init(wrappingKeyLength);
|
|
|
762105 |
+ return keyGen.generateKey();
|
|
|
762105 |
+ }
|
|
|
762105 |
+
|
|
|
762105 |
+ public static void doHMAC(Mac mozillaHmac, String clearText)
|
|
|
762105 |
+ throws Exception {
|
|
|
762105 |
+ byte[] mozillaHmacOut;
|
|
|
762105 |
+
|
|
|
762105 |
+ //Get the Mozilla HMAC
|
|
|
762105 |
+ mozillaHmacOut = mozillaHmac.doFinal(clearText.getBytes());
|
|
|
762105 |
+
|
|
|
762105 |
+ if (mozillaHmacOut.length == mozillaHmac.getMacLength()) {
|
|
|
762105 |
+ System.out.println(PROVIDER + " supports " +
|
|
|
762105 |
+ mozillaHmac.getAlgorithm() + " and the output size is " + mozillaHmac.getMacLength());
|
|
|
762105 |
+ } else {
|
|
|
762105 |
+ throw new Exception("ERROR: hmac output size is " +
|
|
|
762105 |
+ mozillaHmacOut.length + ", should be " +
|
|
|
762105 |
+ mozillaHmac.getMacLength());
|
|
|
762105 |
+ }
|
|
|
762105 |
+ }
|
|
|
762105 |
+
|
|
|
762105 |
+
|
|
|
762105 |
+}
|
|
|
762105 |
diff -r 17d1d7b740ca -r eec15518fd61 org/mozilla/jss/tests/all.pl
|
|
|
762105 |
--- a/org/mozilla/jss/tests/all.pl Mon May 01 10:39:50 2017 -0700
|
|
|
762105 |
+++ b/org/mozilla/jss/tests/all.pl Fri Sep 01 16:15:54 2017 -0700
|
|
|
762105 |
@@ -492,6 +492,10 @@
|
|
|
762105 |
$command = "$java -cp $jss_classpath org.mozilla.jss.tests.HMACTest $testdir $pwfile";
|
|
|
762105 |
run_test($testname, $command);
|
|
|
762105 |
|
|
|
762105 |
+$testname = "HMAC Unwrap";
|
|
|
762105 |
+$command = "$java -cp $jss_classpath org.mozilla.jss.tests.HmacTest $testdir $pwfile";
|
|
|
762105 |
+run_test($testname, $command);
|
|
|
762105 |
+
|
|
|
762105 |
$testname = "KeyWrapping ";
|
|
|
762105 |
$command = "$java -cp $jss_classpath org.mozilla.jss.tests.JCAKeyWrap $testdir $pwfile";
|
|
|
762105 |
run_test($testname, $command);
|