harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smish...@apache.org
Subject svn commit: r395856 - in /incubator/harmony/enhanced/classlib/trunk/modules/security/src: main/java/common/java/security/cert/X509Certificate.java test/java/common/java/security/cert/X509CertificateTest.java
Date Fri, 21 Apr 2006 11:23:48 GMT
Author: smishura
Date: Fri Apr 21 04:23:47 2006
New Revision: 395856

URL: http://svn.apache.org/viewcvs?rev=395856&view=rev
Log:
Fixing default implementation of java.security.cert.X509Certificate + modifying regression
test.

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/cert/X509Certificate.java
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/java/security/cert/X509CertificateTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/cert/X509Certificate.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/cert/X509Certificate.java?rev=395856&r1=395855&r2=395856&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/cert/X509Certificate.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/cert/X509Certificate.java
Fri Apr 21 04:23:47 2006
@@ -21,17 +21,13 @@
 
 package java.security.cert;
 
+import java.io.ByteArrayInputStream;
 import java.math.BigInteger;
 import java.security.Principal;
-import java.security.cert.Certificate;
-import java.security.cert.CertificateEncodingException;
-import java.security.cert.CertificateExpiredException;
-import java.security.cert.CertificateNotYetValidException;
-import java.security.cert.CertificateParsingException;
-import java.security.cert.X509Extension;
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
+
 import javax.security.auth.x500.X500Principal;
 
 /**
@@ -78,7 +74,21 @@
      * @com.intel.drl.spec_ref
      */
     public X500Principal getIssuerX500Principal() {
-        throw new RuntimeException("Method should be overriden.");
+
+        try {
+            // TODO if there is no X.509 certificate provider installed
+            // should we try to access Harmony X509CertImpl via classForName?
+            CertificateFactory factory = CertificateFactory
+                    .getInstance("X.509");
+
+            X509Certificate cert = (X509Certificate) factory
+                    .generateCertificate(new ByteArrayInputStream(getEncoded()));
+
+            return cert.getIssuerX500Principal();
+
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to get X500Principal issuer", e);
+        }
     }
 
     /**
@@ -90,7 +100,21 @@
      * @com.intel.drl.spec_ref
      */
     public X500Principal getSubjectX500Principal() {
-        throw new RuntimeException("Method should be overriden.");
+
+        try {
+            // TODO if there is no X.509 certificate provider installed
+            // should we try to access Harmony X509CertImpl via classForName?
+            CertificateFactory factory = CertificateFactory
+                    .getInstance("X.509");
+
+            X509Certificate cert = (X509Certificate) factory
+                    .generateCertificate(new ByteArrayInputStream(getEncoded()));
+
+            return cert.getSubjectX500Principal();
+
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to get X500Principal subject", e);
+        }
     }
 
     /**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/java/security/cert/X509CertificateTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/java/security/cert/X509CertificateTest.java?rev=395856&r1=395855&r2=395856&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/java/security/cert/X509CertificateTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/java/security/cert/X509CertificateTest.java
Fri Apr 21 04:23:47 2006
@@ -28,14 +28,11 @@
 import java.security.Principal;
 import java.security.PublicKey;
 import java.security.SignatureException;
-import java.security.cert.CertificateEncodingException;
-import java.security.cert.CertificateException;
-import java.security.cert.CertificateExpiredException;
-import java.security.cert.CertificateNotYetValidException;
-import java.security.cert.X509Certificate;
 import java.util.Date;
 import java.util.Set;
 
+import javax.security.auth.x500.X500Principal;
+
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
@@ -45,6 +42,72 @@
  */
 public class X509CertificateTest extends TestCase {
 
+    // certificate encoding was created by hands
+    // according to X.509 Certificate ASN.1 notation
+    private static final byte[] x509CertEnc = new byte[] {
+    // Certificate: SEQUENCE
+            0x30, 0x6B,
+
+            //
+            // TBSCertificate: SEQUENCE {
+            //
+            0x30, 0x5C,
+
+            // version: [0] EXPLICIT Version DEFAULT v1
+            (byte) 0xA0, 0x03, 0x02, 0x01, 0x00,
+
+            // serialNumber: CertificateSerialNumber
+            0x02, 0x01, 0x05,
+
+            // signature: AlgorithmIdentifier
+            0x30, 0x07, // SEQUENCE
+            0x06, 0x02, 0x03, 0x05,//OID
+            0x01, 0x01, 0x07, //ANY
+
+            //issuer: Name
+            0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03,
+            0x13, 0x01, 0x5A, // CN=Z
+
+            //validity: Validity
+            0x30, 0x1E, // SEQUENCE
+            // notBefore: UTCTime
+            0x17, 0x0D, 0x39, 0x39, 0x31, 0x32, 0x31, 0x33, 0x31, 0x34, 0x31,
+            0x35, 0x31, 0x36, 0x5A, // 13 Dec 1999 14:15:16
+            // notAfter:  UTCTime
+            0x17, 0x0D, 0x30, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30,
+            0x30, 0x30, 0x30, 0x5A, // 01 Jan 2000 00:00:00
+
+            //subject: Name
+            0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03,
+            0x13, 0x01, 0x59, // CN=Y
+
+            //SubjectPublicKeyInfo  ::=  SEQUENCE  {
+            //    algorithm            AlgorithmIdentifier,
+            //    subjectPublicKey     BIT STRING  }
+            0x30, 0x0D, // SEQUENCE
+            0x30, 0x07, // SEQUENCE
+            0x06, 0x02, 0x03, 0x05,//OID
+            0x01, 0x01, 0x07, //ANY
+            0x03, 0x02, 0x00, 0x01, // subjectPublicKey
+
+            // issuerUniqueID - missed
+            // subjectUniqueID - missed
+            // extensions - missed
+
+            // } end TBSCertificate
+
+            //
+            // signatureAlgorithm: AlgorithmIdentifier
+            //
+            0x30, 0x07, // SEQUENCE
+            0x06, 0x02, 0x03, 0x05,//OID
+            0x01, 0x01, 0x07, //ANY
+
+            //
+            // signature: BIT STRING  
+            //
+            0x03, 0x02, 0x00, 0x01 };
+
     // has stub implementation for abstract methods
     private static class MyX509Certificate extends X509Certificate {
 
@@ -171,27 +234,31 @@
     }
 
     /**
-     * getIssuerX500Principal() method testing.
+     * @tests java.security.cert.X509Certificate#getIssuerX500Principal()
      */
     public void testGetIssuerX500Principal() {
-        try {
-            new MyX509Certificate().getIssuerX500Principal();
-            fail("Runtime exception should be thrown "
-                        + "in default implementation");
-        } catch (RuntimeException e) {
-        }
+        // return valid encoding
+        MyX509Certificate cert = new MyX509Certificate() {
+            public byte[] getEncoded() {
+                return x509CertEnc;
+            };
+        };
+
+        assertEquals(new X500Principal("CN=Z"), cert.getIssuerX500Principal());
     }
 
     /**
-     * getSubjectX500Principal() method testing.
+     * @tests java.security.cert.X509Certificate#getSubjectX500Principal()
      */
     public void testGetSubjectX500Principal() {
-        try {
-            new MyX509Certificate().getSubjectX500Principal();
-            fail("Runtime exception should be thrown "
-                        + "in default implementation");
-        } catch (RuntimeException e) {
-        }
+        // return valid encoding
+        MyX509Certificate cert = new MyX509Certificate() {
+            public byte[] getEncoded() {
+                return x509CertEnc;
+            };
+        };
+
+        assertEquals(new X500Principal("CN=Y"), cert.getSubjectX500Principal());
     }
 
     /**



Mime
View raw message