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.checksum;
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.checksum.actions.ChecksumInit;
030import org.apache.directory.shared.kerberos.codec.checksum.actions.StoreChecksum;
031import org.apache.directory.shared.kerberos.codec.checksum.actions.StoreCksumType;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035
036/**
037 * This class implements the Checksum structure. All the actions are declared
038 * in this class. As it is a singleton, these declaration are only done once. If
039 * an action is to be added or modified, this is where the work is to be done !
040 *
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public final class ChecksumGrammar extends AbstractGrammar<ChecksumContainer>
044{
045    /** The logger */
046    static final Logger LOG = LoggerFactory.getLogger( ChecksumGrammar.class );
047
048    /** A speedup for logger */
049    static final boolean IS_DEBUG = LOG.isDebugEnabled();
050
051    /** The instance of grammar. ChecksumGrammar is a singleton */
052    private static Grammar<ChecksumContainer> instance = new ChecksumGrammar();
053
054
055    /**
056     * Creates a new ChecksumGrammar object.
057     */
058    @SuppressWarnings("unchecked")
059    private ChecksumGrammar()
060    {
061        setName( ChecksumGrammar.class.getName() );
062
063        // Create the transitions table
064        super.transitions = new GrammarTransition[ChecksumStatesEnum.LAST_CHECKSUM_STATE.ordinal()][256];
065
066        // ============================================================================================
067        // Checksum
068        // ============================================================================================
069        // --------------------------------------------------------------------------------------------
070        // Transition from Checksum init to Checksum SEQ OF
071        // --------------------------------------------------------------------------------------------
072        // Checksum   ::= SEQUENCE OF
073        super.transitions[ChecksumStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
074            new GrammarTransition<ChecksumContainer>(
075                ChecksumStatesEnum.START_STATE,
076                ChecksumStatesEnum.CHECKSUM_SEQ_STATE,
077                UniversalTag.SEQUENCE,
078                new ChecksumInit() );
079
080        // --------------------------------------------------------------------------------------------
081        // Transition from Checksum SEQ to checksumtype
082        // --------------------------------------------------------------------------------------------
083        // Checksum      ::= SEQUENCE {
084        //        cksumtype       [0]
085        super.transitions[ChecksumStatesEnum.CHECKSUM_SEQ_STATE.ordinal()][KerberosConstants.CHECKSUM_TYPE_TAG] =
086            new GrammarTransition<ChecksumContainer>(
087                ChecksumStatesEnum.CHECKSUM_SEQ_STATE,
088                ChecksumStatesEnum.CHECKSUM_TYPE_TAG_STATE,
089                KerberosConstants.CHECKSUM_TYPE_TAG,
090                new CheckNotNullLength<ChecksumContainer>() );
091
092        // --------------------------------------------------------------------------------------------
093        // Transition from checksumtype tag to checksumtype
094        // --------------------------------------------------------------------------------------------
095        // Checksum      ::= SEQUENCE {
096        //          cksumtype       [0] Int32
097        super.transitions[ChecksumStatesEnum.CHECKSUM_TYPE_TAG_STATE.ordinal()][UniversalTag.INTEGER.getValue()] =
098            new GrammarTransition<ChecksumContainer>(
099                ChecksumStatesEnum.CHECKSUM_TYPE_TAG_STATE,
100                ChecksumStatesEnum.CHECKSUM_TYPE_STATE,
101                UniversalTag.INTEGER,
102                new StoreCksumType() );
103
104        // --------------------------------------------------------------------------------------------
105        // Transition from checksumtype to checksum tag
106        // --------------------------------------------------------------------------------------------
107        // Checksum      ::= SEQUENCE {
108        //          checksum        [1]
109        super.transitions[ChecksumStatesEnum.CHECKSUM_TYPE_STATE.ordinal()][KerberosConstants.CHECKSUM_CHECKSUM_TAG] =
110            new GrammarTransition<ChecksumContainer>(
111                ChecksumStatesEnum.CHECKSUM_TYPE_STATE,
112                ChecksumStatesEnum.CHECKSUM_CHECKSUM_TAG_STATE,
113                KerberosConstants.CHECKSUM_CHECKSUM_TAG,
114                new CheckNotNullLength<ChecksumContainer>() );
115
116        // --------------------------------------------------------------------------------------------
117        // Transition from checksum tag to checksum value
118        // --------------------------------------------------------------------------------------------
119        // Checksum      ::= SEQUENCE {
120        //          checksum        [1] OCTET STRING
121        super.transitions[ChecksumStatesEnum.CHECKSUM_CHECKSUM_TAG_STATE.ordinal()][UniversalTag.OCTET_STRING
122            .getValue()] =
123            new GrammarTransition<ChecksumContainer>(
124                ChecksumStatesEnum.CHECKSUM_CHECKSUM_TAG_STATE,
125                ChecksumStatesEnum.CHECKSUM_CHECKSUM_STATE,
126                UniversalTag.OCTET_STRING,
127                new StoreChecksum() );
128    }
129
130
131    /**
132     * Get the instance of this grammar
133     *
134     * @return An instance on the Checksum Grammar
135     */
136    public static Grammar<ChecksumContainer> getInstance()
137    {
138        return instance;
139    }
140}