author | valeriep |
Tue, 08 May 2012 17:57:48 -0700 | |
changeset 12685 | 8a448b5b9006 |
parent 10336 | 0bb1999251f8 |
child 16045 | 9d08c3b9a6a0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7043
diff
changeset
|
2 |
* Copyright (c) 2005, 2011, 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 com.sun.crypto.provider; |
|
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.*; |
|
35 |
||
36 |
import static com.sun.crypto.provider.TlsPrfGenerator.*; |
|
37 |
||
38 |
/** |
|
39 |
* KeyGenerator implementation for the SSL/TLS master secret derivation. |
|
40 |
* |
|
41 |
* @author Andreas Sterbenz |
|
42 |
* @since 1.6 |
|
43 |
*/ |
|
44 |
public final class TlsKeyMaterialGenerator extends KeyGeneratorSpi { |
|
45 |
||
46 |
private final static String MSG = "TlsKeyMaterialGenerator must be " |
|
47 |
+ "initialized using a TlsKeyMaterialParameterSpec"; |
|
48 |
||
49 |
private TlsKeyMaterialParameterSpec spec; |
|
50 |
||
51 |
private int protocolVersion; |
|
52 |
||
53 |
public TlsKeyMaterialGenerator() { |
|
54 |
} |
|
55 |
||
56 |
protected void engineInit(SecureRandom random) { |
|
57 |
throw new InvalidParameterException(MSG); |
|
58 |
} |
|
59 |
||
60 |
protected void engineInit(AlgorithmParameterSpec params, |
|
61 |
SecureRandom random) throws InvalidAlgorithmParameterException { |
|
62 |
if (params instanceof TlsKeyMaterialParameterSpec == false) { |
|
63 |
throw new InvalidAlgorithmParameterException(MSG); |
|
64 |
} |
|
65 |
this.spec = (TlsKeyMaterialParameterSpec)params; |
|
66 |
if ("RAW".equals(spec.getMasterSecret().getFormat()) == false) { |
|
7043 | 67 |
throw new InvalidAlgorithmParameterException( |
68 |
"Key format must be RAW"); |
|
2 | 69 |
} |
7043 | 70 |
protocolVersion = (spec.getMajorVersion() << 8) |
71 |
| spec.getMinorVersion(); |
|
72 |
if ((protocolVersion < 0x0300) || (protocolVersion > 0x0303)) { |
|
73 |
throw new InvalidAlgorithmParameterException( |
|
74 |
"Only SSL 3.0, TLS 1.0/1.1/1.2 supported"); |
|
2 | 75 |
} |
76 |
} |
|
77 |
||
78 |
protected void engineInit(int keysize, SecureRandom random) { |
|
79 |
throw new InvalidParameterException(MSG); |
|
80 |
} |
|
81 |
||
82 |
protected SecretKey engineGenerateKey() { |
|
83 |
if (spec == null) { |
|
7043 | 84 |
throw new IllegalStateException( |
85 |
"TlsKeyMaterialGenerator must be initialized"); |
|
2 | 86 |
} |
87 |
try { |
|
88 |
return engineGenerateKey0(); |
|
89 |
} catch (GeneralSecurityException e) { |
|
90 |
throw new ProviderException(e); |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
private SecretKey engineGenerateKey0() throws GeneralSecurityException { |
|
95 |
byte[] masterSecret = spec.getMasterSecret().getEncoded(); |
|
96 |
||
97 |
byte[] clientRandom = spec.getClientRandom(); |
|
98 |
byte[] serverRandom = spec.getServerRandom(); |
|
99 |
||
100 |
SecretKey clientMacKey = null; |
|
101 |
SecretKey serverMacKey = null; |
|
102 |
SecretKey clientCipherKey = null; |
|
7043 | 103 |
SecretKey serverCipherKey = null; |
2 | 104 |
IvParameterSpec clientIv = null; |
105 |
IvParameterSpec serverIv = null; |
|
106 |
||
107 |
int macLength = spec.getMacKeyLength(); |
|
108 |
int expandedKeyLength = spec.getExpandedCipherKeyLength(); |
|
109 |
boolean isExportable = (expandedKeyLength != 0); |
|
110 |
int keyLength = spec.getCipherKeyLength(); |
|
111 |
int ivLength = spec.getIvLength(); |
|
112 |
||
7043 | 113 |
int keyBlockLen = macLength + keyLength |
114 |
+ (isExportable ? 0 : ivLength); |
|
2 | 115 |
keyBlockLen <<= 1; |
116 |
byte[] keyBlock = new byte[keyBlockLen]; |
|
117 |
||
7043 | 118 |
// These may be used again later for exportable suite calculations. |
119 |
MessageDigest md5 = null; |
|
120 |
MessageDigest sha = null; |
|
2 | 121 |
|
122 |
// generate key block |
|
7043 | 123 |
if (protocolVersion >= 0x0303) { |
124 |
// TLS 1.2 |
|
2 | 125 |
byte[] seed = concat(serverRandom, clientRandom); |
7043 | 126 |
keyBlock = doTLS12PRF(masterSecret, LABEL_KEY_EXPANSION, seed, |
127 |
keyBlockLen, spec.getPRFHashAlg(), |
|
128 |
spec.getPRFHashLength(), spec.getPRFBlockSize()); |
|
129 |
} else if (protocolVersion >= 0x0301) { |
|
130 |
// TLS 1.0/1.1 |
|
131 |
md5 = MessageDigest.getInstance("MD5"); |
|
132 |
sha = MessageDigest.getInstance("SHA1"); |
|
133 |
byte[] seed = concat(serverRandom, clientRandom); |
|
134 |
keyBlock = doTLS10PRF(masterSecret, LABEL_KEY_EXPANSION, seed, |
|
2 | 135 |
keyBlockLen, md5, sha); |
136 |
} else { |
|
137 |
// SSL |
|
7043 | 138 |
md5 = MessageDigest.getInstance("MD5"); |
139 |
sha = MessageDigest.getInstance("SHA1"); |
|
2 | 140 |
keyBlock = new byte[keyBlockLen]; |
141 |
||
142 |
byte[] tmp = new byte[20]; |
|
143 |
for (int i = 0, remaining = keyBlockLen; |
|
144 |
remaining > 0; |
|
145 |
i++, remaining -= 16) { |
|
146 |
||
147 |
sha.update(SSL3_CONST[i]); |
|
148 |
sha.update(masterSecret); |
|
149 |
sha.update(serverRandom); |
|
150 |
sha.update(clientRandom); |
|
151 |
sha.digest(tmp, 0, 20); |
|
152 |
||
153 |
md5.update(masterSecret); |
|
154 |
md5.update(tmp); |
|
155 |
||
156 |
if (remaining >= 16) { |
|
157 |
md5.digest(keyBlock, i << 4, 16); |
|
158 |
} else { |
|
159 |
md5.digest(tmp, 0, 16); |
|
160 |
System.arraycopy(tmp, 0, keyBlock, i << 4, remaining); |
|
161 |
} |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
// partition keyblock into individual secrets |
|
166 |
||
167 |
int ofs = 0; |
|
168 |
byte[] tmp = new byte[macLength]; |
|
169 |
||
170 |
// mac keys |
|
171 |
System.arraycopy(keyBlock, ofs, tmp, 0, macLength); |
|
172 |
ofs += macLength; |
|
173 |
clientMacKey = new SecretKeySpec(tmp, "Mac"); |
|
174 |
||
175 |
System.arraycopy(keyBlock, ofs, tmp, 0, macLength); |
|
176 |
ofs += macLength; |
|
177 |
serverMacKey = new SecretKeySpec(tmp, "Mac"); |
|
178 |
||
179 |
if (keyLength == 0) { // SSL_RSA_WITH_NULL_* ciphersuites |
|
180 |
return new TlsKeyMaterialSpec(clientMacKey, serverMacKey); |
|
181 |
} |
|
182 |
||
183 |
String alg = spec.getCipherAlgorithm(); |
|
184 |
||
7043 | 185 |
// cipher keys |
2 | 186 |
byte[] clientKeyBytes = new byte[keyLength]; |
187 |
System.arraycopy(keyBlock, ofs, clientKeyBytes, 0, keyLength); |
|
188 |
ofs += keyLength; |
|
189 |
||
190 |
byte[] serverKeyBytes = new byte[keyLength]; |
|
191 |
System.arraycopy(keyBlock, ofs, serverKeyBytes, 0, keyLength); |
|
192 |
ofs += keyLength; |
|
193 |
||
194 |
if (isExportable == false) { |
|
195 |
// cipher keys |
|
196 |
clientCipherKey = new SecretKeySpec(clientKeyBytes, alg); |
|
197 |
serverCipherKey = new SecretKeySpec(serverKeyBytes, alg); |
|
198 |
||
7043 | 199 |
// IV keys if needed. |
2 | 200 |
if (ivLength != 0) { |
201 |
tmp = new byte[ivLength]; |
|
202 |
||
203 |
System.arraycopy(keyBlock, ofs, tmp, 0, ivLength); |
|
204 |
ofs += ivLength; |
|
205 |
clientIv = new IvParameterSpec(tmp); |
|
206 |
||
207 |
System.arraycopy(keyBlock, ofs, tmp, 0, ivLength); |
|
208 |
ofs += ivLength; |
|
209 |
serverIv = new IvParameterSpec(tmp); |
|
210 |
} |
|
211 |
} else { |
|
7043 | 212 |
// if exportable suites, calculate the alternate |
2 | 213 |
// cipher key expansion and IV generation |
7043 | 214 |
if (protocolVersion >= 0x0302) { |
215 |
// TLS 1.1+ |
|
216 |
throw new RuntimeException( |
|
217 |
"Internal Error: TLS 1.1+ should not be negotiating" + |
|
218 |
"exportable ciphersuites"); |
|
219 |
} else if (protocolVersion == 0x0301) { |
|
220 |
// TLS 1.0 |
|
2 | 221 |
byte[] seed = concat(clientRandom, serverRandom); |
222 |
||
7043 | 223 |
tmp = doTLS10PRF(clientKeyBytes, LABEL_CLIENT_WRITE_KEY, seed, |
2 | 224 |
expandedKeyLength, md5, sha); |
225 |
clientCipherKey = new SecretKeySpec(tmp, alg); |
|
226 |
||
7043 | 227 |
tmp = doTLS10PRF(serverKeyBytes, LABEL_SERVER_WRITE_KEY, seed, |
2 | 228 |
expandedKeyLength, md5, sha); |
229 |
serverCipherKey = new SecretKeySpec(tmp, alg); |
|
230 |
||
231 |
if (ivLength != 0) { |
|
232 |
tmp = new byte[ivLength]; |
|
7043 | 233 |
byte[] block = doTLS10PRF(null, LABEL_IV_BLOCK, seed, |
2 | 234 |
ivLength << 1, md5, sha); |
235 |
System.arraycopy(block, 0, tmp, 0, ivLength); |
|
236 |
clientIv = new IvParameterSpec(tmp); |
|
237 |
System.arraycopy(block, ivLength, tmp, 0, ivLength); |
|
238 |
serverIv = new IvParameterSpec(tmp); |
|
239 |
} |
|
240 |
} else { |
|
7043 | 241 |
// SSLv3 |
2 | 242 |
tmp = new byte[expandedKeyLength]; |
243 |
||
244 |
md5.update(clientKeyBytes); |
|
245 |
md5.update(clientRandom); |
|
246 |
md5.update(serverRandom); |
|
247 |
System.arraycopy(md5.digest(), 0, tmp, 0, expandedKeyLength); |
|
248 |
clientCipherKey = new SecretKeySpec(tmp, alg); |
|
249 |
||
250 |
md5.update(serverKeyBytes); |
|
251 |
md5.update(serverRandom); |
|
252 |
md5.update(clientRandom); |
|
253 |
System.arraycopy(md5.digest(), 0, tmp, 0, expandedKeyLength); |
|
254 |
serverCipherKey = new SecretKeySpec(tmp, alg); |
|
255 |
||
256 |
if (ivLength != 0) { |
|
257 |
tmp = new byte[ivLength]; |
|
258 |
||
259 |
md5.update(clientRandom); |
|
260 |
md5.update(serverRandom); |
|
261 |
System.arraycopy(md5.digest(), 0, tmp, 0, ivLength); |
|
262 |
clientIv = new IvParameterSpec(tmp); |
|
263 |
||
264 |
md5.update(serverRandom); |
|
265 |
md5.update(clientRandom); |
|
266 |
System.arraycopy(md5.digest(), 0, tmp, 0, ivLength); |
|
267 |
serverIv = new IvParameterSpec(tmp); |
|
268 |
} |
|
269 |
} |
|
270 |
} |
|
271 |
||
272 |
return new TlsKeyMaterialSpec(clientMacKey, serverMacKey, |
|
273 |
clientCipherKey, clientIv, serverCipherKey, serverIv); |
|
274 |
} |
|
275 |
||
276 |
} |