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.kdcRep;
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.kdcRep.actions.AddPaData;
030import org.apache.directory.shared.kerberos.codec.kdcRep.actions.CheckMsgType;
031import org.apache.directory.shared.kerberos.codec.kdcRep.actions.StoreCName;
032import org.apache.directory.shared.kerberos.codec.kdcRep.actions.StoreCRealm;
033import org.apache.directory.shared.kerberos.codec.kdcRep.actions.StoreEncPart;
034import org.apache.directory.shared.kerberos.codec.kdcRep.actions.StorePvno;
035import org.apache.directory.shared.kerberos.codec.kdcRep.actions.StoreTicket;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039
040/**
041 * This class implements the KdcReq structure. All the actions are declared
042 * in this class. As it is a singleton, these declaration are only done once. If
043 * an action is to be added or modified, this is where the work is to be done !
044 *
045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046 */
047public final class KdcRepGrammar extends AbstractGrammar<KdcRepContainer>
048{
049    /** The logger */
050    static final Logger LOG = LoggerFactory.getLogger( KdcRepGrammar.class );
051
052    /** A speedup for logger */
053    static final boolean IS_DEBUG = LOG.isDebugEnabled();
054
055    /** The instance of grammar. KdcReqGrammar is a singleton */
056    private static Grammar<KdcRepContainer> instance = new KdcRepGrammar();
057
058
059    /**
060     * Creates a new KdcRepGrammar object.
061     */
062    @SuppressWarnings("unchecked")
063    private KdcRepGrammar()
064    {
065        setName( KdcRepGrammar.class.getName() );
066
067        // Create the transitions table
068        super.transitions = new GrammarTransition[KdcRepStatesEnum.LAST_KDC_REP_STATE.ordinal()][256];
069
070        // ============================================================================================
071        // KdcReq
072        // ============================================================================================
073        // --------------------------------------------------------------------------------------------
074        // Transition from KdcRep init to KdcRep SEQ
075        // --------------------------------------------------------------------------------------------
076        // KDC-REP         ::= SEQUENCE {
077        super.transitions[KdcRepStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
078            new GrammarTransition<KdcRepContainer>(
079                KdcRepStatesEnum.START_STATE,
080                KdcRepStatesEnum.KDC_REP_SEQ_STATE,
081                UniversalTag.SEQUENCE,
082                new CheckNotNullLength<KdcRepContainer>() );
083
084        // --------------------------------------------------------------------------------------------
085        // Transition from KdcRep SEQ to pvno tag
086        // --------------------------------------------------------------------------------------------
087        // KDC-REP         ::= SEQUENCE {
088        //         pvno            [0]
089        super.transitions[KdcRepStatesEnum.KDC_REP_SEQ_STATE.ordinal()][KerberosConstants.KDC_REP_PVNO_TAG] =
090            new GrammarTransition<KdcRepContainer>(
091                KdcRepStatesEnum.KDC_REP_SEQ_STATE,
092                KdcRepStatesEnum.KDC_REP_PVNO_TAG_STATE,
093                KerberosConstants.KDC_REP_PVNO_TAG,
094                new CheckNotNullLength<KdcRepContainer>() );
095
096        // --------------------------------------------------------------------------------------------
097        // Transition from pvno tag to pvno value
098        // --------------------------------------------------------------------------------------------
099        // KDC-REP         ::= SEQUENCE {
100        //         pvno            [0] INTEGER (5)
101        super.transitions[KdcRepStatesEnum.KDC_REP_PVNO_TAG_STATE.ordinal()][UniversalTag.INTEGER.getValue()] =
102            new GrammarTransition<KdcRepContainer>(
103                KdcRepStatesEnum.KDC_REP_PVNO_TAG_STATE,
104                KdcRepStatesEnum.KDC_REP_PVNO_STATE,
105                UniversalTag.INTEGER,
106                new StorePvno() );
107
108        // --------------------------------------------------------------------------------------------
109        // Transition from pvno value to msg-type tag
110        // --------------------------------------------------------------------------------------------
111        // KDC-REP         ::= SEQUENCE {
112        //         ...
113        //         msg-type        [1]
114        super.transitions[KdcRepStatesEnum.KDC_REP_PVNO_STATE.ordinal()][KerberosConstants.KDC_REP_MSG_TYPE_TAG] =
115            new GrammarTransition<KdcRepContainer>(
116                KdcRepStatesEnum.KDC_REP_PVNO_STATE,
117                KdcRepStatesEnum.KDC_REP_MSG_TYPE_TAG_STATE,
118                KerberosConstants.KDC_REP_MSG_TYPE_TAG,
119                new CheckNotNullLength<KdcRepContainer>() );
120
121        // --------------------------------------------------------------------------------------------
122        // Transition from msg-type tag to msg-type value
123        // --------------------------------------------------------------------------------------------
124        // KDC-REP         ::= SEQUENCE {
125        //         ...
126        //         msg-type        [1] INTEGER (11 -- AS -- | 13 -- TGS --),
127        super.transitions[KdcRepStatesEnum.KDC_REP_MSG_TYPE_TAG_STATE.ordinal()][UniversalTag.INTEGER.getValue()] =
128            new GrammarTransition<KdcRepContainer>(
129                KdcRepStatesEnum.KDC_REP_MSG_TYPE_TAG_STATE,
130                KdcRepStatesEnum.KDC_REP_MSG_TYPE_STATE,
131                UniversalTag.INTEGER,
132                new CheckMsgType() );
133
134        // --------------------------------------------------------------------------------------------
135        // Transition from msg-type value pa-data tag
136        // --------------------------------------------------------------------------------------------
137        // KDC-REP         ::= SEQUENCE {
138        //         ...
139        //         padata          [2]
140        super.transitions[KdcRepStatesEnum.KDC_REP_MSG_TYPE_STATE.ordinal()][KerberosConstants.KDC_REP_PA_DATA_TAG] =
141            new GrammarTransition<KdcRepContainer>(
142                KdcRepStatesEnum.KDC_REP_MSG_TYPE_STATE,
143                KdcRepStatesEnum.KDC_REP_PA_DATA_TAG_STATE,
144                KerberosConstants.KDC_REP_PA_DATA_TAG,
145                new CheckNotNullLength<KdcRepContainer>() );
146
147        // --------------------------------------------------------------------------------------------
148        // Transition from pa-data tag to pa-data sequence
149        // --------------------------------------------------------------------------------------------
150        // KDC-REP         ::= SEQUENCE {
151        //         ...
152        //         padata          [2] SEQUENCE OF
153        super.transitions[KdcRepStatesEnum.KDC_REP_PA_DATA_TAG_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
154            new GrammarTransition<KdcRepContainer>(
155                KdcRepStatesEnum.KDC_REP_PA_DATA_TAG_STATE,
156                KdcRepStatesEnum.KDC_REP_PA_DATA_STATE,
157                UniversalTag.SEQUENCE,
158                new CheckNotNullLength<KdcRepContainer>() );
159
160        // --------------------------------------------------------------------------------------------
161        // Transition from pa-data sequence to PA-DATA
162        // --------------------------------------------------------------------------------------------
163        // KDC-REP         ::= SEQUENCE {
164        //         ...
165        //         padata          [2] SEQUENCE OF PA-DATA
166        super.transitions[KdcRepStatesEnum.KDC_REP_PA_DATA_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
167            new GrammarTransition<KdcRepContainer>(
168                KdcRepStatesEnum.KDC_REP_PA_DATA_STATE,
169                KdcRepStatesEnum.KDC_REP_PA_DATA_STATE,
170                UniversalTag.SEQUENCE,
171                new AddPaData() );
172
173        // --------------------------------------------------------------------------------------------
174        // Transition from PA-DATA to crealm tag
175        // --------------------------------------------------------------------------------------------
176        // KDC-REP         ::= SEQUENCE {
177        //         ...
178        //         crealm          [3]
179        super.transitions[KdcRepStatesEnum.KDC_REP_PA_DATA_STATE.ordinal()][KerberosConstants.KDC_REP_CREALM_TAG] =
180            new GrammarTransition<KdcRepContainer>(
181                KdcRepStatesEnum.KDC_REP_PA_DATA_STATE,
182                KdcRepStatesEnum.KDC_REP_CREALM_TAG_STATE,
183                KerberosConstants.KDC_REP_CREALM_TAG,
184                new CheckNotNullLength<KdcRepContainer>() );
185
186        // --------------------------------------------------------------------------------------------
187        // Transition from msg-type value to crealm tag (pa-data is empty)
188        // --------------------------------------------------------------------------------------------
189        // KDC-REP         ::= SEQUENCE {
190        //         ...
191        //         crealm          [3]
192        super.transitions[KdcRepStatesEnum.KDC_REP_MSG_TYPE_STATE.ordinal()][KerberosConstants.KDC_REP_CREALM_TAG] =
193            new GrammarTransition<KdcRepContainer>(
194                KdcRepStatesEnum.KDC_REP_MSG_TYPE_STATE,
195                KdcRepStatesEnum.KDC_REP_CREALM_TAG_STATE,
196                KerberosConstants.KDC_REP_CREALM_TAG,
197                new CheckNotNullLength<KdcRepContainer>() );
198
199        // --------------------------------------------------------------------------------------------
200        // Transition from crealm tag to crealm value
201        // --------------------------------------------------------------------------------------------
202        // KDC-REP         ::= SEQUENCE {
203        //         ...
204        //         crealm          [3] Realm,
205        super.transitions[KdcRepStatesEnum.KDC_REP_CREALM_TAG_STATE.ordinal()][UniversalTag.GENERAL_STRING.getValue()] =
206            new GrammarTransition<KdcRepContainer>(
207                KdcRepStatesEnum.KDC_REP_CREALM_TAG_STATE,
208                KdcRepStatesEnum.KDC_REP_CREALM_STATE,
209                UniversalTag.GENERAL_STRING,
210                new StoreCRealm() );
211
212        // --------------------------------------------------------------------------------------------
213        // Transition from crealm value to cname
214        // --------------------------------------------------------------------------------------------
215        // KDC-REP         ::= SEQUENCE {
216        //         ...
217        //         cname           [4] PrincipalName,
218        super.transitions[KdcRepStatesEnum.KDC_REP_CREALM_STATE.ordinal()][KerberosConstants.KDC_REP_CNAME_TAG] =
219            new GrammarTransition<KdcRepContainer>(
220                KdcRepStatesEnum.KDC_REP_CREALM_STATE,
221                KdcRepStatesEnum.KDC_REP_CNAME_STATE,
222                KerberosConstants.KDC_REP_CNAME_TAG,
223                new StoreCName() );
224
225        // --------------------------------------------------------------------------------------------
226        // Transition from cname to ticket
227        // --------------------------------------------------------------------------------------------
228        // KDC-REP         ::= SEQUENCE {
229        //         ...
230        //         ticket          [5] Ticket,
231        super.transitions[KdcRepStatesEnum.KDC_REP_CNAME_STATE.ordinal()][KerberosConstants.KDC_REP_TICKET_TAG] =
232            new GrammarTransition<KdcRepContainer>(
233                KdcRepStatesEnum.KDC_REP_CNAME_STATE,
234                KdcRepStatesEnum.KDC_REP_TICKET_STATE,
235                KerberosConstants.KDC_REP_TICKET_TAG,
236                new StoreTicket() );
237
238        // --------------------------------------------------------------------------------------------
239        // Transition from ticket to enc-part
240        // --------------------------------------------------------------------------------------------
241        // KDC-REP         ::= SEQUENCE {
242        //         ...
243        //         enc-part        [6] EncryptedData
244        super.transitions[KdcRepStatesEnum.KDC_REP_TICKET_STATE.ordinal()][KerberosConstants.KDC_REP_ENC_PART_TAG] =
245            new GrammarTransition<KdcRepContainer>(
246                KdcRepStatesEnum.KDC_REP_TICKET_STATE,
247                KdcRepStatesEnum.KDC_REP_ENC_PART_STATE,
248                KerberosConstants.KDC_REP_ENC_PART_TAG,
249                new StoreEncPart() );
250    }
251
252
253    /**
254     * Get the instance of this grammar
255     *
256     * @return An instance on the KDC-REQ Grammar
257     */
258    public static Grammar<KdcRepContainer> getInstance()
259    {
260        return instance;
261    }
262}