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