jdk/src/java.base/share/classes/sun/security/jca/Providers.java
changeset 31270 e6470b24700d
parent 25859 3317bb8137f4
child 36511 9d0388c6b336
equal deleted inserted replaced
31269:14968253ce7e 31270:e6470b24700d
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    56 
    56 
    57     private Providers() {
    57     private Providers() {
    58         // empty
    58         // empty
    59     }
    59     }
    60 
    60 
    61     // we need special handling to resolve circularities when loading
    61     // After the switch to modules, JDK providers are all in modules and JDK
    62     // signed JAR files during startup. The code below is part of that.
    62     // no longer needs to load signed jars during start up.
    63 
    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     //
    64     // Basically, before we load data from a signed JAR file, we parse
    68     // Basically, before we load data from a signed JAR file, we parse
    65     // the PKCS#7 file and verify the signature. We need a
    69     // the PKCS#7 file and verify the signature. We need a
    66     // CertificateFactory, Signatures, etc. to do that. We have to make
    70     // CertificateFactory, Signatures, etc. to do that. We have to make
    67     // sure that we do not try to load the implementation from the JAR
    71     // sure that we do not try to load the implementation from the JAR
    68     // file we are just verifying.
    72     // file we are just verifying.
    73     // to the Thread executing the JAR verification code.
    77     // to the Thread executing the JAR verification code.
    74     //
    78     //
    75     // The code here is used by sun.security.util.SignatureFileVerifier.
    79     // The code here is used by sun.security.util.SignatureFileVerifier.
    76     // See there for details.
    80     // See there for details.
    77 
    81 
    78     private static final String BACKUP_PROVIDER_CLASSNAME =
    82     // Hardcoded names of providers to use for JAR verification.
    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     // MUST NOT be on the bootclasspath and not in signed JAR files.
    83     private static final String[] jarVerificationProviders = {
    84     private static final String[] jarVerificationProviders = {
    84         "sun.security.provider.Sun",
    85         "SUN",
    85         "sun.security.rsa.SunRsaSign",
    86         "SunRsaSign",
    86         // Note: SunEC *is* in a signed JAR file, but it's not signed
    87         // Note: when SunEC is in a signed JAR file, it's not signed
    87         // by EC itself. So it's still safe to be listed here.
    88         // by EC algorithms. So it's still safe to be listed here.
       
    89         // Need to use class name here, otherwise it cannot be loaded for
       
    90         // jar verification. Only those providers in java.base are created
       
    91         // directly by ProviderConfig class.
    88         "sun.security.ec.SunEC",
    92         "sun.security.ec.SunEC",
    89         BACKUP_PROVIDER_CLASSNAME,
       
    90     };
    93     };
    91 
    94 
    92     // Return to Sun provider or its backup.
    95     // Return Sun provider.
    93     // This method should only be called by
    96     // This method should only be called by
    94     // sun.security.util.ManifestEntryVerifier and java.security.SecureRandom.
    97     // sun.security.util.ManifestEntryVerifier and java.security.SecureRandom.
    95     public static Provider getSunProvider() {
    98     public static Provider getSunProvider() {
    96         try {
    99         return new sun.security.provider.Sun();
    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     }
   100     }
   108 
   101 
   109     /**
   102     /**
   110      * Start JAR verification. This sets a special provider list for
   103      * Start JAR verification. This sets a special provider list for
   111      * the current thread. You MUST save the return value from this
   104      * the current thread. You MUST save the return value from this
   113      * once you are done.
   106      * once you are done.
   114      */
   107      */
   115     public static Object startJarVerification() {
   108     public static Object startJarVerification() {
   116         ProviderList currentList = getProviderList();
   109         ProviderList currentList = getProviderList();
   117         ProviderList jarList = currentList.getJarList(jarVerificationProviders);
   110         ProviderList jarList = currentList.getJarList(jarVerificationProviders);
       
   111         if (jarList.getProvider("SUN") == null) {
       
   112             // add backup provider
       
   113             Provider p;
       
   114             try {
       
   115                 p = new sun.security.provider.VerificationProvider();
       
   116             } catch (Exception e) {
       
   117                 throw new RuntimeException("Missing provider for jar verification", e);
       
   118             }
       
   119             ProviderList.add(jarList, p);
       
   120         }
   118         // return the old thread-local provider list, usually null
   121         // return the old thread-local provider list, usually null
   119         return beginThreadProviderList(jarList);
   122         return beginThreadProviderList(jarList);
   120     }
   123     }
   121 
   124 
   122     /**
   125     /**