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.crypto.encryption.EncryptionType;
27  import org.apache.directory.server.kerberos.shared.messages.value.EncryptionTypeInfo2Entry;
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.DERGeneralString;
31  import org.apache.directory.shared.asn1.der.DERInteger;
32  import org.apache.directory.shared.asn1.der.DEROctetString;
33  import org.apache.directory.shared.asn1.der.DERSequence;
34  import org.apache.directory.shared.asn1.der.DERTaggedObject;
35  
36  
37  /**
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   * @version $Rev: 540371 $, $Date: 2007-05-21 17:00:43 -0700 (Mon, 21 May 2007) $
40   */
41  public class EncryptionTypeInfo2Decoder
42  {
43      /**
44       * Decodes a byte array into an array of {@link EncryptionTypeInfo2Entry}.
45       *
46       * @param encodedEntries
47       * @return The array of {@link EncryptionTypeInfo2Entry}.
48       * @throws IOException
49       */
50      public EncryptionTypeInfo2Entry[] decode( byte[] encodedEntries ) throws IOException
51      {
52          ASN1InputStream ais = new ASN1InputStream( encodedEntries );
53  
54          DERSequence sequence = ( DERSequence ) ais.readObject();
55  
56          return decodeSequence( sequence );
57      }
58  
59  
60      /**
61       * ETYPE-INFO2             ::= SEQUENCE SIZE (1..MAX) OF ETYPE-INFO2-ENTRY
62       */
63      protected static EncryptionTypeInfo2Entry[] decodeSequence( DERSequence sequence )
64      {
65          EncryptionTypeInfo2Entry[] entrySequence = new EncryptionTypeInfo2Entry[sequence.size()];
66  
67          int ii = 0;
68          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
69          {
70              DERSequence object = (DERSequence)e.nextElement();
71              entrySequence[ii] = decode( object );
72              ii++;
73          }
74  
75          return entrySequence;
76      }
77  
78  
79      /**
80       * ETYPE-INFO2-ENTRY       ::= SEQUENCE {
81       *         etype           [0] Int32,
82       *         salt            [1] KerberosString OPTIONAL,
83       *         s2kparams       [2] OCTET STRING OPTIONAL
84       * }
85       */
86      protected static EncryptionTypeInfo2Entry decode( DERSequence sequence )
87      {
88          EncryptionType encryptionType = EncryptionType.NULL;
89          String salt = new String();
90          byte[] s2kparams = new byte[0];
91  
92          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
93          {
94              DERTaggedObject object = (DERTaggedObject)e.nextElement();
95              int tag = object.getTagNo();
96              DEREncodable derObject = object.getObject();
97  
98              switch ( tag )
99              {
100                 case 0:
101                     DERInteger tag0 = ( DERInteger ) derObject;
102                     encryptionType = EncryptionType.getTypeByOrdinal( tag0.intValue() );
103                     break;
104                 case 1:
105                     DERGeneralString tag1 = ( DERGeneralString ) derObject;
106                     salt = tag1.getString();
107                     break;
108                 case 2:
109                     DEROctetString tag2 = ( DEROctetString ) derObject;
110                     s2kparams = tag2.getOctets();
111                     break;
112             }
113         }
114 
115         return new EncryptionTypeInfo2Entry( encryptionType, salt, s2kparams );
116     }
117 }