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.IOException;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.server.kerberos.shared.messages.KdcReply;
27  import org.apache.directory.server.kerberos.shared.messages.value.PaData;
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.DERGeneralString;
31  import org.apache.directory.shared.asn1.der.DERInteger;
32  import org.apache.directory.shared.asn1.der.DEROctetString;
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: 587624 $, $Date: 2007-10-23 21:22:08 +0200 (Di, 23 Okt 2007) $
40   */
41  public class KdcReplyEncoder
42  {
43      /**
44       * Encodes a {@link KdcReply} into a {@link ByteBuffer}.
45       * 
46       * AS-REP ::=    [APPLICATION 11] KDC-REP
47       * TGS-REP ::=   [APPLICATION 13] KDC-REP
48       *
49       * @param app
50       * @param out
51       * @throws IOException
52       */
53      public void encode( KdcReply app, ByteBuffer out ) throws IOException
54      {
55          ASN1OutputStream aos = new ASN1OutputStream( out );
56  
57          DERSequence kdcrep = encodeKdcReplySequence( app );
58          aos.writeObject( DERApplicationSpecific.valueOf( app.getMessageType().getOrdinal(), kdcrep ) );
59  
60          aos.close();
61      }
62  
63  
64      /*
65       KDC-REP ::=   SEQUENCE {
66       pvno[0]                    INTEGER,
67       msg-type[1]                INTEGER,
68       padata[2]                  SEQUENCE OF PA-DATA OPTIONAL,
69       crealm[3]                  Realm,
70       cname[4]                   PrincipalName,
71       ticket[5]                  Ticket,
72       enc-part[6]                EncryptedData
73       }*/
74      private DERSequence encodeKdcReplySequence( KdcReply app )
75      {
76          DERSequence sequence = new DERSequence();
77  
78          sequence.add( new DERTaggedObject( 0, DERInteger.valueOf( app.getProtocolVersionNumber() ) ) );
79  
80          sequence.add( new DERTaggedObject( 1, DERInteger.valueOf( app.getMessageType().getOrdinal() ) ) );
81  
82          if ( app.getPaData() != null )
83          {
84              sequence.add( new DERTaggedObject( 2, encodePreAuthData( app.getPaData() ) ) );
85          }
86  
87          sequence.add( new DERTaggedObject( 3, DERGeneralString.valueOf( app.getClientRealm().toString() ) ) );
88  
89          sequence.add( new DERTaggedObject( 4, PrincipalNameEncoder.encode( app.getClientPrincipal() ) ) );
90  
91          sequence.add( new DERTaggedObject( 5, TicketEncoder.encode( app.getTicket() ) ) );
92  
93          sequence.add( new DERTaggedObject( 6, EncryptedDataEncoder.encodeSequence( app.getEncPart() ) ) );
94  
95          return sequence;
96      }
97  
98  
99      /*
100      PA-DATA ::=        SEQUENCE {
101      padata-type[1]        INTEGER,
102      padata-value[2]       OCTET STRING,
103      -- might be encoded AP-REQ
104      }*/
105     private DERSequence encodePreAuthData( PaData[] preAuthData )
106     {
107         DERSequence preAuth = new DERSequence();
108 
109         for ( int ii = 0; ii < preAuthData.length; ii++ )
110         {
111             DERSequence sequence = new DERSequence();
112 
113             sequence.add( new DERTaggedObject( 1, DERInteger.valueOf( preAuthData[ii].getPaDataType().getOrdinal() ) ) );
114             sequence.add( new DERTaggedObject( 2, new DEROctetString( preAuthData[ii].getPaDataValue() ) ) );
115             preAuth.add( sequence );
116         }
117 
118         return preAuth;
119     }
120 }