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.EncryptionKey;
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-22 02:00:43 +0200 (Di, 22 Mai 2007) $
39   */
40  public class EncryptionKeyDecoder
41  {
42      /**
43       * Decodes a byte array into an {@link EncryptionKey}.
44       *
45       * @param encodedEncryptionKey
46       * @return The {@link EncryptionKey}.
47       * @throws IOException
48       */
49      public static EncryptionKey decode( byte[] encodedEncryptionKey ) throws IOException
50      {
51          ASN1InputStream ais = new ASN1InputStream( encodedEncryptionKey );
52  
53          DERSequence sequence = ( DERSequence ) ais.readObject();
54  
55          return decode( sequence );
56      }
57  
58  
59      /**
60       * EncryptionKey ::=   SEQUENCE {
61       *     keytype[0]    INTEGER,
62       *     keyvalue[1]   OCTET STRING
63       * }
64       */
65      protected static EncryptionKey decode( DERSequence sequence )
66      {
67          EncryptionType type = EncryptionType.NULL;
68          byte[] data = null;
69  
70          for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
71          {
72              DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
73              int tag = object.getTagNo();
74              DEREncodable derObject = object.getObject();
75  
76              switch ( tag )
77              {
78                  case 0:
79                      DERInteger tag0 = ( DERInteger ) derObject;
80                      type = EncryptionType.getTypeByOrdinal( tag0.intValue() );
81                      break;
82                  case 1:
83                      DEROctetString tag1 = ( DEROctetString ) derObject;
84                      data = tag1.getOctets();
85                      break;
86              }
87          }
88  
89          return new EncryptionKey( type, data );
90      }
91  }