001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.kerberos.shared.io.decoder;
021    
022    
023    import java.io.IOException;
024    import java.util.Enumeration;
025    
026    import org.apache.directory.server.kerberos.shared.messages.Encodable;
027    import org.apache.directory.server.kerberos.shared.messages.components.Authenticator;
028    import org.apache.directory.server.kerberos.shared.messages.components.AuthenticatorModifier;
029    import org.apache.directory.shared.asn1.der.ASN1InputStream;
030    import org.apache.directory.shared.asn1.der.DERApplicationSpecific;
031    import org.apache.directory.shared.asn1.der.DEREncodable;
032    import org.apache.directory.shared.asn1.der.DERGeneralString;
033    import org.apache.directory.shared.asn1.der.DERGeneralizedTime;
034    import org.apache.directory.shared.asn1.der.DERInteger;
035    import org.apache.directory.shared.asn1.der.DERSequence;
036    import org.apache.directory.shared.asn1.der.DERTaggedObject;
037    
038    
039    /**
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev: 502338 $, $Date: 2007-02-01 20:59:43 +0100 (Thu, 01 Feb 2007) $
042     */
043    public class AuthenticatorDecoder implements Decoder, DecoderFactory
044    {
045        public Decoder getDecoder()
046        {
047            return new AuthenticatorDecoder();
048        }
049    
050    
051        public Encodable decode( byte[] encodedAuthenticator ) throws IOException
052        {
053            ASN1InputStream ais = new ASN1InputStream( encodedAuthenticator );
054    
055            DERApplicationSpecific app = ( DERApplicationSpecific ) ais.readObject();
056    
057            DERSequence sequence = ( DERSequence ) app.getObject();
058    
059            return decode( sequence );
060        }
061    
062    
063        /**
064         * -- Unencrypted authenticator
065         * Authenticator ::=    [APPLICATION 2] SEQUENCE
066         * {
067         *                authenticator-vno[0]          INTEGER,
068         *                crealm[1]                     Realm,
069         *                cname[2]                      PrincipalName,
070         *                cksum[3]                      Checksum OPTIONAL,
071         *                cusec[4]                      INTEGER,
072         *                ctime[5]                      KerberosTime,
073         *                subkey[6]                     EncryptionKey OPTIONAL,
074         *                seq-number[7]                 INTEGER OPTIONAL,
075         *  
076         *                authorization-data[8]         AuthorizationData OPTIONAL
077         * }
078         */
079        protected static Authenticator decode( DERSequence sequence )
080        {
081            AuthenticatorModifier modifier = new AuthenticatorModifier();
082    
083            for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
084            {
085                DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
086                int tag = object.getTagNo();
087                DEREncodable derObject = object.getObject();
088    
089                switch ( tag )
090                {
091                    case 0:
092                        DERInteger tag0 = ( DERInteger ) derObject;
093                        modifier.setVersionNumber( tag0.intValue() );
094                        break;
095                    case 1:
096                        DERGeneralString tag1 = ( DERGeneralString ) derObject;
097                        modifier.setClientRealm( tag1.getString() );
098                        break;
099                    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    }