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.encryptedData;
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.encryptedData.actions.EncryptedDataInit;
030import org.apache.directory.shared.kerberos.codec.encryptedData.actions.StoreCipher;
031import org.apache.directory.shared.kerberos.codec.encryptedData.actions.StoreEType;
032import org.apache.directory.shared.kerberos.codec.encryptedData.actions.StoreKvno;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036
037/**
038 * This class implements the EncryptedData 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 EncryptedDataGrammar extends AbstractGrammar<EncryptedDataContainer>
045{
046    /** The logger */
047    static final Logger LOG = LoggerFactory.getLogger( EncryptedDataGrammar.class );
048
049    /** A speedup for logger */
050    static final boolean IS_DEBUG = LOG.isDebugEnabled();
051
052    /** The instance of grammar. EncryptedDataGrammar is a singleton */
053    private static Grammar<EncryptedDataContainer> instance = new EncryptedDataGrammar();
054
055
056    /**
057     * Creates a new EncryptedDataGrammar object.
058     */
059    @SuppressWarnings("unchecked")
060    private EncryptedDataGrammar()
061    {
062        setName( EncryptedDataGrammar.class.getName() );
063
064        // Create the transitions table
065        super.transitions = new GrammarTransition[EncryptedDataStatesEnum.LAST_ENCRYPTED_DATA_STATE.ordinal()][256];
066
067        // ============================================================================================
068        // EncryptedData
069        // ============================================================================================
070        // --------------------------------------------------------------------------------------------
071        // Transition from EncryptedData init to EncryptedData SEQ
072        // --------------------------------------------------------------------------------------------
073        // EncryptedData   ::= SEQUENCE
074        super.transitions[EncryptedDataStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
075            new GrammarTransition<EncryptedDataContainer>(
076                EncryptedDataStatesEnum.START_STATE,
077                EncryptedDataStatesEnum.ENCRYPTED_DATA_SEQ_STATE,
078                UniversalTag.SEQUENCE,
079                new EncryptedDataInit() );
080
081        // --------------------------------------------------------------------------------------------
082        // Transition from EncryptedData SEQ to etype tag
083        // --------------------------------------------------------------------------------------------
084        // EncryptedData   ::= SEQUENCE {
085        //         etype       [0]
086        super.transitions[EncryptedDataStatesEnum.ENCRYPTED_DATA_SEQ_STATE.ordinal()][KerberosConstants.ENCRYPTED_DATA_ETYPE_TAG] =
087            new GrammarTransition<EncryptedDataContainer>(
088                EncryptedDataStatesEnum.ENCRYPTED_DATA_SEQ_STATE,
089                EncryptedDataStatesEnum.ENCRYPTED_DATA_ETYPE_TAG_STATE,
090                KerberosConstants.ENCRYPTED_DATA_ETYPE_TAG,
091                new CheckNotNullLength<EncryptedDataContainer>() );
092
093        // --------------------------------------------------------------------------------------------
094        // Transition from etype tag to etype value
095        // --------------------------------------------------------------------------------------------
096        // EncryptedData   ::= SEQUENCE {
097        //         etype       [0] Int32,
098        super.transitions[EncryptedDataStatesEnum.ENCRYPTED_DATA_ETYPE_TAG_STATE.ordinal()][UniversalTag.INTEGER
099            .getValue()] =
100            new GrammarTransition<EncryptedDataContainer>(
101                EncryptedDataStatesEnum.ENCRYPTED_DATA_ETYPE_TAG_STATE,
102                EncryptedDataStatesEnum.ENCRYPTED_DATA_ETYPE_STATE,
103                UniversalTag.INTEGER,
104                new StoreEType() );
105
106        // --------------------------------------------------------------------------------------------
107        // Transition from etype value to kvno tag
108        // --------------------------------------------------------------------------------------------
109        // EncryptedData   ::= SEQUENCE {
110        //         ...
111        //         kvno     [1]
112        super.transitions[EncryptedDataStatesEnum.ENCRYPTED_DATA_ETYPE_STATE.ordinal()][KerberosConstants.ENCRYPTED_DATA_KVNO_TAG] =
113            new GrammarTransition<EncryptedDataContainer>(
114                EncryptedDataStatesEnum.ENCRYPTED_DATA_ETYPE_STATE,
115                EncryptedDataStatesEnum.ENCRYPTED_DATA_KVNO_TAG_STATE,
116                KerberosConstants.ENCRYPTED_DATA_KVNO_TAG,
117                new CheckNotNullLength<EncryptedDataContainer>() );
118
119        // --------------------------------------------------------------------------------------------
120        // Transition from etype value to cipher tag (kvno is missing)
121        // --------------------------------------------------------------------------------------------
122        // EncryptedData   ::= SEQUENCE {
123        //         ...
124        //         cipher     [2]
125        super.transitions[EncryptedDataStatesEnum.ENCRYPTED_DATA_ETYPE_STATE.ordinal()][KerberosConstants.ENCRYPTED_DATA_CIPHER_TAG] =
126            new GrammarTransition<EncryptedDataContainer>(
127                EncryptedDataStatesEnum.ENCRYPTED_DATA_ETYPE_STATE,
128                EncryptedDataStatesEnum.ENCRYPTED_DATA_CIPHER_TAG_STATE,
129                KerberosConstants.ENCRYPTED_DATA_CIPHER_TAG,
130                new CheckNotNullLength<EncryptedDataContainer>() );
131
132        // --------------------------------------------------------------------------------------------
133        // Transition from kvno tag to kvno value
134        // --------------------------------------------------------------------------------------------
135        // EncryptedData   ::= SEQUENCE {
136        //         ...
137        //         kvno     [1] UInt32
138        super.transitions[EncryptedDataStatesEnum.ENCRYPTED_DATA_KVNO_TAG_STATE.ordinal()][UniversalTag.INTEGER
139            .getValue()] =
140            new GrammarTransition<EncryptedDataContainer>(
141                EncryptedDataStatesEnum.ENCRYPTED_DATA_KVNO_TAG_STATE,
142                EncryptedDataStatesEnum.ENCRYPTED_DATA_KVNO_STATE,
143                UniversalTag.INTEGER,
144                new StoreKvno() );
145
146        // --------------------------------------------------------------------------------------------
147        // Transition from kvno value value to cipher tag
148        // --------------------------------------------------------------------------------------------
149        // EncryptedData   ::= SEQUENCE {
150        //         ...
151        //         cipher     [2]
152        super.transitions[EncryptedDataStatesEnum.ENCRYPTED_DATA_KVNO_STATE.ordinal()][KerberosConstants.ENCRYPTED_DATA_CIPHER_TAG] =
153            new GrammarTransition<EncryptedDataContainer>(
154                EncryptedDataStatesEnum.ENCRYPTED_DATA_KVNO_STATE,
155                EncryptedDataStatesEnum.ENCRYPTED_DATA_CIPHER_TAG_STATE,
156                KerberosConstants.ENCRYPTED_DATA_CIPHER_TAG,
157                new CheckNotNullLength<EncryptedDataContainer>() );
158
159        // --------------------------------------------------------------------------------------------
160        // Transition from cipher tag to cipher value
161        // --------------------------------------------------------------------------------------------
162        // EncryptedData   ::= SEQUENCE {
163        //         ...
164        //         cipher     [2] OCTET STRING
165        super.transitions[EncryptedDataStatesEnum.ENCRYPTED_DATA_CIPHER_TAG_STATE.ordinal()][UniversalTag.OCTET_STRING
166            .getValue()] =
167            new GrammarTransition<EncryptedDataContainer>(
168                EncryptedDataStatesEnum.ENCRYPTED_DATA_CIPHER_TAG_STATE,
169                EncryptedDataStatesEnum.ENCRYPTED_DATA_CIPHER_STATE,
170                UniversalTag.OCTET_STRING,
171                new StoreCipher() );
172    }
173
174
175    /**
176     * Get the instance of this grammar
177     *
178     * @return An instance on the EncryptedData Grammar
179     */
180    public static Grammar<EncryptedDataContainer> getInstance()
181    {
182        return instance;
183    }
184}