directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r588239 - in /directory/apacheds/branches/bigbang/kerberos-shared/src: main/java/org/apache/directory/server/kerberos/shared/crypto/checksum/ main/java/org/apache/directory/server/kerberos/shared/io/decoder/ main/java/org/apache/directory/s...
Date Thu, 25 Oct 2007 14:24:29 GMT
Author: elecharny
Date: Thu Oct 25 07:24:28 2007
New Revision: 588239

URL: http://svn.apache.org/viewvc?rev=588239&view=rev
Log:
Updated the Checksum class
Moved the ChecksumType from a class to an enum
Added a test class for Checksum

Added:
    directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/ChecksumTest.java
Modified:
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/crypto/checksum/ChecksumType.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/ChecksumDecoder.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/Checksum.java

Modified: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/crypto/checksum/ChecksumType.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/crypto/checksum/ChecksumType.java?rev=588239&r1=588238&r2=588239&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/crypto/checksum/ChecksumType.java
(original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/crypto/checksum/ChecksumType.java
Thu Oct 25 07:24:28 2007
@@ -20,126 +20,104 @@
 package org.apache.directory.server.kerberos.shared.crypto.checksum;
 
 
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-
 /**
  * A type-safe enumeration of Kerberos checksum types.
  * 
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public final class ChecksumType implements Comparable<ChecksumType>
+public enum ChecksumType implements Comparable<ChecksumType>
 {
     /**
      * The "unknown" checksum type.
      */
-    public static final ChecksumType UNKNOWN = new ChecksumType( -1, "UNKNOWN" );
+    UNKNOWN( -1 ),
 
     /**
      * The "null" checksum type.
      */
-    public static final ChecksumType NULL = new ChecksumType( 0, "NULL" );
+    NULL( 0 ),
 
     /**
      * The CRC32 checksum type.
      */
-    public static final ChecksumType CRC32 = new ChecksumType( 1, "CRC32" );
+    CRC32( 1 ),
 
     /**
      * The rsa-md4 checksum type.
      */
-    public static final ChecksumType RSA_MD4 = new ChecksumType( 2, "rsa-md4" );
+    RSA_MD4( 2 ),
 
     /**
      * The rsa-md4-des checksum type.
      */
-    public static final ChecksumType RSA_MD4_DES = new ChecksumType( 3, "rsa-md4-des" );
+    RSA_MD4_DES( 3 ),
 
     /**
      * The des-mac checksum type.
      */
-    public static final ChecksumType DES_MAC = new ChecksumType( 4, "des-mac" );
+    DES_MAC( 4 ),
 
     /**
      * The des-mac-k checksum type.
      */
-    public static final ChecksumType DES_MAC_K = new ChecksumType( 5, "des-mac-k" );
+    DES_MAC_K( 5 ),
 
     /**
      * The rsa-md4-des-k checksum type.
      */
-    public static final ChecksumType RSA_MD4_DES_K = new ChecksumType( 6, "rsa-md4-des-k"
);
+    RSA_MD4_DES_K( 6 ),
 
     /**
      * The rsa-md5 checksum type.
      */
-    public static final ChecksumType RSA_MD5 = new ChecksumType( 7, "rsa-md5" );
+    RSA_MD5( 7 ),
 
     /**
      * The rsa-md5-des checksum type.
      */
-    public static final ChecksumType RSA_MD5_DES = new ChecksumType( 8, "rsa-md5-des" );
+    RSA_MD5_DES( 8 ),
 
     /**
      * The rsa-md5-des3 checksum type.
      */
-    public static final ChecksumType RSA_MD5_DES3 = new ChecksumType( 9, "rsa-md5-des3" );
+    RSA_MD5_DES3( 9 ),
 
     /**
      * The sha1 (unkeyed) checksum type.
      */
-    public static final ChecksumType SHA1 = new ChecksumType( 10, "sha1 (unkeyed)" );
+    SHA1( 10 ),
 
     /**
      * The hmac-sha1-des3-kd checksum type.
      */
-    public static final ChecksumType HMAC_SHA1_DES3_KD = new ChecksumType( 12, "hmac-sha1-des3-kd"
);
+    HMAC_SHA1_DES3_KD( 12 ),
 
     /**
      * The hmac-sha1-des3 checksum type.
      */
-    public static final ChecksumType HMAC_SHA1_DES3 = new ChecksumType( 13, "hmac-sha1-des3"
);
+    HMAC_SHA1_DES3( 13 ),
 
     /**
      * The sha1 (unkeyed) checksum type.
      */
-    public static final ChecksumType SHA1_2 = new ChecksumType( 14, "sha1 (unkeyed)" );
+    SHA1_2 ( 14 ),
 
     /**
      * The hmac-sha1-96-aes128 checksum type.
      */
-    public static final ChecksumType HMAC_SHA1_96_AES128 = new ChecksumType( 15, "hmac-sha1-96-aes128"
);
+    HMAC_SHA1_96_AES128( 15 ),
 
     /**
      * The hmac-sha1-96-aes256 checksum type.
      */
-    public static final ChecksumType HMAC_SHA1_96_AES256 = new ChecksumType( 16, "hmac-sha1-96-aes256"
);
+    HMAC_SHA1_96_AES256( 16 ),
 
     /**
      * The hmac-md5 checksum type.
      */
-    public static final ChecksumType HMAC_MD5 = new ChecksumType( -138, "hmac-md5" );
-
-    /**
-     * Array for building a List of VALUES.
-     */
-    private static final ChecksumType[] values =
-        { UNKNOWN, NULL, CRC32, RSA_MD4, RSA_MD4_DES, DES_MAC, DES_MAC_K, RSA_MD4_DES_K,
RSA_MD5, RSA_MD5_DES,
-            RSA_MD5_DES3, SHA1, HMAC_SHA1_DES3_KD, HMAC_SHA1_DES3, SHA1_2, HMAC_SHA1_96_AES128,
HMAC_SHA1_96_AES256,
-            HMAC_MD5 };
+    HMAC_MD5( -138 );
 
-    /**
-     * A List of all the checksum type constants.
-     */
-    public static final List<ChecksumType> VALUES = Collections.unmodifiableList( Arrays.asList(
values ) );
-
-    /**
-     * The name of the checksum type.
-     */
-    private final String name;
 
     /**
      * The value/code for the checksum type.
@@ -150,10 +128,9 @@
     /**
      * Private constructor prevents construction outside of this class.
      */
-    private ChecksumType( int ordinal, String name )
+    private ChecksumType( int ordinal )
     {
         this.ordinal = ordinal;
-        this.name = name;
     }
 
 
@@ -165,15 +142,28 @@
      */
     public static ChecksumType getTypeByOrdinal( int type )
     {
-        for ( int ii = 0; ii < values.length; ii++ )
+        switch ( type )
         {
-            if ( values[ii].ordinal == type )
-            {
-                return values[ii];
-            }
+            case -1 : return UNKNOWN;
+            case 0  : return NULL;
+            case 1  : return CRC32;
+            case 2  : return RSA_MD4;
+            case 3  : return RSA_MD4_DES;
+            case 4  : return DES_MAC;
+            case 5  : return DES_MAC_K;
+            case 6  : return RSA_MD4_DES_K;
+            case 7  : return RSA_MD5;
+            case 8  : return RSA_MD5_DES;
+            case 9  : return RSA_MD5_DES3;
+            case 10 : return SHA1;
+            case 12 : return HMAC_SHA1_DES3_KD;
+            case 13 : return HMAC_SHA1_DES3;
+            case 14 : return SHA1_2;
+            case 15 : return HMAC_SHA1_96_AES128;
+            case 16 : return HMAC_SHA1_96_AES256;
+            case -138 : return HMAC_MD5;
+            default : return UNKNOWN;
         }
-
-        return UNKNOWN;
     }
 
 
@@ -185,17 +175,5 @@
     public int getOrdinal()
     {
         return ordinal;
-    }
-
-
-    public int compareTo( ChecksumType that )
-    {
-        return ordinal - that.ordinal;
-    }
-
-
-    public String toString()
-    {
-        return name;
     }
 }

Modified: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/ChecksumDecoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/ChecksumDecoder.java?rev=588239&r1=588238&r2=588239&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/ChecksumDecoder.java
(original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/ChecksumDecoder.java
Thu Oct 25 07:24:28 2007
@@ -50,7 +50,7 @@
         ChecksumType type = ChecksumType.NULL;
         byte[] data = null;
 
-        for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
+        for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements();
)
         {
             DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
             int tag = object.getTagNo();
@@ -62,6 +62,7 @@
                     DERInteger tag0 = ( DERInteger ) derObject;
                     type = ChecksumType.getTypeByOrdinal( tag0.intValue() );
                     break;
+                    
                 case 1:
                     DEROctetString tag1 = ( DEROctetString ) derObject;
                     data = tag1.getOctets();

Modified: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/Checksum.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/Checksum.java?rev=588239&r1=588238&r2=588239&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/Checksum.java
(original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/Checksum.java
Thu Oct 25 07:24:28 2007
@@ -20,34 +20,77 @@
 package org.apache.directory.server.kerberos.shared.messages.value;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
 import java.util.Arrays;
 
 import org.apache.directory.server.kerberos.shared.crypto.checksum.ChecksumType;
+import org.apache.directory.shared.asn1.AbstractAsn1Object;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.asn1.codec.EncoderException;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 /**
+ * The Checksum structure is used to store a checksum associated to a type.
+ * 
+ * The ASN.1 grammar is :
+ * Checksum        ::= SEQUENCE {
+ *       cksumtype       [0] Int32,
+ *       checksum        [1] OCTET STRING
+ * }
+ * 
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class Checksum
+public class Checksum extends AbstractAsn1Object
 {
-    private ChecksumType checksumType;
+    /** The logger */
+    private static final Logger log = LoggerFactory.getLogger( Checksum.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = log.isDebugEnabled();
+
+    /** The checksum type used */
+    private ChecksumType cksumtype;
+
+    /** The byte array containing the checksum */
     private byte[] checksum;
 
+    // Storage for computed lengths
+    private transient int checksumTypeLength;
+    private transient int checksumBytesLength;
+    private transient int checksumLength;
+
+
+    /**
+     * Creates a new instance of Checksum.
+     */
+    public Checksum()
+    {
+    }
 
+    
     /**
      * Creates a new instance of Checksum.
      *
-     * @param checksumType
-     * @param checksum
+     * @param checksumType The checksum type used
+     * @param checksum The checksum value
      */
-    public Checksum( ChecksumType checksumType, byte[] checksum )
+    public Checksum( ChecksumType cksumtype, byte[] checksum )
     {
-        this.checksumType = checksumType;
+        this.cksumtype = cksumtype;
         this.checksum = checksum;
     }
 
 
+    /**
+     * @see Object#equals(Object)
+     */
     public boolean equals( Object o )
     {
         if ( this == o )
@@ -55,14 +98,14 @@
             return true;
         }
 
-        if ( !( o instanceof Checksum ) )
+        if ( ( o != null ) || !( o instanceof Checksum ) )
         {
             return false;
         }
 
         Checksum that = ( Checksum ) o;
 
-        return ( this.checksumType == that.checksumType ) && ( Arrays.equals( this.checksum,
that.checksum ) );
+        return ( cksumtype == that.cksumtype ) && ( Arrays.equals( checksum, that.checksum
) );
     }
 
 
@@ -78,12 +121,165 @@
 
 
     /**
+     * Set the checksum Value.
+     *
+     * @param checksum The checksum value
+     */
+    public void setChecksumValue( byte[] checksum )
+    {
+        this.checksum = checksum;
+    }
+
+
+    /**
      * Returns the {@link ChecksumType}.
      *
      * @return The {@link ChecksumType}.
      */
     public ChecksumType getChecksumType()
     {
-        return checksumType;
+        return cksumtype;
+    }
+
+
+    /**
+     * Set the {@link ChecksumType}.
+     *
+     * @param cksumtype The checksum algorithm used
+     */
+    public void setChecksumType( ChecksumType cksumType )
+    {
+        this.cksumtype = cksumType;
+    }
+
+
+    /**
+     * Compute the checksum length
+     * 
+     * Checksum :
+     * 
+     * 0x30 L1 checksum sequence
+     *  |
+     *  +--> 0xA0 L2 cksumtype tag
+     *  |     |
+     *  |     +--> 0x02 L2-1 cksumtype (int)
+     *  |
+     *  +--> 0xA1 L3 checksum tag
+     *        |
+     *        +--> 0x04 L3-1 checksum (OCTET STRING)
+     *        
+     *  where L1 = L2 + lenght(0xA0) + length(L2) +
+     *             L3 + lenght(0xA1) + length(L3) 
+     *  and
+     *  L2 = L2-1 + length(0x02) + length( L2-1) 
+     *  L3 = L3-1 + length(0x04) + length( L3-1) 
+     */
+    public int computeLength()
+    {
+        // Compute the checksulType. The Length will always be contained in 1 byte
+        checksumTypeLength = 1 + 1 + Value.getNbBytes( cksumtype.getOrdinal() );
+        checksumLength = 1 + TLV.getNbBytes( checksumTypeLength ) + checksumTypeLength;
+
+        // Compute the checksum Value
+        if ( checksum == null )
+        {
+            checksumBytesLength = 1 + 1;
+        }
+        else
+        {
+            checksumBytesLength = 1 + TLV.getNbBytes( checksum.length ) + checksum.length;
+        }
+
+        checksumLength += 1 + TLV.getNbBytes( checksumBytesLength ) + checksumBytesLength;
+
+        // Compute the whole sequence length
+        int checksumSeqLength = 1 + Value.getNbBytes( checksumLength ) + checksumLength;
+
+        return checksumSeqLength;
+
+    }
+
+
+    /**
+     * Encode the Checksum message to a PDU. 
+     * 
+     * Checksum :
+     * 
+     * 0x30 LL
+     *   0xA0 LL 
+     *     0x02 0x01 cksumtype
+     *   0xA1 LL 
+     *     0x04 LL Checksum
+     * 
+     * @param buffer The buffer where to put the PDU. It should have been allocated
+     * before, with the right size.
+     * @return The constructed PDU.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try
+        {
+            // The Checksum SEQ Tag
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            buffer.put( TLV.getBytes( checksumLength ) );
+
+            // The cksumtype, first the tag, then the value
+            buffer.put( ( byte ) 0xA0 );
+            buffer.put( TLV.getBytes( checksumTypeLength ) );
+            Value.encode( buffer, cksumtype.getOrdinal() );
+
+            // The checksum, first the tag, then the value
+            buffer.put( ( byte ) 0xA1 );
+            buffer.put( TLV.getBytes( checksumBytesLength ) );
+            Value.encode( buffer, checksum );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            log.error( "Cannot encode the Checksum object, the PDU size is {} when only {}
bytes has been allocated", 1
+                + TLV.getNbBytes( checksumLength ) + checksumLength, buffer.capacity() );
+            throw new EncoderException( "The PDU buffer size is too small !" );
+        }
+
+        if ( IS_DEBUG )
+        {
+            log.debug( "Checksum encoding : {}", StringTools.dumpBytes( buffer.array() )
);
+            log.debug( "Checksum initial value : {}", toString() );
+        }
+
+        return buffer;
+    }
+
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        return toString( "" );
+    }
+
+
+    /**
+     * @see Object#toString()
+     */
+    public String toString( String tabs )
+    {
+        StringBuilder sb = new StringBuilder();
+
+        sb.append( tabs ).append( "Checksum : {\n" );
+        sb.append( tabs ).append( "    cksumtype: " ).append(  cksumtype ).append( '\n' );
+
+        if ( checksum != null )
+        {
+            sb.append( tabs + "    checksum:" ).append( StringTools.dumpBytes( checksum )
).append( '\n' );
+        }
+
+        sb.append( tabs + "}\n" );
+
+        return sb.toString();
     }
 }

Added: directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/ChecksumTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/ChecksumTest.java?rev=588239&view=auto
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/ChecksumTest.java
(added)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/ChecksumTest.java
Thu Oct 25 07:24:28 2007
@@ -0,0 +1,81 @@
+/*
+ *  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.server.kerberos.shared.messages.value;
+
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import org.apache.directory.server.kerberos.shared.crypto.checksum.ChecksumType;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Test the Checksum encoding and decoding
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 542147 $, $Date: 2007-05-28 10:14:21 +0200 (Mon, 28 May 2007) $
+ */
+public class ChecksumTest extends TestCase
+{
+    public void testEncodingChecksum() throws Exception
+    {
+        Checksum chk = new Checksum( ChecksumType.CRC32, new byte[]
+            { 0x01, 0x02, 0x03 } );
+
+        ByteBuffer encoded = ByteBuffer.allocate( chk.computeLength() );
+
+        chk.encode( encoded );
+
+        byte[] expectedResult = new byte[]
+            { 
+            0x30, 0x0c, 
+              (byte)0xA0, 0x03, 
+                0x02, 0x01, 0x01, 
+              (byte)0xA1, 0x05, 
+                0x04, 0x03, 
+                  0x01, 0x02, 0x03 
+            };
+
+        assertTrue( Arrays.equals( expectedResult, encoded.array() ) );
+    }
+
+
+    public void testEncodingNullChecksum() throws Exception
+    {
+        Checksum chk = new Checksum( ChecksumType.CRC32, null );
+
+        ByteBuffer encoded = ByteBuffer.allocate( chk.computeLength() );
+
+        chk.encode( encoded );
+
+        byte[] expectedResult = new byte[]
+            { 
+                0x30, 0x09, 
+                  ( byte ) 0xA0, 
+                    0x03, 0x02, 0x01, 0x01, 
+                  ( byte ) 0xA1, 0x02, 
+                    0x04, 0x00 
+            };
+
+        assertTrue( Arrays.equals( expectedResult, encoded.array() ) );
+    }
+}



Mime
View raw message