• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1996, 2008, Oracle and/or its affiliates. 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle 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 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 
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  */
41 final
42 class SessionId
43 {
44     private byte sessionId [];          // max 32 bytes
45 
46     /** Constructs a new session ID ... perhaps for a rejoinable session */
SessionId(boolean isRejoinable, SecureRandom generator)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) */
SessionId(byte sessionId [])57     SessionId (byte sessionId [])
58         { this.sessionId = sessionId; }
59 
60     /** Returns the length of the ID, in bytes */
length()61     int length ()
62         { return sessionId.length; }
63 
64     /** Returns the bytes in the ID.  May be an empty array.  */
getId()65     byte [] getId ()
66     {
67         return sessionId.clone ();
68     }
69 
70     /** Returns the ID as a string */
toString()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 */
hashCode()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 */
equals(Object obj)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 }
115