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.KerberosMessageType;
27  import org.apache.directory.server.kerberos.shared.messages.ApplicationRequest;
28  import org.apache.directory.server.kerberos.shared.messages.value.ApOptions;
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.DERBitString;
32  import org.apache.directory.shared.asn1.der.DEREncodable;
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: 589780 $, $Date: 2007-10-29 19:14:59 +0100 (Mo, 29 Okt 2007) $
41   */
42  public class ApplicationRequestDecoder
43  {
44      /**
45       * Decodes a byte array into an {@link ApplicationRequest}.
46       *
47       * @param encodedAuthHeader
48       * @return The {@link ApplicationRequest}.
49       * @throws IOException
50       */
51      public ApplicationRequest decode( byte[] encodedAuthHeader ) throws IOException
52      {
53          ASN1InputStream ais = new ASN1InputStream( encodedAuthHeader );
54  
55          DERApplicationSpecific app = ( DERApplicationSpecific ) ais.readObject();
56  
57          DERSequence apreq = ( DERSequence ) app.getObject();
58  
59          return decodeApplicationRequestSequence( apreq );
60      }
61  
62  
63      /*
64       AP-REQ ::=      [APPLICATION 14] SEQUENCE {
65       pvno[0]                       INTEGER,
66       msg-type[1]                   INTEGER,
67  
68       ap-options[2]                 APOptions,
69       ticket[3]                     Ticket,
70       authenticator[4]              EncryptedData
71       }
72       */
73      private ApplicationRequest decodeApplicationRequestSequence( DERSequence sequence ) throws IOException
74      {
75          ApplicationRequest authHeader = new ApplicationRequest();
76  
77          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
78          {
79              DERTaggedObject object = ( ( DERTaggedObject ) e.nextElement() );
80              int tag = object.getTagNo();
81              DEREncodable derObject = object.getObject();
82  
83              switch ( tag )
84              {
85                  case 0:
86                      DERInteger tag0 = ( DERInteger ) derObject;
87                      authHeader.setProtocolVersionNumber( tag0.intValue() );
88                      break;
89                      
90                  case 1:
91                      DERInteger tag1 = ( DERInteger ) derObject;
92                      authHeader.setMessageType( KerberosMessageType.getTypeByOrdinal( tag1.intValue() ) );
93                      break;
94                      
95                  case 2:
96                      DERBitString apOptions = ( DERBitString ) derObject;
97                      authHeader.setApOptions( new ApOptions( apOptions.getOctets() ) );
98                      break;
99                  case 3:
100                     DERApplicationSpecific tag3 = ( DERApplicationSpecific ) derObject;
101                     authHeader.setTicket( TicketDecoder.decode( tag3 ) );
102                     break;
103                     
104                 case 4:
105                     DERSequence tag4 = ( DERSequence ) derObject;
106                     authHeader.setEncPart( EncryptedDataDecoder.decode( tag4 ) );
107                     break;
108             }
109         }
110 
111         return authHeader;
112     }
113 }