5434N/A/*
5434N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5434N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5434N/A *
5434N/A * This code is free software; you can redistribute it and/or modify it
5434N/A * under the terms of the GNU General Public License version 2 only, as
5434N/A * published by the Free Software Foundation.
5434N/A *
5434N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5434N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5434N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5434N/A * version 2 for more details (a copy is included in the LICENSE file that
5434N/A * accompanied this code).
5434N/A *
5434N/A * You should have received a copy of the GNU General Public License version
5434N/A * 2 along with this work; if not, write to the Free Software Foundation,
5434N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5434N/A *
5434N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5434N/A * or visit www.oracle.com if you need additional information or have any
5434N/A * questions.
5434N/A */
5434N/A
5434N/A/*
5434N/A * @test
5437N/A * @bug 7195931 7197071 7198146
5434N/A * @summary UnsatisfiedLinkError on PKCS11.C_GetOperationState while
5434N/A * using NSS from jre7u6+
5434N/A */
5434N/Aimport java.net.*;
5434N/Aimport java.io.*;
5434N/Aimport java.security.*;
5437N/Aimport java.lang.reflect.*;
5434N/A
5434N/A/**
5434N/A * When the Java specification version is incremented, all of the providers
5434N/A * must be recompiled with the proper implementation version to match.
5434N/A */
5434N/Apublic class CheckManifestForRelease {
5434N/A
5434N/A /**
5434N/A * @param args the command line arguments
5434N/A */
5434N/A public static void main(String[] args) throws Exception {
5434N/A checkP11MessageDigestClone();
5437N/A checkFileManifests();
5434N/A }
5434N/A
5434N/A /*
5437N/A * Iterate over the files of interest: JCE framework and providers
5434N/A */
5437N/A static private void checkFileManifests() throws Exception {
5434N/A System.out.println("=============");
5437N/A String libDirName = System.getProperty("java.home", ".") + "/lib";
5437N/A String extDirName = libDirName + "/ext";
5437N/A
5437N/A System.out.println("Checking Manifest in directory: \n " +
5437N/A extDirName);
5434N/A
5437N/A /*
5437N/A * Current list of JCE providers, all of which currently live in
5437N/A * the extensions directory. Add if more are created.
5437N/A */
5437N/A String[] providers = new String[]{
5434N/A "sunjce_provider.jar",
5434N/A "sunec.jar",
5434N/A "sunmscapi.jar",
5434N/A "sunpkcs11.jar",
5434N/A "ucrypto.jar"
5434N/A };
5434N/A
5437N/A checkManifest(libDirName, "jce.jar");
5437N/A for (String provider : providers) {
5437N/A checkManifest(extDirName, provider);
5437N/A }
5437N/A System.out.println("Passed.");
5437N/A }
5437N/A
5437N/A // Helper method to format the URL properly.
5437N/A static private String formatURL(String dir, String file) {
5437N/A return "jar:file:///" + dir + "/" + file + "!/";
5437N/A }
5434N/A
5437N/A static private String specVersion =
5437N/A System.getProperty("java.specification.version");
5437N/A
5437N/A /*
5437N/A * Test the root cause, which is that there were no manifest values
5437N/A * for many of the providers, and for those that had them, there was
5437N/A * no test to make sure that the impl version was appropriate for
5437N/A * the spec version.
5437N/A */
5437N/A static private void checkManifest(String dir, String file)
5437N/A throws Exception {
5437N/A
5437N/A System.out.println("Checking: " + file);
5434N/A
5437N/A String url = formatURL(dir, file);
5437N/A JarURLConnection urlc =
5437N/A (JarURLConnection) (new URL(url).openConnection());
5434N/A
5437N/A String implVersion;
5437N/A try {
5437N/A implVersion = urlc.getManifest().getMainAttributes().getValue(
5437N/A "Implementation-Version");
5437N/A } catch (FileNotFoundException e) {
5437N/A /*
5437N/A * If the file doesn't exist (e.g. mscapi on solaris),
5437N/A * skip it. If there are other problems, fail out.
5437N/A */
5437N/A System.out.println(" " + file + " not found, skipping...");
5437N/A return;
5437N/A }
5434N/A
5437N/A if (implVersion == null) {
5437N/A throw new Exception(
5437N/A "Implementation-Version not found in Manifest");
5437N/A }
5437N/A
5437N/A if (!implVersion.startsWith(specVersion)) {
5437N/A throw new Exception(
5437N/A "Implementation-Version does not match " +
5437N/A "Specification-Version");
5434N/A }
5434N/A }
5434N/A
5434N/A /*
5437N/A * Workaround for unfortunately generified forName() API
5437N/A */
5437N/A @SuppressWarnings("unchecked")
5437N/A static private Class<Provider> getProviderClass(String name)
5437N/A throws Exception {
5437N/A return (Class<Provider>)Class.forName(name);
5437N/A }
5437N/A
5437N/A /*
5437N/A * Check the symptom, an UnsatisfiedLinkError in MessageDigests.
5434N/A */
5434N/A static private void checkP11MessageDigestClone() throws Exception {
5437N/A
5434N/A System.out.println("=============");
5434N/A System.out.println("Checking for UnsatisfiedLinkError");
5434N/A String os = System.getProperty("os.name");
5434N/A // Only run on Solaris
5434N/A if (!os.equals("SunOS")) {
5434N/A return;
5434N/A }
5434N/A
5434N/A /*
5434N/A * We have to do some gyrations here, since the code to exercise
5437N/A * this is in the P11 MessageDigests, and most of those mechanisms
5437N/A * are disabled by default.
5434N/A */
5434N/A String customP11File =
5437N/A System.getProperty("TESTSRC", ".") + "/p11-solaris.txt";
5437N/A
5437N/A /*
5437N/A * In 7u, we don't have a 64 PKCS11 windows build yet, so we
5437N/A * have to do some dynamic checking to determine if there is
5437N/A * a PKCS11 library available to test against. Otherwise, the
5437N/A * windows 64 bit will throw a compilation error before the
5437N/A * test is even run.
5437N/A */
5437N/A Constructor<Provider> cons;
5437N/A Provider provider;
5437N/A try {
5437N/A Class<Provider> clazz =
5437N/A getProviderClass("sun.security.pkcs11.SunPKCS11");
5437N/A cons = clazz.getConstructor(new Class[]{String.class});
5437N/A provider = cons.newInstance(new Object[]{customP11File});
5437N/A } catch (Exception ex) {
5437N/A System.out.println("Skipping test - no PKCS11 provider available");
5437N/A return;
5437N/A }
5434N/A
5434N/A try {
5434N/A MessageDigest md = MessageDigest.getInstance("SHA1", provider);
5434N/A md.update((byte) 0x01);
5434N/A System.out.println(md.getProvider());
5434N/A md.clone();
5434N/A } catch (Exception e) {
5437N/A // These kinds of failure are ok. We're testing the
5434N/A // UnsatisfiedLinkError here.
5434N/A }
5437N/A System.out.println("Passed.");
5434N/A }
5434N/A}