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.messages.components.Ticket;
27  import org.apache.directory.shared.asn1.der.ASN1InputStream;
28  import org.apache.directory.shared.asn1.der.DERApplicationSpecific;
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.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 TicketDecoder
41  {
42      /**
43       * Decodes a byte array into an {@link Ticket}.
44       *
45       * @param encodedTicket
46       * @return The {@link Ticket}.
47       * @throws IOException
48       */
49      public static Ticket decode( byte[] encodedTicket ) throws IOException
50      {
51          ASN1InputStream ais = new ASN1InputStream( encodedTicket );
52  
53          DERApplicationSpecific app = ( DERApplicationSpecific ) ais.readObject();
54  
55          return decode( app );
56      }
57  
58  
59      /**
60       * Decodes a {@link DERSequence} into an array of {@link Ticket}s.
61       *
62       * @param sequence
63       * @return The array of {@link Ticket}s.
64       * @throws IOException
65       */
66      public static Ticket[] decodeSequence( DERSequence sequence ) throws IOException
67      {
68          Ticket[] tickets = new Ticket[sequence.size()];
69  
70          int ii = 0;
71          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
72          {
73              DERApplicationSpecific object = ( DERApplicationSpecific ) e.nextElement();
74              tickets[ii] = decode( object );
75          }
76  
77          return tickets;
78      }
79  
80  
81      /**
82       * Ticket ::=                    [APPLICATION 1] SEQUENCE {
83       *     tkt-vno[0]                   INTEGER,
84       *     realm[1]                     Realm,
85       *     sname[2]                     PrincipalName,
86       *     enc-part[3]                  EncryptedData
87       * }
88       */
89      protected static Ticket decode( DERApplicationSpecific app ) throws IOException
90      {
91          DERSequence sequence = ( DERSequence ) app.getObject();
92  
93          Ticket ticket = new Ticket();
94  
95          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
96          {
97              DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
98              int tag = object.getTagNo();
99              DEREncodable derObject = object.getObject();
100 
101             switch ( tag )
102             {
103                 case 0:
104                     DERInteger tag0 = ( DERInteger ) derObject;
105                     ticket.setTktVno( tag0.intValue() );
106                     break;
107                     
108                 case 1:
109                     DERGeneralString tag1 = ( DERGeneralString ) derObject;
110                     ticket.setRealm( tag1.getString() );
111                     break;
112                     
113                 case 2:
114                     DERSequence tag2 = ( DERSequence ) derObject;
115                     ticket.setSName( PrincipalNameDecoder.decode( tag2 ) );
116                     break;
117                     
118                 case 3:
119                     DERSequence tag3 = ( DERSequence ) derObject;
120                     ticket.setEncPart( EncryptedDataDecoder.decode( tag3 ) );
121                     break;
122             }
123         }
124 
125         return ticket;
126     }
127 }