Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/krb5/PrincipalName.java
38830 views
/*1* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*27* (C) Copyright IBM Corp. 1999 All Rights Reserved.28* Copyright 1997 The Open Group Research Institute. All rights reserved.29*/3031package sun.security.krb5;3233import sun.security.krb5.internal.*;34import sun.security.util.*;35import java.net.*;36import java.util.Vector;37import java.util.Locale;38import java.io.IOException;39import java.math.BigInteger;40import java.util.Arrays;41import sun.security.krb5.internal.ccache.CCacheOutputStream;42import sun.security.krb5.internal.util.KerberosString;434445/**46* Implements the ASN.1 PrincipalName type and its realm in a single class.47* <pre>{@code48* Realm ::= KerberosString49*50* PrincipalName ::= SEQUENCE {51* name-type [0] Int32,52* name-string [1] SEQUENCE OF KerberosString53* }54* }</pre>55* This class is immutable.56* @see Realm57*/58public class PrincipalName implements Cloneable {5960//name types6162/**63* Name type not known64*/65public static final int KRB_NT_UNKNOWN = 0;6667/**68* Just the name of the principal as in DCE, or for users69*/70public static final int KRB_NT_PRINCIPAL = 1;7172/**73* Service and other unique instance (krbtgt)74*/75public static final int KRB_NT_SRV_INST = 2;7677/**78* Service with host name as instance (telnet, rcommands)79*/80public static final int KRB_NT_SRV_HST = 3;8182/**83* Service with host as remaining components84*/85public static final int KRB_NT_SRV_XHST = 4;8687/**88* Unique ID89*/90public static final int KRB_NT_UID = 5;9192/**93* Enterprise name (alias)94*/95public static final int KRB_NT_ENTERPRISE = 10;9697/**98* TGS Name99*/100public static final String TGS_DEFAULT_SRV_NAME = "krbtgt";101public static final int TGS_DEFAULT_NT = KRB_NT_SRV_INST;102103public static final char NAME_COMPONENT_SEPARATOR = '/';104public static final char NAME_REALM_SEPARATOR = '@';105public static final char REALM_COMPONENT_SEPARATOR = '.';106107public static final String NAME_COMPONENT_SEPARATOR_STR = "/";108public static final String NAME_REALM_SEPARATOR_STR = "@";109public static final String REALM_COMPONENT_SEPARATOR_STR = ".";110111// Instance fields.112113/**114* The name type, from PrincipalName's name-type field.115*/116private final int nameType;117118/**119* The name strings, from PrincipalName's name-strings field. This field120* must be neither null nor empty. Each entry of it must also be neither121* null nor empty. Make sure to clone the field when it's passed in or out.122*/123private final String[] nameStrings;124125/**126* The realm this principal belongs to.127*/128private final Realm nameRealm; // not null129130131/**132* When constructing a PrincipalName, whether the realm is included in133* the input, or deduced from default realm or domain-realm mapping.134*/135private final boolean realmDeduced;136137// cached default salt, not used in clone138private transient String salt = null;139140// There are 3 basic constructors. All other constructors must call them.141// All basic constructors must call validateNameStrings.142// 1. From name components143// 2. From name144// 3. From DER encoding145146/**147* Creates a PrincipalName.148*/149public PrincipalName(int nameType, String[] nameStrings, Realm nameRealm) {150if (nameRealm == null) {151throw new IllegalArgumentException("Null realm not allowed");152}153validateNameStrings(nameStrings);154this.nameType = nameType;155this.nameStrings = nameStrings.clone();156this.nameRealm = nameRealm;157this.realmDeduced = false;158}159160// This method is called by Windows NativeCred.c161public PrincipalName(String[] nameParts, String realm) throws RealmException {162this(KRB_NT_UNKNOWN, nameParts, new Realm(realm));163}164165// Validate a nameStrings argument166private static void validateNameStrings(String[] ns) {167if (ns == null) {168throw new IllegalArgumentException("Null nameStrings not allowed");169}170if (ns.length == 0) {171throw new IllegalArgumentException("Empty nameStrings not allowed");172}173for (String s: ns) {174if (s == null) {175throw new IllegalArgumentException("Null nameString not allowed");176}177if (s.isEmpty()) {178throw new IllegalArgumentException("Empty nameString not allowed");179}180}181}182183public Object clone() {184try {185PrincipalName pName = (PrincipalName) super.clone();186UNSAFE.putObject(this, NAME_STRINGS_OFFSET, nameStrings.clone());187return pName;188} catch (CloneNotSupportedException ex) {189throw new AssertionError("Should never happen");190}191}192193private static final long NAME_STRINGS_OFFSET;194private static final sun.misc.Unsafe UNSAFE;195static {196try {197sun.misc.Unsafe unsafe = sun.misc.Unsafe.getUnsafe();198NAME_STRINGS_OFFSET = unsafe.objectFieldOffset(199PrincipalName.class.getDeclaredField("nameStrings"));200UNSAFE = unsafe;201} catch (ReflectiveOperationException e) {202throw new Error(e);203}204}205206@Override207public boolean equals(Object o) {208if (this == o) {209return true;210}211if (o instanceof PrincipalName) {212PrincipalName other = (PrincipalName)o;213return nameRealm.equals(other.nameRealm) &&214Arrays.equals(nameStrings, other.nameStrings);215}216return false;217}218219/**220* Returns the ASN.1 encoding of the221* <pre>{@code222* PrincipalName ::= SEQUENCE {223* name-type [0] Int32,224* name-string [1] SEQUENCE OF KerberosString225* }226*227* KerberosString ::= GeneralString (IA5String)228* }</pre>229*230* <p>231* This definition reflects the Network Working Group RFC 4120232* specification available at233* <a href="http://www.ietf.org/rfc/rfc4120.txt">234* http://www.ietf.org/rfc/rfc4120.txt</a>.235*236* @param encoding DER-encoded PrincipalName (without Realm)237* @param realm the realm for this name238* @exception Asn1Exception if an error occurs while decoding239* an ASN1 encoded data.240* @exception Asn1Exception if there is an ASN1 encoding error241* @exception IOException if an I/O error occurs242* @exception IllegalArgumentException if encoding is null243* reading encoded data.244*/245public PrincipalName(DerValue encoding, Realm realm)246throws Asn1Exception, IOException {247if (realm == null) {248throw new IllegalArgumentException("Null realm not allowed");249}250realmDeduced = false;251nameRealm = realm;252DerValue der;253if (encoding == null) {254throw new IllegalArgumentException("Null encoding not allowed");255}256if (encoding.getTag() != DerValue.tag_Sequence) {257throw new Asn1Exception(Krb5.ASN1_BAD_ID);258}259der = encoding.getData().getDerValue();260if ((der.getTag() & 0x1F) == 0x00) {261BigInteger bint = der.getData().getBigInteger();262nameType = bint.intValue();263} else {264throw new Asn1Exception(Krb5.ASN1_BAD_ID);265}266der = encoding.getData().getDerValue();267if ((der.getTag() & 0x01F) == 0x01) {268DerValue subDer = der.getData().getDerValue();269if (subDer.getTag() != DerValue.tag_SequenceOf) {270throw new Asn1Exception(Krb5.ASN1_BAD_ID);271}272Vector<String> v = new Vector<>();273DerValue subSubDer;274while(subDer.getData().available() > 0) {275subSubDer = subDer.getData().getDerValue();276String namePart = new KerberosString(subSubDer).toString();277v.addElement(namePart);278}279nameStrings = new String[v.size()];280v.copyInto(nameStrings);281validateNameStrings(nameStrings);282} else {283throw new Asn1Exception(Krb5.ASN1_BAD_ID);284}285}286287/**288* Parse (unmarshal) a <code>PrincipalName</code> from a DER289* input stream. This form290* parsing might be used when expanding a value which is part of291* a constructed sequence and uses explicitly tagged type.292*293* @exception Asn1Exception on error.294* @param data the Der input stream value, which contains one or295* more marshaled value.296* @param explicitTag tag number.297* @param optional indicate if this data field is optional298* @param realm the realm for the name299* @return an instance of <code>PrincipalName</code>, or null if the300* field is optional and missing.301*/302public static PrincipalName parse(DerInputStream data,303byte explicitTag, boolean304optional,305Realm realm)306throws Asn1Exception, IOException, RealmException {307308if ((optional) && (((byte)data.peekByte() & (byte)0x1F) !=309explicitTag))310return null;311DerValue der = data.getDerValue();312if (explicitTag != (der.getTag() & (byte)0x1F)) {313throw new Asn1Exception(Krb5.ASN1_BAD_ID);314} else {315DerValue subDer = der.getData().getDerValue();316if (realm == null) {317realm = Realm.getDefault();318}319return new PrincipalName(subDer, realm);320}321}322323324// XXX Error checkin consistent with MIT krb5_parse_name325// Code repetition, realm parsed again by class Realm326private static String[] parseName(String name) {327328Vector<String> tempStrings = new Vector<>();329String temp = name;330int i = 0;331int componentStart = 0;332String component;333334while (i < temp.length()) {335if (temp.charAt(i) == NAME_COMPONENT_SEPARATOR) {336/*337* If this separator is escaped then don't treat it338* as a separator339*/340if (i > 0 && temp.charAt(i - 1) == '\\') {341temp = temp.substring(0, i - 1) +342temp.substring(i, temp.length());343continue;344}345else {346if (componentStart <= i) {347component = temp.substring(componentStart, i);348tempStrings.addElement(component);349}350componentStart = i + 1;351}352} else {353if (temp.charAt(i) == NAME_REALM_SEPARATOR) {354/*355* If this separator is escaped then don't treat it356* as a separator357*/358if (i > 0 && temp.charAt(i - 1) == '\\') {359temp = temp.substring(0, i - 1) +360temp.substring(i, temp.length());361continue;362} else {363if (componentStart < i) {364component = temp.substring(componentStart, i);365tempStrings.addElement(component);366}367componentStart = i + 1;368break;369}370}371}372i++;373}374375if (i == temp.length()) {376component = temp.substring(componentStart, i);377tempStrings.addElement(component);378}379380String[] result = new String[tempStrings.size()];381tempStrings.copyInto(result);382return result;383}384385/**386* Constructs a PrincipalName from a string.387* @param name the name388* @param type the type389* @param realm the realm, null if not known. Note that when realm is not390* null, it will be always used even if there is a realm part in name. When391* realm is null, will read realm part from name, or try to map a realm392* (for KRB_NT_SRV_HST), or use the default realm, or fail393* @throws RealmException394*/395public PrincipalName(String name, int type, String realm)396throws RealmException {397if (name == null) {398throw new IllegalArgumentException("Null name not allowed");399}400String[] nameParts = parseName(name);401validateNameStrings(nameParts);402if (realm == null) {403realm = Realm.parseRealmAtSeparator(name);404}405406// No realm info from parameter and string, must deduce later407realmDeduced = realm == null;408409switch (type) {410case KRB_NT_SRV_HST:411if (nameParts.length >= 2) {412String hostName = nameParts[1];413try {414// RFC4120 does not recommend canonicalizing a hostname.415// However, for compatibility reason, we will try416// canonicalize it and see if the output looks better.417418String canonicalized = (InetAddress.getByName(hostName)).419getCanonicalHostName();420421// Looks if canonicalized is a longer format of hostName,422// we accept cases like423// bunny -> bunny.rabbit.hole424if (canonicalized.toLowerCase(Locale.ENGLISH).startsWith(425hostName.toLowerCase(Locale.ENGLISH)+".")) {426hostName = canonicalized;427}428} catch (UnknownHostException | SecurityException e) {429// not canonicalized or no permission to do so, use old430}431if (hostName.endsWith(".")) {432hostName = hostName.substring(0, hostName.length() - 1);433}434nameParts[1] = hostName.toLowerCase(Locale.ENGLISH);435}436nameStrings = nameParts;437nameType = type;438439if (realm != null) {440nameRealm = new Realm(realm);441} else {442// We will try to get realm name from the mapping in443// the configuration. If it is not specified444// we will use the default realm. This nametype does445// not allow a realm to be specified. The name string must of446// the form service@host and this is internally changed into447// service/host by Kerberos448String mapRealm = mapHostToRealm(nameParts[1]);449if (mapRealm != null) {450nameRealm = new Realm(mapRealm);451} else {452nameRealm = Realm.getDefault();453}454}455break;456case KRB_NT_UNKNOWN:457case KRB_NT_PRINCIPAL:458case KRB_NT_SRV_INST:459case KRB_NT_SRV_XHST:460case KRB_NT_UID:461case KRB_NT_ENTERPRISE:462nameStrings = nameParts;463nameType = type;464if (realm != null) {465nameRealm = new Realm(realm);466} else {467nameRealm = Realm.getDefault();468}469break;470default:471throw new IllegalArgumentException("Illegal name type");472}473}474475public PrincipalName(String name, int type) throws RealmException {476this(name, type, (String)null);477}478479public PrincipalName(String name) throws RealmException {480this(name, KRB_NT_UNKNOWN);481}482483public PrincipalName(String name, String realm) throws RealmException {484this(name, KRB_NT_UNKNOWN, realm);485}486487public static PrincipalName tgsService(String r1, String r2)488throws KrbException {489return new PrincipalName(PrincipalName.KRB_NT_SRV_INST,490new String[] {PrincipalName.TGS_DEFAULT_SRV_NAME, r1},491new Realm(r2));492}493494public String getRealmAsString() {495return getRealmString();496}497498public String getPrincipalNameAsString() {499StringBuffer temp = new StringBuffer(nameStrings[0]);500for (int i = 1; i < nameStrings.length; i++)501temp.append(nameStrings[i]);502return temp.toString();503}504505public int hashCode() {506return toString().hashCode();507}508509public String getName() {510return toString();511}512513public int getNameType() {514return nameType;515}516517public String[] getNameStrings() {518return nameStrings.clone();519}520521public byte[][] toByteArray() {522byte[][] result = new byte[nameStrings.length][];523for (int i = 0; i < nameStrings.length; i++) {524result[i] = new byte[nameStrings[i].length()];525result[i] = nameStrings[i].getBytes();526}527return result;528}529530public String getRealmString() {531return nameRealm.toString();532}533534public Realm getRealm() {535return nameRealm;536}537538public String getSalt() {539if (salt == null) {540StringBuffer salt = new StringBuffer();541salt.append(nameRealm.toString());542for (int i = 0; i < nameStrings.length; i++) {543salt.append(nameStrings[i]);544}545return salt.toString();546}547return salt;548}549550public String toString() {551StringBuffer str = new StringBuffer();552for (int i = 0; i < nameStrings.length; i++) {553if (i > 0)554str.append("/");555String n = nameStrings[i];556n = n.replace("@", "\\@");557str.append(n);558}559str.append("@");560str.append(nameRealm.toString());561return str.toString();562}563564public String getNameString() {565StringBuffer str = new StringBuffer();566for (int i = 0; i < nameStrings.length; i++) {567if (i > 0)568str.append("/");569str.append(nameStrings[i]);570}571return str.toString();572}573574/**575* Encodes a <code>PrincipalName</code> object. Note that only the type and576* names are encoded. To encode the realm, call getRealm().asn1Encode().577* @return the byte array of the encoded PrncipalName object.578* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.579* @exception IOException if an I/O error occurs while reading encoded data.580*581*/582public byte[] asn1Encode() throws Asn1Exception, IOException {583DerOutputStream bytes = new DerOutputStream();584DerOutputStream temp = new DerOutputStream();585BigInteger bint = BigInteger.valueOf(this.nameType);586temp.putInteger(bint);587bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);588temp = new DerOutputStream();589DerValue der[] = new DerValue[nameStrings.length];590for (int i = 0; i < nameStrings.length; i++) {591der[i] = new KerberosString(nameStrings[i]).toDerValue();592}593temp.putSequence(der);594bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);595temp = new DerOutputStream();596temp.write(DerValue.tag_Sequence, bytes);597return temp.toByteArray();598}599600601/**602* Checks if two <code>PrincipalName</code> objects have identical values in their corresponding data fields.603*604* @param pname the other <code>PrincipalName</code> object.605* @return true if two have identical values, otherwise, return false.606*/607// It is used in <code>sun.security.krb5.internal.ccache</code> package.608public boolean match(PrincipalName pname) {609boolean matched = true;610//name type is just a hint, no two names can be the same ignoring name type.611// if (this.nameType != pname.nameType) {612// matched = false;613// }614if ((this.nameRealm != null) && (pname.nameRealm != null)) {615if (!(this.nameRealm.toString().equalsIgnoreCase(pname.nameRealm.toString()))) {616matched = false;617}618}619if (this.nameStrings.length != pname.nameStrings.length) {620matched = false;621} else {622for (int i = 0; i < this.nameStrings.length; i++) {623if (!(this.nameStrings[i].equalsIgnoreCase(pname.nameStrings[i]))) {624matched = false;625}626}627}628return matched;629}630631/**632* Writes data field values of <code>PrincipalName</code> in FCC format to an output stream.633*634* @param cos a <code>CCacheOutputStream</code> for writing data.635* @exception IOException if an I/O exception occurs.636* @see sun.security.krb5.internal.ccache.CCacheOutputStream637*/638public void writePrincipal(CCacheOutputStream cos) throws IOException {639cos.write32(nameType);640cos.write32(nameStrings.length);641byte[] realmBytes = null;642realmBytes = nameRealm.toString().getBytes();643cos.write32(realmBytes.length);644cos.write(realmBytes, 0, realmBytes.length);645byte[] bytes = null;646for (int i = 0; i < nameStrings.length; i++) {647bytes = nameStrings[i].getBytes();648cos.write32(bytes.length);649cos.write(bytes, 0, bytes.length);650}651}652653/**654* Returns the instance component of a name.655* In a multi-component name such as a KRB_NT_SRV_INST656* name, the second component is returned.657* Null is returned if there are not two or more658* components in the name.659*660* @return instance component of a multi-component name.661*/662public String getInstanceComponent()663{664if (nameStrings != null && nameStrings.length >= 2)665{666return new String(nameStrings[1]);667}668669return null;670}671672static String mapHostToRealm(String name) {673String result = null;674try {675String subname = null;676Config c = Config.getInstance();677if ((result = c.get("domain_realm", name)) != null)678return result;679else {680for (int i = 1; i < name.length(); i++) {681if ((name.charAt(i) == '.') && (i != name.length() - 1)) { //mapping could be .ibm.com = AUSTIN.IBM.COM682subname = name.substring(i);683result = c.get("domain_realm", subname);684if (result != null) {685break;686}687else {688subname = name.substring(i + 1); //or mapping could be ibm.com = AUSTIN.IBM.COM689result = c.get("domain_realm", subname);690if (result != null) {691break;692}693}694}695}696}697} catch (KrbException e) {698}699return result;700}701702public boolean isRealmDeduced() {703return realmDeduced;704}705}706707708