< prev index next >

src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11TlsKeyMaterialGenerator.java

Print this page

        

@@ -36,10 +36,12 @@
 import sun.security.internal.spec.*;
 import sun.security.internal.interfaces.TlsMasterSecret;
 
 import static sun.security.pkcs11.TemplateManager.*;
 import sun.security.pkcs11.wrapper.*;
+import sun.security.ssl.ProtocolVersion;
+
 import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
 
 /**
  * KeyGenerator to calculate the SSL/TLS key material (cipher keys and ivs,
  * mac keys) from the master secret.

@@ -59,10 +61,12 @@
     private final String algorithm;
 
     // mechanism id
     private long mechanism;
 
+    private ProtocolVersion tlsVersion;
+
     // parameter spec
     @SuppressWarnings("deprecation")
     private TlsKeyMaterialParameterSpec spec;
 
     // master secret as a P11Key

@@ -94,28 +98,35 @@
         if (params instanceof TlsKeyMaterialParameterSpec == false) {
             throw new InvalidAlgorithmParameterException(MSG);
         }
 
         TlsKeyMaterialParameterSpec spec = (TlsKeyMaterialParameterSpec)params;
-        int version = (spec.getMajorVersion() << 8) | spec.getMinorVersion();
+        tlsVersion = ProtocolVersion.valueOf(spec.getMajorVersion(), spec.getMinorVersion());
 
-        if ((version == 0x0300 && !supportSSLv3) || (version < 0x0300) ||
-            (version > 0x0302)) {
+        if ((tlsVersion.compareTo(ProtocolVersion.SSL30) == 0 && !supportSSLv3) ||
+                (tlsVersion.compareTo(ProtocolVersion.SSL30) < 0) ||
+                (tlsVersion.compareTo(ProtocolVersion.TLS12) > 0)) {
              throw new InvalidAlgorithmParameterException
                     ("Only" + (supportSSLv3? " SSL 3.0,": "") +
-                     " TLS 1.0, and TLS 1.1 are supported (0x" +
-                     Integer.toHexString(version) + ")");
+                     " TLS 1.0, TLS 1.1 and TLS 1.2 are supported (" +
+                     tlsVersion + ")");
         }
         try {
             p11Key = P11SecretKeyFactory.convertKey
                             (token, spec.getMasterSecret(), "TlsMasterSecret");
         } catch (InvalidKeyException e) {
             throw new InvalidAlgorithmParameterException("init() failed", e);
         }
         this.spec = spec;
-        this.mechanism = (version == 0x0300)?
-            CKM_SSL3_KEY_AND_MAC_DERIVE : CKM_TLS_KEY_AND_MAC_DERIVE;
+        if (tlsVersion.compareTo(ProtocolVersion.SSL30) == 0) {
+            mechanism = CKM_SSL3_KEY_AND_MAC_DERIVE;
+        } else if (tlsVersion.compareTo(ProtocolVersion.TLS10) == 0 ||
+                    tlsVersion.compareTo(ProtocolVersion.TLS11) == 0) {
+            mechanism = CKM_TLS_KEY_AND_MAC_DERIVE;
+        } else if (tlsVersion.compareTo(ProtocolVersion.TLS12) == 0) {
+            mechanism = CKM_TLS12_KEY_AND_MAC_DERIVE;
+        }
     }
 
     protected void engineInit(int keysize, SecureRandom random) {
         throw new InvalidParameterException(MSG);
     }

@@ -139,12 +150,22 @@
             expandedKeyBits = keyBits;
         }
 
         CK_SSL3_RANDOM_DATA random = new CK_SSL3_RANDOM_DATA
                             (spec.getClientRandom(), spec.getServerRandom());
-        CK_SSL3_KEY_MAT_PARAMS params = new CK_SSL3_KEY_MAT_PARAMS
+        Object params = null;
+        CK_MECHANISM ckMechanism = null;
+        if (tlsVersion.compareTo(ProtocolVersion.TLS12) < 0) {
+            params = new CK_SSL3_KEY_MAT_PARAMS
                             (macBits, keyBits, ivBits, isExportable, random);
+            ckMechanism = new CK_MECHANISM(mechanism, (CK_SSL3_KEY_MAT_PARAMS)params);
+        } else if (tlsVersion.compareTo(ProtocolVersion.TLS12) == 0) {
+            params = new CK_TLS12_KEY_MAT_PARAMS
+                    (macBits, keyBits, ivBits, isExportable, random,
+                    SunPKCS11.hashAlgorithmToHashMechanismMap.get(spec.getPRFHashAlg()));
+            ckMechanism = new CK_MECHANISM(mechanism, (CK_TLS12_KEY_MAT_PARAMS)params);
+        }
 
         String cipherAlgorithm = spec.getCipherAlgorithm();
         long keyType = P11SecretKeyFactory.getKeyType(cipherAlgorithm);
         if (keyType < 0) {
             if (keyBits != 0) {

@@ -171,14 +192,20 @@
                 attributes = new CK_ATTRIBUTE[0];
             }
             attributes = token.getAttributes
                 (O_GENERATE, CKO_SECRET_KEY, keyType, attributes);
             // the returned keyID is a dummy, ignore
-            long keyID = token.p11.C_DeriveKey(session.id(),
-                new CK_MECHANISM(mechanism, params), p11Key.keyID, attributes);
+            token.p11.C_DeriveKey(session.id(),
+                ckMechanism, p11Key.keyID, attributes);
+
 
-            CK_SSL3_KEY_MAT_OUT out = params.pReturnedKeyMaterial;
+            CK_SSL3_KEY_MAT_OUT out = null;
+            if (params instanceof CK_SSL3_KEY_MAT_PARAMS) {
+                out = ((CK_SSL3_KEY_MAT_PARAMS)params).pReturnedKeyMaterial;
+            } else if (params instanceof CK_TLS12_KEY_MAT_PARAMS) {
+                out = ((CK_TLS12_KEY_MAT_PARAMS)params).pReturnedKeyMaterial;
+            }
             // Note that the MAC keys do not inherit all attributes from the
             // template, but they do inherit the sensitive/extractable/token
             // flags, which is all P11Key cares about.
             SecretKey clientMacKey, serverMacKey;
 
< prev index next >