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.messages;
21
22
23 import java.nio.BufferOverflowException;
24 import java.nio.ByteBuffer;
25
26 import org.apache.directory.api.asn1.EncoderException;
27 import org.apache.directory.api.asn1.ber.tlv.BerValue;
28 import org.apache.directory.api.asn1.ber.tlv.TLV;
29 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
30 import org.apache.directory.api.util.Strings;
31 import org.apache.directory.server.i18n.I18n;
32 import org.apache.directory.shared.kerberos.KerberosConstants;
33 import org.apache.directory.shared.kerberos.KerberosMessageType;
34 import org.apache.directory.shared.kerberos.codec.options.ApOptions;
35 import org.apache.directory.shared.kerberos.components.EncryptedData;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class ApReq extends KerberosMessage
54 {
55
56 private static final Logger LOG = LoggerFactory.getLogger( ApReq.class );
57
58
59 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
60
61
62 private ApOptions apOptions;
63
64
65 private Ticket ticket;
66
67
68 private EncryptedData authenticator;
69
70
71 private int pvnoLength;
72 private int msgTypeLength;
73 private int apOptionsLength;
74 private int ticketLength;
75 private int authenticatorLength;
76 private int apReqLength;
77 private int apReqSeqLength;
78
79
80
81
82
83 public ApReq()
84 {
85 super( KerberosMessageType.AP_REQ );
86 }
87
88
89
90
91
92
93
94 public ApOptions getApOptions()
95 {
96 return apOptions;
97 }
98
99
100
101
102
103
104
105 public Ticket getTicket()
106 {
107 return ticket;
108 }
109
110
111
112
113
114
115
116
117 public boolean getOption( int option )
118 {
119 return apOptions.get( option );
120 }
121
122
123
124
125
126
127
128 public void setOption( ApOptions apOptions )
129 {
130 this.apOptions = apOptions;
131 }
132
133
134
135
136
137
138
139 public void clearOption( int option )
140 {
141 apOptions.clear( option );
142 }
143
144
145
146
147
148
149
150 public EncryptedData getAuthenticator()
151 {
152 return authenticator;
153 }
154
155
156
157
158
159
160
161 public void setAuthenticator( EncryptedData authenticator )
162 {
163 this.authenticator = authenticator;
164 }
165
166
167
168
169
170
171
172 public void setApOptions( ApOptions options )
173 {
174 apOptions = options;
175 }
176
177
178
179
180
181
182
183 public void setTicket( Ticket ticket )
184 {
185 this.ticket = ticket;
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219 public int computeLength()
220 {
221 reset();
222
223
224 pvnoLength = 1 + 1 + BerValue.getNbBytes( getProtocolVersionNumber() );
225
226
227 msgTypeLength = 1 + 1 + BerValue.getNbBytes( getMessageType().getValue() );
228
229
230 apOptionsLength = 1 + 1 + apOptions.getBytes().length;
231
232
233 ticketLength = ticket.computeLength();
234
235
236 authenticatorLength = authenticator.computeLength();
237
238
239 apReqLength =
240 1 + TLV.getNbBytes( pvnoLength ) + pvnoLength +
241 1 + TLV.getNbBytes( msgTypeLength ) + msgTypeLength +
242 1 + TLV.getNbBytes( apOptionsLength ) + apOptionsLength +
243 1 + TLV.getNbBytes( ticketLength ) + ticketLength +
244 1 + TLV.getNbBytes( authenticatorLength ) + authenticatorLength;
245
246 apReqSeqLength = 1 + TLV.getNbBytes( apReqLength ) + apReqLength;
247
248 return 1 + TLV.getNbBytes( apReqSeqLength ) + apReqSeqLength;
249 }
250
251
252
253
254
255
256
257
258
259 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
260 {
261 if ( buffer == null )
262 {
263 buffer = ByteBuffer.allocate( computeLength() );
264 }
265
266 try
267 {
268
269 buffer.put( ( byte ) KerberosConstants.AP_REQ_TAG );
270 buffer.put( TLV.getBytes( apReqSeqLength ) );
271
272
273 buffer.put( UniversalTag.SEQUENCE.getValue() );
274 buffer.put( TLV.getBytes( apReqLength ) );
275
276
277
278 buffer.put( ( byte ) KerberosConstants.AP_REQ_PVNO_TAG );
279 buffer.put( TLV.getBytes( pvnoLength ) );
280
281
282 BerValue.encode( buffer, getProtocolVersionNumber() );
283
284
285
286 buffer.put( ( byte ) KerberosConstants.AP_REQ_MSG_TYPE_TAG );
287 buffer.put( TLV.getBytes( msgTypeLength ) );
288
289
290 BerValue.encode( buffer, getMessageType().getValue() );
291
292
293
294 buffer.put( ( byte ) KerberosConstants.AP_REQ_AP_OPTIONS_TAG );
295 buffer.put( TLV.getBytes( apOptionsLength ) );
296
297
298 BerValue.encode( buffer, apOptions );
299
300
301
302 buffer.put( ( byte ) KerberosConstants.AP_REQ_TICKET_TAG );
303 buffer.put( TLV.getBytes( ticketLength ) );
304
305
306 ticket.encode( buffer );
307
308
309
310 buffer.put( ( byte ) KerberosConstants.AP_REQ_AUTHENTICATOR_TAG );
311 buffer.put( TLV.getBytes( authenticatorLength ) );
312
313
314 authenticator.encode( buffer );
315 }
316 catch ( BufferOverflowException boe )
317 {
318 LOG.error( I18n.err( I18n.ERR_137, 1 + TLV.getNbBytes( apReqLength ) + apReqLength,
319 buffer.capacity() ) );
320 throw new EncoderException( I18n.err( I18n.ERR_138 ), boe );
321 }
322
323 if ( IS_DEBUG )
324 {
325 LOG.debug( "AP-REQ encoding : {}", Strings.dumpBytes( buffer.array() ) );
326 LOG.debug( "AP-REQ initial value : {}", this );
327 }
328
329 return buffer;
330 }
331
332
333
334
335
336 private void reset()
337 {
338 pvnoLength = 0;
339 msgTypeLength = 0;
340 apOptionsLength = 0;
341 ticketLength = 0;
342 authenticatorLength = 0;
343 apReqLength = 0;
344 apReqSeqLength = 0;
345 }
346
347
348
349
350
351 public String toString()
352 {
353 StringBuilder sb = new StringBuilder();
354
355 sb.append( "AP-REQ :\n" );
356 sb.append( " pvno : " ).append( getProtocolVersionNumber() ).append( "\n" );
357 sb.append( " msg-type : " ).append( getMessageType() ).append( "\n" );
358 sb.append( " ap-options : " ).append( apOptions ).append( "\n" );
359 sb.append( " ticket : " ).append( ticket ).append( "\n" );
360 sb.append( " authenticator : " ).append( authenticator ).append( "\n" );
361
362 return sb.toString();
363 }
364 }