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.Encodable;
27  import org.apache.directory.server.kerberos.shared.messages.components.EncTicketPart;
28  import org.apache.directory.shared.asn1.der.ASN1OutputStream;
29  import org.apache.directory.shared.asn1.der.DERApplicationSpecific;
30  import org.apache.directory.shared.asn1.der.DERBitString;
31  import org.apache.directory.shared.asn1.der.DERGeneralString;
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: 546367 $, $Date: 2007-06-12 05:30:24 +0200 (Di, 12 Jun 2007) $
39   */
40  public class EncTicketPartEncoder implements Encoder, EncoderFactory
41  {
42      /**
43       * Application code constant for the {@link EncTicketPart} (3).
44       */
45      private static final int APPLICATION_CODE = 3;
46  
47  
48      public byte[] encode( Encodable ticketPart ) throws IOException
49      {
50          ByteArrayOutputStream baos = new ByteArrayOutputStream();
51          ASN1OutputStream aos = new ASN1OutputStream( baos );
52  
53          DERSequence ticketSequence = encodeInitialSequence( ( EncTicketPart ) ticketPart );
54          aos.writeObject( DERApplicationSpecific.valueOf( APPLICATION_CODE, ticketSequence ) );
55          aos.close();
56  
57          return baos.toByteArray();
58      }
59  
60  
61      public Encoder getEncoder()
62      {
63          return new EncTicketPartEncoder();
64      }
65  
66  
67      /**
68       * Encodes an {@link EncTicketPart} into a {@link DERSequence}.
69       * 
70       * -- Encrypted part of ticket
71       * EncTicketPart ::=     [APPLICATION 3] SEQUENCE {
72       *                       flags[0]             TicketFlags,
73       *                       key[1]               EncryptionKey,
74       *                       crealm[2]            Realm,
75       *                       cname[3]             PrincipalName,
76       *                       transited[4]         TransitedEncoding,
77       *                       authtime[5]          KerberosTime,
78       *                       starttime[6]         KerberosTime OPTIONAL,
79       *                       endtime[7]           KerberosTime,
80       *                       renew-till[8]        KerberosTime OPTIONAL,
81       *                       caddr[9]             HostAddresses OPTIONAL,
82       *                       authorization-data[10]   AuthorizationData OPTIONAL
83       * }
84       * 
85       * @param ticketPart 
86       * @return The {@link DERSequence}.
87       */
88      public DERSequence encodeInitialSequence( EncTicketPart ticketPart )
89      {
90          DERSequence sequence = new DERSequence();
91  
92          sequence.add( new DERTaggedObject( 0, new DERBitString( ticketPart.getFlags().getBytes() ) ) );
93          sequence.add( new DERTaggedObject( 1, EncryptionKeyEncoder.encodeSequence( ticketPart.getSessionKey() ) ) );
94          sequence.add( new DERTaggedObject( 2, DERGeneralString.valueOf( ticketPart.getClientRealm().toString() ) ) );
95          sequence.add( new DERTaggedObject( 3, PrincipalNameEncoder.encode( ticketPart.getClientPrincipal() ) ) );
96          sequence.add( new DERTaggedObject( 4, TransitedEncodingEncoder.encode( ticketPart.getTransitedEncoding() ) ) );
97          sequence.add( new DERTaggedObject( 5, KerberosTimeEncoder.encode( ticketPart.getAuthTime() ) ) );
98  
99          // OPTIONAL
100         if ( ticketPart.getStartTime() != null )
101         {
102             sequence.add( new DERTaggedObject( 6, KerberosTimeEncoder.encode( ticketPart.getStartTime() ) ) );
103         }
104 
105         sequence.add( new DERTaggedObject( 7, KerberosTimeEncoder.encode( ticketPart.getEndTime() ) ) );
106 
107         // OPTIONAL
108         if ( ticketPart.getRenewTill() != null )
109         {
110             sequence.add( new DERTaggedObject( 8, KerberosTimeEncoder.encode( ticketPart.getRenewTill() ) ) );
111         }
112 
113         // OPTIONAL
114         if ( ticketPart.getClientAddresses() != null )
115         {
116             sequence
117                 .add( new DERTaggedObject( 9, HostAddressesEncoder.encodeSequence( ticketPart.getClientAddresses() ) ) );
118         }
119 
120         // OPTIONAL
121         if ( ticketPart.getAuthorizationData() != null )
122         {
123             sequence
124                 .add( new DERTaggedObject( 10, AuthorizationDataEncoder.encode( ticketPart.getAuthorizationData() ) ) );
125         }
126 
127         return sequence;
128     }
129 }