0N/A/*
2362N/A * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.security.auth.kerberos;
0N/A
0N/Aimport java.io.*;
0N/Aimport sun.security.krb5.Asn1Exception;
0N/Aimport sun.security.krb5.KrbException;
0N/Aimport sun.security.krb5.PrincipalName;
0N/Aimport sun.security.krb5.Realm;
0N/Aimport sun.security.util.*;
0N/A
0N/A/**
0N/A * This class encapsulates a Kerberos principal.
0N/A *
0N/A * @author Mayank Upadhyay
0N/A * @since 1.4
0N/A */
0N/A
0N/Apublic final class KerberosPrincipal
0N/A implements java.security.Principal, java.io.Serializable {
0N/A
0N/A private static final long serialVersionUID = -7374788026156829911L;
0N/A
0N/A //name types
0N/A
0N/A /**
0N/A * unknown name type.
0N/A */
0N/A
0N/A public static final int KRB_NT_UNKNOWN = 0;
0N/A
0N/A /**
0N/A * user principal name type.
0N/A */
0N/A
0N/A public static final int KRB_NT_PRINCIPAL = 1;
0N/A
0N/A /**
0N/A * service and other unique instance (krbtgt) name type.
0N/A */
0N/A public static final int KRB_NT_SRV_INST = 2;
0N/A
0N/A /**
0N/A * service with host name as instance (telnet, rcommands) name type.
0N/A */
0N/A
0N/A public static final int KRB_NT_SRV_HST = 3;
0N/A
0N/A /**
0N/A * service with host as remaining components name type.
0N/A */
0N/A
0N/A public static final int KRB_NT_SRV_XHST = 4;
0N/A
0N/A /**
0N/A * unique ID name type.
0N/A */
0N/A
0N/A public static final int KRB_NT_UID = 5;
0N/A
0N/A
0N/A private transient String fullName;
0N/A
0N/A private transient String realm;
0N/A
0N/A private transient int nameType;
0N/A
0N/A private static final char NAME_REALM_SEPARATOR = '@';
0N/A
0N/A /**
0N/A * Constructs a KerberosPrincipal from the provided string input. The
0N/A * name type for this principal defaults to
0N/A * {@link #KRB_NT_PRINCIPAL KRB_NT_PRINCIPAL}
0N/A * This string is assumed to contain a name in the format
0N/A * that is specified in Section 2.1.1. (Kerberos Principal Name Form) of
0N/A * <a href=http://www.ietf.org/rfc/rfc1964.txt> RFC 1964 </a>
0N/A * (for example, <i>duke@FOO.COM</i>, where <i>duke</i>
0N/A * represents a principal, and <i>FOO.COM</i> represents a realm).
0N/A *
0N/A * <p>If the input name does not contain a realm, the default realm
0N/A * is used. The default realm can be specified either in a Kerberos
0N/A * configuration file or via the java.security.krb5.realm
0N/A * system property. For more information,
0N/A * <a href="../../../../../technotes/guides/security/jgss/tutorials/index.html">
0N/A * Kerberos Requirements </a>
0N/A *
0N/A * @param name the principal name
0N/A * @throws IllegalArgumentException if name is improperly
0N/A * formatted, if name is null, or if name does not contain
0N/A * the realm to use and the default realm is not specified
0N/A * in either a Kerberos configuration file or via the
0N/A * java.security.krb5.realm system property.
0N/A */
0N/A public KerberosPrincipal(String name) {
0N/A
0N/A PrincipalName krb5Principal = null;
0N/A
0N/A try {
0N/A // Appends the default realm if it is missing
0N/A krb5Principal = new PrincipalName(name, KRB_NT_PRINCIPAL);
0N/A } catch (KrbException e) {
0N/A throw new IllegalArgumentException(e.getMessage());
0N/A }
0N/A nameType = KRB_NT_PRINCIPAL; // default name type
0N/A fullName = krb5Principal.toString();
0N/A realm = krb5Principal.getRealmString();
0N/A }
0N/A
0N/A /**
0N/A * Constructs a KerberosPrincipal from the provided string and
0N/A * name type input. The string is assumed to contain a name in the
0N/A * format that is specified in Section 2.1 (Mandatory Name Forms) of
0N/A * <a href=http://www.ietf.org/rfc/rfc1964.txt>RFC 1964</a>.
0N/A * Valid name types are specified in Section 6.2 (Principal Names) of
0N/A * <a href=http://www.ietf.org/rfc/rfc4120.txt>RFC 4120</a>.
0N/A * The input name must be consistent with the provided name type.
0N/A * (for example, <i>duke@FOO.COM</i>, is a valid input string for the
0N/A * name type, KRB_NT_PRINCIPAL where <i>duke</i>
0N/A * represents a principal, and <i>FOO.COM</i> represents a realm).
0N/A
0N/A * <p> If the input name does not contain a realm, the default realm
0N/A * is used. The default realm can be specified either in a Kerberos
0N/A * configuration file or via the java.security.krb5.realm
0N/A * system property. For more information, see
0N/A * <a href="../../../../../technotes/guides/security/jgss/tutorials/index.html">
0N/A * Kerberos Requirements</a>.
0N/A *
0N/A * @param name the principal name
0N/A * @param nameType the name type of the principal
0N/A * @throws IllegalArgumentException if name is improperly
0N/A * formatted, if name is null, if the nameType is not supported,
0N/A * or if name does not contain the realm to use and the default
0N/A * realm is not specified in either a Kerberos configuration
0N/A * file or via the java.security.krb5.realm system property.
0N/A */
0N/A
0N/A public KerberosPrincipal(String name, int nameType) {
0N/A
0N/A PrincipalName krb5Principal = null;
0N/A
0N/A try {
0N/A // Appends the default realm if it is missing
0N/A krb5Principal = new PrincipalName(name,nameType);
0N/A } catch (KrbException e) {
0N/A throw new IllegalArgumentException(e.getMessage());
0N/A }
0N/A
0N/A this.nameType = nameType;
0N/A fullName = krb5Principal.toString();
0N/A realm = krb5Principal.getRealmString();
0N/A }
0N/A /**
0N/A * Returns the realm component of this Kerberos principal.
0N/A *
0N/A * @return the realm component of this Kerberos principal.
0N/A */
0N/A public String getRealm() {
0N/A return realm;
0N/A }
0N/A
0N/A /**
0N/A * Returns a hashcode for this principal. The hash code is defined to
0N/A * be the result of the following calculation:
0N/A * <pre><code>
0N/A * hashCode = getName().hashCode();
0N/A * </code></pre>
0N/A *
0N/A * @return a hashCode() for the <code>KerberosPrincipal</code>
0N/A */
0N/A public int hashCode() {
0N/A return getName().hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Compares the specified Object with this Principal for equality.
0N/A * Returns true if the given object is also a
0N/A * <code>KerberosPrincipal</code> and the two
0N/A * <code>KerberosPrincipal</code> instances are equivalent.
0N/A * More formally two <code>KerberosPrincipal</code> instances are equal
0N/A * if the values returned by <code>getName()</code> are equal and the
0N/A * values returned by <code>getNameType()</code> are equal.
0N/A *
0N/A * @param other the Object to compare to
0N/A * @return true if the Object passed in represents the same principal
0N/A * as this one, false otherwise.
0N/A */
0N/A public boolean equals(Object other) {
0N/A
0N/A if (other == this)
0N/A return true;
0N/A
0N/A if (! (other instanceof KerberosPrincipal)) {
0N/A return false;
0N/A } else {
0N/A String myFullName = getName();
0N/A String otherFullName = ((KerberosPrincipal) other).getName();
0N/A if (nameType == ((KerberosPrincipal)other).nameType &&
0N/A myFullName.equals(otherFullName)) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Save the KerberosPrincipal object to a stream
0N/A *
0N/A * @serialData this <code>KerberosPrincipal</code> is serialized
0N/A * by writing out the PrincipalName and the
0N/A * realm in their DER-encoded form as specified in Section 5.2.2 of
0N/A * <a href=http://www.ietf.org/rfc/rfc4120.txt> RFC4120</a>.
0N/A */
0N/A
0N/A private void writeObject(ObjectOutputStream oos)
0N/A throws IOException {
0N/A
0N/A PrincipalName krb5Principal = null;
0N/A try {
0N/A krb5Principal = new PrincipalName(fullName,nameType);
0N/A oos.writeObject(krb5Principal.asn1Encode());
0N/A oos.writeObject(krb5Principal.getRealm().asn1Encode());
0N/A } catch (Exception e) {
0N/A IOException ioe = new IOException(e.getMessage());
0N/A ioe.initCause(e);
0N/A throw ioe;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Reads this object from a stream (i.e., deserializes it)
0N/A */
0N/A
0N/A private void readObject(ObjectInputStream ois)
0N/A throws IOException, ClassNotFoundException {
0N/A byte[] asn1EncPrincipal = (byte [])ois.readObject();
0N/A byte[] encRealm = (byte [])ois.readObject();
0N/A try {
0N/A PrincipalName krb5Principal = new PrincipalName(new
0N/A DerValue(asn1EncPrincipal));
0N/A realm = (new Realm(new DerValue(encRealm))).toString();
0N/A fullName = krb5Principal.toString() + NAME_REALM_SEPARATOR +
0N/A realm.toString();
0N/A nameType = krb5Principal.getNameType();
0N/A } catch (Exception e) {
0N/A IOException ioe = new IOException(e.getMessage());
0N/A ioe.initCause(e);
0N/A throw ioe;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The returned string corresponds to the single-string
0N/A * representation of a Kerberos Principal name as specified in
0N/A * Section 2.1 of <a href=http://www.ietf.org/rfc/rfc1964.txt>RFC 1964</a>.
0N/A *
0N/A * @return the principal name.
0N/A */
0N/A public String getName() {
0N/A return fullName;
0N/A }
0N/A
0N/A /**
0N/A * Returns the name type of the KerberosPrincipal. Valid name types
0N/A * are specified in Section 6.2 of
0N/A * <a href=http://www.ietf.org/rfc/rfc4120.txt> RFC4120</a>.
0N/A *
0N/A * @return the name type.
0N/A *
0N/A */
0N/A
0N/A public int getNameType() {
0N/A return nameType;
0N/A }
0N/A
0N/A // Inherits javadocs from Object
0N/A public String toString() {
0N/A return getName();
0N/A }
0N/A}