author | weijun |
Thu, 07 Jun 2018 23:19:25 +0800 | |
branch | JDK-8145252-TLS13-branch |
changeset 56692 | 7b0bde908f58 |
parent 48225 | 718669e6b375 |
child 57950 | 4612a3cfb927 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48225
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2005, 2017, 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 |
||
33 |
import sun.security.internal.interfaces.TlsMasterSecret; |
|
34 |
import sun.security.internal.spec.TlsMasterSecretParameterSpec; |
|
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 TlsMasterSecretGenerator extends KeyGeneratorSpi { |
|
45 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
27804
diff
changeset
|
46 |
private static final String MSG = "TlsMasterSecretGenerator must be " |
2 | 47 |
+ "initialized using a TlsMasterSecretParameterSpec"; |
48 |
||
27804
4659e70271c4
8066617: Suppress deprecation warnings in java.base module
darcy
parents:
25859
diff
changeset
|
49 |
@SuppressWarnings("deprecation") |
2 | 50 |
private TlsMasterSecretParameterSpec spec; |
51 |
||
52 |
private int protocolVersion; |
|
53 |
||
54 |
public TlsMasterSecretGenerator() { |
|
55 |
} |
|
56 |
||
57 |
protected void engineInit(SecureRandom random) { |
|
58 |
throw new InvalidParameterException(MSG); |
|
59 |
} |
|
60 |
||
27804
4659e70271c4
8066617: Suppress deprecation warnings in java.base module
darcy
parents:
25859
diff
changeset
|
61 |
@SuppressWarnings("deprecation") |
2 | 62 |
protected void engineInit(AlgorithmParameterSpec params, |
63 |
SecureRandom random) throws InvalidAlgorithmParameterException { |
|
64 |
if (params instanceof TlsMasterSecretParameterSpec == false) { |
|
65 |
throw new InvalidAlgorithmParameterException(MSG); |
|
66 |
} |
|
67 |
this.spec = (TlsMasterSecretParameterSpec)params; |
|
68 |
if ("RAW".equals(spec.getPremasterSecret().getFormat()) == false) { |
|
7043 | 69 |
throw new InvalidAlgorithmParameterException( |
70 |
"Key format must be RAW"); |
|
2 | 71 |
} |
7043 | 72 |
protocolVersion = (spec.getMajorVersion() << 8) |
73 |
| spec.getMinorVersion(); |
|
74 |
if ((protocolVersion < 0x0300) || (protocolVersion > 0x0303)) { |
|
75 |
throw new InvalidAlgorithmParameterException( |
|
76 |
"Only SSL 3.0, TLS 1.0/1.1/1.2 supported"); |
|
2 | 77 |
} |
78 |
} |
|
79 |
||
80 |
protected void engineInit(int keysize, SecureRandom random) { |
|
81 |
throw new InvalidParameterException(MSG); |
|
82 |
} |
|
83 |
||
84 |
protected SecretKey engineGenerateKey() { |
|
85 |
if (spec == null) { |
|
7043 | 86 |
throw new IllegalStateException( |
87 |
"TlsMasterSecretGenerator must be initialized"); |
|
2 | 88 |
} |
89 |
SecretKey premasterKey = spec.getPremasterSecret(); |
|
90 |
byte[] premaster = premasterKey.getEncoded(); |
|
91 |
||
92 |
int premasterMajor, premasterMinor; |
|
93 |
if (premasterKey.getAlgorithm().equals("TlsRsaPremasterSecret")) { |
|
94 |
// RSA |
|
95 |
premasterMajor = premaster[0] & 0xff; |
|
96 |
premasterMinor = premaster[1] & 0xff; |
|
97 |
} else { |
|
56692
7b0bde908f58
remove more references of KRB5 and move the existing cipher suites to unsupported
weijun
parents:
48225
diff
changeset
|
98 |
// DH, others |
2 | 99 |
premasterMajor = -1; |
100 |
premasterMinor = -1; |
|
101 |
} |
|
102 |
||
103 |
try { |
|
104 |
byte[] master; |
|
105 |
if (protocolVersion >= 0x0301) { |
|
48225
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
106 |
byte[] label; |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
107 |
byte[] seed; |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
108 |
byte[] extendedMasterSecretSessionHash = |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
109 |
spec.getExtendedMasterSecretSessionHash(); |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
110 |
if (extendedMasterSecretSessionHash.length != 0) { |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
111 |
label = LABEL_EXTENDED_MASTER_SECRET; |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
112 |
seed = extendedMasterSecretSessionHash; |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
113 |
} else { |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
114 |
byte[] clientRandom = spec.getClientRandom(); |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
115 |
byte[] serverRandom = spec.getServerRandom(); |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
116 |
label = LABEL_MASTER_SECRET; |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
117 |
seed = concat(clientRandom, serverRandom); |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
118 |
} |
7043 | 119 |
master = ((protocolVersion >= 0x0303) ? |
48225
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
120 |
doTLS12PRF(premaster, label, seed, 48, |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
121 |
spec.getPRFHashAlg(), spec.getPRFHashLength(), |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
122 |
spec.getPRFBlockSize()) : |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
123 |
doTLS10PRF(premaster, label, seed, 48)); |
2 | 124 |
} else { |
125 |
master = new byte[48]; |
|
126 |
MessageDigest md5 = MessageDigest.getInstance("MD5"); |
|
127 |
MessageDigest sha = MessageDigest.getInstance("SHA"); |
|
128 |
||
48225
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
129 |
byte[] clientRandom = spec.getClientRandom(); |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
130 |
byte[] serverRandom = spec.getServerRandom(); |
2 | 131 |
byte[] tmp = new byte[20]; |
132 |
for (int i = 0; i < 3; i++) { |
|
133 |
sha.update(SSL3_CONST[i]); |
|
134 |
sha.update(premaster); |
|
135 |
sha.update(clientRandom); |
|
136 |
sha.update(serverRandom); |
|
137 |
sha.digest(tmp, 0, 20); |
|
138 |
||
139 |
md5.update(premaster); |
|
140 |
md5.update(tmp); |
|
141 |
md5.digest(master, i << 4, 16); |
|
142 |
} |
|
143 |
||
144 |
} |
|
145 |
||
7043 | 146 |
return new TlsMasterSecretKey(master, premasterMajor, |
147 |
premasterMinor); |
|
2 | 148 |
} catch (NoSuchAlgorithmException e) { |
149 |
throw new ProviderException(e); |
|
150 |
} catch (DigestException e) { |
|
151 |
throw new ProviderException(e); |
|
152 |
} |
|
153 |
} |
|
154 |
||
27804
4659e70271c4
8066617: Suppress deprecation warnings in java.base module
darcy
parents:
25859
diff
changeset
|
155 |
@SuppressWarnings("deprecation") |
4659e70271c4
8066617: Suppress deprecation warnings in java.base module
darcy
parents:
25859
diff
changeset
|
156 |
private static final class TlsMasterSecretKey implements TlsMasterSecret { |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7043
diff
changeset
|
157 |
private static final long serialVersionUID = 1019571680375368880L; |
2 | 158 |
|
159 |
private byte[] key; |
|
160 |
private final int majorVersion, minorVersion; |
|
161 |
||
162 |
TlsMasterSecretKey(byte[] key, int majorVersion, int minorVersion) { |
|
163 |
this.key = key; |
|
164 |
this.majorVersion = majorVersion; |
|
165 |
this.minorVersion = minorVersion; |
|
166 |
} |
|
167 |
||
168 |
public int getMajorVersion() { |
|
169 |
return majorVersion; |
|
170 |
} |
|
171 |
||
172 |
public int getMinorVersion() { |
|
173 |
return minorVersion; |
|
174 |
} |
|
175 |
||
176 |
public String getAlgorithm() { |
|
177 |
return "TlsMasterSecret"; |
|
178 |
} |
|
179 |
||
180 |
public String getFormat() { |
|
181 |
return "RAW"; |
|
182 |
} |
|
183 |
||
184 |
public byte[] getEncoded() { |
|
185 |
return key.clone(); |
|
186 |
} |
|
187 |
||
188 |
} |
|
48225
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
189 |
} |
2 | 190 |