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.Authenticator;
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.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$, $Date$
39   */
40  public class AuthenticatorEncoder implements Encoder, EncoderFactory
41  {
42      /**
43       * Application code constant for the {@link Authenticator} (2).
44       */
45      private static final int APPLICATION_CODE = 2;
46  
47  
48      /**
49       * Encodes an {@link Authenticator} into a byte array.
50       *
51       * @param authenticator
52       * @return The byte array.
53       * @throws IOException
54       */
55      public byte[] encode( Encodable authenticator ) throws IOException
56      {
57          ByteArrayOutputStream baos = new ByteArrayOutputStream();
58          ASN1OutputStream aos = new ASN1OutputStream( baos );
59  
60          DERSequence replySequence = encodeInitialSequence( ( Authenticator ) authenticator );
61          aos.writeObject( DERApplicationSpecific.valueOf( APPLICATION_CODE, replySequence ) );
62          aos.close();
63  
64          return baos.toByteArray();
65      }
66  
67  
68      public Encoder getEncoder()
69      {
70          return new AuthenticatorEncoder();
71      }
72  
73  
74      /**
75       * Encodes an {@link Authenticator} into a {@link DERSequence}.
76       * 
77       * -- Unencrypted authenticator
78       * Authenticator ::=    [APPLICATION 2] SEQUENCE
79       * {
80       *                authenticator-vno[0]          INTEGER,
81       *                crealm[1]                     Realm,
82       *                cname[2]                      PrincipalName,
83       *                cksum[3]                      Checksum OPTIONAL,
84       *                cusec[4]                      INTEGER,
85       *                ctime[5]                      KerberosTime,
86       *                subkey[6]                     EncryptionKey OPTIONAL,
87       *                seq-number[7]                 INTEGER OPTIONAL,
88       *  
89       *                authorization-data[8]         AuthorizationData OPTIONAL
90       * }
91       * 
92       * @param authenticator 
93       * @return The {@link DERSequence}.
94       */
95      private DERSequence encodeInitialSequence( Authenticator authenticator )
96      {
97          String clientRealm = authenticator.getClientPrincipal().getRealm();
98  
99          DERSequence sequence = new DERSequence();
100 
101         sequence.add( new DERTaggedObject( 0, DERInteger.valueOf( authenticator.getVersionNumber() ) ) );
102         sequence.add( new DERTaggedObject( 1, DERGeneralString.valueOf( clientRealm ) ) );
103         sequence.add( new DERTaggedObject( 2, PrincipalNameEncoder.encode( authenticator.getClientPrincipal() ) ) );
104 
105         // OPTIONAL
106         if ( authenticator.getChecksum() != null )
107         {
108             sequence.add( new DERTaggedObject( 3, ChecksumEncoder.encode( authenticator.getChecksum() ) ) );
109         }
110 
111         sequence.add( new DERTaggedObject( 4, DERInteger.valueOf( authenticator.getClientMicroSecond() ) ) );
112         sequence.add( new DERTaggedObject( 5, KerberosTimeEncoder.encode( authenticator.getClientTime() ) ) );
113 
114         // OPTIONAL
115         if ( authenticator.getSubSessionKey() != null )
116         {
117             sequence.add( new DERTaggedObject( 6, EncryptionKeyEncoder
118                 .encodeSequence( authenticator.getSubSessionKey() ) ) );
119         }
120 
121         // OPTIONAL
122         if ( authenticator.getSequenceNumber() > 0 )
123         {
124             sequence.add( new DERTaggedObject( 7, DERInteger.valueOf( authenticator.getSequenceNumber() ) ) );
125         }
126 
127         // OPTIONAL
128         if ( authenticator.getAuthorizationData() != null )
129         {
130             sequence.add( new DERTaggedObject( 8, AuthorizationDataEncoder
131                 .encode( authenticator.getAuthorizationData() ) ) );
132         }
133 
134         return sequence;
135     }
136 }