< prev index next >

src/java.base/share/classes/java/security/Provider.java

Print this page

   1 /*
   2  * Copyright (c) 1996, 2023, 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
  23  * questions.
  24  */
  25 
  26 package java.security;
  27 
  28 import jdk.internal.event.SecurityProviderServiceEvent;
  29 

  30 import java.io.*;

  31 import java.util.*;
  32 import static java.util.Locale.ENGLISH;
  33 import java.lang.ref.*;
  34 import java.lang.reflect.*;
  35 import java.util.function.BiConsumer;
  36 import java.util.function.BiFunction;
  37 import java.util.function.Function;
  38 import java.util.concurrent.ConcurrentHashMap;
  39 
  40 /**
  41  * This class represents a "provider" for the
  42  * Java Security API, where a provider implements some or all parts of
  43  * Java Security. Services that a provider may implement include:
  44  *
  45  * <ul>
  46  *
  47  * <li>Algorithms (such as DSA, RSA, or SHA-256).
  48  *
  49  * <li>Key generation, conversion, and management facilities (such as for
  50  * algorithm-specific keys).

1539             return lowerString.hashCode();
1540         }
1541 
1542         public boolean equals(Object obj) {
1543             if (this == obj) {
1544                 return true;
1545             }
1546             return obj instanceof UString other
1547                     && lowerString.equals(other.lowerString);
1548         }
1549 
1550         public String toString() {
1551             return string;
1552         }
1553     }
1554 
1555     // describe relevant properties of a type of engine
1556     private static class EngineDescription {
1557         final String name;
1558         final boolean supportsParameter;
1559         final String constructorParameterClassName;
1560 
1561         EngineDescription(String name, boolean sp, String paramName) {
1562             this.name = name;
1563             this.supportsParameter = sp;
1564             this.constructorParameterClassName = paramName;
1565         }
1566     }
1567 
1568     // built in knowledge of the engine types shipped as part of the JDK
1569     private static final Map<String,EngineDescription> knownEngines;
1570 
1571     private static void addEngine(String name, boolean sp, String paramName) {
1572         EngineDescription ed = new EngineDescription(name, sp, paramName);
1573         // also index by canonical name to avoid toLowerCase() for some lookups
1574         knownEngines.put(name.toLowerCase(ENGLISH), ed);
1575         knownEngines.put(name, ed);
1576     }
1577 
1578     static {
1579         knownEngines = new HashMap<>();
1580         // JCA
1581         addEngine("AlgorithmParameterGenerator",        false, null);
1582         addEngine("AlgorithmParameters",                false, null);
1583         addEngine("KeyFactory",                         false, null);
1584         addEngine("KeyPairGenerator",                   false, null);
1585         addEngine("KeyStore",                           false, null);
1586         addEngine("MessageDigest",                      false, null);
1587         addEngine("SecureRandom",                       false,
1588                 "java.security.SecureRandomParameters");
1589         addEngine("Signature",                          true,  null);
1590         addEngine("CertificateFactory",                 false, null);
1591         addEngine("CertPathBuilder",                    false, null);
1592         addEngine("CertPathValidator",                  false, null);
1593         addEngine("CertStore",                          false,
1594                             "java.security.cert.CertStoreParameters");
1595         // JCE
1596         addEngine("Cipher",                             true,  null);
1597         addEngine("ExemptionMechanism",                 false, null);
1598         addEngine("Mac",                                true,  null);
1599         addEngine("KeyAgreement",                       true,  null);
1600         addEngine("KeyGenerator",                       false, null);
1601         addEngine("SecretKeyFactory",                   false, null);
1602         addEngine("KEM",                                true,  null);
1603         // JSSE
1604         addEngine("KeyManagerFactory",                  false, null);
1605         addEngine("SSLContext",                         false, null);
1606         addEngine("TrustManagerFactory",                false, null);
1607         // JGSS
1608         addEngine("GssApiMechanism",                    false, null);
1609         // SASL
1610         addEngine("SaslClientFactory",                  false, null);
1611         addEngine("SaslServerFactory",                  false, null);
1612         // POLICY


1613         addEngine("Policy",                             false,
1614                             "java.security.Policy$Parameters");
1615         // CONFIGURATION
1616         addEngine("Configuration",                      false,
1617                             "javax.security.auth.login.Configuration$Parameters");
1618         // XML DSig
1619         addEngine("XMLSignatureFactory",                false, null);
1620         addEngine("KeyInfoFactory",                     false, null);
1621         addEngine("TransformService",                   false, null);
1622         // Smart Card I/O
1623         addEngine("TerminalFactory",                    false,
1624                             "java.lang.Object");
1625     }
1626 
1627     // get the "standard" (mixed-case) engine name for arbitrary case engine name
1628     // if there is no known engine by that name, return s
1629     private static String getEngineName(String s) {
1630         // try original case first, usually correct
1631         EngineDescription e = knownEngines.get(s);
1632         if (e == null) {
1633             e = knownEngines.get(s.toLowerCase(ENGLISH));
1634         }
1635         return (e == null) ? s : e.name;
1636     }
1637 
1638     /**
1639      * The description of a security service. It encapsulates the properties
1640      * of a service and contains a factory method to obtain new implementation
1641      * instances of this service.
1642      *
1643      * <p>Each service has a provider that offers the service, a type,
1644      * an algorithm name, and the name of the class that implements the

1878         public Object newInstance(Object constructorParameter)
1879                 throws NoSuchAlgorithmException {
1880             if (!registered) {
1881                 if (provider.getService(type, algorithm) != this) {
1882                     throw new NoSuchAlgorithmException
1883                         ("Service not registered with Provider "
1884                         + provider.getName() + ": " + this);
1885                 }
1886                 registered = true;
1887             }
1888             Class<?> ctrParamClz;
1889             try {
1890                 EngineDescription cap = engineDescription;
1891                 if (cap == null) {
1892                     // unknown engine type, use generic code
1893                     // this is the code path future for non-core
1894                     // optional packages
1895                     ctrParamClz = constructorParameter == null?
1896                         null : constructorParameter.getClass();
1897                 } else {
1898                     ctrParamClz = cap.constructorParameterClassName == null?
1899                         null : Class.forName(cap.constructorParameterClassName);
1900                     if (constructorParameter != null) {
1901                         if (ctrParamClz == null) {
1902                             throw new InvalidParameterException
1903                                 ("constructorParameter not used with " + type
1904                                 + " engines");
1905                         } else {
1906                             Class<?> argClass = constructorParameter.getClass();
1907                             if (!ctrParamClz.isAssignableFrom(argClass)) {
1908                                 throw new InvalidParameterException
1909                                     ("constructorParameter must be instanceof "
1910                                     + cap.constructorParameterClassName.replace('$', '.')
1911                                     + " for engine type " + type);
1912                             }
1913                         }
1914                     }
1915                 }
1916                 // constructorParameter can be null if not provided
1917                 return newInstanceUtil(ctrParamClz, constructorParameter);
1918             } catch (NoSuchAlgorithmException e) {
1919                 throw e;
1920             } catch (InvocationTargetException e) {
1921                 throw new NoSuchAlgorithmException
1922                     ("Error constructing implementation (algorithm: "
1923                     + algorithm + ", provider: " + provider.getName()
1924                     + ", class: " + className + ")", e.getCause());
1925             } catch (Exception e) {
1926                 throw new NoSuchAlgorithmException
1927                     ("Error constructing implementation (algorithm: "
1928                     + algorithm + ", provider: " + provider.getName()
1929                     + ", class: " + className + ")", e);
1930             }

   1 /*
   2  * Copyright (c) 1996, 2024, 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
  23  * questions.
  24  */
  25 
  26 package java.security;
  27 
  28 import jdk.internal.event.SecurityProviderServiceEvent;
  29 
  30 import javax.security.auth.login.Configuration;
  31 import java.io.*;
  32 import java.security.cert.CertStoreParameters;
  33 import java.util.*;
  34 import static java.util.Locale.ENGLISH;
  35 import java.lang.ref.*;
  36 import java.lang.reflect.*;
  37 import java.util.function.BiConsumer;
  38 import java.util.function.BiFunction;
  39 import java.util.function.Function;
  40 import java.util.concurrent.ConcurrentHashMap;
  41 
  42 /**
  43  * This class represents a "provider" for the
  44  * Java Security API, where a provider implements some or all parts of
  45  * Java Security. Services that a provider may implement include:
  46  *
  47  * <ul>
  48  *
  49  * <li>Algorithms (such as DSA, RSA, or SHA-256).
  50  *
  51  * <li>Key generation, conversion, and management facilities (such as for
  52  * algorithm-specific keys).

1541             return lowerString.hashCode();
1542         }
1543 
1544         public boolean equals(Object obj) {
1545             if (this == obj) {
1546                 return true;
1547             }
1548             return obj instanceof UString other
1549                     && lowerString.equals(other.lowerString);
1550         }
1551 
1552         public String toString() {
1553             return string;
1554         }
1555     }
1556 
1557     // describe relevant properties of a type of engine
1558     private static class EngineDescription {
1559         final String name;
1560         final boolean supportsParameter;
1561         final Class<?> constructorParameterClass;
1562 
1563         EngineDescription(String name, boolean sp, Class<?> constructorParameterClass) {
1564             this.name = name;
1565             this.supportsParameter = sp;
1566             this.constructorParameterClass = constructorParameterClass;
1567         }
1568     }
1569 
1570     // built in knowledge of the engine types shipped as part of the JDK
1571     private static final Map<String,EngineDescription> knownEngines;
1572 
1573     private static void addEngine(String name, boolean sp, Class<?> constructorParameterClass) {
1574         EngineDescription ed = new EngineDescription(name, sp, constructorParameterClass);
1575         // also index by canonical name to avoid toLowerCase() for some lookups
1576         knownEngines.put(name.toLowerCase(ENGLISH), ed);
1577         knownEngines.put(name, ed);
1578     }
1579 
1580     static {
1581         knownEngines = new HashMap<>();
1582         // JCA
1583         addEngine("AlgorithmParameterGenerator",        false, null);
1584         addEngine("AlgorithmParameters",                false, null);
1585         addEngine("KeyFactory",                         false, null);
1586         addEngine("KeyPairGenerator",                   false, null);
1587         addEngine("KeyStore",                           false, null);
1588         addEngine("MessageDigest",                      false, null);
1589         addEngine("SecureRandom",                       false,
1590                 SecureRandomParameters.class);
1591         addEngine("Signature",                          true,  null);
1592         addEngine("CertificateFactory",                 false, null);
1593         addEngine("CertPathBuilder",                    false, null);
1594         addEngine("CertPathValidator",                  false, null);
1595         addEngine("CertStore",                          false,
1596                 CertStoreParameters.class);
1597         // JCE
1598         addEngine("Cipher",                             true,  null);
1599         addEngine("ExemptionMechanism",                 false, null);
1600         addEngine("Mac",                                true,  null);
1601         addEngine("KeyAgreement",                       true,  null);
1602         addEngine("KeyGenerator",                       false, null);
1603         addEngine("SecretKeyFactory",                   false, null);
1604         addEngine("KEM",                                true,  null);
1605         // JSSE
1606         addEngine("KeyManagerFactory",                  false, null);
1607         addEngine("SSLContext",                         false, null);
1608         addEngine("TrustManagerFactory",                false, null);
1609         // JGSS
1610         addEngine("GssApiMechanism",                    false, null);
1611         // SASL
1612         addEngine("SaslClientFactory",                  false, null);
1613         addEngine("SaslServerFactory",                  false, null);
1614         // POLICY
1615         @SuppressWarnings("removal")
1616         Class<Policy.Parameters> policyParams = Policy.Parameters.class;
1617         addEngine("Policy",                             false,
1618                 policyParams);
1619         // CONFIGURATION
1620         addEngine("Configuration",                      false,
1621                 Configuration.Parameters.class);
1622         // XML DSig
1623         addEngine("XMLSignatureFactory",                false, null);
1624         addEngine("KeyInfoFactory",                     false, null);
1625         addEngine("TransformService",                   false, null);
1626         // Smart Card I/O
1627         addEngine("TerminalFactory",                    false,
1628                             Object.class);
1629     }
1630 
1631     // get the "standard" (mixed-case) engine name for arbitrary case engine name
1632     // if there is no known engine by that name, return s
1633     private static String getEngineName(String s) {
1634         // try original case first, usually correct
1635         EngineDescription e = knownEngines.get(s);
1636         if (e == null) {
1637             e = knownEngines.get(s.toLowerCase(ENGLISH));
1638         }
1639         return (e == null) ? s : e.name;
1640     }
1641 
1642     /**
1643      * The description of a security service. It encapsulates the properties
1644      * of a service and contains a factory method to obtain new implementation
1645      * instances of this service.
1646      *
1647      * <p>Each service has a provider that offers the service, a type,
1648      * an algorithm name, and the name of the class that implements the

1882         public Object newInstance(Object constructorParameter)
1883                 throws NoSuchAlgorithmException {
1884             if (!registered) {
1885                 if (provider.getService(type, algorithm) != this) {
1886                     throw new NoSuchAlgorithmException
1887                         ("Service not registered with Provider "
1888                         + provider.getName() + ": " + this);
1889                 }
1890                 registered = true;
1891             }
1892             Class<?> ctrParamClz;
1893             try {
1894                 EngineDescription cap = engineDescription;
1895                 if (cap == null) {
1896                     // unknown engine type, use generic code
1897                     // this is the code path future for non-core
1898                     // optional packages
1899                     ctrParamClz = constructorParameter == null?
1900                         null : constructorParameter.getClass();
1901                 } else {
1902                     ctrParamClz = cap.constructorParameterClass;

1903                     if (constructorParameter != null) {
1904                         if (ctrParamClz == null) {
1905                             throw new InvalidParameterException
1906                                 ("constructorParameter not used with " + type
1907                                 + " engines");
1908                         } else {
1909                             Class<?> argClass = constructorParameter.getClass();
1910                             if (!ctrParamClz.isAssignableFrom(argClass)) {
1911                                 throw new InvalidParameterException
1912                                     ("constructorParameter must be instanceof "
1913                                     + cap.constructorParameterClass.getName().replace('$', '.')
1914                                     + " for engine type " + type);
1915                             }
1916                         }
1917                     }
1918                 }
1919                 // constructorParameter can be null if not provided
1920                 return newInstanceUtil(ctrParamClz, constructorParameter);
1921             } catch (NoSuchAlgorithmException e) {
1922                 throw e;
1923             } catch (InvocationTargetException e) {
1924                 throw new NoSuchAlgorithmException
1925                     ("Error constructing implementation (algorithm: "
1926                     + algorithm + ", provider: " + provider.getName()
1927                     + ", class: " + className + ")", e.getCause());
1928             } catch (Exception e) {
1929                 throw new NoSuchAlgorithmException
1930                     ("Error constructing implementation (algorithm: "
1931                     + algorithm + ", provider: " + provider.getName()
1932                     + ", class: " + className + ")", e);
1933             }
< prev index next >