2
|
1 |
/*
|
|
2 |
* Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
|
|
27 |
package sun.security.ssl;
|
|
28 |
|
|
29 |
import java.io.*;
|
|
30 |
import java.net.*;
|
|
31 |
import java.security.GeneralSecurityException;
|
|
32 |
import java.security.AccessController;
|
|
33 |
import java.security.AccessControlContext;
|
|
34 |
import java.security.PrivilegedAction;
|
|
35 |
import java.util.*;
|
|
36 |
|
|
37 |
import javax.crypto.BadPaddingException;
|
|
38 |
|
|
39 |
import javax.net.ssl.*;
|
|
40 |
|
|
41 |
import com.sun.net.ssl.internal.ssl.X509ExtendedTrustManager;
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Implementation of an SSL socket. This is a normal connection type
|
|
45 |
* socket, implementing SSL over some lower level socket, such as TCP.
|
|
46 |
* Because it is layered over some lower level socket, it MUST override
|
|
47 |
* all default socket methods.
|
|
48 |
*
|
|
49 |
* <P> This API offers a non-traditional option for establishing SSL
|
|
50 |
* connections. You may first establish the connection directly, then pass
|
|
51 |
* that connection to the SSL socket constructor with a flag saying which
|
|
52 |
* role should be taken in the handshake protocol. (The two ends of the
|
|
53 |
* connection must not choose the same role!) This allows setup of SSL
|
|
54 |
* proxying or tunneling, and also allows the kind of "role reversal"
|
|
55 |
* that is required for most FTP data transfers.
|
|
56 |
*
|
|
57 |
* @see javax.net.ssl.SSLSocket
|
|
58 |
* @see SSLServerSocket
|
|
59 |
*
|
|
60 |
* @author David Brownell
|
|
61 |
*/
|
|
62 |
final public class SSLSocketImpl extends BaseSSLSocketImpl {
|
|
63 |
|
|
64 |
/*
|
|
65 |
* ERROR HANDLING GUIDELINES
|
|
66 |
* (which exceptions to throw and catch and which not to throw and catch)
|
|
67 |
*
|
|
68 |
* . if there is an IOException (SocketException) when accessing the
|
|
69 |
* underlying Socket, pass it through
|
|
70 |
*
|
|
71 |
* . do not throw IOExceptions, throw SSLExceptions (or a subclass)
|
|
72 |
*
|
|
73 |
* . for internal errors (things that indicate a bug in JSSE or a
|
|
74 |
* grossly misconfigured J2RE), throw either an SSLException or
|
|
75 |
* a RuntimeException at your convenience.
|
|
76 |
*
|
|
77 |
* . handshaking code (Handshaker or HandshakeMessage) should generally
|
|
78 |
* pass through exceptions, but can handle them if they know what to
|
|
79 |
* do.
|
|
80 |
*
|
|
81 |
* . exception chaining should be used for all new code. If you happen
|
|
82 |
* to touch old code that does not use chaining, you should change it.
|
|
83 |
*
|
|
84 |
* . there is a top level exception handler that sits at all entry
|
|
85 |
* points from application code to SSLSocket read/write code. It
|
|
86 |
* makes sure that all errors are handled (see handleException()).
|
|
87 |
*
|
|
88 |
* . JSSE internal code should generally not call close(), call
|
|
89 |
* closeInternal().
|
|
90 |
*/
|
|
91 |
|
|
92 |
/*
|
|
93 |
* There's a state machine associated with each connection, which
|
|
94 |
* among other roles serves to negotiate session changes.
|
|
95 |
*
|
|
96 |
* - START with constructor, until the TCP connection's around.
|
|
97 |
* - HANDSHAKE picks session parameters before allowing traffic.
|
|
98 |
* There are many substates due to sequencing requirements
|
|
99 |
* for handshake messages.
|
|
100 |
* - DATA may be transmitted.
|
|
101 |
* - RENEGOTIATE state allows concurrent data and handshaking
|
|
102 |
* traffic ("same" substates as HANDSHAKE), and terminates
|
|
103 |
* in selection of new session (and connection) parameters
|
|
104 |
* - ERROR state immediately precedes abortive disconnect.
|
|
105 |
* - SENT_CLOSE sent a close_notify to the peer. For layered,
|
|
106 |
* non-autoclose socket, must now read close_notify
|
|
107 |
* from peer before closing the connection. For nonlayered or
|
|
108 |
* non-autoclose socket, close connection and go onto
|
|
109 |
* cs_CLOSED state.
|
|
110 |
* - CLOSED after sending close_notify alert, & socket is closed.
|
|
111 |
* SSL connection objects are not reused.
|
|
112 |
* - APP_CLOSED once the application calls close(). Then it behaves like
|
|
113 |
* a closed socket, e.g.. getInputStream() throws an Exception.
|
|
114 |
*
|
|
115 |
* State affects what SSL record types may legally be sent:
|
|
116 |
*
|
|
117 |
* - Handshake ... only in HANDSHAKE and RENEGOTIATE states
|
|
118 |
* - App Data ... only in DATA and RENEGOTIATE states
|
|
119 |
* - Alert ... in HANDSHAKE, DATA, RENEGOTIATE
|
|
120 |
*
|
|
121 |
* Re what may be received: same as what may be sent, except that
|
|
122 |
* HandshakeRequest handshaking messages can come from servers even
|
|
123 |
* in the application data state, to request entry to RENEGOTIATE.
|
|
124 |
*
|
|
125 |
* The state machine within HANDSHAKE and RENEGOTIATE states controls
|
|
126 |
* the pending session, not the connection state, until the change
|
|
127 |
* cipher spec and "Finished" handshake messages are processed and
|
|
128 |
* make the "new" session become the current one.
|
|
129 |
*
|
|
130 |
* NOTE: details of the SMs always need to be nailed down better.
|
|
131 |
* The text above illustrates the core ideas.
|
|
132 |
*
|
|
133 |
* +---->-------+------>--------->-------+
|
|
134 |
* | | |
|
|
135 |
* <-----< ^ ^ <-----< v
|
|
136 |
*START>----->HANDSHAKE>----->DATA>----->RENEGOTIATE SENT_CLOSE
|
|
137 |
* v v v | |
|
|
138 |
* | | | | v
|
|
139 |
* +------------+---------------+ v ERROR
|
|
140 |
* | | |
|
|
141 |
* v | |
|
|
142 |
* ERROR>------>----->CLOSED<--------<----+-- +
|
|
143 |
* |
|
|
144 |
* v
|
|
145 |
* APP_CLOSED
|
|
146 |
*
|
|
147 |
* ALSO, note that the the purpose of handshaking (renegotiation is
|
|
148 |
* included) is to assign a different, and perhaps new, session to
|
|
149 |
* the connection. The SSLv3 spec is a bit confusing on that new
|
|
150 |
* protocol feature.
|
|
151 |
*/
|
|
152 |
private static final int cs_START = 0;
|
|
153 |
private static final int cs_HANDSHAKE = 1;
|
|
154 |
private static final int cs_DATA = 2;
|
|
155 |
private static final int cs_RENEGOTIATE = 3;
|
|
156 |
private static final int cs_ERROR = 4;
|
|
157 |
private static final int cs_SENT_CLOSE = 5;
|
|
158 |
private static final int cs_CLOSED = 6;
|
|
159 |
private static final int cs_APP_CLOSED = 7;
|
|
160 |
|
|
161 |
|
|
162 |
/*
|
|
163 |
* Client authentication be off, requested, or required.
|
|
164 |
*
|
|
165 |
* Migrated to SSLEngineImpl:
|
|
166 |
* clauth_none/cl_auth_requested/clauth_required
|
|
167 |
*/
|
|
168 |
|
|
169 |
/*
|
|
170 |
* Drives the protocol state machine.
|
|
171 |
*/
|
|
172 |
private int connectionState;
|
|
173 |
|
|
174 |
/*
|
|
175 |
* Flag indicating if the next record we receive MUST be a Finished
|
|
176 |
* message. Temporarily set during the handshake to ensure that
|
|
177 |
* a change cipher spec message is followed by a finished message.
|
|
178 |
*/
|
|
179 |
private boolean expectingFinished;
|
|
180 |
|
|
181 |
/*
|
|
182 |
* For improved diagnostics, we detail connection closure
|
|
183 |
* If the socket is closed (connectionState >= cs_ERROR),
|
|
184 |
* closeReason != null indicates if the socket was closed
|
|
185 |
* because of an error or because or normal shutdown.
|
|
186 |
*/
|
|
187 |
private SSLException closeReason;
|
|
188 |
|
|
189 |
/*
|
|
190 |
* Per-connection private state that doesn't change when the
|
|
191 |
* session is changed.
|
|
192 |
*/
|
|
193 |
private byte doClientAuth;
|
|
194 |
private boolean roleIsServer;
|
|
195 |
private CipherSuiteList enabledCipherSuites;
|
|
196 |
private boolean enableSessionCreation = true;
|
|
197 |
private String host;
|
|
198 |
private boolean autoClose = true;
|
|
199 |
private AccessControlContext acc;
|
|
200 |
|
|
201 |
// hostname identification algorithm, the hostname identification is
|
|
202 |
// disabled by default.
|
|
203 |
private String identificationAlg = null;
|
|
204 |
|
|
205 |
/*
|
|
206 |
* READ ME * READ ME * READ ME * READ ME * READ ME * READ ME *
|
|
207 |
* IMPORTANT STUFF TO UNDERSTANDING THE SYNCHRONIZATION ISSUES.
|
|
208 |
* READ ME * READ ME * READ ME * READ ME * READ ME * READ ME *
|
|
209 |
*
|
|
210 |
* There are several locks here.
|
|
211 |
*
|
|
212 |
* The primary lock is the per-instance lock used by
|
|
213 |
* synchronized(this) and the synchronized methods. It controls all
|
|
214 |
* access to things such as the connection state and variables which
|
|
215 |
* affect handshaking. If we are inside a synchronized method, we
|
|
216 |
* can access the state directly, otherwise, we must use the
|
|
217 |
* synchronized equivalents.
|
|
218 |
*
|
|
219 |
* The handshakeLock is used to ensure that only one thread performs
|
|
220 |
* the *complete initial* handshake. If someone is handshaking, any
|
|
221 |
* stray application or startHandshake() requests who find the
|
|
222 |
* connection state is cs_HANDSHAKE will stall on handshakeLock
|
|
223 |
* until handshaking is done. Once the handshake is done, we either
|
|
224 |
* succeeded or failed, but we can never go back to the cs_HANDSHAKE
|
|
225 |
* or cs_START state again.
|
|
226 |
*
|
|
227 |
* Note that the read/write() calls here in SSLSocketImpl are not
|
|
228 |
* obviously synchronized. In fact, it's very nonintuitive, and
|
|
229 |
* requires careful examination of code paths. Grab some coffee,
|
|
230 |
* and be careful with any code changes.
|
|
231 |
*
|
|
232 |
* There can be only three threads active at a time in the I/O
|
|
233 |
* subsection of this class.
|
|
234 |
* 1. startHandshake
|
|
235 |
* 2. AppInputStream
|
|
236 |
* 3. AppOutputStream
|
|
237 |
* One thread could call startHandshake().
|
|
238 |
* AppInputStream/AppOutputStream read() and write() calls are each
|
|
239 |
* synchronized on 'this' in their respective classes, so only one
|
|
240 |
* app. thread will be doing a SSLSocketImpl.read() or .write()'s at
|
|
241 |
* a time.
|
|
242 |
*
|
|
243 |
* If handshaking is required (state cs_HANDSHAKE), and
|
|
244 |
* getConnectionState() for some/all threads returns cs_HANDSHAKE,
|
|
245 |
* only one can grab the handshakeLock, and the rest will stall
|
|
246 |
* either on getConnectionState(), or on the handshakeLock if they
|
|
247 |
* happen to successfully race through the getConnectionState().
|
|
248 |
*
|
|
249 |
* If a writer is doing the initial handshaking, it must create a
|
|
250 |
* temporary reader to read the responses from the other side. As a
|
|
251 |
* side-effect, the writer's reader will have priority over any
|
|
252 |
* other reader. However, the writer's reader is not allowed to
|
|
253 |
* consume any application data. When handshakeLock is finally
|
|
254 |
* released, we either have a cs_DATA connection, or a
|
|
255 |
* cs_CLOSED/cs_ERROR socket.
|
|
256 |
*
|
|
257 |
* The writeLock is held while writing on a socket connection and
|
|
258 |
* also to protect the MAC and cipher for their direction. The
|
|
259 |
* writeLock is package private for Handshaker which holds it while
|
|
260 |
* writing the ChangeCipherSpec message.
|
|
261 |
*
|
|
262 |
* To avoid the problem of a thread trying to change operational
|
|
263 |
* modes on a socket while handshaking is going on, we synchronize
|
|
264 |
* on 'this'. If handshaking has not started yet, we tell the
|
|
265 |
* handshaker to change its mode. If handshaking has started,
|
|
266 |
* we simply store that request until the next pending session
|
|
267 |
* is created, at which time the new handshaker's state is set.
|
|
268 |
*
|
|
269 |
* The readLock is held during readRecord(), which is responsible
|
|
270 |
* for reading an InputRecord, decrypting it, and processing it.
|
|
271 |
* The readLock ensures that these three steps are done atomically
|
|
272 |
* and that once started, no other thread can block on InputRecord.read.
|
|
273 |
* This is necessary so that processing of close_notify alerts
|
|
274 |
* from the peer are handled properly.
|
|
275 |
*/
|
|
276 |
private Object handshakeLock;
|
|
277 |
Object writeLock;
|
|
278 |
private Object readLock;
|
|
279 |
|
|
280 |
private InputRecord inrec;
|
|
281 |
|
|
282 |
/*
|
|
283 |
* Crypto state that's reinitialized when the session changes.
|
|
284 |
*/
|
|
285 |
private MAC readMAC, writeMAC;
|
|
286 |
private CipherBox readCipher, writeCipher;
|
|
287 |
// NOTE: compression state would be saved here
|
|
288 |
|
|
289 |
/*
|
|
290 |
* The authentication context holds all information used to establish
|
|
291 |
* who this end of the connection is (certificate chains, private keys,
|
|
292 |
* etc) and who is trusted (e.g. as CAs or websites).
|
|
293 |
*/
|
|
294 |
private SSLContextImpl sslContext;
|
|
295 |
|
|
296 |
|
|
297 |
/*
|
|
298 |
* This connection is one of (potentially) many associated with
|
|
299 |
* any given session. The output of the handshake protocol is a
|
|
300 |
* new session ... although all the protocol description talks
|
|
301 |
* about changing the cipher spec (and it does change), in fact
|
|
302 |
* that's incidental since it's done by changing everything that
|
|
303 |
* is associated with a session at the same time. (TLS/IETF may
|
|
304 |
* change that to add client authentication w/o new key exchg.)
|
|
305 |
*/
|
|
306 |
private SSLSessionImpl sess;
|
|
307 |
private Handshaker handshaker;
|
|
308 |
|
|
309 |
|
|
310 |
/*
|
|
311 |
* If anyone wants to get notified about handshake completions,
|
|
312 |
* they'll show up on this list.
|
|
313 |
*/
|
|
314 |
private HashMap<HandshakeCompletedListener, AccessControlContext>
|
|
315 |
handshakeListeners;
|
|
316 |
|
|
317 |
|
|
318 |
/*
|
|
319 |
* Reuse the same internal input/output streams.
|
|
320 |
*/
|
|
321 |
private InputStream sockInput;
|
|
322 |
private OutputStream sockOutput;
|
|
323 |
|
|
324 |
|
|
325 |
/*
|
|
326 |
* These input and output streams block their data in SSL records,
|
|
327 |
* and usually arrange integrity and privacy protection for those
|
|
328 |
* records. The guts of the SSL protocol are wrapped up in these
|
|
329 |
* streams, and in the handshaking that establishes the details of
|
|
330 |
* that integrity and privacy protection.
|
|
331 |
*/
|
|
332 |
private AppInputStream input;
|
|
333 |
private AppOutputStream output;
|
|
334 |
|
|
335 |
/*
|
|
336 |
* The protocols we support are SSL Version 3.0) and
|
|
337 |
* TLS (version 3.1).
|
|
338 |
* In addition we support a pseudo protocol called
|
|
339 |
* SSLv2Hello which when set will result in an SSL v2 Hello
|
|
340 |
* being sent with SSLv3 or TLSv1 version info.
|
|
341 |
*/
|
|
342 |
private ProtocolList enabledProtocols;
|
|
343 |
|
|
344 |
/*
|
|
345 |
* The SSL version associated with this connection.
|
|
346 |
*/
|
|
347 |
private ProtocolVersion protocolVersion = ProtocolVersion.DEFAULT;
|
|
348 |
|
|
349 |
/* Class and subclass dynamic debugging support */
|
|
350 |
private static final Debug debug = Debug.getInstance("ssl");
|
|
351 |
|
|
352 |
//
|
|
353 |
// CONSTRUCTORS AND INITIALIZATION CODE
|
|
354 |
//
|
|
355 |
|
|
356 |
/**
|
|
357 |
* Constructs an SSL connection to a named host at a specified port,
|
|
358 |
* using the authentication context provided. This endpoint acts as
|
|
359 |
* the client, and may rejoin an existing SSL session if appropriate.
|
|
360 |
*
|
|
361 |
* @param context authentication context to use
|
|
362 |
* @param host name of the host with which to connect
|
|
363 |
* @param port number of the server's port
|
|
364 |
*/
|
|
365 |
SSLSocketImpl(SSLContextImpl context, String host, int port)
|
|
366 |
throws IOException, UnknownHostException {
|
|
367 |
super();
|
|
368 |
this.host = host;
|
|
369 |
init(context, false);
|
|
370 |
SocketAddress socketAddress = new InetSocketAddress(host, port);
|
|
371 |
connect(socketAddress, 0);
|
|
372 |
}
|
|
373 |
|
|
374 |
|
|
375 |
/**
|
|
376 |
* Constructs an SSL connection to a server at a specified address.
|
|
377 |
* and TCP port, using the authentication context provided. This
|
|
378 |
* endpoint acts as the client, and may rejoin an existing SSL session
|
|
379 |
* if appropriate.
|
|
380 |
*
|
|
381 |
* @param context authentication context to use
|
|
382 |
* @param address the server's host
|
|
383 |
* @param port its port
|
|
384 |
*/
|
|
385 |
SSLSocketImpl(SSLContextImpl context, InetAddress host, int port)
|
|
386 |
throws IOException {
|
|
387 |
super();
|
|
388 |
init(context, false);
|
|
389 |
SocketAddress socketAddress = new InetSocketAddress(host, port);
|
|
390 |
connect(socketAddress, 0);
|
|
391 |
}
|
|
392 |
|
|
393 |
/**
|
|
394 |
* Constructs an SSL connection to a named host at a specified port,
|
|
395 |
* using the authentication context provided. This endpoint acts as
|
|
396 |
* the client, and may rejoin an existing SSL session if appropriate.
|
|
397 |
*
|
|
398 |
* @param context authentication context to use
|
|
399 |
* @param host name of the host with which to connect
|
|
400 |
* @param port number of the server's port
|
|
401 |
* @param localAddr the local address the socket is bound to
|
|
402 |
* @param localPort the local port the socket is bound to
|
|
403 |
*/
|
|
404 |
SSLSocketImpl(SSLContextImpl context, String host, int port,
|
|
405 |
InetAddress localAddr, int localPort)
|
|
406 |
throws IOException, UnknownHostException {
|
|
407 |
super();
|
|
408 |
this.host = host;
|
|
409 |
init(context, false);
|
|
410 |
bind(new InetSocketAddress(localAddr, localPort));
|
|
411 |
SocketAddress socketAddress = new InetSocketAddress(host, port);
|
|
412 |
connect(socketAddress, 0);
|
|
413 |
}
|
|
414 |
|
|
415 |
|
|
416 |
/**
|
|
417 |
* Constructs an SSL connection to a server at a specified address.
|
|
418 |
* and TCP port, using the authentication context provided. This
|
|
419 |
* endpoint acts as the client, and may rejoin an existing SSL session
|
|
420 |
* if appropriate.
|
|
421 |
*
|
|
422 |
* @param context authentication context to use
|
|
423 |
* @param address the server's host
|
|
424 |
* @param port its port
|
|
425 |
* @param localAddr the local address the socket is bound to
|
|
426 |
* @param localPort the local port the socket is bound to
|
|
427 |
*/
|
|
428 |
SSLSocketImpl(SSLContextImpl context, InetAddress host, int port,
|
|
429 |
InetAddress localAddr, int localPort)
|
|
430 |
throws IOException {
|
|
431 |
super();
|
|
432 |
init(context, false);
|
|
433 |
bind(new InetSocketAddress(localAddr, localPort));
|
|
434 |
SocketAddress socketAddress = new InetSocketAddress(host, port);
|
|
435 |
connect(socketAddress, 0);
|
|
436 |
}
|
|
437 |
|
|
438 |
/*
|
|
439 |
* Package-private constructor used ONLY by SSLServerSocket. The
|
|
440 |
* java.net package accepts the TCP connection after this call is
|
|
441 |
* made. This just initializes handshake state to use "server mode",
|
|
442 |
* giving control over the use of SSL client authentication.
|
|
443 |
*/
|
|
444 |
SSLSocketImpl(SSLContextImpl context, boolean serverMode,
|
|
445 |
CipherSuiteList suites, byte clientAuth,
|
|
446 |
boolean sessionCreation, ProtocolList protocols)
|
|
447 |
throws IOException {
|
|
448 |
super();
|
|
449 |
doClientAuth = clientAuth;
|
|
450 |
enableSessionCreation = sessionCreation;
|
|
451 |
init(context, serverMode);
|
|
452 |
|
|
453 |
/*
|
|
454 |
* Override what was picked out for us.
|
|
455 |
*/
|
|
456 |
enabledCipherSuites = suites;
|
|
457 |
enabledProtocols = protocols;
|
|
458 |
}
|
|
459 |
|
|
460 |
|
|
461 |
/**
|
|
462 |
* Package-private constructor used to instantiate an unconnected
|
|
463 |
* socket. The java.net package will connect it, either when the
|
|
464 |
* connect() call is made by the application. This instance is
|
|
465 |
* meant to set handshake state to use "client mode".
|
|
466 |
*/
|
|
467 |
SSLSocketImpl(SSLContextImpl context) {
|
|
468 |
super();
|
|
469 |
init(context, false);
|
|
470 |
}
|
|
471 |
|
|
472 |
|
|
473 |
/**
|
|
474 |
* Layer SSL traffic over an existing connection, rather than creating
|
|
475 |
* a new connection. The existing connection may be used only for SSL
|
|
476 |
* traffic (using this SSLSocket) until the SSLSocket.close() call
|
|
477 |
* returns. However, if a protocol error is detected, that existing
|
|
478 |
* connection is automatically closed.
|
|
479 |
*
|
|
480 |
* <P> This particular constructor always uses the socket in the
|
|
481 |
* role of an SSL client. It may be useful in cases which start
|
|
482 |
* using SSL after some initial data transfers, for example in some
|
|
483 |
* SSL tunneling applications or as part of some kinds of application
|
|
484 |
* protocols which negotiate use of a SSL based security.
|
|
485 |
*
|
|
486 |
* @param sock the existing connection
|
|
487 |
* @param context the authentication context to use
|
|
488 |
*/
|
|
489 |
SSLSocketImpl(SSLContextImpl context, Socket sock, String host,
|
|
490 |
int port, boolean autoClose) throws IOException {
|
|
491 |
super(sock);
|
|
492 |
// We always layer over a connected socket
|
|
493 |
if (!sock.isConnected()) {
|
|
494 |
throw new SocketException("Underlying socket is not connected");
|
|
495 |
}
|
|
496 |
this.host = host;
|
|
497 |
init(context, false);
|
|
498 |
this.autoClose = autoClose;
|
|
499 |
doneConnect();
|
|
500 |
}
|
|
501 |
|
|
502 |
/**
|
|
503 |
* Initializes the client socket.
|
|
504 |
*/
|
|
505 |
private void init(SSLContextImpl context, boolean isServer) {
|
|
506 |
sslContext = context;
|
|
507 |
sess = SSLSessionImpl.nullSession;
|
|
508 |
|
|
509 |
/*
|
|
510 |
* role is as specified, state is START until after
|
|
511 |
* the low level connection's established.
|
|
512 |
*/
|
|
513 |
roleIsServer = isServer;
|
|
514 |
connectionState = cs_START;
|
|
515 |
|
|
516 |
/*
|
|
517 |
* default read and write side cipher and MAC support
|
|
518 |
*
|
|
519 |
* Note: compression support would go here too
|
|
520 |
*/
|
|
521 |
readCipher = CipherBox.NULL;
|
|
522 |
readMAC = MAC.NULL;
|
|
523 |
writeCipher = CipherBox.NULL;
|
|
524 |
writeMAC = MAC.NULL;
|
|
525 |
|
|
526 |
enabledCipherSuites = CipherSuiteList.getDefault();
|
|
527 |
enabledProtocols = ProtocolList.getDefault();
|
|
528 |
handshakeLock = new Object();
|
|
529 |
writeLock = new Object();
|
|
530 |
readLock = new Object();
|
|
531 |
inrec = null;
|
|
532 |
|
|
533 |
// save the acc
|
|
534 |
acc = AccessController.getContext();
|
|
535 |
|
|
536 |
input = new AppInputStream(this);
|
|
537 |
output = new AppOutputStream(this);
|
|
538 |
}
|
|
539 |
|
|
540 |
/**
|
|
541 |
* Connects this socket to the server with a specified timeout
|
|
542 |
* value.
|
|
543 |
*
|
|
544 |
* This method is either called on an unconnected SSLSocketImpl by the
|
|
545 |
* application, or it is called in the constructor of a regular
|
|
546 |
* SSLSocketImpl. If we are layering on top on another socket, then
|
|
547 |
* this method should not be called, because we assume that the
|
|
548 |
* underlying socket is already connected by the time it is passed to
|
|
549 |
* us.
|
|
550 |
*
|
|
551 |
* @param endpoint the <code>SocketAddress</code>
|
|
552 |
* @param timeout the timeout value to be used, 0 is no timeout
|
|
553 |
* @throws IOException if an error occurs during the connection
|
|
554 |
* @throws SocketTimeoutException if timeout expires before connecting
|
|
555 |
*/
|
|
556 |
public void connect(SocketAddress endpoint, int timeout)
|
|
557 |
throws IOException {
|
|
558 |
|
|
559 |
if (self != this) {
|
|
560 |
throw new SocketException("Already connected");
|
|
561 |
}
|
|
562 |
|
|
563 |
if (!(endpoint instanceof InetSocketAddress)) {
|
|
564 |
throw new SocketException(
|
|
565 |
"Cannot handle non-Inet socket addresses.");
|
|
566 |
}
|
|
567 |
|
|
568 |
super.connect(endpoint, timeout);
|
|
569 |
doneConnect();
|
|
570 |
}
|
|
571 |
|
|
572 |
/**
|
|
573 |
* Initialize the handshaker and socket streams.
|
|
574 |
*
|
|
575 |
* Called by connect, the layered constructor, and SSLServerSocket.
|
|
576 |
*/
|
|
577 |
void doneConnect() throws IOException {
|
|
578 |
/*
|
|
579 |
* Save the input and output streams. May be done only after
|
|
580 |
* java.net actually connects using the socket "self", else
|
|
581 |
* we get some pretty bizarre failure modes.
|
|
582 |
*/
|
|
583 |
if (self == this) {
|
|
584 |
sockInput = super.getInputStream();
|
|
585 |
sockOutput = super.getOutputStream();
|
|
586 |
} else {
|
|
587 |
sockInput = self.getInputStream();
|
|
588 |
sockOutput = self.getOutputStream();
|
|
589 |
}
|
|
590 |
|
|
591 |
/*
|
|
592 |
* Move to handshaking state, with pending session initialized
|
|
593 |
* to defaults and the appropriate kind of handshaker set up.
|
|
594 |
*/
|
|
595 |
initHandshaker();
|
|
596 |
}
|
|
597 |
|
|
598 |
synchronized private int getConnectionState() {
|
|
599 |
return connectionState;
|
|
600 |
}
|
|
601 |
|
|
602 |
synchronized private void setConnectionState(int state) {
|
|
603 |
connectionState = state;
|
|
604 |
}
|
|
605 |
|
|
606 |
AccessControlContext getAcc() {
|
|
607 |
return acc;
|
|
608 |
}
|
|
609 |
|
|
610 |
//
|
|
611 |
// READING AND WRITING RECORDS
|
|
612 |
//
|
|
613 |
|
|
614 |
/*
|
|
615 |
* Record Output. Application data can't be sent until the first
|
|
616 |
* handshake establishes a session.
|
|
617 |
*
|
|
618 |
* NOTE: we let empty records be written as a hook to force some
|
|
619 |
* TCP-level activity, notably handshaking, to occur.
|
|
620 |
*/
|
|
621 |
void writeRecord(OutputRecord r) throws IOException {
|
|
622 |
/*
|
|
623 |
* The loop is in case of HANDSHAKE --> ERROR transitions, etc
|
|
624 |
*/
|
|
625 |
loop:
|
|
626 |
while (r.contentType() == Record.ct_application_data) {
|
|
627 |
/*
|
|
628 |
* Not all states support passing application data. We
|
|
629 |
* synchronize access to the connection state, so that
|
|
630 |
* synchronous handshakes can complete cleanly.
|
|
631 |
*/
|
|
632 |
switch (getConnectionState()) {
|
|
633 |
|
|
634 |
/*
|
|
635 |
* We've deferred the initial handshaking till just now,
|
|
636 |
* when presumably a thread's decided it's OK to block for
|
|
637 |
* longish periods of time for I/O purposes (as well as
|
|
638 |
* configured the cipher suites it wants to use).
|
|
639 |
*/
|
|
640 |
case cs_HANDSHAKE:
|
|
641 |
performInitialHandshake();
|
|
642 |
break;
|
|
643 |
|
|
644 |
case cs_DATA:
|
|
645 |
case cs_RENEGOTIATE:
|
|
646 |
break loop;
|
|
647 |
|
|
648 |
case cs_ERROR:
|
|
649 |
fatal(Alerts.alert_close_notify,
|
|
650 |
"error while writing to socket");
|
|
651 |
break; // dummy
|
|
652 |
|
|
653 |
case cs_SENT_CLOSE:
|
|
654 |
case cs_CLOSED:
|
|
655 |
case cs_APP_CLOSED:
|
|
656 |
// we should never get here (check in AppOutputStream)
|
|
657 |
// this is just a fallback
|
|
658 |
if (closeReason != null) {
|
|
659 |
throw closeReason;
|
|
660 |
} else {
|
|
661 |
throw new SocketException("Socket closed");
|
|
662 |
}
|
|
663 |
|
|
664 |
/*
|
|
665 |
* Else something's goofy in this state machine's use.
|
|
666 |
*/
|
|
667 |
default:
|
|
668 |
throw new SSLProtocolException("State error, send app data");
|
|
669 |
}
|
|
670 |
}
|
|
671 |
|
|
672 |
//
|
|
673 |
// Don't bother to really write empty records. We went this
|
|
674 |
// far to drive the handshake machinery, for correctness; not
|
|
675 |
// writing empty records improves performance by cutting CPU
|
|
676 |
// time and network resource usage. However, some protocol
|
|
677 |
// implementations are fragile and don't like to see empty
|
|
678 |
// records, so this also increases robustness.
|
|
679 |
//
|
|
680 |
synchronized (writeLock) {
|
|
681 |
if (!r.isEmpty()) {
|
|
682 |
// r.compress(c);
|
|
683 |
r.addMAC(writeMAC);
|
|
684 |
r.encrypt(writeCipher);
|
|
685 |
r.write(sockOutput);
|
|
686 |
}
|
|
687 |
}
|
|
688 |
}
|
|
689 |
|
|
690 |
|
|
691 |
/*
|
|
692 |
* Read an application data record. Alerts and handshake
|
|
693 |
* messages are handled directly.
|
|
694 |
*/
|
|
695 |
void readDataRecord(InputRecord r) throws IOException {
|
|
696 |
if (getConnectionState() == cs_HANDSHAKE) {
|
|
697 |
performInitialHandshake();
|
|
698 |
}
|
|
699 |
readRecord(r, true);
|
|
700 |
}
|
|
701 |
|
|
702 |
|
|
703 |
/*
|
|
704 |
* Clear the pipeline of records from the peer, optionally returning
|
|
705 |
* application data. Caller is responsible for knowing that it's
|
|
706 |
* possible to do this kind of clearing, if they don't want app
|
|
707 |
* data -- e.g. since it's the initial SSL handshake.
|
|
708 |
*
|
|
709 |
* Don't synchronize (this) during a blocking read() since it
|
|
710 |
* protects data which is accessed on the write side as well.
|
|
711 |
*/
|
|
712 |
private void readRecord(InputRecord r, boolean needAppData)
|
|
713 |
throws IOException {
|
|
714 |
int state;
|
|
715 |
|
|
716 |
// readLock protects reading and processing of an InputRecord.
|
|
717 |
// It keeps the reading from sockInput and processing of the record
|
|
718 |
// atomic so that no two threads can be blocked on the
|
|
719 |
// read from the same input stream at the same time.
|
|
720 |
// This is required for example when a reader thread is
|
|
721 |
// blocked on the read and another thread is trying to
|
|
722 |
// close the socket. For a non-autoclose, layered socket,
|
|
723 |
// the thread performing the close needs to read the close_notify.
|
|
724 |
//
|
|
725 |
// Use readLock instead of 'this' for locking because
|
|
726 |
// 'this' also protects data accessed during writing.
|
|
727 |
synchronized (readLock) {
|
|
728 |
/*
|
|
729 |
* Read and handle records ... return application data
|
|
730 |
* ONLY if it's needed.
|
|
731 |
*/
|
|
732 |
|
|
733 |
while (((state = getConnectionState()) != cs_CLOSED) &&
|
|
734 |
(state != cs_ERROR) && (state != cs_APP_CLOSED)) {
|
|
735 |
/*
|
|
736 |
* Read a record ... maybe emitting an alert if we get a
|
|
737 |
* comprehensible but unsupported "hello" message during
|
|
738 |
* format checking (e.g. V2).
|
|
739 |
*/
|
|
740 |
try {
|
|
741 |
r.setAppDataValid(false);
|
|
742 |
r.read(sockInput, sockOutput);
|
|
743 |
} catch (SSLProtocolException e) {
|
|
744 |
try {
|
|
745 |
fatal(Alerts.alert_unexpected_message, e);
|
|
746 |
} catch (IOException x) {
|
|
747 |
// discard this exception
|
|
748 |
}
|
|
749 |
throw e;
|
|
750 |
} catch (EOFException eof) {
|
|
751 |
boolean handshaking = (getConnectionState() <= cs_HANDSHAKE);
|
|
752 |
boolean rethrow = requireCloseNotify || handshaking;
|
|
753 |
if ((debug != null) && Debug.isOn("ssl")) {
|
|
754 |
System.out.println(threadName() +
|
|
755 |
", received EOFException: "
|
|
756 |
+ (rethrow ? "error" : "ignored"));
|
|
757 |
}
|
|
758 |
if (rethrow) {
|
|
759 |
SSLException e;
|
|
760 |
if (handshaking) {
|
|
761 |
e = new SSLHandshakeException
|
|
762 |
("Remote host closed connection during handshake");
|
|
763 |
} else {
|
|
764 |
e = new SSLProtocolException
|
|
765 |
("Remote host closed connection incorrectly");
|
|
766 |
}
|
|
767 |
e.initCause(eof);
|
|
768 |
throw e;
|
|
769 |
} else {
|
|
770 |
// treat as if we had received a close_notify
|
|
771 |
closeInternal(false);
|
|
772 |
continue;
|
|
773 |
}
|
|
774 |
}
|
|
775 |
|
|
776 |
|
|
777 |
/*
|
|
778 |
* The basic SSLv3 record protection involves (optional)
|
|
779 |
* encryption for privacy, and an integrity check ensuring
|
|
780 |
* data origin authentication. We do them both here, and
|
|
781 |
* throw a fatal alert if the integrity check fails.
|
|
782 |
*/
|
|
783 |
try {
|
|
784 |
r.decrypt(readCipher);
|
|
785 |
} catch (BadPaddingException e) {
|
|
786 |
// RFC 2246 states that decryption_failed should be used
|
|
787 |
// for this purpose. However, that allows certain attacks,
|
|
788 |
// so we just send bad record MAC. We also need to make
|
|
789 |
// sure to always check the MAC to avoid a timing attack
|
|
790 |
// for the same issue. See paper by Vaudenay et al.
|
|
791 |
r.checkMAC(readMAC);
|
|
792 |
// use the same alert types as for MAC failure below
|
|
793 |
byte alertType = (r.contentType() == Record.ct_handshake)
|
|
794 |
? Alerts.alert_handshake_failure
|
|
795 |
: Alerts.alert_bad_record_mac;
|
|
796 |
fatal(alertType, "Invalid padding", e);
|
|
797 |
}
|
|
798 |
if (!r.checkMAC(readMAC)) {
|
|
799 |
if (r.contentType() == Record.ct_handshake) {
|
|
800 |
fatal(Alerts.alert_handshake_failure,
|
|
801 |
"bad handshake record MAC");
|
|
802 |
} else {
|
|
803 |
fatal(Alerts.alert_bad_record_mac, "bad record MAC");
|
|
804 |
}
|
|
805 |
}
|
|
806 |
|
|
807 |
// if (!r.decompress(c))
|
|
808 |
// fatal(Alerts.alert_decompression_failure,
|
|
809 |
// "decompression failure");
|
|
810 |
|
|
811 |
/*
|
|
812 |
* Process the record.
|
|
813 |
*/
|
|
814 |
synchronized (this) {
|
|
815 |
switch (r.contentType()) {
|
|
816 |
case Record.ct_handshake:
|
|
817 |
/*
|
|
818 |
* Handshake messages always go to a pending session
|
|
819 |
* handshaker ... if there isn't one, create one. This
|
|
820 |
* must work asynchronously, for renegotiation.
|
|
821 |
*
|
|
822 |
* NOTE that handshaking will either resume a session
|
|
823 |
* which was in the cache (and which might have other
|
|
824 |
* connections in it already), or else will start a new
|
|
825 |
* session (new keys exchanged) with just this connection
|
|
826 |
* in it.
|
|
827 |
*/
|
|
828 |
initHandshaker();
|
|
829 |
|
|
830 |
/*
|
|
831 |
* process the handshake record ... may contain just
|
|
832 |
* a partial handshake message or multiple messages.
|
|
833 |
*
|
|
834 |
* The handshaker state machine will ensure that it's
|
|
835 |
* a finished message.
|
|
836 |
*/
|
|
837 |
handshaker.process_record(r, expectingFinished);
|
|
838 |
expectingFinished = false;
|
|
839 |
|
|
840 |
if (handshaker.isDone()) {
|
|
841 |
sess = handshaker.getSession();
|
|
842 |
handshaker = null;
|
|
843 |
connectionState = cs_DATA;
|
|
844 |
|
|
845 |
//
|
|
846 |
// Tell folk about handshake completion, but do
|
|
847 |
// it in a separate thread.
|
|
848 |
//
|
|
849 |
if (handshakeListeners != null) {
|
|
850 |
HandshakeCompletedEvent event =
|
|
851 |
new HandshakeCompletedEvent(this, sess);
|
|
852 |
|
|
853 |
Thread t = new NotifyHandshakeThread(
|
|
854 |
handshakeListeners.entrySet(), event);
|
|
855 |
t.start();
|
|
856 |
}
|
|
857 |
}
|
|
858 |
if (needAppData || connectionState != cs_DATA) {
|
|
859 |
continue;
|
|
860 |
} else {
|
|
861 |
return;
|
|
862 |
}
|
|
863 |
|
|
864 |
case Record.ct_application_data:
|
|
865 |
// Pass this right back up to the application.
|
|
866 |
if (connectionState != cs_DATA
|
|
867 |
&& connectionState != cs_RENEGOTIATE
|
|
868 |
&& connectionState != cs_SENT_CLOSE) {
|
|
869 |
throw new SSLProtocolException(
|
|
870 |
"Data received in non-data state: " +
|
|
871 |
connectionState);
|
|
872 |
}
|
|
873 |
if (expectingFinished) {
|
|
874 |
throw new SSLProtocolException
|
|
875 |
("Expecting finished message, received data");
|
|
876 |
}
|
|
877 |
if (!needAppData) {
|
|
878 |
throw new SSLException("Discarding app data");
|
|
879 |
}
|
|
880 |
|
|
881 |
r.setAppDataValid(true);
|
|
882 |
return;
|
|
883 |
|
|
884 |
case Record.ct_alert:
|
|
885 |
recvAlert(r);
|
|
886 |
continue;
|
|
887 |
|
|
888 |
case Record.ct_change_cipher_spec:
|
|
889 |
if ((connectionState != cs_HANDSHAKE
|
|
890 |
&& connectionState != cs_RENEGOTIATE)
|
|
891 |
|| r.available() != 1
|
|
892 |
|| r.read() != 1) {
|
|
893 |
fatal(Alerts.alert_unexpected_message,
|
|
894 |
"illegal change cipher spec msg, state = "
|
|
895 |
+ connectionState);
|
|
896 |
}
|
|
897 |
|
|
898 |
//
|
|
899 |
// The first message after a change_cipher_spec
|
|
900 |
// record MUST be a "Finished" handshake record,
|
|
901 |
// else it's a protocol violation. We force this
|
|
902 |
// to be checked by a minor tweak to the state
|
|
903 |
// machine.
|
|
904 |
//
|
|
905 |
changeReadCiphers();
|
|
906 |
// next message MUST be a finished message
|
|
907 |
expectingFinished = true;
|
|
908 |
continue;
|
|
909 |
|
|
910 |
default:
|
|
911 |
//
|
|
912 |
// TLS requires that unrecognized records be ignored.
|
|
913 |
//
|
|
914 |
if (debug != null && Debug.isOn("ssl")) {
|
|
915 |
System.out.println(threadName() +
|
|
916 |
", Received record type: "
|
|
917 |
+ r.contentType());
|
|
918 |
}
|
|
919 |
continue;
|
|
920 |
} // switch
|
|
921 |
} // synchronized (this)
|
|
922 |
}
|
|
923 |
|
|
924 |
//
|
|
925 |
// couldn't read, due to some kind of error
|
|
926 |
//
|
|
927 |
r.close();
|
|
928 |
return;
|
|
929 |
} // synchronized (readLock)
|
|
930 |
}
|
|
931 |
|
|
932 |
//
|
|
933 |
// HANDSHAKE RELATED CODE
|
|
934 |
//
|
|
935 |
|
|
936 |
/**
|
|
937 |
* Return the AppInputStream. For use by Handshaker only.
|
|
938 |
*/
|
|
939 |
AppInputStream getAppInputStream() {
|
|
940 |
return input;
|
|
941 |
}
|
|
942 |
|
|
943 |
/**
|
|
944 |
* Initialize and get the server handshaker. Used by SSLServerSocketImpl
|
|
945 |
* for the ciphersuite availability test *only*.
|
|
946 |
*/
|
|
947 |
ServerHandshaker getServerHandshaker() throws SSLException {
|
|
948 |
initHandshaker();
|
|
949 |
return (ServerHandshaker)handshaker;
|
|
950 |
}
|
|
951 |
|
|
952 |
/**
|
|
953 |
* Initialize the handshaker object. This means:
|
|
954 |
*
|
|
955 |
* . if a handshake is already in progress (state is cs_HANDSHAKE
|
|
956 |
* or cs_RENEGOTIATE), do nothing and return
|
|
957 |
*
|
|
958 |
* . if the socket is already closed, throw an Exception (internal error)
|
|
959 |
*
|
|
960 |
* . otherwise (cs_START or cs_DATA), create the appropriate handshaker
|
|
961 |
* object, initialize it, and advance the connection state (to
|
|
962 |
* cs_HANDSHAKE or cs_RENEGOTIATE, respectively).
|
|
963 |
*
|
|
964 |
* This method is called right after a new socket is created, when
|
|
965 |
* starting renegotiation, or when changing client/ server mode of the
|
|
966 |
* socket.
|
|
967 |
*/
|
|
968 |
private void initHandshaker() {
|
|
969 |
switch (connectionState) {
|
|
970 |
|
|
971 |
//
|
|
972 |
// Starting a new handshake.
|
|
973 |
//
|
|
974 |
case cs_START:
|
|
975 |
case cs_DATA:
|
|
976 |
break;
|
|
977 |
|
|
978 |
//
|
|
979 |
// We're already in the middle of a handshake.
|
|
980 |
//
|
|
981 |
case cs_HANDSHAKE:
|
|
982 |
case cs_RENEGOTIATE:
|
|
983 |
return;
|
|
984 |
|
|
985 |
//
|
|
986 |
// Anyone allowed to call this routine is required to
|
|
987 |
// do so ONLY if the connection state is reasonable...
|
|
988 |
//
|
|
989 |
default:
|
|
990 |
throw new IllegalStateException("Internal error");
|
|
991 |
}
|
|
992 |
|
|
993 |
// state is either cs_START or cs_DATA
|
|
994 |
if (connectionState == cs_START) {
|
|
995 |
connectionState = cs_HANDSHAKE;
|
|
996 |
} else { // cs_DATA
|
|
997 |
connectionState = cs_RENEGOTIATE;
|
|
998 |
}
|
|
999 |
if (roleIsServer) {
|
|
1000 |
handshaker = new ServerHandshaker
|
|
1001 |
(this, sslContext, enabledProtocols, doClientAuth);
|
|
1002 |
} else {
|
|
1003 |
handshaker = new ClientHandshaker
|
|
1004 |
(this, sslContext, enabledProtocols);
|
|
1005 |
}
|
|
1006 |
handshaker.enabledCipherSuites = enabledCipherSuites;
|
|
1007 |
handshaker.setEnableSessionCreation(enableSessionCreation);
|
|
1008 |
if (connectionState == cs_RENEGOTIATE) {
|
|
1009 |
// don't use SSLv2Hello when renegotiating
|
|
1010 |
handshaker.output.r.setHelloVersion(protocolVersion);
|
|
1011 |
}
|
|
1012 |
}
|
|
1013 |
|
|
1014 |
/**
|
|
1015 |
* Synchronously perform the initial handshake.
|
|
1016 |
*
|
|
1017 |
* If the handshake is already in progress, this method blocks until it
|
|
1018 |
* is completed. If the initial handshake has already been completed,
|
|
1019 |
* it returns immediately.
|
|
1020 |
*/
|
|
1021 |
private void performInitialHandshake() throws IOException {
|
|
1022 |
// use handshakeLock and the state check to make sure only
|
|
1023 |
// one thread performs the handshake
|
|
1024 |
synchronized (handshakeLock) {
|
|
1025 |
if (getConnectionState() == cs_HANDSHAKE) {
|
|
1026 |
/*
|
|
1027 |
* All initial handshaking goes through this
|
|
1028 |
* InputRecord until we have a valid SSL connection.
|
|
1029 |
* Once initial handshaking is finished, AppInputStream's
|
|
1030 |
* InputRecord can handle any future renegotiation.
|
|
1031 |
*
|
|
1032 |
* Keep this local so that it goes out of scope and is
|
|
1033 |
* eventually GC'd.
|
|
1034 |
*/
|
|
1035 |
if (inrec == null) {
|
|
1036 |
inrec = new InputRecord();
|
|
1037 |
|
|
1038 |
/*
|
|
1039 |
* Grab the characteristics already assigned to
|
|
1040 |
* AppInputStream's InputRecord. Enable checking for
|
|
1041 |
* SSLv2 hellos on this first handshake.
|
|
1042 |
*/
|
|
1043 |
inrec.setHandshakeHash(input.r.getHandshakeHash());
|
|
1044 |
inrec.setHelloVersion(input.r.getHelloVersion());
|
|
1045 |
inrec.enableFormatChecks();
|
|
1046 |
}
|
|
1047 |
|
|
1048 |
kickstartHandshake();
|
|
1049 |
readRecord(inrec, false);
|
|
1050 |
inrec = null;
|
|
1051 |
}
|
|
1052 |
}
|
|
1053 |
}
|
|
1054 |
|
|
1055 |
/**
|
|
1056 |
* Starts an SSL handshake on this connection.
|
|
1057 |
*/
|
|
1058 |
public void startHandshake() throws IOException {
|
|
1059 |
// start an ssl handshake that could be resumed from timeout exception
|
|
1060 |
startHandshake(true);
|
|
1061 |
}
|
|
1062 |
|
|
1063 |
/**
|
|
1064 |
* Starts an ssl handshake on this connection.
|
|
1065 |
*
|
|
1066 |
* @param resumable indicates the handshake process is resumable from a
|
|
1067 |
* certain exception. If <code>resumable</code>, the socket will
|
|
1068 |
* be reserved for exceptions like timeout; otherwise, the socket
|
|
1069 |
* will be closed, no further communications could be done.
|
|
1070 |
*/
|
|
1071 |
private void startHandshake(boolean resumable) throws IOException {
|
|
1072 |
checkWrite();
|
|
1073 |
try {
|
|
1074 |
if (getConnectionState() == cs_HANDSHAKE) {
|
|
1075 |
// do initial handshake
|
|
1076 |
performInitialHandshake();
|
|
1077 |
} else {
|
|
1078 |
// start renegotiation
|
|
1079 |
kickstartHandshake();
|
|
1080 |
}
|
|
1081 |
} catch (Exception e) {
|
|
1082 |
// shutdown and rethrow (wrapped) exception as appropriate
|
|
1083 |
handleException(e, resumable);
|
|
1084 |
}
|
|
1085 |
}
|
|
1086 |
|
|
1087 |
/**
|
|
1088 |
* Kickstart the handshake if it is not already in progress.
|
|
1089 |
* This means:
|
|
1090 |
*
|
|
1091 |
* . if handshaking is already underway, do nothing and return
|
|
1092 |
*
|
|
1093 |
* . if the socket is not connected or already closed, throw an
|
|
1094 |
* Exception.
|
|
1095 |
*
|
|
1096 |
* . otherwise, call initHandshake() to initialize the handshaker
|
|
1097 |
* object and progress the state. Then, send the initial
|
|
1098 |
* handshaking message if appropriate (always on clients and
|
|
1099 |
* on servers when renegotiating).
|
|
1100 |
*/
|
|
1101 |
private synchronized void kickstartHandshake() throws IOException {
|
|
1102 |
switch (connectionState) {
|
|
1103 |
|
|
1104 |
case cs_HANDSHAKE:
|
|
1105 |
// handshaker already setup, proceed
|
|
1106 |
break;
|
|
1107 |
|
|
1108 |
case cs_DATA:
|
|
1109 |
// initialize the handshaker, move to cs_RENEGOTIATE
|
|
1110 |
initHandshaker();
|
|
1111 |
break;
|
|
1112 |
|
|
1113 |
case cs_RENEGOTIATE:
|
|
1114 |
// handshaking already in progress, return
|
|
1115 |
return;
|
|
1116 |
|
|
1117 |
/*
|
|
1118 |
* The only way to get a socket in the state is when
|
|
1119 |
* you have an unconnected socket.
|
|
1120 |
*/
|
|
1121 |
case cs_START:
|
|
1122 |
throw new SocketException(
|
|
1123 |
"handshaking attempted on unconnected socket");
|
|
1124 |
|
|
1125 |
default:
|
|
1126 |
throw new SocketException("connection is closed");
|
|
1127 |
}
|
|
1128 |
|
|
1129 |
//
|
|
1130 |
// Kickstart handshake state machine if we need to ...
|
|
1131 |
//
|
|
1132 |
// Note that handshaker.kickstart() writes the message
|
|
1133 |
// to its HandshakeOutStream, which calls back into
|
|
1134 |
// SSLSocketImpl.writeRecord() to send it.
|
|
1135 |
//
|
|
1136 |
if (!handshaker.started()) {
|
|
1137 |
if (handshaker instanceof ClientHandshaker) {
|
|
1138 |
// send client hello
|
|
1139 |
handshaker.kickstart();
|
|
1140 |
} else {
|
|
1141 |
if (connectionState == cs_HANDSHAKE) {
|
|
1142 |
// initial handshake, no kickstart message to send
|
|
1143 |
} else {
|
|
1144 |
// we want to renegotiate, send hello request
|
|
1145 |
handshaker.kickstart();
|
|
1146 |
// hello request is not included in the handshake
|
|
1147 |
// hashes, reset them
|
|
1148 |
handshaker.handshakeHash.reset();
|
|
1149 |
}
|
|
1150 |
}
|
|
1151 |
}
|
|
1152 |
}
|
|
1153 |
|
|
1154 |
//
|
|
1155 |
// CLOSURE RELATED CALLS
|
|
1156 |
//
|
|
1157 |
|
|
1158 |
/**
|
|
1159 |
* Return whether the socket has been explicitly closed by the application.
|
|
1160 |
*/
|
|
1161 |
public boolean isClosed() {
|
|
1162 |
return getConnectionState() == cs_APP_CLOSED;
|
|
1163 |
}
|
|
1164 |
|
|
1165 |
/**
|
|
1166 |
* Return whether we have reached end-of-file.
|
|
1167 |
*
|
|
1168 |
* If the socket is not connected, has been shutdown because of an error
|
|
1169 |
* or has been closed, throw an Exception.
|
|
1170 |
*/
|
|
1171 |
boolean checkEOF() throws IOException {
|
|
1172 |
switch (getConnectionState()) {
|
|
1173 |
case cs_START:
|
|
1174 |
throw new SocketException("Socket is not connected");
|
|
1175 |
|
|
1176 |
case cs_HANDSHAKE:
|
|
1177 |
case cs_DATA:
|
|
1178 |
case cs_RENEGOTIATE:
|
|
1179 |
case cs_SENT_CLOSE:
|
|
1180 |
return false;
|
|
1181 |
|
|
1182 |
case cs_APP_CLOSED:
|
|
1183 |
throw new SocketException("Socket is closed");
|
|
1184 |
|
|
1185 |
case cs_ERROR:
|
|
1186 |
case cs_CLOSED:
|
|
1187 |
default:
|
|
1188 |
// either closed because of error, or normal EOF
|
|
1189 |
if (closeReason == null) {
|
|
1190 |
return true;
|
|
1191 |
}
|
|
1192 |
IOException e = new SSLException
|
|
1193 |
("Connection has been shutdown: " + closeReason);
|
|
1194 |
e.initCause(closeReason);
|
|
1195 |
throw e;
|
|
1196 |
|
|
1197 |
}
|
|
1198 |
}
|
|
1199 |
|
|
1200 |
/**
|
|
1201 |
* Check if we can write data to this socket. If not, throw an IOException.
|
|
1202 |
*/
|
|
1203 |
void checkWrite() throws IOException {
|
|
1204 |
if (checkEOF() || (getConnectionState() == cs_SENT_CLOSE)) {
|
|
1205 |
// we are at EOF, write must throw Exception
|
|
1206 |
throw new SocketException("Connection closed by remote host");
|
|
1207 |
}
|
|
1208 |
}
|
|
1209 |
|
|
1210 |
private void closeSocket() throws IOException {
|
|
1211 |
if ((debug != null) && Debug.isOn("ssl")) {
|
|
1212 |
System.out.println(threadName() + ", called closeSocket()");
|
|
1213 |
}
|
|
1214 |
if (self == this) {
|
|
1215 |
super.close();
|
|
1216 |
} else {
|
|
1217 |
self.close();
|
|
1218 |
}
|
|
1219 |
}
|
|
1220 |
|
|
1221 |
/*
|
|
1222 |
* Closing the connection is tricky ... we can't officially close the
|
|
1223 |
* connection until we know the other end is ready to go away too,
|
|
1224 |
* and if ever the connection gets aborted we must forget session
|
|
1225 |
* state (it becomes invalid).
|
|
1226 |
*/
|
|
1227 |
|
|
1228 |
/**
|
|
1229 |
* Closes the SSL connection. SSL includes an application level
|
|
1230 |
* shutdown handshake; you should close SSL sockets explicitly
|
|
1231 |
* rather than leaving it for finalization, so that your remote
|
|
1232 |
* peer does not experience a protocol error.
|
|
1233 |
*/
|
|
1234 |
public void close() throws IOException {
|
|
1235 |
if ((debug != null) && Debug.isOn("ssl")) {
|
|
1236 |
System.out.println(threadName() + ", called close()");
|
|
1237 |
}
|
|
1238 |
closeInternal(true); // caller is initiating close
|
|
1239 |
setConnectionState(cs_APP_CLOSED);
|
|
1240 |
}
|
|
1241 |
|
|
1242 |
/**
|
|
1243 |
* Don't synchronize the whole method because waitForClose()
|
|
1244 |
* (which calls readRecord()) might be called.
|
|
1245 |
*
|
|
1246 |
* @param selfInitiated Indicates which party initiated the close.
|
|
1247 |
* If selfInitiated, this side is initiating a close; for layered and
|
|
1248 |
* non-autoclose socket, wait for close_notify response.
|
|
1249 |
* If !selfInitiated, peer sent close_notify; we reciprocate but
|
|
1250 |
* no need to wait for response.
|
|
1251 |
*/
|
|
1252 |
private void closeInternal(boolean selfInitiated) throws IOException {
|
|
1253 |
if ((debug != null) && Debug.isOn("ssl")) {
|
|
1254 |
System.out.println(threadName() + ", called closeInternal("
|
|
1255 |
+ selfInitiated + ")");
|
|
1256 |
}
|
|
1257 |
|
|
1258 |
int state = getConnectionState();
|
|
1259 |
try {
|
|
1260 |
switch (state) {
|
|
1261 |
/*
|
|
1262 |
* java.net code sometimes closes sockets "early", when
|
|
1263 |
* we can't actually do I/O on them.
|
|
1264 |
*/
|
|
1265 |
case cs_START:
|
|
1266 |
break;
|
|
1267 |
|
|
1268 |
/*
|
|
1269 |
* If we're closing down due to error, we already sent (or else
|
|
1270 |
* received) the fatal alert ... no niceties, blow the connection
|
|
1271 |
* away as quickly as possible (even if we didn't allocate the
|
|
1272 |
* socket ourselves; it's unusable, regardless).
|
|
1273 |
*/
|
|
1274 |
case cs_ERROR:
|
|
1275 |
closeSocket();
|
|
1276 |
break;
|
|
1277 |
|
|
1278 |
/*
|
|
1279 |
* Sometimes close() gets called more than once.
|
|
1280 |
*/
|
|
1281 |
case cs_CLOSED:
|
|
1282 |
case cs_APP_CLOSED:
|
|
1283 |
break;
|
|
1284 |
|
|
1285 |
/*
|
|
1286 |
* Otherwise we indicate clean termination.
|
|
1287 |
*/
|
|
1288 |
// case cs_HANDSHAKE:
|
|
1289 |
// case cs_DATA:
|
|
1290 |
// case cs_RENEGOTIATE:
|
|
1291 |
// case cs_SENT_CLOSE:
|
|
1292 |
default:
|
|
1293 |
synchronized (this) {
|
|
1294 |
if (((state = getConnectionState()) == cs_CLOSED) ||
|
|
1295 |
(state == cs_ERROR) || (state == cs_APP_CLOSED)) {
|
|
1296 |
return; // connection was closed while we waited
|
|
1297 |
}
|
|
1298 |
if (state != cs_SENT_CLOSE) {
|
|
1299 |
warning(Alerts.alert_close_notify);
|
|
1300 |
connectionState = cs_SENT_CLOSE;
|
|
1301 |
}
|
|
1302 |
}
|
|
1303 |
// If state was cs_SENT_CLOSE before, we don't do the actual
|
|
1304 |
// closing since it is already in progress.
|
|
1305 |
if (state == cs_SENT_CLOSE) {
|
|
1306 |
if (debug != null && Debug.isOn("ssl")) {
|
|
1307 |
System.out.println(threadName() +
|
|
1308 |
", close invoked again; state = " +
|
|
1309 |
getConnectionState());
|
|
1310 |
}
|
|
1311 |
if (selfInitiated == false) {
|
|
1312 |
// We were called because a close_notify message was
|
|
1313 |
// received. This may be due to another thread calling
|
|
1314 |
// read() or due to our call to waitForClose() below.
|
|
1315 |
// In either case, just return.
|
|
1316 |
return;
|
|
1317 |
}
|
|
1318 |
// Another thread explicitly called close(). We need to
|
|
1319 |
// wait for the closing to complete before returning.
|
|
1320 |
synchronized (this) {
|
|
1321 |
while (connectionState < cs_CLOSED) {
|
|
1322 |
try {
|
|
1323 |
this.wait();
|
|
1324 |
} catch (InterruptedException e) {
|
|
1325 |
// ignore
|
|
1326 |
}
|
|
1327 |
}
|
|
1328 |
}
|
|
1329 |
if ((debug != null) && Debug.isOn("ssl")) {
|
|
1330 |
System.out.println(threadName() +
|
|
1331 |
", after primary close; state = " +
|
|
1332 |
getConnectionState());
|
|
1333 |
}
|
|
1334 |
return;
|
|
1335 |
}
|
|
1336 |
|
|
1337 |
if (self == this) {
|
|
1338 |
super.close();
|
|
1339 |
} else if (autoClose) {
|
|
1340 |
self.close();
|
|
1341 |
} else if (selfInitiated) {
|
|
1342 |
// layered && non-autoclose
|
|
1343 |
// read close_notify alert to clear input stream
|
|
1344 |
waitForClose(false);
|
|
1345 |
}
|
|
1346 |
|
|
1347 |
// state will be set to cs_CLOSED in the finally block below
|
|
1348 |
|
|
1349 |
break;
|
|
1350 |
}
|
|
1351 |
} finally {
|
|
1352 |
synchronized (this) {
|
|
1353 |
// Upon exit from this method, the state is always >= cs_CLOSED
|
|
1354 |
connectionState = (connectionState == cs_APP_CLOSED)
|
|
1355 |
? cs_APP_CLOSED : cs_CLOSED;
|
|
1356 |
// notify any threads waiting for the closing to finish
|
|
1357 |
this.notifyAll();
|
|
1358 |
}
|
|
1359 |
}
|
|
1360 |
}
|
|
1361 |
|
|
1362 |
/**
|
|
1363 |
* Reads a close_notify or a fatal alert from the input stream.
|
|
1364 |
* Keep reading records until we get a close_notify or until
|
|
1365 |
* the connection is otherwise closed. The close_notify or alert
|
|
1366 |
* might be read by another reader,
|
|
1367 |
* which will then process the close and set the connection state.
|
|
1368 |
*/
|
|
1369 |
void waitForClose(boolean rethrow) throws IOException {
|
|
1370 |
if (debug != null && Debug.isOn("ssl")) {
|
|
1371 |
System.out.println(threadName() +
|
|
1372 |
", waiting for close_notify or alert: state "
|
|
1373 |
+ getConnectionState());
|
|
1374 |
}
|
|
1375 |
|
|
1376 |
try {
|
|
1377 |
int state;
|
|
1378 |
|
|
1379 |
while (((state = getConnectionState()) != cs_CLOSED) &&
|
|
1380 |
(state != cs_ERROR) && (state != cs_APP_CLOSED)) {
|
|
1381 |
// create the InputRecord if it isn't intialized.
|
|
1382 |
if (inrec == null) {
|
|
1383 |
inrec = new InputRecord();
|
|
1384 |
}
|
|
1385 |
|
|
1386 |
// Ask for app data and then throw it away
|
|
1387 |
try {
|
|
1388 |
readRecord(inrec, true);
|
|
1389 |
} catch (SocketTimeoutException e) {
|
|
1390 |
// if time out, ignore the exception and continue
|
|
1391 |
}
|
|
1392 |
}
|
|
1393 |
inrec = null;
|
|
1394 |
} catch (IOException e) {
|
|
1395 |
if (debug != null && Debug.isOn("ssl")) {
|
|
1396 |
System.out.println(threadName() +
|
|
1397 |
", Exception while waiting for close " +e);
|
|
1398 |
}
|
|
1399 |
if (rethrow) {
|
|
1400 |
throw e; // pass exception up
|
|
1401 |
}
|
|
1402 |
}
|
|
1403 |
}
|
|
1404 |
|
|
1405 |
//
|
|
1406 |
// EXCEPTION AND ALERT HANDLING
|
|
1407 |
//
|
|
1408 |
|
|
1409 |
/**
|
|
1410 |
* Handle an exception. This method is called by top level exception
|
|
1411 |
* handlers (in read(), write()) to make sure we always shutdown the
|
|
1412 |
* connection correctly and do not pass runtime exception to the
|
|
1413 |
* application.
|
|
1414 |
*/
|
|
1415 |
void handleException(Exception e) throws IOException {
|
|
1416 |
handleException(e, true);
|
|
1417 |
}
|
|
1418 |
|
|
1419 |
/**
|
|
1420 |
* Handle an exception. This method is called by top level exception
|
|
1421 |
* handlers (in read(), write(), startHandshake()) to make sure we
|
|
1422 |
* always shutdown the connection correctly and do not pass runtime
|
|
1423 |
* exception to the application.
|
|
1424 |
*
|
|
1425 |
* This method never returns normally, it always throws an IOException.
|
|
1426 |
*
|
|
1427 |
* We first check if the socket has already been shutdown because of an
|
|
1428 |
* error. If so, we just rethrow the exception. If the socket has not
|
|
1429 |
* been shutdown, we sent a fatal alert and remember the exception.
|
|
1430 |
*
|
|
1431 |
* @param e the Exception
|
|
1432 |
* @param resumable indicates the caller process is resumable from the
|
|
1433 |
* exception. If <code>resumable</code>, the socket will be
|
|
1434 |
* reserved for exceptions like timeout; otherwise, the socket
|
|
1435 |
* will be closed, no further communications could be done.
|
|
1436 |
*/
|
|
1437 |
synchronized private void handleException(Exception e, boolean resumable)
|
|
1438 |
throws IOException {
|
|
1439 |
if ((debug != null) && Debug.isOn("ssl")) {
|
|
1440 |
System.out.println(threadName()
|
|
1441 |
+ ", handling exception: " + e.toString());
|
|
1442 |
}
|
|
1443 |
|
|
1444 |
// don't close the Socket in case of timeouts or interrupts if
|
|
1445 |
// the process is resumable.
|
|
1446 |
if (e instanceof InterruptedIOException && resumable) {
|
|
1447 |
throw (IOException)e;
|
|
1448 |
}
|
|
1449 |
|
|
1450 |
// if we've already shutdown because of an error,
|
|
1451 |
// there is nothing to do except rethrow the exception
|
|
1452 |
if (closeReason != null) {
|
|
1453 |
if (e instanceof IOException) { // includes SSLException
|
|
1454 |
throw (IOException)e;
|
|
1455 |
} else {
|
|
1456 |
// this is odd, not an IOException.
|
|
1457 |
// normally, this should not happen
|
|
1458 |
// if closeReason has been already been set
|
|
1459 |
throw Alerts.getSSLException(Alerts.alert_internal_error, e,
|
|
1460 |
"Unexpected exception");
|
|
1461 |
}
|
|
1462 |
}
|
|
1463 |
|
|
1464 |
// need to perform error shutdown
|
|
1465 |
boolean isSSLException = (e instanceof SSLException);
|
|
1466 |
if ((isSSLException == false) && (e instanceof IOException)) {
|
|
1467 |
// IOException from the socket
|
|
1468 |
// this means the TCP connection is already dead
|
|
1469 |
// we call fatal just to set the error status
|
|
1470 |
try {
|
|
1471 |
fatal(Alerts.alert_unexpected_message, e);
|
|
1472 |
} catch (IOException ee) {
|
|
1473 |
// ignore (IOException wrapped in SSLException)
|
|
1474 |
}
|
|
1475 |
// rethrow original IOException
|
|
1476 |
throw (IOException)e;
|
|
1477 |
}
|
|
1478 |
|
|
1479 |
// must be SSLException or RuntimeException
|
|
1480 |
byte alertType;
|
|
1481 |
if (isSSLException) {
|
|
1482 |
if (e instanceof SSLHandshakeException) {
|
|
1483 |
alertType = Alerts.alert_handshake_failure;
|
|
1484 |
} else {
|
|
1485 |
alertType = Alerts.alert_unexpected_message;
|
|
1486 |
}
|
|
1487 |
} else {
|
|
1488 |
alertType = Alerts.alert_internal_error;
|
|
1489 |
}
|
|
1490 |
fatal(alertType, e);
|
|
1491 |
}
|
|
1492 |
|
|
1493 |
/*
|
|
1494 |
* Send a warning alert.
|
|
1495 |
*/
|
|
1496 |
void warning(byte description) {
|
|
1497 |
sendAlert(Alerts.alert_warning, description);
|
|
1498 |
}
|
|
1499 |
|
|
1500 |
synchronized void fatal(byte description, String diagnostic)
|
|
1501 |
throws IOException {
|
|
1502 |
fatal(description, diagnostic, null);
|
|
1503 |
}
|
|
1504 |
|
|
1505 |
synchronized void fatal(byte description, Throwable cause)
|
|
1506 |
throws IOException {
|
|
1507 |
fatal(description, null, cause);
|
|
1508 |
}
|
|
1509 |
|
|
1510 |
/*
|
|
1511 |
* Send a fatal alert, and throw an exception so that callers will
|
|
1512 |
* need to stand on their heads to accidentally continue processing.
|
|
1513 |
*/
|
|
1514 |
synchronized void fatal(byte description, String diagnostic,
|
|
1515 |
Throwable cause) throws IOException {
|
|
1516 |
if ((input != null) && (input.r != null)) {
|
|
1517 |
input.r.close();
|
|
1518 |
}
|
|
1519 |
sess.invalidate();
|
|
1520 |
|
|
1521 |
int oldState = connectionState;
|
|
1522 |
connectionState = cs_ERROR;
|
|
1523 |
|
|
1524 |
/*
|
|
1525 |
* Has there been an error received yet? If not, remember it.
|
|
1526 |
* By RFC 2246, we don't bother waiting for a response.
|
|
1527 |
* Fatal errors require immediate shutdown.
|
|
1528 |
*/
|
|
1529 |
if (closeReason == null) {
|
|
1530 |
/*
|
|
1531 |
* Try to clear the kernel buffer to avoid TCP connection resets.
|
|
1532 |
*/
|
|
1533 |
if (oldState == cs_HANDSHAKE) {
|
|
1534 |
sockInput.skip(sockInput.available());
|
|
1535 |
}
|
|
1536 |
sendAlert(Alerts.alert_fatal, description);
|
|
1537 |
if (cause instanceof SSLException) { // only true if != null
|
|
1538 |
closeReason = (SSLException)cause;
|
|
1539 |
} else {
|
|
1540 |
closeReason =
|
|
1541 |
Alerts.getSSLException(description, cause, diagnostic);
|
|
1542 |
}
|
|
1543 |
}
|
|
1544 |
|
|
1545 |
/*
|
|
1546 |
* Clean up our side.
|
|
1547 |
*/
|
|
1548 |
closeSocket();
|
|
1549 |
connectionState = (oldState == cs_APP_CLOSED) ? cs_APP_CLOSED
|
|
1550 |
: cs_CLOSED;
|
|
1551 |
throw closeReason;
|
|
1552 |
}
|
|
1553 |
|
|
1554 |
|
|
1555 |
/*
|
|
1556 |
* Process an incoming alert ... caller must already have synchronized
|
|
1557 |
* access to "this".
|
|
1558 |
*/
|
|
1559 |
private void recvAlert(InputRecord r) throws IOException {
|
|
1560 |
byte level = (byte)r.read();
|
|
1561 |
byte description = (byte)r.read();
|
|
1562 |
if (description == -1) { // check for short message
|
|
1563 |
fatal(Alerts.alert_illegal_parameter, "Short alert message");
|
|
1564 |
}
|
|
1565 |
|
|
1566 |
if (debug != null && (Debug.isOn("record") ||
|
|
1567 |
Debug.isOn("handshake"))) {
|
|
1568 |
synchronized (System.out) {
|
|
1569 |
System.out.print(threadName());
|
|
1570 |
System.out.print(", RECV " + protocolVersion + " ALERT: ");
|
|
1571 |
if (level == Alerts.alert_fatal) {
|
|
1572 |
System.out.print("fatal, ");
|
|
1573 |
} else if (level == Alerts.alert_warning) {
|
|
1574 |
System.out.print("warning, ");
|
|
1575 |
} else {
|
|
1576 |
System.out.print("<level " + (0x0ff & level) + ">, ");
|
|
1577 |
}
|
|
1578 |
System.out.println(Alerts.alertDescription(description));
|
|
1579 |
}
|
|
1580 |
}
|
|
1581 |
|
|
1582 |
if (level == Alerts.alert_warning) {
|
|
1583 |
if (description == Alerts.alert_close_notify) {
|
|
1584 |
if (connectionState == cs_HANDSHAKE) {
|
|
1585 |
fatal(Alerts.alert_unexpected_message,
|
|
1586 |
"Received close_notify during handshake");
|
|
1587 |
} else {
|
|
1588 |
closeInternal(false); // reply to close
|
|
1589 |
}
|
|
1590 |
} else {
|
|
1591 |
|
|
1592 |
//
|
|
1593 |
// The other legal warnings relate to certificates,
|
|
1594 |
// e.g. no_certificate, bad_certificate, etc; these
|
|
1595 |
// are important to the handshaking code, which can
|
|
1596 |
// also handle illegal protocol alerts if needed.
|
|
1597 |
//
|
|
1598 |
if (handshaker != null) {
|
|
1599 |
handshaker.handshakeAlert(description);
|
|
1600 |
}
|
|
1601 |
}
|
|
1602 |
} else { // fatal or unknown level
|
|
1603 |
String reason = "Received fatal alert: "
|
|
1604 |
+ Alerts.alertDescription(description);
|
|
1605 |
if (closeReason == null) {
|
|
1606 |
closeReason = Alerts.getSSLException(description, reason);
|
|
1607 |
}
|
|
1608 |
fatal(Alerts.alert_unexpected_message, reason);
|
|
1609 |
}
|
|
1610 |
}
|
|
1611 |
|
|
1612 |
|
|
1613 |
/*
|
|
1614 |
* Emit alerts. Caller must have synchronized with "this".
|
|
1615 |
*/
|
|
1616 |
private void sendAlert(byte level, byte description) {
|
|
1617 |
if (connectionState >= cs_CLOSED) {
|
|
1618 |
return;
|
|
1619 |
}
|
|
1620 |
|
|
1621 |
OutputRecord r = new OutputRecord(Record.ct_alert);
|
|
1622 |
r.setVersion(protocolVersion);
|
|
1623 |
|
|
1624 |
boolean useDebug = debug != null && Debug.isOn("ssl");
|
|
1625 |
if (useDebug) {
|
|
1626 |
synchronized (System.out) {
|
|
1627 |
System.out.print(threadName());
|
|
1628 |
System.out.print(", SEND " + protocolVersion + " ALERT: ");
|
|
1629 |
if (level == Alerts.alert_fatal) {
|
|
1630 |
System.out.print("fatal, ");
|
|
1631 |
} else if (level == Alerts.alert_warning) {
|
|
1632 |
System.out.print("warning, ");
|
|
1633 |
} else {
|
|
1634 |
System.out.print("<level = " + (0x0ff & level) + ">, ");
|
|
1635 |
}
|
|
1636 |
System.out.println("description = "
|
|
1637 |
+ Alerts.alertDescription(description));
|
|
1638 |
}
|
|
1639 |
}
|
|
1640 |
|
|
1641 |
r.write(level);
|
|
1642 |
r.write(description);
|
|
1643 |
try {
|
|
1644 |
writeRecord(r);
|
|
1645 |
} catch (IOException e) {
|
|
1646 |
if (useDebug) {
|
|
1647 |
System.out.println(threadName() +
|
|
1648 |
", Exception sending alert: " + e);
|
|
1649 |
}
|
|
1650 |
}
|
|
1651 |
}
|
|
1652 |
|
|
1653 |
//
|
|
1654 |
// VARIOUS OTHER METHODS
|
|
1655 |
//
|
|
1656 |
|
|
1657 |
/*
|
|
1658 |
* When a connection finishes handshaking by enabling use of a newly
|
|
1659 |
* negotiated session, each end learns about it in two halves (read,
|
|
1660 |
* and write). When both read and write ciphers have changed, and the
|
|
1661 |
* last handshake message has been read, the connection has joined
|
|
1662 |
* (rejoined) the new session.
|
|
1663 |
*
|
|
1664 |
* NOTE: The SSLv3 spec is rather unclear on the concepts here.
|
|
1665 |
* Sessions don't change once they're established (including cipher
|
|
1666 |
* suite and master secret) but connections can join them (and leave
|
|
1667 |
* them). They're created by handshaking, though sometime handshaking
|
|
1668 |
* causes connections to join up with pre-established sessions.
|
|
1669 |
*/
|
|
1670 |
private void changeReadCiphers() throws SSLException {
|
|
1671 |
if (connectionState != cs_HANDSHAKE
|
|
1672 |
&& connectionState != cs_RENEGOTIATE) {
|
|
1673 |
throw new SSLProtocolException(
|
|
1674 |
"State error, change cipher specs");
|
|
1675 |
}
|
|
1676 |
|
|
1677 |
// ... create decompressor
|
|
1678 |
|
|
1679 |
try {
|
|
1680 |
readCipher = handshaker.newReadCipher();
|
|
1681 |
readMAC = handshaker.newReadMAC();
|
|
1682 |
} catch (GeneralSecurityException e) {
|
|
1683 |
// "can't happen"
|
|
1684 |
throw (SSLException)new SSLException
|
|
1685 |
("Algorithm missing: ").initCause(e);
|
|
1686 |
}
|
|
1687 |
}
|
|
1688 |
|
|
1689 |
// used by Handshaker
|
|
1690 |
void changeWriteCiphers() throws SSLException {
|
|
1691 |
if (connectionState != cs_HANDSHAKE
|
|
1692 |
&& connectionState != cs_RENEGOTIATE) {
|
|
1693 |
throw new SSLProtocolException(
|
|
1694 |
"State error, change cipher specs");
|
|
1695 |
}
|
|
1696 |
|
|
1697 |
// ... create compressor
|
|
1698 |
|
|
1699 |
try {
|
|
1700 |
writeCipher = handshaker.newWriteCipher();
|
|
1701 |
writeMAC = handshaker.newWriteMAC();
|
|
1702 |
} catch (GeneralSecurityException e) {
|
|
1703 |
// "can't happen"
|
|
1704 |
throw (SSLException)new SSLException
|
|
1705 |
("Algorithm missing: ").initCause(e);
|
|
1706 |
}
|
|
1707 |
}
|
|
1708 |
|
|
1709 |
/*
|
|
1710 |
* Updates the SSL version associated with this connection.
|
|
1711 |
* Called from Handshaker once it has determined the negotiated version.
|
|
1712 |
*/
|
|
1713 |
synchronized void setVersion(ProtocolVersion protocolVersion) {
|
|
1714 |
this.protocolVersion = protocolVersion;
|
|
1715 |
output.r.setVersion(protocolVersion);
|
|
1716 |
}
|
|
1717 |
|
|
1718 |
synchronized String getHost() {
|
|
1719 |
if (host == null) {
|
|
1720 |
host = getInetAddress().getHostName();
|
|
1721 |
}
|
|
1722 |
return host;
|
|
1723 |
}
|
|
1724 |
|
|
1725 |
/**
|
|
1726 |
* Gets an input stream to read from the peer on the other side.
|
|
1727 |
* Data read from this stream was always integrity protected in
|
|
1728 |
* transit, and will usually have been confidentiality protected.
|
|
1729 |
*/
|
|
1730 |
synchronized public InputStream getInputStream() throws IOException {
|
|
1731 |
if (isClosed()) {
|
|
1732 |
throw new SocketException("Socket is closed");
|
|
1733 |
}
|
|
1734 |
|
|
1735 |
/*
|
|
1736 |
* Can't call isConnected() here, because the Handshakers
|
|
1737 |
* do some initialization before we actually connect.
|
|
1738 |
*/
|
|
1739 |
if (connectionState == cs_START) {
|
|
1740 |
throw new SocketException("Socket is not connected");
|
|
1741 |
}
|
|
1742 |
|
|
1743 |
return input;
|
|
1744 |
}
|
|
1745 |
|
|
1746 |
/**
|
|
1747 |
* Gets an output stream to write to the peer on the other side.
|
|
1748 |
* Data written on this stream is always integrity protected, and
|
|
1749 |
* will usually be confidentiality protected.
|
|
1750 |
*/
|
|
1751 |
synchronized public OutputStream getOutputStream() throws IOException {
|
|
1752 |
if (isClosed()) {
|
|
1753 |
throw new SocketException("Socket is closed");
|
|
1754 |
}
|
|
1755 |
|
|
1756 |
/*
|
|
1757 |
* Can't call isConnected() here, because the Handshakers
|
|
1758 |
* do some initialization before we actually connect.
|
|
1759 |
*/
|
|
1760 |
if (connectionState == cs_START) {
|
|
1761 |
throw new SocketException("Socket is not connected");
|
|
1762 |
}
|
|
1763 |
|
|
1764 |
return output;
|
|
1765 |
}
|
|
1766 |
|
|
1767 |
/**
|
|
1768 |
* Returns the the SSL Session in use by this connection. These can
|
|
1769 |
* be long lived, and frequently correspond to an entire login session
|
|
1770 |
* for some user.
|
|
1771 |
*/
|
|
1772 |
public SSLSession getSession() {
|
|
1773 |
/*
|
|
1774 |
* Force a synchronous handshake, if appropriate.
|
|
1775 |
*/
|
|
1776 |
if (getConnectionState() == cs_HANDSHAKE) {
|
|
1777 |
try {
|
|
1778 |
// start handshaking, if failed, the connection will be closed.
|
|
1779 |
startHandshake(false);
|
|
1780 |
} catch (IOException e) {
|
|
1781 |
// handshake failed. log and return a nullSession
|
|
1782 |
if (debug != null && Debug.isOn("handshake")) {
|
|
1783 |
System.out.println(threadName() +
|
|
1784 |
", IOException in getSession(): " + e);
|
|
1785 |
}
|
|
1786 |
}
|
|
1787 |
}
|
|
1788 |
synchronized (this) {
|
|
1789 |
return sess;
|
|
1790 |
}
|
|
1791 |
}
|
|
1792 |
|
|
1793 |
/**
|
|
1794 |
* Controls whether new connections may cause creation of new SSL
|
|
1795 |
* sessions.
|
|
1796 |
*
|
|
1797 |
* As long as handshaking has not started, we can change
|
|
1798 |
* whether we enable session creations. Otherwise,
|
|
1799 |
* we will need to wait for the next handshake.
|
|
1800 |
*/
|
|
1801 |
synchronized public void setEnableSessionCreation(boolean flag) {
|
|
1802 |
enableSessionCreation = flag;
|
|
1803 |
|
|
1804 |
if ((handshaker != null) && !handshaker.started()) {
|
|
1805 |
handshaker.setEnableSessionCreation(enableSessionCreation);
|
|
1806 |
}
|
|
1807 |
}
|
|
1808 |
|
|
1809 |
/**
|
|
1810 |
* Returns true if new connections may cause creation of new SSL
|
|
1811 |
* sessions.
|
|
1812 |
*/
|
|
1813 |
synchronized public boolean getEnableSessionCreation() {
|
|
1814 |
return enableSessionCreation;
|
|
1815 |
}
|
|
1816 |
|
|
1817 |
|
|
1818 |
/**
|
|
1819 |
* Sets the flag controlling whether a server mode socket
|
|
1820 |
* *REQUIRES* SSL client authentication.
|
|
1821 |
*
|
|
1822 |
* As long as handshaking has not started, we can change
|
|
1823 |
* whether client authentication is needed. Otherwise,
|
|
1824 |
* we will need to wait for the next handshake.
|
|
1825 |
*/
|
|
1826 |
synchronized public void setNeedClientAuth(boolean flag) {
|
|
1827 |
doClientAuth = (flag ?
|
|
1828 |
SSLEngineImpl.clauth_required : SSLEngineImpl.clauth_none);
|
|
1829 |
|
|
1830 |
if ((handshaker != null) &&
|
|
1831 |
(handshaker instanceof ServerHandshaker) &&
|
|
1832 |
!handshaker.started()) {
|
|
1833 |
((ServerHandshaker) handshaker).setClientAuth(doClientAuth);
|
|
1834 |
}
|
|
1835 |
}
|
|
1836 |
|
|
1837 |
synchronized public boolean getNeedClientAuth() {
|
|
1838 |
return (doClientAuth == SSLEngineImpl.clauth_required);
|
|
1839 |
}
|
|
1840 |
|
|
1841 |
/**
|
|
1842 |
* Sets the flag controlling whether a server mode socket
|
|
1843 |
* *REQUESTS* SSL client authentication.
|
|
1844 |
*
|
|
1845 |
* As long as handshaking has not started, we can change
|
|
1846 |
* whether client authentication is requested. Otherwise,
|
|
1847 |
* we will need to wait for the next handshake.
|
|
1848 |
*/
|
|
1849 |
synchronized public void setWantClientAuth(boolean flag) {
|
|
1850 |
doClientAuth = (flag ?
|
|
1851 |
SSLEngineImpl.clauth_requested : SSLEngineImpl.clauth_none);
|
|
1852 |
|
|
1853 |
if ((handshaker != null) &&
|
|
1854 |
(handshaker instanceof ServerHandshaker) &&
|
|
1855 |
!handshaker.started()) {
|
|
1856 |
((ServerHandshaker) handshaker).setClientAuth(doClientAuth);
|
|
1857 |
}
|
|
1858 |
}
|
|
1859 |
|
|
1860 |
synchronized public boolean getWantClientAuth() {
|
|
1861 |
return (doClientAuth == SSLEngineImpl.clauth_requested);
|
|
1862 |
}
|
|
1863 |
|
|
1864 |
|
|
1865 |
/**
|
|
1866 |
* Sets the flag controlling whether the socket is in SSL
|
|
1867 |
* client or server mode. Must be called before any SSL
|
|
1868 |
* traffic has started.
|
|
1869 |
*/
|
|
1870 |
synchronized public void setUseClientMode(boolean flag) {
|
|
1871 |
switch (connectionState) {
|
|
1872 |
|
|
1873 |
case cs_START:
|
|
1874 |
roleIsServer = !flag;
|
|
1875 |
break;
|
|
1876 |
|
|
1877 |
case cs_HANDSHAKE:
|
|
1878 |
/*
|
|
1879 |
* If we have a handshaker, but haven't started
|
|
1880 |
* SSL traffic, we can throw away our current
|
|
1881 |
* handshaker, and start from scratch. Don't
|
|
1882 |
* need to call doneConnect() again, we already
|
|
1883 |
* have the streams.
|
|
1884 |
*/
|
|
1885 |
assert(handshaker != null);
|
|
1886 |
if (!handshaker.started()) {
|
|
1887 |
roleIsServer = !flag;
|
|
1888 |
connectionState = cs_START;
|
|
1889 |
initHandshaker();
|
|
1890 |
break;
|
|
1891 |
}
|
|
1892 |
|
|
1893 |
// If handshake has started, that's an error. Fall through...
|
|
1894 |
|
|
1895 |
default:
|
|
1896 |
if (debug != null && Debug.isOn("ssl")) {
|
|
1897 |
System.out.println(threadName() +
|
|
1898 |
", setUseClientMode() invoked in state = " +
|
|
1899 |
connectionState);
|
|
1900 |
}
|
|
1901 |
throw new IllegalArgumentException(
|
|
1902 |
"Cannot change mode after SSL traffic has started");
|
|
1903 |
}
|
|
1904 |
}
|
|
1905 |
|
|
1906 |
synchronized public boolean getUseClientMode() {
|
|
1907 |
return !roleIsServer;
|
|
1908 |
}
|
|
1909 |
|
|
1910 |
|
|
1911 |
/**
|
|
1912 |
* Returns the names of the cipher suites which could be enabled for use
|
|
1913 |
* on an SSL connection. Normally, only a subset of these will actually
|
|
1914 |
* be enabled by default, since this list may include cipher suites which
|
|
1915 |
* do not support the mutual authentication of servers and clients, or
|
|
1916 |
* which do not protect data confidentiality. Servers may also need
|
|
1917 |
* certain kinds of certificates to use certain cipher suites.
|
|
1918 |
*
|
|
1919 |
* @return an array of cipher suite names
|
|
1920 |
*/
|
|
1921 |
public String[] getSupportedCipherSuites() {
|
|
1922 |
CipherSuiteList.clearAvailableCache();
|
|
1923 |
return CipherSuiteList.getSupported().toStringArray();
|
|
1924 |
}
|
|
1925 |
|
|
1926 |
/**
|
|
1927 |
* Controls which particular cipher suites are enabled for use on
|
|
1928 |
* this connection. The cipher suites must have been listed by
|
|
1929 |
* getCipherSuites() as being supported. Even if a suite has been
|
|
1930 |
* enabled, it might never be used if no peer supports it or the
|
|
1931 |
* requisite certificates (and private keys) are not available.
|
|
1932 |
*
|
|
1933 |
* @param suites Names of all the cipher suites to enable.
|
|
1934 |
*/
|
|
1935 |
synchronized public void setEnabledCipherSuites(String[] suites) {
|
|
1936 |
enabledCipherSuites = new CipherSuiteList(suites);
|
|
1937 |
if ((handshaker != null) && !handshaker.started()) {
|
|
1938 |
handshaker.enabledCipherSuites = enabledCipherSuites;
|
|
1939 |
}
|
|
1940 |
}
|
|
1941 |
|
|
1942 |
/**
|
|
1943 |
* Returns the names of the SSL cipher suites which are currently enabled
|
|
1944 |
* for use on this connection. When an SSL socket is first created,
|
|
1945 |
* all enabled cipher suites <em>(a)</em> protect data confidentiality,
|
|
1946 |
* by traffic encryption, and <em>(b)</em> can mutually authenticate
|
|
1947 |
* both clients and servers. Thus, in some environments, this value
|
|
1948 |
* might be empty.
|
|
1949 |
*
|
|
1950 |
* @return an array of cipher suite names
|
|
1951 |
*/
|
|
1952 |
synchronized public String[] getEnabledCipherSuites() {
|
|
1953 |
return enabledCipherSuites.toStringArray();
|
|
1954 |
}
|
|
1955 |
|
|
1956 |
|
|
1957 |
/**
|
|
1958 |
* Returns the protocols that are supported by this implementation.
|
|
1959 |
* A subset of the supported protocols may be enabled for this connection
|
|
1960 |
* @ returns an array of protocol names.
|
|
1961 |
*/
|
|
1962 |
public String[] getSupportedProtocols() {
|
|
1963 |
return ProtocolList.getSupported().toStringArray();
|
|
1964 |
}
|
|
1965 |
|
|
1966 |
/**
|
|
1967 |
* Controls which protocols are enabled for use on
|
|
1968 |
* this connection. The protocols must have been listed by
|
|
1969 |
* getSupportedProtocols() as being supported.
|
|
1970 |
*
|
|
1971 |
* @param protocols protocols to enable.
|
|
1972 |
* @exception IllegalArgumentException when one of the protocols
|
|
1973 |
* named by the parameter is not supported.
|
|
1974 |
*/
|
|
1975 |
synchronized public void setEnabledProtocols(String[] protocols) {
|
|
1976 |
enabledProtocols = new ProtocolList(protocols);
|
|
1977 |
if ((handshaker != null) && !handshaker.started()) {
|
|
1978 |
handshaker.setEnabledProtocols(enabledProtocols);
|
|
1979 |
}
|
|
1980 |
}
|
|
1981 |
|
|
1982 |
synchronized public String[] getEnabledProtocols() {
|
|
1983 |
return enabledProtocols.toStringArray();
|
|
1984 |
}
|
|
1985 |
|
|
1986 |
/**
|
|
1987 |
* Assigns the socket timeout.
|
|
1988 |
* @see java.net.Socket#setSoTimeout
|
|
1989 |
*/
|
|
1990 |
public void setSoTimeout(int timeout) throws SocketException {
|
|
1991 |
if ((debug != null) && Debug.isOn("ssl")) {
|
|
1992 |
System.out.println(threadName() +
|
|
1993 |
", setSoTimeout(" + timeout + ") called");
|
|
1994 |
}
|
|
1995 |
if (self == this) {
|
|
1996 |
super.setSoTimeout(timeout);
|
|
1997 |
} else {
|
|
1998 |
self.setSoTimeout(timeout);
|
|
1999 |
}
|
|
2000 |
}
|
|
2001 |
|
|
2002 |
/**
|
|
2003 |
* Registers an event listener to receive notifications that an
|
|
2004 |
* SSL handshake has completed on this connection.
|
|
2005 |
*/
|
|
2006 |
public synchronized void addHandshakeCompletedListener(
|
|
2007 |
HandshakeCompletedListener listener) {
|
|
2008 |
if (listener == null) {
|
|
2009 |
throw new IllegalArgumentException("listener is null");
|
|
2010 |
}
|
|
2011 |
if (handshakeListeners == null) {
|
|
2012 |
handshakeListeners = new
|
|
2013 |
HashMap<HandshakeCompletedListener, AccessControlContext>(4);
|
|
2014 |
}
|
|
2015 |
handshakeListeners.put(listener, AccessController.getContext());
|
|
2016 |
}
|
|
2017 |
|
|
2018 |
|
|
2019 |
/**
|
|
2020 |
* Removes a previously registered handshake completion listener.
|
|
2021 |
*/
|
|
2022 |
public synchronized void removeHandshakeCompletedListener(
|
|
2023 |
HandshakeCompletedListener listener) {
|
|
2024 |
if (handshakeListeners == null) {
|
|
2025 |
throw new IllegalArgumentException("no listeners");
|
|
2026 |
}
|
|
2027 |
if (handshakeListeners.remove(listener) == null) {
|
|
2028 |
throw new IllegalArgumentException("listener not registered");
|
|
2029 |
}
|
|
2030 |
if (handshakeListeners.isEmpty()) {
|
|
2031 |
handshakeListeners = null;
|
|
2032 |
}
|
|
2033 |
}
|
|
2034 |
|
|
2035 |
/**
|
|
2036 |
* Try to configure the endpoint identification algorithm of the socket.
|
|
2037 |
*
|
|
2038 |
* @param identificationAlgorithm the algorithm used to check the
|
|
2039 |
* endpoint identity.
|
|
2040 |
* @return true if the identification algorithm configuration success.
|
|
2041 |
*/
|
|
2042 |
synchronized public boolean trySetHostnameVerification(
|
|
2043 |
String identificationAlgorithm) {
|
|
2044 |
if (sslContext.getX509TrustManager() instanceof
|
|
2045 |
X509ExtendedTrustManager) {
|
|
2046 |
this.identificationAlg = identificationAlgorithm;
|
|
2047 |
return true;
|
|
2048 |
} else {
|
|
2049 |
return false;
|
|
2050 |
}
|
|
2051 |
}
|
|
2052 |
|
|
2053 |
/**
|
|
2054 |
* Returns the endpoint identification algorithm of the socket.
|
|
2055 |
*/
|
|
2056 |
synchronized public String getHostnameVerification() {
|
|
2057 |
return identificationAlg;
|
|
2058 |
}
|
|
2059 |
|
|
2060 |
//
|
|
2061 |
// We allocate a separate thread to deliver handshake completion
|
|
2062 |
// events. This ensures that the notifications don't block the
|
|
2063 |
// protocol state machine.
|
|
2064 |
//
|
|
2065 |
private static class NotifyHandshakeThread extends Thread {
|
|
2066 |
|
|
2067 |
private Set<Map.Entry<HandshakeCompletedListener,AccessControlContext>>
|
|
2068 |
targets; // who gets notified
|
|
2069 |
private HandshakeCompletedEvent event; // the notification
|
|
2070 |
|
|
2071 |
NotifyHandshakeThread(
|
|
2072 |
Set<Map.Entry<HandshakeCompletedListener,AccessControlContext>>
|
|
2073 |
entrySet, HandshakeCompletedEvent e) {
|
|
2074 |
|
|
2075 |
super("HandshakeCompletedNotify-Thread");
|
|
2076 |
targets = entrySet;
|
|
2077 |
event = e;
|
|
2078 |
}
|
|
2079 |
|
|
2080 |
public void run() {
|
|
2081 |
for (Map.Entry<HandshakeCompletedListener,AccessControlContext>
|
|
2082 |
entry : targets) {
|
|
2083 |
|
|
2084 |
final HandshakeCompletedListener l = entry.getKey();
|
|
2085 |
AccessControlContext acc = entry.getValue();
|
|
2086 |
AccessController.doPrivileged(new PrivilegedAction<Void>() {
|
|
2087 |
public Void run() {
|
|
2088 |
l.handshakeCompleted(event);
|
|
2089 |
return null;
|
|
2090 |
}
|
|
2091 |
}, acc);
|
|
2092 |
}
|
|
2093 |
}
|
|
2094 |
}
|
|
2095 |
|
|
2096 |
/**
|
|
2097 |
* Return the name of the current thread. Utility method.
|
|
2098 |
*/
|
|
2099 |
private static String threadName() {
|
|
2100 |
return Thread.currentThread().getName();
|
|
2101 |
}
|
|
2102 |
|
|
2103 |
/**
|
|
2104 |
* Returns a printable representation of this end of the connection.
|
|
2105 |
*/
|
|
2106 |
public String toString() {
|
|
2107 |
StringBuffer retval = new StringBuffer(80);
|
|
2108 |
|
|
2109 |
retval.append(Integer.toHexString(hashCode()));
|
|
2110 |
retval.append("[");
|
|
2111 |
retval.append(sess.getCipherSuite());
|
|
2112 |
retval.append(": ");
|
|
2113 |
|
|
2114 |
if (self == this) {
|
|
2115 |
retval.append(super.toString());
|
|
2116 |
} else {
|
|
2117 |
retval.append(self.toString());
|
|
2118 |
}
|
|
2119 |
retval.append("]");
|
|
2120 |
|
|
2121 |
return retval.toString();
|
|
2122 |
}
|
|
2123 |
}
|