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.encoder;
021    
022    
023    import java.io.ByteArrayOutputStream;
024    import java.io.IOException;
025    
026    import org.apache.directory.server.kerberos.shared.messages.ApplicationRequest;
027    import org.apache.directory.shared.asn1.der.ASN1OutputStream;
028    import org.apache.directory.shared.asn1.der.DERApplicationSpecific;
029    import org.apache.directory.shared.asn1.der.DERBitString;
030    import org.apache.directory.shared.asn1.der.DERInteger;
031    import org.apache.directory.shared.asn1.der.DERSequence;
032    import org.apache.directory.shared.asn1.der.DERTaggedObject;
033    
034    
035    /**
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev: 540371 $, $Date: 2007-05-21 17:00:43 -0700 (Mon, 21 May 2007) $
038     */
039    public class ApplicationRequestEncoder
040    {
041        /**
042         * Application code constant for the {@link ApplicationRequest} (14).
043         */
044        public static final int APPLICATION_CODE = 14;
045    
046    
047        /**
048         * Encodes an {@link ApplicationRequest} into a byte array.
049         *
050         * @param request
051         * @return The byte array.
052         * @throws IOException
053         */
054        public byte[] encode( ApplicationRequest request ) throws IOException
055        {
056            ByteArrayOutputStream baos = new ByteArrayOutputStream();
057            ASN1OutputStream aos = new ASN1OutputStream( baos );
058    
059            DERSequence requestSequence = encodeReplySequence( request );
060            aos.writeObject( DERApplicationSpecific.valueOf( APPLICATION_CODE, requestSequence ) );
061            aos.close();
062    
063            return baos.toByteArray();
064        }
065    
066    
067        /*
068         AP-REQ ::=      [APPLICATION 14] SEQUENCE {
069         pvno[0]                       INTEGER,
070         msg-type[1]                   INTEGER,
071         ap-options[2]                 APOptions,
072         ticket[3]                     Ticket,
073         authenticator[4]              EncryptedData
074         }
075         */
076        private DERSequence encodeReplySequence( ApplicationRequest message )
077        {
078            DERSequence sequence = new DERSequence();
079    
080            sequence.add( new DERTaggedObject( 0, DERInteger.valueOf( message.getProtocolVersionNumber() ) ) );
081            sequence.add( new DERTaggedObject( 1, DERInteger.valueOf( message.getMessageType().getOrdinal() ) ) );
082            sequence.add( new DERTaggedObject( 2, new DERBitString( message.getApOptions().getBytes() ) ) );
083            sequence.add( new DERTaggedObject( 3, TicketEncoder.encode( message.getTicket() ) ) );
084            sequence.add( new DERTaggedObject( 4, EncryptedDataEncoder.encodeSequence( message.getEncPart() ) ) );
085    
086            return sequence;
087        }
088    }