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.krbSafe;
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.krbSafe.actions.CheckMsgType;
030import org.apache.directory.shared.kerberos.codec.krbSafe.actions.KrbSafeInit;
031import org.apache.directory.shared.kerberos.codec.krbSafe.actions.StoreChecksum;
032import org.apache.directory.shared.kerberos.codec.krbSafe.actions.StorePvno;
033import org.apache.directory.shared.kerberos.codec.krbSafe.actions.StoreSafeBody;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037
038/**
039 * This class implements the KRB-SAFE structure. All the actions are declared
040 * in this class. As it is a singleton, these declaration are only done once. If
041 * an action is to be added or modified, this is where the work is to be done !
042 *
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 */
045public final class KrbSafeGrammar extends AbstractGrammar<KrbSafeContainer>
046{
047    /** The logger */
048    static final Logger LOG = LoggerFactory.getLogger( KrbSafeGrammar.class );
049
050    /** A speedup for logger */
051    static final boolean IS_DEBUG = LOG.isDebugEnabled();
052
053    /** The instance of grammar. KrbSafeGrammar is a singleton */
054    private static Grammar<KrbSafeContainer> instance = new KrbSafeGrammar();
055
056
057    /**
058     * Creates a new KrbSafeGrammar object.
059     */
060    @SuppressWarnings("unchecked")
061    private KrbSafeGrammar()
062    {
063        setName( KrbSafeGrammar.class.getName() );
064
065        // Create the transitions table
066        super.transitions = new GrammarTransition[KrbSafeStatesEnum.LAST_KRB_SAFE_STATE.ordinal()][256];
067
068        // ============================================================================================
069        // KRB-SAFE
070        // ============================================================================================
071        // --------------------------------------------------------------------------------------------
072        // Transition from KrbSafe init to KrbSafe tag
073        // --------------------------------------------------------------------------------------------
074        // KRB-SAFE       ::= [APPLICATION 20]
075        super.transitions[KrbSafeStatesEnum.START_STATE.ordinal()][KerberosConstants.KRB_SAFE_TAG] =
076            new GrammarTransition<KrbSafeContainer>(
077                KrbSafeStatesEnum.START_STATE,
078                KrbSafeStatesEnum.KRB_SAFE_TAG_STATE,
079                KerberosConstants.KRB_SAFE_TAG,
080                new KrbSafeInit() );
081
082        // --------------------------------------------------------------------------------------------
083        // Transition from KrbSafe tag to KrbSafe SEQ
084        // --------------------------------------------------------------------------------------------
085        // KRB-SAFE       ::= [APPLICATION 20] SEQUENCE {
086        super.transitions[KrbSafeStatesEnum.KRB_SAFE_TAG_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
087            new GrammarTransition<KrbSafeContainer>(
088                KrbSafeStatesEnum.KRB_SAFE_TAG_STATE,
089                KrbSafeStatesEnum.KRB_SAFE_SEQ_STATE,
090                UniversalTag.SEQUENCE,
091                new CheckNotNullLength<KrbSafeContainer>() );
092
093        // --------------------------------------------------------------------------------------------
094        // Transition from KrbSafe SEQ to pvno tag
095        // --------------------------------------------------------------------------------------------
096        // KRB-SAFE         ::= SEQUENCE {
097        //         pvno            [0]
098        super.transitions[KrbSafeStatesEnum.KRB_SAFE_SEQ_STATE.ordinal()][KerberosConstants.KRB_SAFE_PVNO_TAG] =
099            new GrammarTransition<KrbSafeContainer>(
100                KrbSafeStatesEnum.KRB_SAFE_SEQ_STATE,
101                KrbSafeStatesEnum.KRB_SAFE_PVNO_TAG_STATE,
102                KerberosConstants.KRB_SAFE_PVNO_TAG,
103                new CheckNotNullLength<KrbSafeContainer>() );
104
105        // --------------------------------------------------------------------------------------------
106        // Transition from pvno tag to pvno value
107        // --------------------------------------------------------------------------------------------
108        // KRB-SAFE         ::= SEQUENCE {
109        //         pvno            [0] INTEGER (5) ,
110        super.transitions[KrbSafeStatesEnum.KRB_SAFE_PVNO_TAG_STATE.ordinal()][UniversalTag.INTEGER.getValue()] =
111            new GrammarTransition<KrbSafeContainer>(
112                KrbSafeStatesEnum.KRB_SAFE_PVNO_TAG_STATE,
113                KrbSafeStatesEnum.KRB_SAFE_PVNO_STATE,
114                UniversalTag.INTEGER,
115                new StorePvno() );
116
117        // --------------------------------------------------------------------------------------------
118        // Transition from pvno to msg-type tag
119        // --------------------------------------------------------------------------------------------
120        // msg-type        [1]
121        super.transitions[KrbSafeStatesEnum.KRB_SAFE_PVNO_STATE.ordinal()][KerberosConstants.KRB_SAFE_MSGTYPE_TAG] =
122            new GrammarTransition<KrbSafeContainer>(
123                KrbSafeStatesEnum.KRB_SAFE_PVNO_STATE,
124                KrbSafeStatesEnum.KRB_SAFE_MSGTYPE_TAG_STATE,
125                KerberosConstants.KRB_SAFE_MSGTYPE_TAG,
126                new CheckNotNullLength<KrbSafeContainer>() );
127
128        // --------------------------------------------------------------------------------------------
129        // Transition from msg-type tag to msg-type value
130        // --------------------------------------------------------------------------------------------
131        // msg-type        [1] INTEGER (30)
132        super.transitions[KrbSafeStatesEnum.KRB_SAFE_MSGTYPE_TAG_STATE.ordinal()][UniversalTag.INTEGER.getValue()] =
133            new GrammarTransition<KrbSafeContainer>(
134                KrbSafeStatesEnum.KRB_SAFE_MSGTYPE_TAG_STATE,
135                KrbSafeStatesEnum.KRB_SAFE_MSGTYPE_STATE,
136                UniversalTag.INTEGER,
137                new CheckMsgType() );
138
139        // --------------------------------------------------------------------------------------------
140        // Transition from msg-type value to safe-body tag
141        // --------------------------------------------------------------------------------------------
142        // safe-body       [2] KRB-SAFE-BODY
143        super.transitions[KrbSafeStatesEnum.KRB_SAFE_MSGTYPE_STATE.ordinal()][KerberosConstants.KRB_SAFE_SAFE_BODY_TAG] =
144            new GrammarTransition<KrbSafeContainer>(
145                KrbSafeStatesEnum.KRB_SAFE_MSGTYPE_STATE,
146                KrbSafeStatesEnum.KRB_SAFE_SAFE_BODY_TAG_STATE,
147                KerberosConstants.KRB_SAFE_SAFE_BODY_TAG,
148                new StoreSafeBody() );
149
150        // --------------------------------------------------------------------------------------------
151        // Transition from safe-body tag to cksum tag
152        // --------------------------------------------------------------------------------------------
153        // cksum           [3] Checksum
154        super.transitions[KrbSafeStatesEnum.KRB_SAFE_SAFE_BODY_TAG_STATE.ordinal()][KerberosConstants.KRB_SAFE_CKSUM_TAG] =
155            new GrammarTransition<KrbSafeContainer>(
156                KrbSafeStatesEnum.KRB_SAFE_SAFE_BODY_TAG_STATE,
157                KrbSafeStatesEnum.KRB_SAFE_CKSUM_TAG_STATE,
158                KerberosConstants.KRB_SAFE_CKSUM_TAG,
159                new StoreChecksum() );
160    }
161
162
163    /**
164     * Get the instance of this grammar
165     *
166     * @return An instance on the KRB-SAFE Grammar
167     */
168    public static Grammar<KrbSafeContainer> getInstance()
169    {
170        return instance;
171    }
172}