jdk/src/share/classes/sun/security/krb5/PrincipalName.java
author weijun
Wed, 20 May 2009 10:12:00 +0800
changeset 2918 395b9ffa7cc6
parent 2 90ce3da70b43
child 3949 00603a93b589
permissions -rw-r--r--
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2918
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
     2
 * Portions Copyright 2000-2009 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package sun.security.krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.krb5.internal.ccache.CCacheOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class encapsulates a Kerberos principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class PrincipalName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    //name types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Name type not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static final int KRB_NT_UNKNOWN =   0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Just the name of the principal as in DCE, or for users
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)
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)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static final int KRB_NT_SRV_HST =   3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Service with host as remaining components
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static final int KRB_NT_SRV_XHST =  4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Unique ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static final int KRB_NT_UID = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * TGS Name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static final String TGS_DEFAULT_SRV_NAME = "krbtgt";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static final int TGS_DEFAULT_NT = KRB_NT_SRV_INST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static final char NAME_COMPONENT_SEPARATOR = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static final char NAME_REALM_SEPARATOR = '@';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static final char REALM_COMPONENT_SEPARATOR = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static final String NAME_COMPONENT_SEPARATOR_STR = "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static final String NAME_REALM_SEPARATOR_STR = "@";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static final String REALM_COMPONENT_SEPARATOR_STR = ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private int nameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private String[] nameStrings;  // Principal names don't mutate often
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private Realm nameRealm;  // optional; a null realm means use default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // Note: the nameRealm is not included in the default ASN.1 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // salt for principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private String salt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    protected PrincipalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public PrincipalName(String[] nameParts, int type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        throws IllegalArgumentException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (nameParts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new IllegalArgumentException("Null input not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        nameStrings = new String[nameParts.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        System.arraycopy(nameParts, 0, nameStrings, 0, nameParts.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        nameType = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        nameRealm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public PrincipalName(String[] nameParts) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this(nameParts, KRB_NT_UNKNOWN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        PrincipalName pName = new PrincipalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        pName.nameType = nameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (nameStrings != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            pName.nameStrings =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                new String[nameStrings.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                System.arraycopy(nameStrings,0,pName.nameStrings,0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                nameStrings.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (nameRealm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            pName.nameRealm = (Realm)nameRealm.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return pName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Added to workaround a bug where the equals method that takes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * PrincipalName is not being called but Object.equals(Object) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * being called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (o instanceof PrincipalName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return equals((PrincipalName)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public boolean equals(PrincipalName other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (!equalsWithoutRealm(other)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if ((nameRealm != null && other.nameRealm == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            (nameRealm == null && other.nameRealm != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (nameRealm != null && other.nameRealm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (!nameRealm.equals(other.nameRealm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    boolean equalsWithoutRealm(PrincipalName other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (nameType != KRB_NT_UNKNOWN &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            other.nameType != KRB_NT_UNKNOWN &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            nameType != other.nameType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if ((nameStrings != null && other.nameStrings == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            (nameStrings == null && other.nameStrings != 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 (nameStrings != null && other.nameStrings != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (nameStrings.length != other.nameStrings.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            for (int i = 0; i < nameStrings.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                if (!nameStrings[i].equals(other.nameStrings[i]))
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Returns the ASN.1 encoding of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * PrincipalName    ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *          name-type       [0] Int32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *          name-string     [1] SEQUENCE OF KerberosString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * KerberosString   ::= GeneralString (IA5String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * </xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param encoding a Der-encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @exception Asn1Exception if an error occurs while decoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @exception Asn1Exception if there is an ASN1 encoding error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @exception IOException if an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @exception IllegalArgumentException if encoding is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public PrincipalName(DerValue encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        nameRealm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        DerValue der;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (encoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            throw new IllegalArgumentException("Null input not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (encoding.getTag() != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if ((der.getTag() & 0x1F) == 0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            BigInteger bint = der.getData().getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            nameType = bint.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if ((der.getTag() & 0x01F) == 0x01) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (subDer.getTag() != DerValue.tag_SequenceOf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            Vector<String> v = new Vector<String> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            DerValue subSubDer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            while(subDer.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                subSubDer = subDer.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                v.addElement(subSubDer.getGeneralString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (v.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                nameStrings = new String[v.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                v.copyInto(nameStrings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                nameStrings = new String[] {""};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Parse (unmarshal) a <code>PrincipalName</code> from a DER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @exception Asn1Exception on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param data the Der input stream value, which contains one or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * more marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param optional indicate if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return an instance of <code>PrincipalName</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public static PrincipalName parse(DerInputStream data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                                      byte explicitTag, boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                      optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if ((optional) && (((byte)data.peekByte() & (byte)0x1F) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                           explicitTag))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (explicitTag != (der.getTag() & (byte)0x1F))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return new PrincipalName(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
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
    // This is protected because the definition of a principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    // string is fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    // XXX Error checkin consistent with MIT krb5_parse_name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    // Code repetition, realm parsed again by class Realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    protected static String[] parseName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        Vector<String> tempStrings = new Vector<String> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        String temp = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int componentStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        String component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        while (i < temp.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (temp.charAt(i) == NAME_COMPONENT_SEPARATOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                 * If this separator is escaped then don't treat it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                 * as a separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                if (i > 0 && temp.charAt(i - 1) == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    temp = temp.substring(0, i - 1) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        temp.substring(i, temp.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    if (componentStart < i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        component = temp.substring(componentStart, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        tempStrings.addElement(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    componentStart = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                if (temp.charAt(i) == NAME_REALM_SEPARATOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                     * If this separator is escaped then don't treat it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                     * as a separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    if (i > 0 && temp.charAt(i - 1) == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        temp = temp.substring(0, i - 1) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                            temp.substring(i, temp.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        if (componentStart < i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            component = temp.substring(componentStart, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                            tempStrings.addElement(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        componentStart = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (i == temp.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (componentStart < i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            component = temp.substring(componentStart, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            tempStrings.addElement(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        String[] result = new String[tempStrings.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        tempStrings.copyInto(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public PrincipalName(String name, int type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        throws RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            throw new IllegalArgumentException("Null name not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        String[] nameParts = parseName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        Realm tempRealm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        String realmString = Realm.parseRealmAtSeparator(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (realmString == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                Config config = Config.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                realmString = config.getDefaultRealm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                RealmException re =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    new RealmException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                re.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                throw re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (realmString != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            tempRealm = new Realm(realmString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        case KRB_NT_SRV_HST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            if (nameParts.length >= 2) {
2918
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   384
                String hostName = nameParts[1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                try {
2918
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   386
                    // RFC4120 does not recommend canonicalizing a hostname.
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   387
                    // However, for compatibility reason, we will try
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   388
                    // canonicalize it and see if the output looks better.
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   389
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   390
                    String canonicalized = (InetAddress.getByName(hostName)).
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   391
                            getCanonicalHostName();
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   392
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   393
                    // Looks if canonicalized is a longer format of hostName,
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   394
                    // we accept cases like
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   395
                    //     bunny -> bunny.rabbit.hole
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   396
                    if (canonicalized.toLowerCase()
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   397
                            .startsWith(hostName.toLowerCase()+".")) {
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   398
                        hostName = canonicalized;
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   399
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                } catch (UnknownHostException e) {
2918
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   401
                    // no canonicalization, use old
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                }
2918
395b9ffa7cc6 6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents: 2
diff changeset
   403
                nameParts[1] = hostName.toLowerCase();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            nameStrings = nameParts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            nameType = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                // We will try to get realm name from the mapping in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                // the configuration. If it is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                // we will use the default realm. This nametype does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                // not allow a realm to be specified. The name string must of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                // the form service@host and this is internally changed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                // service/host by Kerberos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            String mapRealm =  mapHostToRealm(nameParts[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (mapRealm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                nameRealm = new Realm(mapRealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                nameRealm = tempRealm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        case KRB_NT_UNKNOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        case KRB_NT_PRINCIPAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        case KRB_NT_SRV_INST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        case KRB_NT_SRV_XHST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        case KRB_NT_UID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            nameStrings = nameParts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            nameType = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            nameRealm = tempRealm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            throw new IllegalArgumentException("Illegal name type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public PrincipalName(String name) throws RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        this(name, KRB_NT_UNKNOWN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public PrincipalName(String name, String realm) throws RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        this(name, KRB_NT_UNKNOWN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        nameRealm = new Realm(realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public String getRealmAsString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        return getRealmString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    public String getPrincipalNameAsString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        StringBuffer temp = new StringBuffer(nameStrings[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        for (int i = 1; i < nameStrings.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            temp.append(nameStrings[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return temp.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        return toString().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        return toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public int getNameType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        return nameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    public String[] getNameStrings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        return nameStrings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    public byte[][] toByteArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        byte[][] result = new byte[nameStrings.length][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        for (int i = 0; i < nameStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            result[i] = new byte[nameStrings[i].length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            result[i] = nameStrings[i].getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public String getRealmString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (nameRealm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            return nameRealm.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public Realm getRealm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return nameRealm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    public void setRealm(Realm new_nameRealm) throws RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        nameRealm = new_nameRealm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    public void setRealm(String realmsString) throws RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        nameRealm = new Realm(realmsString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public String getSalt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (salt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            StringBuffer salt = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            if (nameRealm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                salt.append(nameRealm.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            for (int i = 0; i < nameStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                salt.append(nameStrings[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            return salt.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        return salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    public void setSalt(String salt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        this.salt = salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        StringBuffer str = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        for (int i = 0; i < nameStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (i > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                str.append("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            str.append(nameStrings[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (nameRealm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            str.append("@");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            str.append(nameRealm.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        return str.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    public String getNameString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        StringBuffer str = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        for (int i = 0; i < nameStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            if (i > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                str.append("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            str.append(nameStrings[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        return str.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Encodes a <code>PrincipalName</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @return the byte array of the encoded PrncipalName object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        BigInteger bint = BigInteger.valueOf(this.nameType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        temp.putInteger(bint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        DerValue der[] = new DerValue[nameStrings.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        for (int i = 0; i < nameStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            der[i] = new DerValue(DerValue.tag_GeneralString, nameStrings[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        temp.putSequence(der);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return temp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Checks if two <code>PrincipalName</code> objects have identical values in their corresponding data fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @param pname the other <code>PrincipalName</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @return true if two have identical values, otherwise, return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    // It is used in <code>sun.security.krb5.internal.ccache</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public boolean match(PrincipalName pname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        boolean matched = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        //name type is just a hint, no two names can be the same ignoring name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        // if (this.nameType != pname.nameType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        //      matched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if ((this.nameRealm != null) && (pname.nameRealm != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            if (!(this.nameRealm.toString().equalsIgnoreCase(pname.nameRealm.toString()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                matched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (this.nameStrings.length != pname.nameStrings.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            matched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            for (int i = 0; i < this.nameStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                if (!(this.nameStrings[i].equalsIgnoreCase(pname.nameStrings[i]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    matched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return matched;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Writes data field values of <code>PrincipalName</code> in FCC format to an output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @param cos a <code>CCacheOutputStream</code> for writing data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @exception IOException if an I/O exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @see sun.security.krb5.internal.ccache.CCacheOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    public void writePrincipal(CCacheOutputStream cos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        cos.write32(nameType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        cos.write32(nameStrings.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        if (nameRealm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            byte[] realmBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            realmBytes = nameRealm.toString().getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            cos.write32(realmBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            cos.write(realmBytes, 0, realmBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        byte[] bytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        for (int i = 0; i < nameStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            bytes = nameStrings[i].getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            cos.write32(bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            cos.write(bytes, 0, bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * Creates a KRB_NT_SRV_INST name from the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * name components and realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @param primary the primary component of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @param instance the instance component of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @param realm the realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @throws KrbException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    protected PrincipalName(String primary, String instance, String realm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                            int type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        if (type != KRB_NT_SRV_INST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            throw new KrbException(Krb5.KRB_ERR_GENERIC, "Bad name type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        String[] nParts = new String[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        nParts[0] = primary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        nParts[1] = instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        this.nameStrings = nParts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        this.nameRealm = new Realm(realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        this.nameType = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Returns the instance component of a name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * In a multi-component name such as a KRB_NT_SRV_INST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * name, the second component is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * Null is returned if there are not two or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * components in the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @returns instance component of a multi-component name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    public String getInstanceComponent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        if (nameStrings != null && nameStrings.length >= 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                return new String(nameStrings[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    static String mapHostToRealm(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            String subname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            Config c = Config.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            if ((result = c.getDefault(name, "domain_realm")) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                for (int i = 1; i < name.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    if ((name.charAt(i) == '.') && (i != name.length() - 1)) { //mapping could be .ibm.com = AUSTIN.IBM.COM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                        subname = name.substring(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                        result = c.getDefault(subname, "domain_realm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                        if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                            subname = name.substring(i + 1);      //or mapping could be ibm.com = AUSTIN.IBM.COM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                            result = c.getDefault(subname, "domain_realm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                            if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
}