View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   * 
19   */
20  package org.apache.directory.shared.kerberos.messages;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  import org.apache.directory.api.asn1.EncoderException;
26  import org.apache.directory.api.asn1.ber.tlv.TLV;
27  import org.apache.directory.shared.kerberos.KerberosConstants;
28  import org.apache.directory.shared.kerberos.KerberosMessageType;
29  import org.apache.directory.shared.kerberos.components.KdcReq;
30  
31  
32  /**
33   * TGS-REQ message. It's just a KDC-REQ message with a message type set to 12.
34   *  It will store the object described by the ASN.1 grammar :
35   * <pre>
36   * TGS-REQ         ::= [APPLICATION 12] &lt;KDC-REQ&gt;
37   * </pre>
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class TgsReq extends KdcReq
41  {
42      // Storage for computed lengths
43      private int kdcReqLength;
44      private int tgsReqLength;
45  
46  
47      /**
48       * Creates a new instance of TGS-REQ.
49       */
50      public TgsReq()
51      {
52          super( KerberosMessageType.TGS_REQ );
53      }
54  
55  
56      /**
57       * Compute the TGS-REQ length
58       * <pre>
59       * TGS-REQ :
60       * 
61       * 0x6A L1 TGS-REQ message
62       *  |
63       *  +--&gt;  0x30 L2 KDC-REQ sequence
64       * </pre>
65       */
66      @Override
67      public int computeLength()
68      {
69          kdcReqLength = 0;
70          tgsReqLength = 0;
71  
72          kdcReqLength = super.computeLength();
73          tgsReqLength = 1 + TLV.getNbBytes( kdcReqLength ) + kdcReqLength;
74  
75          return tgsReqLength;
76      }
77  
78  
79      /**
80       * Encode the TGS-REQ component
81       * 
82       * @param buffer The buffer containing the encoded result
83       * @return The encoded component
84       * @throws EncoderException If the encoding failed
85       */
86      @Override
87      public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
88      {
89          if ( buffer == null )
90          {
91              buffer = ByteBuffer.allocate( computeLength() );
92          }
93  
94          // The TGS-REQ SEQ Tag
95          buffer.put( ( byte ) KerberosConstants.TGS_REQ_TAG );
96          buffer.put( TLV.getBytes( kdcReqLength ) );
97  
98          // The KDC-REQ --------------------------------------------------------
99          super.encode( buffer );
100 
101         return buffer;
102     }
103 }