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.decoder;
21  
22  
23  import java.io.IOException;
24  import java.util.Enumeration;
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.server.kerberos.shared.messages.components.AuthenticatorModifier;
29  import org.apache.directory.shared.asn1.der.ASN1InputStream;
30  import org.apache.directory.shared.asn1.der.DERApplicationSpecific;
31  import org.apache.directory.shared.asn1.der.DEREncodable;
32  import org.apache.directory.shared.asn1.der.DERGeneralString;
33  import org.apache.directory.shared.asn1.der.DERGeneralizedTime;
34  import org.apache.directory.shared.asn1.der.DERInteger;
35  import org.apache.directory.shared.asn1.der.DERSequence;
36  import org.apache.directory.shared.asn1.der.DERTaggedObject;
37  
38  
39  /**
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   * @version $Rev: 502338 $, $Date: 2007-02-01 20:59:43 +0100 (Do, 01 Feb 2007) $
42   */
43  public class AuthenticatorDecoder implements Decoder, DecoderFactory
44  {
45      public Decoder getDecoder()
46      {
47          return new AuthenticatorDecoder();
48      }
49  
50  
51      public Encodable decode( byte[] encodedAuthenticator ) throws IOException
52      {
53          ASN1InputStream ais = new ASN1InputStream( encodedAuthenticator );
54  
55          DERApplicationSpecific app = ( DERApplicationSpecific ) ais.readObject();
56  
57          DERSequence sequence = ( DERSequence ) app.getObject();
58  
59          return decode( sequence );
60      }
61  
62  
63      /**
64       * -- Unencrypted authenticator
65       * Authenticator ::=    [APPLICATION 2] SEQUENCE
66       * {
67       *                authenticator-vno[0]          INTEGER,
68       *                crealm[1]                     Realm,
69       *                cname[2]                      PrincipalName,
70       *                cksum[3]                      Checksum OPTIONAL,
71       *                cusec[4]                      INTEGER,
72       *                ctime[5]                      KerberosTime,
73       *                subkey[6]                     EncryptionKey OPTIONAL,
74       *                seq-number[7]                 INTEGER OPTIONAL,
75       *  
76       *                authorization-data[8]         AuthorizationData OPTIONAL
77       * }
78       */
79      protected static Authenticator decode( DERSequence sequence )
80      {
81          AuthenticatorModifier modifier = new AuthenticatorModifier();
82  
83          for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
84          {
85              DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
86              int tag = object.getTagNo();
87              DEREncodable derObject = object.getObject();
88  
89              switch ( tag )
90              {
91                  case 0:
92                      DERInteger tag0 = ( DERInteger ) derObject;
93                      modifier.setVersionNumber( tag0.intValue() );
94                      break;
95                  case 1:
96                      DERGeneralString tag1 = ( DERGeneralString ) derObject;
97                      modifier.setClientRealm( tag1.getString() );
98                      break;
99                  case 2:
100                     DERSequence tag2 = ( DERSequence ) derObject;
101                     modifier.setClientName( PrincipalNameDecoder.decode( tag2 ) );
102                     break;
103                 case 3:
104                     DERSequence tag3 = ( DERSequence ) derObject;
105                     modifier.setChecksum( ChecksumDecoder.decode( tag3 ) );
106                     break;
107                 case 4:
108                     DERInteger tag4 = ( DERInteger ) derObject;
109                     modifier.setClientMicroSecond( tag4.intValue() );
110                     break;
111                 case 5:
112                     DERGeneralizedTime tag5 = ( DERGeneralizedTime ) derObject;
113                     modifier.setClientTime( KerberosTimeDecoder.decode( tag5 ) );
114                     break;
115                 case 6:
116                     DERSequence tag6 = ( DERSequence ) derObject;
117                     modifier.setSubSessionKey( EncryptionKeyDecoder.decode( tag6 ) );
118                     break;
119                 case 7:
120                     DERInteger tag7 = ( DERInteger ) derObject;
121                     modifier.setSequenceNumber( tag7.intValue() );
122                     break;
123                 case 8:
124                     DERSequence tag8 = ( DERSequence ) derObject;
125                     modifier.setAuthorizationData( AuthorizationDataDecoder.decodeSequence( tag8 ) );
126                     break;
127             }
128         }
129 
130         return modifier.getAuthenticator();
131     }
132 }