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.value.PaData;
027    import org.apache.directory.server.kerberos.shared.messages.value.types.PaDataType;
028    import org.apache.directory.shared.asn1.der.ASN1InputStream;
029    import org.apache.directory.shared.asn1.der.DEREncodable;
030    import org.apache.directory.shared.asn1.der.DERInteger;
031    import org.apache.directory.shared.asn1.der.DEROctetString;
032    import org.apache.directory.shared.asn1.der.DERSequence;
033    import org.apache.directory.shared.asn1.der.DERTaggedObject;
034    
035    
036    /**
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev: 587624 $, $Date: 2007-10-23 21:22:08 +0200 (Tue, 23 Oct 2007) $
039     */
040    public class PreAuthenticationDataDecoder
041    {
042        /**
043         * Decodes a byte array into {@link PaData}.
044         *
045         * @param encodedPreAuthData
046         * @return The {@link PaData}.
047         * @throws IOException
048         */
049        public PaData decode( byte[] encodedPreAuthData ) throws IOException
050        {
051            ASN1InputStream ais = new ASN1InputStream( encodedPreAuthData );
052    
053            DERSequence sequence = ( DERSequence ) ais.readObject();
054    
055            return decode( sequence );
056        }
057    
058    
059        /**
060         * KDC-REQ ::=        SEQUENCE {
061         *            pvno[1]               INTEGER,
062         *            msg-type[2]           INTEGER,
063         *            padata[3]             SEQUENCE OF PA-DATA OPTIONAL,
064         *            req-body[4]           KDC-REQ-BODY
065         * }
066         */
067        protected static PaData[] decodeSequence( DERSequence sequence )
068        {
069            PaData[] paDataSequence = new PaData[sequence.size()];
070    
071            int ii = 0;
072            
073            for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
074            {
075                DERSequence object = ( DERSequence ) e.nextElement();
076                PaData paData = PreAuthenticationDataDecoder.decode( object );
077                paDataSequence[ii] = paData;
078                ii++;
079            }
080    
081            return paDataSequence;
082        }
083    
084    
085        /**
086         * PA-DATA ::=        SEQUENCE {
087         *            padata-type[1]        INTEGER,
088         *            padata-value[2]       OCTET STRING,
089         *                          -- might be encoded AP-REQ
090         * }
091         */
092        protected static PaData decode( DERSequence sequence )
093        {
094            PaData paData = new PaData();
095    
096            for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
097            {
098                DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
099                int tag = object.getTagNo();
100                DEREncodable derObject = object.getObject();
101    
102                switch ( tag )
103                {
104                    case 1:
105                        DERInteger padataType = ( DERInteger ) derObject;
106                        PaDataType type = PaDataType.getTypeByOrdinal( padataType.intValue() );
107                        paData.setPaDataType( type );
108                        break;
109                    case 2:
110                        DEROctetString padataValue = ( DEROctetString ) derObject;
111                        paData.setPaDataValue( padataValue.getOctets() );
112                        break;
113                }
114            }
115    
116            return paData;
117        }
118    }