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 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.
24 */
25
26
27 package sun.security.ssl;
28
29 import java.security.SecureRandom;
30 import javax.net.ssl.SSLProtocolException;
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 {
45 static int MAX_LENGTH = 32;
46 private byte[] sessionId; // max 32 bytes
47
48 /** Constructs a new session ID ... perhaps for a rejoinable session */
49 SessionId (boolean isRejoinable, SecureRandom generator)
74 public String toString ()
75 {
76 int len = sessionId.length;
77 StringBuilder sb = new StringBuilder (10 + 2 * len);
78
79 sb.append("{");
80 for (int i = 0; i < len; i++) {
81 sb.append(0x0ff & sessionId[i]);
82 if (i != (len - 1))
83 sb.append (", ");
84 }
85 sb.append("}");
86 return sb.toString ();
87 }
88
89
90 /** Returns a value which is the same for session IDs which are equal */
91 @Override
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 */
102 @Override
103 public boolean equals (Object obj)
104 {
105 if (!(obj instanceof SessionId))
106 return false;
107
108 SessionId s = (SessionId) obj;
109 byte[] b = s.getId ();
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 }
119
120 /**
121 * Checks the length of the session ID to make sure it sits within
122 * the range called out in the specification
123 */
124 void checkLength(ProtocolVersion pv) throws SSLProtocolException {
125 // As of today all versions of TLS have a 32-byte maximum length.
126 // In the future we can do more here to support protocol versions
127 // that may have longer max lengths.
128 if (sessionId.length > MAX_LENGTH) {
129 throw new SSLProtocolException("Invalid session ID length (" +
130 sessionId.length + " bytes)");
131 }
132 }
133
134 }
|
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 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.
24 */
25
26
27 package sun.security.ssl;
28
29 import java.security.SecureRandom;
30 import java.util.Arrays;
31 import javax.net.ssl.SSLProtocolException;
32
33 /**
34 * Encapsulates an SSL session ID. SSL Session IDs are not reused by
35 * servers during the lifetime of any sessions it created. Sessions may
36 * be used by many connections, either concurrently (for example, two
37 * connections to a web server at the same time) or sequentially (over as
38 * long a time period as is allowed by a given server).
39 *
40 * @author Satish Dharmaraj
41 * @author David Brownell
42 */
43 final
44 class SessionId
45 {
46 static int MAX_LENGTH = 32;
47 private byte[] sessionId; // max 32 bytes
48
49 /** Constructs a new session ID ... perhaps for a rejoinable session */
50 SessionId (boolean isRejoinable, SecureRandom generator)
75 public String toString ()
76 {
77 int len = sessionId.length;
78 StringBuilder sb = new StringBuilder (10 + 2 * len);
79
80 sb.append("{");
81 for (int i = 0; i < len; i++) {
82 sb.append(0x0ff & sessionId[i]);
83 if (i != (len - 1))
84 sb.append (", ");
85 }
86 sb.append("}");
87 return sb.toString ();
88 }
89
90
91 /** Returns a value which is the same for session IDs which are equal */
92 @Override
93 public int hashCode ()
94 {
95 return Arrays.hashCode(sessionId);
96 }
97
98 /** Returns true if the parameter is the same session ID */
99 @Override
100 public boolean equals (Object obj)
101 {
102 return obj instanceof SessionId &&
103 Arrays.equals(sessionId, ((SessionId)obj).sessionId);
104 }
105
106 /**
107 * Checks the length of the session ID to make sure it sits within
108 * the range called out in the specification
109 */
110 void checkLength(ProtocolVersion pv) throws SSLProtocolException {
111 // As of today all versions of TLS have a 32-byte maximum length.
112 // In the future we can do more here to support protocol versions
113 // that may have longer max lengths.
114 if (sessionId.length > MAX_LENGTH) {
115 throw new SSLProtocolException("Invalid session ID length (" +
116 sessionId.length + " bytes)");
117 }
118 }
119
120 }
|