jdk/src/share/classes/javax/security/auth/kerberos/KerberosPrincipal.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 13247 74902cfeb9c6
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, 2006, 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 javax.security.auth.kerberos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.security.krb5.Asn1Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.krb5.KrbException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.krb5.PrincipalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.krb5.Realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This class encapsulates a Kerberos principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author Mayank Upadhyay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public final class KerberosPrincipal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    implements java.security.Principal, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final long serialVersionUID = -7374788026156829911L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    //name types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * unknown name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static final int KRB_NT_UNKNOWN =   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
     * user principal name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final int KRB_NT_PRINCIPAL = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * service and other unique instance (krbtgt) name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static final int KRB_NT_SRV_INST =  2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * service with host name as instance (telnet, rcommands) name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static final int KRB_NT_SRV_HST =   3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * service with host as remaining components name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static final int KRB_NT_SRV_XHST =  4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * unique ID name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static final int KRB_NT_UID = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private transient String fullName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private transient String realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private transient int nameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static final char NAME_REALM_SEPARATOR = '@';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Constructs a KerberosPrincipal from the provided string input. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * name type for this  principal defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * {@link #KRB_NT_PRINCIPAL KRB_NT_PRINCIPAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * This string is assumed to contain a name in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * that is specified in Section 2.1.1. (Kerberos Principal Name Form) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <a href=http://www.ietf.org/rfc/rfc1964.txt> RFC 1964 </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * (for example, <i>duke@FOO.COM</i>, where <i>duke</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * represents a principal, and <i>FOO.COM</i> represents a realm).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <p>If the input name does not contain a realm, the default realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * is used. The default realm can be specified either in a Kerberos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * configuration file or via the java.security.krb5.realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * system property. For more information,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <a href="../../../../../technotes/guides/security/jgss/tutorials/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Kerberos Requirements </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param name the principal name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @throws IllegalArgumentException if name is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * formatted, if name is null, or if name does not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * the realm to use and the default realm is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * in either a Kerberos configuration file or via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * java.security.krb5.realm system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public KerberosPrincipal(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        PrincipalName krb5Principal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // Appends the default realm if it is missing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            krb5Principal = new PrincipalName(name, KRB_NT_PRINCIPAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        nameType = KRB_NT_PRINCIPAL;  // default name type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        fullName = krb5Principal.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        realm = krb5Principal.getRealmString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Constructs a KerberosPrincipal from the provided string and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * name type input.  The string is assumed to contain a name in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * format that is specified in Section 2.1 (Mandatory Name Forms) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <a href=http://www.ietf.org/rfc/rfc1964.txt>RFC 1964</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Valid name types are specified in Section 6.2 (Principal Names) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <a href=http://www.ietf.org/rfc/rfc4120.txt>RFC 4120</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * The input name must be consistent with the provided name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * (for example, <i>duke@FOO.COM</i>, is a valid input string for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * name type, KRB_NT_PRINCIPAL where <i>duke</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * represents a principal, and <i>FOO.COM</i> represents a realm).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <p> If the input name does not contain a realm, the default realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * is used. The default realm can be specified either in a Kerberos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * configuration file or via the java.security.krb5.realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * system property. For more information, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <a href="../../../../../technotes/guides/security/jgss/tutorials/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Kerberos Requirements</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param name the principal name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param nameType the name type of the principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @throws IllegalArgumentException if name is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * formatted, if name is null, if the nameType is not supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * or if name does not contain the realm to use and the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * realm is not specified in either a Kerberos configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * file or via the java.security.krb5.realm system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public KerberosPrincipal(String name, int nameType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        PrincipalName krb5Principal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            // Appends the default realm if it is missing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            krb5Principal  = new PrincipalName(name,nameType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        this.nameType = nameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        fullName = krb5Principal.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        realm = krb5Principal.getRealmString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Returns the realm component of this Kerberos principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return the realm component of this Kerberos principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public String getRealm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Returns a hashcode for this principal. The hash code is defined to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * be the result of the following  calculation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <pre><code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *  hashCode = getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * </code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return a hashCode() for the <code>KerberosPrincipal</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return getName().hashCode();
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
     * Compares the specified Object with this Principal for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Returns true if the given object is also a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <code>KerberosPrincipal</code> and the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <code>KerberosPrincipal</code> instances are equivalent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * More formally two <code>KerberosPrincipal</code> instances are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * if the values returned by <code>getName()</code> are equal and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * values returned by <code>getNameType()</code> are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param other the Object to compare to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return true if the Object passed in represents the same principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * as this one, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (other == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (! (other instanceof KerberosPrincipal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            String myFullName = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            String otherFullName = ((KerberosPrincipal) other).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (nameType == ((KerberosPrincipal)other).nameType &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                myFullName.equals(otherFullName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                 return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Save the KerberosPrincipal object to a stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @serialData this <code>KerberosPrincipal</code> is serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *          by writing out the PrincipalName and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *          realm in their DER-encoded form as specified in Section 5.2.2 of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *          <a href=http://www.ietf.org/rfc/rfc4120.txt> RFC4120</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private void writeObject(ObjectOutputStream oos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        PrincipalName krb5Principal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            krb5Principal  = new PrincipalName(fullName,nameType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            oos.writeObject(krb5Principal.asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            oos.writeObject(krb5Principal.getRealm().asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            IOException ioe = new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            ioe.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Reads this object from a stream (i.e., deserializes it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private void readObject(ObjectInputStream ois)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        byte[] asn1EncPrincipal = (byte [])ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        byte[] encRealm = (byte [])ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
           PrincipalName krb5Principal = new PrincipalName(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                                DerValue(asn1EncPrincipal));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
           realm = (new Realm(new DerValue(encRealm))).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
           fullName = krb5Principal.toString() + NAME_REALM_SEPARATOR +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                         realm.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
           nameType = krb5Principal.getNameType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            IOException ioe = new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            ioe.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * The returned string corresponds to the single-string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * representation of a Kerberos Principal name as specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Section 2.1 of <a href=http://www.ietf.org/rfc/rfc1964.txt>RFC 1964</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @return the principal name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return fullName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Returns the name type of the KerberosPrincipal. Valid name types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * are specified in Section 6.2 of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * <a href=http://www.ietf.org/rfc/rfc4120.txt> RFC4120</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @return the name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public int getNameType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return nameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    // Inherits javadocs from Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
}