harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r399648 [1/2] - in /incubator/harmony/enhanced/classlib/trunk: make/patternsets/ modules/archive/META-INF/ modules/archive/src/main/java/java/util/jar/ modules/luni/META-INF/ modules/luni/src/main/java/org/apache/harmony/luni/internal/net/w...
Date Thu, 04 May 2006 10:13:27 GMT
Author: tellison
Date: Thu May  4 03:13:20 2006
New Revision: 399648

URL: http://svn.apache.org/viewcvs?rev=399648&view=rev
Log:
Move BASE64 encoding from a SECURITY util to a LUNI util to improve modularity.

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/Base64.java
      - copied, changed from r399638, incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/misc/Base64.java
Removed:
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/misc/
Modified:
    incubator/harmony/enhanced/classlib/trunk/make/patternsets/security.txt
    incubator/harmony/enhanced/classlib/trunk/modules/archive/META-INF/MANIFEST.MF
    incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarVerifier.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/META-INF/MANIFEST.MF
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
    incubator/harmony/enhanced/classlib/trunk/modules/prefs/META-INF/MANIFEST.MF
    incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/AbstractPreferences.java
    incubator/harmony/enhanced/classlib/trunk/modules/security/META-INF/MANIFEST.MF
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/provider/cert/X509CertFactoryImpl.java
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/java/security/cert/X509CertificateTest.java
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/support/provider/cert/CertFactoryTestData.java

Modified: incubator/harmony/enhanced/classlib/trunk/make/patternsets/security.txt
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/make/patternsets/security.txt?rev=399648&r1=399647&r2=399648&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/make/patternsets/security.txt (original)
+++ incubator/harmony/enhanced/classlib/trunk/make/patternsets/security.txt Thu May  4 03:13:20 2006
@@ -1,22 +1,22 @@
-# Copyright 2005 The Apache Software Foundation or its licensors, as applicable
-# 
-# Licensed 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.
-
-java/security/*
-java/security/acl/*
-java/security/cert/*
-java/security/interfaces/*
-java/security/spec/*
-javax/security/cert/*
-org/apache/harmony/security/**/*
-org/apache/harmony/misc/*
+# Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+# 
+# Licensed 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.
+
+java/security/*
+java/security/acl/*
+java/security/cert/*
+java/security/interfaces/*
+java/security/spec/*
+javax/security/cert/*
+org/apache/harmony/security/**/*
+

Modified: incubator/harmony/enhanced/classlib/trunk/modules/archive/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/META-INF/MANIFEST.MF?rev=399648&r1=399647&r2=399648&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/META-INF/MANIFEST.MF (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/META-INF/MANIFEST.MF Thu May  4 03:13:20 2006
@@ -18,7 +18,6 @@
  java.util,
  javax.security.auth.x500,
  org.apache.harmony.luni.util,
- org.apache.harmony.misc,
  org.apache.harmony.security.utils,
  tests.support;hy_usage=test;resolution:=optional,
  tests.support.resource;hy_usage=test;resolution:=optional

Modified: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarVerifier.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarVerifier.java?rev=399648&r1=399647&r2=399648&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarVerifier.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarVerifier.java Thu May  4 03:13:20 2006
@@ -32,10 +32,10 @@
 import java.util.Vector;
 import java.util.zip.ZipEntry;
 
-import org.apache.harmony.misc.Base64;
 
 import org.apache.harmony.security.utils.JarUtils;
 
+import org.apache.harmony.luni.util.Base64;
 import org.apache.harmony.luni.util.Msg;
 
 /**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/META-INF/MANIFEST.MF?rev=399648&r1=399647&r2=399648&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/META-INF/MANIFEST.MF (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/META-INF/MANIFEST.MF Thu May  4 03:13:20 2006
@@ -18,7 +18,6 @@
  java.util.regex,
  java.util.zip,
  javax.net.ssl,
- org.apache.harmony.misc,
  org.apache.harmony.nio,
  tests.support;resolution:=optional;hy_usage=test,
  tests.support.resource;resolution:=optional;hy_usage=test,

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java?rev=399648&r1=399647&r2=399648&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java Thu May  4 03:13:20 2006
@@ -941,7 +941,7 @@
 			connected = false;
 			// base64 encode the username and password
 			byte[] bytes = (pa.getUserName() + ":" + new String(pa.getPassword())).getBytes("ISO8859_1");
-			String encoded = org.apache.harmony.misc.Base64.encode(bytes, "ISO8859_1");
+			String encoded = org.apache.harmony.luni.util.Base64.encode(bytes, "ISO8859_1");
 			setRequestProperty("Authorization", scheme + " " + encoded);
 			continue;
 		}

Copied: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/Base64.java (from r399638, incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/misc/Base64.java)
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/Base64.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/Base64.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/misc/Base64.java&r1=399638&r2=399648&rev=399648&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/misc/Base64.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/Base64.java Thu May  4 03:13:20 2006
@@ -19,7 +19,7 @@
 * @version $Revision$
 */
 
-package org.apache.harmony.misc;
+package org.apache.harmony.luni.util;
 
 import java.io.UnsupportedEncodingException;
 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/prefs/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/prefs/META-INF/MANIFEST.MF?rev=399648&r1=399647&r2=399648&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/prefs/META-INF/MANIFEST.MF (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/prefs/META-INF/MANIFEST.MF Thu May  4 03:13:20 2006
@@ -17,7 +17,6 @@
  javax.xml.parsers,
  javax.xml.transform,
  org.apache.harmony.luni.util,
- org.apache.harmony.misc,
  org.apache.xpath,
  org.w3c.dom,
  org.xml.sax,

Modified: incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/AbstractPreferences.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/AbstractPreferences.java?rev=399648&r1=399647&r2=399648&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/AbstractPreferences.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/AbstractPreferences.java Thu May  4 03:13:20 2006
@@ -25,7 +25,7 @@
 import java.util.StringTokenizer;
 import java.util.TreeSet;
 
-import org.apache.harmony.misc.Base64;
+import org.apache.harmony.luni.util.Base64;
 
 /**
  * This class is partly implementation of <code>Preferences</code>, which can be 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/META-INF/MANIFEST.MF?rev=399648&r1=399647&r2=399648&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/META-INF/MANIFEST.MF (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/META-INF/MANIFEST.MF Thu May  4 03:13:20 2006
@@ -23,6 +23,7 @@
  javax.security.auth.login,
  javax.security.auth.x500,
  org.apache.harmony.crypto.utils,
+ org.apache.harmony.luni.util,
  tests.support;resolution:=optional;hy_usage=test,
  tests.support.resource;resolution:=optional;hy_usage=test
 Export-Package: java.security,
@@ -31,7 +32,6 @@
  java.security.interfaces,
  java.security.spec,
  javax.security.cert,
- org.apache.harmony.misc,
  org.apache.harmony.security;version="1.0.0",
  org.apache.harmony.security.asn1;version="1.0.0",
  org.apache.harmony.security.fortress;version="1.0.0",

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/provider/cert/X509CertFactoryImpl.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/provider/cert/X509CertFactoryImpl.java?rev=399648&r1=399647&r2=399648&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/provider/cert/X509CertFactoryImpl.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/provider/cert/X509CertFactoryImpl.java Thu May  4 03:13:20 2006
@@ -1,368 +1,368 @@
-/*
- *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
- *
- *  Licensed 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.
- */
-
-/**
-* @author Alexander Y. Kleymenov
-* @version $Revision$
-*/
-
-package org.apache.harmony.security.provider.cert;
-
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.security.cert.CRL;
-import java.security.cert.CRLException;
-import java.security.cert.CertPath;
-import java.security.cert.Certificate;
-import java.security.cert.CertificateException;
-import java.security.cert.CertificateFactorySpi;
-import java.security.cert.X509CRL;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-
-import org.apache.harmony.misc.Base64;
-import org.apache.harmony.security.asn1.BerInputStream;
-
-/**
- * X509CertFactoryImpl
- */
-public class X509CertFactoryImpl extends CertificateFactorySpi {
-
-    private static Cache CERT_CASHE = new Cache();
-    private static Cache CRL_CASHE = new Cache();
-    
-    public X509CertFactoryImpl() {}
-
-    /**
-     * engineGenerateCertificate
-     */
-    public Certificate engineGenerateCertificate(InputStream inStream)
-            throws CertificateException {
-        try {
-            if (inStream.markSupported()) {
-            } else {
-                inStream = new RestoringInputStream(inStream);
-            }
-            inStream.mark(32);
-            byte[] buff = new byte[28];
-            if (inStream.read(buff) < 28) {
-                throw new CertificateException(
-                        "Input Stream contains not enought data.");
-            }
-            if ("-----BEGIN CERTIFICATE-----".equals(new String(buff, 0, 27))) {
-                int size = inStream.available();
-                if (size == 0) {
-                    size = 2048;
-                }
-                buff = new byte[size];
-                int index=0, ch;
-                while ((ch = inStream.read()) != '-') {
-                    if (ch == -1) {
-                        throw new CertificateException(
-                                "Incorrect Base64 encoding: unexpected EOF.");
-                    }
-                    buff[index++] = (byte) ch;
-                    if (index == size) {
-                        byte[] newbuff = new byte[size+1024];
-                        System.arraycopy(buff, 0, newbuff, 0, size);
-                        buff = newbuff;
-                    }
-                }
-                byte[] tmp = new byte[25];
-                inStream.read(tmp);
-                if (!new String(tmp).startsWith("----END CERTIFICATE-----")) {
-                    throw new CertificateException(
-                    "Incorrect Base64 encoding: 'END CERTIFICATE' expected.");
-                }
-                // skip new line: set the position to the next certificate:
-                inStream.mark(1);
-                while (((ch = inStream.read()) != -1) && (ch == '\n' || ch == '\r')) {
-                    inStream.mark(1);
-                }
-                inStream.reset();
-                buff = Base64.decode(buff, index);
-                if (buff == null) {
-                    throw new CertificateException("Incorrect Base64 encoding.");
-                }
-                long hash = CERT_CASHE.getHash(buff);
-                if (CERT_CASHE.contains(hash)) {
-                    Certificate res = (Certificate) CERT_CASHE.get(hash, buff);
-                    if (res != null) {
-                        return res;
-                    }
-                }
-                Certificate res = new X509CertImpl(buff);
-                CERT_CASHE.put(hash, buff, res);
-                return res;
-            } else {
-                inStream.reset();
-                long hash = CERT_CASHE.getHash(buff);
-                if (CERT_CASHE.contains(hash)) {
-                    byte[] encoding = new byte[BerInputStream.getLength(buff)];
-                    inStream.read(encoding);
-                    Certificate res = 
-                        (Certificate) CERT_CASHE.get(hash, encoding);
-                    if (res != null) {
-                        return res;
-                    }
-                    res = new X509CertImpl(encoding);
-                    CERT_CASHE.put(hash, encoding, res);
-                    return res;
-                } else {
-                    Certificate res = new X509CertImpl(inStream);
-                    CERT_CASHE.put(hash, res.getEncoded(), res);
-                    return res;
-                }
-            }
-        } catch (IOException e) {
-            throw new CertificateException(e);
-        }
-    }
-    
-    /**
-     * engineGenerateCertificates
-     * FIXME: 1.5 updates are needed Collection <? extends Certificate>
-     */
-    public Collection engineGenerateCertificates(InputStream inStream)
-            throws CertificateException {
-        ArrayList result = new ArrayList();
-        try {
-            if (inStream.markSupported()) {
-            } else {
-                inStream = new RestoringInputStream(inStream);
-            }
-            inStream.mark(1);
-            // FIXME: Check if it is a PKCS7 structure, if not, do following:
-            while (inStream.read() != -1) {
-                inStream.reset();
-                result.add(engineGenerateCertificate(inStream));
-                inStream.mark(1);
-            }
-        } catch (IOException e) {
-            e.printStackTrace();
-            throw new CertificateException(e);
-        }
-        return result;
-    }
-
-    /**
-     * engineGenerateCRL
-     */
-    public CRL engineGenerateCRL(InputStream inStream)
-            throws CRLException {
-        try {
-            if (inStream.markSupported()) {
-            } else {
-                inStream = new RestoringInputStream(inStream);
-            }
-            inStream.mark(32);
-            byte[] buff = new byte[28];
-            if (inStream.read(buff) < 28) {
-                throw new CRLException(
-                        "Input Stream contains not enought data.");
-            }
-
-            inStream.reset();
-            long hash = CRL_CASHE.getHash(buff);
-            if (CRL_CASHE.contains(hash)) {
-                byte[] encoding = new byte[BerInputStream.getLength(buff)];
-                inStream.read(encoding);
-                CRL res = 
-                    (CRL) CRL_CASHE.get(hash, encoding);
-                if (res != null) {
-                    return res;
-                }
-                res = new X509CRLImpl(encoding);
-                CRL_CASHE.put(hash, encoding, res);
-                return res;
-            } else {
-                X509CRL res = new X509CRLImpl(inStream);
-                CRL_CASHE.put(hash, res.getEncoded(), res);
-                return res;
-            }
-        } catch (IOException e) {
-            throw new CRLException(e);
-        }
-    }
-
-    /**
-     * engineGenerateCRLs
-     * FIXME: 1.5 updates are needed Collection <? extends CRL>
-     */
-    public Collection engineGenerateCRLs(InputStream inStream)
-            throws CRLException {
-        if (inStream == null) {
-            throw new CRLException("Null input stream provided.");
-        }
-        ArrayList result = new ArrayList();
-        try {
-            if (inStream.markSupported()) {
-            } else {
-                inStream = new RestoringInputStream(inStream);
-            }
-            inStream.mark(1);
-            // FIXME: Check if it is a PKCS7 structure, if not, do following:
-            while (inStream.read() != -1) {
-                inStream.reset();
-                result.add(engineGenerateCRL(inStream));
-                inStream.mark(1);
-            }
-        } catch (IOException e) {
-            e.printStackTrace();
-            throw new CRLException(e);
-        }
-        return result;
-    }
-
-    /**
-     * engineGenerateCertPath
-     */
-    public CertPath engineGenerateCertPath(InputStream inStream)
-            throws CertificateException {
-        return X509CertPathImpl.getInstance(inStream);
-    }
-
-    /**
-     * engineGenerateCertPath
-     */
-    public CertPath engineGenerateCertPath(InputStream inStream, String encoding)
-            throws CertificateException {
-        return X509CertPathImpl.getInstance(inStream, encoding);
-    }
-
-    /**
-     * engineGenerateCertPath
-     */
-    public CertPath engineGenerateCertPath(List certificates)
-            throws CertificateException {
-        return new X509CertPathImpl(certificates);
-    }
-
-    /**
-     * engineGetCertPathEncodings
-     * FIXME: 1.5 updates are needed Iterator <String>
-     */
-    public Iterator engineGetCertPathEncodings() {
-        return X509CertPathImpl.encodings.iterator();
-    }
-
-    /**
-     * Class represents the stream which supports reset to the
-     * marked state with readlimit == BUFF_SIZE.
-     */
-    private static class RestoringInputStream extends InputStream {
-
-        private final InputStream inStream;
-        private final static int BUFF_SIZE = 32;
-        private final int[] buff = new int[BUFF_SIZE*2];
-        // position in the buffer
-        private int pos = -1;
-        // the last byte in the buffer
-        private int bar = 0;
-        // the last cell of the buffer
-        private int end = 0;
-        
-        public RestoringInputStream(InputStream inStream) {
-            this.inStream = inStream;
-        }
-
-        public int available() throws IOException {
-            return (bar - pos) + inStream.available();
-        }
-
-        public void close() throws IOException {
-            inStream.close();
-        }
-
-        public void mark(int readlimit) {
-            if (pos < 0) {
-                pos = 0;
-                bar = 0;
-                end = BUFF_SIZE - 1;
-            } else {
-                end = (pos + BUFF_SIZE - 1) % BUFF_SIZE;
-            }
-        }
-
-        public boolean markSupported() {
-            return true;
-        }
-
-        public int read() throws IOException {
-            if (pos >= 0) {
-                int cur = pos % BUFF_SIZE;
-                if (cur < bar) {
-                    pos++;
-                    return buff[cur];
-                }
-                if (cur != end) {
-                    buff[cur] = inStream.read();
-                    bar = cur+1;
-                    pos++;
-                    return buff[cur];
-                } else {
-                    pos = -1; // can not operate anymore
-                }
-            }
-            return inStream.read();
-        }
-
-        public int read(byte[] b) throws IOException {
-            return read(b, 0, b.length);
-        }
-
-        public int read(byte[] b, int off, int len) throws IOException {
-            int read_b;
-            int i;
-            for (i=0; i<len; i++) {
-                if ((read_b = read()) == -1) {
-                    return (i == 0) ? -1 : i; 
-                }
-                b[off+i] = (byte) read_b;
-            }
-            return i;
-        }
-
-        public void reset() throws IOException {
-            if (pos >= 0) {
-                pos = (end + 1) % BUFF_SIZE;
-            } else {
-                throw new IOException("Could not reset the stream: "
-                    + "position became invalid or stream has not been marked.");
-            }
-        }
-
-        public long skip(long n) throws IOException {
-            if (pos >= 0) {
-                long i = 0;
-                int av = available();
-                if (av < n) {
-                    n = av;
-                }
-                while ((i < n) && (read() != -1)) {
-                    i++;
-                }
-                return i;
-            } else {
-                return inStream.skip(n);
-            }
-        }
-    }
-}
-
+/*
+ *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed 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.
+ */
+
+/**
+* @author Alexander Y. Kleymenov
+* @version $Revision$
+*/
+
+package org.apache.harmony.security.provider.cert;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.cert.CRL;
+import java.security.cert.CRLException;
+import java.security.cert.CertPath;
+import java.security.cert.Certificate;
+import java.security.cert.CertificateException;
+import java.security.cert.CertificateFactorySpi;
+import java.security.cert.X509CRL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.harmony.luni.util.Base64;
+import org.apache.harmony.security.asn1.BerInputStream;
+
+/**
+ * X509CertFactoryImpl
+ */
+public class X509CertFactoryImpl extends CertificateFactorySpi {
+
+    private static Cache CERT_CASHE = new Cache();
+    private static Cache CRL_CASHE = new Cache();
+    
+    public X509CertFactoryImpl() {}
+
+    /**
+     * engineGenerateCertificate
+     */
+    public Certificate engineGenerateCertificate(InputStream inStream)
+            throws CertificateException {
+        try {
+            if (inStream.markSupported()) {
+            } else {
+                inStream = new RestoringInputStream(inStream);
+            }
+            inStream.mark(32);
+            byte[] buff = new byte[28];
+            if (inStream.read(buff) < 28) {
+                throw new CertificateException(
+                        "Input Stream contains not enought data.");
+            }
+            if ("-----BEGIN CERTIFICATE-----".equals(new String(buff, 0, 27))) {
+                int size = inStream.available();
+                if (size == 0) {
+                    size = 2048;
+                }
+                buff = new byte[size];
+                int index=0, ch;
+                while ((ch = inStream.read()) != '-') {
+                    if (ch == -1) {
+                        throw new CertificateException(
+                                "Incorrect Base64 encoding: unexpected EOF.");
+                    }
+                    buff[index++] = (byte) ch;
+                    if (index == size) {
+                        byte[] newbuff = new byte[size+1024];
+                        System.arraycopy(buff, 0, newbuff, 0, size);
+                        buff = newbuff;
+                    }
+                }
+                byte[] tmp = new byte[25];
+                inStream.read(tmp);
+                if (!new String(tmp).startsWith("----END CERTIFICATE-----")) {
+                    throw new CertificateException(
+                    "Incorrect Base64 encoding: 'END CERTIFICATE' expected.");
+                }
+                // skip new line: set the position to the next certificate:
+                inStream.mark(1);
+                while (((ch = inStream.read()) != -1) && (ch == '\n' || ch == '\r')) {
+                    inStream.mark(1);
+                }
+                inStream.reset();
+                buff = Base64.decode(buff, index);
+                if (buff == null) {
+                    throw new CertificateException("Incorrect Base64 encoding.");
+                }
+                long hash = CERT_CASHE.getHash(buff);
+                if (CERT_CASHE.contains(hash)) {
+                    Certificate res = (Certificate) CERT_CASHE.get(hash, buff);
+                    if (res != null) {
+                        return res;
+                    }
+                }
+                Certificate res = new X509CertImpl(buff);
+                CERT_CASHE.put(hash, buff, res);
+                return res;
+            } else {
+                inStream.reset();
+                long hash = CERT_CASHE.getHash(buff);
+                if (CERT_CASHE.contains(hash)) {
+                    byte[] encoding = new byte[BerInputStream.getLength(buff)];
+                    inStream.read(encoding);
+                    Certificate res = 
+                        (Certificate) CERT_CASHE.get(hash, encoding);
+                    if (res != null) {
+                        return res;
+                    }
+                    res = new X509CertImpl(encoding);
+                    CERT_CASHE.put(hash, encoding, res);
+                    return res;
+                } else {
+                    Certificate res = new X509CertImpl(inStream);
+                    CERT_CASHE.put(hash, res.getEncoded(), res);
+                    return res;
+                }
+            }
+        } catch (IOException e) {
+            throw new CertificateException(e);
+        }
+    }
+    
+    /**
+     * engineGenerateCertificates
+     * FIXME: 1.5 updates are needed Collection <? extends Certificate>
+     */
+    public Collection engineGenerateCertificates(InputStream inStream)
+            throws CertificateException {
+        ArrayList result = new ArrayList();
+        try {
+            if (inStream.markSupported()) {
+            } else {
+                inStream = new RestoringInputStream(inStream);
+            }
+            inStream.mark(1);
+            // FIXME: Check if it is a PKCS7 structure, if not, do following:
+            while (inStream.read() != -1) {
+                inStream.reset();
+                result.add(engineGenerateCertificate(inStream));
+                inStream.mark(1);
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+            throw new CertificateException(e);
+        }
+        return result;
+    }
+
+    /**
+     * engineGenerateCRL
+     */
+    public CRL engineGenerateCRL(InputStream inStream)
+            throws CRLException {
+        try {
+            if (inStream.markSupported()) {
+            } else {
+                inStream = new RestoringInputStream(inStream);
+            }
+            inStream.mark(32);
+            byte[] buff = new byte[28];
+            if (inStream.read(buff) < 28) {
+                throw new CRLException(
+                        "Input Stream contains not enought data.");
+            }
+
+            inStream.reset();
+            long hash = CRL_CASHE.getHash(buff);
+            if (CRL_CASHE.contains(hash)) {
+                byte[] encoding = new byte[BerInputStream.getLength(buff)];
+                inStream.read(encoding);
+                CRL res = 
+                    (CRL) CRL_CASHE.get(hash, encoding);
+                if (res != null) {
+                    return res;
+                }
+                res = new X509CRLImpl(encoding);
+                CRL_CASHE.put(hash, encoding, res);
+                return res;
+            } else {
+                X509CRL res = new X509CRLImpl(inStream);
+                CRL_CASHE.put(hash, res.getEncoded(), res);
+                return res;
+            }
+        } catch (IOException e) {
+            throw new CRLException(e);
+        }
+    }
+
+    /**
+     * engineGenerateCRLs
+     * FIXME: 1.5 updates are needed Collection <? extends CRL>
+     */
+    public Collection engineGenerateCRLs(InputStream inStream)
+            throws CRLException {
+        if (inStream == null) {
+            throw new CRLException("Null input stream provided.");
+        }
+        ArrayList result = new ArrayList();
+        try {
+            if (inStream.markSupported()) {
+            } else {
+                inStream = new RestoringInputStream(inStream);
+            }
+            inStream.mark(1);
+            // FIXME: Check if it is a PKCS7 structure, if not, do following:
+            while (inStream.read() != -1) {
+                inStream.reset();
+                result.add(engineGenerateCRL(inStream));
+                inStream.mark(1);
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+            throw new CRLException(e);
+        }
+        return result;
+    }
+
+    /**
+     * engineGenerateCertPath
+     */
+    public CertPath engineGenerateCertPath(InputStream inStream)
+            throws CertificateException {
+        return X509CertPathImpl.getInstance(inStream);
+    }
+
+    /**
+     * engineGenerateCertPath
+     */
+    public CertPath engineGenerateCertPath(InputStream inStream, String encoding)
+            throws CertificateException {
+        return X509CertPathImpl.getInstance(inStream, encoding);
+    }
+
+    /**
+     * engineGenerateCertPath
+     */
+    public CertPath engineGenerateCertPath(List certificates)
+            throws CertificateException {
+        return new X509CertPathImpl(certificates);
+    }
+
+    /**
+     * engineGetCertPathEncodings
+     * FIXME: 1.5 updates are needed Iterator <String>
+     */
+    public Iterator engineGetCertPathEncodings() {
+        return X509CertPathImpl.encodings.iterator();
+    }
+
+    /**
+     * Class represents the stream which supports reset to the
+     * marked state with readlimit == BUFF_SIZE.
+     */
+    private static class RestoringInputStream extends InputStream {
+
+        private final InputStream inStream;
+        private final static int BUFF_SIZE = 32;
+        private final int[] buff = new int[BUFF_SIZE*2];
+        // position in the buffer
+        private int pos = -1;
+        // the last byte in the buffer
+        private int bar = 0;
+        // the last cell of the buffer
+        private int end = 0;
+        
+        public RestoringInputStream(InputStream inStream) {
+            this.inStream = inStream;
+        }
+
+        public int available() throws IOException {
+            return (bar - pos) + inStream.available();
+        }
+
+        public void close() throws IOException {
+            inStream.close();
+        }
+
+        public void mark(int readlimit) {
+            if (pos < 0) {
+                pos = 0;
+                bar = 0;
+                end = BUFF_SIZE - 1;
+            } else {
+                end = (pos + BUFF_SIZE - 1) % BUFF_SIZE;
+            }
+        }
+
+        public boolean markSupported() {
+            return true;
+        }
+
+        public int read() throws IOException {
+            if (pos >= 0) {
+                int cur = pos % BUFF_SIZE;
+                if (cur < bar) {
+                    pos++;
+                    return buff[cur];
+                }
+                if (cur != end) {
+                    buff[cur] = inStream.read();
+                    bar = cur+1;
+                    pos++;
+                    return buff[cur];
+                } else {
+                    pos = -1; // can not operate anymore
+                }
+            }
+            return inStream.read();
+        }
+
+        public int read(byte[] b) throws IOException {
+            return read(b, 0, b.length);
+        }
+
+        public int read(byte[] b, int off, int len) throws IOException {
+            int read_b;
+            int i;
+            for (i=0; i<len; i++) {
+                if ((read_b = read()) == -1) {
+                    return (i == 0) ? -1 : i; 
+                }
+                b[off+i] = (byte) read_b;
+            }
+            return i;
+        }
+
+        public void reset() throws IOException {
+            if (pos >= 0) {
+                pos = (end + 1) % BUFF_SIZE;
+            } else {
+                throw new IOException("Could not reset the stream: "
+                    + "position became invalid or stream has not been marked.");
+            }
+        }
+
+        public long skip(long n) throws IOException {
+            if (pos >= 0) {
+                long i = 0;
+                int av = available();
+                if (av < n) {
+                    n = av;
+                }
+                while ((i < n) && (read() != -1)) {
+                    i++;
+                }
+                return i;
+            } else {
+                return inStream.skip(n);
+            }
+        }
+    }
+}
+

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=399648&r1=399647&r2=399648&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 Thu May  4 03:13:20 2006
@@ -1,284 +1,284 @@
-/*
- *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
- *
- *  Licensed 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.
- */
-
-/**
-* @author Alexander Y. Kleymenov
-* @version $Revision$
-*/
-
-package java.security.cert;
-
-import java.io.ByteArrayInputStream;
-import java.math.BigInteger;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
-import java.security.NoSuchProviderException;
-import java.security.Principal;
-import java.security.PublicKey;
-import java.security.SignatureException;
-import java.util.Date;
-import java.util.Set;
-
-import javax.security.auth.x500.X500Principal;
-
-import org.apache.harmony.misc.Base64;
-import org.apache.harmony.security.support.cert.TestUtils;
-
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-/**
- * X509CertificateTest
- */
-public class X509CertificateTest extends TestCase {
-
-    // Base64 encoded form of ASN.1 DER encoded X.509 Certificate
-    // (see RFC 3280 at http://www.ietf.org/rfc/rfc3280.txt)
-    // (generated by using of classes from 
-    // org.apache.harmony.security.x509 package)
-    static String base64cert =
-          "MIIByzCCATagAwIBAgICAiswCwYJKoZIhvcNAQEFMB0xGzAZBgNVBAoT"
-        + "EkNlcnRpZmljYXRlIElzc3VlcjAeFw0wNjA0MjYwNjI4MjJaFw0zMzAz"
-        + "MDExNjQ0MDlaMB0xGzAZBgNVBAoTEkNlcnRpZmljYXRlIElzc3VlcjCB"
-        + "nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAkLGLsPdSPDMyP1OUOKu+"
-        + "U3cvbNK5RGaQ3bXc5aDjvApx43BcaoXgt6YD/5yXz0OsIooj5yA37+bY"
-        + "JGcVrvFD5FMPdDd3vjNPQOep0MzG4CdbkaZde5SigPabOMQYS4oUyLBx"
-        + "W3LGG0mUODe5AGGqtqXU0GlKg4K2je6cCtookCUCAwEAAaMeMBwwGgYD"
-        + "VR0RAQH/BBAwDoEMcmZjQDgyMi5OYW1lMAsGCSqGSIb3DQEBBQOBgQBZ"
-        + "pVXj01dOpqnZErU+Qb50j8lJD1dIaz1eJTvJCSadj7ziV1VtnnapI07c"
-        + "XEa7ONzcHQTYTG10poHfOK/a0BaULF3GlctDESilwQYbW5BdfpAlZpbH"
-        + "AFLcUDh6Eq50kc+0A/anh/j3mgBNuvbIMo7hHNnZB6k/prswm2BszyLD"
-        + "yw==";
-
-    // Base64 encoded form of ASN.1 DER encoded X.509 CRL
-    // (see RFC 3280 at http://www.ietf.org/rfc/rfc3280.txt)
-    // (generated by using of classes from 
-    // org.apache.harmony.security.x509 package)
-    static String base64crl = 
-          "MIHXMIGXAgEBMAkGByqGSM44BAMwFTETMBEGA1UEChMKQ1JMIElzc3Vl"
-        + "chcNMDYwNDI3MDYxMzQ1WhcNMDYwNDI3MDYxNTI1WjBBMD8CAgIrFw0w"
-        + "NjA0MjcwNjEzNDZaMCowCgYDVR0VBAMKAQEwHAYDVR0YBBUYEzIwMDYw"
-        + "NDI3MDYxMzQ1LjQ2OFqgDzANMAsGA1UdFAQEBAQEBDAJBgcqhkjOOAQD"
-        + "AzAAMC0CFQCk0t0DTyu82QpajbBlxX9uXvUDSgIUSBN4g+xTEeexs/0k"
-        + "9AkjBhjF0Es=";
-
-    // has stub implementation for abstract methods
-    private static class MyX509Certificate extends X509Certificate {
-
-            public void checkValidity()
-                    throws CertificateExpiredException,
-                           CertificateNotYetValidException {}
-
-            public void checkValidity(Date date)
-                    throws CertificateExpiredException,
-                           CertificateNotYetValidException {}
-
-            public int getVersion() {
-                return 3;
-            }
-
-            public BigInteger getSerialNumber() {
-                return null;
-            }
-
-            public Principal getIssuerDN() {
-                return null;
-            }
-
-            public Principal getSubjectDN() {
-                return null;
-            }
-
-            public Date getNotBefore() {
-                return null;
-            }
-
-            public Date getNotAfter() {
-                return null;
-            }
-
-            public byte[] getTBSCertificate()
-                                throws CertificateEncodingException
-            {
-                return null;
-            }
-
-            public byte[] getSignature() {
-                return null;
-            }
-
-            public String getSigAlgName() {
-                return null;
-            }
-
-            public String getSigAlgOID() {
-                return null;
-            }
-
-            public byte[] getSigAlgParams() {
-                return null;
-            }
-
-            public boolean[] getIssuerUniqueID() {
-                return null;
-            }
-
-            public boolean[] getSubjectUniqueID() {
-                return null;
-            }
-
-            public boolean[] getKeyUsage() {
-                return null;
-            }
-
-            public int getBasicConstraints() {
-                return 0;
-            }
-
-            public void verify(PublicKey key)
-                         throws CertificateException, NoSuchAlgorithmException,
-                                InvalidKeyException, NoSuchProviderException,
-                                SignatureException
-            {
-            }
-
-            public void verify(PublicKey key,
-                                        String sigProvider)
-                         throws CertificateException, NoSuchAlgorithmException,
-                                InvalidKeyException, NoSuchProviderException,
-                                SignatureException
-            {
-            }
-
-            public String toString() {
-                return "";
-            }
-
-            public PublicKey getPublicKey() {
-                return null;
-            }
-
-            public byte[] getEncoded() throws CertificateEncodingException
-            {
-                return null;
-            }
-
-            public Set getNonCriticalExtensionOIDs() {
-                return null;
-            }
-
-            public Set getCriticalExtensionOIDs() {
-                return null;
-            }
-
-            public byte[] getExtensionValue(String oid) {
-                return null;
-            }
-
-            public boolean hasUnsupportedCriticalExtension() {
-                return false;
-            }
-    }
-
-    /**
-     * @tests java.security.cert.X509Certificate#getType()
-     */
-    public void testGetType() {
-        assertEquals("X.509", new MyX509Certificate().getType());
-    }
-
-    /**
-     * @tests java.security.cert.X509Certificate#getIssuerX500Principal()
-     */
-    public void testGetIssuerX500Principal() {
-        // return valid encoding
-        MyX509Certificate cert = new MyX509Certificate() {
-            public byte[] getEncoded() {
-                return TestUtils.getX509Certificate_v1();
-            };
-        };
-
-        assertEquals(new X500Principal("CN=Z"), cert.getIssuerX500Principal());
-    }
-
-    /**
-     * @tests java.security.cert.X509Certificate#getSubjectX500Principal()
-     */
-    public void testGetSubjectX500Principal() {
-        // return valid encoding
-        MyX509Certificate cert = new MyX509Certificate() {
-            public byte[] getEncoded() {
-                return TestUtils.getX509Certificate_v1();
-            };
-        };
-
-        assertEquals(new X500Principal("CN=Y"), cert.getSubjectX500Principal());
-    }
-
-    /**
-     * @tests java.security.cert.X509Certificate#getExtendedKeyUsage()
-     */
-    public void testGetExtendedKeyUsage() throws CertificateParsingException {
-        assertNull(new MyX509Certificate().getExtendedKeyUsage());
-    }
-
-    /**
-     * @tests java.security.cert.X509Certificate#getSubjectAlternativeNames()
-     */
-    public void testGetSubjectAlternativeNames()
-            throws CertificateParsingException {
-
-        assertNull(new MyX509Certificate().getSubjectAlternativeNames());
-    }
-
-    /**
-     * @tests java.security.cert.X509Certificate#getIssuerAlternativeNames()
-     */
-    public void testGetIssuerAlternativeNames()
-            throws CertificateParsingException {
-
-        assertNull(new MyX509Certificate().getIssuerAlternativeNames());
-    }
-
-    /**
-     * @tests java.security.cert.X509Certificate#getExtensionValue()
-     */
-    public void testGetExtensionValue() throws Exception {
-        // Regression for HARMONY-419
-        ByteArrayInputStream is = null;
-        CertificateFactory certFactory = CertificateFactory
-                .getInstance("X.509");
-        is = new ByteArrayInputStream(Base64.decode(base64cert.getBytes()));
-        X509Certificate cert = (X509Certificate) certFactory
-                .generateCertificate(is);
-        cert.getExtensionValue("1.1.1.1");
-
-        is = new ByteArrayInputStream(Base64.decode(base64crl.getBytes()));
-        X509CRL crl = (X509CRL) certFactory.generateCRL(is);
-        crl.getExtensionValue("1.1.1.1");
-    }
-    
-    
-    public static Test suite() {
-        return new TestSuite(X509CertificateTest.class);
-    }
-
-    public static void main(String[] args) {
-        junit.textui.TestRunner.run(suite());
-    }
-}
-
+/*
+ *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed 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.
+ */
+
+/**
+* @author Alexander Y. Kleymenov
+* @version $Revision$
+*/
+
+package java.security.cert;
+
+import java.io.ByteArrayInputStream;
+import java.math.BigInteger;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.Principal;
+import java.security.PublicKey;
+import java.security.SignatureException;
+import java.util.Date;
+import java.util.Set;
+
+import javax.security.auth.x500.X500Principal;
+
+import org.apache.harmony.luni.util.Base64;
+import org.apache.harmony.security.support.cert.TestUtils;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * X509CertificateTest
+ */
+public class X509CertificateTest extends TestCase {
+
+    // Base64 encoded form of ASN.1 DER encoded X.509 Certificate
+    // (see RFC 3280 at http://www.ietf.org/rfc/rfc3280.txt)
+    // (generated by using of classes from 
+    // org.apache.harmony.security.x509 package)
+    static String base64cert =
+          "MIIByzCCATagAwIBAgICAiswCwYJKoZIhvcNAQEFMB0xGzAZBgNVBAoT"
+        + "EkNlcnRpZmljYXRlIElzc3VlcjAeFw0wNjA0MjYwNjI4MjJaFw0zMzAz"
+        + "MDExNjQ0MDlaMB0xGzAZBgNVBAoTEkNlcnRpZmljYXRlIElzc3VlcjCB"
+        + "nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAkLGLsPdSPDMyP1OUOKu+"
+        + "U3cvbNK5RGaQ3bXc5aDjvApx43BcaoXgt6YD/5yXz0OsIooj5yA37+bY"
+        + "JGcVrvFD5FMPdDd3vjNPQOep0MzG4CdbkaZde5SigPabOMQYS4oUyLBx"
+        + "W3LGG0mUODe5AGGqtqXU0GlKg4K2je6cCtookCUCAwEAAaMeMBwwGgYD"
+        + "VR0RAQH/BBAwDoEMcmZjQDgyMi5OYW1lMAsGCSqGSIb3DQEBBQOBgQBZ"
+        + "pVXj01dOpqnZErU+Qb50j8lJD1dIaz1eJTvJCSadj7ziV1VtnnapI07c"
+        + "XEa7ONzcHQTYTG10poHfOK/a0BaULF3GlctDESilwQYbW5BdfpAlZpbH"
+        + "AFLcUDh6Eq50kc+0A/anh/j3mgBNuvbIMo7hHNnZB6k/prswm2BszyLD"
+        + "yw==";
+
+    // Base64 encoded form of ASN.1 DER encoded X.509 CRL
+    // (see RFC 3280 at http://www.ietf.org/rfc/rfc3280.txt)
+    // (generated by using of classes from 
+    // org.apache.harmony.security.x509 package)
+    static String base64crl = 
+          "MIHXMIGXAgEBMAkGByqGSM44BAMwFTETMBEGA1UEChMKQ1JMIElzc3Vl"
+        + "chcNMDYwNDI3MDYxMzQ1WhcNMDYwNDI3MDYxNTI1WjBBMD8CAgIrFw0w"
+        + "NjA0MjcwNjEzNDZaMCowCgYDVR0VBAMKAQEwHAYDVR0YBBUYEzIwMDYw"
+        + "NDI3MDYxMzQ1LjQ2OFqgDzANMAsGA1UdFAQEBAQEBDAJBgcqhkjOOAQD"
+        + "AzAAMC0CFQCk0t0DTyu82QpajbBlxX9uXvUDSgIUSBN4g+xTEeexs/0k"
+        + "9AkjBhjF0Es=";
+
+    // has stub implementation for abstract methods
+    private static class MyX509Certificate extends X509Certificate {
+
+            public void checkValidity()
+                    throws CertificateExpiredException,
+                           CertificateNotYetValidException {}
+
+            public void checkValidity(Date date)
+                    throws CertificateExpiredException,
+                           CertificateNotYetValidException {}
+
+            public int getVersion() {
+                return 3;
+            }
+
+            public BigInteger getSerialNumber() {
+                return null;
+            }
+
+            public Principal getIssuerDN() {
+                return null;
+            }
+
+            public Principal getSubjectDN() {
+                return null;
+            }
+
+            public Date getNotBefore() {
+                return null;
+            }
+
+            public Date getNotAfter() {
+                return null;
+            }
+
+            public byte[] getTBSCertificate()
+                                throws CertificateEncodingException
+            {
+                return null;
+            }
+
+            public byte[] getSignature() {
+                return null;
+            }
+
+            public String getSigAlgName() {
+                return null;
+            }
+
+            public String getSigAlgOID() {
+                return null;
+            }
+
+            public byte[] getSigAlgParams() {
+                return null;
+            }
+
+            public boolean[] getIssuerUniqueID() {
+                return null;
+            }
+
+            public boolean[] getSubjectUniqueID() {
+                return null;
+            }
+
+            public boolean[] getKeyUsage() {
+                return null;
+            }
+
+            public int getBasicConstraints() {
+                return 0;
+            }
+
+            public void verify(PublicKey key)
+                         throws CertificateException, NoSuchAlgorithmException,
+                                InvalidKeyException, NoSuchProviderException,
+                                SignatureException
+            {
+            }
+
+            public void verify(PublicKey key,
+                                        String sigProvider)
+                         throws CertificateException, NoSuchAlgorithmException,
+                                InvalidKeyException, NoSuchProviderException,
+                                SignatureException
+            {
+            }
+
+            public String toString() {
+                return "";
+            }
+
+            public PublicKey getPublicKey() {
+                return null;
+            }
+
+            public byte[] getEncoded() throws CertificateEncodingException
+            {
+                return null;
+            }
+
+            public Set getNonCriticalExtensionOIDs() {
+                return null;
+            }
+
+            public Set getCriticalExtensionOIDs() {
+                return null;
+            }
+
+            public byte[] getExtensionValue(String oid) {
+                return null;
+            }
+
+            public boolean hasUnsupportedCriticalExtension() {
+                return false;
+            }
+    }
+
+    /**
+     * @tests java.security.cert.X509Certificate#getType()
+     */
+    public void testGetType() {
+        assertEquals("X.509", new MyX509Certificate().getType());
+    }
+
+    /**
+     * @tests java.security.cert.X509Certificate#getIssuerX500Principal()
+     */
+    public void testGetIssuerX500Principal() {
+        // return valid encoding
+        MyX509Certificate cert = new MyX509Certificate() {
+            public byte[] getEncoded() {
+                return TestUtils.getX509Certificate_v1();
+            };
+        };
+
+        assertEquals(new X500Principal("CN=Z"), cert.getIssuerX500Principal());
+    }
+
+    /**
+     * @tests java.security.cert.X509Certificate#getSubjectX500Principal()
+     */
+    public void testGetSubjectX500Principal() {
+        // return valid encoding
+        MyX509Certificate cert = new MyX509Certificate() {
+            public byte[] getEncoded() {
+                return TestUtils.getX509Certificate_v1();
+            };
+        };
+
+        assertEquals(new X500Principal("CN=Y"), cert.getSubjectX500Principal());
+    }
+
+    /**
+     * @tests java.security.cert.X509Certificate#getExtendedKeyUsage()
+     */
+    public void testGetExtendedKeyUsage() throws CertificateParsingException {
+        assertNull(new MyX509Certificate().getExtendedKeyUsage());
+    }
+
+    /**
+     * @tests java.security.cert.X509Certificate#getSubjectAlternativeNames()
+     */
+    public void testGetSubjectAlternativeNames()
+            throws CertificateParsingException {
+
+        assertNull(new MyX509Certificate().getSubjectAlternativeNames());
+    }
+
+    /**
+     * @tests java.security.cert.X509Certificate#getIssuerAlternativeNames()
+     */
+    public void testGetIssuerAlternativeNames()
+            throws CertificateParsingException {
+
+        assertNull(new MyX509Certificate().getIssuerAlternativeNames());
+    }
+
+    /**
+     * @tests java.security.cert.X509Certificate#getExtensionValue()
+     */
+    public void testGetExtensionValue() throws Exception {
+        // Regression for HARMONY-419
+        ByteArrayInputStream is = null;
+        CertificateFactory certFactory = CertificateFactory
+                .getInstance("X.509");
+        is = new ByteArrayInputStream(Base64.decode(base64cert.getBytes()));
+        X509Certificate cert = (X509Certificate) certFactory
+                .generateCertificate(is);
+        cert.getExtensionValue("1.1.1.1");
+
+        is = new ByteArrayInputStream(Base64.decode(base64crl.getBytes()));
+        X509CRL crl = (X509CRL) certFactory.generateCRL(is);
+        crl.getExtensionValue("1.1.1.1");
+    }
+    
+    
+    public static Test suite() {
+        return new TestSuite(X509CertificateTest.class);
+    }
+
+    public static void main(String[] args) {
+        junit.textui.TestRunner.run(suite());
+    }
+}
+



Mime
View raw message