You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2010/11/12 20:04:29 UTC

svn commit: r1034521 - in /directory/apacheds/trunk/kerberos-codec/src: main/java/org/apache/directory/shared/kerberos/codec/checksum/ main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ test/java/org/apache/directory/shared/kerberos...

Author: kayyagari
Date: Fri Nov 12 19:04:29 2010
New Revision: 1034521

URL: http://svn.apache.org/viewvc?rev=1034521&view=rev
Log:
o implementation of Checksum codec and test cases

Added:
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumContainer.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumGrammar.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumStatesEnum.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumCksumType.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumInit.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumValue.java
    directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/ChecksumDecoderTest.java

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumContainer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumContainer.java?rev=1034521&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumContainer.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumContainer.java Fri Nov 12 19:04:29 2010
@@ -0,0 +1,66 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.kerberos.codec.checksum;
+
+import org.apache.directory.shared.asn1.ber.AbstractContainer;
+import org.apache.directory.shared.kerberos.components.Checksum;
+
+
+/**
+ * The Checksum container stores the Checksum decoded by the Asn1Decoder.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ChecksumContainer extends AbstractContainer
+{
+    /** holds Checksum */
+    private Checksum checksum;
+
+    /**
+     * Creates a new ChecksumContainer object.
+     */
+    public ChecksumContainer()
+    {
+        super();
+        this.stateStack = new int[1];
+        this.grammar = ChecksumGrammar.getInstance();
+        setTransition( ChecksumStatesEnum.START_STATE );
+    }
+
+
+    /**
+     * @return Returns the Checksum.
+     */
+    public Checksum getChecksum()
+    {
+        return checksum;
+    }
+
+    
+    /**
+     * Set a Checksum Object into the container
+     * 
+     * @param checksum The Checksum to set.
+     */
+    public void setChecksum( Checksum checksum )
+    {
+        this.checksum = checksum;
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumGrammar.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumGrammar.java?rev=1034521&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumGrammar.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumGrammar.java Fri Nov 12 19:04:29 2010
@@ -0,0 +1,126 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.kerberos.codec.checksum;
+
+
+import org.apache.directory.shared.asn1.ber.grammar.AbstractGrammar;
+import org.apache.directory.shared.asn1.ber.grammar.Grammar;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarTransition;
+import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.shared.kerberos.KerberosConstants;
+import org.apache.directory.shared.kerberos.codec.actions.CheckNotNullLength;
+import org.apache.directory.shared.kerberos.codec.checksum.actions.ChecksumCksumType;
+import org.apache.directory.shared.kerberos.codec.checksum.actions.ChecksumInit;
+import org.apache.directory.shared.kerberos.codec.checksum.actions.ChecksumValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This class implements the Checksum structure. All the actions are declared
+ * in this class. As it is a singleton, these declaration are only done once. If
+ * an action is to be added or modified, this is where the work is to be done !
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public final class ChecksumGrammar extends AbstractGrammar
+{
+    /** The logger */
+    static final Logger LOG = LoggerFactory.getLogger( ChecksumGrammar.class );
+
+    /** A speedup for logger */
+    static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+    /** The instance of grammar. ChecksumGrammar is a singleton */
+    private static Grammar instance = new ChecksumGrammar();
+
+
+    /**
+     * Creates a new ChecksumGrammar object.
+     */
+    private ChecksumGrammar()
+    {
+        setName( ChecksumGrammar.class.getName() );
+
+        // Create the transitions table
+        super.transitions = new GrammarTransition[ChecksumStatesEnum.LAST_CHECKSUM_STATE.ordinal()][256];
+
+        // ============================================================================================
+        // Checksum 
+        // ============================================================================================
+        // --------------------------------------------------------------------------------------------
+        // Transition from Checksum init to Checksum SEQ OF
+        // --------------------------------------------------------------------------------------------
+        // Checksum   ::= SEQUENCE OF
+        super.transitions[ChecksumStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] = new GrammarTransition(
+            ChecksumStatesEnum.START_STATE, ChecksumStatesEnum.CHECKSUM_SEQ_STATE, UniversalTag.SEQUENCE.getValue(),
+            new ChecksumInit() );
+        
+        // --------------------------------------------------------------------------------------------
+        // Transition from Checksum SEQ to checksumtype
+        // --------------------------------------------------------------------------------------------
+        // Checksum      ::= SEQUENCE {
+        //        cksumtype       [0]
+        super.transitions[ChecksumStatesEnum.CHECKSUM_SEQ_STATE.ordinal()][KerberosConstants.CHECKSUM_TYPE_TAG] = new GrammarTransition(
+            ChecksumStatesEnum.CHECKSUM_SEQ_STATE, ChecksumStatesEnum.CHECKSUM_TYPE_TAG_STATE, KerberosConstants.CHECKSUM_TYPE_TAG,
+            new CheckNotNullLength() );
+        
+        // --------------------------------------------------------------------------------------------
+        // Transition from checksumtype tag to checksumtype
+        // --------------------------------------------------------------------------------------------
+        // Checksum      ::= SEQUENCE {
+        //          cksumtype       [0] Int32
+        super.transitions[ChecksumStatesEnum.CHECKSUM_TYPE_TAG_STATE.ordinal()][UniversalTag.INTEGER.getValue()] = new GrammarTransition(
+            ChecksumStatesEnum.CHECKSUM_TYPE_TAG_STATE, ChecksumStatesEnum.CHECKSUM_TYPE_STATE, UniversalTag.INTEGER.getValue(),
+            new ChecksumCksumType() );
+        
+        // --------------------------------------------------------------------------------------------
+        // Transition from checksumtype to checksum tag
+        // --------------------------------------------------------------------------------------------
+        // Checksum      ::= SEQUENCE {
+        //          checksum        [1]
+        super.transitions[ChecksumStatesEnum.CHECKSUM_TYPE_STATE.ordinal()][KerberosConstants.CHECKSUM_CHECKSUM_TAG] = new GrammarTransition(
+            ChecksumStatesEnum.CHECKSUM_TYPE_STATE, ChecksumStatesEnum.CHECKSUM_CHECKSUM_TAG_STATE, KerberosConstants.CHECKSUM_CHECKSUM_TAG,
+            new CheckNotNullLength() );
+        
+        // --------------------------------------------------------------------------------------------
+        // Transition from checksum tag to checksum value
+        // --------------------------------------------------------------------------------------------
+        // Checksum      ::= SEQUENCE {
+        //          checksum        [1] OCTET STRING
+        super.transitions[ChecksumStatesEnum.CHECKSUM_CHECKSUM_TAG_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] = new GrammarTransition(
+            ChecksumStatesEnum.CHECKSUM_CHECKSUM_TAG_STATE, ChecksumStatesEnum.CHECKSUM_CHECKSUM_STATE, UniversalTag.OCTET_STRING.getValue(),
+            new ChecksumValue() );
+    }
+
+
+    // ~ Methods
+    // ------------------------------------------------------------------------------------
+
+    /**
+     * Get the instance of this grammar
+     * 
+     * @return An instance on the AuthorizationData Grammar
+     */
+    public static Grammar getInstance()
+    {
+        return instance;
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumStatesEnum.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumStatesEnum.java?rev=1034521&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumStatesEnum.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/ChecksumStatesEnum.java Fri Nov 12 19:04:29 2010
@@ -0,0 +1,112 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.kerberos.codec.checksum;
+
+
+import org.apache.directory.shared.asn1.ber.grammar.Grammar;
+import org.apache.directory.shared.asn1.ber.grammar.States;
+import org.apache.directory.shared.kerberos.codec.KerberosMessageGrammar;
+
+
+/**
+ * This class store the Checksum grammar's constants. It is also used for debugging
+ * purpose
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public enum ChecksumStatesEnum implements States
+{
+    // Start
+    START_STATE,                  // 0
+    
+    CHECKSUM_SEQ_STATE,           // 1
+    
+    CHECKSUM_TYPE_TAG_STATE,      // 2
+    
+    CHECKSUM_TYPE_STATE,          // 3
+    
+    CHECKSUM_CHECKSUM_TAG_STATE,  // 4
+    
+    CHECKSUM_CHECKSUM_STATE,      // 5
+    
+    // End
+    LAST_CHECKSUM_STATE; // 6
+
+    
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar code
+     * @return The grammar name
+     */
+    public String getGrammarName( int grammar )
+    {
+        return "CHECKSUM_GRAMMAR";
+    }
+
+
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar class
+     * @return The grammar name
+     */
+    public String getGrammarName( Grammar grammar )
+    {
+        if ( grammar instanceof KerberosMessageGrammar )
+        {
+            return "CHECKSUM_GRAMMAR";
+        }
+        else
+        {
+            return "UNKNOWN GRAMMAR";
+        }
+    }
+
+
+    /**
+     * Get the string representing the state
+     * 
+     * @param state The state number
+     * @return The String representing the state
+     */
+    public String getState( int state )
+    {
+        return ( ( state == LAST_CHECKSUM_STATE.ordinal() ) ? "LAST_CHECKSUM_STATE" : name() );
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isEndState()
+    {
+        return this == LAST_CHECKSUM_STATE;
+    }
+    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public ChecksumStatesEnum getStartState()
+    {
+        return START_STATE;
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumCksumType.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumCksumType.java?rev=1034521&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumCksumType.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumCksumType.java Fri Nov 12 19:04:29 2010
@@ -0,0 +1,106 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+
+package org.apache.directory.shared.kerberos.codec.checksum.actions;
+
+
+import org.apache.directory.shared.asn1.ber.Asn1Container;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.asn1.util.IntegerDecoder;
+import org.apache.directory.shared.asn1.util.IntegerDecoderException;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.kerberos.codec.checksum.ChecksumContainer;
+import org.apache.directory.shared.kerberos.components.Checksum;
+import org.apache.directory.shared.kerberos.crypto.checksum.ChecksumType;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Sets the Checksum's type.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ChecksumCksumType extends GrammarAction
+{
+    /** The logger */
+    private static final Logger LOG = LoggerFactory.getLogger( ChecksumInit.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+    /**
+     * Creates a new instance of ChecksumReadType.
+     */
+    public ChecksumCksumType()
+    {
+        super( "Checksum cksumtype" );
+    }
+    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public void action( Asn1Container container ) throws DecoderException
+    {
+        ChecksumContainer checksumContainer = ( ChecksumContainer ) container;
+
+        TLV tlv = checksumContainer.getCurrentTLV();
+
+        // The Length should not be null
+        if ( tlv.getLength() == 0 )
+        {
+            LOG.error( I18n.err( I18n.ERR_04066 ) );
+
+            // This will generate a PROTOCOL_ERROR
+            throw new DecoderException( I18n.err( I18n.ERR_04067 ) );
+        }
+        
+        Checksum checksum = checksumContainer.getChecksum();
+        // The Checksum's type is an integer
+        Value value = tlv.getValue();
+        
+        try
+        {
+            int cksumType = IntegerDecoder.parse( value );
+
+            checksum.setChecksumType( ChecksumType.getTypeByValue( cksumType ) );
+
+            if ( IS_DEBUG )
+            {
+                LOG.debug( "cksumType : " + cksumType );
+            }
+        }
+        catch ( IntegerDecoderException ide )
+        {
+            LOG.error( I18n.err( I18n.ERR_04070, StringTools.dumpBytes( value.getData() ), ide
+                .getLocalizedMessage() ) );
+
+            // This will generate a PROTOCOL_ERROR
+            throw new DecoderException( ide.getMessage() );
+        }
+
+    }
+
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumInit.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumInit.java?rev=1034521&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumInit.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumInit.java Fri Nov 12 19:04:29 2010
@@ -0,0 +1,83 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.kerberos.codec.checksum.actions;
+
+
+import org.apache.directory.shared.asn1.ber.Asn1Container;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.kerberos.codec.checksum.ChecksumContainer;
+import org.apache.directory.shared.kerberos.components.Checksum;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The action used to initialize the Checksum object
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ChecksumInit extends GrammarAction
+{
+    /** The logger */
+    private static final Logger LOG = LoggerFactory.getLogger( ChecksumInit.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+
+    /**
+     * Instantiates a new ChecksumInit action.
+     */
+    public ChecksumInit()
+    {
+        super( "Creates a Checksum instance" );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void action( Asn1Container container ) throws DecoderException
+    {
+        ChecksumContainer checksumContainer = ( ChecksumContainer ) container;
+
+        TLV tlv = checksumContainer.getCurrentTLV();
+
+        // The Length should not be null
+        if ( tlv.getLength() == 0 )
+        {
+            LOG.error( I18n.err( I18n.ERR_04066 ) );
+
+            // This will generate a PROTOCOL_ERROR
+            throw new DecoderException( I18n.err( I18n.ERR_04067 ) );
+        }
+        
+        Checksum checksum = new Checksum();
+        checksumContainer.setChecksum( checksum );
+        
+        if ( IS_DEBUG )
+        {
+            LOG.debug( "Checksum created" );
+        }
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumValue.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumValue.java?rev=1034521&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumValue.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/checksum/actions/ChecksumValue.java Fri Nov 12 19:04:29 2010
@@ -0,0 +1,98 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.kerberos.codec.checksum.actions;
+
+
+import org.apache.directory.shared.asn1.ber.Asn1Container;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.kerberos.codec.checksum.ChecksumContainer;
+import org.apache.directory.shared.kerberos.components.Checksum;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The action used to store the Checksum's 'checksum' value
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ChecksumValue extends GrammarAction
+{
+    /** The logger */
+    private static final Logger LOG = LoggerFactory.getLogger( ChecksumValue.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+
+    /**
+     * Instantiates a new ChecksumData action.
+     */
+    public ChecksumValue()
+    {
+        super( "Checksum's 'checksum' value" );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void action( Asn1Container container ) throws DecoderException
+    {
+        ChecksumContainer checksumContainer = ( ChecksumContainer ) container;
+
+        TLV tlv = checksumContainer.getCurrentTLV();
+
+        // The Length should not be null
+        if ( tlv.getLength() == 0 ) 
+        {
+            LOG.error( I18n.err( I18n.ERR_04066 ) );
+
+            // This will generate a PROTOCOL_ERROR
+            throw new DecoderException( I18n.err( I18n.ERR_04067 ) );
+        }
+        
+        Value value = tlv.getValue();
+        
+        // The encrypted data should not be null
+        if ( value.getData() == null ) 
+        {
+            LOG.error( I18n.err( I18n.ERR_04066 ) );
+
+            // This will generate a PROTOCOL_ERROR
+            throw new DecoderException( I18n.err( I18n.ERR_04067 ) );
+        }
+        
+        Checksum checksum = checksumContainer.getChecksum();
+        checksum.setChecksumValue( value.getData() );
+        
+        if ( IS_DEBUG )
+        {
+            LOG.debug( "checksum value : {}", StringTools.dumpBytes( value.getData() ) );
+        }
+        
+        checksumContainer.setGrammarEndAllowed( true );
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/ChecksumDecoderTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/ChecksumDecoderTest.java?rev=1034521&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/ChecksumDecoderTest.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/ChecksumDecoderTest.java Fri Nov 12 19:04:29 2010
@@ -0,0 +1,167 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+
+package org.apache.directory.shared.kerberos.codec;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import org.apache.directory.shared.asn1.ber.Asn1Decoder;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.asn1.codec.EncoderException;
+import org.apache.directory.shared.kerberos.codec.checksum.ChecksumContainer;
+import org.apache.directory.shared.kerberos.components.Checksum;
+import org.apache.directory.shared.kerberos.crypto.checksum.ChecksumType;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.junit.Test;
+
+/**
+ * Test cases for Checksum codec.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ChecksumDecoderTest
+{
+    @Test
+    public void testDecodeChecksum()
+    {
+        Asn1Decoder krbDecoder = new Asn1Decoder();
+        
+        ByteBuffer stream = ByteBuffer.allocate( 0x11 );
+
+        stream.put( new byte[]
+            { 
+                0x30, 0x0F,
+                  (byte)0xA0, 0x03,                 // cksumtype
+                    0x02, 0x01, 0x02,
+                  (byte)0xA1, 0x08,                 // checksum
+                    0x04, 0x06, 'c', 'h', 'k', 's', 'u', 'm'
+            } );
+        
+        String decodedPdu = StringTools.dumpBytes( stream.array() );
+        stream.flip();
+
+        ChecksumContainer chkContainer = new ChecksumContainer();
+        
+        try
+        {
+            krbDecoder.decode( stream, chkContainer );
+        }
+        catch ( DecoderException de )
+        {
+            fail( de.getMessage() );
+        }
+
+        Checksum checksum = chkContainer.getChecksum();
+        
+        assertEquals( ChecksumType.getTypeByValue( 2 ), checksum.getChecksumType() );
+        assertTrue( Arrays.equals( StringTools.getBytesUtf8( "chksum" ), checksum.getChecksumValue() ) );
+        
+        ByteBuffer bb = ByteBuffer.allocate( checksum.computeLength() );
+        
+        try
+        {
+            bb = checksum.encode( bb );
+    
+            // Check the length
+            assertEquals( 0x11, bb.limit() );
+    
+            String encodedPdu = StringTools.dumpBytes( bb.array() );
+    
+            assertEquals( encodedPdu, decodedPdu );
+        }
+        catch ( EncoderException ee )
+        {
+            fail();
+        }
+    }
+    
+    
+    @Test( expected = DecoderException.class )
+    public void testDecodeChecksumWithoutType() throws DecoderException
+    {
+        Asn1Decoder krbDecoder = new Asn1Decoder();
+        
+        ByteBuffer stream = ByteBuffer.allocate( 0xC );
+
+        stream.put( new byte[]
+            { 
+                0x30, 0xA,
+                  (byte)0xA1, 0x08,                 // checksum
+                    0x04, 0x06, 'c', 'h', 'k', 's', 'u', 'm'
+            } );
+        
+        stream.flip();
+
+        ChecksumContainer chkContainer = new ChecksumContainer();
+        
+        krbDecoder.decode( stream, chkContainer );
+        fail();
+    }
+    
+    
+    @Test( expected = DecoderException.class )
+    public void testDecodeChecksumWithoutChecksumValue() throws DecoderException
+    {
+        Asn1Decoder krbDecoder = new Asn1Decoder();
+        
+        ByteBuffer stream = ByteBuffer.allocate( 0x07 );
+
+        stream.put( new byte[]
+            { 
+                0x30, 0x05,
+                  (byte)0xA0, 0x03,                 // cksumtype
+                    0x02, 0x01, 0x02
+            } );
+        
+        stream.flip();
+
+        ChecksumContainer chkContainer = new ChecksumContainer();
+        
+        krbDecoder.decode( stream, chkContainer );
+        fail();
+    }
+    
+    
+    @Test( expected = DecoderException.class )
+    public void testDecodeChecksumWithEmptySeq() throws DecoderException
+    {
+        Asn1Decoder krbDecoder = new Asn1Decoder();
+        
+        ByteBuffer stream = ByteBuffer.allocate( 2 );
+
+        stream.put( new byte[]
+            { 
+                0x30, 0x0
+            } );
+        
+        stream.flip();
+
+        ChecksumContainer chkContainer = new ChecksumContainer();
+        
+        krbDecoder.decode( stream, chkContainer );
+        fail();
+    }
+
+}