1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.shared.kerberos.components;
21
22
23 import java.nio.ByteBuffer;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.directory.api.asn1.EncoderException;
28 import org.apache.directory.api.asn1.ber.tlv.BerValue;
29 import org.apache.directory.api.asn1.ber.tlv.TLV;
30 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
31 import org.apache.directory.api.util.Strings;
32 import org.apache.directory.server.i18n.I18n;
33 import org.apache.directory.shared.kerberos.KerberosConstants;
34 import org.apache.directory.shared.kerberos.KerberosMessageType;
35 import org.apache.directory.shared.kerberos.messages.KerberosMessage;
36 import org.apache.directory.shared.kerberos.messages.Ticket;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class KdcRep extends KerberosMessage
58 {
59
60 private List<PaData> paData;
61
62
63 private String crealm;
64
65
66 private byte[] crealmBytes;
67
68
69 private PrincipalName cname;
70
71
72 private Ticket ticket;
73
74
75 private EncryptedData encPart;
76
77
78 protected EncKdcRepPart encKdcRepPart;
79
80
81 private int pvnoLength;
82 private int msgTypeLength;
83 private int paDataLength;
84 private int paDataSeqLength;
85 private int[] paDataLengths;
86 private int cnameLength;
87 private int crealmLength;
88 private int ticketLength;
89 private int encPartLength;
90 private int kdcRepSeqLength;
91 private int kdcRepLength;
92
93
94
95
96
97 public KdcRep( KerberosMessageType msgType )
98 {
99 super( msgType );
100 paData = new ArrayList<>();
101 }
102
103
104
105
106
107 public int getPvno()
108 {
109 return getProtocolVersionNumber();
110 }
111
112
113
114
115
116 public void setPvno( int pvno )
117 {
118 setProtocolVersionNumber( pvno );
119 }
120
121
122
123
124
125 public List<PaData> getPaData()
126 {
127 return paData;
128 }
129
130
131
132
133
134 public void addPaData( PaData paData )
135 {
136 this.paData.add( paData );
137 }
138
139
140
141
142
143
144
145 public String getCRealm()
146 {
147 return crealm;
148 }
149
150
151
152
153
154
155 public void setCRealm( String crealm )
156 {
157 this.crealm = crealm;
158 }
159
160
161
162
163
164
165
166 public PrincipalName getCName()
167 {
168 return cname;
169 }
170
171
172
173
174
175
176 public void setCName( PrincipalName cname )
177 {
178 this.cname = cname;
179 }
180
181
182
183
184
185
186
187 public Ticket getTicket()
188 {
189 return ticket;
190 }
191
192
193
194
195
196
197 public void setTicket( Ticket ticket )
198 {
199 this.ticket = ticket;
200 }
201
202
203
204
205
206
207
208 public EncryptedData getEncPart()
209 {
210 return encPart;
211 }
212
213
214
215
216
217 public void setEncPart( EncryptedData encPart )
218 {
219 this.encPart = encPart;
220 }
221
222
223
224
225
226 public EncKdcRepPart getEncKdcRepPart()
227 {
228 return encKdcRepPart;
229 }
230
231
232
233
234
235 public void setEncKdcRepPart( EncKdcRepPart encKdcRepPart )
236 {
237 this.encKdcRepPart = encKdcRepPart;
238 }
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 public int computeLength()
284 {
285
286 pvnoLength = 1 + 1 + 1;
287 kdcRepSeqLength = 1 + TLV.getNbBytes( pvnoLength ) + pvnoLength;
288
289
290 msgTypeLength = 1 + 1 + 1;
291 kdcRepSeqLength += 1 + TLV.getNbBytes( msgTypeLength ) + msgTypeLength;
292
293
294 if ( !paData.isEmpty() )
295 {
296 paDataLengths = new int[paData.size()];
297 int pos = 0;
298 paDataSeqLength = 0;
299
300 for ( PaData paDataElem : paData )
301 {
302 paDataLengths[pos] = paDataElem.computeLength();
303 paDataSeqLength += paDataLengths[pos];
304 pos++;
305 }
306
307 paDataLength = 1 + TLV.getNbBytes( paDataSeqLength ) + paDataSeqLength;
308 kdcRepSeqLength += 1 + TLV.getNbBytes( paDataLength ) + paDataLength;
309 }
310
311
312 crealmBytes = Strings.getBytesUtf8( crealm );
313 crealmLength = 1 + TLV.getNbBytes( crealmBytes.length ) + crealmBytes.length;
314 kdcRepSeqLength += 1 + TLV.getNbBytes( crealmLength ) + crealmLength;
315
316
317 cnameLength = cname.computeLength();
318 kdcRepSeqLength += 1 + TLV.getNbBytes( cnameLength ) + cnameLength;
319
320
321 ticketLength = ticket.computeLength();
322 kdcRepSeqLength += 1 + TLV.getNbBytes( ticketLength ) + ticketLength;
323
324
325 encPartLength = encPart.computeLength();
326 kdcRepSeqLength += 1 + TLV.getNbBytes( encPartLength ) + encPartLength;
327
328
329 kdcRepLength = 1 + TLV.getNbBytes( kdcRepSeqLength ) + kdcRepSeqLength;
330
331 return kdcRepLength;
332 }
333
334
335
336
337
338
339
340
341
342 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
343 {
344 if ( buffer == null )
345 {
346 throw new EncoderException( I18n.err( I18n.ERR_148 ) );
347 }
348
349
350 buffer.put( UniversalTag.SEQUENCE.getValue() );
351 buffer.put( TLV.getBytes( kdcRepSeqLength ) );
352
353
354
355 buffer.put( ( byte ) KerberosConstants.KDC_REP_PVNO_TAG );
356 buffer.put( TLV.getBytes( pvnoLength ) );
357
358
359 BerValue.encode( buffer, getProtocolVersionNumber() );
360
361
362
363 buffer.put( ( byte ) KerberosConstants.KDC_REP_MSG_TYPE_TAG );
364 buffer.put( TLV.getBytes( msgTypeLength ) );
365
366
367 BerValue.encode( buffer, getMessageType().getValue() );
368
369
370 if ( !paData.isEmpty() )
371 {
372
373 buffer.put( ( byte ) KerberosConstants.KDC_REP_PA_DATA_TAG );
374 buffer.put( TLV.getBytes( paDataLength ) );
375
376
377 buffer.put( UniversalTag.SEQUENCE.getValue() );
378 buffer.put( TLV.getBytes( paDataSeqLength ) );
379
380
381 for ( PaData paDataElem : paData )
382 {
383 paDataElem.encode( buffer );
384 }
385 }
386
387
388
389 buffer.put( ( byte ) KerberosConstants.KDC_REP_CREALM_TAG );
390 buffer.put( TLV.getBytes( crealmLength ) );
391
392
393 buffer.put( UniversalTag.GENERAL_STRING.getValue() );
394 buffer.put( TLV.getBytes( crealmBytes.length ) );
395 buffer.put( crealmBytes );
396
397
398
399 buffer.put( ( byte ) KerberosConstants.KDC_REP_CNAME_TAG );
400 buffer.put( TLV.getBytes( cnameLength ) );
401
402
403 cname.encode( buffer );
404
405
406
407 buffer.put( ( byte ) KerberosConstants.KDC_REP_TICKET_TAG );
408 buffer.put( TLV.getBytes( ticketLength ) );
409
410
411 ticket.encode( buffer );
412
413
414
415 buffer.put( ( byte ) KerberosConstants.KDC_REP_ENC_PART_TAG );
416 buffer.put( TLV.getBytes( encPartLength ) );
417
418
419 encPart.encode( buffer );
420
421 return buffer;
422 }
423
424
425
426
427
428 public String toString( String tabs )
429 {
430 StringBuilder sb = new StringBuilder();
431
432 sb.append( tabs ).append(
433 "\n>-------------------------------------------------------------------------------\n" );
434 sb.append( tabs ).append( "KdcRep : " );
435
436 if ( getMessageType() == KerberosMessageType.AS_REP )
437 {
438 sb.append( "AS-REP" ).append( '\n' );
439 }
440 else if ( getMessageType() == KerberosMessageType.TGS_REP )
441 {
442 sb.append( "TGS-REP" ).append( '\n' );
443 }
444 else
445 {
446 sb.append( "Unknown" ).append( '\n' );
447 }
448
449 sb.append( tabs ).append( "pvno : " ).append( getProtocolVersionNumber() ).append( '\n' );
450
451 sb.append( tabs ).append( "msg-type : " ).append( getMessageType() ).append( '\n' );
452
453 for ( PaData paDataElem : paData )
454 {
455 sb.append( tabs ).append( "padata : " ).append( paDataElem.toString( tabs + " " ) ).append( '\n' );
456 }
457
458 sb.append( tabs ).append( "crealm : " ).append( crealm ).append( '\n' );
459 sb.append( tabs ).append( "cname : " ).append( cname ).append( '\n' );
460 sb.append( ticket.toString( tabs ) );
461 sb.append( encPart.toString( tabs ) );
462 sb.append( tabs ).append(
463 "\n-------------------------------------------------------------------------------<\n" );
464
465 return sb.toString();
466 }
467
468
469
470
471
472 public String toString()
473 {
474 return toString( "" );
475 }
476 }