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.KerberosTime;
030import org.apache.directory.shared.kerberos.components.KdcRep;
031import org.apache.directory.shared.kerberos.components.PrincipalName;
032import org.apache.directory.shared.kerberos.flags.TicketFlags;
033
034
035/**
036 * AS-REQ message. It's just a KDC-REP message with a message type set to 11.
037 *  It will store the object described by the ASN.1 grammar :
038 * <pre>
039 * AS-REP          ::= [APPLICATION 11] &lt;KDC-RE&gt;P
040 * </pre>
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public class AsRep extends KdcRep
044{
045    // Storage for computed lengths
046    private int kdcRepLength;
047    private int asRepLength;
048
049
050    /**
051     * Creates a new instance of AS-REP.
052     */
053    public AsRep()
054    {
055        super( KerberosMessageType.AS_REP );
056    }
057
058
059    /**
060     * Returns the end {@link KerberosTime}.
061     *
062     * @return The end {@link KerberosTime}.
063     */
064    public KerberosTime getEndTime()
065    {
066        return encKdcRepPart.getEndTime();
067    }
068
069
070    /**
071     * Returns the {@link TicketFlags}.
072     *
073     * @return The {@link TicketFlags}.
074     */
075    public TicketFlags getFlags()
076    {
077        return encKdcRepPart.getFlags();
078    }
079
080
081    /**
082     * Returns the nonce.
083     *
084     * @return The nonce.
085     */
086    public int getNonce()
087    {
088        return encKdcRepPart.getNonce();
089    }
090
091
092    /**
093     * Returns the renew till {@link KerberosTime}.
094     *
095     * @return The renew till {@link KerberosTime}.
096     */
097    public KerberosTime getRenewTill()
098    {
099        return encKdcRepPart.getRenewTill();
100    }
101
102
103    /**
104     * Returns the start {@link KerberosTime}.
105     *
106     * @return The start {@link KerberosTime}.
107     */
108    public KerberosTime getStartTime()
109    {
110        return encKdcRepPart.getStartTime();
111    }
112
113
114    /**
115     * Returns the server {@link PrincipalName}.
116     *
117     * @return The server {@link PrincipalName}.
118     */
119    public PrincipalName getSName()
120    {
121        return encKdcRepPart.getSName();
122    }
123
124
125    /**
126     * Compute the AS-REP length
127     * <pre>
128     * AS-REP :
129     * 
130     * 0x6B L1 AS-REP message
131     *  |
132     *  +--&gt;  0x30 L2 KDC-REP sequence
133     * </pre>
134     */
135    @Override
136    public int computeLength()
137    {
138        kdcRepLength = super.computeLength();
139        asRepLength = 1 + TLV.getNbBytes( kdcRepLength ) + kdcRepLength;
140
141        return asRepLength;
142    }
143
144
145    /**
146     * Encode the AS-REP component
147     * 
148     * @param buffer The buffer containing the encoded result
149     * @return The encoded component
150     * @throws org.apache.directory.api.asn1.EncoderException If the encoding failed
151     */
152    @Override
153    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
154    {
155        if ( buffer == null )
156        {
157            buffer = ByteBuffer.allocate( computeLength() );
158        }
159
160        // The AS-REP SEQ Tag
161        buffer.put( ( byte ) KerberosConstants.AS_REP_TAG );
162        buffer.put( TLV.getBytes( kdcRepLength ) );
163
164        // The KDC-REP --------------------------------------------------------
165        super.encode( buffer );
166
167        return buffer;
168    }
169}