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
021package org.apache.directory.shared.kerberos.messages;
022
023
024import java.nio.BufferOverflowException;
025import java.nio.ByteBuffer;
026
027import org.apache.directory.api.asn1.EncoderException;
028import org.apache.directory.api.asn1.ber.tlv.BerValue;
029import org.apache.directory.api.asn1.ber.tlv.TLV;
030import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
031import org.apache.directory.api.util.Strings;
032import org.apache.directory.server.i18n.I18n;
033import org.apache.directory.shared.kerberos.KerberosConstants;
034import org.apache.directory.shared.kerberos.KerberosMessageType;
035import org.apache.directory.shared.kerberos.components.EncryptedData;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039
040/**
041 * Class representing KRB-PRIV message
042 * 
043 * <pre>
044 * KRB-PRIV        ::= [APPLICATION 21] SEQUENCE {
045 *      pvno            [0] INTEGER (5),
046 *      msg-type        [1] INTEGER (21),
047 *                      -- NOTE: there is no [2] tag
048 *      enc-part        [3] EncryptedData -- EncKrbPrivPart
049 * }
050 * </pre>
051 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
052 */
053public class KrbPriv extends KerberosMessage
054{
055    /** The logger */
056    private static final Logger log = LoggerFactory.getLogger( KrbError.class );
057
058    /** Speedup for logs */
059    private static final boolean IS_DEBUG = log.isDebugEnabled();
060
061    /** the encrypted EncKrbPrivPart component */
062    private EncryptedData encPart;
063
064    // Storage for computed lengths
065    private int pvnoLen;
066    private int msgTypeLength;
067    private int encPartLen;
068    private int krbPrivSeqLen;
069    private int krbPrivLen;
070
071
072    /**
073     * Creates a new instance of KrbPriv.
074     */
075    public KrbPriv()
076    {
077        super( 5, KerberosMessageType.KRB_PRIV );
078    }
079
080
081    /**
082     * @return the encPart
083     */
084    public EncryptedData getEncPart()
085    {
086        return encPart;
087    }
088
089
090    /**
091     * @param encPart the encPart to set
092     */
093    public void setEncPart( EncryptedData encPart )
094    {
095        this.encPart = encPart;
096    }
097
098
099    /**
100     * Compute the KRB-PRIV length
101     * <pre>
102     * KRB-PRIV :
103     * 
104     * 0x75 L1 KRB-PRIV APPLICATION[21]
105     *  |
106     *  +--&gt; 0x30 L2 KRB-PRIV sequence
107     *        |
108     *        +--&gt; 0xA0 0x03 pvno tag
109     *        |     |
110     *        |     +--&gt; 0x02 0x01 0x05 pvno (5)
111     *        |
112     *        +--&gt; 0xA1 0x03 msg-type tag
113     *        |     |
114     *        |     +--&gt; 0x02 0x01 0x15 msg-type (21)
115     *        |     
116     *        +--&gt; 0xA3 L3 enc-part (EncryptedData -- EncKrbPrivPart)
117     * </pre>
118     */
119    @Override
120    public int computeLength()
121    {
122        pvnoLen = 1 + 1 + 1;
123        krbPrivSeqLen = 1 + TLV.getNbBytes( pvnoLen ) + pvnoLen;
124
125        msgTypeLength = 1 + 1 + BerValue.getNbBytes( getMessageType().getValue() );
126        krbPrivSeqLen += 1 + TLV.getNbBytes( msgTypeLength ) + msgTypeLength;
127
128        encPartLen = encPart.computeLength();
129        krbPrivSeqLen += 1 + TLV.getNbBytes( encPartLen ) + encPartLen;
130
131        krbPrivLen += 1 + TLV.getNbBytes( krbPrivSeqLen ) + krbPrivSeqLen;
132
133        return 1 + TLV.getNbBytes( krbPrivLen ) + krbPrivLen;
134    }
135
136
137    /**
138     * {@inheritDoc}
139     */
140    @Override
141    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
142    {
143        if ( buffer == null )
144        {
145            throw new EncoderException( I18n.err( I18n.ERR_148 ) );
146        }
147
148        try
149        {
150            // The KRB-SAFE APPLICATION tag
151            buffer.put( ( byte ) KerberosConstants.KRB_PRIV_TAG );
152            buffer.put( TLV.getBytes( krbPrivLen ) );
153
154            // The KRB-SAFE sequence
155            buffer.put( UniversalTag.SEQUENCE.getValue() );
156            buffer.put( TLV.getBytes( krbPrivSeqLen ) );
157
158            // pvno tag and value
159            buffer.put( ( byte ) KerberosConstants.KRB_PRIV_PVNO_TAG );
160            buffer.put( TLV.getBytes( pvnoLen ) );
161            BerValue.encode( buffer, getProtocolVersionNumber() );
162
163            // msg-type tag and value
164            buffer.put( ( byte ) KerberosConstants.KRB_PRIV_MSGTYPE_TAG );
165            buffer.put( TLV.getBytes( msgTypeLength ) );
166            BerValue.encode( buffer, getMessageType().getValue() );
167
168            // enc-part
169            buffer.put( ( byte ) KerberosConstants.KRB_PRIV_ENC_PART_TAG );
170            buffer.put( TLV.getBytes( encPartLen ) );
171            encPart.encode( buffer );
172        }
173        catch ( BufferOverflowException boe )
174        {
175            log.error( I18n.err( I18n.ERR_738_CANNOT_ENCODE_KRB_PRIV, 1 + TLV.getNbBytes( krbPrivLen )
176                + krbPrivLen, buffer.capacity() ) );
177            throw new EncoderException( I18n.err( I18n.ERR_138 ), boe );
178        }
179
180        if ( IS_DEBUG )
181        {
182            log.debug( "KrbPriv encoding : {}", Strings.dumpBytes( buffer.array() ) );
183            log.debug( "KrbPriv initial value : {}", this );
184        }
185
186        return buffer;
187    }
188
189
190    /**
191     * @see Object#toString()
192     */
193    public String toString()
194    {
195        StringBuilder sb = new StringBuilder();
196
197        sb.append( "KRB-PRIV : {\n" );
198        sb.append( "    pvno: " ).append( getProtocolVersionNumber() ).append( '\n' );
199        sb.append( "    msgType: " ).append( getMessageType() ).append( '\n' );
200        sb.append( "    msgType: " ).append( getEncPart() ).append( '\n' );
201        sb.append( "}\n" );
202
203        return sb.toString();
204    }
205}