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.value.PaData;
27  import org.apache.directory.server.kerberos.shared.messages.value.types.PaDataType;
28  import org.apache.directory.shared.asn1.der.ASN1InputStream;
29  import org.apache.directory.shared.asn1.der.DEREncodable;
30  import org.apache.directory.shared.asn1.der.DERInteger;
31  import org.apache.directory.shared.asn1.der.DEROctetString;
32  import org.apache.directory.shared.asn1.der.DERSequence;
33  import org.apache.directory.shared.asn1.der.DERTaggedObject;
34  
35  
36  /**
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev: 587624 $, $Date: 2007-10-23 21:22:08 +0200 (Di, 23 Okt 2007) $
39   */
40  public class PreAuthenticationDataDecoder
41  {
42      /**
43       * Decodes a byte array into {@link PaData}.
44       *
45       * @param encodedPreAuthData
46       * @return The {@link PaData}.
47       * @throws IOException
48       */
49      public PaData decode( byte[] encodedPreAuthData ) throws IOException
50      {
51          ASN1InputStream ais = new ASN1InputStream( encodedPreAuthData );
52  
53          DERSequence sequence = ( DERSequence ) ais.readObject();
54  
55          return decode( sequence );
56      }
57  
58  
59      /**
60       * KDC-REQ ::=        SEQUENCE {
61       *            pvno[1]               INTEGER,
62       *            msg-type[2]           INTEGER,
63       *            padata[3]             SEQUENCE OF PA-DATA OPTIONAL,
64       *            req-body[4]           KDC-REQ-BODY
65       * }
66       */
67      protected static PaData[] decodeSequence( DERSequence sequence )
68      {
69          PaData[] paDataSequence = new PaData[sequence.size()];
70  
71          int ii = 0;
72          
73          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
74          {
75              DERSequence object = ( DERSequence ) e.nextElement();
76              PaData paData = PreAuthenticationDataDecoder.decode( object );
77              paDataSequence[ii] = paData;
78              ii++;
79          }
80  
81          return paDataSequence;
82      }
83  
84  
85      /**
86       * PA-DATA ::=        SEQUENCE {
87       *            padata-type[1]        INTEGER,
88       *            padata-value[2]       OCTET STRING,
89       *                          -- might be encoded AP-REQ
90       * }
91       */
92      protected static PaData decode( DERSequence sequence )
93      {
94          PaData paData = new PaData();
95  
96          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
97          {
98              DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
99              int tag = object.getTagNo();
100             DEREncodable derObject = object.getObject();
101 
102             switch ( tag )
103             {
104                 case 1:
105                     DERInteger padataType = ( DERInteger ) derObject;
106                     PaDataType type = PaDataType.getTypeByOrdinal( padataType.intValue() );
107                     paData.setPaDataType( type );
108                     break;
109                 case 2:
110                     DEROctetString padataValue = ( DEROctetString ) derObject;
111                     paData.setPaDataValue( padataValue.getOctets() );
112                     break;
113             }
114         }
115 
116         return paData;
117     }
118 }