src/java.base/share/classes/sun/security/ssl/JsseJce.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
child 56692 7b0bde908f58
permissions -rw-r--r--
Initial TLSv1.3 Implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3957
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3957
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3957
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3957
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3957
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.interfaces.RSAPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.spec.*;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    32
import java.util.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.*;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    34
import sun.security.jca.ProviderList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.jca.Providers;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    36
import static sun.security.ssl.SunJSSE.cryptoProvider;
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
    37
import sun.security.util.ECUtil;
40416
5d91b2fd668c 8130181: Deprecate java.security.Provider(String, double, String), add Provider(Strin
valeriep
parents: 39563
diff changeset
    38
import static sun.security.util.SecurityConstants.PROVIDER_VER;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class contains a few static methods for interaction with the JCA/JCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * to obtain implementations, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
final class JsseJce {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    47
    static final boolean ALLOW_ECC =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    48
            Utilities.getBooleanProperty("com.sun.net.ssl.enableECC", true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
    50
    private static final ProviderList fipsProviderList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        // force FIPS flag initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        // Because isFIPS() is synchronized and cryptoProvider is not modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // after it completes, this also eliminates the need for any further
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        // synchronization when accessing cryptoProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (SunJSSE.isFIPS() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            fipsProviderList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            // Setup a ProviderList that can be used by the trust manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            // during certificate chain validation. All the crypto must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            // from the FIPS provider, but we also allow the required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            // certificate related services from the SUN provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            Provider sun = Security.getProvider("SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (sun == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                throw new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    ("FIPS mode: SUN provider must be installed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            Provider sunCerts = new SunCertificates(sun);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            fipsProviderList = ProviderList.newList(cryptoProvider, sunCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final class SunCertificates extends Provider {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9691
diff changeset
    75
        private static final long serialVersionUID = -3284138292032213752L;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9691
diff changeset
    76
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        SunCertificates(final Provider p) {
40416
5d91b2fd668c 8130181: Deprecate java.security.Provider(String, double, String), add Provider(Strin
valeriep
parents: 39563
diff changeset
    78
            super("SunCertificates", PROVIDER_VER, "SunJSSE internal");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10336
diff changeset
    80
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    // copy certificate related services from the Sun provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    for (Map.Entry<Object,Object> entry : p.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        String key = (String)entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        if (key.startsWith("CertPathValidator.")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                || key.startsWith("CertPathBuilder.")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                || key.startsWith("CertStore.")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                || key.startsWith("CertificateFactory.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                            put(key, entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * JCE transformation string for RSA with PKCS#1 v1.5 padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Can be used for encryption, decryption, signing, verifying.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   102
    static final String CIPHER_RSA_PKCS1 = "RSA/ECB/PKCS1Padding";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * JCE transformation string for the stream cipher RC4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   106
    static final String CIPHER_RC4 = "RC4";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * JCE transformation string for DES in CBC mode without padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   110
    static final String CIPHER_DES = "DES/CBC/NoPadding";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * JCE transformation string for (3-key) Triple DES in CBC mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * without padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   115
    static final String CIPHER_3DES = "DESede/CBC/NoPadding";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * JCE transformation string for AES in CBC mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * without padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   120
    static final String CIPHER_AES = "AES/CBC/NoPadding";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   122
     * JCE transformation string for AES in GCM mode
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   123
     * without padding.
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   124
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   125
    static final String CIPHER_AES_GCM = "AES/GCM/NoPadding";
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   126
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * JCA identifier string for DSA, i.e. a DSA with SHA-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   129
    static final String SIGNATURE_DSA = "DSA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * JCA identifier string for ECDSA, i.e. a ECDSA with SHA-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   133
    static final String SIGNATURE_ECDSA = "SHA1withECDSA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * JCA identifier string for Raw DSA, i.e. a DSA signature without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * hashing where the application provides the SHA-1 hash of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Note that the standard name is "NONEwithDSA" but we use "RawDSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * for compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   140
    static final String SIGNATURE_RAWDSA = "RawDSA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * JCA identifier string for Raw ECDSA, i.e. a DSA signature without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * hashing where the application provides the SHA-1 hash of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   145
    static final String SIGNATURE_RAWECDSA = "NONEwithECDSA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * JCA identifier string for Raw RSA, i.e. a RSA PKCS#1 v1.5 signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * without hashing where the application provides the hash of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Used for RSA client authentication with a 36 byte hash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   151
    static final String SIGNATURE_RAWRSA = "NONEwithRSA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * JCA identifier string for the SSL/TLS style RSA Signature. I.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * an signature using RSA with PKCS#1 v1.5 padding signing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * concatenation of an MD5 and SHA-1 digest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   157
    static final String SIGNATURE_SSLRSA = "MD5andSHA1withRSA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private JsseJce() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // no instantiation of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
34826
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   163
    static boolean isEcAvailable() {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   164
        return EcAvailability.isAvailable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
3957
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
   167
    static boolean isKerberosAvailable() {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   168
        return false;
3957
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
   169
    }
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
   170
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Return an JCE cipher implementation for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    static Cipher getCipher(String transformation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                return Cipher.getInstance(transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                return Cipher.getInstance(transformation, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } catch (NoSuchPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw new NoSuchAlgorithmException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Return an JCA signature implementation for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * The algorithm string should be one of the constants defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    static Signature getSignature(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return Signature.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            // reference equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (algorithm == SIGNATURE_SSLRSA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                // The SunPKCS11 provider currently does not support this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                // special algorithm. We allow a fallback in this case because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                // the SunJSSE implementation does the actual crypto using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                // a NONEwithRSA signature obtained from the cryptoProvider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                if (cryptoProvider.getService("Signature", algorithm) == null) {
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 5506
diff changeset
   204
                    // Calling Signature.getInstance() and catching the
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 5506
diff changeset
   205
                    // exception would be cleaner, but exceptions are a little
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 5506
diff changeset
   206
                    // expensive. So we check directly via getService().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        return Signature.getInstance(algorithm, "SunJSSE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    } catch (NoSuchProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        throw new NoSuchAlgorithmException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return Signature.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    static KeyGenerator getKeyGenerator(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return KeyGenerator.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return KeyGenerator.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    static KeyPairGenerator getKeyPairGenerator(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return KeyPairGenerator.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return KeyPairGenerator.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    static KeyAgreement getKeyAgreement(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return KeyAgreement.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            return KeyAgreement.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    static Mac getMac(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return Mac.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return Mac.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    static KeyFactory getKeyFactory(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return KeyFactory.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return KeyFactory.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
39563
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   263
    static AlgorithmParameters getAlgorithmParameters(String algorithm)
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   264
            throws NoSuchAlgorithmException {
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   265
        if (cryptoProvider == null) {
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   266
            return AlgorithmParameters.getInstance(algorithm);
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   267
        } else {
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   268
            return AlgorithmParameters.getInstance(algorithm, cryptoProvider);
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   269
        }
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   270
    }
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   271
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    static SecureRandom getSecureRandom() throws KeyManagementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        // Try "PKCS11" first. If that is not supported, iterate through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // the provider and return the first working implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return SecureRandom.getInstance("PKCS11", cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        for (Provider.Service s : cryptoProvider.getServices()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (s.getType().equals("SecureRandom")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                try {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   286
                    return SecureRandom.getInstance(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   287
                            s.getAlgorithm(), cryptoProvider);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                } catch (NoSuchAlgorithmException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        throw new KeyManagementException("FIPS mode: no SecureRandom "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            + " implementation found in provider " + cryptoProvider.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    static MessageDigest getMD5() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return getMessageDigest("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    static MessageDigest getSHA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return getMessageDigest("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    static MessageDigest getMessageDigest(String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                return MessageDigest.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                return MessageDigest.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            throw new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        ("Algorithm " + algorithm + " not available", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    static int getRSAKeyLength(PublicKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        BigInteger modulus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            modulus = ((RSAPublicKey)key).getModulus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            RSAPublicKeySpec spec = getRSAPublicKeySpec(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            modulus = spec.getModulus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return modulus.bitLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    static RSAPublicKeySpec getRSAPublicKeySpec(PublicKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            RSAPublicKey rsaKey = (RSAPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return new RSAPublicKeySpec(rsaKey.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                        rsaKey.getPublicExponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            KeyFactory factory = JsseJce.getKeyFactory("RSA");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   337
            return factory.getKeySpec(key, RSAPublicKeySpec.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9691
diff changeset
   339
            throw new RuntimeException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    static ECParameterSpec getECParameterSpec(String namedCurveOid) {
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
   344
        return ECUtil.getECParameterSpec(cryptoProvider, namedCurveOid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    static String getNamedCurveOid(ECParameterSpec params) {
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
   348
        return ECUtil.getCurveName(cryptoProvider, params);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    static ECPoint decodePoint(byte[] encoded, EllipticCurve curve)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            throws java.io.IOException {
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
   353
        return ECUtil.decodePoint(encoded, curve);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    static byte[] encodePoint(ECPoint point, EllipticCurve curve) {
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
   357
        return ECUtil.encodePoint(point, curve);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    // In FIPS mode, set thread local providers; otherwise a no-op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    // Must be paired with endFipsProvider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    static Object beginFipsProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (fipsProviderList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return Providers.beginThreadProviderList(fipsProviderList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    static void endFipsProvider(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (fipsProviderList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            Providers.endThreadProviderList((ProviderList)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
34826
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   376
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   377
    // lazy initialization holder class idiom for static default parameters
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   378
    //
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   379
    // See Effective Java Second Edition: Item 71.
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   380
    private static class EcAvailability {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   381
        // Is EC crypto available?
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   382
        private static final boolean isAvailable;
34826
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   383
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   384
        static {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   385
            boolean mediator = true;
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   386
            try {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   387
                JsseJce.getSignature(SIGNATURE_ECDSA);
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   388
                JsseJce.getSignature(SIGNATURE_RAWECDSA);
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   389
                JsseJce.getKeyAgreement("ECDH");
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   390
                JsseJce.getKeyFactory("EC");
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   391
                JsseJce.getKeyPairGenerator("EC");
39563
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   392
                JsseJce.getAlgorithmParameters("EC");
34826
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   393
            } catch (Exception e) {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   394
                mediator = false;
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   395
            }
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   396
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   397
            isAvailable = mediator;
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   398
        }
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   399
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
}