You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2010/11/18 17:17:57 UTC

svn commit: r1036514 - in /directory/apacheds/trunk/kerberos-codec/src: main/java/org/apache/directory/shared/kerberos/codec/apReq/ main/java/org/apache/directory/shared/kerberos/codec/apReq/actions/ main/java/org/apache/directory/shared/kerberos/messa...

Author: elecharny
Date: Thu Nov 18 16:17:56 2010
New Revision: 1036514

URL: http://svn.apache.org/viewvc?rev=1036514&view=rev
Log:
o Added some tests for ApReq
o Fixed a few errors in the grammar and actions for AP-REQ

Added:
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/apReq/actions/ApReqInit.java
    directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/ApReqDecoderTest.java
Modified:
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/apReq/ApReqGrammar.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/messages/ApReq.java

Modified: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/apReq/ApReqGrammar.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/apReq/ApReqGrammar.java?rev=1036514&r1=1036513&r2=1036514&view=diff
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/apReq/ApReqGrammar.java (original)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/apReq/ApReqGrammar.java Thu Nov 18 16:17:56 2010
@@ -26,6 +26,7 @@ import org.apache.directory.shared.asn1.
 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.apReq.actions.ApReqInit;
 import org.apache.directory.shared.kerberos.codec.apReq.actions.CheckMsgType;
 import org.apache.directory.shared.kerberos.codec.apReq.actions.StoreApOptions;
 import org.apache.directory.shared.kerberos.codec.apReq.actions.StoreAuthenticator;
@@ -73,7 +74,7 @@ public final class ApReqGrammar extends 
         // AP-REQ          ::= [APPLICATION 14]
         super.transitions[ApReqStatesEnum.START_STATE.ordinal()][KerberosConstants.AP_REQ_TAG] = new GrammarTransition(
             ApReqStatesEnum.START_STATE, ApReqStatesEnum.AP_REQ_STATE, KerberosConstants.AP_REQ_TAG,
-            new CheckNotNullLength() );
+            new ApReqInit() );
 
         // --------------------------------------------------------------------------------------------
         // Transition from AP-REQ tag to AP-REQ SEQ {

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/apReq/actions/ApReqInit.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/apReq/actions/ApReqInit.java?rev=1036514&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/apReq/actions/ApReqInit.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/apReq/actions/ApReqInit.java Thu Nov 18 16:17:56 2010
@@ -0,0 +1,77 @@
+/*
+ *  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.apReq.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.KerberosMessageGrammar;
+import org.apache.directory.shared.kerberos.codec.apReq.ApReqContainer;
+import org.apache.directory.shared.kerberos.messages.ApReq;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The action used to initialize the AP-REQ object
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ApReqInit extends GrammarAction
+{
+    /** The logger */
+    private static final Logger LOG = LoggerFactory.getLogger( KerberosMessageGrammar.class );
+
+    /**
+     * Instantiates a new ApReqInit action.
+     */
+    public ApReqInit()
+    {
+        super( "AP-REQ initialization" );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void action( Asn1Container container ) throws DecoderException
+    {
+        ApReqContainer apReqContainer = ( ApReqContainer ) container;
+
+        TLV tlv = apReqContainer.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 ) );
+        }
+        
+        // Create the AP-REQ now
+        ApReq apReq = new ApReq();
+        
+        apReqContainer.setApReq( apReq );
+    }
+}

Modified: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/messages/ApReq.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/messages/ApReq.java?rev=1036514&r1=1036513&r2=1036514&view=diff
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/messages/ApReq.java (original)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/messages/ApReq.java Thu Nov 18 16:17:56 2010
@@ -24,6 +24,7 @@ import java.nio.ByteBuffer;
 
 import org.apache.directory.server.i18n.I18n;
 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.kerberos.KerberosConstants;
@@ -72,6 +73,7 @@ public class ApReq extends KerberosMessa
     private transient int ticketLength;
     private transient int authenticatorLength;
     private transient int apReqLength;
+    private transient int apReqSeqLength;
 
     /**
      * Creates a new instance of ApplicationRequest.
@@ -189,25 +191,27 @@ public class ApReq extends KerberosMessa
      * 
      * 0x6E L1 AP-REQ [APPLICATION 14]
      *  |
-     *  +--> 0xA0 0x03 pvno tag
-     *  |     |
-     *  |     +--> 0x02 0x01 0x05 pvno (5)
-     *  |
-     *  +--> 0xA1 0x03 msg-type tag
-     *  |     |
-     *  |     +--> 0x02 0x01 0x0E msg-type (14)
-     *  |     
-     *  +--> 0xA2 0x03 APOptions tag
-     *  |     |
-     *  |     +--> 0x03 0x05 0x00 b1 b2 b3 b4 APOtions
-     *  |     
-     *  +--> 0xA3 L3 ticket tag
-     *  |     |
-     *  |     +--> 0x61 L3-1 ticket
-     *  |
-     *  +--> 0xA4 L4 authenticator tag
+     *  +--> 0x30 L2
+     *        |
+     *        +--> 0xA0 0x03 pvno tag
+     *        |     |
+     *        |     +--> 0x02 0x01 0x05 pvno (5)
      *        |
-     *        +--> 0x30 L4-1 authenticator (encrypted)
+     *        +--> 0xA1 0x03 msg-type tag
+     *        |     |
+     *        |     +--> 0x02 0x01 0x0E msg-type (14)
+     *        |     
+     *        +--> 0xA2 0x03 APOptions tag
+     *        |     |
+     *        |     +--> 0x03 0x05 0x00 b1 b2 b3 b4 APOtions
+     *        |     
+     *        +--> 0xA3 L3 ticket tag
+     *        |     |
+     *        |     +--> 0x61 L3-1 ticket
+     *        |
+     *        +--> 0xA4 L4 authenticator tag
+     *              |
+     *              +--> 0x30 L4-1 authenticator (encrypted)
      * </pre>
      */
     public int computeLength()
@@ -235,7 +239,9 @@ public class ApReq extends KerberosMessa
             1 + TLV.getNbBytes( ticketLength ) + ticketLength +
             1 + TLV.getNbBytes( authenticatorLength ) + authenticatorLength;
         
-        return 1 + TLV.getNbBytes( apReqLength ) + apReqLength;
+        apReqSeqLength = 1 + TLV.getNbBytes( apReqLength ) + apReqLength;
+        
+        return 1 + TLV.getNbBytes( apReqSeqLength ) + apReqSeqLength;
     }
 
     
@@ -257,6 +263,10 @@ public class ApReq extends KerberosMessa
         {
             // The AP-REP Tag
             buffer.put( (byte)KerberosConstants.AP_REQ_TAG );
+            buffer.put( TLV.getBytes( apReqSeqLength ) );
+            
+            // The AP-REP SEQ Tag
+            buffer.put( UniversalTag.SEQUENCE.getValue() );
             buffer.put( TLV.getBytes( apReqLength ) );
             
             // The PVNO -------------------------------------------------------

Added: directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/ApReqDecoderTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/ApReqDecoderTest.java?rev=1036514&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/ApReqDecoderTest.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/ApReqDecoderTest.java Thu Nov 18 16:17:56 2010
@@ -0,0 +1,230 @@
+/*
+ *  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 org.apache.directory.junit.tools.Concurrent;
+import org.apache.directory.junit.tools.ConcurrentJunitRunner;
+import org.apache.directory.shared.asn1.ber.Asn1Container;
+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.apReq.ApReqContainer;
+import org.apache.directory.shared.kerberos.messages.ApReq;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+
+/**
+ * Test the decoder for a ApReq
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrent()
+public class ApReqDecoderTest
+{
+    /**
+     * Test the decoding of a ApReq message
+     */
+    @Test
+    public void testDecodeFullApReq() throws Exception
+    {
+        Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+        ByteBuffer stream = ByteBuffer.allocate( 0x6C );
+        
+        stream.put( new byte[]
+        {
+          0x6E, 0x6A,
+            0x30, 0x68,
+              (byte)0xA0, 0x03,                 // pvno
+                0x02, 0x01, 0x05,
+              (byte)0xA1, 0x03,                 // msg-type
+                0x02, 0x01, 0x0E,
+              (byte)0xA2, 0x07,                 // APOptions
+                0x03, 0x05, 0x00, 0x60, 0x00, 0x00, 0x00,
+              (byte)0xA3, 0x40,                 // Ticket
+                0x61, 0x3E, 
+                  0x30, 0x3C, 
+                    (byte)0xA0, 0x03, 
+                      0x02, 0x01, 0x05, 
+                    (byte)0xA1, 0x0D, 
+                      0x1B, 0x0B, 
+                        'E', 'X', 'A', 'M', 'P', 'L', 'E', '.', 'C', 'O', 'M', 
+                    (byte)0xA2, 0x13, 
+                      0x30, 0x11, 
+                        (byte)0xA0, 0x03, 
+                          0x02, 0x01, 0x01, 
+                        (byte)0xA1, 0x0A, 
+                          0x30, 0x08, 
+                            0x1B, 0x06, 
+                              'c', 'l', 'i', 'e', 'n', 't', 
+                    (byte)0xA3, 0x11, 
+                      0x30, 0x0F, 
+                        (byte)0xA0, 0x03, 
+                          0x02, 0x01, 0x11, 
+                        (byte)0xA2, 0x08, 
+                          0x04, 0x06, 
+                            'a', 'b', 'c', 'd', 'e', 'f', 
+              (byte)0xA4, 0x11,                 // Authenticator
+                0x30, 0x0F, 
+                  (byte)0xA0, 0x03, 
+                    0x02, 0x01, 0x11, 
+                  (byte)0xA2, 0x08, 
+                    0x04, 0x06, 
+                      'a', 'b', 'c', 'd', 'e', 'f', 
+        });
+
+        stream.flip();
+
+        // Allocate a ApReq Container
+        ApReqContainer apReqContainer = new ApReqContainer();
+        apReqContainer.setStream( stream );
+        
+        // Decode the ApReq PDU
+        try
+        {
+            kerberosDecoder.decode( stream, apReqContainer );
+        }
+        catch ( DecoderException de )
+        {
+            fail( de.getMessage() );
+        }
+
+        ApReq apReq = apReqContainer.getApReq();
+        
+        assertTrue( apReq instanceof ApReq );
+        
+        // Check the encoding
+        int length = apReq.computeLength();
+
+        // Check the length
+        assertEquals( 0x6C, length );
+        
+        // Check the encoding
+        ByteBuffer encodedPdu = ByteBuffer.allocate( length );
+        
+        try
+        {
+            encodedPdu = apReq.encode( encodedPdu );
+    
+            // Check the length
+            assertEquals( 0x6C, encodedPdu.limit() );
+        }
+        catch ( EncoderException ee )
+        {
+            fail();
+        }
+    }
+    
+    
+    /**
+     * Test the decoding of a ApReq message with a bad MsgType
+     */
+    @Test( expected = DecoderException.class)
+    public void testDecodeFullApReqBadMsgType() throws Exception
+    {
+        Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+        ByteBuffer stream = ByteBuffer.allocate( 0x193 );
+        
+        stream.put( new byte[]
+        {
+            0x6E, 0x6A,
+            0x30, 0x68,
+              (byte)0xA0, 0x03,                 // pvno
+                0x02, 0x01, 0x05,
+              (byte)0xA1, 0x03,                 // msg-type (wrong...)
+                0x02, 0x01, 0x0D,
+              (byte)0xA2, 0x07,                 // APOptions
+                0x03, 0x05, 0x00, 0x60, 0x00, 0x00, 0x00,
+              (byte)0xA3, 0x40,                 // Ticket
+                0x61, 0x3E, 
+                  0x30, 0x3C, 
+                    (byte)0xA0, 0x03, 
+                      0x02, 0x01, 0x05, 
+                    (byte)0xA1, 0x0D, 
+                      0x1B, 0x0B, 
+                        'E', 'X', 'A', 'M', 'P', 'L', 'E', '.', 'C', 'O', 'M', 
+                    (byte)0xA2, 0x13, 
+                      0x30, 0x11, 
+                        (byte)0xA0, 0x03, 
+                          0x02, 0x01, 0x01, 
+                        (byte)0xA1, 0x0A, 
+                          0x30, 0x08, 
+                            0x1B, 0x06, 
+                              'c', 'l', 'i', 'e', 'n', 't', 
+                    (byte)0xA3, 0x11, 
+                      0x30, 0x0F, 
+                        (byte)0xA0, 0x03, 
+                          0x02, 0x01, 0x11, 
+                        (byte)0xA2, 0x08, 
+                          0x04, 0x06, 
+                            'a', 'b', 'c', 'd', 'e', 'f', 
+              (byte)0xA4, 0x11,                 // Authenticator
+                0x30, 0x0F, 
+                  (byte)0xA0, 0x03, 
+                    0x02, 0x01, 0x11, 
+                  (byte)0xA2, 0x08, 
+                    0x04, 0x06, 
+                      'a', 'b', 'c', 'd', 'e', 'f', 
+        });
+
+        stream.flip();
+
+        // Allocate a ApReq Container
+        ApReqContainer apReqContainer = new ApReqContainer();
+        apReqContainer.setStream( stream );
+        
+        // Decode the ApReq PDU
+        kerberosDecoder.decode( stream, apReqContainer );
+        fail();
+    }
+    
+    
+    /**
+     * Test the decoding of a AP-REQ with nothing in it
+     */
+    @Test( expected = DecoderException.class)
+    public void testApReqEmpty() throws DecoderException
+    {
+        Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+        ByteBuffer stream = ByteBuffer.allocate( 0x02 );
+        
+        stream.put( new byte[]
+            { 0x6A, 0x00 } );
+
+        stream.flip();
+
+        // Allocate a AP-REQ Container
+        Asn1Container apReqContainer = new ApReqContainer();
+
+        // Decode the AP-REQ PDU
+        kerberosDecoder.decode( stream, apReqContainer );
+        fail();
+    }
+}