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.apRep;
021
022
023import org.apache.directory.api.asn1.actions.CheckNotNullLength;
024import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
025import org.apache.directory.api.asn1.ber.grammar.Grammar;
026import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
027import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
028import org.apache.directory.shared.kerberos.KerberosConstants;
029import org.apache.directory.shared.kerberos.codec.apRep.actions.ApRepInit;
030import org.apache.directory.shared.kerberos.codec.apRep.actions.CheckMsgType;
031import org.apache.directory.shared.kerberos.codec.apRep.actions.StoreEncPart;
032import org.apache.directory.shared.kerberos.codec.apRep.actions.StorePvno;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036
037/**
038 * This class implements the AP-REP structure. All the actions are declared
039 * in this class. As it is a singleton, these declaration are only done once. If
040 * an action is to be added or modified, this is where the work is to be done !
041 *
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 */
044public final class ApRepGrammar extends AbstractGrammar<ApRepContainer>
045{
046    /** The logger */
047    static final Logger LOG = LoggerFactory.getLogger( ApRepGrammar.class );
048
049    /** A speedup for logger */
050    static final boolean IS_DEBUG = LOG.isDebugEnabled();
051
052    /** The instance of grammar. ApRepGrammar is a singleton */
053    private static Grammar<ApRepContainer> instance = new ApRepGrammar();
054
055
056    /**
057     * Creates a new ApRepGrammar object.
058     */
059    @SuppressWarnings("unchecked")
060    private ApRepGrammar()
061    {
062        setName( ApRepGrammar.class.getName() );
063
064        // Create the transitions table
065        super.transitions = new GrammarTransition[ApRepStatesEnum.LAST_AP_REP_STATE.ordinal()][256];
066
067        // ============================================================================================
068        // AP-REP
069        // ============================================================================================
070        // --------------------------------------------------------------------------------------------
071        // Transition from AP-REP init to AP-REP tag
072        // --------------------------------------------------------------------------------------------
073        // AP-REP          ::= [APPLICATION 15]
074        super.transitions[ApRepStatesEnum.START_STATE.ordinal()][KerberosConstants.AP_REP_TAG] =
075            new GrammarTransition<ApRepContainer>(
076                ApRepStatesEnum.START_STATE,
077                ApRepStatesEnum.AP_REP_STATE,
078                KerberosConstants.AP_REP_TAG,
079                new ApRepInit() );
080
081        // --------------------------------------------------------------------------------------------
082        // Transition from AP-REP tag to AP-REP SEQUENCE
083        // --------------------------------------------------------------------------------------------
084        // AP-REP          ::= [APPLICATION 15] SEQUENCE OF {
085        super.transitions[ApRepStatesEnum.AP_REP_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
086            new GrammarTransition<ApRepContainer>(
087                ApRepStatesEnum.AP_REP_STATE,
088                ApRepStatesEnum.AP_REP_SEQ_STATE,
089                UniversalTag.SEQUENCE,
090                new CheckNotNullLength<ApRepContainer>() );
091
092        // --------------------------------------------------------------------------------------------
093        // Transition from AP-REP SEQUENCE to pvno tag
094        // --------------------------------------------------------------------------------------------
095        // AP-REP          ::= [APPLICATION 15] SEQUENCE OF {
096        //        pvno            [0]
097        super.transitions[ApRepStatesEnum.AP_REP_SEQ_STATE.ordinal()][KerberosConstants.AP_REP_PVNO_TAG] =
098            new GrammarTransition<ApRepContainer>(
099                ApRepStatesEnum.AP_REP_SEQ_STATE,
100                ApRepStatesEnum.AP_REP_PVNO_TAG_STATE,
101                KerberosConstants.AP_REP_PVNO_TAG,
102                new CheckNotNullLength<ApRepContainer>() );
103
104        // --------------------------------------------------------------------------------------------
105        // Transition from pvno tag to pvno value
106        // --------------------------------------------------------------------------------------------
107        // AP-REP          ::= [APPLICATION 15] SEQUENCE OF {
108        //        pvno            [0] INTEGER (5),
109        super.transitions[ApRepStatesEnum.AP_REP_PVNO_TAG_STATE.ordinal()][UniversalTag.INTEGER.getValue()] =
110            new GrammarTransition<ApRepContainer>(
111                ApRepStatesEnum.AP_REP_PVNO_TAG_STATE,
112                ApRepStatesEnum.AP_REP_PVNO_STATE,
113                UniversalTag.INTEGER,
114                new StorePvno() );
115
116        // --------------------------------------------------------------------------------------------
117        // Transition from pvno value to msg-type tag
118        // --------------------------------------------------------------------------------------------
119        // AP-REP          ::= [APPLICATION 15] SEQUENCE OF {
120        //        ...
121        //        msg-type        [1]
122        super.transitions[ApRepStatesEnum.AP_REP_PVNO_STATE.ordinal()][KerberosConstants.AP_REP_MSG_TYPE_TAG] =
123            new GrammarTransition<ApRepContainer>(
124                ApRepStatesEnum.AP_REP_PVNO_STATE,
125                ApRepStatesEnum.AP_REP_MSG_TYPE_TAG_STATE,
126                KerberosConstants.AP_REP_MSG_TYPE_TAG,
127                new CheckNotNullLength<ApRepContainer>() );
128
129        // --------------------------------------------------------------------------------------------
130        // Transition from msg-type tag to msg-type value
131        // --------------------------------------------------------------------------------------------
132        // AP-REP          ::= [APPLICATION 15] SEQUENCE OF {
133        //        ...
134        //        msg-type        [1] INTEGER (15),
135        super.transitions[ApRepStatesEnum.AP_REP_MSG_TYPE_TAG_STATE.ordinal()][UniversalTag.INTEGER.getValue()] =
136            new GrammarTransition<ApRepContainer>(
137                ApRepStatesEnum.AP_REP_MSG_TYPE_TAG_STATE,
138                ApRepStatesEnum.AP_REP_MSG_TYPE_STATE,
139                UniversalTag.INTEGER,
140                new CheckMsgType() );
141
142        // --------------------------------------------------------------------------------------------
143        // Transition from msg-type value to enc-part
144        // --------------------------------------------------------------------------------------------
145        // AP-REP          ::= [APPLICATION 15] SEQUENCE OF {
146        //        ...
147        //        enc-part        [2] EncryptedData -- EncAPRepPart
148        // }
149        super.transitions[ApRepStatesEnum.AP_REP_MSG_TYPE_STATE.ordinal()][KerberosConstants.AP_REP_ENC_PART_TAG] =
150            new GrammarTransition<ApRepContainer>(
151                ApRepStatesEnum.AP_REP_MSG_TYPE_STATE,
152                ApRepStatesEnum.AP_REP_ENC_PART_STATE,
153                KerberosConstants.AP_REP_ENC_PART_TAG,
154                new StoreEncPart() );
155    }
156
157
158    /**
159     * Get the instance of this grammar
160     *
161     * @return An instance on the AP-REP Grammar
162     */
163    public static Grammar<ApRepContainer> getInstance()
164    {
165        return instance;
166    }
167}