author | asmotrak |
Mon, 02 Mar 2015 12:56:22 -0800 | |
changeset 31689 | 1201792aa3a3 |
parent 31538 | 0981099a3e54 |
child 31695 | 4d10942c9a7b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
31689 | 2 |
* Copyright (c) 1996, 2015, 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 |
||
27 |
package sun.security.ssl; |
|
28 |
||
29 |
import java.io.*; |
|
30 |
import java.util.*; |
|
31 |
import java.security.*; |
|
32 |
import java.security.cert.*; |
|
33 |
import java.security.interfaces.*; |
|
34 |
import java.security.spec.ECParameterSpec; |
|
35 |
||
36 |
import javax.crypto.SecretKey; |
|
37 |
import javax.crypto.spec.SecretKeySpec; |
|
38 |
||
39 |
import javax.net.ssl.*; |
|
40 |
||
20539
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
41 |
import sun.security.util.KeyUtil; |
31689 | 42 |
import sun.security.util.LegacyAlgorithmConstraints; |
20539
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
43 |
import sun.security.action.GetPropertyAction; |
2 | 44 |
import sun.security.ssl.HandshakeMessage.*; |
45 |
import sun.security.ssl.CipherSuite.*; |
|
7043 | 46 |
import sun.security.ssl.SignatureAndHashAlgorithm.*; |
2 | 47 |
import static sun.security.ssl.CipherSuite.KeyExchange.*; |
48 |
||
49 |
/** |
|
50 |
* ServerHandshaker does the protocol handshaking from the point |
|
51 |
* of view of a server. It is driven asychronously by handshake messages |
|
52 |
* as delivered by the parent Handshaker class, and also uses |
|
53 |
* common functionality (e.g. key generation) that is provided there. |
|
54 |
* |
|
55 |
* @author David Brownell |
|
56 |
*/ |
|
57 |
final class ServerHandshaker extends Handshaker { |
|
58 |
||
59 |
// is the server going to require the client to authenticate? |
|
30904 | 60 |
private ClientAuthType doClientAuth; |
2 | 61 |
|
62 |
// our authentication info |
|
63 |
private X509Certificate[] certs; |
|
64 |
private PrivateKey privateKey; |
|
65 |
||
17189 | 66 |
private Object serviceCreds; |
2 | 67 |
|
68 |
// flag to check for clientCertificateVerify message |
|
69 |
private boolean needClientVerify = false; |
|
70 |
||
71 |
/* |
|
72 |
* For exportable ciphersuites using non-exportable key sizes, we use |
|
73 |
* ephemeral RSA keys. We could also do anonymous RSA in the same way |
|
74 |
* but there are no such ciphersuites currently defined. |
|
75 |
*/ |
|
76 |
private PrivateKey tempPrivateKey; |
|
77 |
private PublicKey tempPublicKey; |
|
78 |
||
79 |
/* |
|
80 |
* For anonymous and ephemeral Diffie-Hellman key exchange, we use |
|
81 |
* ephemeral Diffie-Hellman keys. |
|
82 |
*/ |
|
83 |
private DHCrypt dh; |
|
84 |
||
85 |
// Helper for ECDH based key exchanges |
|
86 |
private ECDHCrypt ecdh; |
|
87 |
||
88 |
// version request by the client in its ClientHello |
|
89 |
// we remember it for the RSA premaster secret version check |
|
90 |
private ProtocolVersion clientRequestedVersion; |
|
91 |
||
92 |
private SupportedEllipticCurvesExtension supportedCurves; |
|
93 |
||
7043 | 94 |
// the preferable signature algorithm used by ServerKeyExchange message |
95 |
SignatureAndHashAlgorithm preferableSignatureAlgorithm; |
|
96 |
||
20539
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
97 |
// Flag to use smart ephemeral DH key which size matches the corresponding |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
98 |
// authentication key |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
99 |
private static final boolean useSmartEphemeralDHKeys; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
100 |
|
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
101 |
// Flag to use legacy ephemeral DH key which size is 512 bits for |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
102 |
// exportable cipher suites, and 768 bits for others |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
103 |
private static final boolean useLegacyEphemeralDHKeys; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
104 |
|
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
105 |
// The customized ephemeral DH key size for non-exportable cipher suites. |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
106 |
private static final int customizedDHKeySize; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
107 |
|
31689 | 108 |
// legacy algorithm constraints |
109 |
private static final AlgorithmConstraints legacyAlgorithmConstraints = |
|
110 |
new LegacyAlgorithmConstraints( |
|
111 |
LegacyAlgorithmConstraints.PROPERTY_TLS_LEGACY_ALGS, |
|
112 |
new SSLAlgorithmDecomposer()); |
|
113 |
||
20539
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
114 |
static { |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
115 |
String property = AccessController.doPrivileged( |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
116 |
new GetPropertyAction("jdk.tls.ephemeralDHKeySize")); |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
117 |
if (property == null || property.length() == 0) { |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
118 |
useLegacyEphemeralDHKeys = false; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
119 |
useSmartEphemeralDHKeys = false; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
120 |
customizedDHKeySize = -1; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
121 |
} else if ("matched".equals(property)) { |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
122 |
useLegacyEphemeralDHKeys = false; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
123 |
useSmartEphemeralDHKeys = true; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
124 |
customizedDHKeySize = -1; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
125 |
} else if ("legacy".equals(property)) { |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
126 |
useLegacyEphemeralDHKeys = true; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
127 |
useSmartEphemeralDHKeys = false; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
128 |
customizedDHKeySize = -1; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
129 |
} else { |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
130 |
useLegacyEphemeralDHKeys = false; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
131 |
useSmartEphemeralDHKeys = false; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
132 |
|
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
133 |
try { |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
134 |
customizedDHKeySize = Integer.parseUnsignedInt(property); |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
135 |
if (customizedDHKeySize < 1024 || customizedDHKeySize > 2048) { |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
136 |
throw new IllegalArgumentException( |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
137 |
"Customized DH key size should be positive integer " + |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
138 |
"between 1024 and 2048 bits, inclusive"); |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
139 |
} |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
140 |
} catch (NumberFormatException nfe) { |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
141 |
throw new IllegalArgumentException( |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
142 |
"Invalid system property jdk.tls.ephemeralDHKeySize"); |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
143 |
} |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
144 |
} |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
145 |
} |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
146 |
|
2 | 147 |
/* |
148 |
* Constructor ... use the keys found in the auth context. |
|
149 |
*/ |
|
150 |
ServerHandshaker(SSLSocketImpl socket, SSLContextImpl context, |
|
30904 | 151 |
ProtocolList enabledProtocols, ClientAuthType clientAuth, |
6856 | 152 |
ProtocolVersion activeProtocolVersion, boolean isInitialHandshake, |
153 |
boolean secureRenegotiation, |
|
154 |
byte[] clientVerifyData, byte[] serverVerifyData) { |
|
5182 | 155 |
|
2 | 156 |
super(socket, context, enabledProtocols, |
30904 | 157 |
(clientAuth != ClientAuthType.CLIENT_AUTH_NONE), false, |
6856 | 158 |
activeProtocolVersion, isInitialHandshake, secureRenegotiation, |
159 |
clientVerifyData, serverVerifyData); |
|
2 | 160 |
doClientAuth = clientAuth; |
161 |
} |
|
162 |
||
163 |
/* |
|
164 |
* Constructor ... use the keys found in the auth context. |
|
165 |
*/ |
|
166 |
ServerHandshaker(SSLEngineImpl engine, SSLContextImpl context, |
|
30904 | 167 |
ProtocolList enabledProtocols, ClientAuthType clientAuth, |
6856 | 168 |
ProtocolVersion activeProtocolVersion, |
169 |
boolean isInitialHandshake, boolean secureRenegotiation, |
|
30904 | 170 |
byte[] clientVerifyData, byte[] serverVerifyData, |
171 |
boolean isDTLS) { |
|
5182 | 172 |
|
2 | 173 |
super(engine, context, enabledProtocols, |
30904 | 174 |
(clientAuth != ClientAuthType.CLIENT_AUTH_NONE), false, |
6856 | 175 |
activeProtocolVersion, isInitialHandshake, secureRenegotiation, |
30904 | 176 |
clientVerifyData, serverVerifyData, isDTLS); |
2 | 177 |
doClientAuth = clientAuth; |
178 |
} |
|
179 |
||
180 |
/* |
|
181 |
* As long as handshaking has not started, we can change |
|
182 |
* whether client authentication is required. Otherwise, |
|
183 |
* we will need to wait for the next handshake. |
|
184 |
*/ |
|
30904 | 185 |
void setClientAuth(ClientAuthType clientAuth) { |
2 | 186 |
doClientAuth = clientAuth; |
187 |
} |
|
188 |
||
189 |
/* |
|
190 |
* This routine handles all the server side handshake messages, one at |
|
191 |
* a time. Given the message type (and in some cases the pending cipher |
|
192 |
* spec) it parses the type-specific message. Then it calls a function |
|
193 |
* that handles that specific message. |
|
194 |
* |
|
195 |
* It updates the state machine as each message is processed, and writes |
|
196 |
* responses as needed using the connection in the constructor. |
|
197 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
198 |
@Override |
2 | 199 |
void processMessage(byte type, int message_len) |
200 |
throws IOException { |
|
30904 | 201 |
|
202 |
// check the handshake state |
|
203 |
handshakeState.check(type); |
|
2 | 204 |
|
205 |
switch (type) { |
|
206 |
case HandshakeMessage.ht_client_hello: |
|
30904 | 207 |
ClientHello ch = new ClientHello(input, message_len, isDTLS); |
208 |
handshakeState.update(ch, resumingSession); |
|
209 |
||
2 | 210 |
/* |
211 |
* send it off for processing. |
|
212 |
*/ |
|
213 |
this.clientHello(ch); |
|
214 |
break; |
|
215 |
||
216 |
case HandshakeMessage.ht_certificate: |
|
30904 | 217 |
if (doClientAuth == ClientAuthType.CLIENT_AUTH_NONE) { |
2 | 218 |
fatalSE(Alerts.alert_unexpected_message, |
219 |
"client sent unsolicited cert chain"); |
|
220 |
// NOTREACHED |
|
221 |
} |
|
30904 | 222 |
CertificateMsg certificateMsg = new CertificateMsg(input); |
223 |
handshakeState.update(certificateMsg, resumingSession); |
|
224 |
this.clientCertificate(certificateMsg); |
|
2 | 225 |
break; |
226 |
||
227 |
case HandshakeMessage.ht_client_key_exchange: |
|
228 |
SecretKey preMasterSecret; |
|
229 |
switch (keyExchange) { |
|
230 |
case K_RSA: |
|
231 |
case K_RSA_EXPORT: |
|
232 |
/* |
|
233 |
* The client's pre-master secret is decrypted using |
|
234 |
* either the server's normal private RSA key, or the |
|
235 |
* temporary one used for non-export or signing-only |
|
236 |
* certificates/keys. |
|
237 |
*/ |
|
7039 | 238 |
RSAClientKeyExchange pms = new RSAClientKeyExchange( |
239 |
protocolVersion, clientRequestedVersion, |
|
240 |
sslContext.getSecureRandom(), input, |
|
241 |
message_len, privateKey); |
|
30904 | 242 |
handshakeState.update(pms, resumingSession); |
2 | 243 |
preMasterSecret = this.clientKeyExchange(pms); |
244 |
break; |
|
245 |
case K_DHE_RSA: |
|
246 |
case K_DHE_DSS: |
|
247 |
case K_DH_ANON: |
|
248 |
/* |
|
249 |
* The pre-master secret is derived using the normal |
|
250 |
* Diffie-Hellman calculation. Note that the main |
|
251 |
* protocol difference in these five flavors is in how |
|
252 |
* the ServerKeyExchange message was constructed! |
|
253 |
*/ |
|
30904 | 254 |
DHClientKeyExchange dhcke = new DHClientKeyExchange(input); |
255 |
handshakeState.update(dhcke, resumingSession); |
|
256 |
preMasterSecret = this.clientKeyExchange(dhcke); |
|
2 | 257 |
break; |
258 |
case K_ECDH_RSA: |
|
259 |
case K_ECDH_ECDSA: |
|
260 |
case K_ECDHE_RSA: |
|
261 |
case K_ECDHE_ECDSA: |
|
262 |
case K_ECDH_ANON: |
|
30904 | 263 |
ECDHClientKeyExchange ecdhcke = |
264 |
new ECDHClientKeyExchange(input); |
|
265 |
handshakeState.update(ecdhcke, resumingSession); |
|
266 |
preMasterSecret = this.clientKeyExchange(ecdhcke); |
|
2 | 267 |
break; |
268 |
default: |
|
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
269 |
ClientKeyExchangeService p = |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
270 |
ClientKeyExchangeService.find(keyExchange.name); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
271 |
if (p == null) { |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
272 |
throw new SSLProtocolException |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
273 |
("Unrecognized key exchange: " + keyExchange); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
274 |
} |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
275 |
byte[] encodedTicket = input.getBytes16(); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
276 |
input.getBytes16(); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
277 |
byte[] secret = input.getBytes16(); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
278 |
ClientKeyExchange cke = p.createServerExchange(protocolVersion, |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
279 |
clientRequestedVersion, |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
280 |
sslContext.getSecureRandom(), |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
281 |
encodedTicket, |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
282 |
secret, |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
283 |
this.getAccSE(), serviceCreds); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
284 |
handshakeState.update(cke, resumingSession); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
285 |
preMasterSecret = this.clientKeyExchange(cke); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
286 |
break; |
2 | 287 |
} |
288 |
||
289 |
// |
|
290 |
// All keys are calculated from the premaster secret |
|
291 |
// and the exchanged nonces in the same way. |
|
292 |
// |
|
293 |
calculateKeys(preMasterSecret, clientRequestedVersion); |
|
294 |
break; |
|
295 |
||
296 |
case HandshakeMessage.ht_certificate_verify: |
|
30904 | 297 |
CertificateVerify cvm = |
298 |
new CertificateVerify(input, |
|
299 |
localSupportedSignAlgs, protocolVersion); |
|
300 |
handshakeState.update(cvm, resumingSession); |
|
301 |
this.clientCertificateVerify(cvm); |
|
302 |
||
2 | 303 |
break; |
304 |
||
305 |
case HandshakeMessage.ht_finished: |
|
30904 | 306 |
Finished cfm = |
307 |
new Finished(protocolVersion, input, cipherSuite); |
|
308 |
handshakeState.update(cfm, resumingSession); |
|
309 |
this.clientFinished(cfm); |
|
28550 | 310 |
|
2 | 311 |
break; |
312 |
||
313 |
default: |
|
314 |
throw new SSLProtocolException( |
|
315 |
"Illegal server handshake msg, " + type); |
|
316 |
} |
|
317 |
||
318 |
} |
|
319 |
||
320 |
||
321 |
/* |
|
322 |
* ClientHello presents the server with a bunch of options, to which the |
|
323 |
* server replies with a ServerHello listing the ones which this session |
|
324 |
* will use. If needed, it also writes its Certificate plus in some cases |
|
325 |
* a ServerKeyExchange message. It may also write a CertificateRequest, |
|
326 |
* to elicit a client certificate. |
|
327 |
* |
|
328 |
* All these messages are terminated by a ServerHelloDone message. In |
|
329 |
* most cases, all this can be sent in a single Record. |
|
330 |
*/ |
|
331 |
private void clientHello(ClientHello mesg) throws IOException { |
|
332 |
if (debug != null && Debug.isOn("handshake")) { |
|
333 |
mesg.print(System.out); |
|
334 |
} |
|
5182 | 335 |
|
18283
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
17189
diff
changeset
|
336 |
// Reject client initiated renegotiation? |
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
17189
diff
changeset
|
337 |
// |
18570
9078dc28a187
8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents:
18283
diff
changeset
|
338 |
// If server side should reject client-initiated renegotiation, |
9078dc28a187
8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents:
18283
diff
changeset
|
339 |
// send an alert_handshake_failure fatal alert, not a no_renegotiation |
9078dc28a187
8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents:
18283
diff
changeset
|
340 |
// warning alert (no_renegotiation must be a warning: RFC 2246). |
9078dc28a187
8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents:
18283
diff
changeset
|
341 |
// no_renegotiation might seem more natural at first, but warnings |
9078dc28a187
8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents:
18283
diff
changeset
|
342 |
// are not appropriate because the sending party does not know how |
9078dc28a187
8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents:
18283
diff
changeset
|
343 |
// the receiving party will behave. This state must be treated as |
9078dc28a187
8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents:
18283
diff
changeset
|
344 |
// a fatal server condition. |
9078dc28a187
8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents:
18283
diff
changeset
|
345 |
// |
9078dc28a187
8019359: To comment why not use no_renegotiation to reject client initiated renegotiation
xuelei
parents:
18283
diff
changeset
|
346 |
// This will not have any impact on server initiated renegotiation. |
18283
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
17189
diff
changeset
|
347 |
if (rejectClientInitiatedRenego && !isInitialHandshake && |
30904 | 348 |
!serverHelloRequested) { |
18283
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
17189
diff
changeset
|
349 |
fatalSE(Alerts.alert_handshake_failure, |
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
17189
diff
changeset
|
350 |
"Client initiated renegotiation is not allowed"); |
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
17189
diff
changeset
|
351 |
} |
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
17189
diff
changeset
|
352 |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
353 |
// check the server name indication if required |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
354 |
ServerNameExtension clientHelloSNIExt = (ServerNameExtension) |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
355 |
mesg.extensions.get(ExtensionType.EXT_SERVER_NAME); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
356 |
if (!sniMatchers.isEmpty()) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
357 |
// we do not reject client without SNI extension |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
358 |
if (clientHelloSNIExt != null && |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
359 |
!clientHelloSNIExt.isMatched(sniMatchers)) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
360 |
fatalSE(Alerts.alert_unrecognized_name, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
361 |
"Unrecognized server name indication"); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
362 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
363 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
364 |
|
6856 | 365 |
// Does the message include security renegotiation indication? |
366 |
boolean renegotiationIndicated = false; |
|
5182 | 367 |
|
6856 | 368 |
// check the TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
369 |
CipherSuiteList cipherSuites = mesg.getCipherSuites(); |
|
370 |
if (cipherSuites.contains(CipherSuite.C_SCSV)) { |
|
371 |
renegotiationIndicated = true; |
|
372 |
if (isInitialHandshake) { |
|
373 |
secureRenegotiation = true; |
|
374 |
} else { |
|
375 |
// abort the handshake with a fatal handshake_failure alert |
|
376 |
if (secureRenegotiation) { |
|
377 |
fatalSE(Alerts.alert_handshake_failure, |
|
378 |
"The SCSV is present in a secure renegotiation"); |
|
379 |
} else { |
|
380 |
fatalSE(Alerts.alert_handshake_failure, |
|
381 |
"The SCSV is present in a insecure renegotiation"); |
|
382 |
} |
|
383 |
} |
|
384 |
} |
|
5182 | 385 |
|
6856 | 386 |
// check the "renegotiation_info" extension |
387 |
RenegotiationInfoExtension clientHelloRI = (RenegotiationInfoExtension) |
|
388 |
mesg.extensions.get(ExtensionType.EXT_RENEGOTIATION_INFO); |
|
389 |
if (clientHelloRI != null) { |
|
390 |
renegotiationIndicated = true; |
|
391 |
if (isInitialHandshake) { |
|
392 |
// verify the length of the "renegotiated_connection" field |
|
393 |
if (!clientHelloRI.isEmpty()) { |
|
394 |
// abort the handshake with a fatal handshake_failure alert |
|
395 |
fatalSE(Alerts.alert_handshake_failure, |
|
396 |
"The renegotiation_info field is not empty"); |
|
397 |
} |
|
5182 | 398 |
|
6856 | 399 |
secureRenegotiation = true; |
400 |
} else { |
|
401 |
if (!secureRenegotiation) { |
|
402 |
// unexpected RI extension for insecure renegotiation, |
|
403 |
// abort the handshake with a fatal handshake_failure alert |
|
404 |
fatalSE(Alerts.alert_handshake_failure, |
|
405 |
"The renegotiation_info is present in a insecure " + |
|
406 |
"renegotiation"); |
|
5182 | 407 |
} |
408 |
||
6856 | 409 |
// verify the client_verify_data value |
410 |
if (!Arrays.equals(clientVerifyData, |
|
411 |
clientHelloRI.getRenegotiatedConnection())) { |
|
412 |
fatalSE(Alerts.alert_handshake_failure, |
|
413 |
"Incorrect verify data in ClientHello " + |
|
414 |
"renegotiation_info message"); |
|
415 |
} |
|
416 |
} |
|
417 |
} else if (!isInitialHandshake && secureRenegotiation) { |
|
418 |
// if the connection's "secure_renegotiation" flag is set to TRUE |
|
419 |
// and the "renegotiation_info" extension is not present, abort |
|
420 |
// the handshake. |
|
421 |
fatalSE(Alerts.alert_handshake_failure, |
|
422 |
"Inconsistent secure renegotiation indication"); |
|
423 |
} |
|
424 |
||
425 |
// if there is no security renegotiation indication or the previous |
|
426 |
// handshake is insecure. |
|
427 |
if (!renegotiationIndicated || !secureRenegotiation) { |
|
428 |
if (isInitialHandshake) { |
|
429 |
if (!allowLegacyHelloMessages) { |
|
430 |
// abort the handshake with a fatal handshake_failure alert |
|
431 |
fatalSE(Alerts.alert_handshake_failure, |
|
432 |
"Failed to negotiate the use of secure renegotiation"); |
|
433 |
} |
|
434 |
||
435 |
// continue with legacy ClientHello |
|
436 |
if (debug != null && Debug.isOn("handshake")) { |
|
437 |
System.out.println("Warning: No renegotiation " + |
|
438 |
"indication in ClientHello, allow legacy ClientHello"); |
|
439 |
} |
|
440 |
} else if (!allowUnsafeRenegotiation) { |
|
441 |
// abort the handshake |
|
30904 | 442 |
if (activeProtocolVersion.useTLS10PlusSpec()) { |
18283
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
17189
diff
changeset
|
443 |
// respond with a no_renegotiation warning |
6856 | 444 |
warningSE(Alerts.alert_no_renegotiation); |
445 |
||
446 |
// invalidate the handshake so that the caller can |
|
447 |
// dispose this object. |
|
448 |
invalidated = true; |
|
449 |
||
450 |
// If there is still unread block in the handshake |
|
451 |
// input stream, it would be truncated with the disposal |
|
452 |
// and the next handshake message will become incomplete. |
|
453 |
// |
|
454 |
// However, according to SSL/TLS specifications, no more |
|
455 |
// handshake message could immediately follow ClientHello |
|
456 |
// or HelloRequest. But in case of any improper messages, |
|
457 |
// we'd better check to ensure there is no remaining bytes |
|
458 |
// in the handshake input stream. |
|
459 |
if (input.available() > 0) { |
|
460 |
fatalSE(Alerts.alert_unexpected_message, |
|
461 |
"ClientHello followed by an unexpected " + |
|
462 |
"handshake message"); |
|
463 |
} |
|
464 |
||
465 |
return; |
|
466 |
} else { |
|
467 |
// For SSLv3, send the handshake_failure fatal error. |
|
468 |
// Note that SSLv3 does not define a no_renegotiation |
|
469 |
// alert like TLSv1. However we cannot ignore the message |
|
470 |
// simply, otherwise the other side was waiting for a |
|
471 |
// response that would never come. |
|
472 |
fatalSE(Alerts.alert_handshake_failure, |
|
473 |
"Renegotiation is not allowed"); |
|
474 |
} |
|
475 |
} else { // !isInitialHandshake && allowUnsafeRenegotiation |
|
476 |
// continue with unsafe renegotiation. |
|
477 |
if (debug != null && Debug.isOn("handshake")) { |
|
478 |
System.out.println( |
|
479 |
"Warning: continue with insecure renegotiation"); |
|
480 |
} |
|
5182 | 481 |
} |
482 |
} |
|
483 |
||
30904 | 484 |
// check the "max_fragment_length" extension |
485 |
MaxFragmentLengthExtension maxFragLenExt = (MaxFragmentLengthExtension) |
|
486 |
mesg.extensions.get(ExtensionType.EXT_MAX_FRAGMENT_LENGTH); |
|
487 |
if ((maxFragLenExt != null) && (maximumPacketSize != 0)) { |
|
488 |
// Not yet consider the impact of IV/MAC/padding. |
|
489 |
int estimatedMaxFragSize = maximumPacketSize; |
|
490 |
if (isDTLS) { |
|
491 |
estimatedMaxFragSize -= DTLSRecord.headerSize; |
|
492 |
} else { |
|
493 |
estimatedMaxFragSize -= SSLRecord.headerSize; |
|
494 |
} |
|
495 |
||
496 |
if (maxFragLenExt.getMaxFragLen() > estimatedMaxFragSize) { |
|
497 |
// For better interoperability, abort the maximum fragment |
|
498 |
// length negotiation, rather than terminate the connection |
|
499 |
// with a fatal alert. |
|
500 |
maxFragLenExt = null; |
|
501 |
||
502 |
// fatalSE(Alerts.alert_illegal_parameter, |
|
503 |
// "Not an allowed max_fragment_length value"); |
|
504 |
} |
|
505 |
} |
|
506 |
||
507 |
// cookie exchange |
|
508 |
if (isDTLS) { |
|
509 |
HelloCookieManager hcMgr = sslContext.getHelloCookieManager(); |
|
510 |
if ((mesg.cookie == null) || (mesg.cookie.length == 0) || |
|
511 |
(!hcMgr.isValid(mesg))) { |
|
512 |
||
513 |
// |
|
514 |
// Perform cookie exchange for DTLS handshaking if no cookie |
|
515 |
// or the cookie is invalid in the ClientHello message. |
|
516 |
// |
|
517 |
HelloVerifyRequest m0 = new HelloVerifyRequest(hcMgr, mesg); |
|
518 |
||
519 |
if (debug != null && Debug.isOn("handshake")) { |
|
520 |
m0.print(System.out); |
|
521 |
} |
|
522 |
||
523 |
m0.write(output); |
|
524 |
handshakeState.update(m0, resumingSession); |
|
525 |
output.flush(); |
|
526 |
||
527 |
return; |
|
528 |
} |
|
529 |
} |
|
2 | 530 |
|
531 |
/* |
|
532 |
* FIRST, construct the ServerHello using the options and priorities |
|
533 |
* from the ClientHello. Update the (pending) cipher spec as we do |
|
534 |
* so, and save the client's version to protect against rollback |
|
535 |
* attacks. |
|
536 |
* |
|
537 |
* There are a bunch of minor tasks here, and one major one: deciding |
|
538 |
* if the short or the full handshake sequence will be used. |
|
539 |
*/ |
|
540 |
ServerHello m1 = new ServerHello(); |
|
541 |
||
542 |
clientRequestedVersion = mesg.protocolVersion; |
|
543 |
||
7039 | 544 |
// select a proper protocol version. |
545 |
ProtocolVersion selectedVersion = |
|
546 |
selectProtocolVersion(clientRequestedVersion); |
|
547 |
if (selectedVersion == null || |
|
548 |
selectedVersion.v == ProtocolVersion.SSL20Hello.v) { |
|
2 | 549 |
fatalSE(Alerts.alert_handshake_failure, |
550 |
"Client requested protocol " + clientRequestedVersion + |
|
7039 | 551 |
" not enabled or not supported"); |
2 | 552 |
} |
7043 | 553 |
|
7804 | 554 |
handshakeHash.protocolDetermined(selectedVersion); |
2 | 555 |
setVersion(selectedVersion); |
556 |
||
557 |
m1.protocolVersion = protocolVersion; |
|
558 |
||
559 |
// |
|
560 |
// random ... save client and server values for later use |
|
561 |
// in computing the master secret (from pre-master secret) |
|
562 |
// and thence the other crypto keys. |
|
563 |
// |
|
564 |
// NOTE: this use of three inputs to generating _each_ set |
|
565 |
// of ciphers slows things down, but it does increase the |
|
566 |
// security since each connection in the session can hold |
|
567 |
// its own authenticated (and strong) keys. One could make |
|
568 |
// creation of a session a rare thing... |
|
569 |
// |
|
570 |
clnt_random = mesg.clnt_random; |
|
571 |
svr_random = new RandomCookie(sslContext.getSecureRandom()); |
|
572 |
m1.svr_random = svr_random; |
|
573 |
||
574 |
session = null; // forget about the current session |
|
575 |
// |
|
576 |
// Here we go down either of two paths: (a) the fast one, where |
|
577 |
// the client's asked to rejoin an existing session, and the server |
|
578 |
// permits this; (b) the other one, where a new session is created. |
|
579 |
// |
|
580 |
if (mesg.sessionId.length() != 0) { |
|
581 |
// client is trying to resume a session, let's see... |
|
582 |
||
583 |
SSLSessionImpl previous = ((SSLSessionContextImpl)sslContext |
|
584 |
.engineGetServerSessionContext()) |
|
585 |
.get(mesg.sessionId.getId()); |
|
586 |
// |
|
587 |
// Check if we can use the fast path, resuming a session. We |
|
588 |
// can do so iff we have a valid record for that session, and |
|
589 |
// the cipher suite for that session was on the list which the |
|
590 |
// client requested, and if we're not forgetting any needed |
|
591 |
// authentication on the part of the client. |
|
592 |
// |
|
593 |
if (previous != null) { |
|
594 |
resumingSession = previous.isRejoinable(); |
|
595 |
||
596 |
if (resumingSession) { |
|
597 |
ProtocolVersion oldVersion = previous.getProtocolVersion(); |
|
598 |
// cannot resume session with different version |
|
599 |
if (oldVersion != protocolVersion) { |
|
600 |
resumingSession = false; |
|
601 |
} |
|
602 |
} |
|
603 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
604 |
// cannot resume session with different server name indication |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
605 |
if (resumingSession) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
606 |
List<SNIServerName> oldServerNames = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
607 |
previous.getRequestedServerNames(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
608 |
if (clientHelloSNIExt != null) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
609 |
if (!clientHelloSNIExt.isIdentical(oldServerNames)) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
610 |
resumingSession = false; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
611 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
612 |
} else if (!oldServerNames.isEmpty()) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
613 |
resumingSession = false; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
614 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
615 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
616 |
if (!resumingSession && |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
617 |
debug != null && Debug.isOn("handshake")) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
618 |
System.out.println( |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
619 |
"The requested server name indication " + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
620 |
"is not identical to the previous one"); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
621 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
622 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
623 |
|
2 | 624 |
if (resumingSession && |
30904 | 625 |
(doClientAuth == ClientAuthType.CLIENT_AUTH_REQUIRED)) { |
2 | 626 |
try { |
627 |
previous.getPeerPrincipal(); |
|
628 |
} catch (SSLPeerUnverifiedException e) { |
|
629 |
resumingSession = false; |
|
630 |
} |
|
631 |
} |
|
632 |
||
633 |
// validate subject identity |
|
634 |
if (resumingSession) { |
|
635 |
CipherSuite suite = previous.getSuite(); |
|
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
636 |
ClientKeyExchangeService p = |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
637 |
ClientKeyExchangeService.find(suite.keyExchange.name); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
638 |
if (p != null) { |
2 | 639 |
Principal localPrincipal = previous.getLocalPrincipal(); |
640 |
||
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
641 |
if (p.isRelated( |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
642 |
false, getAccSE(), localPrincipal)) { |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
643 |
if (debug != null && Debug.isOn("session")) |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
644 |
System.out.println("Subject can" + |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
645 |
" provide creds for princ"); |
2 | 646 |
} else { |
647 |
resumingSession = false; |
|
648 |
if (debug != null && Debug.isOn("session")) |
|
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
649 |
System.out.println("Subject cannot" + |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
650 |
" provide creds for princ"); |
2 | 651 |
} |
652 |
} |
|
653 |
} |
|
654 |
||
655 |
if (resumingSession) { |
|
656 |
CipherSuite suite = previous.getSuite(); |
|
657 |
// verify that the ciphersuite from the cached session |
|
658 |
// is in the list of client requested ciphersuites and |
|
659 |
// we have it enabled |
|
6856 | 660 |
if ((isNegotiable(suite) == false) || |
2 | 661 |
(mesg.getCipherSuites().contains(suite) == false)) { |
662 |
resumingSession = false; |
|
663 |
} else { |
|
664 |
// everything looks ok, set the ciphersuite |
|
665 |
// this should be done last when we are sure we |
|
666 |
// will resume |
|
667 |
setCipherSuite(suite); |
|
668 |
} |
|
669 |
} |
|
670 |
||
671 |
if (resumingSession) { |
|
672 |
session = previous; |
|
673 |
if (debug != null && |
|
674 |
(Debug.isOn("handshake") || Debug.isOn("session"))) { |
|
675 |
System.out.println("%% Resuming " + session); |
|
676 |
} |
|
677 |
} |
|
678 |
} |
|
30904 | 679 |
} // else client did not try to resume |
2 | 680 |
|
681 |
// |
|
682 |
// If client hasn't specified a session we can resume, start a |
|
683 |
// new one and choose its cipher suite and compression options. |
|
684 |
// Unless new session creation is disabled for this connection! |
|
685 |
// |
|
686 |
if (session == null) { |
|
687 |
if (!enableNewSession) { |
|
688 |
throw new SSLException("Client did not resume a session"); |
|
689 |
} |
|
7043 | 690 |
|
6856 | 691 |
supportedCurves = (SupportedEllipticCurvesExtension) |
692 |
mesg.extensions.get(ExtensionType.EXT_ELLIPTIC_CURVES); |
|
7043 | 693 |
|
694 |
// We only need to handle the "signature_algorithm" extension |
|
695 |
// for full handshakes and TLS 1.2 or later. |
|
30904 | 696 |
if (protocolVersion.useTLS12PlusSpec()) { |
7043 | 697 |
SignatureAlgorithmsExtension signAlgs = |
698 |
(SignatureAlgorithmsExtension)mesg.extensions.get( |
|
699 |
ExtensionType.EXT_SIGNATURE_ALGORITHMS); |
|
700 |
if (signAlgs != null) { |
|
701 |
Collection<SignatureAndHashAlgorithm> peerSignAlgs = |
|
702 |
signAlgs.getSignAlgorithms(); |
|
703 |
if (peerSignAlgs == null || peerSignAlgs.isEmpty()) { |
|
704 |
throw new SSLHandshakeException( |
|
705 |
"No peer supported signature algorithms"); |
|
706 |
} |
|
707 |
||
708 |
Collection<SignatureAndHashAlgorithm> |
|
709 |
supportedPeerSignAlgs = |
|
710 |
SignatureAndHashAlgorithm.getSupportedAlgorithms( |
|
711 |
peerSignAlgs); |
|
712 |
if (supportedPeerSignAlgs.isEmpty()) { |
|
713 |
throw new SSLHandshakeException( |
|
714 |
"No supported signature and hash algorithm " + |
|
715 |
"in common"); |
|
716 |
} |
|
717 |
||
718 |
setPeerSupportedSignAlgs(supportedPeerSignAlgs); |
|
719 |
} // else, need to use peer implicit supported signature algs |
|
720 |
} |
|
721 |
||
722 |
session = new SSLSessionImpl(protocolVersion, CipherSuite.C_NULL, |
|
723 |
getLocalSupportedSignAlgs(), |
|
724 |
sslContext.getSecureRandom(), |
|
725 |
getHostAddressSE(), getPortSE()); |
|
726 |
||
30904 | 727 |
if (protocolVersion.useTLS12PlusSpec()) { |
7043 | 728 |
if (peerSupportedSignAlgs != null) { |
729 |
session.setPeerSupportedSignatureAlgorithms( |
|
730 |
peerSupportedSignAlgs); |
|
731 |
} // else, we will set the implicit peer supported signature |
|
732 |
// algorithms in chooseCipherSuite() |
|
733 |
} |
|
734 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
735 |
// set the server name indication in the session |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
736 |
List<SNIServerName> clientHelloSNI = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
737 |
Collections.<SNIServerName>emptyList(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
738 |
if (clientHelloSNIExt != null) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
739 |
clientHelloSNI = clientHelloSNIExt.getServerNames(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
740 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
741 |
session.setRequestedServerNames(clientHelloSNI); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
742 |
|
7043 | 743 |
// set the handshake session |
744 |
setHandshakeSessionSE(session); |
|
745 |
||
746 |
// choose cipher suite and corresponding private key |
|
2 | 747 |
chooseCipherSuite(mesg); |
7043 | 748 |
|
749 |
session.setSuite(cipherSuite); |
|
2 | 750 |
session.setLocalPrivateKey(privateKey); |
7043 | 751 |
|
2 | 752 |
// chooseCompression(mesg); |
30904 | 753 |
|
754 |
// set the negotiated maximum fragment in the session |
|
755 |
// |
|
756 |
// The protocol version and cipher suite have been negotiated |
|
757 |
// in previous processes. |
|
758 |
if (maxFragLenExt != null) { |
|
759 |
int maxFragLen = maxFragLenExt.getMaxFragLen(); |
|
760 |
||
761 |
// More check of the requested "max_fragment_length" extension. |
|
762 |
if (maximumPacketSize != 0) { |
|
763 |
int estimatedMaxFragSize = cipherSuite.calculatePacketSize( |
|
764 |
maxFragLen, protocolVersion, isDTLS); |
|
765 |
if (estimatedMaxFragSize > maximumPacketSize) { |
|
766 |
// For better interoperability, abort the maximum |
|
767 |
// fragment length negotiation, rather than terminate |
|
768 |
// the connection with a fatal alert. |
|
769 |
maxFragLenExt = null; |
|
770 |
||
771 |
// fatalSE(Alerts.alert_illegal_parameter, |
|
772 |
// "Not an allowed max_fragment_length value"); |
|
773 |
} |
|
774 |
} |
|
775 |
||
776 |
if (maxFragLenExt != null) { |
|
777 |
session.setNegotiatedMaxFragSize(maxFragLen); |
|
778 |
} |
|
779 |
} |
|
780 |
||
781 |
session.setMaximumPacketSize(maximumPacketSize); |
|
7043 | 782 |
} else { |
783 |
// set the handshake session |
|
784 |
setHandshakeSessionSE(session); |
|
785 |
} |
|
786 |
||
30904 | 787 |
if (protocolVersion.useTLS12PlusSpec()) { |
7043 | 788 |
handshakeHash.setFinishedAlg(cipherSuite.prfAlg.getPRFHashAlg()); |
2 | 789 |
} |
790 |
||
791 |
m1.cipherSuite = cipherSuite; |
|
792 |
m1.sessionId = session.getSessionId(); |
|
793 |
m1.compression_method = session.getCompression(); |
|
794 |
||
6856 | 795 |
if (secureRenegotiation) { |
796 |
// For ServerHellos that are initial handshakes, then the |
|
797 |
// "renegotiated_connection" field in "renegotiation_info" |
|
798 |
// extension is of zero length. |
|
799 |
// |
|
800 |
// For ServerHellos that are renegotiating, this field contains |
|
801 |
// the concatenation of client_verify_data and server_verify_data. |
|
802 |
// |
|
803 |
// Note that for initial handshakes, both the clientVerifyData |
|
804 |
// variable and serverVerifyData variable are of zero length. |
|
805 |
HelloExtension serverHelloRI = new RenegotiationInfoExtension( |
|
806 |
clientVerifyData, serverVerifyData); |
|
807 |
m1.extensions.add(serverHelloRI); |
|
808 |
} |
|
809 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
810 |
if (!sniMatchers.isEmpty() && clientHelloSNIExt != null) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
811 |
// When resuming a session, the server MUST NOT include a |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
812 |
// server_name extension in the server hello. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
813 |
if (!resumingSession) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
814 |
ServerNameExtension serverHelloSNI = new ServerNameExtension(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
815 |
m1.extensions.add(serverHelloSNI); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
816 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
817 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
12874
diff
changeset
|
818 |
|
30904 | 819 |
if ((maxFragLenExt != null) && !resumingSession) { |
820 |
// When resuming a session, the server MUST NOT include a |
|
821 |
// max_fragment_length extension in the server hello. |
|
822 |
// |
|
823 |
// Otherwise, use the same value as the requested extension. |
|
824 |
m1.extensions.add(maxFragLenExt); |
|
825 |
} |
|
826 |
||
2 | 827 |
if (debug != null && Debug.isOn("handshake")) { |
828 |
m1.print(System.out); |
|
829 |
System.out.println("Cipher suite: " + session.getSuite()); |
|
830 |
} |
|
831 |
m1.write(output); |
|
30904 | 832 |
handshakeState.update(m1, resumingSession); |
2 | 833 |
|
834 |
// |
|
835 |
// If we are resuming a session, we finish writing handshake |
|
836 |
// messages right now and then finish. |
|
837 |
// |
|
838 |
if (resumingSession) { |
|
839 |
calculateConnectionKeys(session.getMasterSecret()); |
|
840 |
sendChangeCipherAndFinish(false); |
|
30904 | 841 |
|
842 |
// expecting the final ChangeCipherSpec and Finished messages |
|
843 |
expectingFinishFlightSE(); |
|
844 |
||
2 | 845 |
return; |
846 |
} |
|
847 |
||
848 |
||
849 |
/* |
|
850 |
* SECOND, write the server Certificate(s) if we need to. |
|
851 |
* |
|
852 |
* NOTE: while an "anonymous RSA" mode is explicitly allowed by |
|
853 |
* the protocol, we can't support it since all of the SSL flavors |
|
854 |
* defined in the protocol spec are explicitly stated to require |
|
855 |
* using RSA certificates. |
|
856 |
*/ |
|
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
857 |
if (ClientKeyExchangeService.find(cipherSuite.keyExchange.name) != null) { |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
858 |
// No external key exchange provider needs a cert now. |
2 | 859 |
} else if ((keyExchange != K_DH_ANON) && (keyExchange != K_ECDH_ANON)) { |
860 |
if (certs == null) { |
|
861 |
throw new RuntimeException("no certificates"); |
|
862 |
} |
|
863 |
||
864 |
CertificateMsg m2 = new CertificateMsg(certs); |
|
865 |
||
866 |
/* |
|
867 |
* Set local certs in the SSLSession, output |
|
868 |
* debug info, and then actually write to the client. |
|
869 |
*/ |
|
870 |
session.setLocalCertificates(certs); |
|
871 |
if (debug != null && Debug.isOn("handshake")) { |
|
872 |
m2.print(System.out); |
|
873 |
} |
|
874 |
m2.write(output); |
|
30904 | 875 |
handshakeState.update(m2, resumingSession); |
2 | 876 |
|
877 |
// XXX has some side effects with OS TCP buffering, |
|
878 |
// leave it out for now |
|
879 |
||
880 |
// let client verify chain in the meantime... |
|
881 |
// output.flush(); |
|
882 |
} else { |
|
883 |
if (certs != null) { |
|
884 |
throw new RuntimeException("anonymous keyexchange with certs"); |
|
885 |
} |
|
886 |
} |
|
887 |
||
888 |
/* |
|
889 |
* THIRD, the ServerKeyExchange message ... iff it's needed. |
|
890 |
* |
|
891 |
* It's usually needed unless there's an encryption-capable |
|
892 |
* RSA cert, or a D-H cert. The notable exception is that |
|
893 |
* exportable ciphers used with big RSA keys need to downgrade |
|
894 |
* to use short RSA keys, even when the key/cert encrypts OK. |
|
895 |
*/ |
|
896 |
||
897 |
ServerKeyExchange m3; |
|
898 |
switch (keyExchange) { |
|
899 |
case K_RSA: |
|
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
900 |
// no server key exchange for RSA ciphersuites |
2 | 901 |
m3 = null; |
902 |
break; |
|
903 |
case K_RSA_EXPORT: |
|
904 |
if (JsseJce.getRSAKeyLength(certs[0].getPublicKey()) > 512) { |
|
905 |
try { |
|
906 |
m3 = new RSA_ServerKeyExchange( |
|
907 |
tempPublicKey, privateKey, |
|
908 |
clnt_random, svr_random, |
|
909 |
sslContext.getSecureRandom()); |
|
910 |
privateKey = tempPrivateKey; |
|
911 |
} catch (GeneralSecurityException e) { |
|
912 |
m3 = null; // make compiler happy |
|
30904 | 913 |
throw new SSLException( |
914 |
"Error generating RSA server key exchange", e); |
|
2 | 915 |
} |
916 |
} else { |
|
917 |
// RSA_EXPORT with short key, don't need ServerKeyExchange |
|
918 |
m3 = null; |
|
919 |
} |
|
920 |
break; |
|
921 |
case K_DHE_RSA: |
|
922 |
case K_DHE_DSS: |
|
923 |
try { |
|
924 |
m3 = new DH_ServerKeyExchange(dh, |
|
925 |
privateKey, |
|
926 |
clnt_random.random_bytes, |
|
927 |
svr_random.random_bytes, |
|
7043 | 928 |
sslContext.getSecureRandom(), |
929 |
preferableSignatureAlgorithm, |
|
930 |
protocolVersion); |
|
2 | 931 |
} catch (GeneralSecurityException e) { |
932 |
m3 = null; // make compiler happy |
|
30904 | 933 |
throw new SSLException( |
934 |
"Error generating DH server key exchange", e); |
|
2 | 935 |
} |
936 |
break; |
|
937 |
case K_DH_ANON: |
|
7043 | 938 |
m3 = new DH_ServerKeyExchange(dh, protocolVersion); |
2 | 939 |
break; |
940 |
case K_ECDHE_RSA: |
|
941 |
case K_ECDHE_ECDSA: |
|
942 |
case K_ECDH_ANON: |
|
943 |
try { |
|
944 |
m3 = new ECDH_ServerKeyExchange(ecdh, |
|
945 |
privateKey, |
|
946 |
clnt_random.random_bytes, |
|
947 |
svr_random.random_bytes, |
|
7043 | 948 |
sslContext.getSecureRandom(), |
949 |
preferableSignatureAlgorithm, |
|
950 |
protocolVersion); |
|
2 | 951 |
} catch (GeneralSecurityException e) { |
952 |
m3 = null; // make compiler happy |
|
30904 | 953 |
throw new SSLException( |
954 |
"Error generating ECDH server key exchange", e); |
|
2 | 955 |
} |
956 |
break; |
|
957 |
case K_ECDH_RSA: |
|
958 |
case K_ECDH_ECDSA: |
|
959 |
// ServerKeyExchange not used for fixed ECDH |
|
960 |
m3 = null; |
|
961 |
break; |
|
962 |
default: |
|
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
963 |
ClientKeyExchangeService p = |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
964 |
ClientKeyExchangeService.find(keyExchange.name); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
965 |
if (p != null) { |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
966 |
// No external key exchange provider needs a cert now. |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
967 |
m3 = null; |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
968 |
break; |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
969 |
} |
2 | 970 |
throw new RuntimeException("internal error: " + keyExchange); |
971 |
} |
|
972 |
if (m3 != null) { |
|
973 |
if (debug != null && Debug.isOn("handshake")) { |
|
974 |
m3.print(System.out); |
|
975 |
} |
|
976 |
m3.write(output); |
|
30904 | 977 |
handshakeState.update(m3, resumingSession); |
2 | 978 |
} |
979 |
||
980 |
// |
|
981 |
// FOURTH, the CertificateRequest message. The details of |
|
982 |
// the message can be affected by the key exchange algorithm |
|
983 |
// in use. For example, certs with fixed Diffie-Hellman keys |
|
984 |
// are only useful with the DH_DSS and DH_RSA key exchange |
|
985 |
// algorithms. |
|
986 |
// |
|
987 |
// Needed only if server requires client to authenticate self. |
|
988 |
// Illegal for anonymous flavors, so we need to check that. |
|
989 |
// |
|
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
990 |
// No external key exchange provider needs a cert now. |
30904 | 991 |
if (doClientAuth != ClientAuthType.CLIENT_AUTH_NONE && |
7043 | 992 |
keyExchange != K_DH_ANON && keyExchange != K_ECDH_ANON && |
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
993 |
ClientKeyExchangeService.find(keyExchange.name) == null) { |
2 | 994 |
|
995 |
CertificateRequest m4; |
|
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
30905
diff
changeset
|
996 |
X509Certificate[] caCerts; |
2 | 997 |
|
7043 | 998 |
Collection<SignatureAndHashAlgorithm> localSignAlgs = null; |
30904 | 999 |
if (protocolVersion.useTLS12PlusSpec()) { |
7043 | 1000 |
// We currently use all local upported signature and hash |
1001 |
// algorithms. However, to minimize the computation cost |
|
1002 |
// of requested hash algorithms, we may use a restricted |
|
1003 |
// set of signature algorithms in the future. |
|
1004 |
localSignAlgs = getLocalSupportedSignAlgs(); |
|
1005 |
if (localSignAlgs.isEmpty()) { |
|
1006 |
throw new SSLHandshakeException( |
|
1007 |
"No supported signature algorithm"); |
|
1008 |
} |
|
1009 |
||
1010 |
Set<String> localHashAlgs = |
|
1011 |
SignatureAndHashAlgorithm.getHashAlgorithmNames( |
|
1012 |
localSignAlgs); |
|
1013 |
if (localHashAlgs.isEmpty()) { |
|
1014 |
throw new SSLHandshakeException( |
|
1015 |
"No supported signature algorithm"); |
|
1016 |
} |
|
1017 |
} |
|
1018 |
||
2 | 1019 |
caCerts = sslContext.getX509TrustManager().getAcceptedIssuers(); |
7043 | 1020 |
m4 = new CertificateRequest(caCerts, keyExchange, |
1021 |
localSignAlgs, protocolVersion); |
|
2 | 1022 |
|
1023 |
if (debug != null && Debug.isOn("handshake")) { |
|
1024 |
m4.print(System.out); |
|
1025 |
} |
|
1026 |
m4.write(output); |
|
30904 | 1027 |
handshakeState.update(m4, resumingSession); |
2 | 1028 |
} |
1029 |
||
1030 |
/* |
|
1031 |
* FIFTH, say ServerHelloDone. |
|
1032 |
*/ |
|
1033 |
ServerHelloDone m5 = new ServerHelloDone(); |
|
1034 |
||
1035 |
if (debug != null && Debug.isOn("handshake")) { |
|
1036 |
m5.print(System.out); |
|
1037 |
} |
|
1038 |
m5.write(output); |
|
30904 | 1039 |
handshakeState.update(m5, resumingSession); |
2 | 1040 |
|
1041 |
/* |
|
1042 |
* Flush any buffered messages so the client will see them. |
|
1043 |
* Ideally, all the messages above go in a single network level |
|
1044 |
* message to the client. Without big Certificate chains, it's |
|
1045 |
* going to be the common case. |
|
1046 |
*/ |
|
1047 |
output.flush(); |
|
1048 |
} |
|
1049 |
||
1050 |
/* |
|
1051 |
* Choose cipher suite from among those supported by client. Sets |
|
1052 |
* the cipherSuite and keyExchange variables. |
|
1053 |
*/ |
|
1054 |
private void chooseCipherSuite(ClientHello mesg) throws IOException { |
|
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18570
diff
changeset
|
1055 |
CipherSuiteList prefered; |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18570
diff
changeset
|
1056 |
CipherSuiteList proposed; |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18570
diff
changeset
|
1057 |
if (preferLocalCipherSuites) { |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18570
diff
changeset
|
1058 |
prefered = getActiveCipherSuites(); |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18570
diff
changeset
|
1059 |
proposed = mesg.getCipherSuites(); |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18570
diff
changeset
|
1060 |
} else { |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18570
diff
changeset
|
1061 |
prefered = mesg.getCipherSuites(); |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18570
diff
changeset
|
1062 |
proposed = getActiveCipherSuites(); |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18570
diff
changeset
|
1063 |
} |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18570
diff
changeset
|
1064 |
|
31689 | 1065 |
List<CipherSuite> legacySuites = new ArrayList<>(); |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18570
diff
changeset
|
1066 |
for (CipherSuite suite : prefered.collection()) { |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18570
diff
changeset
|
1067 |
if (isNegotiable(proposed, suite) == false) { |
2 | 1068 |
continue; |
1069 |
} |
|
6856 | 1070 |
|
30904 | 1071 |
if (doClientAuth == ClientAuthType.CLIENT_AUTH_REQUIRED) { |
6856 | 1072 |
if ((suite.keyExchange == K_DH_ANON) || |
1073 |
(suite.keyExchange == K_ECDH_ANON)) { |
|
2 | 1074 |
continue; |
1075 |
} |
|
1076 |
} |
|
31689 | 1077 |
|
1078 |
if (!legacyAlgorithmConstraints.permits(null, suite.name, null)) { |
|
1079 |
legacySuites.add(suite); |
|
1080 |
continue; |
|
1081 |
} |
|
1082 |
||
2 | 1083 |
if (trySetCipherSuite(suite) == false) { |
1084 |
continue; |
|
1085 |
} |
|
1086 |
return; |
|
1087 |
} |
|
31689 | 1088 |
|
1089 |
for (CipherSuite suite : legacySuites) { |
|
1090 |
if (trySetCipherSuite(suite)) { |
|
1091 |
return; |
|
1092 |
} |
|
1093 |
} |
|
1094 |
||
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18570
diff
changeset
|
1095 |
fatalSE(Alerts.alert_handshake_failure, "no cipher suites in common"); |
2 | 1096 |
} |
1097 |
||
1098 |
/** |
|
1099 |
* Set the given CipherSuite, if possible. Return the result. |
|
1100 |
* The call succeeds if the CipherSuite is available and we have |
|
1101 |
* the necessary certificates to complete the handshake. We don't |
|
1102 |
* check if the CipherSuite is actually enabled. |
|
1103 |
* |
|
1104 |
* If successful, this method also generates ephemeral keys if |
|
1105 |
* required for this ciphersuite. This may take some time, so this |
|
1106 |
* method should only be called if you really want to use the |
|
1107 |
* CipherSuite. |
|
1108 |
* |
|
7039 | 1109 |
* This method is called from chooseCipherSuite() in this class. |
2 | 1110 |
*/ |
1111 |
boolean trySetCipherSuite(CipherSuite suite) { |
|
1112 |
/* |
|
1113 |
* If we're resuming a session we know we can |
|
1114 |
* support this key exchange algorithm and in fact |
|
1115 |
* have already cached the result of it in |
|
1116 |
* the session state. |
|
1117 |
*/ |
|
1118 |
if (resumingSession) { |
|
1119 |
return true; |
|
1120 |
} |
|
1121 |
||
6856 | 1122 |
if (suite.isNegotiable() == false) { |
2 | 1123 |
return false; |
1124 |
} |
|
1125 |
||
7043 | 1126 |
// must not negotiate the obsoleted weak cipher suites. |
30904 | 1127 |
if (protocolVersion.obsoletes(suite)) { |
7039 | 1128 |
return false; |
1129 |
} |
|
1130 |
||
7043 | 1131 |
// must not negotiate unsupported cipher suites. |
30904 | 1132 |
if (!protocolVersion.supports(suite)) { |
7043 | 1133 |
return false; |
1134 |
} |
|
1135 |
||
2 | 1136 |
KeyExchange keyExchange = suite.keyExchange; |
1137 |
||
1138 |
// null out any existing references |
|
1139 |
privateKey = null; |
|
1140 |
certs = null; |
|
1141 |
dh = null; |
|
1142 |
tempPrivateKey = null; |
|
1143 |
tempPublicKey = null; |
|
1144 |
||
7043 | 1145 |
Collection<SignatureAndHashAlgorithm> supportedSignAlgs = null; |
30904 | 1146 |
if (protocolVersion.useTLS12PlusSpec()) { |
7043 | 1147 |
if (peerSupportedSignAlgs != null) { |
1148 |
supportedSignAlgs = peerSupportedSignAlgs; |
|
1149 |
} else { |
|
1150 |
SignatureAndHashAlgorithm algorithm = null; |
|
1151 |
||
1152 |
// we may optimize the performance |
|
1153 |
switch (keyExchange) { |
|
1154 |
// If the negotiated key exchange algorithm is one of |
|
1155 |
// (RSA, DHE_RSA, DH_RSA, RSA_PSK, ECDH_RSA, ECDHE_RSA), |
|
1156 |
// behave as if client had sent the value {sha1,rsa}. |
|
1157 |
case K_RSA: |
|
1158 |
case K_DHE_RSA: |
|
1159 |
case K_DH_RSA: |
|
1160 |
// case K_RSA_PSK: |
|
1161 |
case K_ECDH_RSA: |
|
1162 |
case K_ECDHE_RSA: |
|
1163 |
algorithm = SignatureAndHashAlgorithm.valueOf( |
|
1164 |
HashAlgorithm.SHA1.value, |
|
1165 |
SignatureAlgorithm.RSA.value, 0); |
|
1166 |
break; |
|
1167 |
// If the negotiated key exchange algorithm is one of |
|
1168 |
// (DHE_DSS, DH_DSS), behave as if the client had |
|
1169 |
// sent the value {sha1,dsa}. |
|
1170 |
case K_DHE_DSS: |
|
1171 |
case K_DH_DSS: |
|
1172 |
algorithm = SignatureAndHashAlgorithm.valueOf( |
|
1173 |
HashAlgorithm.SHA1.value, |
|
1174 |
SignatureAlgorithm.DSA.value, 0); |
|
1175 |
break; |
|
1176 |
// If the negotiated key exchange algorithm is one of |
|
1177 |
// (ECDH_ECDSA, ECDHE_ECDSA), behave as if the client |
|
1178 |
// had sent value {sha1,ecdsa}. |
|
1179 |
case K_ECDH_ECDSA: |
|
1180 |
case K_ECDHE_ECDSA: |
|
1181 |
algorithm = SignatureAndHashAlgorithm.valueOf( |
|
1182 |
HashAlgorithm.SHA1.value, |
|
1183 |
SignatureAlgorithm.ECDSA.value, 0); |
|
1184 |
break; |
|
1185 |
default: |
|
1186 |
// no peer supported signature algorithms |
|
1187 |
} |
|
1188 |
||
1189 |
if (algorithm == null) { |
|
1190 |
supportedSignAlgs = |
|
1191 |
Collections.<SignatureAndHashAlgorithm>emptySet(); |
|
1192 |
} else { |
|
1193 |
supportedSignAlgs = |
|
1194 |
new ArrayList<SignatureAndHashAlgorithm>(1); |
|
1195 |
supportedSignAlgs.add(algorithm); |
|
1196 |
} |
|
1197 |
||
1198 |
// Sets the peer supported signature algorithm to use in KM |
|
1199 |
// temporarily. |
|
1200 |
session.setPeerSupportedSignatureAlgorithms(supportedSignAlgs); |
|
1201 |
} |
|
1202 |
} |
|
1203 |
||
2 | 1204 |
switch (keyExchange) { |
1205 |
case K_RSA: |
|
7043 | 1206 |
// need RSA certs for authentication |
1207 |
if (setupPrivateKeyAndChain("RSA") == false) { |
|
1208 |
return false; |
|
1209 |
} |
|
1210 |
break; |
|
2 | 1211 |
case K_RSA_EXPORT: |
1212 |
// need RSA certs for authentication |
|
1213 |
if (setupPrivateKeyAndChain("RSA") == false) { |
|
1214 |
return false; |
|
1215 |
} |
|
1216 |
||
7043 | 1217 |
try { |
1218 |
if (JsseJce.getRSAKeyLength(certs[0].getPublicKey()) > 512) { |
|
1219 |
if (!setupEphemeralRSAKeys(suite.exportable)) { |
|
1220 |
return false; |
|
1221 |
} |
|
1222 |
} |
|
1223 |
} catch (RuntimeException e) { |
|
1224 |
// could not determine keylength, ignore key |
|
1225 |
return false; |
|
1226 |
} |
|
1227 |
break; |
|
1228 |
case K_DHE_RSA: |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9499
diff
changeset
|
1229 |
// need RSA certs for authentication |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9499
diff
changeset
|
1230 |
if (setupPrivateKeyAndChain("RSA") == false) { |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9499
diff
changeset
|
1231 |
return false; |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9499
diff
changeset
|
1232 |
} |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9499
diff
changeset
|
1233 |
|
7043 | 1234 |
// get preferable peer signature algorithm for server key exchange |
30904 | 1235 |
if (protocolVersion.useTLS12PlusSpec()) { |
7043 | 1236 |
preferableSignatureAlgorithm = |
1237 |
SignatureAndHashAlgorithm.getPreferableAlgorithm( |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9499
diff
changeset
|
1238 |
supportedSignAlgs, "RSA", privateKey); |
7043 | 1239 |
if (preferableSignatureAlgorithm == null) { |
2 | 1240 |
return false; |
1241 |
} |
|
7043 | 1242 |
} |
1243 |
||
20539
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1244 |
setupEphemeralDHKeys(suite.exportable, privateKey); |
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9499
diff
changeset
|
1245 |
break; |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9499
diff
changeset
|
1246 |
case K_ECDHE_RSA: |
7043 | 1247 |
// need RSA certs for authentication |
1248 |
if (setupPrivateKeyAndChain("RSA") == false) { |
|
1249 |
return false; |
|
1250 |
} |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9499
diff
changeset
|
1251 |
|
7043 | 1252 |
// get preferable peer signature algorithm for server key exchange |
30904 | 1253 |
if (protocolVersion.useTLS12PlusSpec()) { |
7043 | 1254 |
preferableSignatureAlgorithm = |
1255 |
SignatureAndHashAlgorithm.getPreferableAlgorithm( |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9499
diff
changeset
|
1256 |
supportedSignAlgs, "RSA", privateKey); |
7043 | 1257 |
if (preferableSignatureAlgorithm == null) { |
2 | 1258 |
return false; |
1259 |
} |
|
7043 | 1260 |
} |
1261 |
||
1262 |
if (setupEphemeralECDHKeys() == false) { |
|
1263 |
return false; |
|
1264 |
} |
|
2 | 1265 |
break; |
1266 |
case K_DHE_DSS: |
|
7043 | 1267 |
// get preferable peer signature algorithm for server key exchange |
30904 | 1268 |
if (protocolVersion.useTLS12PlusSpec()) { |
7043 | 1269 |
preferableSignatureAlgorithm = |
1270 |
SignatureAndHashAlgorithm.getPreferableAlgorithm( |
|
1271 |
supportedSignAlgs, "DSA"); |
|
1272 |
if (preferableSignatureAlgorithm == null) { |
|
1273 |
return false; |
|
1274 |
} |
|
1275 |
} |
|
1276 |
||
2 | 1277 |
// need DSS certs for authentication |
1278 |
if (setupPrivateKeyAndChain("DSA") == false) { |
|
1279 |
return false; |
|
1280 |
} |
|
20539
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1281 |
|
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1282 |
setupEphemeralDHKeys(suite.exportable, privateKey); |
2 | 1283 |
break; |
1284 |
case K_ECDHE_ECDSA: |
|
7043 | 1285 |
// get preferable peer signature algorithm for server key exchange |
30904 | 1286 |
if (protocolVersion.useTLS12PlusSpec()) { |
7043 | 1287 |
preferableSignatureAlgorithm = |
1288 |
SignatureAndHashAlgorithm.getPreferableAlgorithm( |
|
1289 |
supportedSignAlgs, "ECDSA"); |
|
1290 |
if (preferableSignatureAlgorithm == null) { |
|
1291 |
return false; |
|
1292 |
} |
|
1293 |
} |
|
1294 |
||
2 | 1295 |
// need EC cert signed using EC |
1296 |
if (setupPrivateKeyAndChain("EC_EC") == false) { |
|
1297 |
return false; |
|
1298 |
} |
|
1299 |
if (setupEphemeralECDHKeys() == false) { |
|
1300 |
return false; |
|
1301 |
} |
|
1302 |
break; |
|
1303 |
case K_ECDH_RSA: |
|
1304 |
// need EC cert signed using RSA |
|
1305 |
if (setupPrivateKeyAndChain("EC_RSA") == false) { |
|
1306 |
return false; |
|
1307 |
} |
|
1308 |
setupStaticECDHKeys(); |
|
1309 |
break; |
|
1310 |
case K_ECDH_ECDSA: |
|
1311 |
// need EC cert signed using EC |
|
1312 |
if (setupPrivateKeyAndChain("EC_EC") == false) { |
|
1313 |
return false; |
|
1314 |
} |
|
1315 |
setupStaticECDHKeys(); |
|
1316 |
break; |
|
1317 |
case K_DH_ANON: |
|
1318 |
// no certs needed for anonymous |
|
20539
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1319 |
setupEphemeralDHKeys(suite.exportable, null); |
2 | 1320 |
break; |
1321 |
case K_ECDH_ANON: |
|
1322 |
// no certs needed for anonymous |
|
1323 |
if (setupEphemeralECDHKeys() == false) { |
|
1324 |
return false; |
|
1325 |
} |
|
1326 |
break; |
|
1327 |
default: |
|
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1328 |
ClientKeyExchangeService p = |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1329 |
ClientKeyExchangeService.find(keyExchange.name); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1330 |
if (p == null) { |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1331 |
// internal error, unknown key exchange |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1332 |
throw new RuntimeException("Unrecognized cipherSuite: " + suite); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1333 |
} |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1334 |
// need service creds |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1335 |
if (serviceCreds == null) { |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1336 |
AccessControlContext acc = getAccSE(); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1337 |
serviceCreds = p.getServiceCreds(acc); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1338 |
if (serviceCreds != null) { |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1339 |
if (debug != null && Debug.isOn("handshake")) { |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1340 |
System.out.println("Using serviceCreds"); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1341 |
} |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1342 |
} |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1343 |
if (serviceCreds == null) { |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1344 |
return false; |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1345 |
} |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1346 |
} |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1347 |
break; |
2 | 1348 |
} |
1349 |
setCipherSuite(suite); |
|
7043 | 1350 |
|
1351 |
// set the peer implicit supported signature algorithms |
|
30904 | 1352 |
if (protocolVersion.useTLS12PlusSpec()) { |
7043 | 1353 |
if (peerSupportedSignAlgs == null) { |
1354 |
setPeerSupportedSignAlgs(supportedSignAlgs); |
|
1355 |
// we had alreay update the session |
|
1356 |
} |
|
1357 |
} |
|
2 | 1358 |
return true; |
1359 |
} |
|
1360 |
||
1361 |
/* |
|
1362 |
* Get some "ephemeral" RSA keys for this context. This means |
|
1363 |
* generating them if it's not already been done. |
|
1364 |
* |
|
1365 |
* Note that we currently do not implement any ciphersuites that use |
|
1366 |
* strong ephemeral RSA. (We do not support the EXPORT1024 ciphersuites |
|
1367 |
* and standard RSA ciphersuites prohibit ephemeral mode for some reason) |
|
1368 |
* This means that export is always true and 512 bit keys are generated. |
|
1369 |
*/ |
|
1370 |
private boolean setupEphemeralRSAKeys(boolean export) { |
|
1371 |
KeyPair kp = sslContext.getEphemeralKeyManager(). |
|
1372 |
getRSAKeyPair(export, sslContext.getSecureRandom()); |
|
1373 |
if (kp == null) { |
|
1374 |
return false; |
|
1375 |
} else { |
|
1376 |
tempPublicKey = kp.getPublic(); |
|
1377 |
tempPrivateKey = kp.getPrivate(); |
|
1378 |
return true; |
|
1379 |
} |
|
1380 |
} |
|
1381 |
||
1382 |
/* |
|
1383 |
* Acquire some "ephemeral" Diffie-Hellman keys for this handshake. |
|
1384 |
* We don't reuse these, for improved forward secrecy. |
|
1385 |
*/ |
|
20539
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1386 |
private void setupEphemeralDHKeys(boolean export, Key key) { |
2 | 1387 |
/* |
20539
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1388 |
* 768 bits ephemeral DH private keys were used to be used in |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1389 |
* ServerKeyExchange except that exportable ciphers max out at 512 |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1390 |
* bits modulus values. We still adhere to this behavior in legacy |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1391 |
* mode (system property "jdk.tls.ephemeralDHKeySize" is defined |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1392 |
* as "legacy"). |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1393 |
* |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1394 |
* Old JDK (JDK 7 and previous) releases don't support DH keys bigger |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1395 |
* than 1024 bits. We have to consider the compatibility requirement. |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1396 |
* 1024 bits DH key is always used for non-exportable cipher suites |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1397 |
* in default mode (system property "jdk.tls.ephemeralDHKeySize" |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1398 |
* is not defined). |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1399 |
* |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1400 |
* However, if applications want more stronger strength, setting |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1401 |
* system property "jdk.tls.ephemeralDHKeySize" to "matched" |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1402 |
* is a workaround to use ephemeral DH key which size matches the |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1403 |
* corresponding authentication key. For example, if the public key |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1404 |
* size of an authentication certificate is 2048 bits, then the |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1405 |
* ephemeral DH key size should be 2048 bits accordingly unless |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1406 |
* the cipher suite is exportable. This key sizing scheme keeps |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1407 |
* the cryptographic strength consistent between authentication |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1408 |
* keys and key-exchange keys. |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1409 |
* |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1410 |
* Applications may also want to customize the ephemeral DH key size |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1411 |
* to a fixed length for non-exportable cipher suites. This can be |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1412 |
* approached by setting system property "jdk.tls.ephemeralDHKeySize" |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1413 |
* to a valid positive integer between 1024 and 2048 bits, inclusive. |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1414 |
* |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1415 |
* Note that the minimum acceptable key size is 1024 bits except |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1416 |
* exportable cipher suites or legacy mode. |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1417 |
* |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1418 |
* Note that the maximum acceptable key size is 2048 bits because |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1419 |
* DH keys bigger than 2048 are not always supported by underlying |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1420 |
* JCE providers. |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1421 |
* |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1422 |
* Note that per RFC 2246, the key size limit of DH is 512 bits for |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1423 |
* exportable cipher suites. Because of the weakness, exportable |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1424 |
* cipher suites are deprecated since TLS v1.1 and they are not |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1425 |
* enabled by default in Oracle provider. The legacy behavior is |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1426 |
* reserved and 512 bits DH key is always used for exportable |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1427 |
* cipher suites. |
2 | 1428 |
*/ |
20539
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1429 |
int keySize = export ? 512 : 1024; // default mode |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1430 |
if (!export) { |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1431 |
if (useLegacyEphemeralDHKeys) { // legacy mode |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1432 |
keySize = 768; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1433 |
} else if (useSmartEphemeralDHKeys) { // matched mode |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1434 |
if (key != null) { |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1435 |
int ks = KeyUtil.getKeySize(key); |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1436 |
// Note that SunJCE provider only supports 2048 bits DH |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1437 |
// keys bigger than 1024. Please DON'T use value other |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1438 |
// than 1024 and 2048 at present. We may improve the |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1439 |
// underlying providers and key size here in the future. |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1440 |
// |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1441 |
// keySize = ks <= 1024 ? 1024 : (ks >= 2048 ? 2048 : ks); |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1442 |
keySize = ks <= 1024 ? 1024 : 2048; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1443 |
} // Otherwise, anonymous cipher suites, 1024-bit is used. |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1444 |
} else if (customizedDHKeySize > 0) { // customized mode |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1445 |
keySize = customizedDHKeySize; |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1446 |
} |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1447 |
} |
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1448 |
|
cbff16f695c6
6956398: make ephemeral DH key match the length of the certificate key
xuelei
parents:
19823
diff
changeset
|
1449 |
dh = new DHCrypt(keySize, sslContext.getSecureRandom()); |
2 | 1450 |
} |
1451 |
||
1452 |
// Setup the ephemeral ECDH parameters. |
|
1453 |
// If we cannot continue because we do not support any of the curves that |
|
1454 |
// the client requested, return false. Otherwise (all is well), return true. |
|
1455 |
private boolean setupEphemeralECDHKeys() { |
|
1456 |
int index = -1; |
|
1457 |
if (supportedCurves != null) { |
|
1458 |
// if the client sent the supported curves extension, pick the |
|
1459 |
// first one that we support; |
|
1460 |
for (int curveId : supportedCurves.curveIds()) { |
|
1461 |
if (SupportedEllipticCurvesExtension.isSupported(curveId)) { |
|
1462 |
index = curveId; |
|
1463 |
break; |
|
1464 |
} |
|
1465 |
} |
|
1466 |
if (index < 0) { |
|
1467 |
// no match found, cannot use this ciphersuite |
|
1468 |
return false; |
|
1469 |
} |
|
1470 |
} else { |
|
1471 |
// pick our preference |
|
1472 |
index = SupportedEllipticCurvesExtension.DEFAULT.curveIds()[0]; |
|
1473 |
} |
|
1474 |
String oid = SupportedEllipticCurvesExtension.getCurveOid(index); |
|
1475 |
ecdh = new ECDHCrypt(oid, sslContext.getSecureRandom()); |
|
1476 |
return true; |
|
1477 |
} |
|
1478 |
||
1479 |
private void setupStaticECDHKeys() { |
|
1480 |
// don't need to check whether the curve is supported, already done |
|
1481 |
// in setupPrivateKeyAndChain(). |
|
1482 |
ecdh = new ECDHCrypt(privateKey, certs[0].getPublicKey()); |
|
1483 |
} |
|
1484 |
||
1485 |
/** |
|
1486 |
* Retrieve the server key and certificate for the specified algorithm |
|
1487 |
* from the KeyManager and set the instance variables. |
|
1488 |
* |
|
1489 |
* @return true if successful, false if not available or invalid |
|
1490 |
*/ |
|
1491 |
private boolean setupPrivateKeyAndChain(String algorithm) { |
|
1492 |
X509ExtendedKeyManager km = sslContext.getX509KeyManager(); |
|
1493 |
String alias; |
|
1494 |
if (conn != null) { |
|
1495 |
alias = km.chooseServerAlias(algorithm, null, conn); |
|
1496 |
} else { |
|
1497 |
alias = km.chooseEngineServerAlias(algorithm, null, engine); |
|
1498 |
} |
|
1499 |
if (alias == null) { |
|
1500 |
return false; |
|
1501 |
} |
|
1502 |
PrivateKey tempPrivateKey = km.getPrivateKey(alias); |
|
1503 |
if (tempPrivateKey == null) { |
|
1504 |
return false; |
|
1505 |
} |
|
1506 |
X509Certificate[] tempCerts = km.getCertificateChain(alias); |
|
1507 |
if ((tempCerts == null) || (tempCerts.length == 0)) { |
|
1508 |
return false; |
|
1509 |
} |
|
1510 |
String keyAlgorithm = algorithm.split("_")[0]; |
|
1511 |
PublicKey publicKey = tempCerts[0].getPublicKey(); |
|
1512 |
if ((tempPrivateKey.getAlgorithm().equals(keyAlgorithm) == false) |
|
1513 |
|| (publicKey.getAlgorithm().equals(keyAlgorithm) == false)) { |
|
1514 |
return false; |
|
1515 |
} |
|
1516 |
// For ECC certs, check whether we support the EC domain parameters. |
|
1517 |
// If the client sent a SupportedEllipticCurves ClientHello extension, |
|
1518 |
// check against that too. |
|
1519 |
if (keyAlgorithm.equals("EC")) { |
|
1520 |
if (publicKey instanceof ECPublicKey == false) { |
|
1521 |
return false; |
|
1522 |
} |
|
1523 |
ECParameterSpec params = ((ECPublicKey)publicKey).getParams(); |
|
1524 |
int index = SupportedEllipticCurvesExtension.getCurveIndex(params); |
|
1525 |
if (SupportedEllipticCurvesExtension.isSupported(index) == false) { |
|
1526 |
return false; |
|
1527 |
} |
|
1528 |
if ((supportedCurves != null) && !supportedCurves.contains(index)) { |
|
1529 |
return false; |
|
1530 |
} |
|
1531 |
} |
|
1532 |
this.privateKey = tempPrivateKey; |
|
1533 |
this.certs = tempCerts; |
|
1534 |
return true; |
|
1535 |
} |
|
1536 |
||
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1537 |
/* |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1538 |
* Returns premaster secret for external key exchange services. |
2 | 1539 |
*/ |
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1540 |
private SecretKey clientKeyExchange(ClientKeyExchange mesg) |
2 | 1541 |
throws IOException { |
1542 |
||
1543 |
if (debug != null && Debug.isOn("handshake")) { |
|
1544 |
mesg.print(System.out); |
|
1545 |
} |
|
1546 |
||
1547 |
// Record the principals involved in exchange |
|
1548 |
session.setPeerPrincipal(mesg.getPeerPrincipal()); |
|
1549 |
session.setLocalPrincipal(mesg.getLocalPrincipal()); |
|
1550 |
||
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1551 |
return mesg.clientKeyExchange(); |
2 | 1552 |
} |
1553 |
||
1554 |
/* |
|
1555 |
* Diffie Hellman key exchange is used when the server presented |
|
1556 |
* D-H parameters in its certificate (signed using RSA or DSS/DSA), |
|
1557 |
* or else the server presented no certificate but sent D-H params |
|
1558 |
* in a ServerKeyExchange message. Use of D-H is specified by the |
|
1559 |
* cipher suite chosen. |
|
1560 |
* |
|
1561 |
* The message optionally contains the client's D-H public key (if |
|
1562 |
* it wasn't not sent in a client certificate). As always with D-H, |
|
1563 |
* if a client and a server have each other's D-H public keys and |
|
1564 |
* they use common algorithm parameters, they have a shared key |
|
1565 |
* that's derived via the D-H calculation. That key becomes the |
|
1566 |
* pre-master secret. |
|
1567 |
*/ |
|
1568 |
private SecretKey clientKeyExchange(DHClientKeyExchange mesg) |
|
1569 |
throws IOException { |
|
1570 |
||
1571 |
if (debug != null && Debug.isOn("handshake")) { |
|
1572 |
mesg.print(System.out); |
|
1573 |
} |
|
16080 | 1574 |
return dh.getAgreedSecret(mesg.getClientPublicKey(), false); |
2 | 1575 |
} |
1576 |
||
1577 |
private SecretKey clientKeyExchange(ECDHClientKeyExchange mesg) |
|
1578 |
throws IOException { |
|
1579 |
||
1580 |
if (debug != null && Debug.isOn("handshake")) { |
|
1581 |
mesg.print(System.out); |
|
1582 |
} |
|
1583 |
return ecdh.getAgreedSecret(mesg.getEncodedPoint()); |
|
1584 |
} |
|
1585 |
||
1586 |
/* |
|
1587 |
* Client wrote a message to verify the certificate it sent earlier. |
|
1588 |
* |
|
1589 |
* Note that this certificate isn't involved in key exchange. Client |
|
1590 |
* authentication messages are included in the checksums used to |
|
1591 |
* validate the handshake (e.g. Finished messages). Other than that, |
|
1592 |
* the _exact_ identity of the client is less fundamental to protocol |
|
1593 |
* security than its role in selecting keys via the pre-master secret. |
|
1594 |
*/ |
|
1595 |
private void clientCertificateVerify(CertificateVerify mesg) |
|
1596 |
throws IOException { |
|
1597 |
||
1598 |
if (debug != null && Debug.isOn("handshake")) { |
|
1599 |
mesg.print(System.out); |
|
1600 |
} |
|
1601 |
||
30904 | 1602 |
if (protocolVersion.useTLS12PlusSpec()) { |
7043 | 1603 |
SignatureAndHashAlgorithm signAlg = |
1604 |
mesg.getPreferableSignatureAlgorithm(); |
|
1605 |
if (signAlg == null) { |
|
1606 |
throw new SSLHandshakeException( |
|
1607 |
"Illegal CertificateVerify message"); |
|
1608 |
} |
|
1609 |
||
1610 |
String hashAlg = |
|
1611 |
SignatureAndHashAlgorithm.getHashAlgorithmName(signAlg); |
|
1612 |
if (hashAlg == null || hashAlg.length() == 0) { |
|
1613 |
throw new SSLHandshakeException( |
|
1614 |
"No supported hash algorithm"); |
|
1615 |
} |
|
1616 |
} |
|
1617 |
||
2 | 1618 |
try { |
1619 |
PublicKey publicKey = |
|
1620 |
session.getPeerCertificates()[0].getPublicKey(); |
|
1621 |
||
1622 |
boolean valid = mesg.verify(protocolVersion, handshakeHash, |
|
1623 |
publicKey, session.getMasterSecret()); |
|
1624 |
if (valid == false) { |
|
1625 |
fatalSE(Alerts.alert_bad_certificate, |
|
1626 |
"certificate verify message signature error"); |
|
1627 |
} |
|
1628 |
} catch (GeneralSecurityException e) { |
|
1629 |
fatalSE(Alerts.alert_bad_certificate, |
|
1630 |
"certificate verify format error", e); |
|
1631 |
} |
|
1632 |
||
1633 |
// reset the flag for clientCertificateVerify message |
|
1634 |
needClientVerify = false; |
|
1635 |
} |
|
1636 |
||
1637 |
||
1638 |
/* |
|
1639 |
* Client writes "finished" at the end of its handshake, after cipher |
|
1640 |
* spec is changed. We verify it and then send ours. |
|
1641 |
* |
|
1642 |
* When we're resuming a session, we'll have already sent our own |
|
1643 |
* Finished message so just the verification is needed. |
|
1644 |
*/ |
|
1645 |
private void clientFinished(Finished mesg) throws IOException { |
|
1646 |
if (debug != null && Debug.isOn("handshake")) { |
|
1647 |
mesg.print(System.out); |
|
1648 |
} |
|
1649 |
||
1650 |
/* |
|
1651 |
* Verify if client did send the certificate when client |
|
1652 |
* authentication was required, otherwise server should not proceed |
|
1653 |
*/ |
|
30904 | 1654 |
if (doClientAuth == ClientAuthType.CLIENT_AUTH_REQUIRED) { |
2 | 1655 |
// get X500Principal of the end-entity certificate for X509-based |
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
30904
diff
changeset
|
1656 |
// ciphersuites, or Kerberos principal for Kerberos ciphersuites, etc |
2 | 1657 |
session.getPeerPrincipal(); |
1658 |
} |
|
1659 |
||
1660 |
/* |
|
1661 |
* Verify if client did send clientCertificateVerify message following |
|
1662 |
* the client Certificate, otherwise server should not proceed |
|
1663 |
*/ |
|
1664 |
if (needClientVerify) { |
|
1665 |
fatalSE(Alerts.alert_handshake_failure, |
|
1666 |
"client did not send certificate verify message"); |
|
1667 |
} |
|
1668 |
||
1669 |
/* |
|
1670 |
* Verify the client's message with the "before" digest of messages, |
|
1671 |
* and forget about continuing to use that digest. |
|
1672 |
*/ |
|
7043 | 1673 |
boolean verified = mesg.verify(handshakeHash, Finished.CLIENT, |
1674 |
session.getMasterSecret()); |
|
2 | 1675 |
|
1676 |
if (!verified) { |
|
1677 |
fatalSE(Alerts.alert_handshake_failure, |
|
1678 |
"client 'finished' message doesn't verify"); |
|
1679 |
// NOTREACHED |
|
1680 |
} |
|
1681 |
||
1682 |
/* |
|
6856 | 1683 |
* save client verify data for secure renegotiation |
1684 |
*/ |
|
1685 |
if (secureRenegotiation) { |
|
1686 |
clientVerifyData = mesg.getVerifyData(); |
|
1687 |
} |
|
1688 |
||
1689 |
/* |
|
2 | 1690 |
* OK, it verified. If we're doing the full handshake, add that |
1691 |
* "Finished" message to the hash of handshake messages, then send |
|
1692 |
* the change_cipher_spec and Finished message. |
|
1693 |
*/ |
|
1694 |
if (!resumingSession) { |
|
1695 |
sendChangeCipherAndFinish(true); |
|
30904 | 1696 |
} else { |
1697 |
handshakeFinished = true; |
|
2 | 1698 |
} |
1699 |
||
1700 |
/* |
|
1701 |
* Update the session cache only after the handshake completed, else |
|
1702 |
* we're open to an attack against a partially completed handshake. |
|
1703 |
*/ |
|
1704 |
session.setLastAccessedTime(System.currentTimeMillis()); |
|
1705 |
if (!resumingSession && session.isRejoinable()) { |
|
1706 |
((SSLSessionContextImpl)sslContext.engineGetServerSessionContext()) |
|
1707 |
.put(session); |
|
1708 |
if (debug != null && Debug.isOn("session")) { |
|
1709 |
System.out.println( |
|
1710 |
"%% Cached server session: " + session); |
|
1711 |
} |
|
1712 |
} else if (!resumingSession && |
|
1713 |
debug != null && Debug.isOn("session")) { |
|
1714 |
System.out.println( |
|
1715 |
"%% Didn't cache non-resumable server session: " |
|
1716 |
+ session); |
|
1717 |
} |
|
1718 |
} |
|
1719 |
||
1720 |
/* |
|
1721 |
* Compute finished message with the "server" digest (and then forget |
|
1722 |
* about that digest, it can't be used again). |
|
1723 |
*/ |
|
1724 |
private void sendChangeCipherAndFinish(boolean finishedTag) |
|
1725 |
throws IOException { |
|
1726 |
||
30904 | 1727 |
// Reload if this message has been reserved. |
1728 |
handshakeHash.reload(); |
|
2 | 1729 |
|
1730 |
Finished mesg = new Finished(protocolVersion, handshakeHash, |
|
7043 | 1731 |
Finished.SERVER, session.getMasterSecret(), cipherSuite); |
2 | 1732 |
|
1733 |
/* |
|
1734 |
* Send the change_cipher_spec record; then our Finished handshake |
|
1735 |
* message will be the last handshake message. Flush, and now we |
|
1736 |
* are ready for application data!! |
|
1737 |
*/ |
|
1738 |
sendChangeCipherSpec(mesg, finishedTag); |
|
1739 |
||
1740 |
/* |
|
6856 | 1741 |
* save server verify data for secure renegotiation |
1742 |
*/ |
|
1743 |
if (secureRenegotiation) { |
|
1744 |
serverVerifyData = mesg.getVerifyData(); |
|
1745 |
} |
|
2 | 1746 |
} |
1747 |
||
1748 |
||
1749 |
/* |
|
1750 |
* Returns a HelloRequest message to kickstart renegotiations |
|
1751 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1752 |
@Override |
2 | 1753 |
HandshakeMessage getKickstartMessage() { |
1754 |
return new HelloRequest(); |
|
1755 |
} |
|
1756 |
||
1757 |
||
1758 |
/* |
|
1759 |
* Fault detected during handshake. |
|
1760 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1761 |
@Override |
2 | 1762 |
void handshakeAlert(byte description) throws SSLProtocolException { |
1763 |
||
1764 |
String message = Alerts.alertDescription(description); |
|
1765 |
||
1766 |
if (debug != null && Debug.isOn("handshake")) { |
|
1767 |
System.out.println("SSL -- handshake alert: " |
|
1768 |
+ message); |
|
1769 |
} |
|
1770 |
||
1771 |
/* |
|
1772 |
* It's ok to get a no_certificate alert from a client of which |
|
1773 |
* we *requested* authentication information. |
|
1774 |
* However, if we *required* it, then this is not acceptable. |
|
1775 |
* |
|
1776 |
* Anyone calling getPeerCertificates() on the |
|
1777 |
* session will get an SSLPeerUnverifiedException. |
|
1778 |
*/ |
|
1779 |
if ((description == Alerts.alert_no_certificate) && |
|
30904 | 1780 |
(doClientAuth == ClientAuthType.CLIENT_AUTH_REQUESTED)) { |
2 | 1781 |
return; |
1782 |
} |
|
1783 |
||
1784 |
throw new SSLProtocolException("handshake alert: " + message); |
|
1785 |
} |
|
1786 |
||
1787 |
/* |
|
1788 |
* RSA key exchange is normally used. The client encrypts a "pre-master |
|
1789 |
* secret" with the server's public key, from the Certificate (or else |
|
1790 |
* ServerKeyExchange) message that was sent to it by the server. That's |
|
1791 |
* decrypted using the private key before we get here. |
|
1792 |
*/ |
|
7043 | 1793 |
private SecretKey clientKeyExchange(RSAClientKeyExchange mesg) |
1794 |
throws IOException { |
|
2 | 1795 |
|
1796 |
if (debug != null && Debug.isOn("handshake")) { |
|
1797 |
mesg.print(System.out); |
|
1798 |
} |
|
1799 |
return mesg.preMaster; |
|
1800 |
} |
|
1801 |
||
1802 |
/* |
|
1803 |
* Verify the certificate sent by the client. We'll only get one if we |
|
1804 |
* sent a CertificateRequest to request client authentication. If we |
|
1805 |
* are in TLS mode, the client may send a message with no certificates |
|
1806 |
* to indicate it does not have an appropriate chain. (In SSLv3 mode, |
|
1807 |
* it would send a no certificate alert). |
|
1808 |
*/ |
|
1809 |
private void clientCertificate(CertificateMsg mesg) throws IOException { |
|
1810 |
if (debug != null && Debug.isOn("handshake")) { |
|
1811 |
mesg.print(System.out); |
|
1812 |
} |
|
1813 |
||
1814 |
X509Certificate[] peerCerts = mesg.getCertificateChain(); |
|
1815 |
||
1816 |
if (peerCerts.length == 0) { |
|
1817 |
/* |
|
1818 |
* If the client authentication is only *REQUESTED* (e.g. |
|
1819 |
* not *REQUIRED*, this is an acceptable condition.) |
|
1820 |
*/ |
|
30904 | 1821 |
if (doClientAuth == ClientAuthType.CLIENT_AUTH_REQUESTED) { |
2 | 1822 |
return; |
1823 |
} else { |
|
1824 |
fatalSE(Alerts.alert_bad_certificate, |
|
1825 |
"null cert chain"); |
|
1826 |
} |
|
1827 |
} |
|
1828 |
||
1829 |
// ask the trust manager to verify the chain |
|
1830 |
X509TrustManager tm = sslContext.getX509TrustManager(); |
|
1831 |
||
1832 |
try { |
|
1833 |
// find out the types of client authentication used |
|
1834 |
PublicKey key = peerCerts[0].getPublicKey(); |
|
1835 |
String keyAlgorithm = key.getAlgorithm(); |
|
1836 |
String authType; |
|
1837 |
if (keyAlgorithm.equals("RSA")) { |
|
1838 |
authType = "RSA"; |
|
1839 |
} else if (keyAlgorithm.equals("DSA")) { |
|
1840 |
authType = "DSA"; |
|
1841 |
} else if (keyAlgorithm.equals("EC")) { |
|
1842 |
authType = "EC"; |
|
1843 |
} else { |
|
1844 |
// unknown public key type |
|
1845 |
authType = "UNKNOWN"; |
|
1846 |
} |
|
1847 |
||
1848 |
if (tm instanceof X509ExtendedTrustManager) { |
|
7043 | 1849 |
if (conn != null) { |
1850 |
((X509ExtendedTrustManager)tm).checkClientTrusted( |
|
1851 |
peerCerts.clone(), |
|
2 | 1852 |
authType, |
7043 | 1853 |
conn); |
1854 |
} else { |
|
1855 |
((X509ExtendedTrustManager)tm).checkClientTrusted( |
|
1856 |
peerCerts.clone(), |
|
1857 |
authType, |
|
1858 |
engine); |
|
1859 |
} |
|
2 | 1860 |
} else { |
7043 | 1861 |
// Unlikely to happen, because we have wrapped the old |
1862 |
// X509TrustManager with the new X509ExtendedTrustManager. |
|
1863 |
throw new CertificateException( |
|
1864 |
"Improper X509TrustManager implementation"); |
|
2 | 1865 |
} |
1866 |
} catch (CertificateException e) { |
|
1867 |
// This will throw an exception, so include the original error. |
|
1868 |
fatalSE(Alerts.alert_certificate_unknown, e); |
|
1869 |
} |
|
1870 |
// set the flag for clientCertificateVerify message |
|
1871 |
needClientVerify = true; |
|
1872 |
||
1873 |
session.setPeerCertificates(peerCerts); |
|
1874 |
} |
|
1875 |
} |