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.KerberosMessageType;
27  import org.apache.directory.server.kerberos.shared.messages.application.PrivateMessage;
28  import org.apache.directory.shared.asn1.der.ASN1InputStream;
29  import org.apache.directory.shared.asn1.der.DERApplicationSpecific;
30  import org.apache.directory.shared.asn1.der.DEREncodable;
31  import org.apache.directory.shared.asn1.der.DERInteger;
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: 589780 $, $Date: 2007-10-29 19:14:59 +0100 (Mo, 29 Okt 2007) $
39   */
40  public class PrivateMessageDecoder
41  {
42      /**
43       * Decodes a byte array into a {@link PrivateMessage}.
44       *
45       * @param encodedPrivateMessage
46       * @return The {@link PrivateMessage}.
47       * @throws IOException
48       */
49      public PrivateMessage decode( byte[] encodedPrivateMessage ) throws IOException
50      {
51          ASN1InputStream ais = new ASN1InputStream( encodedPrivateMessage );
52  
53          DERApplicationSpecific app = ( DERApplicationSpecific ) ais.readObject();
54  
55          DERSequence privateMessage = ( DERSequence ) app.getObject();
56  
57          return decodePrivateMessageSequence( privateMessage );
58      }
59  
60  
61      private PrivateMessage decodePrivateMessageSequence( DERSequence sequence )
62      {
63          PrivateMessage message = new PrivateMessage();
64  
65          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
66          {
67              DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
68              int tag = object.getTagNo();
69              DEREncodable derObject = object.getObject();
70  
71              switch ( tag )
72              {
73                  case 0:
74                      DERInteger tag0 = ( DERInteger ) derObject;
75                      message.setProtocolVersionNumber( tag0.intValue() );
76                      break;
77                      
78                  case 1:
79                      DERInteger tag1 = ( DERInteger ) derObject;
80                      message.setMessageType( KerberosMessageType.getTypeByOrdinal( tag1.intValue() ) );
81                      break;
82                      
83                  case 3:
84                      DERSequence tag3 = ( DERSequence ) derObject;
85                      message.setEncryptedPart( EncryptedDataDecoder.decode( tag3 ) );
86                      break;
87              }
88          }
89  
90          return message;
91      }
92  }