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.crypto.encryption.EncryptionType;
027    import org.apache.directory.server.kerberos.shared.messages.value.EncryptionTypeInfoEntry;
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: 540371 $, $Date: 2007-05-21 17:00:43 -0700 (Mon, 21 May 2007) $
039     */
040    public class EncryptionTypeInfoDecoder
041    {
042        /**
043         * Decodes a byte array into an array of {@link EncryptionTypeInfoEntry}.
044         *
045         * @param encodedEntries
046         * @return The array of {@link EncryptionTypeInfoEntry}.
047         * @throws IOException
048         */
049        public EncryptionTypeInfoEntry[] decode( byte[] encodedEntries ) throws IOException
050        {
051            ASN1InputStream ais = new ASN1InputStream( encodedEntries );
052    
053            DERSequence sequence = ( DERSequence ) ais.readObject();
054    
055            return decodeSequence( sequence );
056        }
057    
058    
059        /**
060         * ETYPE-INFO              ::= SEQUENCE OF ETYPE-INFO-ENTRY
061         */
062        protected static EncryptionTypeInfoEntry[] decodeSequence( DERSequence sequence )
063        {
064            EncryptionTypeInfoEntry[] entrySequence = new EncryptionTypeInfoEntry[sequence.size()];
065    
066            int ii = 0;
067            for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
068            {
069                DERSequence object = (DERSequence)e.nextElement();
070                entrySequence[ii] = decode( object );
071                ii++;
072            }
073    
074            return entrySequence;
075        }
076    
077    
078        /**
079         * ETYPE-INFO-ENTRY        ::= SEQUENCE {
080         *     etype               [0] Int32,
081         *     salt                [1] OCTET STRING OPTIONAL
082         * }
083         */
084        protected static EncryptionTypeInfoEntry decode( DERSequence sequence )
085        {
086            EncryptionType encryptionType = EncryptionType.NULL;
087            byte[] salt = new byte[0];
088    
089            for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
090            {
091                DERTaggedObject object = (DERTaggedObject)e.nextElement();
092                int tag = object.getTagNo();
093                DEREncodable derObject = object.getObject();
094    
095                switch ( tag )
096                {
097                    case 0:
098                        DERInteger type = ( DERInteger ) derObject;
099                        encryptionType = EncryptionType.getTypeByOrdinal( type.intValue() );
100                        break;
101                    case 1:
102                        DEROctetString value = ( DEROctetString ) derObject;
103                        salt = value.getOctets();
104                        break;
105                }
106            }
107    
108            return new EncryptionTypeInfoEntry( encryptionType, salt );
109        }
110    }