You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by gh...@apache.org on 2006/04/28 18:39:09 UTC

svn commit: r397949 - in /incubator/harmony/enhanced/classlib/trunk: modules/archive/src/main/java/java/util/jar/ modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ support/src/test/java/tests/resources/

Author: gharley
Date: Fri Apr 28 09:39:07 2006
New Revision: 397949

URL: http://svn.apache.org/viewcvs?rev=397949&view=rev
Log:
HARMONY-162 : One JSE 5 method are not implemented in java/util/jar/JarEntry

Added:
    incubator/harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/TestCodeSigners.jar   (with props)
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarEntry.java
    incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarEntryTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarEntry.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarEntry.java?rev=397949&r1=397948&r2=397949&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarEntry.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarEntry.java Fri Apr 28 09:39:07 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 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.
@@ -15,11 +15,19 @@
 
 package java.util.jar;
 
-
 import java.io.IOException;
+import java.security.CodeSigner;
+import java.security.cert.CertPath;
 import java.security.cert.Certificate;
+import java.security.cert.CertificateException;
+import java.security.cert.CertificateFactory;
+import java.security.cert.X509Certificate;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.zip.ZipEntry;
 
+import javax.security.auth.x500.X500Principal;
+
 public class JarEntry extends ZipEntry {
 	private Attributes attributes;
 
@@ -27,6 +35,13 @@
 
 	Certificate certificates[];
 
+	CodeSigner signers[];
+
+	// Cached factory used to build CertPath-s in <code>getCodeSigners()</code>.
+	private CertificateFactory factory;
+
+	private boolean isFactoryChecked = false;
+
 	/**
 	 * Create a new JarEntry named name
 	 * 
@@ -89,5 +104,95 @@
 		parentJar = je.parentJar;
 		attributes = je.attributes;
 		certificates = je.certificates;
+		signers = je.signers;
+
+	}
+
+	/**
+	 * Returns the code signers for the jar entry. If there is no such code
+	 * signers, returns null. Only when the jar entry has been completely
+	 * verified by reading till the end of the jar entry, can the method be
+	 * called. Or else the method will return null.
+	 * 
+	 * @return the code signers for the jar entry.
+	 */
+	public CodeSigner[] getCodeSigners() {
+		if (null == signers) {
+			signers = getCodeSigners(certificates);
+
+		}
+		if (null == signers) {
+			return null;
+		}
+
+		CodeSigner[] tmp = new CodeSigner[signers.length];
+		System.arraycopy(signers, 0, tmp, 0, tmp.length);
+		return tmp;
+
+	}
+
+	private CodeSigner[] getCodeSigners(Certificate[] certs) {
+
+		X500Principal prevIssuer = null;
+		ArrayList<Certificate> list = new ArrayList<Certificate>(certs.length);
+		ArrayList<CodeSigner> asigners = new ArrayList<CodeSigner>();
+
+		for (int i = 0; i < certs.length; i++) {
+			if (!(certs[i] instanceof X509Certificate)) {
+				// Only X509CErtificate-s are taken into account - see API spec.
+				continue;
+			}
+			X509Certificate x509 = (X509Certificate) certs[i];
+			if (null != prevIssuer) {
+				X500Principal subj = x509.getSubjectX500Principal();
+				if (!prevIssuer.equals(subj)) {
+					// Ok, this ends the previous chain,
+					// so transform this one into CertPath ...
+					addCodeSigner(asigners, list);
+					// ... and start a new one
+					list.clear();
+				}// else { it's still the same chain }
+
+			}
+			prevIssuer = x509.getIssuerX500Principal();
+			list.add(x509);
+		}
+		if (!list.isEmpty()) {
+			addCodeSigner(asigners, list);
+		}
+		if (asigners.isEmpty()) {
+			// 'signers' is 'null' already
+			return null;
+		}
+
+		CodeSigner[] tmp = new CodeSigner[asigners.size()];
+		asigners.toArray(tmp);
+		return tmp;
+
+	}
+
+	private void addCodeSigner(ArrayList<CodeSigner> asigners,
+			List<Certificate> list) {
+		CertPath certPath = null;
+		if (!isFactoryChecked) {
+			try {
+				factory = CertificateFactory.getInstance("X.509");
+			} catch (CertificateException ex) {
+				// do nothing
+			} finally {
+				isFactoryChecked = true;
+			}
+		}
+		if (null == factory) {
+			return;
+		}
+		try {
+			certPath = factory.generateCertPath(list);
+		} catch (CertificateException ex) {
+			// do nothing
+		}
+		if (null != certPath) {
+			asigners.add(new CodeSigner(certPath, null));
+		}
 	}
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarEntryTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarEntryTest.java?rev=397949&r1=397948&r2=397949&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarEntryTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarEntryTest.java Fri Apr 28 09:39:07 2006
@@ -15,6 +15,10 @@
 package org.apache.harmony.archive.tests.java.util.jar;
 
 import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.CodeSigner;
+import java.util.List;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 
@@ -105,6 +109,36 @@
 				.getCertificates());
 	}
 
+    
+	/**
+	 * @tests java.util.jar.JarEntry#getCodeSigners()
+	 */
+	public void test_getCodeSigners() throws IOException {
+	    String jarFileName = "TestCodeSigners.jar";
+	    Support_Resources.copyFile(resources, null, jarFileName);
+	    File file = new File(resources, jarFileName);
+	    JarFile jarFile = new JarFile(file);
+	    JarEntry jarEntry = jarFile.getJarEntry("Test.class");
+	    InputStream in = jarFile.getInputStream(jarEntry);
+	    byte[] buffer = new byte[1024];
+	    while(in.available()>0)
+	    {
+	        in.read(buffer);
+	    }
+	    CodeSigner[] codeSigners = jarEntry.getCodeSigners();
+	    assertEquals(2, codeSigners.length);
+	    List certs_bob = codeSigners[0].getSignerCertPath().getCertificates();
+	    List certs_alice = codeSigners[1].getSignerCertPath().getCertificates();
+	    if(1 == certs_bob.size())
+	    {
+	        List temp = certs_bob;
+	        certs_bob = certs_alice;
+	        certs_alice = temp;
+	    }
+	    assertEquals(2,certs_bob.size());
+	    assertEquals(1,certs_alice.size());
+	}    
+    
 	/**
 	 * Sets up the fixture, for example, open a network connection. This method
 	 * is called before a test is executed.

Added: incubator/harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/TestCodeSigners.jar
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/TestCodeSigners.jar?rev=397949&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/TestCodeSigners.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream