author | robm |
Thu, 10 Jan 2019 07:54:16 -0800 | |
changeset 53248 | ed36ff53642c |
parent 50768 | 68fa3d4026ea |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
17483
diff
changeset
|
2 |
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.jndi.ldap.ext; |
|
27 |
||
28 |
import java.io.InputStream; |
|
29 |
import java.io.OutputStream; |
|
30 |
import java.io.IOException; |
|
31 |
||
32 |
import java.security.Principal; |
|
33 |
import java.security.cert.X509Certificate; |
|
34 |
import java.security.cert.CertificateException; |
|
35 |
||
36 |
import javax.net.ssl.SSLSession; |
|
37 |
import javax.net.ssl.SSLSocket; |
|
38 |
import javax.net.ssl.SSLSocketFactory; |
|
39 |
import javax.net.ssl.SSLPeerUnverifiedException; |
|
40 |
import javax.net.ssl.HostnameVerifier; |
|
41 |
import sun.security.util.HostnameChecker; |
|
42 |
||
43 |
import javax.naming.ldap.*; |
|
44 |
import com.sun.jndi.ldap.Connection; |
|
45 |
||
46 |
/** |
|
47 |
* This class implements the LDAPv3 Extended Response for StartTLS as |
|
48 |
* defined in |
|
49 |
* <a href="http://www.ietf.org/rfc/rfc2830.txt">Lightweight Directory |
|
50 |
* Access Protocol (v3): Extension for Transport Layer Security</a> |
|
51 |
* |
|
52 |
* The object identifier for StartTLS is 1.3.6.1.4.1.1466.20037 |
|
53 |
* and no extended response value is defined. |
|
54 |
* |
|
30655 | 55 |
* <p> |
2 | 56 |
* The Start TLS extended request and response are used to establish |
57 |
* a TLS connection over the existing LDAP connection associated with |
|
30655 | 58 |
* the JNDI context on which {@code extendedOperation()} is invoked. |
2 | 59 |
* |
60 |
* @see StartTlsRequest |
|
61 |
* @author Vincent Ryan |
|
62 |
*/ |
|
63 |
final public class StartTlsResponseImpl extends StartTlsResponse { |
|
64 |
||
65 |
private static final boolean debug = false; |
|
66 |
||
67 |
/* |
|
68 |
* The dNSName type in a subjectAltName extension of an X.509 certificate |
|
69 |
*/ |
|
70 |
private static final int DNSNAME_TYPE = 2; |
|
71 |
||
72 |
/* |
|
73 |
* The server's hostname. |
|
74 |
*/ |
|
75 |
private transient String hostname = null; |
|
76 |
||
77 |
/* |
|
78 |
* The LDAP socket. |
|
79 |
*/ |
|
80 |
private transient Connection ldapConnection = null; |
|
81 |
||
82 |
/* |
|
83 |
* The original input stream. |
|
84 |
*/ |
|
85 |
private transient InputStream originalInputStream = null; |
|
86 |
||
87 |
/* |
|
88 |
* The original output stream. |
|
89 |
*/ |
|
90 |
private transient OutputStream originalOutputStream = null; |
|
91 |
||
92 |
/* |
|
93 |
* The SSL socket. |
|
94 |
*/ |
|
95 |
private transient SSLSocket sslSocket = null; |
|
96 |
||
97 |
/* |
|
98 |
* The SSL socket factories. |
|
99 |
*/ |
|
100 |
private transient SSLSocketFactory defaultFactory = null; |
|
101 |
private transient SSLSocketFactory currentFactory = null; |
|
102 |
||
103 |
/* |
|
104 |
* The list of cipher suites to be enabled. |
|
105 |
*/ |
|
106 |
private transient String[] suites = null; |
|
107 |
||
108 |
/* |
|
109 |
* The hostname verifier callback. |
|
110 |
*/ |
|
111 |
private transient HostnameVerifier verifier = null; |
|
112 |
||
113 |
/* |
|
114 |
* The flag to indicate that the TLS connection is closed. |
|
115 |
*/ |
|
116 |
private transient boolean isClosed = true; |
|
117 |
||
118 |
private static final long serialVersionUID = -1126624615143411328L; |
|
119 |
||
120 |
// public no-arg constructor required by JDK's Service Provider API. |
|
121 |
||
122 |
public StartTlsResponseImpl() {} |
|
123 |
||
124 |
/** |
|
125 |
* Overrides the default list of cipher suites enabled for use on the |
|
126 |
* TLS connection. The cipher suites must have already been listed by |
|
30655 | 127 |
* {@code SSLSocketFactory.getSupportedCipherSuites()} as being supported. |
2 | 128 |
* Even if a suite has been enabled, it still might not be used because |
129 |
* the peer does not support it, or because the requisite certificates |
|
130 |
* (and private keys) are not available. |
|
131 |
* |
|
132 |
* @param suites The non-null list of names of all the cipher suites to |
|
133 |
* enable. |
|
134 |
* @see #negotiate |
|
135 |
*/ |
|
136 |
public void setEnabledCipherSuites(String[] suites) { |
|
17483
ed57f2f26fb6
8010814: More buffers are stored or returned without cloning
xuelei
parents:
10324
diff
changeset
|
137 |
// The impl does accept null suites, although the spec requires |
ed57f2f26fb6
8010814: More buffers are stored or returned without cloning
xuelei
parents:
10324
diff
changeset
|
138 |
// a non-null list. |
ed57f2f26fb6
8010814: More buffers are stored or returned without cloning
xuelei
parents:
10324
diff
changeset
|
139 |
this.suites = suites == null ? null : suites.clone(); |
2 | 140 |
} |
141 |
||
142 |
/** |
|
30655 | 143 |
* Overrides the default hostname verifier used by {@code negotiate()} |
2 | 144 |
* after the TLS handshake has completed. If |
30655 | 145 |
* {@code setHostnameVerifier()} has not been called before |
146 |
* {@code negotiate()} is invoked, {@code negotiate()} |
|
2 | 147 |
* will perform a simple case ignore match. If called after |
30655 | 148 |
* {@code negotiate()}, this method does not do anything. |
2 | 149 |
* |
150 |
* @param verifier The non-null hostname verifier callback. |
|
151 |
* @see #negotiate |
|
152 |
*/ |
|
153 |
public void setHostnameVerifier(HostnameVerifier verifier) { |
|
154 |
this.verifier = verifier; |
|
155 |
} |
|
156 |
||
157 |
/** |
|
158 |
* Negotiates a TLS session using the default SSL socket factory. |
|
159 |
* <p> |
|
30655 | 160 |
* This method is equivalent to {@code negotiate(null)}. |
2 | 161 |
* |
162 |
* @return The negotiated SSL session |
|
30655 | 163 |
* @throws IOException If an IO error was encountered while establishing |
2 | 164 |
* the TLS session. |
165 |
* @see #setEnabledCipherSuites |
|
166 |
* @see #setHostnameVerifier |
|
167 |
*/ |
|
168 |
public SSLSession negotiate() throws IOException { |
|
169 |
||
170 |
return negotiate(null); |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Negotiates a TLS session using an SSL socket factory. |
|
175 |
* <p> |
|
176 |
* Creates an SSL socket using the supplied SSL socket factory and |
|
177 |
* attaches it to the existing connection. Performs the TLS handshake |
|
178 |
* and returns the negotiated session information. |
|
179 |
* <p> |
|
30655 | 180 |
* If cipher suites have been set via {@code setEnabledCipherSuites} |
2 | 181 |
* then they are enabled before the TLS handshake begins. |
182 |
* <p> |
|
183 |
* Hostname verification is performed after the TLS handshake completes. |
|
184 |
* The default check performs a case insensitive match of the server's |
|
185 |
* hostname against that in the server's certificate. The server's |
|
186 |
* hostname is extracted from the subjectAltName in the server's |
|
187 |
* certificate (if present). Otherwise the value of the common name |
|
188 |
* attribute of the subject name is used. If a callback has |
|
30655 | 189 |
* been set via {@code setHostnameVerifier} then that verifier is used if |
2 | 190 |
* the default check fails. |
191 |
* <p> |
|
192 |
* If an error occurs then the SSL socket is closed and an IOException |
|
193 |
* is thrown. The underlying connection remains intact. |
|
194 |
* |
|
195 |
* @param factory The possibly null SSL socket factory to use. |
|
196 |
* If null, the default SSL socket factory is used. |
|
197 |
* @return The negotiated SSL session |
|
30655 | 198 |
* @throws IOException If an IO error was encountered while establishing |
2 | 199 |
* the TLS session. |
200 |
* @see #setEnabledCipherSuites |
|
201 |
* @see #setHostnameVerifier |
|
202 |
*/ |
|
203 |
public SSLSession negotiate(SSLSocketFactory factory) throws IOException { |
|
204 |
||
205 |
if (isClosed && sslSocket != null) { |
|
206 |
throw new IOException("TLS connection is closed."); |
|
207 |
} |
|
208 |
||
209 |
if (factory == null) { |
|
210 |
factory = getDefaultFactory(); |
|
211 |
} |
|
212 |
||
213 |
if (debug) { |
|
214 |
System.out.println("StartTLS: About to start handshake"); |
|
215 |
} |
|
216 |
||
217 |
SSLSession sslSession = startHandshake(factory).getSession(); |
|
218 |
||
219 |
if (debug) { |
|
220 |
System.out.println("StartTLS: Completed handshake"); |
|
221 |
} |
|
222 |
||
223 |
SSLPeerUnverifiedException verifExcep = null; |
|
224 |
try { |
|
225 |
if (verify(hostname, sslSession)) { |
|
226 |
isClosed = false; |
|
227 |
return sslSession; |
|
228 |
} |
|
229 |
} catch (SSLPeerUnverifiedException e) { |
|
230 |
// Save to return the cause |
|
231 |
verifExcep = e; |
|
232 |
} |
|
233 |
if ((verifier != null) && |
|
234 |
verifier.verify(hostname, sslSession)) { |
|
235 |
isClosed = false; |
|
236 |
return sslSession; |
|
237 |
} |
|
238 |
||
239 |
// Verification failed |
|
240 |
close(); |
|
241 |
sslSession.invalidate(); |
|
242 |
if (verifExcep == null) { |
|
243 |
verifExcep = new SSLPeerUnverifiedException( |
|
244 |
"hostname of the server '" + hostname + |
|
245 |
"' does not match the hostname in the " + |
|
246 |
"server's certificate."); |
|
247 |
} |
|
248 |
throw verifExcep; |
|
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* Closes the TLS connection gracefully and reverts back to the underlying |
|
253 |
* connection. |
|
254 |
* |
|
30655 | 255 |
* @throws IOException If an IO error was encountered while closing the |
2 | 256 |
* TLS connection |
257 |
*/ |
|
258 |
public void close() throws IOException { |
|
259 |
||
260 |
if (isClosed) { |
|
261 |
return; |
|
262 |
} |
|
263 |
||
264 |
if (debug) { |
|
265 |
System.out.println("StartTLS: replacing SSL " + |
|
266 |
"streams with originals"); |
|
267 |
} |
|
268 |
||
269 |
// Replace SSL streams with the original streams |
|
270 |
ldapConnection.replaceStreams( |
|
271 |
originalInputStream, originalOutputStream); |
|
272 |
||
273 |
if (debug) { |
|
274 |
System.out.println("StartTLS: closing SSL Socket"); |
|
275 |
} |
|
276 |
sslSocket.close(); |
|
277 |
||
278 |
isClosed = true; |
|
279 |
} |
|
280 |
||
281 |
/** |
|
282 |
* Sets the connection for TLS to use. The TLS connection will be attached |
|
283 |
* to this connection. |
|
284 |
* |
|
285 |
* @param ldapConnection The non-null connection to use. |
|
286 |
* @param hostname The server's hostname. If null, the hostname used to |
|
287 |
* open the connection will be used instead. |
|
288 |
*/ |
|
289 |
public void setConnection(Connection ldapConnection, String hostname) { |
|
290 |
this.ldapConnection = ldapConnection; |
|
53248
ed36ff53642c
8214440: ldap over a TLS connection negotiate failed with "javax.net.ssl.SSLPeerUnverifiedException: hostname of the server '' does not match the hostname in the server's certificate"
robm
parents:
50768
diff
changeset
|
291 |
this.hostname = (hostname == null || hostname.isEmpty()) |
ed36ff53642c
8214440: ldap over a TLS connection negotiate failed with "javax.net.ssl.SSLPeerUnverifiedException: hostname of the server '' does not match the hostname in the server's certificate"
robm
parents:
50768
diff
changeset
|
292 |
? ldapConnection.host : hostname; |
2 | 293 |
originalInputStream = ldapConnection.inStream; |
294 |
originalOutputStream = ldapConnection.outStream; |
|
295 |
} |
|
296 |
||
297 |
/* |
|
298 |
* Returns the default SSL socket factory. |
|
299 |
* |
|
300 |
* @return The default SSL socket factory. |
|
32227
34721a47bc92
8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents:
30655
diff
changeset
|
301 |
* @throws IOException If TLS is not supported. |
2 | 302 |
*/ |
303 |
private SSLSocketFactory getDefaultFactory() throws IOException { |
|
304 |
||
305 |
if (defaultFactory != null) { |
|
306 |
return defaultFactory; |
|
307 |
} |
|
308 |
||
309 |
return (defaultFactory = |
|
310 |
(SSLSocketFactory) SSLSocketFactory.getDefault()); |
|
311 |
} |
|
312 |
||
313 |
/* |
|
314 |
* Start the TLS handshake and manipulate the input and output streams. |
|
315 |
* |
|
316 |
* @param factory The SSL socket factory to use. |
|
317 |
* @return The SSL socket. |
|
32227
34721a47bc92
8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents:
30655
diff
changeset
|
318 |
* @throws IOException If an exception occurred while performing the |
2 | 319 |
* TLS handshake. |
320 |
*/ |
|
321 |
private SSLSocket startHandshake(SSLSocketFactory factory) |
|
322 |
throws IOException { |
|
323 |
||
324 |
if (ldapConnection == null) { |
|
325 |
throw new IllegalStateException("LDAP connection has not been set." |
|
326 |
+ " TLS requires an existing LDAP connection."); |
|
327 |
} |
|
328 |
||
329 |
if (factory != currentFactory) { |
|
330 |
// Create SSL socket layered over the existing connection |
|
331 |
sslSocket = (SSLSocket) factory.createSocket(ldapConnection.sock, |
|
332 |
ldapConnection.host, ldapConnection.port, false); |
|
333 |
currentFactory = factory; |
|
334 |
||
335 |
if (debug) { |
|
336 |
System.out.println("StartTLS: Created socket : " + sslSocket); |
|
337 |
} |
|
338 |
} |
|
339 |
||
340 |
if (suites != null) { |
|
341 |
sslSocket.setEnabledCipherSuites(suites); |
|
342 |
if (debug) { |
|
343 |
System.out.println("StartTLS: Enabled cipher suites"); |
|
344 |
} |
|
345 |
} |
|
346 |
||
347 |
// Connection must be quite for handshake to proceed |
|
348 |
||
349 |
try { |
|
350 |
if (debug) { |
|
351 |
System.out.println( |
|
352 |
"StartTLS: Calling sslSocket.startHandshake"); |
|
353 |
} |
|
354 |
sslSocket.startHandshake(); |
|
355 |
if (debug) { |
|
356 |
System.out.println( |
|
357 |
"StartTLS: + Finished sslSocket.startHandshake"); |
|
358 |
} |
|
359 |
||
360 |
// Replace original streams with the new SSL streams |
|
361 |
ldapConnection.replaceStreams(sslSocket.getInputStream(), |
|
362 |
sslSocket.getOutputStream()); |
|
363 |
if (debug) { |
|
364 |
System.out.println("StartTLS: Replaced IO Streams"); |
|
365 |
} |
|
366 |
||
367 |
} catch (IOException e) { |
|
368 |
if (debug) { |
|
369 |
System.out.println("StartTLS: Got IO error during handshake"); |
|
370 |
e.printStackTrace(); |
|
371 |
} |
|
372 |
||
373 |
sslSocket.close(); |
|
374 |
isClosed = true; |
|
375 |
throw e; // pass up exception |
|
376 |
} |
|
377 |
||
378 |
return sslSocket; |
|
379 |
} |
|
380 |
||
381 |
/* |
|
382 |
* Verifies that the hostname in the server's certificate matches the |
|
383 |
* hostname of the server. |
|
384 |
* The server's first certificate is examined. If it has a subjectAltName |
|
385 |
* that contains a dNSName then that is used as the server's hostname. |
|
386 |
* The server's hostname may contain a wildcard for its left-most name part. |
|
387 |
* Otherwise, if the certificate has no subjectAltName then the value of |
|
388 |
* the common name attribute of the subject name is used. |
|
389 |
* |
|
390 |
* @param hostname The hostname of the server. |
|
391 |
* @param session the SSLSession used on the connection to host. |
|
392 |
* @return true if the hostname is verified, false otherwise. |
|
393 |
*/ |
|
394 |
||
395 |
private boolean verify(String hostname, SSLSession session) |
|
396 |
throws SSLPeerUnverifiedException { |
|
397 |
||
398 |
java.security.cert.Certificate[] certs = null; |
|
399 |
||
400 |
// if IPv6 strip off the "[]" |
|
401 |
if (hostname != null && hostname.startsWith("[") && |
|
402 |
hostname.endsWith("]")) { |
|
403 |
hostname = hostname.substring(1, hostname.length() - 1); |
|
404 |
} |
|
405 |
try { |
|
406 |
HostnameChecker checker = HostnameChecker.getInstance( |
|
407 |
HostnameChecker.TYPE_LDAP); |
|
50768 | 408 |
// get the subject's certificate |
409 |
certs = session.getPeerCertificates(); |
|
410 |
X509Certificate peerCert; |
|
411 |
if (certs[0] instanceof java.security.cert.X509Certificate) { |
|
412 |
peerCert = (java.security.cert.X509Certificate) certs[0]; |
|
413 |
} else { |
|
414 |
throw new SSLPeerUnverifiedException( |
|
415 |
"Received a non X509Certificate from the server"); |
|
2 | 416 |
} |
50768 | 417 |
checker.match(hostname, peerCert); |
2 | 418 |
|
419 |
// no exception means verification passed |
|
420 |
return true; |
|
421 |
} catch (SSLPeerUnverifiedException e) { |
|
422 |
||
423 |
/* |
|
424 |
* The application may enable an anonymous SSL cipher suite, and |
|
425 |
* hostname verification is not done for anonymous ciphers |
|
426 |
*/ |
|
427 |
String cipher = session.getCipherSuite(); |
|
428 |
if (cipher != null && (cipher.indexOf("_anon_") != -1)) { |
|
429 |
return true; |
|
430 |
} |
|
431 |
throw e; |
|
432 |
} catch (CertificateException e) { |
|
433 |
||
434 |
/* |
|
435 |
* Pass up the cause of the failure |
|
436 |
*/ |
|
437 |
throw(SSLPeerUnverifiedException) |
|
438 |
new SSLPeerUnverifiedException("hostname of the server '" + |
|
439 |
hostname + |
|
440 |
"' does not match the hostname in the " + |
|
441 |
"server's certificate.").initCause(e); |
|
442 |
} |
|
443 |
} |
|
444 |
||
445 |
/* |
|
446 |
* Get the peer principal from the session |
|
447 |
*/ |
|
448 |
private static Principal getPeerPrincipal(SSLSession session) |
|
449 |
throws SSLPeerUnverifiedException { |
|
450 |
Principal principal; |
|
451 |
try { |
|
452 |
principal = session.getPeerPrincipal(); |
|
453 |
} catch (AbstractMethodError e) { |
|
454 |
// if the JSSE provider does not support it, return null, since |
|
455 |
// we need it only for Kerberos. |
|
456 |
principal = null; |
|
457 |
} |
|
458 |
return principal; |
|
459 |
} |
|
460 |
} |