src/java.base/share/classes/sun/security/jca/Providers.java

Print this page
7191662: JCE providers should be located via ServiceLoader
   1 /*
   2  * Copyright (c) 2003, 2011, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  41 
  42     // number of threads currently using thread-local provider lists
  43     // tracked to allow an optimization if == 0
  44     private static volatile int threadListsUsed;
  45 
  46     // current system-wide provider list
  47     // Note volatile immutable object, so no synchronization needed.
  48     private static volatile ProviderList providerList;
  49 
  50     static {
  51         // set providerList to empty list first in case initialization somehow
  52         // triggers a getInstance() call (although that should not happen)
  53         providerList = ProviderList.EMPTY;
  54         providerList = ProviderList.fromSecurityProperties();
  55     }
  56 
  57     private Providers() {
  58         // empty
  59     }
  60 
  61     // we need special handling to resolve circularities when loading
  62     // signed JAR files during startup. The code below is part of that.
  63 




  64     // Basically, before we load data from a signed JAR file, we parse
  65     // the PKCS#7 file and verify the signature. We need a
  66     // CertificateFactory, Signatures, etc. to do that. We have to make
  67     // sure that we do not try to load the implementation from the JAR
  68     // file we are just verifying.
  69     //
  70     // To avoid that, we use different provider settings during JAR
  71     // verification.  However, we do not want those provider settings to
  72     // interfere with other parts of the system. Therefore, we make them local
  73     // to the Thread executing the JAR verification code.
  74     //
  75     // The code here is used by sun.security.util.SignatureFileVerifier.
  76     // See there for details.
  77 
  78     private static final String BACKUP_PROVIDER_CLASSNAME =
  79         "sun.security.provider.VerificationProvider";
  80 
  81     // Hardcoded classnames of providers to use for JAR verification.
  82     // MUST NOT be on the bootclasspath and not in signed JAR files.
  83     private static final String[] jarVerificationProviders = {
  84         "sun.security.provider.Sun",
  85         "sun.security.rsa.SunRsaSign",
  86         // Note: SunEC *is* in a signed JAR file, but it's not signed
  87         // by EC itself. So it's still safe to be listed here.
  88         "sun.security.ec.SunEC",
  89         BACKUP_PROVIDER_CLASSNAME,
  90     };
  91 
  92     // Return to Sun provider or its backup.
  93     // This method should only be called by
  94     // sun.security.util.ManifestEntryVerifier and java.security.SecureRandom.
  95     public static Provider getSunProvider() {
  96         try {
  97             Class<?> clazz = Class.forName(jarVerificationProviders[0]);
  98             return (Provider)clazz.newInstance();
  99         } catch (Exception e) {
 100             try {
 101                 Class<?> clazz = Class.forName(BACKUP_PROVIDER_CLASSNAME);
 102                 return (Provider)clazz.newInstance();
 103             } catch (Exception ee) {
 104                 throw new RuntimeException("Sun provider not found", e);
 105             }
 106         }
 107     }
 108 
 109     /**
 110      * Start JAR verification. This sets a special provider list for
 111      * the current thread. You MUST save the return value from this
 112      * method and you MUST call stopJarVerification() with that object
 113      * once you are done.
 114      */
 115     public static Object startJarVerification() {
 116         ProviderList currentList = getProviderList();
 117         ProviderList jarList = currentList.getJarList(jarVerificationProviders);




 118         // return the old thread-local provider list, usually null
 119         return beginThreadProviderList(jarList);
 120     }
 121 
 122     /**
 123      * Stop JAR verification. Call once you have completed JAR verification.
 124      */
 125     public static void stopJarVerification(Object obj) {
 126         // restore old thread-local provider list
 127         endThreadProviderList((ProviderList)obj);
 128     }
 129 
 130     /**
 131      * Return the current ProviderList. If the thread-local list is set,
 132      * it is returned. Otherwise, the system wide list is returned.
 133      */
 134     public static ProviderList getProviderList() {
 135         ProviderList list = getThreadProviderList();
 136         if (list == null) {
 137             list = getSystemProviderList();


   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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  41 
  42     // number of threads currently using thread-local provider lists
  43     // tracked to allow an optimization if == 0
  44     private static volatile int threadListsUsed;
  45 
  46     // current system-wide provider list
  47     // Note volatile immutable object, so no synchronization needed.
  48     private static volatile ProviderList providerList;
  49 
  50     static {
  51         // set providerList to empty list first in case initialization somehow
  52         // triggers a getInstance() call (although that should not happen)
  53         providerList = ProviderList.EMPTY;
  54         providerList = ProviderList.fromSecurityProperties();
  55     }
  56 
  57     private Providers() {
  58         // empty
  59     }
  60 
  61     // After the switch to modules, JDK providers are all in modules and JDK
  62     // no longer needs to load signed jars during start up.
  63     //
  64     // However, for earlier releases, it need special handling to resolve
  65     // circularities when loading signed JAR files during startup. The code
  66     // below is part of that.
  67     //
  68     // Basically, before we load data from a signed JAR file, we parse
  69     // the PKCS#7 file and verify the signature. We need a
  70     // CertificateFactory, Signatures, etc. to do that. We have to make
  71     // sure that we do not try to load the implementation from the JAR
  72     // file we are just verifying.
  73     //
  74     // To avoid that, we use different provider settings during JAR
  75     // verification.  However, we do not want those provider settings to
  76     // interfere with other parts of the system. Therefore, we make them local
  77     // to the Thread executing the JAR verification code.
  78     //
  79     // The code here is used by sun.security.util.SignatureFileVerifier.
  80     // See there for details.
  81 
  82     private static final String BACKUP_PROVIDER_CLASSNAME =
  83         "sun.security.provider.VerificationProvider";
  84 
  85     // Hardcoded names of providers to use for JAR verification.
  86     // MUST NOT be on the bootclasspath and not in signed JAR files.
  87     private static final String[] jarVerificationProviders = {
  88         "sun.security.provider.Sun",
  89         "sun.security.rsa.SunRsaSign",
  90         // Note: when SunEC is in a signed JAR file, it's not signed
  91         // by EC algorithms. So it's still safe to be listed here.
  92         "sun.security.ec.SunEC",

  93     };
  94 
  95     // Return to Sun provider or its backup.
  96     // This method should only be called by
  97     // sun.security.util.ManifestEntryVerifier and java.security.SecureRandom.
  98     public static Provider getSunProvider() {
  99         try {
 100             return new sun.security.provider.Sun();

 101         } catch (Exception e) {
 102             try {
 103                 Class<?> clazz = Class.forName(BACKUP_PROVIDER_CLASSNAME);
 104                 return (Provider)clazz.newInstance();
 105             } catch (Exception ee) {
 106                 throw new RuntimeException("Sun provider not found", e);
 107             }
 108         }
 109     }
 110 
 111     /**
 112      * Start JAR verification. This sets a special provider list for
 113      * the current thread. You MUST save the return value from this
 114      * method and you MUST call stopJarVerification() with that object
 115      * once you are done.
 116      */
 117     public static Object startJarVerification() {
 118         ProviderList currentList = getProviderList();
 119         ProviderList jarList = currentList.getJarList(jarVerificationProviders);
 120         if (jarList.size() < 3) {
 121             // add backup provider
 122             ProviderList.add(jarList, getSunProvider());
 123         }
 124         // return the old thread-local provider list, usually null
 125         return beginThreadProviderList(jarList);
 126     }
 127 
 128     /**
 129      * Stop JAR verification. Call once you have completed JAR verification.
 130      */
 131     public static void stopJarVerification(Object obj) {
 132         // restore old thread-local provider list
 133         endThreadProviderList((ProviderList)obj);
 134     }
 135 
 136     /**
 137      * Return the current ProviderList. If the thread-local list is set,
 138      * it is returned. Otherwise, the system wide list is returned.
 139      */
 140     public static ProviderList getProviderList() {
 141         ProviderList list = getThreadProviderList();
 142         if (list == null) {
 143             list = getSystemProviderList();