author | mbalao |
Tue, 15 Jan 2019 19:24:07 -0300 | |
changeset 53340 | 142b179dd60e |
parent 53257 | 5170dc2bcf64 |
child 59024 | b046ba510bbc |
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.security.*; |
|
29 |
import java.security.spec.AlgorithmParameterSpec; |
|
30 |
||
31 |
import javax.crypto.*; |
|
32 |
import javax.crypto.spec.*; |
|
33 |
||
34 |
import sun.security.internal.spec.TlsPrfParameterSpec; |
|
35 |
||
36 |
import static sun.security.pkcs11.TemplateManager.*; |
|
37 |
import sun.security.pkcs11.wrapper.*; |
|
38 |
import static sun.security.pkcs11.wrapper.PKCS11Constants.*; |
|
39 |
||
40 |
/** |
|
41 |
* KeyGenerator for the TLS PRF. Note that although the PRF is used in a number |
|
42 |
* of places during the handshake, this class is usually only used to calculate |
|
43 |
* the Finished messages. The reason is that for those other uses more specific |
|
44 |
* PKCS#11 mechanisms have been defined (CKM_SSL3_MASTER_KEY_DERIVE, etc.). |
|
45 |
* |
|
46 |
* <p>This class supports the CKM_TLS_PRF mechanism from PKCS#11 v2.20 and |
|
47 |
* the older NSS private mechanism. |
|
48 |
* |
|
49 |
* @author Andreas Sterbenz |
|
50 |
* @since 1.6 |
|
51 |
*/ |
|
52 |
final class P11TlsPrfGenerator extends KeyGeneratorSpi { |
|
53 |
||
54 |
private final static String MSG = |
|
55 |
"TlsPrfGenerator must be initialized using a TlsPrfParameterSpec"; |
|
56 |
||
57 |
// token instance |
|
58 |
private final Token token; |
|
59 |
||
60 |
// algorithm name |
|
61 |
private final String algorithm; |
|
62 |
||
63 |
// mechanism id |
|
64 |
private final long mechanism; |
|
65 |
||
27936
ca9ee8e3d527
8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents:
25859
diff
changeset
|
66 |
@SuppressWarnings("deprecation") |
2 | 67 |
private TlsPrfParameterSpec spec; |
68 |
||
69 |
private P11Key p11Key; |
|
70 |
||
71 |
P11TlsPrfGenerator(Token token, String algorithm, long mechanism) |
|
72 |
throws PKCS11Exception { |
|
73 |
super(); |
|
74 |
this.token = token; |
|
75 |
this.algorithm = algorithm; |
|
76 |
this.mechanism = mechanism; |
|
77 |
} |
|
78 |
||
79 |
protected void engineInit(SecureRandom random) { |
|
80 |
throw new InvalidParameterException(MSG); |
|
81 |
} |
|
82 |
||
27936
ca9ee8e3d527
8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents:
25859
diff
changeset
|
83 |
@SuppressWarnings("deprecation") |
2 | 84 |
protected void engineInit(AlgorithmParameterSpec params, |
85 |
SecureRandom random) throws InvalidAlgorithmParameterException { |
|
86 |
if (params instanceof TlsPrfParameterSpec == false) { |
|
87 |
throw new InvalidAlgorithmParameterException(MSG); |
|
88 |
} |
|
89 |
this.spec = (TlsPrfParameterSpec)params; |
|
90 |
SecretKey key = spec.getSecret(); |
|
91 |
if (key == null) { |
|
92 |
key = NULL_KEY; |
|
93 |
} |
|
94 |
try { |
|
95 |
p11Key = P11SecretKeyFactory.convertKey(token, key, null); |
|
96 |
} catch (InvalidKeyException e) { |
|
97 |
throw new InvalidAlgorithmParameterException("init() failed", e); |
|
98 |
} |
|
99 |
} |
|
100 |
||
19186
2b66b27d9ea9
7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents:
10336
diff
changeset
|
101 |
// SecretKeySpec does not allow zero length keys, so we define our |
2b66b27d9ea9
7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents:
10336
diff
changeset
|
102 |
// own class. |
2b66b27d9ea9
7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents:
10336
diff
changeset
|
103 |
// |
2b66b27d9ea9
7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents:
10336
diff
changeset
|
104 |
// As an anonymous class cannot make any guarantees about serialization |
2b66b27d9ea9
7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents:
10336
diff
changeset
|
105 |
// compatibility, it is nonsensical for an anonymous class to define a |
2b66b27d9ea9
7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents:
10336
diff
changeset
|
106 |
// serialVersionUID. Suppress warnings relative to missing serialVersionUID |
2b66b27d9ea9
7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents:
10336
diff
changeset
|
107 |
// field in the anonymous subclass of serializable SecretKey. |
2b66b27d9ea9
7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents:
10336
diff
changeset
|
108 |
@SuppressWarnings("serial") |
2 | 109 |
private static final SecretKey NULL_KEY = new SecretKey() { |
110 |
public byte[] getEncoded() { |
|
111 |
return new byte[0]; |
|
112 |
} |
|
113 |
public String getFormat() { |
|
114 |
return "RAW"; |
|
115 |
} |
|
116 |
public String getAlgorithm() { |
|
117 |
return "Generic"; |
|
118 |
} |
|
119 |
}; |
|
120 |
||
121 |
protected void engineInit(int keysize, SecureRandom random) { |
|
122 |
throw new InvalidParameterException(MSG); |
|
123 |
} |
|
124 |
||
125 |
protected SecretKey engineGenerateKey() { |
|
126 |
if (spec == null) { |
|
127 |
throw new IllegalStateException("TlsPrfGenerator must be initialized"); |
|
128 |
} |
|
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
129 |
|
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
130 |
byte[] seed = spec.getSeed(); |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
131 |
|
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
132 |
// TLS 1.2 |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
133 |
if (mechanism == CKM_TLS_MAC) { |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
134 |
SecretKey k = null; |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
135 |
int ulServerOrClient = 0; |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
136 |
if (spec.getLabel().equals("server finished")) { |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
137 |
ulServerOrClient = 1; |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
138 |
} |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
139 |
if (spec.getLabel().equals("client finished")) { |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
140 |
ulServerOrClient = 2; |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
141 |
} |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
142 |
|
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
143 |
if (ulServerOrClient != 0) { |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
144 |
// Finished message |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
145 |
CK_TLS_MAC_PARAMS params = new CK_TLS_MAC_PARAMS( |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
146 |
Functions.getHashMechId(spec.getPRFHashAlg()), |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
147 |
spec.getOutputLength(), ulServerOrClient); |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
148 |
Session session = null; |
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
51800
diff
changeset
|
149 |
long keyID = p11Key.getKeyID(); |
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
150 |
try { |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
151 |
session = token.getOpSession(); |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
152 |
token.p11.C_SignInit(session.id(), |
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
51800
diff
changeset
|
153 |
new CK_MECHANISM(mechanism, params), keyID); |
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
154 |
token.p11.C_SignUpdate(session.id(), 0, seed, 0, seed.length); |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
155 |
byte[] out = token.p11.C_SignFinal |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
156 |
(session.id(), spec.getOutputLength()); |
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
51800
diff
changeset
|
157 |
return new SecretKeySpec(out, "TlsPrf"); |
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
158 |
} catch (PKCS11Exception e) { |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
159 |
throw new ProviderException("Could not calculate PRF", e); |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
160 |
} finally { |
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
51800
diff
changeset
|
161 |
p11Key.releaseKeyID(); |
51800
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
162 |
token.releaseSession(session); |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
163 |
} |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
164 |
} else { |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
165 |
throw new ProviderException("Only Finished message authentication code"+ |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
166 |
" generation supported for TLS 1.2."); |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
167 |
} |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
168 |
} |
bccd9966f1ed
8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents:
47216
diff
changeset
|
169 |
|
2 | 170 |
byte[] label = P11Util.getBytesUTF8(spec.getLabel()); |
171 |
||
172 |
if (mechanism == CKM_NSS_TLS_PRF_GENERAL) { |
|
173 |
Session session = null; |
|
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
51800
diff
changeset
|
174 |
long keyID = p11Key.getKeyID(); |
2 | 175 |
try { |
176 |
session = token.getOpSession(); |
|
177 |
token.p11.C_SignInit |
|
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
51800
diff
changeset
|
178 |
(session.id(), new CK_MECHANISM(mechanism), keyID); |
2 | 179 |
token.p11.C_SignUpdate(session.id(), 0, label, 0, label.length); |
180 |
token.p11.C_SignUpdate(session.id(), 0, seed, 0, seed.length); |
|
181 |
byte[] out = token.p11.C_SignFinal |
|
182 |
(session.id(), spec.getOutputLength()); |
|
183 |
return new SecretKeySpec(out, "TlsPrf"); |
|
184 |
} catch (PKCS11Exception e) { |
|
185 |
throw new ProviderException("Could not calculate PRF", e); |
|
186 |
} finally { |
|
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
51800
diff
changeset
|
187 |
p11Key.releaseKeyID(); |
2 | 188 |
token.releaseSession(session); |
189 |
} |
|
190 |
} |
|
191 |
||
192 |
// mechanism == CKM_TLS_PRF |
|
193 |
||
194 |
byte[] out = new byte[spec.getOutputLength()]; |
|
195 |
CK_TLS_PRF_PARAMS params = new CK_TLS_PRF_PARAMS(seed, label, out); |
|
196 |
||
197 |
Session session = null; |
|
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
51800
diff
changeset
|
198 |
long keyID = p11Key.getKeyID(); |
2 | 199 |
try { |
200 |
session = token.getOpSession(); |
|
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
51800
diff
changeset
|
201 |
token.p11.C_DeriveKey(session.id(), |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
51800
diff
changeset
|
202 |
new CK_MECHANISM(mechanism, params), keyID, null); |
2 | 203 |
return new SecretKeySpec(out, "TlsPrf"); |
204 |
} catch (PKCS11Exception e) { |
|
205 |
throw new ProviderException("Could not calculate PRF", e); |
|
206 |
} finally { |
|
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
51800
diff
changeset
|
207 |
p11Key.releaseKeyID(); |
2 | 208 |
token.releaseSession(session); |
209 |
} |
|
210 |
} |
|
211 |
||
212 |
} |