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.components.EncApRepPart;
28  import org.apache.directory.server.kerberos.shared.messages.components.EncApRepPartModifier;
29  import org.apache.directory.shared.asn1.der.ASN1InputStream;
30  import org.apache.directory.shared.asn1.der.DERApplicationSpecific;
31  import org.apache.directory.shared.asn1.der.DEREncodable;
32  import org.apache.directory.shared.asn1.der.DERGeneralizedTime;
33  import org.apache.directory.shared.asn1.der.DERInteger;
34  import org.apache.directory.shared.asn1.der.DERSequence;
35  import org.apache.directory.shared.asn1.der.DERTaggedObject;
36  
37  
38  /**
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   * @version $Rev: 502338 $, $Date: 2007-02-01 11:59:43 -0800 (Thu, 01 Feb 2007) $
41   */
42  public class EncApRepPartDecoder implements Decoder, DecoderFactory
43  {
44      public Decoder getDecoder()
45      {
46          return new EncApRepPartDecoder();
47      }
48  
49  
50      public Encodable decode( byte[] encodedEncApRepPart ) throws IOException
51      {
52          ASN1InputStream ais = new ASN1InputStream( encodedEncApRepPart );
53  
54          DERApplicationSpecific app = ( DERApplicationSpecific ) ais.readObject();
55  
56          DERSequence apRepPart = ( DERSequence ) app.getObject();
57  
58          return decodeEncApRepPartSequence( apRepPart );
59      }
60  
61  
62      /**
63       * Decodes a {@link DERSequence} into a {@link EncApRepPart}.
64       * 
65       * EncAPRepPart    ::= [APPLICATION 27] SEQUENCE {
66       *         ctime           [0] KerberosTime,
67       *         cusec           [1] Microseconds,
68       *         subkey          [2] EncryptionKey OPTIONAL,
69       *         seq-number      [3] UInt32 OPTIONAL
70       * }
71       *
72       * @param sequence
73       * @return The {@link EncApRepPart}.
74       */
75      private EncApRepPart decodeEncApRepPartSequence( DERSequence sequence )
76      {
77          EncApRepPartModifier modifier = new EncApRepPartModifier();
78  
79          for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
80          {
81              DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
82              int tag = object.getTagNo();
83              DEREncodable derObject = object.getObject();
84  
85              switch ( tag )
86              {
87                  case 0:
88                      DERGeneralizedTime tag0 = ( DERGeneralizedTime ) derObject;
89                      modifier.setClientTime( KerberosTimeDecoder.decode( tag0 ) );
90                      break;
91                  case 1:
92                      DERInteger tag1 = ( DERInteger ) derObject;
93                      modifier.setClientMicroSecond( new Integer( tag1.intValue() ) );
94                      break;
95                  case 2:
96                      DERSequence tag2 = ( DERSequence ) derObject;
97                      modifier.setSubSessionKey( EncryptionKeyDecoder.decode( tag2 ) );
98                      break;
99                  case 3:
100                     DERInteger tag3 = ( DERInteger ) derObject;
101                     modifier.setSequenceNumber( new Integer( tag3.intValue() ) );
102                     break;
103             }
104         }
105 
106         return modifier.getEncApRepPart();
107     }
108 }