jdk/src/java.base/share/classes/sun/security/ssl/SessionId.java
changeset 25859 3317bb8137f4
parent 24969 afa6934dd8e8
child 28565 48712ca501c1
equal deleted inserted replaced
25858:836adbf7a2cd 25859:3317bb8137f4
       
     1 /*
       
     2  * Copyright (c) 1996, 2012, 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 */
       
    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 sessionId.clone ();
       
    68     }
       
    69 
       
    70     /** Returns the ID as a string */
       
    71     @Override
       
    72     public String toString ()
       
    73     {
       
    74         int             len = sessionId.length;
       
    75         StringBuilder    sb = new StringBuilder (10 + 2 * len);
       
    76 
       
    77         sb.append("{");
       
    78         for (int i = 0; i < len; i++) {
       
    79             sb.append(0x0ff & sessionId[i]);
       
    80             if (i != (len - 1))
       
    81                 sb.append (", ");
       
    82         }
       
    83         sb.append("}");
       
    84         return sb.toString ();
       
    85     }
       
    86 
       
    87 
       
    88     /** Returns a value which is the same for session IDs which are equal */
       
    89     @Override
       
    90     public int hashCode ()
       
    91     {
       
    92         int     retval = 0;
       
    93 
       
    94         for (int i = 0; i < sessionId.length; i++)
       
    95             retval += sessionId [i];
       
    96         return retval;
       
    97     }
       
    98 
       
    99     /** Returns true if the parameter is the same session ID */
       
   100     @Override
       
   101     public boolean equals (Object obj)
       
   102     {
       
   103         if (!(obj instanceof SessionId))
       
   104             return false;
       
   105 
       
   106         SessionId s = (SessionId) obj;
       
   107         byte b [] = s.getId ();
       
   108 
       
   109         if (b.length != sessionId.length)
       
   110             return false;
       
   111         for (int i = 0; i < sessionId.length; i++) {
       
   112             if (b [i] != sessionId [i])
       
   113                 return false;
       
   114         }
       
   115         return true;
       
   116     }
       
   117 }