blob: 557fbff30c842d87e35c9b778f6c5735deab28f1 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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
27package sun.security.ssl;
28
29import java.security.SecureRandom;
30
31/**
32 * Encapsulates an SSL session ID. SSL Session IDs are not reused by
33 * servers during the lifetime of any sessions it created. Sessions may
34 * be used by many connections, either concurrently (for example, two
35 * connections to a web server at the same time) or sequentially (over as
36 * long a time period as is allowed by a given server).
37 *
38 * @author Satish Dharmaraj
39 * @author David Brownell
40 */
41final
42class SessionId
43{
44 private byte sessionId []; // max 32 bytes
45
46 /** Constructs a new session ID ... perhaps for a rejoinable session */
47 SessionId (boolean isRejoinable, SecureRandom generator)
48 {
49 if (isRejoinable)
50 // this will be unique, it's a timestamp plus much randomness
51 sessionId = new RandomCookie (generator).random_bytes;
52 else
53 sessionId = new byte [0];
54 }
55
56 /** Constructs a session ID from a byte array (max size 32 bytes) */
57 SessionId (byte sessionId [])
58 { this.sessionId = sessionId; }
59
60 /** Returns the length of the ID, in bytes */
61 int length ()
62 { return sessionId.length; }
63
64 /** Returns the bytes in the ID. May be an empty array. */
65 byte [] getId ()
66 {
67 return (byte []) sessionId.clone ();
68 }
69
70 /** Returns the ID as a string */
71 public String toString ()
72 {
73 int len = sessionId.length;
74 StringBuffer s = new StringBuffer (10 + 2 * len);
75
76 s.append ("{");
77 for (int i = 0; i < len; i++) {
78 s.append (0x0ff & sessionId [i]);
79 if (i != (len - 1))
80 s.append (", ");
81 }
82 s.append ("}");
83 return s.toString ();
84 }
85
86
87 /** Returns a value which is the same for session IDs which are equal */
88 public int hashCode ()
89 {
90 int retval = 0;
91
92 for (int i = 0; i < sessionId.length; i++)
93 retval += sessionId [i];
94 return retval;
95 }
96
97 /** Returns true if the parameter is the same session ID */
98 public boolean equals (Object obj)
99 {
100 if (!(obj instanceof SessionId))
101 return false;
102
103 SessionId s = (SessionId) obj;
104 byte b [] = s.getId ();
105
106 if (b.length != sessionId.length)
107 return false;
108 for (int i = 0; i < sessionId.length; i++) {
109 if (b [i] != sessionId [i])
110 return false;
111 }
112 return true;
113 }
114}