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.EncryptedData;
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: 587682 $, $Date: 2007-10-24 00:47:43 +0200 (Mi, 24 Okt 2007) $
39   */
40  public class EncryptedDataDecoder
41  {
42      /**
43       * Decodes a byte array into an {@link EncryptedData}.
44       *
45       * @param encodedEncryptedData
46       * @return The {@link EncryptedData}.
47       * @throws IOException
48       */
49      public static EncryptedData decode( byte[] encodedEncryptedData ) throws IOException
50      {
51          ASN1InputStream ais = new ASN1InputStream( encodedEncryptedData );
52  
53          DERSequence sequence = ( DERSequence ) ais.readObject();
54  
55          return decode( sequence );
56      }
57  
58  
59      /**
60       * Decodes a {@link DERSequence} into an {@link EncryptedData}.
61       * 
62       * EncryptedData ::=   SEQUENCE {
63       *             etype[0]     INTEGER, -- EncryptionEngine
64       *             kvno[1]      INTEGER OPTIONAL,
65       *             cipher[2]    OCTET STRING -- ciphertext
66       * }
67       * 
68       * @param sequence 
69       * @return The {@link EncryptedData}.
70       */
71      public static EncryptedData decode( DERSequence sequence )
72      {
73          EncryptedData encryptedData = new EncryptedData();
74  
75          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
76          {
77              DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
78              int tag = object.getTagNo();
79              DEREncodable derObject = object.getObject();
80  
81              switch ( tag )
82              {
83                  case 0:
84                      DERInteger etype = ( DERInteger ) derObject;
85                      encryptedData.setEType( EncryptionType.getTypeByOrdinal( etype.intValue() ) );
86                      break;
87                      
88                  case 1:
89                      DERInteger kvno = ( DERInteger ) derObject;
90                      encryptedData.setKvno( kvno.intValue() );
91                      break;
92                      
93                  case 2:
94                      DEROctetString cipher = ( DEROctetString ) derObject;
95                      encryptedData.setCipher( cipher.getOctets() );
96                      break;
97              }
98          }
99  
100         return encryptedData;
101     }
102 }