test/sun/security/pkcs11/PKCS11Test.java

Print this page
7191662: JCE providers should be located via ServiceLoader
   1 /*
   2  * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 // common infrastructure for SunPKCS11 tests
  26 
  27 import java.io.*;
  28 import java.util.*;
  29 import java.lang.reflect.*;
  30 
  31 import java.security.*;
  32 import java.security.spec.ECGenParameterSpec;
  33 import java.security.spec.ECParameterSpec;
  34 
  35 public abstract class PKCS11Test {
  36 
  37     // directory of the test source
  38     static final String BASE = System.getProperty("test.src", ".");
  39 
  40     static final char SEP = File.separatorChar;
  41 
  42     private final static String REL_CLOSED = "../../../../closed/sun/security/pkcs11".replace('/', SEP);
  43 
  44     // directory corresponding to BASE in the /closed hierarchy
  45     static final String CLOSED_BASE;
  46 
  47     static {
  48         // hack
  49         String absBase = new File(BASE).getAbsolutePath();


  53         String p2 = absBase.substring(k + 5);
  54         CLOSED_BASE = p1 + "closed" + p2;
  55     }
  56 
  57     static String NSPR_PREFIX = "";
  58 
  59     // NSS version info
  60     public static enum ECCState { None, Basic, Extended };
  61     static double nss_version = -1;
  62     static ECCState nss_ecc_status = ECCState.Extended;
  63 
  64     // The NSS library we need to search for in getNSSLibDir()
  65     // Default is "libsoftokn3.so", listed as "softokn3"
  66     // The other is "libnss3.so", listed as "nss3".
  67     static String nss_library = "softokn3";
  68 
  69     // NSS versions of each library.  It is simplier to keep nss_version
  70     // for quick checking for generic testing than many if-else statements.
  71     static double softoken3_version = -1;
  72     static double nss3_version = -1;

  73 






























  74     static Provider getSunPKCS11(String config) throws Exception {
  75         Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11");
  76         Constructor cons = clazz.getConstructor(new Class[] {String.class});
  77         Object obj = cons.newInstance(new Object[] {config});
  78         return (Provider)obj;
  79     }
  80 
  81     public abstract void main(Provider p) throws Exception;
  82 
  83     private void premain(Provider p) throws Exception {
  84         long start = System.currentTimeMillis();
  85         System.out.println("Running test with provider " + p.getName() + "...");
  86         main(p);
  87         long stop = System.currentTimeMillis();
  88         System.out.println("Completed test with provider " + p.getName() + " (" + (stop - start) + " ms).");

  89     }
  90 
  91     public static void main(PKCS11Test test) throws Exception {
  92         Provider[] oldProviders = Security.getProviders();
  93         try {
  94             System.out.println("Beginning test run " + test.getClass().getName() + "...");
  95             testDefault(test);
  96             testNSS(test);
  97             testDeimos(test);
  98         } finally {
  99             // NOTE: Do not place a 'return' in any finally block
 100             // as it will suppress exceptions and hide test failures.
 101             Provider[] newProviders = Security.getProviders();
 102             boolean found = true;
 103             // Do not restore providers if nothing changed. This is especailly
 104             // useful for ./Provider/Login.sh, where a SecurityManager exists.
 105             if (oldProviders.length == newProviders.length) {
 106                 found = false;
 107                 for (int i = 0; i<oldProviders.length; i++) {
 108                     if (oldProviders[i] != newProviders[i]) {
 109                         found = true;
 110                         break;
 111                     }
 112                 }
 113             }
 114             if (found) {
 115                 for (Provider p: newProviders) {

 116                     Security.removeProvider(p.getName());
 117                 }
 118                 for (Provider p: oldProviders) {

 119                     Security.addProvider(p);
 120                 }
 121             }
 122         }
 123     }
 124 
 125     public static void testDeimos(PKCS11Test test) throws Exception {
 126         if (new File("/opt/SUNWconn/lib/libpkcs11.so").isFile() == false ||
 127             "true".equals(System.getProperty("NO_DEIMOS"))) {
 128             return;
 129         }
 130         String base = getBase();
 131         String p11config = base + SEP + "nss" + SEP + "p11-deimos.txt";
 132         Provider p = getSunPKCS11(p11config);
 133         test.premain(p);
 134     }
 135 
 136     public static void testDefault(PKCS11Test test) throws Exception {
 137         // run test for default configured PKCS11 providers (if any)
 138 


 559 
 560     private static int nextNibble(StringReader r) throws IOException {
 561         while (true) {
 562             int ch = r.read();
 563             if (ch == -1) {
 564                 return -1;
 565             } else if ((ch >= '0') && (ch <= '9')) {
 566                 return ch - '0';
 567             } else if ((ch >= 'a') && (ch <= 'f')) {
 568                 return ch - 'a' + 10;
 569             } else if ((ch >= 'A') && (ch <= 'F')) {
 570                 return ch - 'A' + 10;
 571             }
 572         }
 573     }
 574 
 575     <T> T[] concat(T[] a, T[] b) {
 576         if ((b == null) || (b.length == 0)) {
 577             return a;
 578         }
 579         T[] r = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
 580         System.arraycopy(a, 0, r, 0, a.length);
 581         System.arraycopy(b, 0, r, a.length, b.length);
 582         return r;
 583     }
 584 
 585     /**
 586      * Returns supported algorithms of specified type.
 587      */
 588     static List<String> getSupportedAlgorithms(String type, String alg,
 589             Provider p) {
 590         // prepare a list of supported algorithms
 591         List<String> algorithms = new ArrayList<>();
 592         Set<Provider.Service> services = p.getServices();
 593         for (Provider.Service service : services) {
 594             if (service.getType().equals(type)
 595                     && service.getAlgorithm().startsWith(alg)) {
 596                 algorithms.add(service.getAlgorithm());
 597             }
 598         }
 599         return algorithms;
 600     }
   1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 // common infrastructure for SunPKCS11 tests
  26 
  27 import java.io.*;
  28 import java.util.*;

  29 
  30 import java.security.*;
  31 import java.security.spec.ECGenParameterSpec;
  32 import java.security.spec.ECParameterSpec;
  33 
  34 public abstract class PKCS11Test {
  35 
  36     // directory of the test source
  37     static final String BASE = System.getProperty("test.src", ".");
  38 
  39     static final char SEP = File.separatorChar;
  40 
  41     private final static String REL_CLOSED = "../../../../closed/sun/security/pkcs11".replace('/', SEP);
  42 
  43     // directory corresponding to BASE in the /closed hierarchy
  44     static final String CLOSED_BASE;
  45 
  46     static {
  47         // hack
  48         String absBase = new File(BASE).getAbsolutePath();


  52         String p2 = absBase.substring(k + 5);
  53         CLOSED_BASE = p1 + "closed" + p2;
  54     }
  55 
  56     static String NSPR_PREFIX = "";
  57 
  58     // NSS version info
  59     public static enum ECCState { None, Basic, Extended };
  60     static double nss_version = -1;
  61     static ECCState nss_ecc_status = ECCState.Extended;
  62 
  63     // The NSS library we need to search for in getNSSLibDir()
  64     // Default is "libsoftokn3.so", listed as "softokn3"
  65     // The other is "libnss3.so", listed as "nss3".
  66     static String nss_library = "softokn3";
  67 
  68     // NSS versions of each library.  It is simplier to keep nss_version
  69     // for quick checking for generic testing than many if-else statements.
  70     static double softoken3_version = -1;
  71     static double nss3_version = -1;
  72     static Provider sunpkcs11;
  73 
  74     // Goes through ServiceLoader instead Provider.getInstance() since
  75     // SunPKCS11 provider is NOT registered for non-Solaris platforms
  76     static {
  77         ServiceLoader sl = ServiceLoader.load(java.security.Provider.class);
  78         Iterator<Provider> iter = sl.iterator();
  79         Provider p = null;
  80         while (iter.hasNext()) {
  81             try {
  82                 p = iter.next();
  83                 if (p.getName().startsWith("SunPKCS11")) {
  84                     break;
  85                 };
  86             } catch (Exception e) {
  87                 // ignore and move on to the next one
  88             }
  89         }
  90         sunpkcs11 = p;
  91     }
  92 
  93     // Return a SunPKCS11 provider with default config file (Solaris)
  94     // or a dummy one which requires further configuration (non-Solaris)
  95     static Provider getSunPKCS11() throws NoSuchProviderException {
  96         if (sunpkcs11 == null) {
  97             throw new NoSuchProviderException("No PKCS11 provider available");
  98         }
  99         return sunpkcs11;
 100     }
 101 
 102      
 103     // Return a SunPKCS11 provider using the specified config file
 104     static Provider getSunPKCS11(String config) throws Exception {
 105         return getSunPKCS11().configure(config);



 106     }
 107 
 108     public abstract void main(Provider p) throws Exception;
 109 
 110     private void premain(Provider p) throws Exception {
 111         long start = System.currentTimeMillis();
 112         System.out.println("Running test with provider " + p.getName() + "...");
 113         main(p);
 114         long stop = System.currentTimeMillis();
 115         System.out.println("Completed test with provider " + p.getName() +
 116             " (" + (stop - start) + " ms).");
 117     }
 118 
 119     public static void main(PKCS11Test test) throws Exception {
 120         Provider[] oldProviders = Security.getProviders();
 121         try {
 122             System.out.println("Beginning test run " + test.getClass().getName() + "...");
 123             testDefault(test);
 124             testNSS(test);
 125             testDeimos(test);
 126         } finally {
 127             // NOTE: Do not place a 'return' in any finally block
 128             // as it will suppress exceptions and hide test failures.
 129             Provider[] newProviders = Security.getProviders();
 130             boolean found = true;
 131             // Do not restore providers if nothing changed. This is especailly
 132             // useful for ./Provider/Login.sh, where a SecurityManager exists.
 133             if (oldProviders.length == newProviders.length) {
 134                 found = false;
 135                 for (int i = 0; i<oldProviders.length; i++) {
 136                     if (oldProviders[i] != newProviders[i]) {
 137                         found = true;
 138                         break;
 139                     }
 140                 }
 141             }
 142             if (found) {
 143                 for (Provider p: newProviders) {
 144                     System.out.println("PKCS11Test: remove new Provider: " + p.getName());
 145                     Security.removeProvider(p.getName());
 146                 }
 147                 for (Provider p: oldProviders) {
 148                     System.out.println("PKCS11Test: add old Provider: " + p.getName());
 149                     Security.addProvider(p);
 150                 }
 151             }
 152         }
 153     }
 154 
 155     public static void testDeimos(PKCS11Test test) throws Exception {
 156         if (new File("/opt/SUNWconn/lib/libpkcs11.so").isFile() == false ||
 157             "true".equals(System.getProperty("NO_DEIMOS"))) {
 158             return;
 159         }
 160         String base = getBase();
 161         String p11config = base + SEP + "nss" + SEP + "p11-deimos.txt";
 162         Provider p = getSunPKCS11(p11config);
 163         test.premain(p);
 164     }
 165 
 166     public static void testDefault(PKCS11Test test) throws Exception {
 167         // run test for default configured PKCS11 providers (if any)
 168 


 589 
 590     private static int nextNibble(StringReader r) throws IOException {
 591         while (true) {
 592             int ch = r.read();
 593             if (ch == -1) {
 594                 return -1;
 595             } else if ((ch >= '0') && (ch <= '9')) {
 596                 return ch - '0';
 597             } else if ((ch >= 'a') && (ch <= 'f')) {
 598                 return ch - 'a' + 10;
 599             } else if ((ch >= 'A') && (ch <= 'F')) {
 600                 return ch - 'A' + 10;
 601             }
 602         }
 603     }
 604 
 605     <T> T[] concat(T[] a, T[] b) {
 606         if ((b == null) || (b.length == 0)) {
 607             return a;
 608         }
 609         T[] r = Arrays.copyOf(a, a.length + b.length);

 610         System.arraycopy(b, 0, r, a.length, b.length);
 611         return r;
 612     }
 613 
 614     /**
 615      * Returns supported algorithms of specified type.
 616      */
 617     static List<String> getSupportedAlgorithms(String type, String alg,
 618             Provider p) {
 619         // prepare a list of supported algorithms
 620         List<String> algorithms = new ArrayList<>();
 621         Set<Provider.Service> services = p.getServices();
 622         for (Provider.Service service : services) {
 623             if (service.getType().equals(type)
 624                     && service.getAlgorithm().startsWith(alg)) {
 625                 algorithms.add(service.getAlgorithm());
 626             }
 627         }
 628         return algorithms;
 629     }