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.encoder;
21  
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  
26  import org.apache.directory.server.kerberos.shared.messages.components.Ticket;
27  import org.apache.directory.shared.asn1.der.ASN1OutputStream;
28  import org.apache.directory.shared.asn1.der.DERApplicationSpecific;
29  import org.apache.directory.shared.asn1.der.DERGeneralString;
30  import org.apache.directory.shared.asn1.der.DERInteger;
31  import org.apache.directory.shared.asn1.der.DERSequence;
32  import org.apache.directory.shared.asn1.der.DERTaggedObject;
33  
34  
35  /**
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   * @version $Rev: 589780 $, $Date: 2007-10-29 19:14:59 +0100 (Mo, 29 Okt 2007) $
38   */
39  public class TicketEncoder
40  {
41      /**
42       * Encodes a {@link Ticket} into a its ASN.1 DER encoding.
43       * 
44       * @param ticket
45       * @return The byte[] containing the ASN.1 DER encoding of the {@link Ticket}.
46       * @throws IOException
47       */
48      public static byte[] encodeTicket( Ticket ticket ) throws IOException
49      {
50          ByteArrayOutputStream baos = new ByteArrayOutputStream();
51          ASN1OutputStream aos = new ASN1OutputStream( baos );
52  
53          aos.writeObject( encode( ticket ) );
54          aos.close();
55  
56          return baos.toByteArray();
57      }
58  
59  
60      /**
61       * Ticket ::=                    [APPLICATION 1] SEQUENCE {
62       *     tkt-vno[0]                   INTEGER,
63       *     realm[1]                     Realm,
64       *     sname[2]                     PrincipalName,
65       *     enc-part[3]                  EncryptedData
66       * }
67       */
68      protected static DERApplicationSpecific encode( Ticket ticket )
69      {
70          DERSequence vector = new DERSequence();
71  
72          vector.add( new DERTaggedObject( 0, DERInteger.valueOf( ticket.getTktVno() ) ) );
73          vector.add( new DERTaggedObject( 1, DERGeneralString.valueOf( ticket.getRealm() ) ) );
74          vector.add( new DERTaggedObject( 2, PrincipalNameEncoder.encode( ticket.getSName() ) ) );
75          vector.add( new DERTaggedObject( 3, EncryptedDataEncoder.encodeSequence( ticket.getEncPart() ) ) );
76  
77          DERApplicationSpecific ticketSequence = null;
78  
79          try
80          {
81              ticketSequence = DERApplicationSpecific.valueOf( 1, vector );
82          }
83          catch ( Exception e )
84          {
85              e.printStackTrace();
86          }
87  
88          return ticketSequence;
89      }
90  
91  
92      protected static DERSequence encodeSequence( Ticket[] tickets )
93      {
94          DERSequence outerVector = new DERSequence();
95  
96          for ( int ii = 0; ii < tickets.length; ii++ )
97          {
98              DERSequence vector = new DERSequence();
99              vector.add( encode( tickets[ii] ) );
100             outerVector.add( vector );
101         }
102 
103         return outerVector;
104     }
105 }