2
|
1 |
/*
|
|
2 |
* Copyright 2005-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
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
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
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 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
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.interfaces.TlsMasterSecret;
|
|
35 |
import sun.security.internal.spec.*;
|
|
36 |
|
|
37 |
import static com.sun.crypto.provider.TlsPrfGenerator.*;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* KeyGenerator implementation for the SSL/TLS master secret derivation.
|
|
41 |
*
|
|
42 |
* @author Andreas Sterbenz
|
|
43 |
* @since 1.6
|
|
44 |
*/
|
|
45 |
public final class TlsKeyMaterialGenerator extends KeyGeneratorSpi {
|
|
46 |
|
|
47 |
private final static String MSG = "TlsKeyMaterialGenerator must be "
|
|
48 |
+ "initialized using a TlsKeyMaterialParameterSpec";
|
|
49 |
|
|
50 |
private TlsKeyMaterialParameterSpec spec;
|
|
51 |
|
|
52 |
private int protocolVersion;
|
|
53 |
|
|
54 |
public TlsKeyMaterialGenerator() {
|
|
55 |
SunJCE.ensureIntegrity(getClass());
|
|
56 |
}
|
|
57 |
|
|
58 |
protected void engineInit(SecureRandom random) {
|
|
59 |
throw new InvalidParameterException(MSG);
|
|
60 |
}
|
|
61 |
|
|
62 |
protected void engineInit(AlgorithmParameterSpec params,
|
|
63 |
SecureRandom random) throws InvalidAlgorithmParameterException {
|
|
64 |
if (params instanceof TlsKeyMaterialParameterSpec == false) {
|
|
65 |
throw new InvalidAlgorithmParameterException(MSG);
|
|
66 |
}
|
|
67 |
this.spec = (TlsKeyMaterialParameterSpec)params;
|
|
68 |
if ("RAW".equals(spec.getMasterSecret().getFormat()) == false) {
|
|
69 |
throw new InvalidAlgorithmParameterException("Key format must be RAW");
|
|
70 |
}
|
|
71 |
protocolVersion = (spec.getMajorVersion() << 8) | spec.getMinorVersion();
|
|
72 |
if ((protocolVersion < 0x0300) || (protocolVersion > 0x0302)) {
|
|
73 |
throw new InvalidAlgorithmParameterException
|
|
74 |
("Only SSL 3.0, TLS 1.0, and TLS 1.1 supported");
|
|
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) {
|
|
84 |
throw new IllegalStateException
|
|
85 |
("TlsKeyMaterialGenerator must be initialized");
|
|
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;
|
|
103 |
IvParameterSpec clientIv = null;
|
|
104 |
SecretKey serverCipherKey = 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 |
|
|
113 |
int keyBlockLen = macLength + keyLength + (isExportable ? 0 : ivLength);
|
|
114 |
keyBlockLen <<= 1;
|
|
115 |
byte[] keyBlock = new byte[keyBlockLen];
|
|
116 |
|
|
117 |
MessageDigest md5 = MessageDigest.getInstance("MD5");
|
|
118 |
MessageDigest sha = MessageDigest.getInstance("SHA1");
|
|
119 |
|
|
120 |
// generate key block
|
|
121 |
if (protocolVersion >= 0x0301) {
|
|
122 |
// TLS
|
|
123 |
byte[] seed = concat(serverRandom, clientRandom);
|
|
124 |
keyBlock = doPRF(masterSecret, LABEL_KEY_EXPANSION, seed,
|
|
125 |
keyBlockLen, md5, sha);
|
|
126 |
} else {
|
|
127 |
// SSL
|
|
128 |
keyBlock = new byte[keyBlockLen];
|
|
129 |
|
|
130 |
byte[] tmp = new byte[20];
|
|
131 |
for (int i = 0, remaining = keyBlockLen;
|
|
132 |
remaining > 0;
|
|
133 |
i++, remaining -= 16) {
|
|
134 |
|
|
135 |
sha.update(SSL3_CONST[i]);
|
|
136 |
sha.update(masterSecret);
|
|
137 |
sha.update(serverRandom);
|
|
138 |
sha.update(clientRandom);
|
|
139 |
sha.digest(tmp, 0, 20);
|
|
140 |
|
|
141 |
md5.update(masterSecret);
|
|
142 |
md5.update(tmp);
|
|
143 |
|
|
144 |
if (remaining >= 16) {
|
|
145 |
md5.digest(keyBlock, i << 4, 16);
|
|
146 |
} else {
|
|
147 |
md5.digest(tmp, 0, 16);
|
|
148 |
System.arraycopy(tmp, 0, keyBlock, i << 4, remaining);
|
|
149 |
}
|
|
150 |
}
|
|
151 |
}
|
|
152 |
|
|
153 |
// partition keyblock into individual secrets
|
|
154 |
|
|
155 |
int ofs = 0;
|
|
156 |
byte[] tmp = new byte[macLength];
|
|
157 |
|
|
158 |
// mac keys
|
|
159 |
System.arraycopy(keyBlock, ofs, tmp, 0, macLength);
|
|
160 |
ofs += macLength;
|
|
161 |
clientMacKey = new SecretKeySpec(tmp, "Mac");
|
|
162 |
|
|
163 |
System.arraycopy(keyBlock, ofs, tmp, 0, macLength);
|
|
164 |
ofs += macLength;
|
|
165 |
serverMacKey = new SecretKeySpec(tmp, "Mac");
|
|
166 |
|
|
167 |
if (keyLength == 0) { // SSL_RSA_WITH_NULL_* ciphersuites
|
|
168 |
return new TlsKeyMaterialSpec(clientMacKey, serverMacKey);
|
|
169 |
}
|
|
170 |
|
|
171 |
String alg = spec.getCipherAlgorithm();
|
|
172 |
|
|
173 |
byte[] clientKeyBytes = new byte[keyLength];
|
|
174 |
System.arraycopy(keyBlock, ofs, clientKeyBytes, 0, keyLength);
|
|
175 |
ofs += keyLength;
|
|
176 |
|
|
177 |
byte[] serverKeyBytes = new byte[keyLength];
|
|
178 |
System.arraycopy(keyBlock, ofs, serverKeyBytes, 0, keyLength);
|
|
179 |
ofs += keyLength;
|
|
180 |
|
|
181 |
if (isExportable == false) {
|
|
182 |
// cipher keys
|
|
183 |
clientCipherKey = new SecretKeySpec(clientKeyBytes, alg);
|
|
184 |
serverCipherKey = new SecretKeySpec(serverKeyBytes, alg);
|
|
185 |
|
|
186 |
if (ivLength != 0) {
|
|
187 |
tmp = new byte[ivLength];
|
|
188 |
|
|
189 |
System.arraycopy(keyBlock, ofs, tmp, 0, ivLength);
|
|
190 |
ofs += ivLength;
|
|
191 |
clientIv = new IvParameterSpec(tmp);
|
|
192 |
|
|
193 |
System.arraycopy(keyBlock, ofs, tmp, 0, ivLength);
|
|
194 |
ofs += ivLength;
|
|
195 |
serverIv = new IvParameterSpec(tmp);
|
|
196 |
}
|
|
197 |
} else {
|
|
198 |
// cipher key expansion and IV generation
|
|
199 |
if (protocolVersion >= 0x0301) {
|
|
200 |
byte[] seed = concat(clientRandom, serverRandom);
|
|
201 |
|
|
202 |
tmp = doPRF(clientKeyBytes, LABEL_CLIENT_WRITE_KEY, seed,
|
|
203 |
expandedKeyLength, md5, sha);
|
|
204 |
clientCipherKey = new SecretKeySpec(tmp, alg);
|
|
205 |
|
|
206 |
tmp = doPRF(serverKeyBytes, LABEL_SERVER_WRITE_KEY, seed,
|
|
207 |
expandedKeyLength, md5, sha);
|
|
208 |
serverCipherKey = new SecretKeySpec(tmp, alg);
|
|
209 |
|
|
210 |
if (ivLength != 0) {
|
|
211 |
tmp = new byte[ivLength];
|
|
212 |
byte[] block = doPRF(null, LABEL_IV_BLOCK, seed,
|
|
213 |
ivLength << 1, md5, sha);
|
|
214 |
System.arraycopy(block, 0, tmp, 0, ivLength);
|
|
215 |
clientIv = new IvParameterSpec(tmp);
|
|
216 |
System.arraycopy(block, ivLength, tmp, 0, ivLength);
|
|
217 |
serverIv = new IvParameterSpec(tmp);
|
|
218 |
}
|
|
219 |
} else {
|
|
220 |
tmp = new byte[expandedKeyLength];
|
|
221 |
|
|
222 |
md5.update(clientKeyBytes);
|
|
223 |
md5.update(clientRandom);
|
|
224 |
md5.update(serverRandom);
|
|
225 |
System.arraycopy(md5.digest(), 0, tmp, 0, expandedKeyLength);
|
|
226 |
clientCipherKey = new SecretKeySpec(tmp, alg);
|
|
227 |
|
|
228 |
md5.update(serverKeyBytes);
|
|
229 |
md5.update(serverRandom);
|
|
230 |
md5.update(clientRandom);
|
|
231 |
System.arraycopy(md5.digest(), 0, tmp, 0, expandedKeyLength);
|
|
232 |
serverCipherKey = new SecretKeySpec(tmp, alg);
|
|
233 |
|
|
234 |
if (ivLength != 0) {
|
|
235 |
tmp = new byte[ivLength];
|
|
236 |
|
|
237 |
md5.update(clientRandom);
|
|
238 |
md5.update(serverRandom);
|
|
239 |
System.arraycopy(md5.digest(), 0, tmp, 0, ivLength);
|
|
240 |
clientIv = new IvParameterSpec(tmp);
|
|
241 |
|
|
242 |
md5.update(serverRandom);
|
|
243 |
md5.update(clientRandom);
|
|
244 |
System.arraycopy(md5.digest(), 0, tmp, 0, ivLength);
|
|
245 |
serverIv = new IvParameterSpec(tmp);
|
|
246 |
}
|
|
247 |
}
|
|
248 |
}
|
|
249 |
|
|
250 |
return new TlsKeyMaterialSpec(clientMacKey, serverMacKey,
|
|
251 |
clientCipherKey, clientIv, serverCipherKey, serverIv);
|
|
252 |
}
|
|
253 |
|
|
254 |
}
|