jdk/src/share/classes/sun/security/ssl/SessionId.java
author xuelei
Sat, 24 Nov 2012 04:09:19 -0800
changeset 14664 e71aa0962e70
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl Reviewed-by: xuelei Contributed-by: Florian Weimer <fweimer@redhat.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.SecureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Encapsulates an SSL session ID.  SSL Session IDs are not reused by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * servers during the lifetime of any sessions it created.  Sessions may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * be used by many connections, either concurrently (for example, two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * connections to a web server at the same time) or sequentially (over as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * long a time period as is allowed by a given server).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author Satish Dharmaraj
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class SessionId
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private byte sessionId [];          // max 32 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /** Constructs a new session ID ... perhaps for a rejoinable session */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    SessionId (boolean isRejoinable, SecureRandom generator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (isRejoinable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            // this will be unique, it's a timestamp plus much randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            sessionId = new RandomCookie (generator).random_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            sessionId = new byte [0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** Constructs a session ID from a byte array (max size 32 bytes) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    SessionId (byte sessionId [])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        { this.sessionId = sessionId; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /** Returns the length of the ID, in bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int length ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        { return sessionId.length; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /** Returns the bytes in the ID.  May be an empty array.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    byte [] getId ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    67
        return sessionId.clone ();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /** 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
    71
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public String toString ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        int             len = sessionId.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        StringBuffer    s = new StringBuffer (10 + 2 * len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        s.append ("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            s.append (0x0ff & sessionId [i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (i != (len - 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                s.append (", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        s.append ("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return s.toString ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /** 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
    89
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public int hashCode ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int     retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        for (int i = 0; i < sessionId.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            retval += sessionId [i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /** 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
   100
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public boolean equals (Object obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (!(obj instanceof SessionId))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        SessionId s = (SessionId) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        byte b [] = s.getId ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (b.length != sessionId.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        for (int i = 0; i < sessionId.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (b [i] != sessionId [i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}