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.server.kerberos.shared.io.decoder;
21  
22  
23  import java.io.IOException;
24  import java.util.Enumeration;
25  
26  import org.apache.directory.server.kerberos.shared.messages.Encodable;
27  import org.apache.directory.server.kerberos.shared.messages.value.EncryptedTimeStamp;
28  import org.apache.directory.server.kerberos.shared.messages.value.EncryptedTimeStampModifier;
29  import org.apache.directory.shared.asn1.der.ASN1InputStream;
30  import org.apache.directory.shared.asn1.der.DEREncodable;
31  import org.apache.directory.shared.asn1.der.DERGeneralizedTime;
32  import org.apache.directory.shared.asn1.der.DERInteger;
33  import org.apache.directory.shared.asn1.der.DERSequence;
34  import org.apache.directory.shared.asn1.der.DERTaggedObject;
35  
36  
37  /**
38   * padata-type     ::= PA-ENC-TIMESTAMP
39   * padata-value    ::= EncryptedData -- PA-ENC-TS-ENC
40   * 
41   * PA-ENC-TS-ENC   ::= SEQUENCE {
42   *         patimestamp[0]               KerberosTime, -- client's time
43   *         pausec[1]                    INTEGER OPTIONAL
44   * }
45   * 
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   * @version $Rev: 502338 $, $Date: 2007-02-01 20:59:43 +0100 (Do, 01 Feb 2007) $
48   */
49  public class EncryptedTimestampDecoder implements Decoder, DecoderFactory
50  {
51      public Decoder getDecoder()
52      {
53          return new EncryptedTimestampDecoder();
54      }
55  
56  
57      public Encodable decode( byte[] encodedEncryptedTimestamp ) throws IOException
58      {
59          ASN1InputStream ais = new ASN1InputStream( encodedEncryptedTimestamp );
60  
61          DERSequence sequence = ( DERSequence ) ais.readObject();
62  
63          return decodeEncryptedTimestamp( sequence );
64      }
65  
66  
67      protected EncryptedTimeStamp decodeEncryptedTimestamp( DERSequence sequence )
68      {
69          EncryptedTimeStampModifier modifier = new EncryptedTimeStampModifier();
70  
71          for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
72          {
73              DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
74              int tag = object.getTagNo();
75              DEREncodable derObject = object.getObject();
76  
77              switch ( tag )
78              {
79                  case 0:
80                      DERGeneralizedTime tag0 = ( DERGeneralizedTime ) derObject;
81                      modifier.setKerberosTime( KerberosTimeDecoder.decode( tag0 ) );
82                      break;
83                  case 1:
84                      DERInteger tag1 = ( DERInteger ) derObject;
85                      modifier.setMicroSecond( tag1.intValue() );
86                      break;
87              }
88          }
89  
90          return modifier.getEncryptedTimestamp();
91      }
92  }