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.codec.actions;
021
022
023import org.apache.directory.api.asn1.DecoderException;
024import org.apache.directory.api.asn1.ber.Asn1Container;
025import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
026import org.apache.directory.api.asn1.ber.tlv.BerValue;
027import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
028import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
029import org.apache.directory.api.asn1.ber.tlv.TLV;
030import org.apache.directory.api.i18n.I18n;
031import org.apache.directory.api.util.Strings;
032import org.apache.directory.shared.kerberos.KerberosMessageType;
033import org.apache.directory.shared.kerberos.codec.kdcRep.KdcRepContainer;
034import org.apache.directory.shared.kerberos.codec.kdcReq.KdcReqContainer;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038
039/**
040 * The action used to read and validate the msg-type
041 *
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 */
044public abstract class AbstractReadMsgType<E extends Asn1Container> extends GrammarAction<E>
045{
046    /** The logger */
047    private static final Logger LOG = LoggerFactory.getLogger( AbstractReadMsgType.class );
048
049    /** The msgType to decode */
050    private KerberosMessageType msgType = null;
051
052
053    /**
054     * Instantiates a new StoreMsgType action.
055     */
056    public AbstractReadMsgType( String name )
057    {
058        super( name );
059    }
060
061
062    /**
063     * Instantiates a new StoreMsgType action.
064     */
065    public AbstractReadMsgType( String name, KerberosMessageType msgType )
066    {
067        super( name );
068        this.msgType = msgType;
069    }
070
071
072    /**
073     * {@inheritDoc}
074     */
075    public final void action( E container ) throws DecoderException
076    {
077        TLV tlv = container.getCurrentTLV();
078
079        // The Length should not be null and should be 1
080        if ( tlv.getLength() != 1 )
081        {
082            LOG.error( I18n.err( I18n.ERR_01308_ZERO_LENGTH_TLV ) );
083
084            // This will generate a PROTOCOL_ERROR
085            throw new DecoderException( I18n.err( I18n.ERR_01309_EMPTY_TLV ) );
086        }
087
088        BerValue value = tlv.getValue();
089
090        try
091        {
092            int msgTypeValue = IntegerDecoder.parse( value );
093
094            if ( msgType != null )
095            {
096                if ( msgType.getValue() == msgTypeValue )
097                {
098                    LOG.debug( "msg-type : {}", msgType );
099
100                    return;
101                }
102
103                String message = I18n.err( I18n.ERR_05102_INVALID_MESSAGE_ID, Strings.dumpBytes( value.getData() ) );
104                LOG.error( message );
105
106                // This will generate a PROTOCOL_ERROR
107                throw new DecoderException( message );
108            }
109            else
110            {
111                KerberosMessageType messageType = KerberosMessageType.getTypeByValue( msgTypeValue );
112
113                if ( ( container instanceof KdcReqContainer )
114                        &&  ( ( ( KdcReqContainer ) container ).getKdcReq().getMessageType() == messageType ) )
115                {
116                    return;
117                }
118                else if ( ( container instanceof KdcRepContainer )
119                        && ( ( ( KdcRepContainer ) container ).getKdcRep().getMessageType() == messageType ) )
120                {
121                    return;
122                }
123
124                String message = I18n.err( I18n.ERR_05102_INVALID_MESSAGE_ID, Strings.dumpBytes( value.getData() ) );
125                LOG.error( message );
126
127                // This will generate a PROTOCOL_ERROR
128                throw new DecoderException( message );
129            }
130        }
131        catch ( IntegerDecoderException ide )
132        {
133            LOG.error( I18n.err( I18n.ERR_05102_INVALID_MESSAGE_ID, Strings.dumpBytes( value.getData() ), ide
134                .getLocalizedMessage() ) );
135
136            // This will generate a PROTOCOL_ERROR
137            throw new DecoderException( ide.getMessage() );
138        }
139    }
140}