1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 package org.apache.directory.shared.kerberos.components;
86
87
88 import java.nio.BufferOverflowException;
89 import java.nio.ByteBuffer;
90 import java.text.ParseException;
91 import java.util.ArrayList;
92 import java.util.List;
93
94 import javax.security.auth.kerberos.KerberosPrincipal;
95
96 import org.apache.directory.api.asn1.AbstractAsn1Object;
97 import org.apache.directory.api.asn1.EncoderException;
98 import org.apache.directory.api.asn1.ber.tlv.BerValue;
99 import org.apache.directory.api.asn1.ber.tlv.TLV;
100 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
101 import org.apache.directory.api.util.StringConstants;
102 import org.apache.directory.api.util.Strings;
103 import org.apache.directory.server.i18n.I18n;
104 import org.apache.directory.shared.kerberos.KerberosUtils;
105 import org.apache.directory.shared.kerberos.codec.types.PrincipalNameType;
106 import org.slf4j.Logger;
107 import org.slf4j.LoggerFactory;
108
109
110
111
112
113
114
115
116
117
118
119
120 public class PrincipalName extends AbstractAsn1Object
121 {
122
123 private static final Logger LOG = LoggerFactory.getLogger( PrincipalName.class );
124
125
126 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
127
128
129 private PrincipalNameType nameType;
130
131
132 private List<String> nameString = new ArrayList<String>();
133
134
135 private String realm;
136
137
138 private List<byte[]> nameBytes;
139
140
141 private int principalNameSeqLength;
142 private int principalTypeTagLength;
143 private int principalTypeLength;
144 private int principalStringsTagLength;
145 private int principalStringsSeqLength;
146
147
148
149
150
151 public PrincipalName()
152 {
153 }
154
155
156
157
158
159
160
161
162
163
164 public PrincipalName( KerberosPrincipal principal )
165 {
166 try
167 {
168 nameString = KerberosUtils.getNames( principal );
169 }
170 catch ( ParseException pe )
171 {
172 nameString = KerberosUtils.EMPTY_PRINCIPAL_NAME;
173 }
174
175 this.nameType = PrincipalNameType.getTypeByValue( principal.getNameType() );
176 }
177
178
179
180
181
182
183
184
185
186 public PrincipalName( String nameString, PrincipalNameType nameType ) throws ParseException
187 {
188 this.nameString = KerberosUtils.getNames( nameString );
189 this.nameType = nameType;
190 }
191
192
193
194
195
196
197
198
199 public PrincipalName( String nameString, int nameType )
200 {
201 try
202 {
203 this.nameString = KerberosUtils.getNames( nameString );
204 }
205 catch ( ParseException pe )
206 {
207 throw new IllegalArgumentException( pe );
208 }
209
210 this.nameType = PrincipalNameType.getTypeByValue( nameType );
211 }
212
213
214
215
216
217
218
219 public PrincipalNameType getNameType()
220 {
221 return nameType;
222 }
223
224
225
226
227
228
229 public void setNameType( PrincipalNameType nameType )
230 {
231 this.nameType = nameType;
232 }
233
234
235
236
237
238
239 public void setNameType( int nameType )
240 {
241 this.nameType = PrincipalNameType.getTypeByValue( nameType );
242 }
243
244
245
246
247
248
249
250 public List<String> getNames()
251 {
252 return nameString;
253 }
254
255
256
257
258
259 public String getNameString()
260 {
261 if ( ( nameString == null ) || ( nameString.size() == 0 ) )
262 {
263 return "";
264 }
265 else
266 {
267 StringBuilder sb = new StringBuilder();
268 boolean isFirst = true;
269
270 for ( String name : nameString )
271 {
272 if ( isFirst )
273 {
274 isFirst = false;
275 }
276 else
277 {
278 sb.append( '/' );
279 }
280
281 sb.append( name );
282 }
283
284 return sb.toString();
285 }
286 }
287
288
289
290
291
292
293 public void addName( String name )
294 {
295 if ( nameString == null )
296 {
297 nameString = new ArrayList<String>();
298 }
299
300 nameString.add( name );
301 }
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328 public int computeLength()
329 {
330
331 principalTypeLength = BerValue.getNbBytes( nameType.getValue() );
332 principalTypeTagLength = 1 + 1 + principalTypeLength;
333
334 principalNameSeqLength = 1 + TLV.getNbBytes( principalTypeTagLength ) + principalTypeTagLength;
335
336
337 if ( ( nameString == null ) || ( nameString.size() == 0 ) )
338 {
339 principalStringsSeqLength = 0;
340 }
341 else
342 {
343 principalStringsSeqLength = 0;
344 nameBytes = new ArrayList<byte[]>( nameString.size() );
345
346 for ( String name : nameString )
347 {
348 if ( name != null )
349 {
350 byte[] bytes = Strings.getBytesUtf8( name );
351 nameBytes.add( bytes );
352 principalStringsSeqLength += 1 + TLV.getNbBytes( bytes.length ) + bytes.length;
353 }
354 else
355 {
356 nameBytes.add( StringConstants.EMPTY_BYTES );
357 principalStringsSeqLength += 1 + 1;
358 }
359 }
360 }
361
362 principalStringsTagLength = 1 + TLV.getNbBytes( principalStringsSeqLength ) + principalStringsSeqLength;
363 principalNameSeqLength += 1 + TLV.getNbBytes( principalStringsTagLength ) + principalStringsTagLength;
364
365
366 return 1 + TLV.getNbBytes( principalNameSeqLength ) + principalNameSeqLength;
367 }
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
390 {
391 if ( buffer == null )
392 {
393 throw new EncoderException( I18n.err( I18n.ERR_148 ) );
394 }
395
396 try
397 {
398
399 buffer.put( UniversalTag.SEQUENCE.getValue() );
400 buffer.put( TLV.getBytes( principalNameSeqLength ) );
401
402
403 buffer.put( ( byte ) 0xA0 );
404 buffer.put( TLV.getBytes( principalTypeTagLength ) );
405 BerValue.encode( buffer, nameType.getValue() );
406
407
408 buffer.put( ( byte ) 0xA1 );
409 buffer.put( TLV.getBytes( principalStringsTagLength ) );
410
411
412 buffer.put( UniversalTag.SEQUENCE.getValue() );
413
414 if ( ( nameString == null ) || ( nameString.size() == 0 ) )
415 {
416 buffer.put( ( byte ) 0x00 );
417 }
418 else
419 {
420 buffer.put( TLV.getBytes( principalStringsSeqLength ) );
421
422
423 for ( byte[] name : nameBytes )
424 {
425 buffer.put( UniversalTag.GENERAL_STRING.getValue() );
426
427 if ( ( name == null ) || ( name.length == 0 ) )
428 {
429 buffer.put( ( byte ) 0x00 );
430 }
431 else
432 {
433 buffer.put( TLV.getBytes( name.length ) );
434 buffer.put( name );
435 }
436 }
437 }
438 }
439 catch ( BufferOverflowException boe )
440 {
441 LOG.error( I18n.err( I18n.ERR_146, 1 + TLV.getNbBytes( principalNameSeqLength )
442 + principalNameSeqLength, buffer.capacity() ) );
443 throw new EncoderException( I18n.err( I18n.ERR_138 ) );
444 }
445
446 if ( IS_DEBUG )
447 {
448 LOG.debug( "PrinipalName encoding : {}", Strings.dumpBytes( buffer.array() ) );
449 LOG.debug( "PrinipalName initial value : {}", toString() );
450 }
451
452 return buffer;
453 }
454
455
456
457
458
459 public String toString()
460 {
461 StringBuilder sb = new StringBuilder();
462
463 sb.append( "{ " );
464
465 sb.append( "name-type: " ).append( nameType.name() );
466
467 if ( ( nameString != null ) && ( nameString.size() != 0 ) )
468 {
469 sb.append( ", name-string : <" );
470 boolean isFirst = true;
471
472 for ( String name : nameString )
473 {
474 if ( isFirst )
475 {
476 isFirst = false;
477 }
478 else
479 {
480 sb.append( ", " );
481 }
482
483 sb.append( '\'' ).append( name ).append( '\'' );
484 }
485
486 sb.append( "> }" );
487 }
488 else
489 {
490 sb.append( " no name-string }" );
491 }
492
493 return sb.toString();
494 }
495
496
497 @Override
498 public int hashCode()
499 {
500 final int prime = 31;
501 int result = 1;
502 result = prime * result + ( ( nameString == null ) ? 0 : nameString.hashCode() );
503 result = prime * result + ( ( nameType == null ) ? 0 : nameType.hashCode() );
504 return result;
505 }
506
507
508 @Override
509 public boolean equals( Object obj )
510 {
511 if ( this == obj )
512 {
513 return true;
514 }
515
516 if ( obj == null )
517 {
518 return false;
519 }
520
521 PrincipalName other = ( PrincipalName ) obj;
522
523 if ( nameString == null )
524 {
525 if ( other.nameString != null )
526 {
527 return false;
528 }
529 }
530 else if ( !nameString.equals( other.nameString ) )
531 {
532 return false;
533 }
534
535 if ( nameType != other.nameType )
536 {
537 return false;
538 }
539
540 return true;
541 }
542
543 }