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.components;
21  
22  
23  import java.nio.BufferOverflowException;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.api.asn1.Asn1Object;
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.KerberosTime;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  
39  /**
40   * The PaEncTsEnc structure is used to store a PA-ENC-TS-ENC associated to a type.
41   * 
42   * The ASN.1 grammar is :
43   * <pre>
44   * PA-ENC-TS-ENC           ::= SEQUENCE {
45   *         patimestamp     [0] KerberosTime -- client's time --,
46   *         pausec          [1] Microseconds OPTIONAL
47   * }
48   * </pre>
49   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
50   */
51  public class PaEncTsEnc implements Asn1Object
52  {
53      /** The logger */
54      private static final Logger log = LoggerFactory.getLogger( PaEncTsEnc.class );
55  
56      /** Speedup for logs */
57      private static final boolean IS_DEBUG = log.isDebugEnabled();
58  
59      /** The patimestamp */
60      private KerberosTime patimestamp;
61  
62      /** The pausec */
63      private Integer pausec;
64  
65      // Storage for computed lengths
66      private int paTimestampLength;
67      private int paUsecLength;
68      private int paEncTsEncLength;
69  
70  
71      /**
72       * Creates a new instance of PaEncTsEnc.
73       */
74      public PaEncTsEnc()
75      {
76      }
77  
78  
79      /**
80       * Creates a new instance of PaEncTsEnc.
81       */
82      public PaEncTsEnc( KerberosTime paTimestamp, int pausec )
83      {
84          this.patimestamp = paTimestamp;
85          this.pausec = pausec;
86      }
87  
88  
89      /**
90       * Returns the patimestamp value.
91       *
92       * @return The patimestamp value.
93       */
94      public KerberosTime getPaTimestamp()
95      {
96          return patimestamp;
97      }
98  
99  
100     /**
101      * Set the patimestamp.
102      *
103      * @param patimestamp The patimestamp value
104      */
105     public void setPaTimestamp( KerberosTime patimestamp )
106     {
107         this.patimestamp = patimestamp;
108     }
109 
110 
111     /**
112      * @return the pausec
113      */
114     public int getPausec()
115     {
116         if ( pausec == null )
117         {
118             return -1;
119         }
120 
121         return pausec;
122     }
123 
124 
125     /**
126      * @param pausec the pausec to set
127      */
128     public void setPausec( int pausec )
129     {
130         this.pausec = pausec;
131     }
132 
133 
134     /**
135      * Compute the PA-ENC-TS-ENC length
136      * <pre>
137      * PA-ENC-TS-ENC :
138      * 
139      * 0x30 L1 PA-ENC-TS-ENC sequence
140      *  |
141      *  +--&gt; 0xA0 0x11 patimestamp tag
142      *  |     |
143      *  |     +--&gt; 0x18 0x0F patimestamp value (KerberosTime)
144      *  |
145      *  +--&gt; 0xA1 L2 pausec tag
146      *        |
147      *        +--&gt; 0x02 L2-1 pausec (INTEGER)
148      *        
149      *  </pre>
150      */
151     public int computeLength()
152     {
153         // The paTimestamp
154         paTimestampLength = 0x11;
155 
156         paEncTsEncLength = 1 + TLV.getNbBytes( paTimestampLength ) + paTimestampLength;
157 
158         // The pausec, if any
159         if ( pausec != null )
160         {
161             int pausecLength = BerValue.getNbBytes( pausec );
162             paUsecLength = 1 + TLV.getNbBytes( pausecLength ) + pausecLength;
163             paEncTsEncLength += 1 + TLV.getNbBytes( paUsecLength ) + paUsecLength;
164         }
165 
166         // Compute the whole sequence length
167         return 1 + TLV.getNbBytes( paEncTsEncLength ) + paEncTsEncLength;
168     }
169 
170 
171     /**
172      * Encode the PA-ENC-TS-ENC message to a PDU. 
173      * 
174      * <pre>
175      * PA-ENC-TS-ENC :
176      * 
177      * 0x30 LL
178      *   0xA0 0x11 
179      *     0x18 0x0F patimestamp
180      *  [0xA1 LL 
181      *     0x02 LL pausec]
182      * </pre>
183      * @param buffer The buffer where to put the PDU. It should have been allocated
184      * before, with the right size.
185      * @return The constructed PDU.
186      */
187     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
188     {
189         if ( buffer == null )
190         {
191             throw new EncoderException( I18n.err( I18n.ERR_148 ) );
192         }
193 
194         try
195         {
196             // The PA-ENC-TS-ENC SEQ Tag
197             buffer.put( UniversalTag.SEQUENCE.getValue() );
198             buffer.put( TLV.getBytes( paEncTsEncLength ) );
199 
200             // The patimestamp, first the tag, then the value
201             buffer.put( ( byte ) KerberosConstants.PA_ENC_TS_ENC_PA_TIMESTAMP_TAG );
202             buffer.put( ( byte ) 0x11 );
203 
204             buffer.put( UniversalTag.GENERALIZED_TIME.getValue() );
205             buffer.put( ( byte ) 0x0F );
206             buffer.put( patimestamp.getBytes() );
207 
208             // The pausec, first the tag, then the value, if any
209             if ( pausec != null )
210             {
211                 buffer.put( ( byte ) KerberosConstants.PA_ENC_TS_ENC_PA_USEC_TAG );
212                 buffer.put( TLV.getBytes( paUsecLength ) );
213                 BerValue.encode( buffer, pausec );
214             }
215         }
216         catch ( BufferOverflowException boe )
217         {
218             log.error( I18n.err( I18n.ERR_140, 1 + TLV.getNbBytes( paEncTsEncLength ) + paEncTsEncLength,
219                 buffer.capacity() ) );
220             throw new EncoderException( I18n.err( I18n.ERR_138 ), boe );
221         }
222 
223         if ( IS_DEBUG )
224         {
225             log.debug( "Checksum encoding : {}", Strings.dumpBytes( buffer.array() ) );
226             log.debug( "Checksum initial value : {}", this );
227         }
228 
229         return buffer;
230     }
231 
232 
233     /**
234      * @see Object#toString()
235      */
236     public String toString()
237     {
238         return toString( "" );
239     }
240 
241 
242     /**
243      * @see Object#toString()
244      */
245     public String toString( String tabs )
246     {
247         StringBuilder sb = new StringBuilder();
248 
249         sb.append( tabs ).append( "PA-ENC-TS-ENC : {\n" );
250         sb.append( tabs ).append( "    patimestamp : " ).append( patimestamp ).append( '\n' );
251 
252         if ( pausec != null )
253         {
254             sb.append( tabs + "    pausec :" ).append( pausec ).append( '\n' );
255         }
256 
257         sb.append( tabs + "}\n" );
258 
259         return sb.toString();
260     }
261 }