author | mbalao |
Wed, 12 Sep 2018 13:09:51 +0200 | |
changeset 51800 | bccd9966f1ed |
parent 47216 | 71c04702a3d5 |
child 53257 | 5170dc2bcf64 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.pkcs11; |
|
27 |
||
28 |
import java.util.*; |
|
29 |
||
30 |
import java.security.*; |
|
31 |
import java.security.spec.AlgorithmParameterSpec; |
|
32 |
||
33 |
import javax.crypto.*; |
|
34 |
import javax.crypto.spec.*; |
|
35 |
||
36 |
import sun.security.internal.spec.*; |
|
37 |
import sun.security.internal.interfaces.TlsMasterSecret; |
|
38 |
||
39 |
import static sun.security.pkcs11.TemplateManager.*; |
|
40 |
import sun.security.pkcs11.wrapper.*; |
|
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
41 |
|
2 | 42 |
import static sun.security.pkcs11.wrapper.PKCS11Constants.*; |
43 |
||
44 |
/** |
|
45 |
* KeyGenerator to calculate the SSL/TLS key material (cipher keys and ivs, |
|
46 |
* mac keys) from the master secret. |
|
47 |
* |
|
48 |
* @author Andreas Sterbenz |
|
49 |
* @since 1.6 |
|
50 |
*/ |
|
51 |
public final class P11TlsKeyMaterialGenerator extends KeyGeneratorSpi { |
|
52 |
||
53 |
private final static String MSG = "TlsKeyMaterialGenerator must be " |
|
54 |
+ "initialized using a TlsKeyMaterialParameterSpec"; |
|
55 |
||
56 |
// token instance |
|
57 |
private final Token token; |
|
58 |
||
59 |
// algorithm name |
|
60 |
private final String algorithm; |
|
61 |
||
62 |
// mechanism id |
|
63 |
private long mechanism; |
|
64 |
||
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
65 |
private int tlsVersion; |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
66 |
|
2 | 67 |
// parameter spec |
27936
ca9ee8e3d527
8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents:
25859
diff
changeset
|
68 |
@SuppressWarnings("deprecation") |
2 | 69 |
private TlsKeyMaterialParameterSpec spec; |
70 |
||
71 |
// master secret as a P11Key |
|
72 |
private P11Key p11Key; |
|
73 |
||
41123
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
27936
diff
changeset
|
74 |
// whether SSLv3 is supported |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
27936
diff
changeset
|
75 |
private final boolean supportSSLv3; |
2 | 76 |
|
77 |
P11TlsKeyMaterialGenerator(Token token, String algorithm, long mechanism) |
|
78 |
throws PKCS11Exception { |
|
79 |
super(); |
|
80 |
this.token = token; |
|
81 |
this.algorithm = algorithm; |
|
82 |
this.mechanism = mechanism; |
|
41123
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
27936
diff
changeset
|
83 |
|
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
27936
diff
changeset
|
84 |
// Given the current lookup order specified in SunPKCS11.java, |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
27936
diff
changeset
|
85 |
// if CKM_SSL3_KEY_AND_MAC_DERIVE is not used to construct this object, |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
27936
diff
changeset
|
86 |
// it means that this mech is disabled or unsupported. |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
27936
diff
changeset
|
87 |
this.supportSSLv3 = (mechanism == CKM_SSL3_KEY_AND_MAC_DERIVE); |
2 | 88 |
} |
89 |
||
90 |
protected void engineInit(SecureRandom random) { |
|
91 |
throw new InvalidParameterException(MSG); |
|
92 |
} |
|
93 |
||
27936
ca9ee8e3d527
8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents:
25859
diff
changeset
|
94 |
@SuppressWarnings("deprecation") |
2 | 95 |
protected void engineInit(AlgorithmParameterSpec params, |
96 |
SecureRandom random) throws InvalidAlgorithmParameterException { |
|
97 |
if (params instanceof TlsKeyMaterialParameterSpec == false) { |
|
98 |
throw new InvalidAlgorithmParameterException(MSG); |
|
99 |
} |
|
41123
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
27936
diff
changeset
|
100 |
|
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
27936
diff
changeset
|
101 |
TlsKeyMaterialParameterSpec spec = (TlsKeyMaterialParameterSpec)params; |
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
102 |
tlsVersion = (spec.getMajorVersion() << 8) | spec.getMinorVersion(); |
41123
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
27936
diff
changeset
|
103 |
|
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
104 |
if ((tlsVersion == 0x0300 && !supportSSLv3) || |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
105 |
(tlsVersion < 0x0300) || (tlsVersion > 0x0303)) { |
41123
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
27936
diff
changeset
|
106 |
throw new InvalidAlgorithmParameterException |
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
27936
diff
changeset
|
107 |
("Only" + (supportSSLv3? " SSL 3.0,": "") + |
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
108 |
" TLS 1.0, TLS 1.1 and TLS 1.2 are supported (" + |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
109 |
tlsVersion + ")"); |
41123
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
27936
diff
changeset
|
110 |
} |
2 | 111 |
try { |
112 |
p11Key = P11SecretKeyFactory.convertKey |
|
113 |
(token, spec.getMasterSecret(), "TlsMasterSecret"); |
|
114 |
} catch (InvalidKeyException e) { |
|
115 |
throw new InvalidAlgorithmParameterException("init() failed", e); |
|
116 |
} |
|
41123
66eaead2a150
8136355: CKM_SSL3_KEY_AND_MAC_DERIVE no longer available by default on Solaris 12
valeriep
parents:
27936
diff
changeset
|
117 |
this.spec = spec; |
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
118 |
if (tlsVersion == 0x0300) { |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
119 |
mechanism = CKM_SSL3_KEY_AND_MAC_DERIVE; |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
120 |
} else if (tlsVersion == 0x0301 || tlsVersion == 0x0302) { |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
121 |
mechanism = CKM_TLS_KEY_AND_MAC_DERIVE; |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
122 |
} |
2 | 123 |
} |
124 |
||
125 |
protected void engineInit(int keysize, SecureRandom random) { |
|
126 |
throw new InvalidParameterException(MSG); |
|
127 |
} |
|
128 |
||
27936
ca9ee8e3d527
8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents:
25859
diff
changeset
|
129 |
@SuppressWarnings("deprecation") |
2 | 130 |
protected SecretKey engineGenerateKey() { |
131 |
if (spec == null) { |
|
132 |
throw new IllegalStateException |
|
133 |
("TlsKeyMaterialGenerator must be initialized"); |
|
134 |
} |
|
135 |
int macBits = spec.getMacKeyLength() << 3; |
|
136 |
int ivBits = spec.getIvLength() << 3; |
|
137 |
||
138 |
int expandedKeyBits = spec.getExpandedCipherKeyLength() << 3; |
|
139 |
int keyBits = spec.getCipherKeyLength() << 3; |
|
140 |
boolean isExportable; |
|
141 |
if (expandedKeyBits != 0) { |
|
142 |
isExportable = true; |
|
143 |
} else { |
|
144 |
isExportable = false; |
|
145 |
expandedKeyBits = keyBits; |
|
146 |
} |
|
147 |
||
148 |
CK_SSL3_RANDOM_DATA random = new CK_SSL3_RANDOM_DATA |
|
149 |
(spec.getClientRandom(), spec.getServerRandom()); |
|
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
150 |
Object params = null; |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
151 |
CK_MECHANISM ckMechanism = null; |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
152 |
if (tlsVersion < 0x0303) { |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
153 |
params = new CK_SSL3_KEY_MAT_PARAMS |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
154 |
(macBits, keyBits, ivBits, isExportable, random); |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
155 |
ckMechanism = new CK_MECHANISM(mechanism, (CK_SSL3_KEY_MAT_PARAMS)params); |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
156 |
} else if (tlsVersion == 0x0303) { |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
157 |
params = new CK_TLS12_KEY_MAT_PARAMS |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
158 |
(macBits, keyBits, ivBits, isExportable, random, |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
159 |
Functions.getHashMechId(spec.getPRFHashAlg())); |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
160 |
ckMechanism = new CK_MECHANISM(mechanism, (CK_TLS12_KEY_MAT_PARAMS)params); |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
161 |
} |
2 | 162 |
|
163 |
String cipherAlgorithm = spec.getCipherAlgorithm(); |
|
164 |
long keyType = P11SecretKeyFactory.getKeyType(cipherAlgorithm); |
|
165 |
if (keyType < 0) { |
|
166 |
if (keyBits != 0) { |
|
167 |
throw new ProviderException |
|
168 |
("Unknown algorithm: " + spec.getCipherAlgorithm()); |
|
169 |
} else { |
|
170 |
// NULL encryption ciphersuites |
|
171 |
keyType = CKK_GENERIC_SECRET; |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
Session session = null; |
|
176 |
try { |
|
177 |
session = token.getObjSession(); |
|
178 |
CK_ATTRIBUTE[] attributes; |
|
179 |
if (keyBits != 0) { |
|
180 |
attributes = new CK_ATTRIBUTE[] { |
|
181 |
new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY), |
|
182 |
new CK_ATTRIBUTE(CKA_KEY_TYPE, keyType), |
|
183 |
new CK_ATTRIBUTE(CKA_VALUE_LEN, expandedKeyBits >> 3), |
|
184 |
}; |
|
185 |
} else { |
|
186 |
// ciphersuites with NULL ciphers |
|
187 |
attributes = new CK_ATTRIBUTE[0]; |
|
188 |
} |
|
189 |
attributes = token.getAttributes |
|
190 |
(O_GENERATE, CKO_SECRET_KEY, keyType, attributes); |
|
191 |
// the returned keyID is a dummy, ignore |
|
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
192 |
token.p11.C_DeriveKey(session.id(), |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
193 |
ckMechanism, p11Key.keyID, attributes); |
2 | 194 |
|
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
195 |
CK_SSL3_KEY_MAT_OUT out = null; |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
196 |
if (params instanceof CK_SSL3_KEY_MAT_PARAMS) { |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
197 |
out = ((CK_SSL3_KEY_MAT_PARAMS)params).pReturnedKeyMaterial; |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
198 |
} else if (params instanceof CK_TLS12_KEY_MAT_PARAMS) { |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
199 |
out = ((CK_TLS12_KEY_MAT_PARAMS)params).pReturnedKeyMaterial; |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
200 |
} |
2 | 201 |
// Note that the MAC keys do not inherit all attributes from the |
202 |
// template, but they do inherit the sensitive/extractable/token |
|
203 |
// flags, which is all P11Key cares about. |
|
16913 | 204 |
SecretKey clientMacKey, serverMacKey; |
205 |
||
206 |
// The MAC size may be zero for GCM mode. |
|
207 |
// |
|
208 |
// PKCS11 does not support GCM mode as the author made the comment, |
|
209 |
// so the macBits is unlikely to be zero. It's only a place holder. |
|
210 |
if (macBits != 0) { |
|
211 |
clientMacKey = P11Key.secretKey |
|
2 | 212 |
(session, out.hClientMacSecret, "MAC", macBits, attributes); |
16913 | 213 |
serverMacKey = P11Key.secretKey |
2 | 214 |
(session, out.hServerMacSecret, "MAC", macBits, attributes); |
16913 | 215 |
} else { |
216 |
clientMacKey = null; |
|
217 |
serverMacKey = null; |
|
218 |
} |
|
219 |
||
2 | 220 |
SecretKey clientCipherKey, serverCipherKey; |
221 |
if (keyBits != 0) { |
|
222 |
clientCipherKey = P11Key.secretKey(session, out.hClientKey, |
|
223 |
cipherAlgorithm, expandedKeyBits, attributes); |
|
224 |
serverCipherKey = P11Key.secretKey(session, out.hServerKey, |
|
225 |
cipherAlgorithm, expandedKeyBits, attributes); |
|
226 |
} else { |
|
227 |
clientCipherKey = null; |
|
228 |
serverCipherKey = null; |
|
229 |
} |
|
230 |
IvParameterSpec clientIv = (out.pIVClient == null) |
|
231 |
? null : new IvParameterSpec(out.pIVClient); |
|
232 |
IvParameterSpec serverIv = (out.pIVServer == null) |
|
233 |
? null : new IvParameterSpec(out.pIVServer); |
|
234 |
||
235 |
return new TlsKeyMaterialSpec(clientMacKey, serverMacKey, |
|
236 |
clientCipherKey, clientIv, serverCipherKey, serverIv); |
|
237 |
||
238 |
} catch (Exception e) { |
|
239 |
throw new ProviderException("Could not generate key", e); |
|
240 |
} finally { |
|
241 |
token.releaseSession(session); |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
} |