author | xuelei |
Mon, 13 May 2013 05:41:51 -0700 | |
changeset 17458 | 4252f06a378f |
parent 14664 | e71aa0962e70 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
17458
4252f06a378f
8005535: SSLSessionImpl should have protected finalize()
xuelei
parents:
14664
diff
changeset
|
2 |
* Copyright (c) 1996, 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 |
||
27 |
package sun.security.ssl; |
|
28 |
||
29 |
import java.net.*; |
|
30 |
import java.util.Enumeration; |
|
31 |
import java.util.Hashtable; |
|
32 |
import java.util.Vector; |
|
7043 | 33 |
import java.util.Collection; |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
34 |
import java.util.Collections; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
35 |
import java.util.List; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
36 |
import java.util.ArrayList; |
2 | 37 |
|
38 |
import java.security.Principal; |
|
39 |
import java.security.PrivateKey; |
|
40 |
import java.security.SecureRandom; |
|
41 |
import java.security.cert.X509Certificate; |
|
42 |
import java.security.cert.CertificateEncodingException; |
|
43 |
||
44 |
import javax.crypto.SecretKey; |
|
45 |
||
46 |
import javax.net.ssl.SSLSessionContext; |
|
47 |
import javax.net.ssl.SSLSessionBindingListener; |
|
48 |
import javax.net.ssl.SSLSessionBindingEvent; |
|
49 |
import javax.net.ssl.SSLPeerUnverifiedException; |
|
50 |
import javax.net.ssl.SSLPermission; |
|
7043 | 51 |
import javax.net.ssl.ExtendedSSLSession; |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
52 |
import javax.net.ssl.SNIServerName; |
2 | 53 |
|
54 |
import static sun.security.ssl.CipherSuite.KeyExchange.*; |
|
55 |
||
56 |
/** |
|
57 |
* Implements the SSL session interface, and exposes the session context |
|
58 |
* which is maintained by SSL servers. |
|
59 |
* |
|
60 |
* <P> Servers have the ability to manage the sessions associated with |
|
61 |
* their authentication context(s). They can do this by enumerating the |
|
62 |
* IDs of the sessions which are cached, examining those sessions, and then |
|
63 |
* perhaps invalidating a given session so that it can't be used again. |
|
64 |
* If servers do not explicitly manage the cache, sessions will linger |
|
65 |
* until memory is low enough that the runtime environment purges cache |
|
66 |
* entries automatically to reclaim space. |
|
67 |
* |
|
68 |
* <P><em> The only reason this class is not package-private is that |
|
69 |
* there's no other public way to get at the server session context which |
|
70 |
* is associated with any given authentication context. </em> |
|
71 |
* |
|
72 |
* @author David Brownell |
|
73 |
*/ |
|
7043 | 74 |
final class SSLSessionImpl extends ExtendedSSLSession { |
2 | 75 |
|
76 |
/* |
|
77 |
* we only really need a single null session |
|
78 |
*/ |
|
79 |
static final SSLSessionImpl nullSession = new SSLSessionImpl(); |
|
80 |
||
81 |
// compression methods |
|
82 |
private static final byte compression_null = 0; |
|
83 |
||
84 |
/* |
|
85 |
* The state of a single session, as described in section 7.1 |
|
86 |
* of the SSLv3 spec. |
|
87 |
*/ |
|
88 |
private final ProtocolVersion protocolVersion; |
|
89 |
private final SessionId sessionId; |
|
90 |
private X509Certificate[] peerCerts; |
|
91 |
private byte compressionMethod; |
|
7043 | 92 |
private CipherSuite cipherSuite; |
2 | 93 |
private SecretKey masterSecret; |
94 |
||
95 |
/* |
|
96 |
* Information not part of the SSLv3 protocol spec, but used |
|
97 |
* to support session management policies. |
|
98 |
*/ |
|
99 |
private final long creationTime = System.currentTimeMillis(); |
|
100 |
private long lastUsedTime = 0; |
|
101 |
private final String host; |
|
102 |
private final int port; |
|
103 |
private SSLSessionContextImpl context; |
|
104 |
private int sessionCount; |
|
105 |
private boolean invalidated; |
|
106 |
private X509Certificate[] localCerts; |
|
107 |
private PrivateKey localPrivateKey; |
|
7043 | 108 |
private String[] localSupportedSignAlgs; |
109 |
private String[] peerSupportedSignAlgs; |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
110 |
private List<SNIServerName> requestedServerNames; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
111 |
|
2 | 112 |
|
113 |
// Principals for non-certificate based cipher suites |
|
114 |
private Principal peerPrincipal; |
|
115 |
private Principal localPrincipal; |
|
116 |
||
117 |
/* |
|
118 |
* We count session creations, eventually for statistical data but |
|
119 |
* also since counters make shorter debugging IDs than the big ones |
|
120 |
* we use in the protocol for uniqueness-over-time. |
|
121 |
*/ |
|
122 |
private static volatile int counter = 0; |
|
123 |
||
124 |
/* |
|
125 |
* Use of session caches is globally enabled/disabled. |
|
126 |
*/ |
|
127 |
private static boolean defaultRejoinable = true; |
|
128 |
||
129 |
/* Class and subclass dynamic debugging support */ |
|
130 |
private static final Debug debug = Debug.getInstance("ssl"); |
|
131 |
||
132 |
/* |
|
133 |
* Create a new non-rejoinable session, using the default (null) |
|
134 |
* cipher spec. This constructor returns a session which could |
|
135 |
* be used either by a client or by a server, as a connection is |
|
136 |
* first opened and before handshaking begins. |
|
137 |
*/ |
|
138 |
private SSLSessionImpl() { |
|
7043 | 139 |
this(ProtocolVersion.NONE, CipherSuite.C_NULL, null, |
140 |
new SessionId(false, null), null, -1); |
|
2 | 141 |
} |
142 |
||
143 |
/* |
|
144 |
* Create a new session, using a given cipher spec. This will |
|
145 |
* be rejoinable if session caching is enabled; the constructor |
|
146 |
* is intended mostly for use by serves. |
|
147 |
*/ |
|
148 |
SSLSessionImpl(ProtocolVersion protocolVersion, CipherSuite cipherSuite, |
|
7043 | 149 |
Collection<SignatureAndHashAlgorithm> algorithms, |
2 | 150 |
SecureRandom generator, String host, int port) { |
7043 | 151 |
this(protocolVersion, cipherSuite, algorithms, |
2 | 152 |
new SessionId(defaultRejoinable, generator), host, port); |
153 |
} |
|
154 |
||
155 |
/* |
|
156 |
* Record a new session, using a given cipher spec and session ID. |
|
157 |
*/ |
|
158 |
SSLSessionImpl(ProtocolVersion protocolVersion, CipherSuite cipherSuite, |
|
7043 | 159 |
Collection<SignatureAndHashAlgorithm> algorithms, |
2 | 160 |
SessionId id, String host, int port) { |
161 |
this.protocolVersion = protocolVersion; |
|
162 |
sessionId = id; |
|
163 |
peerCerts = null; |
|
164 |
compressionMethod = compression_null; |
|
165 |
this.cipherSuite = cipherSuite; |
|
166 |
masterSecret = null; |
|
167 |
this.host = host; |
|
168 |
this.port = port; |
|
169 |
sessionCount = ++counter; |
|
7043 | 170 |
localSupportedSignAlgs = |
171 |
SignatureAndHashAlgorithm.getAlgorithmNames(algorithms); |
|
2 | 172 |
|
173 |
if (debug != null && Debug.isOn("session")) { |
|
7043 | 174 |
System.out.println("%% Initialized: " + this); |
2 | 175 |
} |
176 |
} |
|
177 |
||
178 |
void setMasterSecret(SecretKey secret) { |
|
179 |
if (masterSecret == null) { |
|
180 |
masterSecret = secret; |
|
181 |
} else { |
|
182 |
throw new RuntimeException("setMasterSecret() error"); |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
/** |
|
187 |
* Returns the master secret ... treat with extreme caution! |
|
188 |
*/ |
|
189 |
SecretKey getMasterSecret() { |
|
190 |
return masterSecret; |
|
191 |
} |
|
192 |
||
193 |
void setPeerCertificates(X509Certificate[] peer) { |
|
194 |
if (peerCerts == null) { |
|
195 |
peerCerts = peer; |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
void setLocalCertificates(X509Certificate[] local) { |
|
200 |
localCerts = local; |
|
201 |
} |
|
202 |
||
203 |
void setLocalPrivateKey(PrivateKey privateKey) { |
|
204 |
localPrivateKey = privateKey; |
|
205 |
} |
|
206 |
||
7043 | 207 |
void setPeerSupportedSignatureAlgorithms( |
208 |
Collection<SignatureAndHashAlgorithm> algorithms) { |
|
209 |
peerSupportedSignAlgs = |
|
210 |
SignatureAndHashAlgorithm.getAlgorithmNames(algorithms); |
|
211 |
} |
|
212 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
213 |
void setRequestedServerNames(List<SNIServerName> requestedServerNames) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
214 |
this.requestedServerNames = new ArrayList<>(requestedServerNames); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
215 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
216 |
|
2 | 217 |
/** |
218 |
* Set the peer principal. |
|
219 |
*/ |
|
220 |
void setPeerPrincipal(Principal principal) { |
|
221 |
if (peerPrincipal == null) { |
|
222 |
peerPrincipal = principal; |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Set the local principal. |
|
228 |
*/ |
|
229 |
void setLocalPrincipal(Principal principal) { |
|
230 |
localPrincipal = principal; |
|
231 |
} |
|
232 |
||
233 |
/** |
|
234 |
* Returns true iff this session may be resumed ... sessions are |
|
235 |
* usually resumable. Security policies may suggest otherwise, |
|
236 |
* for example sessions that haven't been used for a while (say, |
|
237 |
* a working day) won't be resumable, and sessions might have a |
|
238 |
* maximum lifetime in any case. |
|
239 |
*/ |
|
240 |
boolean isRejoinable() { |
|
241 |
return sessionId != null && sessionId.length() != 0 && |
|
242 |
!invalidated && isLocalAuthenticationValid(); |
|
243 |
} |
|
244 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
245 |
@Override |
2 | 246 |
public synchronized boolean isValid() { |
247 |
return isRejoinable(); |
|
248 |
} |
|
249 |
||
250 |
/** |
|
251 |
* Check if the authentication used when establishing this session |
|
252 |
* is still valid. Returns true if no authentication was used |
|
253 |
*/ |
|
254 |
boolean isLocalAuthenticationValid() { |
|
255 |
if (localPrivateKey != null) { |
|
256 |
try { |
|
257 |
// if the private key is no longer valid, getAlgorithm() |
|
258 |
// should throw an exception |
|
259 |
// (e.g. Smartcard has been removed from the reader) |
|
260 |
localPrivateKey.getAlgorithm(); |
|
261 |
} catch (Exception e) { |
|
262 |
invalidate(); |
|
263 |
return false; |
|
264 |
} |
|
265 |
} |
|
266 |
return true; |
|
267 |
} |
|
268 |
||
269 |
/** |
|
270 |
* Returns the ID for this session. The ID is fixed for the |
|
271 |
* duration of the session; neither it, nor its value, changes. |
|
272 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
273 |
@Override |
2 | 274 |
public byte[] getId() { |
275 |
return sessionId.getId(); |
|
276 |
} |
|
277 |
||
278 |
/** |
|
279 |
* For server sessions, this returns the set of sessions which |
|
280 |
* are currently valid in this process. For client sessions, |
|
281 |
* this returns null. |
|
282 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
283 |
@Override |
2 | 284 |
public SSLSessionContext getSessionContext() { |
285 |
/* |
|
286 |
* An interim security policy until we can do something |
|
287 |
* more specific in 1.2. Only allow trusted code (code which |
|
288 |
* can set system properties) to get an |
|
289 |
* SSLSessionContext. This is to limit the ability of code to |
|
290 |
* look up specific sessions or enumerate over them. Otherwise, |
|
291 |
* code can only get session objects from successful SSL |
|
292 |
* connections which implies that they must have had permission |
|
293 |
* to make the network connection in the first place. |
|
294 |
*/ |
|
295 |
SecurityManager sm; |
|
296 |
if ((sm = System.getSecurityManager()) != null) { |
|
297 |
sm.checkPermission(new SSLPermission("getSSLSessionContext")); |
|
298 |
} |
|
299 |
||
300 |
return context; |
|
301 |
} |
|
302 |
||
303 |
||
304 |
SessionId getSessionId() { |
|
305 |
return sessionId; |
|
306 |
} |
|
307 |
||
308 |
||
309 |
/** |
|
310 |
* Returns the cipher spec in use on this session |
|
311 |
*/ |
|
312 |
CipherSuite getSuite() { |
|
313 |
return cipherSuite; |
|
314 |
} |
|
315 |
||
316 |
/** |
|
7043 | 317 |
* Resets the cipher spec in use on this session |
318 |
*/ |
|
319 |
void setSuite(CipherSuite suite) { |
|
320 |
cipherSuite = suite; |
|
321 |
||
322 |
if (debug != null && Debug.isOn("session")) { |
|
323 |
System.out.println("%% Negotiating: " + this); |
|
324 |
} |
|
325 |
} |
|
326 |
||
327 |
/** |
|
2 | 328 |
* Returns the name of the cipher suite in use on this session |
329 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
330 |
@Override |
2 | 331 |
public String getCipherSuite() { |
332 |
return getSuite().name; |
|
333 |
} |
|
334 |
||
335 |
ProtocolVersion getProtocolVersion() { |
|
336 |
return protocolVersion; |
|
337 |
} |
|
338 |
||
339 |
/** |
|
340 |
* Returns the standard name of the protocol in use on this session |
|
341 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
342 |
@Override |
2 | 343 |
public String getProtocol() { |
344 |
return getProtocolVersion().name; |
|
345 |
} |
|
346 |
||
347 |
/** |
|
348 |
* Returns the compression technique used in this session |
|
349 |
*/ |
|
350 |
byte getCompression() { |
|
351 |
return compressionMethod; |
|
352 |
} |
|
353 |
||
354 |
/** |
|
355 |
* Returns the hashcode for this session |
|
356 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
357 |
@Override |
2 | 358 |
public int hashCode() { |
359 |
return sessionId.hashCode(); |
|
360 |
} |
|
361 |
||
362 |
||
363 |
/** |
|
364 |
* Returns true if sessions have same ids, false otherwise. |
|
365 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
366 |
@Override |
2 | 367 |
public boolean equals(Object obj) { |
368 |
||
369 |
if (obj == this) { |
|
370 |
return true; |
|
371 |
} |
|
372 |
||
373 |
if (obj instanceof SSLSessionImpl) { |
|
374 |
SSLSessionImpl sess = (SSLSessionImpl) obj; |
|
375 |
return (sessionId != null) && (sessionId.equals( |
|
376 |
sess.getSessionId())); |
|
377 |
} |
|
378 |
||
379 |
return false; |
|
380 |
} |
|
381 |
||
382 |
||
383 |
/** |
|
384 |
* Return the cert chain presented by the peer in the |
|
385 |
* java.security.cert format. |
|
386 |
* Note: This method can be used only when using certificate-based |
|
387 |
* cipher suites; using it with non-certificate-based cipher suites, |
|
388 |
* such as Kerberos, will throw an SSLPeerUnverifiedException. |
|
389 |
* |
|
390 |
* @return array of peer X.509 certs, with the peer's own cert |
|
391 |
* first in the chain, and with the "root" CA last. |
|
392 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
393 |
@Override |
2 | 394 |
public java.security.cert.Certificate[] getPeerCertificates() |
395 |
throws SSLPeerUnverifiedException { |
|
396 |
// |
|
397 |
// clone to preserve integrity of session ... caller can't |
|
398 |
// change record of peer identity even by accident, much |
|
399 |
// less do it intentionally. |
|
400 |
// |
|
401 |
if ((cipherSuite.keyExchange == K_KRB5) || |
|
402 |
(cipherSuite.keyExchange == K_KRB5_EXPORT)) { |
|
403 |
throw new SSLPeerUnverifiedException("no certificates expected" |
|
404 |
+ " for Kerberos cipher suites"); |
|
405 |
} |
|
406 |
if (peerCerts == null) { |
|
407 |
throw new SSLPeerUnverifiedException("peer not authenticated"); |
|
408 |
} |
|
409 |
// Certs are immutable objects, therefore we don't clone them. |
|
410 |
// But do need to clone the array, so that nothing is inserted |
|
411 |
// into peerCerts. |
|
412 |
return (java.security.cert.Certificate[])peerCerts.clone(); |
|
413 |
} |
|
414 |
||
415 |
/** |
|
416 |
* Return the cert chain presented to the peer in the |
|
417 |
* java.security.cert format. |
|
418 |
* Note: This method is useful only when using certificate-based |
|
419 |
* cipher suites. |
|
420 |
* |
|
421 |
* @return array of peer X.509 certs, with the peer's own cert |
|
422 |
* first in the chain, and with the "root" CA last. |
|
423 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
424 |
@Override |
2 | 425 |
public java.security.cert.Certificate[] getLocalCertificates() { |
426 |
// |
|
427 |
// clone to preserve integrity of session ... caller can't |
|
428 |
// change record of peer identity even by accident, much |
|
429 |
// less do it intentionally. |
|
430 |
return (localCerts == null ? null : |
|
431 |
(java.security.cert.Certificate[])localCerts.clone()); |
|
432 |
} |
|
433 |
||
434 |
/** |
|
435 |
* Return the cert chain presented by the peer in the |
|
436 |
* javax.security.cert format. |
|
437 |
* Note: This method can be used only when using certificate-based |
|
438 |
* cipher suites; using it with non-certificate-based cipher suites, |
|
439 |
* such as Kerberos, will throw an SSLPeerUnverifiedException. |
|
440 |
* |
|
441 |
* @return array of peer X.509 certs, with the peer's own cert |
|
442 |
* first in the chain, and with the "root" CA last. |
|
443 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
444 |
@Override |
2 | 445 |
public javax.security.cert.X509Certificate[] getPeerCertificateChain() |
446 |
throws SSLPeerUnverifiedException { |
|
447 |
// |
|
448 |
// clone to preserve integrity of session ... caller can't |
|
449 |
// change record of peer identity even by accident, much |
|
450 |
// less do it intentionally. |
|
451 |
// |
|
452 |
if ((cipherSuite.keyExchange == K_KRB5) || |
|
453 |
(cipherSuite.keyExchange == K_KRB5_EXPORT)) { |
|
454 |
throw new SSLPeerUnverifiedException("no certificates expected" |
|
455 |
+ " for Kerberos cipher suites"); |
|
456 |
} |
|
457 |
if (peerCerts == null) { |
|
458 |
throw new SSLPeerUnverifiedException("peer not authenticated"); |
|
459 |
} |
|
460 |
javax.security.cert.X509Certificate[] certs; |
|
461 |
certs = new javax.security.cert.X509Certificate[peerCerts.length]; |
|
462 |
for (int i = 0; i < peerCerts.length; i++) { |
|
463 |
byte[] der = null; |
|
464 |
try { |
|
465 |
der = peerCerts[i].getEncoded(); |
|
466 |
certs[i] = javax.security.cert.X509Certificate.getInstance(der); |
|
467 |
} catch (CertificateEncodingException e) { |
|
468 |
throw new SSLPeerUnverifiedException(e.getMessage()); |
|
469 |
} catch (javax.security.cert.CertificateException e) { |
|
470 |
throw new SSLPeerUnverifiedException(e.getMessage()); |
|
471 |
} |
|
472 |
} |
|
473 |
||
474 |
return certs; |
|
475 |
} |
|
476 |
||
477 |
/** |
|
478 |
* Return the cert chain presented by the peer. |
|
479 |
* Note: This method can be used only when using certificate-based |
|
480 |
* cipher suites; using it with non-certificate-based cipher suites, |
|
481 |
* such as Kerberos, will throw an SSLPeerUnverifiedException. |
|
482 |
* |
|
483 |
* @return array of peer X.509 certs, with the peer's own cert |
|
484 |
* first in the chain, and with the "root" CA last. |
|
485 |
*/ |
|
486 |
public X509Certificate[] getCertificateChain() |
|
487 |
throws SSLPeerUnverifiedException { |
|
488 |
/* |
|
489 |
* clone to preserve integrity of session ... caller can't |
|
490 |
* change record of peer identity even by accident, much |
|
491 |
* less do it intentionally. |
|
492 |
*/ |
|
493 |
if ((cipherSuite.keyExchange == K_KRB5) || |
|
494 |
(cipherSuite.keyExchange == K_KRB5_EXPORT)) { |
|
495 |
throw new SSLPeerUnverifiedException("no certificates expected" |
|
496 |
+ " for Kerberos cipher suites"); |
|
497 |
} |
|
498 |
if (peerCerts != null) { |
|
51 | 499 |
return peerCerts.clone(); |
2 | 500 |
} else { |
501 |
throw new SSLPeerUnverifiedException("peer not authenticated"); |
|
502 |
} |
|
503 |
} |
|
504 |
||
505 |
/** |
|
506 |
* Returns the identity of the peer which was established as part of |
|
507 |
* defining the session. |
|
508 |
* |
|
509 |
* @return the peer's principal. Returns an X500Principal of the |
|
4236 | 510 |
* end-entity certificate for X509-based cipher suites, and |
511 |
* Principal for Kerberos cipher suites. |
|
2 | 512 |
* |
513 |
* @throws SSLPeerUnverifiedException if the peer's identity has not |
|
514 |
* been verified |
|
515 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
516 |
@Override |
2 | 517 |
public Principal getPeerPrincipal() |
518 |
throws SSLPeerUnverifiedException |
|
519 |
{ |
|
520 |
if ((cipherSuite.keyExchange == K_KRB5) || |
|
521 |
(cipherSuite.keyExchange == K_KRB5_EXPORT)) { |
|
522 |
if (peerPrincipal == null) { |
|
523 |
throw new SSLPeerUnverifiedException("peer not authenticated"); |
|
524 |
} else { |
|
4236 | 525 |
// Eliminate dependency on KerberosPrincipal |
526 |
return peerPrincipal; |
|
2 | 527 |
} |
528 |
} |
|
529 |
if (peerCerts == null) { |
|
530 |
throw new SSLPeerUnverifiedException("peer not authenticated"); |
|
531 |
} |
|
51 | 532 |
return peerCerts[0].getSubjectX500Principal(); |
2 | 533 |
} |
534 |
||
535 |
/** |
|
536 |
* Returns the principal that was sent to the peer during handshaking. |
|
537 |
* |
|
538 |
* @return the principal sent to the peer. Returns an X500Principal |
|
539 |
* of the end-entity certificate for X509-based cipher suites, and |
|
4236 | 540 |
* Principal for Kerberos cipher suites. If no principal was |
2 | 541 |
* sent, then null is returned. |
542 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
543 |
@Override |
2 | 544 |
public Principal getLocalPrincipal() { |
545 |
||
546 |
if ((cipherSuite.keyExchange == K_KRB5) || |
|
547 |
(cipherSuite.keyExchange == K_KRB5_EXPORT)) { |
|
4236 | 548 |
// Eliminate dependency on KerberosPrincipal |
549 |
return (localPrincipal == null ? null : localPrincipal); |
|
2 | 550 |
} |
551 |
return (localCerts == null ? null : |
|
51 | 552 |
localCerts[0].getSubjectX500Principal()); |
2 | 553 |
} |
554 |
||
555 |
/** |
|
556 |
* Returns the time this session was created. |
|
557 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
558 |
@Override |
2 | 559 |
public long getCreationTime() { |
560 |
return creationTime; |
|
561 |
} |
|
562 |
||
563 |
/** |
|
564 |
* Returns the last time this session was used to initialize |
|
565 |
* a connection. |
|
566 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
567 |
@Override |
2 | 568 |
public long getLastAccessedTime() { |
569 |
return (lastUsedTime != 0) ? lastUsedTime : creationTime; |
|
570 |
} |
|
571 |
||
572 |
void setLastAccessedTime(long time) { |
|
573 |
lastUsedTime = time; |
|
574 |
} |
|
575 |
||
576 |
||
577 |
/** |
|
578 |
* Returns the network address of the session's peer. This |
|
579 |
* implementation does not insist that connections between |
|
580 |
* different ports on the same host must necessarily belong |
|
581 |
* to different sessions, though that is of course allowed. |
|
582 |
*/ |
|
583 |
public InetAddress getPeerAddress() { |
|
584 |
try { |
|
585 |
return InetAddress.getByName(host); |
|
586 |
} catch (java.net.UnknownHostException e) { |
|
587 |
return null; |
|
588 |
} |
|
589 |
} |
|
590 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
591 |
@Override |
2 | 592 |
public String getPeerHost() { |
593 |
return host; |
|
594 |
} |
|
595 |
||
596 |
/** |
|
597 |
* Need to provide the port info for caching sessions based on |
|
598 |
* host and port. Accessed by SSLSessionContextImpl |
|
599 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
600 |
@Override |
2 | 601 |
public int getPeerPort() { |
602 |
return port; |
|
603 |
} |
|
604 |
||
605 |
void setContext(SSLSessionContextImpl ctx) { |
|
606 |
if (context == null) { |
|
607 |
context = ctx; |
|
608 |
} |
|
609 |
} |
|
610 |
||
611 |
/** |
|
612 |
* Invalidate a session. Active connections may still exist, but |
|
613 |
* no connections will be able to rejoin this session. |
|
614 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
615 |
@Override |
2 | 616 |
synchronized public void invalidate() { |
617 |
// |
|
618 |
// Can't invalidate the NULL session -- this would be |
|
619 |
// attempted when we get a handshaking error on a brand |
|
620 |
// new connection, with no "real" session yet. |
|
621 |
// |
|
622 |
if (this == nullSession) { |
|
623 |
return; |
|
624 |
} |
|
625 |
invalidated = true; |
|
626 |
if (debug != null && Debug.isOn("session")) { |
|
627 |
System.out.println("%% Invalidated: " + this); |
|
628 |
} |
|
629 |
if (context != null) { |
|
630 |
context.remove(sessionId); |
|
631 |
context = null; |
|
632 |
} |
|
633 |
} |
|
634 |
||
635 |
/* |
|
636 |
* Table of application-specific session data indexed by an application |
|
637 |
* key and the calling security context. This is important since |
|
638 |
* sessions can be shared across different protection domains. |
|
639 |
*/ |
|
7990 | 640 |
private Hashtable<SecureKey, Object> table = new Hashtable<>(); |
2 | 641 |
|
642 |
/** |
|
643 |
* Assigns a session value. Session change events are given if |
|
644 |
* appropriate, to any original value as well as the new value. |
|
645 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
646 |
@Override |
2 | 647 |
public void putValue(String key, Object value) { |
648 |
if ((key == null) || (value == null)) { |
|
649 |
throw new IllegalArgumentException("arguments can not be null"); |
|
650 |
} |
|
651 |
||
652 |
SecureKey secureKey = new SecureKey(key); |
|
653 |
Object oldValue = table.put(secureKey, value); |
|
654 |
||
655 |
if (oldValue instanceof SSLSessionBindingListener) { |
|
656 |
SSLSessionBindingEvent e; |
|
657 |
||
658 |
e = new SSLSessionBindingEvent(this, key); |
|
659 |
((SSLSessionBindingListener)oldValue).valueUnbound(e); |
|
660 |
} |
|
661 |
if (value instanceof SSLSessionBindingListener) { |
|
662 |
SSLSessionBindingEvent e; |
|
663 |
||
664 |
e = new SSLSessionBindingEvent(this, key); |
|
665 |
((SSLSessionBindingListener)value).valueBound(e); |
|
666 |
} |
|
667 |
} |
|
668 |
||
669 |
||
670 |
/** |
|
671 |
* Returns the specified session value. |
|
672 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
673 |
@Override |
2 | 674 |
public Object getValue(String key) { |
675 |
if (key == null) { |
|
676 |
throw new IllegalArgumentException("argument can not be null"); |
|
677 |
} |
|
678 |
||
679 |
SecureKey secureKey = new SecureKey(key); |
|
680 |
return table.get(secureKey); |
|
681 |
} |
|
682 |
||
683 |
||
684 |
/** |
|
685 |
* Removes the specified session value, delivering a session changed |
|
686 |
* event as appropriate. |
|
687 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
688 |
@Override |
2 | 689 |
public void removeValue(String key) { |
690 |
if (key == null) { |
|
691 |
throw new IllegalArgumentException("argument can not be null"); |
|
692 |
} |
|
693 |
||
694 |
SecureKey secureKey = new SecureKey(key); |
|
695 |
Object value = table.remove(secureKey); |
|
696 |
||
697 |
if (value instanceof SSLSessionBindingListener) { |
|
698 |
SSLSessionBindingEvent e; |
|
699 |
||
700 |
e = new SSLSessionBindingEvent(this, key); |
|
701 |
((SSLSessionBindingListener)value).valueUnbound(e); |
|
702 |
} |
|
703 |
} |
|
704 |
||
705 |
||
706 |
/** |
|
707 |
* Lists the names of the session values. |
|
708 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
709 |
@Override |
2 | 710 |
public String[] getValueNames() { |
711 |
Enumeration<SecureKey> e; |
|
7990 | 712 |
Vector<Object> v = new Vector<>(); |
2 | 713 |
SecureKey key; |
714 |
Object securityCtx = SecureKey.getCurrentSecurityContext(); |
|
715 |
||
716 |
for (e = table.keys(); e.hasMoreElements(); ) { |
|
717 |
key = e.nextElement(); |
|
718 |
||
719 |
if (securityCtx.equals(key.getSecurityContext())) { |
|
720 |
v.addElement(key.getAppKey()); |
|
721 |
} |
|
722 |
} |
|
723 |
String[] names = new String[v.size()]; |
|
724 |
v.copyInto(names); |
|
725 |
||
726 |
return names; |
|
727 |
} |
|
728 |
||
729 |
/** |
|
730 |
* Use large packet sizes now or follow RFC 2246 packet sizes (2^14) |
|
731 |
* until changed. |
|
732 |
* |
|
733 |
* In the TLS specification (section 6.2.1, RFC2246), it is not |
|
734 |
* recommended that the plaintext has more than 2^14 bytes. |
|
735 |
* However, some TLS implementations violate the specification. |
|
736 |
* This is a workaround for interoperability with these stacks. |
|
737 |
* |
|
738 |
* Application could accept large fragments up to 2^15 bytes by |
|
739 |
* setting the system property jsse.SSLEngine.acceptLargeFragments |
|
740 |
* to "true". |
|
741 |
*/ |
|
742 |
private boolean acceptLargeFragments = |
|
743 |
Debug.getBooleanProperty("jsse.SSLEngine.acceptLargeFragments", false); |
|
744 |
||
745 |
/** |
|
746 |
* Expand the buffer size of both SSL/TLS network packet and |
|
747 |
* application data. |
|
748 |
*/ |
|
749 |
protected synchronized void expandBufferSizes() { |
|
750 |
acceptLargeFragments = true; |
|
751 |
} |
|
752 |
||
753 |
/** |
|
754 |
* Gets the current size of the largest SSL/TLS packet that is expected |
|
755 |
* when using this session. |
|
756 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
757 |
@Override |
2 | 758 |
public synchronized int getPacketBufferSize() { |
759 |
return acceptLargeFragments ? |
|
760 |
Record.maxLargeRecordSize : Record.maxRecordSize; |
|
761 |
} |
|
762 |
||
763 |
/** |
|
764 |
* Gets the current size of the largest application data that is |
|
765 |
* expected when using this session. |
|
766 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
767 |
@Override |
2 | 768 |
public synchronized int getApplicationBufferSize() { |
769 |
return getPacketBufferSize() - Record.headerSize; |
|
770 |
} |
|
771 |
||
7043 | 772 |
/** |
773 |
* Gets an array of supported signature algorithms that the local side is |
|
774 |
* willing to verify. |
|
775 |
*/ |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
776 |
@Override |
7043 | 777 |
public String[] getLocalSupportedSignatureAlgorithms() { |
778 |
if (localSupportedSignAlgs != null) { |
|
779 |
return localSupportedSignAlgs.clone(); |
|
780 |
} |
|
781 |
||
782 |
return new String[0]; |
|
783 |
} |
|
784 |
||
785 |
/** |
|
786 |
* Gets an array of supported signature algorithms that the peer is |
|
787 |
* able to verify. |
|
788 |
*/ |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
789 |
@Override |
7043 | 790 |
public String[] getPeerSupportedSignatureAlgorithms() { |
791 |
if (peerSupportedSignAlgs != null) { |
|
792 |
return peerSupportedSignAlgs.clone(); |
|
793 |
} |
|
794 |
||
795 |
return new String[0]; |
|
796 |
} |
|
797 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
798 |
/** |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
799 |
* Obtains a <code>List</code> containing all {@link SNIServerName}s |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
800 |
* of the requested Server Name Indication (SNI) extension. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
801 |
*/ |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
802 |
@Override |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
803 |
public List<SNIServerName> getRequestedServerNames() { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
804 |
if (requestedServerNames != null && !requestedServerNames.isEmpty()) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
805 |
return Collections.<SNIServerName>unmodifiableList( |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
806 |
requestedServerNames); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
807 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
808 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
809 |
return Collections.<SNIServerName>emptyList(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
810 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
9035
diff
changeset
|
811 |
|
2 | 812 |
/** Returns a string representation of this SSL session */ |
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
813 |
@Override |
2 | 814 |
public String toString() { |
815 |
return "[Session-" + sessionCount |
|
816 |
+ ", " + getCipherSuite() |
|
817 |
+ "]"; |
|
818 |
} |
|
819 |
||
820 |
/** |
|
821 |
* When SSL sessions are finalized, all values bound to |
|
822 |
* them are removed. |
|
823 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
824 |
@Override |
17458
4252f06a378f
8005535: SSLSessionImpl should have protected finalize()
xuelei
parents:
14664
diff
changeset
|
825 |
protected void finalize() throws Throwable { |
2 | 826 |
String[] names = getValueNames(); |
827 |
for (int i = 0; i < names.length; i++) { |
|
828 |
removeValue(names[i]); |
|
829 |
} |
|
830 |
} |
|
831 |
} |
|
832 |
||
833 |
||
834 |
/** |
|
835 |
* This "struct" class serves as a Hash Key that combines an |
|
836 |
* application-specific key and a security context. |
|
837 |
*/ |
|
838 |
class SecureKey { |
|
839 |
private static Object nullObject = new Object(); |
|
840 |
private Object appKey; |
|
841 |
private Object securityCtx; |
|
842 |
||
843 |
static Object getCurrentSecurityContext() { |
|
844 |
SecurityManager sm = System.getSecurityManager(); |
|
845 |
Object context = null; |
|
846 |
||
847 |
if (sm != null) |
|
848 |
context = sm.getSecurityContext(); |
|
849 |
if (context == null) |
|
850 |
context = nullObject; |
|
851 |
return context; |
|
852 |
} |
|
853 |
||
854 |
SecureKey(Object key) { |
|
855 |
this.appKey = key; |
|
856 |
this.securityCtx = getCurrentSecurityContext(); |
|
857 |
} |
|
858 |
||
859 |
Object getAppKey() { |
|
860 |
return appKey; |
|
861 |
} |
|
862 |
||
863 |
Object getSecurityContext() { |
|
864 |
return securityCtx; |
|
865 |
} |
|
866 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
867 |
@Override |
2 | 868 |
public int hashCode() { |
869 |
return appKey.hashCode() ^ securityCtx.hashCode(); |
|
870 |
} |
|
871 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
872 |
@Override |
2 | 873 |
public boolean equals(Object o) { |
874 |
return o instanceof SecureKey && ((SecureKey)o).appKey.equals(appKey) |
|
875 |
&& ((SecureKey)o).securityCtx.equals(securityCtx); |
|
876 |
} |
|
877 |
} |