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