harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smish...@apache.org
Subject svn commit: r471999 - in /incubator/harmony/enhanced/classlib/trunk/modules/security/src: main/java/common/org/apache/harmony/security/provider/cert/ test/impl/java/org/apache/harmony/security/tests/provider/cert/
Date Tue, 07 Nov 2006 06:07:22 GMT
Author: smishura
Date: Mon Nov  6 22:07:21 2006
New Revision: 471999

URL: http://svn.apache.org/viewvc?view=rev&rev=471999
Log:
Encoder for PKCS#7 SignedData object + regression test

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/provider/cert/X509CertPathImpl.java
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/provider/cert/X509CertPathImplTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/provider/cert/X509CertPathImpl.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/provider/cert/X509CertPathImpl.java?view=diff&rev=471999&r1=471998&r2=471999
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/provider/cert/X509CertPathImpl.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/provider/cert/X509CertPathImpl.java
Mon Nov  6 22:07:21 2006
@@ -36,7 +36,12 @@
 import java.util.List;
 
 import org.apache.harmony.security.asn1.ASN1Any;
+import org.apache.harmony.security.asn1.ASN1Explicit;
+import org.apache.harmony.security.asn1.ASN1Implicit;
+import org.apache.harmony.security.asn1.ASN1Oid;
+import org.apache.harmony.security.asn1.ASN1Sequence;
 import org.apache.harmony.security.asn1.ASN1SequenceOf;
+import org.apache.harmony.security.asn1.ASN1Type;
 import org.apache.harmony.security.asn1.BerInputStream;
 import org.apache.harmony.security.internal.nls.Messages;
 import org.apache.harmony.security.pkcs7.ContentInfo;
@@ -288,13 +293,9 @@
         } else {
             // PKCS7 encoded form
             if (pkcs7Encoding == null) {
-                SignedData sd = new SignedData(1, new ArrayList(),
-                        new ContentInfo(ContentInfo.DATA, null), certificates,
-                        null, new ArrayList());
-                ContentInfo ci = new ContentInfo(ContentInfo.SIGNED_DATA, sd);
-                pkcs7Encoding = ci.getEncoded();
+                pkcs7Encoding = PKCS7_SIGNED_DATA_OBJECT.encode(this);
             }
-            byte[] result = new byte[pkiPathEncoding.length];
+            byte[] result = new byte[pkcs7Encoding.length];
             System.arraycopy(pkcs7Encoding, 0, result, 0,
                                         pkcs7Encoding.length);
             return result;
@@ -363,6 +364,69 @@
                 throw new IllegalArgumentException(Messages.getString("security.161")); //$NON-NLS-1$
             }
             return encodings;
+        }
+    };
+    
+
+    //
+    // encoder for PKCS#7 SignedData
+    // it is assumed that only certificate field is important
+    // all other fields contain precalculated encodings:
+    //
+    // encodes X509CertPathImpl objects
+    //
+    private static final ASN1Sequence ASN1_SIGNED_DATA = new ASN1Sequence(
+            new ASN1Type[] {
+                    // version ,digestAlgorithms, content info
+                    ASN1Any.getInstance(),
+                    // certificates
+                    new ASN1Implicit(0, ASN1),
+                    // set of crls is optional and is missed here
+                    ASN1Any.getInstance(),// signers info
+            }) {
+
+        // precalculated ASN.1 encodings for
+        // version ,digestAlgorithms, content info field of SignedData
+        private final byte[] PRECALCULATED_HEAD = new byte[] { 0x02, 0x01,
+                0x01,// version (v1)
+                0x31, 0x00,// empty set of DigestAlgorithms
+                0x30, 0x03, 0x06, 0x01, 0x00 // empty ContentInfo with oid=0
+        };
+
+        // precalculated empty set of SignerInfos
+        private final byte[] SIGNERS_INFO = new byte[] { 0x31, 0x00 };
+
+        protected void getValues(Object object, Object[] values) {
+            values[0] = PRECALCULATED_HEAD;
+            values[1] = object; // pass X509CertPathImpl object
+            values[2] = SIGNERS_INFO;
+        }
+
+        // stub to prevent using the instance as decoder
+        public Object decode(BerInputStream in) throws IOException {
+            throw new RuntimeException(
+                    "Invalid use of encoder for PKCS#7 SignedData object");
+        }
+    };
+    
+    private static final ASN1Sequence PKCS7_SIGNED_DATA_OBJECT = new ASN1Sequence(
+            new ASN1Type[] { ASN1Any.getInstance(), // contentType
+                    new ASN1Explicit(0, ASN1_SIGNED_DATA) // SignedData
+            }) {
+
+        // precalculated ASN.1 encoding for SignedData object oid
+        private final byte[] SIGNED_DATA_OID = ASN1Oid.getInstance().encode(
+                ContentInfo.SIGNED_DATA);
+
+        protected void getValues(Object object, Object[] values) {
+            values[0] = SIGNED_DATA_OID;
+            values[1] = object; // pass X509CertPathImpl object
+        }
+
+        // stub to prevent using the instance as decoder
+        public Object decode(BerInputStream in) throws IOException {
+            throw new RuntimeException(
+                    "Invalid use of encoder for PKCS#7 SignedData object");
         }
     };
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/provider/cert/X509CertPathImplTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/provider/cert/X509CertPathImplTest.java?view=diff&rev=471999&r1=471998&r2=471999
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/provider/cert/X509CertPathImplTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/provider/cert/X509CertPathImplTest.java
Mon Nov  6 22:07:21 2006
@@ -23,8 +23,10 @@
 package org.apache.harmony.security.tests.provider.cert;
 
 import java.io.ByteArrayInputStream;
+import java.security.cert.CertPath;
 import java.security.cert.CertificateEncodingException;
 import java.security.cert.CertificateException;
+import java.security.cert.CertificateFactory;
 import java.security.cert.X509Certificate;
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -110,7 +112,36 @@
                     + e.getMessage());
         }
     }
-        
+
+    /**
+     * @tests org.apache.harmony.security.provider.cert.getInstance(byte[], java.lang.String)
+     */
+    public void test_getInstance$BLjava_lang_String() throws Exception {
+
+        // Test: getInstance(byte[] in, "PKCS7")
+        // reconverting of the encoded form: from default (PkiPath) to PKCS7
+        byte[] encoding = certPath.getEncoded();
+
+        CertificateFactory factory = CertificateFactory.getInstance("X.509");
+
+        ByteArrayInputStream bais = new ByteArrayInputStream(encoding);
+
+        CertPath cert_path = factory.generateCertPath(bais);
+
+        encoding = cert_path.getEncoded("PKCS7");
+
+        X509CertPathImpl cpath = X509CertPathImpl
+                .getInstance(encoding, "PKCS7");
+        assertEquals("Certificate list size missmatch", certList.size(), cpath
+                .getCertificates().size());
+
+        bais = new ByteArrayInputStream(encoding);
+
+        cpath = X509CertPathImpl.getInstance(bais, "PKCS7");
+        assertEquals("Certificate list size missmatch", certList.size(), cpath
+                .getCertificates().size());
+    }
+
     /**
      * getCertificates() method testing.
      */



Mime
View raw message