jdk/src/share/classes/org/ietf/jgss/ChannelBinding.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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) 2000, 2001, 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 org.ietf.jgss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This class encapsulates the concept of caller-provided channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * binding information. Channel bindings are used to strengthen the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * quality with which peer entity authentication is provided during
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * context establishment.  They enable the GSS-API callers to bind the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * establishment of the security context to relevant characteristics
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * like addresses or to application specific data.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The caller initiating the security context must determine the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * appropriate channel binding values to set in the GSSContext object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The acceptor must provide an identical binding in order to validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * that received tokens possess correct channel-related characteristics.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Use of channel bindings is optional in GSS-API. ChannelBinding can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * set for the {@link GSSContext GSSContext} using the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * GSSContext#setChannelBinding(ChannelBinding) setChannelBinding} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * before the first call to {@link GSSContext#initSecContext(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * initSecContext} or {@link GSSContext#acceptSecContext(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * acceptSecContext} has been performed.  Unless the <code>setChannelBinding</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * method has been used to set the ChannelBinding for a GSSContext object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <code>null</code> ChannelBinding will be assumed. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Conceptually, the GSS-API concatenates the initiator and acceptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * address information, and the application supplied byte array to form an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * octet string.  The mechanism calculates a MIC over this octet string and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * binds the MIC to the context establishment token emitted by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <code>initSecContext</code> method of the <code>GSSContext</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * interface.  The same bindings are set by the context acceptor for its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <code>GSSContext</code> object and during processing of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <code>acceptSecContext</code> method a MIC is calculated in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * way. The calculated MIC is compared with that found in the token, and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * the MICs differ, accept will throw a <code>GSSException</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * major code set to {@link GSSException#BAD_BINDINGS BAD_BINDINGS}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * the context will not be established. Some mechanisms may include the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * actual channel binding data in the token (rather than just a MIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * applications should therefore not use confidential data as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * channel-binding components.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *  Individual mechanisms may impose additional constraints on addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *  that may appear in channel bindings.  For example, a mechanism may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *  verify that the initiator address field of the channel binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *  contains the correct network address of the host system.  Portable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *  applications should therefore ensure that they either provide correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *  information for the address fields, or omit setting of the addressing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *  information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @author Mayank Upadhyay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
public class ChannelBinding {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private InetAddress initiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private InetAddress acceptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private  byte[] appData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Create a ChannelBinding object with user supplied address information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * and data.  <code>null</code> values can be used for any fields which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * application does not want to specify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param initAddr the address of the context initiator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * <code>null</code> value can be supplied to indicate that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * application does not want to set this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param acceptAddr the address of the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * acceptor. <code>null</code> value can be supplied to indicate that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * the application does not want to set this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param appData application supplied data to be used as part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * channel bindings. <code>null</code> value can be supplied to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * indicate that the application does not want to set this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public ChannelBinding(InetAddress initAddr, InetAddress acceptAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        byte[] appData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        initiator = initAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        acceptor = acceptAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (appData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            this.appData = new byte[appData.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            java.lang.System.arraycopy(appData, 0, this.appData, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                appData.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
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
     * Creates a ChannelBinding object without any addressing information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param appData application supplied data to be used as part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * channel bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public ChannelBinding(byte[] appData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this(null, null, appData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Get the initiator's address for this channel binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @return the initiator's address. <code>null</code> is returned if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * the address has not been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public InetAddress getInitiatorAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return initiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Get the acceptor's address for this channel binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return the acceptor's address. null is returned if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * not been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public InetAddress getAcceptorAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return acceptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Get the application specified data for this channel binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return the application data being used as part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * ChannelBinding. <code>null</code> is returned if no application data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * has been specified for the channel binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public byte[] getApplicationData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (appData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        byte[] retVal = new byte[appData.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        System.arraycopy(appData, 0, retVal, 0, appData.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Compares two instances of ChannelBinding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param obj another ChannelBinding to compare this one with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @return true if the two ChannelBinding's contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * the same values for the initiator and acceptor addresses and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * application data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (this == obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (! (obj instanceof ChannelBinding))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        ChannelBinding cb = (ChannelBinding) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if ((initiator != null && cb.initiator == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            (initiator == null && cb.initiator != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (initiator != null && !initiator.equals(cb.initiator))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if ((acceptor != null && cb.acceptor == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            (acceptor == null && cb.acceptor != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (acceptor != null && !acceptor.equals(cb.acceptor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return Arrays.equals(appData, cb.appData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Returns a hashcode value for this ChannelBinding object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return a hashCode value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (initiator != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return initiator.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        else if (acceptor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return acceptor.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        else if (appData != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return new String(appData).hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}