src/java.security.jgss/share/classes/sun/security/krb5/PrincipalName.java
author mchung
Mon, 22 Oct 2018 17:00:04 -0700
changeset 52220 9c260a6b6471
parent 51979 3241975b1830
child 55258 d65d3c37232c
child 58678 9cf78a70fa4f
permissions -rw-r--r--
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference Reviewed-by: dholmes, thartmann
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51979
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5455
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5455
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5455
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5455
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5455
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
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;
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10141
diff changeset
    38
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.math.BigInteger;
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
    41
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.krb5.internal.ccache.CCacheOutputStream;
3949
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2918
diff changeset
    43
import sun.security.krb5.internal.util.KerberosString;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
    47
 * Implements the ASN.1 PrincipalName type and its realm in a single class.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
    48
 * <pre>{@code
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
    49
 *    Realm           ::= KerberosString
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
    50
 *
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
    51
 *    PrincipalName   ::= SEQUENCE {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
    52
 *            name-type       [0] Int32,
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
    53
 *            name-string     [1] SEQUENCE OF KerberosString
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
    54
 *    }
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
    55
 * }</pre>
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
    56
 * This class is immutable.
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
    57
 * @see Realm
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
    59
public class PrincipalName implements Cloneable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    //name types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Name type not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static final int KRB_NT_UNKNOWN =   0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Just the name of the principal as in DCE, or for users
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static final int KRB_NT_PRINCIPAL = 1;
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 and other unique instance (krbtgt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static final int KRB_NT_SRV_INST =  2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Service with host name as instance (telnet, rcommands)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static final int KRB_NT_SRV_HST =   3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Service with host as remaining components
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static final int KRB_NT_SRV_XHST =  4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Unique ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static final int KRB_NT_UID = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * TGS Name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static final String TGS_DEFAULT_SRV_NAME = "krbtgt";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static final int TGS_DEFAULT_NT = KRB_NT_SRV_INST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static final char NAME_COMPONENT_SEPARATOR = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static final char NAME_REALM_SEPARATOR = '@';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public static final char REALM_COMPONENT_SEPARATOR = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static final String NAME_COMPONENT_SEPARATOR_STR = "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static final String NAME_REALM_SEPARATOR_STR = "@";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public static final String REALM_COMPONENT_SEPARATOR_STR = ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   107
    // Instance fields.
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   108
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   109
    /**
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   110
     * The name type, from PrincipalName's name-type field.
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   111
     */
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   112
    private final int nameType;
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   113
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   114
    /**
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   115
     * The name strings, from PrincipalName's name-strings field. This field
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   116
     * must be neither null nor empty. Each entry of it must also be neither
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   117
     * null nor empty. Make sure to clone the field when it's passed in or out.
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   118
     */
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   119
    private final String[] nameStrings;
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   120
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   121
    /**
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   122
     * The realm this principal belongs to.
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   123
     */
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   124
    private final Realm nameRealm;      // not null
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   125
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   126
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   127
    /**
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   128
     * When constructing a PrincipalName, whether the realm is included in
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   129
     * the input, or deduced from default realm or domain-realm mapping.
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   130
     */
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   131
    private final boolean realmDeduced;
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   132
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   133
    // cached default salt, not used in clone
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   134
    private transient String salt = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   136
    // There are 3 basic constructors. All other constructors must call them.
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   137
    // All basic constructors must call validateNameStrings.
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   138
    // 1. From name components
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   139
    // 2. From name
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   140
    // 3. From DER encoding
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   142
    /**
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   143
     * Creates a PrincipalName.
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   144
     */
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   145
    public PrincipalName(int nameType, String[] nameStrings, Realm nameRealm) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   146
        if (nameRealm == null) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   147
            throw new IllegalArgumentException("Null realm not allowed");
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   148
        }
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   149
        validateNameStrings(nameStrings);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   150
        this.nameType = nameType;
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   151
        this.nameStrings = nameStrings.clone();
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   152
        this.nameRealm = nameRealm;
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   153
        this.realmDeduced = false;
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   154
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   156
    // This method is called by Windows NativeCred.c
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   157
    public PrincipalName(String[] nameParts, String realm) throws RealmException {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   158
        this(KRB_NT_UNKNOWN, nameParts, new Realm(realm));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   161
    // Validate a nameStrings argument
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   162
    private static void validateNameStrings(String[] ns) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   163
        if (ns == null) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   164
            throw new IllegalArgumentException("Null nameStrings not allowed");
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   165
        }
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   166
        if (ns.length == 0) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   167
            throw new IllegalArgumentException("Empty nameStrings not allowed");
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   168
        }
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   169
        for (String s: ns) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   170
            if (s == null) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   171
                throw new IllegalArgumentException("Null nameString not allowed");
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   172
            }
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   173
            if (s.isEmpty()) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   174
                throw new IllegalArgumentException("Empty nameString not allowed");
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   175
            }
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   176
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public Object clone() {
5455
19d2395cfe3e 6856069: PrincipalName.clone() does not invoke super.clone()
weijun
parents: 3949
diff changeset
   180
        try {
19d2395cfe3e 6856069: PrincipalName.clone() does not invoke super.clone()
weijun
parents: 3949
diff changeset
   181
            PrincipalName pName = (PrincipalName) super.clone();
52220
9c260a6b6471 8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents: 51979
diff changeset
   182
            UNSAFE.putReference(this, NAME_STRINGS_OFFSET, nameStrings.clone());
5455
19d2395cfe3e 6856069: PrincipalName.clone() does not invoke super.clone()
weijun
parents: 3949
diff changeset
   183
            return pName;
19d2395cfe3e 6856069: PrincipalName.clone() does not invoke super.clone()
weijun
parents: 3949
diff changeset
   184
        } catch (CloneNotSupportedException ex) {
19d2395cfe3e 6856069: PrincipalName.clone() does not invoke super.clone()
weijun
parents: 3949
diff changeset
   185
            throw new AssertionError("Should never happen");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   189
    private static final long NAME_STRINGS_OFFSET;
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 33282
diff changeset
   190
    private static final jdk.internal.misc.Unsafe UNSAFE;
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   191
    static {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   192
        try {
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 33282
diff changeset
   193
            jdk.internal.misc.Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe();
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   194
            NAME_STRINGS_OFFSET = unsafe.objectFieldOffset(
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   195
                    PrincipalName.class.getDeclaredField("nameStrings"));
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   196
            UNSAFE = unsafe;
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   197
        } catch (ReflectiveOperationException e) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   198
            throw new Error(e);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   199
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   202
    @Override
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   203
    public boolean equals(Object o) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   204
        if (this == o) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   205
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   207
        if (o instanceof PrincipalName) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   208
            PrincipalName other = (PrincipalName)o;
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   209
            return nameRealm.equals(other.nameRealm) &&
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   210
                    Arrays.equals(nameStrings, other.nameStrings);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   212
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Returns the ASN.1 encoding of the
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
   217
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * PrincipalName    ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *          name-type       [0] Int32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *          name-string     [1] SEQUENCE OF KerberosString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * KerberosString   ::= GeneralString (IA5String)
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
   224
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   232
     * @param encoding DER-encoded PrincipalName (without Realm)
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   233
     * @param realm the realm for this name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @exception Asn1Exception if an error occurs while decoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @exception Asn1Exception if there is an ASN1 encoding error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @exception IOException if an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @exception IllegalArgumentException if encoding is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   241
    public PrincipalName(DerValue encoding, Realm realm)
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   242
            throws Asn1Exception, IOException {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   243
        if (realm == null) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   244
            throw new IllegalArgumentException("Null realm not allowed");
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   245
        }
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   246
        realmDeduced = false;
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   247
        nameRealm = realm;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        DerValue der;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (encoding == null) {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   250
            throw new IllegalArgumentException("Null encoding not allowed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (encoding.getTag() != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if ((der.getTag() & 0x1F) == 0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            BigInteger bint = der.getData().getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            nameType = bint.intValue();
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
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if ((der.getTag() & 0x01F) == 0x01) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (subDer.getTag() != DerValue.tag_SequenceOf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7183
diff changeset
   268
            Vector<String> v = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            DerValue subSubDer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            while(subDer.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                subSubDer = subDer.getData().getDerValue();
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   272
                String namePart = new KerberosString(subSubDer).toString();
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   273
                v.addElement(namePart);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   275
            nameStrings = new String[v.size()];
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   276
            v.copyInto(nameStrings);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   277
            validateNameStrings(nameStrings);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Parse (unmarshal) a <code>PrincipalName</code> from a DER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @exception Asn1Exception on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param data the Der input stream value, which contains one or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * more marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param optional indicate if this data field is optional
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   294
     * @param realm the realm for the name
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   295
     * @return an instance of <code>PrincipalName</code>, or null if the
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   296
     * field is optional and missing.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public static PrincipalName parse(DerInputStream data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                      byte explicitTag, boolean
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   300
                                      optional,
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   301
                                      Realm realm)
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   302
        throws Asn1Exception, IOException, RealmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if ((optional) && (((byte)data.peekByte() & (byte)0x1F) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                           explicitTag))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        DerValue der = data.getDerValue();
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   308
        if (explicitTag != (der.getTag() & (byte)0x1F)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   310
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            DerValue subDer = der.getData().getDerValue();
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   312
            if (realm == null) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   313
                realm = Realm.getDefault();
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   314
            }
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   315
            return new PrincipalName(subDer, realm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    // XXX Error checkin consistent with MIT krb5_parse_name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    // Code repetition, realm parsed again by class Realm
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   322
    private static String[] parseName(String name) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7183
diff changeset
   324
        Vector<String> tempStrings = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        String temp = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        int componentStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        String component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        while (i < temp.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            if (temp.charAt(i) == NAME_COMPONENT_SEPARATOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                 * If this separator is escaped then don't treat it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                 * as a separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if (i > 0 && temp.charAt(i - 1) == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    temp = temp.substring(0, i - 1) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        temp.substring(i, temp.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                else {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   342
                    if (componentStart <= i) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        component = temp.substring(componentStart, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        tempStrings.addElement(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    componentStart = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   348
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                if (temp.charAt(i) == NAME_REALM_SEPARATOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                     * If this separator is escaped then don't treat it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                     * as a separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    if (i > 0 && temp.charAt(i - 1) == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        temp = temp.substring(0, i - 1) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                            temp.substring(i, temp.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        if (componentStart < i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                            component = temp.substring(componentStart, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                            tempStrings.addElement(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        componentStart = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                }
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   367
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   371
        if (i == temp.length()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            component = temp.substring(componentStart, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            tempStrings.addElement(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        String[] result = new String[tempStrings.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        tempStrings.copyInto(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   381
    /**
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   382
     * Constructs a PrincipalName from a string.
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   383
     * @param name the name
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   384
     * @param type the type
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   385
     * @param realm the realm, null if not known. Note that when realm is not
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   386
     * null, it will be always used even if there is a realm part in name. When
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   387
     * realm is null, will read realm part from name, or try to map a realm
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   388
     * (for KRB_NT_SRV_HST), or use the default realm, or fail
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   389
     * @throws RealmException
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   390
     */
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   391
    public PrincipalName(String name, int type, String realm)
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   392
            throws RealmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            throw new IllegalArgumentException("Null name not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        String[] nameParts = parseName(name);
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   397
        validateNameStrings(nameParts);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   398
        if (realm == null) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   399
            realm = Realm.parseRealmAtSeparator(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   401
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   402
        // No realm info from parameter and string, must deduce later
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   403
        realmDeduced = realm == null;
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   404
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        case KRB_NT_SRV_HST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            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
   408
                String hostName = nameParts[1];
51979
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   409
                Boolean option;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                try {
51979
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   411
                    // If true, try canonicalizing and accept it if it starts
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   412
                    // with the short name. Otherwise, never. Default true.
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   413
                    option = Config.getInstance().getBooleanObject(
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   414
                            "libdefaults", "dns_canonicalize_hostname");
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   415
                } catch (KrbException e) {
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   416
                    option = null;
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   417
                }
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   418
                if (option != Boolean.FALSE) {
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   419
                    try {
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   420
                        // RFC4120 does not recommend canonicalizing a hostname.
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   421
                        // However, for compatibility reason, we will try
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   422
                        // canonicalizing it and see if the output looks better.
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
   423
51979
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   424
                        String canonicalized = (InetAddress.getByName(hostName)).
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   425
                                getCanonicalHostName();
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   426
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   427
                        // Looks if canonicalized is a longer format of hostName,
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   428
                        // we accept cases like
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   429
                        //     bunny -> bunny.rabbit.hole
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   430
                        if (canonicalized.toLowerCase(Locale.ENGLISH).startsWith(
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   431
                                hostName.toLowerCase(Locale.ENGLISH) + ".")) {
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   432
                            hostName = canonicalized;
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   433
                        }
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   434
                    } catch (UnknownHostException | SecurityException e) {
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   435
                        // not canonicalized or no permission to do so, use old
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
   436
                    }
51979
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   437
                    if (hostName.endsWith(".")) {
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   438
                        hostName = hostName.substring(0, hostName.length() - 1);
3241975b1830 8210821: Support dns_canonicalize_hostname in krb5.conf
weijun
parents: 47216
diff changeset
   439
                    }
38438
6e6b1c38fdc0 8149521: automatic discovery of LDAP servers with Kerberos authentication
weijun
parents: 33674
diff changeset
   440
                }
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10141
diff changeset
   441
                nameParts[1] = hostName.toLowerCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            nameStrings = nameParts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            nameType = type;
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   445
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   446
            if (realm != null) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   447
                nameRealm = new Realm(realm);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   448
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                // We will try to get realm name from the mapping in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                // the configuration. If it is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                // we will use the default realm. This nametype does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                // not allow a realm to be specified. The name string must of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                // the form service@host and this is internally changed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                // service/host by Kerberos
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   455
                String mapRealm =  mapHostToRealm(nameParts[1]);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   456
                if (mapRealm != null) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   457
                    nameRealm = new Realm(mapRealm);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   458
                } else {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   459
                    nameRealm = Realm.getDefault();
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   460
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        case KRB_NT_UNKNOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        case KRB_NT_PRINCIPAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        case KRB_NT_SRV_INST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        case KRB_NT_SRV_XHST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        case KRB_NT_UID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            nameStrings = nameParts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            nameType = type;
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   470
            if (realm != null) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   471
                nameRealm = new Realm(realm);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   472
            } else {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   473
                nameRealm = Realm.getDefault();
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   474
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            throw new IllegalArgumentException("Illegal name type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   481
    public PrincipalName(String name, int type) throws RealmException {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   482
        this(name, type, (String)null);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   483
    }
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   484
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public PrincipalName(String name) throws RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        this(name, KRB_NT_UNKNOWN);
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 PrincipalName(String name, String realm) throws RealmException {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   490
        this(name, KRB_NT_UNKNOWN, realm);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   491
    }
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   492
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   493
    public static PrincipalName tgsService(String r1, String r2)
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   494
            throws KrbException {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   495
        return new PrincipalName(PrincipalName.KRB_NT_SRV_INST,
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   496
                new String[] {PrincipalName.TGS_DEFAULT_SRV_NAME, r1},
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   497
                new Realm(r2));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    public String getRealmAsString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        return getRealmString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public String getPrincipalNameAsString() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   505
        StringBuilder temp = new StringBuilder(nameStrings[0]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        for (int i = 1; i < nameStrings.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            temp.append(nameStrings[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        return temp.toString();
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 int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        return toString().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        return toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public int getNameType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        return nameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    public String[] getNameStrings() {
16847
c8b6eab9a611 8005460: [findbugs] Probably returned array should be cloned
weijun
parents: 14342
diff changeset
   524
        return nameStrings.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    public byte[][] toByteArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        byte[][] result = new byte[nameStrings.length][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        for (int i = 0; i < nameStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            result[i] = new byte[nameStrings[i].length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            result[i] = nameStrings[i].getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    public String getRealmString() {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   537
        return nameRealm.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    public Realm getRealm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        return nameRealm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    public String getSalt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (salt == null) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   546
            StringBuilder salt = new StringBuilder();
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   547
            salt.append(nameRealm.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            for (int i = 0; i < nameStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                salt.append(nameStrings[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            return salt.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        return salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public String toString() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   557
        StringBuilder str = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        for (int i = 0; i < nameStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (i > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                str.append("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            str.append(nameStrings[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   563
        str.append("@");
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   564
        str.append(nameRealm.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        return str.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public String getNameString() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   569
        StringBuilder str = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        for (int i = 0; i < nameStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            if (i > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                str.append("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            str.append(nameStrings[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return str.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /**
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   579
     * Encodes a <code>PrincipalName</code> object. Note that only the type and
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   580
     * names are encoded. To encode the realm, call getRealm().asn1Encode().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @return the byte array of the encoded PrncipalName object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        BigInteger bint = BigInteger.valueOf(this.nameType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        temp.putInteger(bint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        temp = new DerOutputStream();
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
   593
        DerValue[] der = new DerValue[nameStrings.length];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        for (int i = 0; i < nameStrings.length; i++) {
3949
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2918
diff changeset
   595
            der[i] = new KerberosString(nameStrings[i]).toDerValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        temp.putSequence(der);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        return temp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * Checks if two <code>PrincipalName</code> objects have identical values in their corresponding data fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @param pname the other <code>PrincipalName</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @return true if two have identical values, otherwise, return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    // It is used in <code>sun.security.krb5.internal.ccache</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    public boolean match(PrincipalName pname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        boolean matched = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        //name type is just a hint, no two names can be the same ignoring name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        // if (this.nameType != pname.nameType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        //      matched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if ((this.nameRealm != null) && (pname.nameRealm != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            if (!(this.nameRealm.toString().equalsIgnoreCase(pname.nameRealm.toString()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                matched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (this.nameStrings.length != pname.nameStrings.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            matched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            for (int i = 0; i < this.nameStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                if (!(this.nameStrings[i].equalsIgnoreCase(pname.nameStrings[i]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    matched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        return matched;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Writes data field values of <code>PrincipalName</code> in FCC format to an output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @param cos a <code>CCacheOutputStream</code> for writing data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @exception IOException if an I/O exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @see sun.security.krb5.internal.ccache.CCacheOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    public void writePrincipal(CCacheOutputStream cos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        cos.write32(nameType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        cos.write32(nameStrings.length);
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   645
        byte[] realmBytes = null;
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   646
        realmBytes = nameRealm.toString().getBytes();
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   647
        cos.write32(realmBytes.length);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10369
diff changeset
   648
        cos.write(realmBytes, 0, realmBytes.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        byte[] bytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        for (int i = 0; i < nameStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            bytes = nameStrings[i].getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            cos.write32(bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            cos.write(bytes, 0, bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * Returns the instance component of a name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * In a multi-component name such as a KRB_NT_SRV_INST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * name, the second component is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * Null is returned if there are not two or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * components in the name.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
   663
     *
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
   664
     * @return instance component of a multi-component name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    public String getInstanceComponent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        if (nameStrings != null && nameStrings.length >= 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                return new String(nameStrings[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    static String mapHostToRealm(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            String subname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            Config c = Config.getInstance();
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 13247
diff changeset
   681
            if ((result = c.get("domain_realm", name)) != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                for (int i = 1; i < name.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    if ((name.charAt(i) == '.') && (i != name.length() - 1)) { //mapping could be .ibm.com = AUSTIN.IBM.COM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        subname = name.substring(i);
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 13247
diff changeset
   687
                        result = c.get("domain_realm", subname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                        if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                            subname = name.substring(i + 1);      //or mapping could be ibm.com = AUSTIN.IBM.COM
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 13247
diff changeset
   693
                            result = c.get("domain_realm", subname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                            if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   706
    public boolean isRealmDeduced() {
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   707
        return realmDeduced;
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32003
diff changeset
   708
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
}