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.application.ApplicationReply;
027    import org.apache.directory.shared.asn1.der.ASN1InputStream;
028    import org.apache.directory.shared.asn1.der.DERApplicationSpecific;
029    import org.apache.directory.shared.asn1.der.DEREncodable;
030    import org.apache.directory.shared.asn1.der.DERSequence;
031    import org.apache.directory.shared.asn1.der.DERTaggedObject;
032    
033    
034    /**
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Rev: 540371 $, $Date: 2007-05-21 17:00:43 -0700 (Mon, 21 May 2007) $
037     */
038    public class ApplicationReplyDecoder
039    {
040        /**
041         * Decodes a byte array into an {@link ApplicationReply}.
042         *
043         * @param encodedAuthHeader
044         * @return The {@link ApplicationReply}.
045         * @throws IOException
046         */
047        public ApplicationReply decode( byte[] encodedAuthHeader ) throws IOException
048        {
049            ASN1InputStream ais = new ASN1InputStream( encodedAuthHeader );
050    
051            DERApplicationSpecific app = ( DERApplicationSpecific ) ais.readObject();
052    
053            DERSequence apreq = ( DERSequence ) app.getObject();
054    
055            return decodeApplicationRequestSequence( apreq );
056        }
057    
058    
059        /*
060         AP-REP ::=         [APPLICATION 15] SEQUENCE {
061         pvno[0]                   INTEGER,
062         msg-type[1]               INTEGER,
063         enc-part[2]               EncryptedData
064         }
065         */
066        private ApplicationReply decodeApplicationRequestSequence( DERSequence sequence )
067        {
068            ApplicationReply authHeader = null;
069    
070            for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
071            {
072                DERTaggedObject object = ( ( DERTaggedObject ) e.nextElement() );
073                int tag = object.getTagNo();
074                DEREncodable derObject = object.getObject();
075    
076                switch ( tag )
077                {
078                    case 0:
079                        //DERInteger tag0 = ( DERInteger ) derObject;
080                        //authHeader.setProtocolVersionNumber( tag0.intValue() );
081                        break;
082                    case 1:
083                        //DERInteger tag1 = ( DERInteger ) derObject;
084                        //authHeader.setMessageType( MessageType.getTypeByOrdinal( tag1.intValue() ) );
085                        break;
086                    case 2:
087                        DERSequence tag2 = ( DERSequence ) derObject;
088                        authHeader = new ApplicationReply( EncryptedDataDecoder.decode( tag2 ) );
089                        break;
090                }
091            }
092    
093            return authHeader;
094        }
095    }