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.KdcReply;
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.DERInteger;
33  import org.apache.directory.shared.asn1.der.DERSequence;
34  import org.apache.directory.shared.asn1.der.DERTaggedObject;
35  
36  
37  /**
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   * @version $Rev: 540371 $, $Date: 2007-05-22 02:00:43 +0200 (Di, 22 Mai 2007) $
40   */
41  public abstract class EncKdcRepPartEncoder implements Encoder
42  {
43      private int applicationCode;
44  
45  
46      protected EncKdcRepPartEncoder(int applicationCode)
47      {
48          this.applicationCode = applicationCode;
49      }
50  
51  
52      public byte[] encode( Encodable app ) throws IOException
53      {
54          ByteArrayOutputStream baos = new ByteArrayOutputStream();
55          ASN1OutputStream aos = new ASN1OutputStream( baos );
56  
57          DERSequence initialSequence = encodeInitialSequence( ( KdcReply ) app );
58          aos.writeObject( DERApplicationSpecific.valueOf( applicationCode, initialSequence ) );
59  
60          return baos.toByteArray();
61      }
62  
63  
64      /**
65       *    EncKDCRepPart ::=   SEQUENCE {
66       *                key[0]                       EncryptionKey,
67       *                last-req[1]                  LastReq,
68       * 
69       *                nonce[2]                     INTEGER,
70       *                key-expiration[3]            KerberosTime OPTIONAL,
71       *                flags[4]                     TicketFlags,
72       *                authtime[5]                  KerberosTime,
73       *                starttime[6]                 KerberosTime OPTIONAL,
74       *                endtime[7]                   KerberosTime,
75       *                renew-till[8]                KerberosTime OPTIONAL,
76       *                srealm[9]                    Realm,
77       *                sname[10]                    PrincipalName,
78       *                caddr[11]                    HostAddresses OPTIONAL
79       * }
80       */
81      protected DERSequence encodeInitialSequence( KdcReply reply )
82      {
83          DERSequence sequence = new DERSequence();
84  
85          sequence.add( new DERTaggedObject( 0, EncryptionKeyEncoder.encodeSequence( reply.getKey() ) ) );
86          sequence.add( new DERTaggedObject( 1, LastRequestEncoder.encode( reply.getLastRequest() ) ) );
87          sequence.add( new DERTaggedObject( 2, DERInteger.valueOf( reply.getNonce() ) ) );
88  
89          // OPTIONAL
90          if ( reply.getKeyExpiration() != null )
91          {
92              sequence.add( new DERTaggedObject( 3, KerberosTimeEncoder.encode( reply.getKeyExpiration() ) ) );
93          }
94  
95          sequence.add( new DERTaggedObject( 4, new DERBitString( reply.getFlags().getBytes() ) ) );
96          sequence.add( new DERTaggedObject( 5, KerberosTimeEncoder.encode( reply.getAuthTime() ) ) );
97  
98          // OPTIONAL
99          if ( reply.getStartTime() != null )
100         {
101             sequence.add( new DERTaggedObject( 6, KerberosTimeEncoder.encode( reply.getStartTime() ) ) );
102         }
103 
104         sequence.add( new DERTaggedObject( 7, KerberosTimeEncoder.encode( reply.getEndTime() ) ) );
105 
106         // OPTIONAL
107         if ( reply.getRenewTill() != null )
108         {
109             sequence.add( new DERTaggedObject( 8, KerberosTimeEncoder.encode( reply.getRenewTill() ) ) );
110         }
111 
112         sequence.add( new DERTaggedObject( 9, DERGeneralString.valueOf( reply.getServerRealm().toString() ) ) );
113         sequence.add( new DERTaggedObject( 10, PrincipalNameEncoder.encode( reply.getServerPrincipal() ) ) );
114 
115         // OPTIONAL
116         if ( reply.getClientAddresses() != null )
117         {
118             sequence.add( new DERTaggedObject( 11, HostAddressesEncoder.encodeSequence( reply.getClientAddresses() ) ) );
119         }
120 
121         return sequence;
122     }
123 }