You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by sm...@apache.org on 2006/05/30 12:16:23 UTC

svn commit: r410235 - in /incubator/harmony/enhanced/classlib/trunk/modules/security/src: main/java/common/org/apache/harmony/security/asn1/DerInputStream.java test/java/common/org/apache/harmony/security/asn1/der/ImplicitTest.java

Author: smishura
Date: Tue May 30 03:16:21 2006
New Revision: 410235

URL: http://svn.apache.org/viewvc?rev=410235&view=rev
Log:
Bug fix for DER decoding octet string. New regression tests for implicit tagging.

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/ImplicitTest.java   (contents, props changed)

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java?rev=410235&r1=410234&r2=410235&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/DerInputStream.java Tue May 30 03:16:21 2006
@@ -106,6 +106,19 @@
     }
 
     /**
+     * @see org.apache.harmony.security.asn1.BerInputStream#readOctetString()
+     */
+    public void readOctetString() throws IOException {
+
+        if ((tag & ASN1Constants.PC_CONSTRUCTED) != 0) {
+            throw new ASN1Exception(
+                    "ASN.1 octetstring: constructed identifier at ["
+                            + tagOffset + "]. Not valid for DER.");
+        }
+        super.readOctetString();
+    }
+
+    /**
      * @see org.apache.harmony.security.asn1.BerInputStream#readSequence(org.apache.harmony.security.asn1.ASN1Sequence)
      */
     public void readSequence(ASN1Sequence sequence) throws IOException {

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/ImplicitTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/ImplicitTest.java?rev=410235&r1=410234&r2=410235&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/ImplicitTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/ImplicitTest.java Tue May 30 03:16:21 2006
@@ -25,16 +25,18 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 
+import junit.framework.TestCase;
+
 import org.apache.harmony.security.asn1.ASN1Boolean;
 import org.apache.harmony.security.asn1.ASN1Constants;
+import org.apache.harmony.security.asn1.ASN1Exception;
 import org.apache.harmony.security.asn1.ASN1Implicit;
+import org.apache.harmony.security.asn1.ASN1OctetString;
 import org.apache.harmony.security.asn1.ASN1SequenceOf;
 import org.apache.harmony.security.asn1.ASN1Type;
 import org.apache.harmony.security.asn1.DerInputStream;
 import org.apache.harmony.security.asn1.DerOutputStream;
 
-import junit.framework.TestCase;
-
 
 /**
  * ASN.1 DER test for Implicitly tagged type
@@ -51,128 +53,117 @@
     private static ASN1SequenceOf sequence = new ASN1SequenceOf(ASN1Boolean
             .getInstance());
 
-    private static Object[][] taggedType;
-
-    protected void setUp() throws Exception {
-        super.setUp();
+    private static Object[][] taggedType = {
+    // format: object to encode / ASN.1 tagged type / byte array
 
-        taggedType = new Object[][] {
-                //format: object to encode / ASN.1 tagged type / byte array
-
-                //
-                // Boolean = false
-                //
-
-                // [UNIVERSAL 5] Boolean
-                new Object[] {
-                        Boolean.FALSE,
-                        new byte[] { 0x05, 0x01, 0x00 },
-                        new ASN1Implicit(ASN1Constants.CLASS_UNIVERSAL, 5,
-                                ASN1Boolean.getInstance()) },
-
-                // [APPLICATION 5] Boolean
-                new Object[] {
-                        Boolean.FALSE,
-                        new byte[] { 0x45, 0x01, 0x00 },
-                        new ASN1Implicit(ASN1Constants.CLASS_APPLICATION, 5,
-                                ASN1Boolean.getInstance()) },
-
-                // [CONTEXT-SPECIFIC 5] Boolean
-                new Object[] {
-                        Boolean.FALSE,
-                        new byte[] { (byte) 0x85, 0x01, 0x00 },
-                        new ASN1Implicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
-                                5, ASN1Boolean.getInstance()) },
-
-                // [5] Boolean (default = CONTEXT-SPECIFIC)
-                new Object[] { Boolean.FALSE,
-                        new byte[] { (byte) 0x85, 0x01, 0x00 },
-                        new ASN1Implicit(5, ASN1Boolean.getInstance()) },
-
-                // [PRIVATE 5] Boolean
-                new Object[] {
-                        Boolean.FALSE,
-                        new byte[] { (byte) 0xC5, 0x01, 0x00 },
-                        new ASN1Implicit(ASN1Constants.CLASS_PRIVATE, 5,
-                                ASN1Boolean.getInstance()) },
-
-                //
-                // Boolean = true
-                //
-
-                // [UNIVERSAL 5] Boolean
-                new Object[] {
-                        Boolean.TRUE,
-                        new byte[] { 0x05, 0x01, (byte) 0xFF },
-                        new ASN1Implicit(ASN1Constants.CLASS_UNIVERSAL, 5,
-                                ASN1Boolean.getInstance()) },
-
-                // [APPLICATION 5] Boolean
-                new Object[] {
-                        Boolean.TRUE,
-                        new byte[] { 0x45, 0x01, (byte) 0xFF },
-                        new ASN1Implicit(ASN1Constants.CLASS_APPLICATION, 5,
-                                ASN1Boolean.getInstance()) },
-
-                // [CONTEXT-SPECIFIC 5] Boolean
-                new Object[] {
-                        Boolean.TRUE,
-                        new byte[] { (byte) 0x85, 0x01, (byte) 0xFF },
-                        new ASN1Implicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
-                                5, ASN1Boolean.getInstance()) },
-
-                // [5] Boolean (default = CONTEXT-SPECIFIC)
-                new Object[] {
-                        Boolean.TRUE,
-                        new byte[] { (byte) 0x85, 0x01, (byte) 0xFF },
-                        new ASN1Implicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
-                                5, ASN1Boolean.getInstance()) },
-
-                // [PRIVATE 5] Boolean
-                new Object[] {
-                        Boolean.TRUE,
-                        new byte[] { (byte) 0xC5, 0x01, (byte) 0xFF },
-                        new ASN1Implicit(ASN1Constants.CLASS_PRIVATE, 5,
-                                ASN1Boolean.getInstance()) },
-                //
-                // SequenceOf - testing constructed ASN.1 type
-                //
-
-                // [UNIVERSAL 5] SequenceOf
-                new Object[] {
-                        new ArrayList(),
-                        new byte[] { 0x25, 0x00 },
-                        new ASN1Implicit(ASN1Constants.CLASS_UNIVERSAL, 5,
-                                sequence) },
-
-                // [APPLICATION 5] SequenceOf
-                new Object[] {
-                        new ArrayList(),
-                        new byte[] { 0x65, 0x00 },
-                        new ASN1Implicit(ASN1Constants.CLASS_APPLICATION, 5,
-                                sequence) },
-
-                // [CONTEXT-SPECIFIC 5] SequenceOf
-                new Object[] {
-                        new ArrayList(),
-                        new byte[] { (byte) 0xA5, 0x00 },
-                        new ASN1Implicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
-                                5, sequence) },
-
-                // [5] SequenceOf (default = CONTEXT-SPECIFIC)
-                new Object[] {
-                        new ArrayList(),
-                        new byte[] { (byte) 0xA5, 0x00 },
-                        new ASN1Implicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
-                                5, sequence) },
-
-                // [PRIVATE 5] SequenceOf
-                new Object[] {
-                        new ArrayList(),
-                        new byte[] { (byte) 0xE5, 0x00 },
-                        new ASN1Implicit(ASN1Constants.CLASS_PRIVATE, 5,
-                                sequence) } };
-    }
+            //
+            // Boolean = false
+            //
+
+            // [UNIVERSAL 5] Boolean
+            {
+                    Boolean.FALSE,
+                    new byte[] { 0x05, 0x01, 0x00 },
+                    new ASN1Implicit(ASN1Constants.CLASS_UNIVERSAL, 5,
+                            ASN1Boolean.getInstance()) },
+
+            // [APPLICATION 5] Boolean
+            {
+                    Boolean.FALSE,
+                    new byte[] { 0x45, 0x01, 0x00 },
+                    new ASN1Implicit(ASN1Constants.CLASS_APPLICATION, 5,
+                            ASN1Boolean.getInstance()) },
+
+            // [CONTEXT-SPECIFIC 5] Boolean
+            {
+                    Boolean.FALSE,
+                    new byte[] { (byte) 0x85, 0x01, 0x00 },
+                    new ASN1Implicit(ASN1Constants.CLASS_CONTEXTSPECIFIC, 5,
+                            ASN1Boolean.getInstance()) },
+
+            // [5] Boolean (default = CONTEXT-SPECIFIC)
+            { Boolean.FALSE, new byte[] { (byte) 0x85, 0x01, 0x00 },
+                    new ASN1Implicit(5, ASN1Boolean.getInstance()) },
+
+            // [PRIVATE 5] Boolean
+            {
+                    Boolean.FALSE,
+                    new byte[] { (byte) 0xC5, 0x01, 0x00 },
+                    new ASN1Implicit(ASN1Constants.CLASS_PRIVATE, 5,
+                            ASN1Boolean.getInstance()) },
+
+            //
+            // Boolean = true
+            //
+
+            // [UNIVERSAL 5] Boolean
+            {
+                    Boolean.TRUE,
+                    new byte[] { 0x05, 0x01, (byte) 0xFF },
+                    new ASN1Implicit(ASN1Constants.CLASS_UNIVERSAL, 5,
+                            ASN1Boolean.getInstance()) },
+
+            // [APPLICATION 5] Boolean
+            {
+                    Boolean.TRUE,
+                    new byte[] { 0x45, 0x01, (byte) 0xFF },
+                    new ASN1Implicit(ASN1Constants.CLASS_APPLICATION, 5,
+                            ASN1Boolean.getInstance()) },
+
+            // [CONTEXT-SPECIFIC 5] Boolean
+            {
+                    Boolean.TRUE,
+                    new byte[] { (byte) 0x85, 0x01, (byte) 0xFF },
+                    new ASN1Implicit(ASN1Constants.CLASS_CONTEXTSPECIFIC, 5,
+                            ASN1Boolean.getInstance()) },
+
+            // [5] Boolean (default = CONTEXT-SPECIFIC)
+            {
+                    Boolean.TRUE,
+                    new byte[] { (byte) 0x85, 0x01, (byte) 0xFF },
+                    new ASN1Implicit(ASN1Constants.CLASS_CONTEXTSPECIFIC, 5,
+                            ASN1Boolean.getInstance()) },
+
+            // [PRIVATE 5] Boolean
+            {
+                    Boolean.TRUE,
+                    new byte[] { (byte) 0xC5, 0x01, (byte) 0xFF },
+                    new ASN1Implicit(ASN1Constants.CLASS_PRIVATE, 5,
+                            ASN1Boolean.getInstance()) },
+            //
+            // SequenceOf - testing constructed ASN.1 type
+            //
+
+            // [UNIVERSAL 5] SequenceOf
+            {
+                    new ArrayList(),
+                    new byte[] { 0x25, 0x00 },
+                    new ASN1Implicit(ASN1Constants.CLASS_UNIVERSAL, 5, sequence) },
+
+            // [APPLICATION 5] SequenceOf
+            {
+                    new ArrayList(),
+                    new byte[] { 0x65, 0x00 },
+                    new ASN1Implicit(ASN1Constants.CLASS_APPLICATION, 5,
+                            sequence) },
+
+            // [CONTEXT-SPECIFIC 5] SequenceOf
+            {
+                    new ArrayList(),
+                    new byte[] { (byte) 0xA5, 0x00 },
+                    new ASN1Implicit(ASN1Constants.CLASS_CONTEXTSPECIFIC, 5,
+                            sequence) },
+
+            // [5] SequenceOf (default = CONTEXT-SPECIFIC)
+            {
+                    new ArrayList(),
+                    new byte[] { (byte) 0xA5, 0x00 },
+                    new ASN1Implicit(ASN1Constants.CLASS_CONTEXTSPECIFIC, 5,
+                            sequence) },
+
+            // [PRIVATE 5] SequenceOf
+            { new ArrayList(), new byte[] { (byte) 0xE5, 0x00 },
+                    new ASN1Implicit(ASN1Constants.CLASS_PRIVATE, 5, sequence) } };
 
     public void testDecode_Valid() throws IOException {
 
@@ -183,7 +174,7 @@
         }
     }
 
-    //FIXME need testcase for decoding invalid encodings
+    // FIXME need testcase for decoding invalid encodings
 
     public void testEncode() throws IOException {
 
@@ -192,6 +183,53 @@
                     (ASN1Type) taggedType[i][2], taggedType[i][0]);
             assertTrue("Test case: " + i, Arrays.equals(
                     (byte[]) taggedType[i][1], out.encoded));
+        }
+    }
+    
+    /**
+     * Tests 2 consecutive implicit string type tagging
+     * 
+     * TYPE1 = [1] IMPLICIT OCTET STRING
+     * TYPE2 = [2] IMPLICIT TYPE1
+     */
+    public void testConsecutiveStringTagging() throws Exception {
+        ASN1Implicit type1 = new ASN1Implicit(1, ASN1OctetString.getInstance());
+
+        ASN1Implicit type2 = new ASN1Implicit(2, type1);
+
+        byte[] primitiveEncoding = new byte[] {
+        // tag: class(CONTEXT SPECIFIC) + number (2) 
+                (byte) 0x82,
+                // length
+                0x03,
+                // value
+                0x00, 0x01, 0x02 };
+
+        byte[] constructedEncoding = new byte[] {
+        // tag: class(CONTEXT SPECIFIC) + constructed +number (2) 
+                (byte) 0xA2,
+                // length
+                0x00 };
+
+        byte[] array = new byte[] { 0x00, 0x01, 0x02 };
+
+        // decode primitive
+        assertTrue(Arrays.equals(array, (byte[]) type2
+                .decode(primitiveEncoding)));
+
+        // encode primitive
+        assertTrue(Arrays.equals(primitiveEncoding, (byte[]) type2
+                .encode(array)));
+
+        // decode constructed
+        try {
+            type2.decode(constructedEncoding);
+            fail("No expected ASN1Exception");
+        } catch (ASN1Exception e) {
+            // FIXME any other check instead of comparing the message???
+            assertEquals(
+                    "ASN.1 octetstring: constructed identifier at [0]. Not valid for DER.",
+                    e.getMessage());
         }
     }
 }

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/ImplicitTest.java
------------------------------------------------------------------------------
    svn:eol-style = native