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.AuthorizationData;
28  import org.apache.directory.server.kerberos.shared.messages.value.AuthorizationDataEntry;
29  import org.apache.directory.server.kerberos.shared.messages.value.types.AuthorizationType;
30  import org.apache.directory.shared.asn1.der.ASN1InputStream;
31  import org.apache.directory.shared.asn1.der.DEREncodable;
32  import org.apache.directory.shared.asn1.der.DERInteger;
33  import org.apache.directory.shared.asn1.der.DEROctetString;
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: 587531 $, $Date: 2007-10-23 16:59:10 +0200 (Di, 23 Okt 2007) $
41   */
42  public class AuthorizationDataDecoder implements Decoder, DecoderFactory
43  {
44      public Decoder getDecoder()
45      {
46          return new AuthorizationDataDecoder();
47      }
48  
49  
50      public Encodable decode( byte[] encodedAuthData ) throws IOException
51      {
52          ASN1InputStream ais = new ASN1InputStream( encodedAuthData );
53  
54          DERSequence sequence = ( DERSequence ) ais.readObject();
55  
56          return decodeSequence( sequence );
57      }
58  
59  
60      /**
61       * AuthorizationData ::=   SEQUENCE OF SEQUENCE {
62       *     ad-type[0]               INTEGER,
63       *     ad-data[1]               OCTET STRING
64       * }
65       */
66      protected static AuthorizationData decodeSequence( DERSequence sequence )
67      {
68          AuthorizationData authData = new AuthorizationData();
69  
70          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
71          {
72              DERSequence object = ( DERSequence ) e.nextElement();
73              AuthorizationDataEntry entry = decodeAuthorizationEntry( object );
74              authData.add( entry );
75          }
76  
77          return authData;
78      }
79  
80  
81      protected static AuthorizationDataEntry decodeAuthorizationEntry( DERSequence sequence )
82      {
83          AuthorizationType type = AuthorizationType.NULL;
84          byte[] data = null;
85  
86          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
87          {
88              DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
89              int tag = object.getTagNo();
90              DEREncodable derObject = object.getObject();
91  
92              switch ( tag )
93              {
94                  case 0:
95                      DERInteger tag0 = ( DERInteger ) derObject;
96                      type = AuthorizationType.getTypeByOrdinal( tag0.intValue() );
97                      break;
98                      
99                  case 1:
100                     DEROctetString tag1 = ( DEROctetString ) derObject;
101                     data = tag1.getOctets();
102                     break;
103             }
104         }
105 
106         return new AuthorizationDataEntry( type, data );
107     }
108 }