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