author | ascarpino |
Wed, 08 Feb 2017 12:08:28 -0800 | |
changeset 43701 | fe8c324ba97c |
parent 31538 | 0981099a3e54 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
14664
diff
changeset
|
2 |
* Copyright (c) 1996, 2012, 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.security.SecureRandom; |
|
28565
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
30 |
import javax.net.ssl.SSLProtocolException; |
2 | 31 |
|
32 |
/** |
|
33 |
* Encapsulates an SSL session ID. SSL Session IDs are not reused by |
|
34 |
* servers during the lifetime of any sessions it created. Sessions may |
|
35 |
* be used by many connections, either concurrently (for example, two |
|
36 |
* connections to a web server at the same time) or sequentially (over as |
|
37 |
* long a time period as is allowed by a given server). |
|
38 |
* |
|
39 |
* @author Satish Dharmaraj |
|
40 |
* @author David Brownell |
|
41 |
*/ |
|
42 |
final |
|
43 |
class SessionId |
|
44 |
{ |
|
28565
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
45 |
static int MAX_LENGTH = 32; |
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
28565
diff
changeset
|
46 |
private byte[] sessionId; // max 32 bytes |
2 | 47 |
|
48 |
/** Constructs a new session ID ... perhaps for a rejoinable session */ |
|
49 |
SessionId (boolean isRejoinable, SecureRandom generator) |
|
50 |
{ |
|
51 |
if (isRejoinable) |
|
52 |
// this will be unique, it's a timestamp plus much randomness |
|
53 |
sessionId = new RandomCookie (generator).random_bytes; |
|
54 |
else |
|
55 |
sessionId = new byte [0]; |
|
56 |
} |
|
57 |
||
58 |
/** Constructs a session ID from a byte array (max size 32 bytes) */ |
|
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
28565
diff
changeset
|
59 |
SessionId (byte[] sessionId) |
2 | 60 |
{ this.sessionId = sessionId; } |
61 |
||
62 |
/** Returns the length of the ID, in bytes */ |
|
63 |
int length () |
|
64 |
{ return sessionId.length; } |
|
65 |
||
66 |
/** Returns the bytes in the ID. May be an empty array. */ |
|
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
28565
diff
changeset
|
67 |
byte[] getId () |
2 | 68 |
{ |
51 | 69 |
return sessionId.clone (); |
2 | 70 |
} |
71 |
||
72 |
/** Returns the ID as a string */ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
73 |
@Override |
2 | 74 |
public String toString () |
75 |
{ |
|
76 |
int len = sessionId.length; |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
77 |
StringBuilder sb = new StringBuilder (10 + 2 * len); |
2 | 78 |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
79 |
sb.append("{"); |
2 | 80 |
for (int i = 0; i < len; i++) { |
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
81 |
sb.append(0x0ff & sessionId[i]); |
2 | 82 |
if (i != (len - 1)) |
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
83 |
sb.append (", "); |
2 | 84 |
} |
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
85 |
sb.append("}"); |
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
86 |
return sb.toString (); |
2 | 87 |
} |
88 |
||
89 |
||
90 |
/** Returns a value which is the same for session IDs which are equal */ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
91 |
@Override |
2 | 92 |
public int hashCode () |
93 |
{ |
|
94 |
int retval = 0; |
|
95 |
||
96 |
for (int i = 0; i < sessionId.length; i++) |
|
97 |
retval += sessionId [i]; |
|
98 |
return retval; |
|
99 |
} |
|
100 |
||
101 |
/** Returns true if the parameter is the same session ID */ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
102 |
@Override |
2 | 103 |
public boolean equals (Object obj) |
104 |
{ |
|
105 |
if (!(obj instanceof SessionId)) |
|
106 |
return false; |
|
107 |
||
108 |
SessionId s = (SessionId) obj; |
|
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
28565
diff
changeset
|
109 |
byte[] b = s.getId (); |
2 | 110 |
|
111 |
if (b.length != sessionId.length) |
|
112 |
return false; |
|
113 |
for (int i = 0; i < sessionId.length; i++) { |
|
114 |
if (b [i] != sessionId [i]) |
|
115 |
return false; |
|
116 |
} |
|
117 |
return true; |
|
118 |
} |
|
28565
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
119 |
|
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
120 |
/** |
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
121 |
* Checks the length of the session ID to make sure it sits within |
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
122 |
* the range called out in the specification |
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
123 |
*/ |
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
124 |
void checkLength(ProtocolVersion pv) throws SSLProtocolException { |
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
125 |
// As of today all versions of TLS have a 32-byte maximum length. |
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
126 |
// In the future we can do more here to support protocol versions |
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
127 |
// that may have longer max lengths. |
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
128 |
if (sessionId.length > MAX_LENGTH) { |
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
129 |
throw new SSLProtocolException("Invalid session ID length (" + |
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
130 |
sessionId.length + " bytes)"); |
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
131 |
} |
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
132 |
} |
48712ca501c1
8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents:
25859
diff
changeset
|
133 |
|
2 | 134 |
} |