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 */
020package org.apache.directory.shared.kerberos.messages;
021
022
023import java.nio.ByteBuffer;
024
025import org.apache.directory.api.asn1.EncoderException;
026import org.apache.directory.api.asn1.ber.tlv.TLV;
027import org.apache.directory.shared.kerberos.KerberosConstants;
028import org.apache.directory.shared.kerberos.KerberosMessageType;
029import org.apache.directory.shared.kerberos.components.KdcReq;
030
031
032/**
033 * TGS-REQ message. It's just a KDC-REQ message with a message type set to 12.
034 *  It will store the object described by the ASN.1 grammar :
035 * <pre>
036 * TGS-REQ         ::= [APPLICATION 12] &lt;KDC-REQ&gt;
037 * </pre>
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class TgsReq extends KdcReq
041{
042    // Storage for computed lengths
043    private int kdcReqLength;
044    private int tgsReqLength;
045
046
047    /**
048     * Creates a new instance of TGS-REQ.
049     */
050    public TgsReq()
051    {
052        super( KerberosMessageType.TGS_REQ );
053    }
054
055
056    /**
057     * Compute the TGS-REQ length
058     * <pre>
059     * TGS-REQ :
060     * 
061     * 0x6A L1 TGS-REQ message
062     *  |
063     *  +--&gt;  0x30 L2 KDC-REQ sequence
064     * </pre>
065     */
066    @Override
067    public int computeLength()
068    {
069        kdcReqLength = 0;
070        tgsReqLength = 0;
071
072        kdcReqLength = super.computeLength();
073        tgsReqLength = 1 + TLV.getNbBytes( kdcReqLength ) + kdcReqLength;
074
075        return tgsReqLength;
076    }
077
078
079    /**
080     * Encode the TGS-REQ component
081     * 
082     * @param buffer The buffer containing the encoded result
083     * @return The encoded component
084     * @throws EncoderException If the encoding failed
085     */
086    @Override
087    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
088    {
089        if ( buffer == null )
090        {
091            buffer = ByteBuffer.allocate( computeLength() );
092        }
093
094        // The TGS-REQ SEQ Tag
095        buffer.put( ( byte ) KerberosConstants.TGS_REQ_TAG );
096        buffer.put( TLV.getBytes( kdcReqLength ) );
097
098        // The KDC-REQ --------------------------------------------------------
099        super.encode( buffer );
100
101        return buffer;
102    }
103}