jdk/src/share/classes/sun/security/jgss/GSSToken.java
author weijun
Tue, 07 May 2013 12:30:36 +0800
changeset 17435 ec797e955dca
parent 5506 202f599c92aa
child 24969 afa6934dd8e8
permissions -rw-r--r--
8010192: Enable native JGSS provider on Mac Reviewed-by: valeriep
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: 2
diff changeset
     2
 * Copyright (c) 2005, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
package sun.security.jgss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.EOFException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Utilities for processing GSS Tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public abstract class GSSToken {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * Copies an integer value to a byte array in little endian form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * @param value the integer value to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * @param array the byte array into which the integer must be copied. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * is assumed that the array will be large enough to hold the 4 bytes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * the integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static final void writeLittleEndian(int value, byte[] array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        writeLittleEndian(value, array, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Copies an integer value to a byte array in little endian form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * @param value the integer value to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param array the byte array into which the integer must be copied. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * is assumed that the array will be large enough to hold the 4 bytes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * the integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param pos the position at which to start writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static final void writeLittleEndian(int value, byte[] array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                               int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        array[pos++] = (byte)(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        array[pos++] = (byte)((value>>>8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        array[pos++] = (byte)((value>>>16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        array[pos++] = (byte)((value>>>24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static final void writeBigEndian(int value, byte[] array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        writeBigEndian(value, array, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static final void writeBigEndian(int value, byte[] array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                               int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        array[pos++] = (byte)((value>>>24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        array[pos++] = (byte)((value>>>16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        array[pos++] = (byte)((value>>>8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        array[pos++] = (byte)(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Reads an integer value from a byte array in little endian form. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * method allows the reading of two byte values as well as four bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * values both of which are needed in the Kerberos v5 GSS-API mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param data the array containing the bytes of the integer value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param pos the offset in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @size the number of bytes to read from the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @return the integer value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static final int readLittleEndian(byte[] data, int pos, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        int retVal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int shifter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        while (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            retVal += (data[pos] & 0xff) << shifter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            shifter += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static final int readBigEndian(byte[] data, int pos, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int retVal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int shifter = (size-1)*8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        while (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            retVal += (data[pos] & 0xff) << shifter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            shifter -= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Writes a two byte integer value to a OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param val the integer value. It will lose the high-order two bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param os the OutputStream to write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @throws IOException if an error occurs while writing to the OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public static final void writeInt(int val, OutputStream os)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        os.write(val>>>8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        os.write(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Writes a two byte integer value to a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param val the integer value. It will lose the high-order two bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param dest the byte array to write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param pos the offset to start writing to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public static final int writeInt(int val, byte[] dest, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        dest[pos++] = (byte)(val>>>8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        dest[pos++] = (byte)val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Reads a two byte integer value from an InputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param is the InputStream to read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @returns the integer value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @throws IOException if some errors occurs while reading the integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static final int readInt(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return (((0xFF & is.read()) << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                 | (0xFF & is.read()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Reads a two byte integer value from a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param src the byte arra to read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param pos the offset to start reading from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @returns the integer value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public static final int readInt(byte[] src, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return ((0xFF & src[pos])<<8 | (0xFF & src[pos+1]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Blocks till the required number of bytes have been read from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param is the InputStream to read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param buffer the buffer to store the bytes into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param throws EOFException if EOF is reached before all bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @throws IOException is an error occurs while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public static final void readFully(InputStream is, byte[] buffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        readFully(is, buffer, 0, buffer.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Blocks till the required number of bytes have been read from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param is the InputStream to read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param buffer the buffer to store the bytes into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param offset the offset to start storing at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param len the number of bytes to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param throws EOFException if EOF is reached before all bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @throws IOException is an error occurs while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public static final void readFully(InputStream is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                       byte[] buffer, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        int temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            temp = is.read(buffer, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (temp == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                throw new EOFException("Cannot read all "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                       + len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                       + " bytes needed to form this token!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            offset += temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            len -= temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public static final void debug(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        System.err.print(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public static final  String getHexBytes(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return getHexBytes(bytes, 0, bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public static final  String getHexBytes(byte[] bytes, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return getHexBytes(bytes, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public static final String getHexBytes(byte[] bytes, int pos, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        for (int i = pos; i < (pos+len); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            int b1 = (bytes[i]>>4) & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            int b2 = bytes[i] & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            sb.append(Integer.toHexString(b1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            sb.append(Integer.toHexString(b2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            sb.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
}