author | xuelei |
Mon, 25 Jun 2018 13:41:39 -0700 | |
changeset 50768 | 68fa3d4026ea |
parent 47216 | 71c04702a3d5 |
child 53064 | 103ed9569fc8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
50768 | 2 |
* Copyright (c) 2003, 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.ssl; |
|
27 |
||
50768 | 28 |
import java.io.IOException; |
29 |
import java.nio.ByteBuffer; |
|
30 |
import java.security.GeneralSecurityException; |
|
31 |
import java.security.PrivateKey; |
|
32 |
import java.security.PublicKey; |
|
33 |
import java.text.MessageFormat; |
|
34 |
import java.util.Locale; |
|
35 |
import javax.crypto.SecretKey; |
|
36 |
import sun.security.ssl.RSAKeyExchange.EphemeralRSACredentials; |
|
37 |
import sun.security.ssl.RSAKeyExchange.EphemeralRSAPossession; |
|
38 |
import sun.security.ssl.RSAKeyExchange.RSAPremasterSecret; |
|
39 |
import sun.security.ssl.SSLHandshake.HandshakeMessage; |
|
40 |
import sun.security.ssl.X509Authentication.X509Credentials; |
|
41 |
import sun.security.ssl.X509Authentication.X509Possession; |
|
42 |
import sun.security.util.HexDumpEncoder; |
|
2 | 43 |
|
44 |
/** |
|
50768 | 45 |
* Pack of the "ClientKeyExchange" handshake message. |
2 | 46 |
*/ |
50768 | 47 |
final class RSAClientKeyExchange { |
48 |
static final SSLConsumer rsaHandshakeConsumer = |
|
49 |
new RSAClientKeyExchangeConsumer(); |
|
50 |
static final HandshakeProducer rsaHandshakeProducer = |
|
51 |
new RSAClientKeyExchangeProducer(); |
|
2 | 52 |
|
50768 | 53 |
/** |
54 |
* The RSA ClientKeyExchange handshake message. |
|
2 | 55 |
*/ |
50768 | 56 |
private static final |
57 |
class RSAClientKeyExchangeMessage extends HandshakeMessage { |
|
58 |
final int protocolVersion; |
|
59 |
final boolean useTLS10PlusSpec; |
|
60 |
final byte[] encrypted; |
|
61 |
||
62 |
RSAClientKeyExchangeMessage(HandshakeContext context, |
|
63 |
RSAPremasterSecret premaster, |
|
64 |
PublicKey publicKey) throws GeneralSecurityException { |
|
65 |
super(context); |
|
66 |
this.protocolVersion = context.clientHelloVersion; |
|
67 |
this.encrypted = premaster.getEncoded( |
|
68 |
publicKey, context.sslContext.getSecureRandom()); |
|
69 |
this.useTLS10PlusSpec = ProtocolVersion.useTLS10PlusSpec( |
|
70 |
protocolVersion, context.sslContext.isDTLS()); |
|
2 | 71 |
} |
72 |
||
50768 | 73 |
RSAClientKeyExchangeMessage(HandshakeContext context, |
74 |
ByteBuffer m) throws IOException { |
|
75 |
super(context); |
|
76 |
||
77 |
if (m.remaining() < 2) { |
|
78 |
context.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
|
79 |
"Invalid RSA ClientKeyExchange message: insufficient data"); |
|
80 |
} |
|
2 | 81 |
|
50768 | 82 |
this.protocolVersion = context.clientHelloVersion; |
83 |
this.useTLS10PlusSpec = ProtocolVersion.useTLS10PlusSpec( |
|
84 |
protocolVersion, context.sslContext.isDTLS()); |
|
85 |
if (useTLS10PlusSpec) { |
|
86 |
this.encrypted = Record.getBytes16(m); |
|
87 |
} else { // SSL 3.0 |
|
88 |
this.encrypted = new byte[m.remaining()]; |
|
89 |
m.get(encrypted); |
|
38741
b29b75c25015
8158111: Make handling of 3rd party providers more stable
igerasim
parents:
36952
diff
changeset
|
90 |
} |
b29b75c25015
8158111: Make handling of 3rd party providers more stable
igerasim
parents:
36952
diff
changeset
|
91 |
} |
50768 | 92 |
|
93 |
@Override |
|
94 |
public SSLHandshake handshakeType() { |
|
95 |
return SSLHandshake.CLIENT_KEY_EXCHANGE; |
|
96 |
} |
|
97 |
||
98 |
@Override |
|
99 |
public int messageLength() { |
|
100 |
if (useTLS10PlusSpec) { |
|
101 |
return encrypted.length + 2; |
|
102 |
} else { |
|
103 |
return encrypted.length; |
|
38741
b29b75c25015
8158111: Make handling of 3rd party providers more stable
igerasim
parents:
36952
diff
changeset
|
104 |
} |
b29b75c25015
8158111: Make handling of 3rd party providers more stable
igerasim
parents:
36952
diff
changeset
|
105 |
} |
b29b75c25015
8158111: Make handling of 3rd party providers more stable
igerasim
parents:
36952
diff
changeset
|
106 |
|
50768 | 107 |
@Override |
108 |
public void send(HandshakeOutStream hos) throws IOException { |
|
109 |
if (useTLS10PlusSpec) { |
|
110 |
hos.putBytes16(encrypted); |
|
111 |
} else { |
|
112 |
hos.write(encrypted); |
|
2 | 113 |
} |
114 |
} |
|
115 |
||
50768 | 116 |
@Override |
117 |
public String toString() { |
|
118 |
MessageFormat messageFormat = new MessageFormat( |
|
119 |
"\"RSA ClientKeyExchange\": '{'\n" + |
|
120 |
" \"client_version\": {0}\n" + |
|
121 |
" \"encncrypted\": '{'\n" + |
|
122 |
"{1}\n" + |
|
123 |
" '}'\n" + |
|
124 |
"'}'", |
|
125 |
Locale.ENGLISH); |
|
38741
b29b75c25015
8158111: Make handling of 3rd party providers more stable
igerasim
parents:
36952
diff
changeset
|
126 |
|
50768 | 127 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
128 |
Object[] messageFields = { |
|
129 |
ProtocolVersion.nameOf(protocolVersion), |
|
130 |
Utilities.indent( |
|
131 |
hexEncoder.encodeBuffer(encrypted), " "), |
|
132 |
}; |
|
133 |
return messageFormat.format(messageFields); |
|
22309 | 134 |
} |
2 | 135 |
} |
136 |
||
50768 | 137 |
/** |
138 |
* The RSA "ClientKeyExchange" handshake message producer. |
|
139 |
*/ |
|
140 |
private static final |
|
141 |
class RSAClientKeyExchangeProducer implements HandshakeProducer { |
|
142 |
// Prevent instantiation of this class. |
|
143 |
private RSAClientKeyExchangeProducer() { |
|
144 |
// blank |
|
35279 | 145 |
} |
146 |
||
50768 | 147 |
@Override |
148 |
public byte[] produce(ConnectionContext context, |
|
149 |
HandshakeMessage message) throws IOException { |
|
150 |
// This happens in client side only. |
|
151 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
152 |
||
153 |
EphemeralRSACredentials rsaCredentials = null; |
|
154 |
X509Credentials x509Credentials = null; |
|
155 |
for (SSLCredentials credential : chc.handshakeCredentials) { |
|
156 |
if (credential instanceof EphemeralRSACredentials) { |
|
157 |
rsaCredentials = (EphemeralRSACredentials)credential; |
|
158 |
if (x509Credentials != null) { |
|
159 |
break; |
|
160 |
} |
|
161 |
} else if (credential instanceof X509Credentials) { |
|
162 |
x509Credentials = (X509Credentials)credential; |
|
163 |
if (rsaCredentials != null) { |
|
164 |
break; |
|
165 |
} |
|
166 |
} |
|
167 |
} |
|
168 |
||
169 |
if (rsaCredentials == null && x509Credentials == null) { |
|
170 |
chc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
171 |
"No RSA credentials negotiated for client key exchange"); |
|
172 |
} |
|
173 |
||
174 |
PublicKey publicKey = (rsaCredentials != null) ? |
|
175 |
rsaCredentials.popPublicKey : x509Credentials.popPublicKey; |
|
176 |
if (!publicKey.getAlgorithm().equals("RSA")) { // unlikely |
|
177 |
chc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
178 |
"Not RSA public key for client key exchange"); |
|
35279 | 179 |
} |
50768 | 180 |
|
181 |
RSAPremasterSecret premaster; |
|
182 |
RSAClientKeyExchangeMessage ckem; |
|
183 |
try { |
|
184 |
premaster = RSAPremasterSecret.createPremasterSecret(chc); |
|
185 |
chc.handshakePossessions.add(premaster); |
|
186 |
ckem = new RSAClientKeyExchangeMessage( |
|
187 |
chc, premaster, publicKey); |
|
188 |
} catch (GeneralSecurityException gse) { |
|
189 |
chc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
190 |
"Cannot generate RSA premaster secret", gse); |
|
191 |
||
192 |
return null; // make the compiler happy |
|
193 |
} |
|
194 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
195 |
SSLLogger.fine( |
|
196 |
"Produced RSA ClientKeyExchange handshake message", ckem); |
|
197 |
} |
|
198 |
||
199 |
// Output the handshake message. |
|
200 |
ckem.write(chc.handshakeOutput); |
|
201 |
chc.handshakeOutput.flush(); |
|
202 |
||
203 |
// update the states |
|
204 |
SSLKeyExchange ke = SSLKeyExchange.valueOf( |
|
205 |
chc.negotiatedCipherSuite.keyExchange, |
|
206 |
chc.negotiatedProtocol); |
|
207 |
if (ke == null) { // unlikely |
|
208 |
chc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
209 |
"Not supported key exchange type"); |
|
210 |
} else { |
|
211 |
SSLKeyDerivation masterKD = ke.createKeyDerivation(chc); |
|
212 |
SecretKey masterSecret = |
|
213 |
masterKD.deriveKey("MasterSecret", null); |
|
214 |
||
215 |
// update the states |
|
216 |
chc.handshakeSession.setMasterSecret(masterSecret); |
|
217 |
SSLTrafficKeyDerivation kd = |
|
218 |
SSLTrafficKeyDerivation.valueOf(chc.negotiatedProtocol); |
|
219 |
if (kd == null) { // unlikely |
|
220 |
chc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
221 |
"Not supported key derivation: " + |
|
222 |
chc.negotiatedProtocol); |
|
223 |
} else { |
|
224 |
chc.handshakeKeyDerivation = |
|
225 |
kd.createKeyDerivation(chc, masterSecret); |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
// The handshake message has been delivered. |
|
230 |
return null; |
|
35279 | 231 |
} |
232 |
} |
|
233 |
||
50768 | 234 |
/** |
235 |
* The RSA "ClientKeyExchange" handshake message consumer. |
|
236 |
*/ |
|
237 |
private static final |
|
238 |
class RSAClientKeyExchangeConsumer implements SSLConsumer { |
|
239 |
// Prevent instantiation of this class. |
|
240 |
private RSAClientKeyExchangeConsumer() { |
|
241 |
// blank |
|
242 |
} |
|
243 |
||
244 |
@Override |
|
245 |
public void consume(ConnectionContext context, |
|
246 |
ByteBuffer message) throws IOException { |
|
247 |
// The consuming happens in server side only. |
|
248 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
249 |
||
250 |
EphemeralRSAPossession rsaPossession = null; |
|
251 |
X509Possession x509Possession = null; |
|
252 |
for (SSLPossession possession : shc.handshakePossessions) { |
|
253 |
if (possession instanceof EphemeralRSAPossession) { |
|
254 |
rsaPossession = (EphemeralRSAPossession)possession; |
|
255 |
break; |
|
256 |
} else if (possession instanceof X509Possession) { |
|
257 |
x509Possession = (X509Possession)possession; |
|
258 |
if (rsaPossession != null) { |
|
259 |
break; |
|
260 |
} |
|
261 |
} |
|
262 |
} |
|
263 |
||
264 |
if (rsaPossession == null && x509Possession == null) { // unlikely |
|
265 |
shc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
266 |
"No RSA possessions negotiated for client key exchange"); |
|
267 |
} |
|
268 |
||
269 |
PrivateKey privateKey = (rsaPossession != null) ? |
|
270 |
rsaPossession.popPrivateKey : x509Possession.popPrivateKey; |
|
271 |
if (!privateKey.getAlgorithm().equals("RSA")) { // unlikely |
|
272 |
shc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
273 |
"Not RSA private key for client key exchange"); |
|
274 |
} |
|
7039 | 275 |
|
50768 | 276 |
RSAClientKeyExchangeMessage ckem = |
277 |
new RSAClientKeyExchangeMessage(shc, message); |
|
278 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
279 |
SSLLogger.fine( |
|
280 |
"Consuming RSA ClientKeyExchange handshake message", ckem); |
|
281 |
} |
|
282 |
||
283 |
// create the credentials |
|
284 |
RSAPremasterSecret premaster; |
|
285 |
try { |
|
286 |
premaster = |
|
287 |
RSAPremasterSecret.decode(shc, privateKey, ckem.encrypted); |
|
288 |
shc.handshakeCredentials.add(premaster); |
|
289 |
} catch (GeneralSecurityException gse) { |
|
290 |
shc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
291 |
"Cannot decode RSA premaster secret", gse); |
|
292 |
} |
|
293 |
||
294 |
// update the states |
|
295 |
SSLKeyExchange ke = SSLKeyExchange.valueOf( |
|
296 |
shc.negotiatedCipherSuite.keyExchange, |
|
297 |
shc.negotiatedProtocol); |
|
298 |
if (ke == null) { // unlikely |
|
299 |
shc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
300 |
"Not supported key exchange type"); |
|
301 |
} else { |
|
302 |
SSLKeyDerivation masterKD = ke.createKeyDerivation(shc); |
|
303 |
SecretKey masterSecret = |
|
304 |
masterKD.deriveKey("MasterSecret", null); |
|
305 |
||
306 |
// update the states |
|
307 |
shc.handshakeSession.setMasterSecret(masterSecret); |
|
308 |
SSLTrafficKeyDerivation kd = |
|
309 |
SSLTrafficKeyDerivation.valueOf(shc.negotiatedProtocol); |
|
310 |
if (kd == null) { // unlikely |
|
311 |
shc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
312 |
"Not supported key derivation: " + |
|
313 |
shc.negotiatedProtocol); |
|
314 |
} else { |
|
315 |
shc.handshakeKeyDerivation = |
|
316 |
kd.createKeyDerivation(shc, masterSecret); |
|
317 |
} |
|
318 |
} |
|
2 | 319 |
} |
320 |
} |
|
321 |
} |