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.KerberosMessageType;
027    import org.apache.directory.server.kerberos.shared.messages.application.PrivateMessage;
028    import org.apache.directory.shared.asn1.der.ASN1InputStream;
029    import org.apache.directory.shared.asn1.der.DERApplicationSpecific;
030    import org.apache.directory.shared.asn1.der.DEREncodable;
031    import org.apache.directory.shared.asn1.der.DERInteger;
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: 589780 $, $Date: 2007-10-29 19:14:59 +0100 (Mon, 29 Oct 2007) $
039     */
040    public class PrivateMessageDecoder
041    {
042        /**
043         * Decodes a byte array into a {@link PrivateMessage}.
044         *
045         * @param encodedPrivateMessage
046         * @return The {@link PrivateMessage}.
047         * @throws IOException
048         */
049        public PrivateMessage decode( byte[] encodedPrivateMessage ) throws IOException
050        {
051            ASN1InputStream ais = new ASN1InputStream( encodedPrivateMessage );
052    
053            DERApplicationSpecific app = ( DERApplicationSpecific ) ais.readObject();
054    
055            DERSequence privateMessage = ( DERSequence ) app.getObject();
056    
057            return decodePrivateMessageSequence( privateMessage );
058        }
059    
060    
061        private PrivateMessage decodePrivateMessageSequence( DERSequence sequence )
062        {
063            PrivateMessage message = new PrivateMessage();
064    
065            for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
066            {
067                DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
068                int tag = object.getTagNo();
069                DEREncodable derObject = object.getObject();
070    
071                switch ( tag )
072                {
073                    case 0:
074                        DERInteger tag0 = ( DERInteger ) derObject;
075                        message.setProtocolVersionNumber( tag0.intValue() );
076                        break;
077                        
078                    case 1:
079                        DERInteger tag1 = ( DERInteger ) derObject;
080                        message.setMessageType( KerberosMessageType.getTypeByOrdinal( tag1.intValue() ) );
081                        break;
082                        
083                    case 3:
084                        DERSequence tag3 = ( DERSequence ) derObject;
085                        message.setEncryptedPart( EncryptedDataDecoder.decode( tag3 ) );
086                        break;
087                }
088            }
089    
090            return message;
091        }
092    }