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.EncryptionTypeInfoEntry;
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: 540371 $, $Date: 2007-05-21 17:00:43 -0700 (Mon, 21 May 2007) $
39   */
40  public class EncryptionTypeInfoDecoder
41  {
42      /**
43       * Decodes a byte array into an array of {@link EncryptionTypeInfoEntry}.
44       *
45       * @param encodedEntries
46       * @return The array of {@link EncryptionTypeInfoEntry}.
47       * @throws IOException
48       */
49      public EncryptionTypeInfoEntry[] decode( byte[] encodedEntries ) throws IOException
50      {
51          ASN1InputStream ais = new ASN1InputStream( encodedEntries );
52  
53          DERSequence sequence = ( DERSequence ) ais.readObject();
54  
55          return decodeSequence( sequence );
56      }
57  
58  
59      /**
60       * ETYPE-INFO              ::= SEQUENCE OF ETYPE-INFO-ENTRY
61       */
62      protected static EncryptionTypeInfoEntry[] decodeSequence( DERSequence sequence )
63      {
64          EncryptionTypeInfoEntry[] entrySequence = new EncryptionTypeInfoEntry[sequence.size()];
65  
66          int ii = 0;
67          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
68          {
69              DERSequence object = (DERSequence)e.nextElement();
70              entrySequence[ii] = decode( object );
71              ii++;
72          }
73  
74          return entrySequence;
75      }
76  
77  
78      /**
79       * ETYPE-INFO-ENTRY        ::= SEQUENCE {
80       *     etype               [0] Int32,
81       *     salt                [1] OCTET STRING OPTIONAL
82       * }
83       */
84      protected static EncryptionTypeInfoEntry decode( DERSequence sequence )
85      {
86          EncryptionType encryptionType = EncryptionType.NULL;
87          byte[] salt = new byte[0];
88  
89          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
90          {
91              DERTaggedObject object = (DERTaggedObject)e.nextElement();
92              int tag = object.getTagNo();
93              DEREncodable derObject = object.getObject();
94  
95              switch ( tag )
96              {
97                  case 0:
98                      DERInteger type = ( DERInteger ) derObject;
99                      encryptionType = EncryptionType.getTypeByOrdinal( type.intValue() );
100                     break;
101                 case 1:
102                     DEROctetString value = ( DEROctetString ) derObject;
103                     salt = value.getOctets();
104                     break;
105             }
106         }
107 
108         return new EncryptionTypeInfoEntry( encryptionType, salt );
109     }
110 }