src/java.base/share/classes/sun/security/ssl/JsseJce.java
changeset 53734 cb1642ccc732
parent 51773 720fd6544b03
--- a/src/java.base/share/classes/sun/security/ssl/JsseJce.java	Tue Feb 12 15:19:25 2019 -0500
+++ b/src/java.base/share/classes/sun/security/ssl/JsseJce.java	Tue Feb 12 13:36:15 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -29,13 +29,7 @@
 import java.security.*;
 import java.security.interfaces.RSAPublicKey;
 import java.security.spec.*;
-import java.util.*;
 import javax.crypto.*;
-import sun.security.jca.ProviderList;
-import sun.security.jca.Providers;
-import static sun.security.ssl.SunJSSE.cryptoProvider;
-import sun.security.util.ECUtil;
-import static sun.security.util.SecurityConstants.PROVIDER_VER;
 
 /**
  * This class contains a few static methods for interaction with the JCA/JCE
@@ -47,54 +41,6 @@
     static final boolean ALLOW_ECC =
             Utilities.getBooleanProperty("com.sun.net.ssl.enableECC", true);
 
-    private static final ProviderList fipsProviderList;
-
-    static {
-        // force FIPS flag initialization
-        // Because isFIPS() is synchronized and cryptoProvider is not modified
-        // after it completes, this also eliminates the need for any further
-        // synchronization when accessing cryptoProvider
-        if (SunJSSE.isFIPS() == false) {
-            fipsProviderList = null;
-        } else {
-            // Setup a ProviderList that can be used by the trust manager
-            // during certificate chain validation. All the crypto must be
-            // from the FIPS provider, but we also allow the required
-            // certificate related services from the SUN provider.
-            Provider sun = Security.getProvider("SUN");
-            if (sun == null) {
-                throw new RuntimeException
-                    ("FIPS mode: SUN provider must be installed");
-            }
-            Provider sunCerts = new SunCertificates(sun);
-            fipsProviderList = ProviderList.newList(cryptoProvider, sunCerts);
-        }
-    }
-
-    private static final class SunCertificates extends Provider {
-        private static final long serialVersionUID = -3284138292032213752L;
-
-        SunCertificates(final Provider p) {
-            super("SunCertificates", PROVIDER_VER, "SunJSSE internal");
-            AccessController.doPrivileged(new PrivilegedAction<Object>() {
-                @Override
-                public Object run() {
-                    // copy certificate related services from the Sun provider
-                    for (Map.Entry<Object,Object> entry : p.entrySet()) {
-                        String key = (String)entry.getKey();
-                        if (key.startsWith("CertPathValidator.")
-                                || key.startsWith("CertPathBuilder.")
-                                || key.startsWith("CertStore.")
-                                || key.startsWith("CertificateFactory.")) {
-                            put(key, entry.getValue());
-                        }
-                    }
-                    return null;
-                }
-            });
-        }
-    }
-
     /**
      * JCE transformation string for RSA with PKCS#1 v1.5 padding.
      * Can be used for encryption, decryption, signing, verifying.
@@ -180,153 +126,6 @@
         return EcAvailability.isAvailable;
     }
 
-    /**
-     * Return an JCE cipher implementation for the specified algorithm.
-     */
-    static Cipher getCipher(String transformation)
-            throws NoSuchAlgorithmException {
-        try {
-            if (cryptoProvider == null) {
-                return Cipher.getInstance(transformation);
-            } else {
-                return Cipher.getInstance(transformation, cryptoProvider);
-            }
-        } catch (NoSuchPaddingException e) {
-            throw new NoSuchAlgorithmException(e);
-        }
-    }
-
-    /**
-     * Return an JCA signature implementation for the specified algorithm.
-     * The algorithm string should be one of the constants defined
-     * in this class.
-     */
-    static Signature getSignature(String algorithm)
-            throws NoSuchAlgorithmException {
-        if (cryptoProvider == null) {
-            return Signature.getInstance(algorithm);
-        } else {
-            // reference equality
-            if (algorithm == SIGNATURE_SSLRSA) {
-                // The SunPKCS11 provider currently does not support this
-                // special algorithm. We allow a fallback in this case because
-                // the SunJSSE implementation does the actual crypto using
-                // a NONEwithRSA signature obtained from the cryptoProvider.
-                if (cryptoProvider.getService("Signature", algorithm) == null) {
-                    // Calling Signature.getInstance() and catching the
-                    // exception would be cleaner, but exceptions are a little
-                    // expensive. So we check directly via getService().
-                    try {
-                        return Signature.getInstance(algorithm, "SunJSSE");
-                    } catch (NoSuchProviderException e) {
-                        throw new NoSuchAlgorithmException(e);
-                    }
-                }
-            }
-            return Signature.getInstance(algorithm, cryptoProvider);
-        }
-    }
-
-    static KeyGenerator getKeyGenerator(String algorithm)
-            throws NoSuchAlgorithmException {
-        if (cryptoProvider == null) {
-            return KeyGenerator.getInstance(algorithm);
-        } else {
-            return KeyGenerator.getInstance(algorithm, cryptoProvider);
-        }
-    }
-
-    static KeyPairGenerator getKeyPairGenerator(String algorithm)
-            throws NoSuchAlgorithmException {
-        if (cryptoProvider == null) {
-            return KeyPairGenerator.getInstance(algorithm);
-        } else {
-            return KeyPairGenerator.getInstance(algorithm, cryptoProvider);
-        }
-    }
-
-    static KeyAgreement getKeyAgreement(String algorithm)
-            throws NoSuchAlgorithmException {
-        if (cryptoProvider == null) {
-            return KeyAgreement.getInstance(algorithm);
-        } else {
-            return KeyAgreement.getInstance(algorithm, cryptoProvider);
-        }
-    }
-
-    static Mac getMac(String algorithm)
-            throws NoSuchAlgorithmException {
-        if (cryptoProvider == null) {
-            return Mac.getInstance(algorithm);
-        } else {
-            return Mac.getInstance(algorithm, cryptoProvider);
-        }
-    }
-
-    static KeyFactory getKeyFactory(String algorithm)
-            throws NoSuchAlgorithmException {
-        if (cryptoProvider == null) {
-            return KeyFactory.getInstance(algorithm);
-        } else {
-            return KeyFactory.getInstance(algorithm, cryptoProvider);
-        }
-    }
-
-    static AlgorithmParameters getAlgorithmParameters(String algorithm)
-            throws NoSuchAlgorithmException {
-        if (cryptoProvider == null) {
-            return AlgorithmParameters.getInstance(algorithm);
-        } else {
-            return AlgorithmParameters.getInstance(algorithm, cryptoProvider);
-        }
-    }
-
-    static SecureRandom getSecureRandom() throws KeyManagementException {
-        if (cryptoProvider == null) {
-            return new SecureRandom();
-        }
-        // Try "PKCS11" first. If that is not supported, iterate through
-        // the provider and return the first working implementation.
-        try {
-            return SecureRandom.getInstance("PKCS11", cryptoProvider);
-        } catch (NoSuchAlgorithmException e) {
-            // ignore
-        }
-        for (Provider.Service s : cryptoProvider.getServices()) {
-            if (s.getType().equals("SecureRandom")) {
-                try {
-                    return SecureRandom.getInstance(
-                            s.getAlgorithm(), cryptoProvider);
-                } catch (NoSuchAlgorithmException ee) {
-                    // ignore
-                }
-            }
-        }
-        throw new KeyManagementException("FIPS mode: no SecureRandom "
-            + " implementation found in provider " + cryptoProvider.getName());
-    }
-
-    static MessageDigest getMD5() {
-        return getMessageDigest("MD5");
-    }
-
-    static MessageDigest getSHA() {
-        return getMessageDigest("SHA");
-    }
-
-    static MessageDigest getMessageDigest(String algorithm) {
-        try {
-            if (cryptoProvider == null) {
-                return MessageDigest.getInstance(algorithm);
-            } else {
-                return MessageDigest.getInstance(algorithm, cryptoProvider);
-            }
-        } catch (NoSuchAlgorithmException e) {
-            throw new RuntimeException
-                        ("Algorithm " + algorithm + " not available", e);
-        }
-    }
-
     static int getRSAKeyLength(PublicKey key) {
         BigInteger modulus;
         if (key instanceof RSAPublicKey) {
@@ -345,47 +144,13 @@
                                         rsaKey.getPublicExponent());
         }
         try {
-            KeyFactory factory = JsseJce.getKeyFactory("RSA");
+            KeyFactory factory = KeyFactory.getInstance("RSA");
             return factory.getKeySpec(key, RSAPublicKeySpec.class);
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }
 
-    static ECParameterSpec getECParameterSpec(String namedCurveOid) {
-        return ECUtil.getECParameterSpec(cryptoProvider, namedCurveOid);
-    }
-
-    static String getNamedCurveOid(ECParameterSpec params) {
-        return ECUtil.getCurveName(cryptoProvider, params);
-    }
-
-    static ECPoint decodePoint(byte[] encoded, EllipticCurve curve)
-            throws java.io.IOException {
-        return ECUtil.decodePoint(encoded, curve);
-    }
-
-    static byte[] encodePoint(ECPoint point, EllipticCurve curve) {
-        return ECUtil.encodePoint(point, curve);
-    }
-
-    // In FIPS mode, set thread local providers; otherwise a no-op.
-    // Must be paired with endFipsProvider.
-    static Object beginFipsProvider() {
-        if (fipsProviderList == null) {
-            return null;
-        } else {
-            return Providers.beginThreadProviderList(fipsProviderList);
-        }
-    }
-
-    static void endFipsProvider(Object o) {
-        if (fipsProviderList != null) {
-            Providers.endThreadProviderList((ProviderList)o);
-        }
-    }
-
-
     // lazy initialization holder class idiom for static default parameters
     //
     // See Effective Java Second Edition: Item 71.
@@ -396,12 +161,12 @@
         static {
             boolean mediator = true;
             try {
-                JsseJce.getSignature(SIGNATURE_ECDSA);
-                JsseJce.getSignature(SIGNATURE_RAWECDSA);
-                JsseJce.getKeyAgreement("ECDH");
-                JsseJce.getKeyFactory("EC");
-                JsseJce.getKeyPairGenerator("EC");
-                JsseJce.getAlgorithmParameters("EC");
+                Signature.getInstance(SIGNATURE_ECDSA);
+                Signature.getInstance(SIGNATURE_RAWECDSA);
+                KeyAgreement.getInstance("ECDH");
+                KeyFactory.getInstance("EC");
+                KeyPairGenerator.getInstance("EC");
+                AlgorithmParameters.getInstance("EC");
             } catch (Exception e) {
                 mediator = false;
             }