directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r578743 [5/12] - in /directory/apacheds/branches/apacheds-kerberos: kerberos-shared/ kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/ kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/crypto...
Date Mon, 24 Sep 2007 10:18:45 GMT
Modified: directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/AuthorizationData.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/AuthorizationData.java?rev=578743&r1=578742&r2=578743&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/AuthorizationData.java (original)
+++ directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/AuthorizationData.java Mon Sep 24 03:18:05 2007
@@ -20,21 +20,48 @@
 package org.apache.directory.server.kerberos.shared.messages.value;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 
 import org.apache.directory.server.kerberos.shared.messages.Encodable;
+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.codec.EncoderException;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 /**
+ * An authorization data container.
+ * 
+ * The ASN.1 grammar is :
+ * 
+ * -- NOTE: AuthorizationData is always used as an OPTIONAL field and
+ * -- should not be empty.
+ * AuthorizationData       ::= SEQUENCE OF AuthorizationDataEntry
+ *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class AuthorizationData implements Encodable
+public class AuthorizationData extends AbstractAsn1Object implements Encodable
 {
+    /** The logger */
+    private static final Logger log = LoggerFactory.getLogger( AuthorizationData.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = log.isDebugEnabled();
+
+    /** The list of AuthrorizationData elements */
     private List<AuthorizationDataEntry> entries = new ArrayList<AuthorizationDataEntry>();
 
+    // Storage for computed lengths
+    private transient int authorizationDataLength;
+
 
     /**
      * Creates a new instance of AuthorizationData.
@@ -68,6 +95,15 @@
 
 
     /**
+     * @return The AuthorizationdataEntry list
+     */
+    public List<AuthorizationDataEntry> getEntries()
+    {
+        return entries;
+    }
+
+
+    /**
      * Returns an {@link Iterator} over the entries in this {@link AuthorizationData}.
      *
      * @return An {@link Iterator} over the entries in this {@link AuthorizationData}.
@@ -75,5 +111,143 @@
     public Iterator iterator()
     {
         return entries.iterator();
+    }
+
+
+    /**
+     * Compute the AuthorizationData length
+     * 
+     * AuthorizationData :
+     * 
+     * 0x30 L1 AuthorizationData
+     *  |
+     *  +--> 0x30 L2 AuthorizationDataEntry
+     *  |
+     *  +--> 0x30 L2 AuthorizationDataEntry
+     *  |
+     *  ...
+     *  |
+     *  +--> 0x30 L2 AuthorizationDataEntry
+     */
+    public int computeLength()
+    {
+        if ( ( entries == null ) || ( entries.size() == 0 ) )
+        {
+            authorizationDataLength = 1;
+
+            return authorizationDataLength + 1;
+        }
+        else
+        {
+            authorizationDataLength = 0;
+
+            for ( AuthorizationDataEntry entry : entries )
+            {
+                authorizationDataLength += entry.computeLength();
+            }
+
+            return 1 + TLV.getNbBytes( authorizationDataLength ) + authorizationDataLength;
+        }
+    }
+
+
+    /**
+     * Encode the AuthorizationData message to a PDU. 
+     * 
+     * AuthorizationData :
+     * 
+     * 0x30 LL
+     *   0x30 LL AuthorizationDataEntry 
+     *   0x30 LL AuthorizationDataEntry
+     *   ... 
+     *   0x30 LL AuthorizationDataEntry 
+     * 
+     * @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 AuthorizationData SEQ Tag
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            buffer.put( TLV.getBytes( authorizationDataLength ) );
+
+            // Each entry, if any
+            if ( ( entries != null ) && ( entries.size() != 0 ) )
+            {
+                for ( AuthorizationDataEntry entry : entries )
+                {
+                    entry.encode( buffer );
+                }
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            log.error(
+                "Cannot encode the AuthorizationData object, the PDU size is {} when only {} bytes has been allocated",
+                1 + TLV.getNbBytes( authorizationDataLength ) + authorizationDataLength, buffer.capacity() );
+            throw new EncoderException( "The PDU buffer size is too small !" );
+        }
+
+        if ( IS_DEBUG )
+        {
+            log.debug( "AuthorizationData encoding : {}", StringTools.dumpBytes( buffer.array() ) );
+            log.debug( "AuthorizationData 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();
+
+        if ( ( entries == null ) || ( entries.size() == 0 ) )
+        {
+            sb.append( tabs ).append( "AuthorizationData : {}\n" );
+        }
+        else
+        {
+            sb.append( tabs ).append( "AuthorizationData : {\n" );
+            boolean isFirst = true;
+
+            for ( AuthorizationDataEntry entry : entries )
+            {
+                if ( isFirst )
+                {
+                    isFirst = false;
+                }
+                else
+                {
+                    sb.append( '\n' );
+                }
+
+                sb.append( entry.toString( tabs + "    " ) ).append( '\n' );
+            }
+
+            sb.append( tabs + "}" );
+        }
+
+        return sb.toString();
     }
 }

Modified: directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/AuthorizationDataEntry.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/AuthorizationDataEntry.java?rev=578743&r1=578742&r2=578743&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/AuthorizationDataEntry.java (original)
+++ directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/AuthorizationDataEntry.java Mon Sep 24 03:18:05 2007
@@ -20,14 +20,57 @@
 package org.apache.directory.server.kerberos.shared.messages.value;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.server.kerberos.shared.messages.value.types.AuthorizationType;
+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;
+
+
 /**
+ * A single AuthorizationData
+ * 
+ * The ASN.1 grammar is :
+ * -- NOTE: AuthorizationData is always used as an OPTIONAL field and
+ * -- should not be empty.
+ * AuthorizationDataEntry       ::= SEQUENCE {
+ *        ad-type         [0] Int32,
+ *        ad-data         [1] OCTET STRING
+ * }
+ *  
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class AuthorizationDataEntry
+public class AuthorizationDataEntry extends AbstractAsn1Object
 {
-    private AuthorizationType authorizationDataType;
-    private byte[] authorizationData;
+    /** The logger */
+    private static final Logger log = LoggerFactory.getLogger( AuthorizationDataEntry.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = log.isDebugEnabled();
+
+    /** The Authorization type. One of :
+     * DER encoding of AD-IF-RELEVANT        1
+     * DER encoding of AD-KDCIssued          4
+     * DER encoding of AD-AND-OR             5
+     * DER encoding of AD-MANDATORY-FOR-KDC  8 
+     **/
+    private AuthorizationType adType;
+
+    /** The data, encrypted */
+    private byte[] adData;
+
+    // Storage for computed lengths
+    private transient int adTypeLength;
+    private transient int adDataLength;
+    private transient int authorizationDataEntryLength;
 
 
     /**
@@ -38,8 +81,8 @@
      */
     public AuthorizationDataEntry( AuthorizationType adType, byte[] adData )
     {
-        authorizationDataType = adType;
-        authorizationData = adData;
+        this.adType = adType;
+        this.adData = adData;
     }
 
 
@@ -50,7 +93,7 @@
      */
     public byte[] getAuthorizationData()
     {
-        return authorizationData;
+        return adData;
     }
 
 
@@ -61,6 +104,138 @@
      */
     public AuthorizationType getAuthorizationDataType()
     {
-        return authorizationDataType;
+        return adType;
+    }
+
+
+    /**
+     * Compute the AuthorizationDataEntry length
+     * 
+     * AuthorizationDataEntry :
+     * 
+     * 0x30 L1 AuthorizationDataEntry
+     *  |
+     *  +--> 0xA0 L2 adType tag
+     *  |     |
+     *  |     +--> 0x02 L2-1 adType (int)
+     *  |
+     *  +--> 0xA1 L3 adData tag
+     *        |
+     *        +--> 0x04 L3-1 adData (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 adType. The Length will always be contained in 1 byte
+        adTypeLength = 1 + 1 + Value.getNbBytes( adType.getOrdinal() );
+        authorizationDataEntryLength = 1 + TLV.getNbBytes( adTypeLength ) + adTypeLength;
+
+        // Compute the keyValue
+        if ( adData == null )
+        {
+            adDataLength = 1 + 1;
+        }
+        else
+        {
+            adDataLength = 1 + TLV.getNbBytes( adData.length ) + adData.length;
+        }
+
+        authorizationDataEntryLength += 1 + TLV.getNbBytes( adDataLength ) + adDataLength;
+
+        // Compute the whole sequence length
+        int authorizationDataEntrySeqLength = 1 + Value.getNbBytes( authorizationDataEntryLength )
+            + authorizationDataEntryLength;
+
+        return authorizationDataEntrySeqLength;
+
+    }
+
+
+    /**
+     * Encode the AuthorizationDataEntry message to a PDU. 
+     * 
+     * AuthorizationDataEntry :
+     * 
+     * 0x30 LL
+     *   0xA0 LL 
+     *     0x02 0x01 adType
+     *   0xA1 LL 
+     *     0x04 LL adData
+     * 
+     * @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 AuthorizationDataEntry SEQ Tag
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            buffer.put( TLV.getBytes( authorizationDataEntryLength ) );
+
+            // The adType, first the tag, then the value
+            buffer.put( ( byte ) 0xA0 );
+            buffer.put( TLV.getBytes( adTypeLength ) );
+            Value.encode( buffer, adType.getOrdinal() );
+
+            // The adData, first the tag, then the value
+            buffer.put( ( byte ) 0xA1 );
+            buffer.put( TLV.getBytes( adDataLength ) );
+            Value.encode( buffer, adData );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            log
+                .error(
+                    "Cannot encode the AuthorizationDataEntry object, the PDU size is {} when only {} bytes has been allocated",
+                    1 + TLV.getNbBytes( authorizationDataEntryLength ) + authorizationDataEntryLength, buffer
+                        .capacity() );
+            throw new EncoderException( "The PDU buffer size is too small !" );
+        }
+
+        if ( IS_DEBUG )
+        {
+            log.debug( "AuthorizationDataEntry encoding : {}", StringTools.dumpBytes( buffer.array() ) );
+            log.debug( "AuthorizationDataEntry 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( "AuthorizationDataEntry : {\n" );
+        sb.append( tabs ).append( "    ad-type: " ).append( adType ).append( '\n' );
+
+        sb.append( tabs ).append( "    ad-data: " ).append( StringTools.dumpBytes( adData ) )
+            .append( "\n" + tabs + "}" );
+
+        return sb.toString();
     }
 }

Modified: directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/Checksum.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/Checksum.java?rev=578743&r1=578742&r2=578743&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/Checksum.java (original)
+++ directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/Checksum.java Mon Sep 24 03:18:05 2007
@@ -20,20 +20,52 @@
 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.
@@ -41,9 +73,9 @@
      * @param checksumType
      * @param checksum
      */
-    public Checksum( ChecksumType checksumType, byte[] checksum )
+    public Checksum( ChecksumType cksumtype, byte[] checksum )
     {
-        this.checksumType = checksumType;
+        this.cksumtype = cksumtype;
         this.checksum = checksum;
     }
 
@@ -55,14 +87,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 ) );
     }
 
 
@@ -84,6 +116,137 @@
      */
     public ChecksumType getChecksumType()
     {
-        return checksumType;
+        return 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();
     }
 }

Modified: directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptedData.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptedData.java?rev=578743&r1=578742&r2=578743&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptedData.java (original)
+++ directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptedData.java Mon Sep 24 03:18:05 2007
@@ -20,32 +20,98 @@
 package org.apache.directory.server.kerberos.shared.messages.value;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
 import org.apache.directory.server.kerberos.shared.crypto.encryption.EncryptionType;
+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;
 
 
 /**
+ * A structure storing an encrypted data element. The ASN.1 grammar is :
+ * 
+ * EncryptedData   ::= SEQUENCE {
+ *        etype   [0] Int32 -- EncryptionType --,
+ *        kvno    [1] UInt32 OPTIONAL,
+ *        cipher  [2] OCTET STRING -- ciphertext
+ * }
+ *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class EncryptedData
+public class EncryptedData extends AbstractAsn1Object
 {
-    private EncryptionType encryptionType;
-    private int keyVersion; //optional
-    private byte[] cipherText;
+    /** The logger */
+    private static final Logger log = LoggerFactory.getLogger( EncryptedData.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = log.isDebugEnabled();
+
+    /** The used encryption algorithm */
+    private EncryptionType eType;
+
+    /** Version number of the key under which data is encrypted */
+    private int kvno;
+
+    /** A flag used to tell if a kvno has been added, as the kvno is optional. */
+    private boolean hasKvno;
+
+    /** The field containing the enciphered text */
+    private byte[] cipher;
+
+    /** A constant used when the key is not present */
+    public static final boolean HAS_KVNO = true;
+
+    // Storage for computed lengths
+    private transient int eTypeTagLength;
+    private transient int kvnoTagLength;
+    private transient int cipherTagLength;
+    private transient int encryptedDataSeqLength;
+
+
+    /**
+     * Creates a new instance of EncryptedData.
+     */
+    public EncryptedData()
+    {
+        hasKvno = !HAS_KVNO;
+    }
+    
+    /**
+     * Creates a new instance of EncryptedData.
+     *
+     * @param etype The encription algorithm
+     * @param kvno The key version
+     * @param cipher the encrypted text
+     */
+    public EncryptedData( EncryptionType eType, int kvno, byte[] cipher )
+    {
+        this.eType = eType;
+        this.hasKvno = kvno > 0;
+        this.kvno = kvno;
+        this.cipher = cipher;
+    }
 
 
     /**
      * Creates a new instance of EncryptedData.
      *
-     * @param type
-     * @param version
-     * @param cipherText
-     */
-    public EncryptedData( EncryptionType type, int version, byte[] cipherText )
-    {
-        encryptionType = type;
-        keyVersion = version;
-        this.cipherText = cipherText;
+     * @param etype The encription algorithm
+     * @param cipher the encrypted text
+     */
+    public EncryptedData( EncryptionType eType, byte[] cipher )
+    {
+        this.eType = eType;
+        this.hasKvno = !HAS_KVNO;
+        kvno = -1;
+        this.cipher = cipher;
     }
 
 
@@ -56,18 +122,46 @@
      */
     public EncryptionType getEncryptionType()
     {
-        return encryptionType;
+        return eType;
     }
 
 
     /**
+     * Set the EncryptionType
+     * @param eType the EncryptionType
+     */
+    public void setEncryptionType( EncryptionType eType )
+    {
+        this.eType = eType;
+    }
+
+    /**
      * Returns the key version.
      *
      * @return The key version.
      */
     public int getKeyVersion()
     {
-        return keyVersion;
+        return hasKvno ? kvno : -1;
+    }
+
+    /**
+     * Set the key version
+     * @param kvno The key version
+     */
+    public void setKeyVersion( int kvno )
+    {
+        this.kvno = kvno;
+    }
+
+    /**
+     * Tells if there is a key version.
+     *
+     * @return <code>true</code> if there is a key version.
+     */
+    public boolean hasKeyVersion()
+    {
+        return hasKvno;
     }
 
 
@@ -76,8 +170,164 @@
      *
      * @return The raw cipher text.
      */
-    public byte[] getCipherText()
+    public byte[] getCipher()
     {
-        return cipherText;
+        return cipher;
+    }
+
+    /**
+     * Set the cipher text
+     * @param cipher The cipher text
+     */
+    public void setCipherText( byte[] cipher )
+    {
+        this.cipher = cipher;
+    }
+    
+
+    /**
+     * Compute the EncryptedData length
+     * 
+     * EncryptedData :
+     * 
+     * 0x30 L1 EncryptedData sequence
+     *  |
+     *  +--> 0xA1 L2 etype tag
+     *  |     |
+     *  |     +--> 0x02 L2-1 etype (int)
+     *  |
+     *  +--> [0xA2 L3 kvno tag
+     *  |     |
+     *  |     +--> 0x30 L3-1 kvno (int)] (optional)
+     *  |
+     *  +--> 0xA2 L4 cipher tag
+     *        |
+     *        +--> 0x04 L4-1 cipher (OCTET STRING)
+     */
+    public int computeLength()
+    {
+        encryptedDataSeqLength = 0;
+
+        // Compute the encryption Type length
+        int eTypeLength = Value.getNbBytes( eType.getOrdinal() );
+        eTypeTagLength = 1 + TLV.getNbBytes( eTypeLength ) + eTypeLength;
+        encryptedDataSeqLength = 1 + TLV.getNbBytes( eTypeTagLength ) + eTypeTagLength; 
+
+
+        // Compute the kvno length if any
+        if ( hasKvno )
+        {
+            int kvnoLength = Value.getNbBytes( kvno );
+            kvnoTagLength = 1 + TLV.getNbBytes( kvnoLength ) + kvnoLength;
+            encryptedDataSeqLength += 1 + TLV.getNbBytes( kvnoTagLength ) + kvnoTagLength;
+        }
+        else
+        {
+            kvnoTagLength = 0;
+        }
+
+        // Compute the cipher
+        if ( ( cipher == null ) || ( cipher.length == 0 ) )
+        {
+            cipherTagLength = 1 + 1;
+        }
+        else
+        {
+            cipherTagLength = 1 + TLV.getNbBytes( cipher.length ) + cipher.length;
+        }
+
+        encryptedDataSeqLength += 1 + TLV.getNbBytes( cipherTagLength ) + cipherTagLength;
+
+        // Compute the whole sequence length
+        return 1 + TLV.getNbBytes( encryptedDataSeqLength ) + encryptedDataSeqLength;
+    }
+
+
+    /**
+     * Encode the EncryptedData message to a PDU. 
+     * 
+     * EncryptedData :
+     * 
+     * 0x30 LL
+     *   0xA0 LL 
+     *     0x02 0x01 etype (integer)
+     *   [0xA1 LL 
+     *     0x02 0x01 kvno (integer)] (optional)
+     *   0xA2 LL 
+     *     0x04 LL cipher (OCTET STRING)
+     * 
+     * @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 EncryptedData SEQ Tag
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            buffer.put( TLV.getBytes( encryptedDataSeqLength ) );
+
+            // The etype, first the tag, then the value
+            buffer.put( ( byte ) 0xA0 );
+            buffer.put( TLV.getBytes( eTypeTagLength ) );
+
+            Value.encode( buffer, eType.getOrdinal() );
+
+            // The kvno, if any, first the tag, then the value
+            if ( hasKvno )
+            {
+                buffer.put( ( byte ) 0xA1 );
+                buffer.put( TLV.getBytes( kvnoTagLength ) );
+
+                Value.encode( buffer, kvno );
+            }
+
+            // The cipher tag
+            buffer.put( ( byte ) 0xA2 );
+            buffer.put( TLV.getBytes( cipherTagLength ) );
+            Value.encode( buffer, cipher );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            log.error(
+                "Cannot encode the EncryptedData object, the PDU size is {} when only {} bytes has been allocated", 1
+                    + TLV.getNbBytes( encryptedDataSeqLength ) + encryptedDataSeqLength, buffer.capacity() );
+            throw new EncoderException( "The PDU buffer size is too small !" );
+        }
+
+        if ( IS_DEBUG )
+        {
+            log.debug( "EncryptedData encoding : {}", StringTools.dumpBytes( buffer.array() ) );
+            log.debug( "EncryptedData initial value : {}", toString() );
+        }
+
+        return buffer;
+    }
+
+
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+
+        sb.append( "EncryptedData : {\n" );
+        sb.append( "    etype: " ).append( eType ).append( '\n' );
+
+        if ( hasKvno )
+        {
+            sb.append( "    kvno: " ).append( kvno ).append( '\n' );
+        }
+
+        sb.append( "    cipher: " ).append( StringTools.dumpBytes( cipher ) ).append( "\n}\n" );
+
+        return sb.toString();
     }
 }

Modified: directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptedTimeStampModifier.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptedTimeStampModifier.java?rev=578743&r1=578742&r2=578743&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptedTimeStampModifier.java (original)
+++ directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptedTimeStampModifier.java Mon Sep 24 03:18:05 2007
@@ -31,13 +31,13 @@
 
 
     /**
-     * Returns the {@link EncryptedTimeStamp}.
+     * Returns the {@link PreAuthEncryptedTimestamp}.
      *
-     * @return The {@link EncryptedTimeStamp}.
+     * @return The {@link PreAuthEncryptedTimestamp}.
      */
-    public EncryptedTimeStamp getEncryptedTimestamp()
+    public PreAuthEncryptedTimestamp getEncryptedTimestamp()
     {
-        return new EncryptedTimeStamp( timeStamp, microSecond );
+        return new PreAuthEncryptedTimestamp( timeStamp, microSecond );
     }
 
 

Modified: directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptionKey.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptionKey.java?rev=578743&r1=578742&r2=578743&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptionKey.java (original)
+++ directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptionKey.java Mon Sep 24 03:18:05 2007
@@ -20,24 +20,56 @@
 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.encryption.EncryptionType;
+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;
 
 
 /**
  * A Kerberos symmetric encryption key, which includes metadata support for
  * the associated key type and key version number.
  * 
+ * The ASN.1 description for this structure is :
+ * EncryptionKey   ::= SEQUENCE {
+ *       keytype         [0] Int32 -- actually encryption type --,
+ *       keyvalue        [1] OCTET STRING
+ * }
+ * 
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class EncryptionKey
+public class EncryptionKey extends AbstractAsn1Object
 {
+    /** The logger */
+    private static final Logger log = LoggerFactory.getLogger( EncryptionKey.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = log.isDebugEnabled();
+
+    // The encryption type
     private EncryptionType keyType;
+
+    // The encrypted value
     private byte[] keyValue;
+
+    // ???
     private int keyVersion;
 
+    // Storage for computed lengths
+    private transient int keyTypeLength;
+    private transient int keyValueLength;
+    private transient int encryptionKeyLength;
+
 
     /**
      * Creates a new instance of EncryptionKey.
@@ -78,10 +110,7 @@
     {
         if ( keyValue != null )
         {
-            for ( int ii = 0; ii < keyValue.length; ii++ )
-            {
-                keyValue[ii] = 0;
-            }
+            Arrays.fill( keyValue, ( byte ) 0x00 );
         }
     }
 
@@ -119,6 +148,9 @@
     }
 
 
+    /**
+     * @see Object#equals(Object)
+     */
     public boolean equals( Object o )
     {
         if ( this == o )
@@ -126,7 +158,7 @@
             return true;
         }
 
-        if ( !( o instanceof EncryptionKey ) )
+        if ( ( o == null ) || !( o instanceof EncryptionKey ) )
         {
             return false;
         }
@@ -136,6 +168,112 @@
     }
 
 
+    /**
+     * Compute the EncryptionKey length
+     * 
+     * EncryptionKey :
+     * 
+     * 0x30 L1 EncryptionKey
+     *  |
+     *  +--> 0xA0 L2 keyType tag
+     *  |     |
+     *  |     +--> 0x02 L2-1 keyType (int)
+     *  |
+     *  +--> 0xA1 L3 keyValue tag
+     *        |
+     *        +--> 0x04 L3-1 keyValue (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 keyType. The Length will always be cobntained in 1 byte
+        keyTypeLength = 1 + 1 + Value.getNbBytes( keyType.getOrdinal() );
+        encryptionKeyLength = 1 + TLV.getNbBytes( keyTypeLength ) + keyTypeLength;
+
+        // Compute the keyValue
+        if ( keyValue == null )
+        {
+            keyValueLength = 1 + 1;
+        }
+        else
+        {
+            keyValueLength = 1 + TLV.getNbBytes( keyValue.length ) + keyValue.length;
+        }
+
+        encryptionKeyLength += 1 + TLV.getNbBytes( keyValueLength ) + keyValueLength;
+
+        // Compute the whole sequence length
+        int encryptionKeySeqLength = 1 + Value.getNbBytes( encryptionKeyLength ) + encryptionKeyLength;
+
+        return encryptionKeySeqLength;
+
+    }
+
+
+    /**
+     * Encode the EncryptionKey message to a PDU. 
+     * 
+     * EncryptionKey :
+     * 
+     * 0x30 LL
+     *   0xA0 LL 
+     *     0x02 0x01 keyType
+     *   0xA1 LL 
+     *     0x04 LL keyValue
+     * 
+     * @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 EncryptionKey SEQ Tag
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            buffer.put( TLV.getBytes( encryptionKeyLength ) );
+
+            // The keyType, first the tag, then the value
+            buffer.put( ( byte ) 0xA0 );
+            buffer.put( TLV.getBytes( keyTypeLength ) );
+            Value.encode( buffer, keyType.getOrdinal() );
+
+            // The keyValue, first the tag, then the value
+            buffer.put( ( byte ) 0xA1 );
+            buffer.put( TLV.getBytes( keyValueLength ) );
+            Value.encode( buffer, keyValue );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            log.error(
+                "Cannot encode the EncryptionKey object, the PDU size is {} when only {} bytes has been allocated", 1
+                    + TLV.getNbBytes( encryptionKeyLength ) + encryptionKeyLength, buffer.capacity() );
+            throw new EncoderException( "The PDU buffer size is too small !" );
+        }
+
+        if ( IS_DEBUG )
+        {
+            log.debug( "EncryptionKey encoding : {}", StringTools.dumpBytes( buffer.array() ) );
+            log.debug( "EncryptionKey initial value : {}", toString() );
+        }
+
+        return buffer;
+    }
+
+
+    /**
+     * @see Object#toString()
+     */
     public String toString()
     {
         return keyType.toString() + " (" + keyType.getOrdinal() + ")";

Modified: directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptionTypeInfoEntry.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptionTypeInfoEntry.java?rev=578743&r1=578742&r2=578743&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptionTypeInfoEntry.java (original)
+++ directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/EncryptionTypeInfoEntry.java Mon Sep 24 03:18:05 2007
@@ -20,28 +20,71 @@
 package org.apache.directory.server.kerberos.shared.messages.value;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
 import org.apache.directory.server.kerberos.shared.crypto.encryption.EncryptionType;
+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 Encryption Type info entry container. The ASN.1 grammar is :
+ * 
+ *  ETYPE-INFO-ENTRY        ::= SEQUENCE {
+ *          etype           [0] Int32,
+ *          salt            [1] OCTET STRING OPTIONAL
+ *  }
+ *
+ * 
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class EncryptionTypeInfoEntry
+public class EncryptionTypeInfoEntry extends AbstractAsn1Object
 {
-    private EncryptionType encryptionType;
+    /** The logger */
+    private static final Logger log = LoggerFactory.getLogger( EncryptionTypeInfoEntry.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = log.isDebugEnabled();
+
+    /** The encryption type */
+    private EncryptionType eType;
+    
+    /** The salt */
     private byte[] salt;
 
+    // Storage for computed lengths
+    private transient int eTypeLength;
+    private transient int saltLength;
+    private transient int encryptionTypeInfoEntryLength;
+
+    /**
+     * Creates a new instance of EncryptionTypeInfoEntry.
+     *
+     * @param eType The encryption type.
+     */
+    public EncryptionTypeInfoEntry( EncryptionType eType )
+    {
+        this.eType = eType;
+        this.salt = null;
+    }
 
     /**
      * Creates a new instance of EncryptionTypeInfoEntry.
      *
-     * @param encryptionType
-     * @param salt
+     * @param eType The encryption type. 
+     * @param salt The salt
      */
-    public EncryptionTypeInfoEntry( EncryptionType encryptionType, byte[] salt )
+    public EncryptionTypeInfoEntry( EncryptionType eType, byte[] salt )
     {
-        this.encryptionType = encryptionType;
+        this.eType = eType;
         this.salt = salt;
     }
 
@@ -64,6 +107,139 @@
      */
     public EncryptionType getEncryptionType()
     {
-        return encryptionType;
+        return eType;
+    }
+
+    /**
+     * Compute the EncryptionTypeInfoEntry length
+     * 
+     * EncryptionTypeInfoEntry :
+     * 
+     * 0x30 L1 EncryptionTypeInfoEntry sequence
+     *  |
+     *  +--> 0xA0 L2 etype tag
+     *  |     |
+     *  |     +--> 0x02 L2-1 etype (int)
+     *  |
+     *  [+--> 0xA1 L3 salt tag
+     *        |
+     *        +--> 0x04 L3-1 salt (OCTET STRING)] OPTIONAL
+     *        
+     *  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 eType.
+        eTypeLength = 1 + TLV.getNbBytes( eType.getOrdinal() ) + Value.getNbBytes( eType.getOrdinal() );
+        encryptionTypeInfoEntryLength = 
+            1 + TLV.getNbBytes( eTypeLength ) + eTypeLength;
+
+        // Compute the salt length, if any
+        if ( salt != null )
+        {
+            saltLength = 1 + TLV.getNbBytes( salt.length ) + salt.length;
+            encryptionTypeInfoEntryLength += 
+                1 + TLV.getNbBytes( saltLength ) + saltLength;
+        }
+        else
+        {
+            saltLength = 0;
+        }
+
+        // Compute the whole sequence length
+        int encryptionTypeInfoEntrySeqLength = 1 + Value.getNbBytes( encryptionTypeInfoEntryLength ) + encryptionTypeInfoEntryLength;
+
+        return encryptionTypeInfoEntrySeqLength;
+    }
+
+
+    /**
+     * Encode the EncryptionTypeInfoEntry message to a PDU. 
+     * 
+     * EncryptionTypeInfoEntry :
+     * 
+     * 0x30 LL
+     *   0xA0 LL 
+     *     0x02 0x01 etype
+     *   0xA1 LL 
+     *     0x04 LL salt
+     * 
+     * @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 EncryptionTypeInfoEntry SEQ Tag
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            buffer.put( TLV.getBytes( encryptionTypeInfoEntryLength ) );
+
+            // The etype, first the tag, then the value
+            buffer.put( ( byte ) 0xA0 );
+            buffer.put( TLV.getBytes( eTypeLength ) );
+            Value.encode( buffer, eType.getOrdinal() );
+
+            // The salt, first the tag, then the value, if any
+            if ( salt != null )
+            {
+                buffer.put( ( byte ) 0xA1 );
+                buffer.put( TLV.getBytes( saltLength ) );
+                Value.encode( buffer, salt );
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            log.error( "Cannot encode the EncryptionTypeInfoEntry object, the PDU size is {} when only {} bytes has been allocated", 1
+                + TLV.getNbBytes( encryptionTypeInfoEntryLength ) + encryptionTypeInfoEntryLength, buffer.capacity() );
+            throw new EncoderException( "The PDU buffer size is too small !" );
+        }
+
+        if ( IS_DEBUG )
+        {
+            log.debug( "EncryptionTypeInfoEntry encoding : {}", StringTools.dumpBytes( buffer.array() ) );
+            log.debug( "EncryptionTypeInfoEntry 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( "EncryptionTypeInfoEntry : {\n" );
+        sb.append( tabs ).append( "    eType: " ).append( eType ).append( '\n' );
+
+        if ( salt != null )
+        {
+            sb.append( tabs + "    salt:" ).append( StringTools.dumpBytes( salt ) ).append( '\n' );
+        }
+
+        sb.append( tabs + "}\n" );
+
+        return sb.toString();
     }
 }

Modified: directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddress.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddress.java?rev=578743&r1=578742&r2=578743&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddress.java (original)
+++ directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddress.java Mon Sep 24 03:18:05 2007
@@ -22,19 +22,65 @@
 
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.server.kerberos.shared.messages.value.types.HostAddressType;
+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;
 
 
 /**
  * Provides host address information.
  * 
+ * The ASN.1 grammaor for this structure is :
+ * 
+ * HostAddress     ::= SEQUENCE  {
+ *        addr-type       [0] Int32,
+ *        address         [1] OCTET STRING
+ * }
+ * 
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class HostAddress
+public class HostAddress extends AbstractAsn1Object
 {
+    /** The logger */
+    private static final Logger log = LoggerFactory.getLogger( HostAddress.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = log.isDebugEnabled();
+
+    /** The host address type. One of :
+     *    Address Type                   Value
+     *
+     *    IPv4                             2
+     *    Directional                      3
+     *    ChaosNet                         5
+     *    XNS                              6
+     *    ISO                              7
+     *    DECNET Phase IV                 12
+     *    AppleTalk DDP                   16
+     *    NetBios                         20
+     *    IPv6                            24
+     */
     private HostAddressType addressType;
+
+    /** The address */
     private byte[] address;
 
+    // Storage for computed lengths
+    private transient int addressTypeLength;
+    private transient int addressLength;
+    private transient int hostAddressLength;
+    private transient int hostAddressSeqLength;
+
 
     /**
      * Creates a new instance of HostAddress.
@@ -56,7 +102,7 @@
      */
     public HostAddress( InetAddress internetAddress )
     {
-        addressType = HostAddressType.ADDRTYPE_IPV4;
+        addressType = HostAddressType.ADDRTYPE_INET;
         byte[] newAddress = internetAddress.getAddress();
         address = new byte[newAddress.length];
         System.arraycopy( newAddress, 0, address, 0, newAddress.length );
@@ -105,6 +151,108 @@
     public byte[] getAddress()
     {
         return address;
+    }
+
+
+    /**
+     * Compute the host address length
+     * 
+     * HostAddress :
+     * 
+     * 0x30 L1 hostAddress sequence
+     *  |
+     *  +--> 0xA0 L2 addressType tag
+     *  |     |
+     *  |     +--> 0x02 L2-1 addressType (int)
+     *  |
+     *  +--> 0xA1 L3 address tag
+     *        |
+     *        +--> 0x04 L3-1 address (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 keyType. The Length will always be cobntained in 1 byte
+        addressTypeLength = 1 + 1 + Value.getNbBytes( addressType.getOrdinal() );
+        hostAddressLength = 1 + TLV.getNbBytes( addressTypeLength ) + addressTypeLength;
+
+        // Compute the keyValue
+        if ( address == null )
+        {
+            addressLength = 1 + 1;
+        }
+        else
+        {
+            addressLength = 1 + TLV.getNbBytes( address.length ) + address.length;
+        }
+
+        hostAddressLength += 1 + TLV.getNbBytes( addressLength ) + addressLength;
+
+        // Compute the whole sequence length
+        hostAddressSeqLength = 1 + Value.getNbBytes( hostAddressLength ) + hostAddressLength;
+
+        return hostAddressSeqLength;
+    }
+
+
+    /**
+     * Encode the HostAddress message to a PDU. 
+     * 
+     * HostAddress :
+     * 
+     * 0x30 LL
+     *   0xA0 LL 
+     *     0x02 0x01 addr-type
+     *   0xA1 LL 
+     *     0x04 LL address
+     * 
+     * @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 HostAddress SEQ Tag
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            buffer.put( TLV.getBytes( hostAddressLength ) );
+
+            // The addr-type, first the tag, then the value
+            buffer.put( ( byte ) 0xA0 );
+            buffer.put( TLV.getBytes( addressTypeLength ) );
+            Value.encode( buffer, addressType.getOrdinal() );
+
+            // The address, first the tag, then the value
+            buffer.put( ( byte ) 0xA1 );
+            buffer.put( TLV.getBytes( addressLength ) );
+            Value.encode( buffer, address );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            log.error(
+                "Cannot encode the HostAddress object, the PDU size is {} when only {} bytes has been allocated", 1
+                    + TLV.getNbBytes( hostAddressLength ) + hostAddressLength, 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;
     }
 
 

Modified: directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java?rev=578743&r1=578742&r2=578743&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java (original)
+++ directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java Mon Sep 24 03:18:05 2007
@@ -20,13 +20,48 @@
 package org.apache.directory.server.kerberos.shared.messages.value;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+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.codec.EncoderException;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
 /**
+ * Store a list of addresses.
+ * 
+ * The ASN.1 grammar is :
+ * 
+ * -- NOTE: HostAddresses is always used as an OPTIONAL field and
+ * -- should not be empty.
+ * HostAddresses   -- NOTE: subtly different from rfc1510,
+ *                 -- but has a value mapping and encodes the same
+ *         ::= SEQUENCE OF HostAddress
+ *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class HostAddresses
+public class HostAddresses extends AbstractAsn1Object
 {
-    private HostAddress[] addresses;
+    /** The logger */
+    private static final Logger log = LoggerFactory.getLogger( HostAddresses.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = log.isDebugEnabled();
+
+    /** List of all HostAddress stored */
+    private List<HostAddress> addresses;
+
+    // Storage for computed lengths
+    private transient int addressesLength;
 
 
     /**
@@ -36,7 +71,25 @@
      */
     public HostAddresses( HostAddress[] addresses )
     {
-        this.addresses = addresses;
+        if ( addresses == null )
+        {
+            this.addresses = new ArrayList<HostAddress>();
+        }
+        else
+        {
+            this.addresses = Arrays.asList( addresses );
+        }
+    }
+
+
+    public void addHostAddress( HostAddress hostAddress )
+    {
+        if ( addresses == null )
+        {
+            addresses = new ArrayList<HostAddress>();
+        }
+
+        addresses.add( hostAddress );
     }
 
 
@@ -50,13 +103,7 @@
     {
         if ( addresses != null )
         {
-            for ( int ii = 0; ii < addresses.length; ii++ )
-            {
-                if ( addresses[ii].equals( address ) )
-                {
-                    return true;
-                }
-            }
+            return addresses.contains( address );
         }
 
         return false;
@@ -79,14 +126,17 @@
 
         if ( this.addresses != null && that.addresses != null )
         {
-            if ( this.addresses.length != that.addresses.length )
+            if ( this.addresses.size() != that.addresses.size() )
             {
                 return false;
             }
 
-            for ( int ii = 0; ii < this.addresses.length; ii++ )
+            HostAddress[] thisHostAddresses = ( HostAddress[] ) addresses.toArray();
+            HostAddress[] thatHostAddresses = ( HostAddress[] ) that.addresses.toArray();
+
+            for ( int i = 0; i < thisHostAddresses.length; i++ )
             {
-                if ( !this.addresses[ii].equals( that.addresses[ii] ) )
+                if ( !thisHostAddresses[i].equals( thatHostAddresses[i] ) )
                 {
                     return false;
                 }
@@ -104,22 +154,120 @@
      */
     public HostAddress[] getAddresses()
     {
-        return addresses;
+        return ( HostAddress[] ) addresses.toArray();
     }
 
 
-    public String toString()
+    /**
+     * Compute the hostAddresses length
+     * 
+     * HostAddresses :
+     * 
+     * 0x30 L1 hostAddresses sequence of HostAddresses
+     *  |
+     *  +--> 0x30 L2[1] Hostaddress[1]
+     *  |
+     *  +--> 0x30 L2[2] Hostaddress[2]
+     *  |
+     *  ...
+     *  |
+     *  +--> 0x30 L2[n] Hostaddress[n]
+     *        
+     *  where L1 = sum( L2[1], l2[2], ..., L2[n] )
+     */
+    public int computeLength()
     {
-        StringBuffer sb = new StringBuffer();
+        // Compute the addresses length.
+        addressesLength = 0;
 
-        for ( int ii = 0; ii < this.addresses.length; ii++ )
+        if ( ( addresses != null ) && ( addresses.size() != 0 ) )
         {
-            sb.append( addresses[ii].toString() );
+            for ( HostAddress hostAddress : addresses )
+            {
+                int length = hostAddress.computeLength();
+                addressesLength += length;
+            }
+        }
 
-            if ( ii < addresses.length - 1 )
+        return 1 + TLV.getNbBytes( addressesLength ) + addressesLength;
+    }
+
+
+    /**
+     * Encode the HostAddress message to a PDU. 
+     * 
+     * HostAddress :
+     * 
+     * 0x30 LL
+     *   0x30 LL hostaddress[1] 
+     *   0x30 LL hostaddress[1]
+     *   ... 
+     *   0x30 LL hostaddress[1] 
+     * 
+     * @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 HostAddresses SEQ Tag
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            buffer.put( TLV.getBytes( addressesLength ) );
+
+            // The hostAddress list, if it's not empty
+            if ( ( addresses != null ) && ( addresses.size() != 0 ) )
+            {
+                for ( HostAddress hostAddress : addresses )
+                {
+                    hostAddress.encode( buffer );
+                }
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            log.error(
+                "Cannot encode the HostAddresses object, the PDU size is {} when only {} bytes has been allocated", 1
+                    + TLV.getNbBytes( addressesLength ) + addressesLength, buffer.capacity() );
+            throw new EncoderException( "The PDU buffer size is too small !" );
+        }
+
+        if ( IS_DEBUG )
+        {
+            log.debug( "HostAddresses encoding : {}", StringTools.dumpBytes( buffer.array() ) );
+            log.debug( "HostAddresses initial value : {}", toString() );
+        }
+
+        return buffer;
+    }
+
+
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+        boolean isFirst = true;
+
+        for ( HostAddress hostAddress : addresses )
+        {
+            if ( isFirst )
+            {
+                isFirst = false;
+            }
+            else
             {
                 sb.append( ", " );
             }
+
+            sb.append( hostAddress.toString() );
         }
 
         return sb.toString();

Modified: directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/KerberosPrincipalModifier.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/KerberosPrincipalModifier.java?rev=578743&r1=578742&r2=578743&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/KerberosPrincipalModifier.java (original)
+++ directory/apacheds/branches/apacheds-kerberos/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/KerberosPrincipalModifier.java Mon Sep 24 03:18:05 2007
@@ -44,8 +44,8 @@
     {
         if ( nameComponent != null )
         {
-            StringBuffer sb = new StringBuffer();
-            sb.append( nameComponent.getNameComponent() );
+            StringBuilder sb = new StringBuilder();
+            sb.append( nameComponent.getNameString() );
 
             if ( realm != null )
             {
@@ -53,7 +53,7 @@
                 sb.append( realm );
             }
 
-            return new KerberosPrincipal( sb.toString(), nameComponent.getNameType() );
+            return new KerberosPrincipal( sb.toString(), nameComponent.getNameType().getOrdinal() );
         }
 
         return null;



Mime
View raw message