src/java.base/share/classes/sun/security/ssl/JsseJce.java
author xuelei
Mon, 25 Jun 2018 08:14:11 -0700
branchJDK-8145252-TLS13-branch
changeset 56806 32a737f51e37
parent 56692 7b0bde908f58
permissions -rw-r--r--
Support RSASSS-PSS for TLS 1.2
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";
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56692
diff changeset
   103
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * JCE transformation string for the stream cipher RC4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   107
    static final String CIPHER_RC4 = "RC4";
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56692
diff changeset
   108
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * JCE transformation string for DES in CBC mode without padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   112
    static final String CIPHER_DES = "DES/CBC/NoPadding";
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56692
diff changeset
   113
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * JCE transformation string for (3-key) Triple DES in CBC mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * without padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   118
    static final String CIPHER_3DES = "DESede/CBC/NoPadding";
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56692
diff changeset
   119
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * JCE transformation string for AES in CBC mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * without padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   124
    static final String CIPHER_AES = "AES/CBC/NoPadding";
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56692
diff changeset
   125
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   127
     * JCE transformation string for AES in GCM mode
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   128
     * without padding.
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   129
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   130
    static final String CIPHER_AES_GCM = "AES/GCM/NoPadding";
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56692
diff changeset
   131
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   132
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * JCA identifier string for DSA, i.e. a DSA with SHA-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   135
    static final String SIGNATURE_DSA = "DSA";
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56692
diff changeset
   136
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * JCA identifier string for ECDSA, i.e. a ECDSA with SHA-1.
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_ECDSA = "SHA1withECDSA";
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56692
diff changeset
   141
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * JCA identifier string for Raw DSA, i.e. a DSA signature without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * hashing where the application provides the SHA-1 hash of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Note that the standard name is "NONEwithDSA" but we use "RawDSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * for compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   148
    static final String SIGNATURE_RAWDSA = "RawDSA";
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56692
diff changeset
   149
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * JCA identifier string for Raw ECDSA, i.e. a DSA signature without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * hashing where the application provides the SHA-1 hash of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   154
    static final String SIGNATURE_RAWECDSA = "NONEwithECDSA";
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56692
diff changeset
   155
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * JCA identifier string for Raw RSA, i.e. a RSA PKCS#1 v1.5 signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * without hashing where the application provides the hash of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Used for RSA client authentication with a 36 byte hash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   161
    static final String SIGNATURE_RAWRSA = "NONEwithRSA";
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56692
diff changeset
   162
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * JCA identifier string for the SSL/TLS style RSA Signature. I.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * an signature using RSA with PKCS#1 v1.5 padding signing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * concatenation of an MD5 and SHA-1 digest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   168
    static final String SIGNATURE_SSLRSA = "MD5andSHA1withRSA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private JsseJce() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // no instantiation of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
34826
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   174
    static boolean isEcAvailable() {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   175
        return EcAvailability.isAvailable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Return an JCE cipher implementation for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    static Cipher getCipher(String transformation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                return Cipher.getInstance(transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                return Cipher.getInstance(transformation, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } catch (NoSuchPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw new NoSuchAlgorithmException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Return an JCA signature implementation for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * The algorithm string should be one of the constants defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    static Signature getSignature(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return Signature.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            // reference equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (algorithm == SIGNATURE_SSLRSA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                // The SunPKCS11 provider currently does not support this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                // special algorithm. We allow a fallback in this case because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                // the SunJSSE implementation does the actual crypto using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                // a NONEwithRSA signature obtained from the cryptoProvider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                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
   211
                    // Calling Signature.getInstance() and catching the
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 5506
diff changeset
   212
                    // 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
   213
                    // expensive. So we check directly via getService().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        return Signature.getInstance(algorithm, "SunJSSE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    } catch (NoSuchProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        throw new NoSuchAlgorithmException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return Signature.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    static KeyGenerator getKeyGenerator(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return KeyGenerator.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return KeyGenerator.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    static KeyPairGenerator getKeyPairGenerator(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return KeyPairGenerator.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return KeyPairGenerator.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    static KeyAgreement getKeyAgreement(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return KeyAgreement.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return KeyAgreement.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    static Mac getMac(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return Mac.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return Mac.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    static KeyFactory getKeyFactory(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return KeyFactory.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return KeyFactory.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
39563
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   270
    static AlgorithmParameters getAlgorithmParameters(String algorithm)
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   271
            throws NoSuchAlgorithmException {
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   272
        if (cryptoProvider == null) {
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   273
            return AlgorithmParameters.getInstance(algorithm);
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   274
        } else {
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   275
            return AlgorithmParameters.getInstance(algorithm, cryptoProvider);
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   276
        }
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   277
    }
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   278
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    static SecureRandom getSecureRandom() throws KeyManagementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // Try "PKCS11" first. If that is not supported, iterate through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // the provider and return the first working implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return SecureRandom.getInstance("PKCS11", cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        for (Provider.Service s : cryptoProvider.getServices()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (s.getType().equals("SecureRandom")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                try {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   293
                    return SecureRandom.getInstance(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   294
                            s.getAlgorithm(), cryptoProvider);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                } catch (NoSuchAlgorithmException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        throw new KeyManagementException("FIPS mode: no SecureRandom "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            + " implementation found in provider " + cryptoProvider.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    static MessageDigest getMD5() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return getMessageDigest("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    static MessageDigest getSHA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return getMessageDigest("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    static MessageDigest getMessageDigest(String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                return MessageDigest.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                return MessageDigest.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        ("Algorithm " + algorithm + " not available", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    static int getRSAKeyLength(PublicKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        BigInteger modulus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            modulus = ((RSAPublicKey)key).getModulus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            RSAPublicKeySpec spec = getRSAPublicKeySpec(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            modulus = spec.getModulus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return modulus.bitLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    static RSAPublicKeySpec getRSAPublicKeySpec(PublicKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            RSAPublicKey rsaKey = (RSAPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return new RSAPublicKeySpec(rsaKey.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                        rsaKey.getPublicExponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            KeyFactory factory = JsseJce.getKeyFactory("RSA");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   344
            return factory.getKeySpec(key, RSAPublicKeySpec.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9691
diff changeset
   346
            throw new RuntimeException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    static ECParameterSpec getECParameterSpec(String namedCurveOid) {
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
   351
        return ECUtil.getECParameterSpec(cryptoProvider, namedCurveOid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    static String getNamedCurveOid(ECParameterSpec params) {
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
   355
        return ECUtil.getCurveName(cryptoProvider, params);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    static ECPoint decodePoint(byte[] encoded, EllipticCurve curve)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            throws java.io.IOException {
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
   360
        return ECUtil.decodePoint(encoded, curve);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    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
   364
        return ECUtil.encodePoint(point, curve);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    // In FIPS mode, set thread local providers; otherwise a no-op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    // Must be paired with endFipsProvider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    static Object beginFipsProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (fipsProviderList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            return Providers.beginThreadProviderList(fipsProviderList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    static void endFipsProvider(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (fipsProviderList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            Providers.endThreadProviderList((ProviderList)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
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
    // lazy initialization holder class idiom for static default parameters
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   385
    //
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   386
    // See Effective Java Second Edition: Item 71.
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   387
    private static class EcAvailability {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   388
        // Is EC crypto available?
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   389
        private static final boolean isAvailable;
34826
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   390
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   391
        static {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   392
            boolean mediator = true;
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   393
            try {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   394
                JsseJce.getSignature(SIGNATURE_ECDSA);
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   395
                JsseJce.getSignature(SIGNATURE_RAWECDSA);
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   396
                JsseJce.getKeyAgreement("ECDH");
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   397
                JsseJce.getKeyFactory("EC");
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   398
                JsseJce.getKeyPairGenerator("EC");
39563
1449ed425710 8148516: Improve the default strength of EC in JDK
xuelei
parents: 34826
diff changeset
   399
                JsseJce.getAlgorithmParameters("EC");
34826
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   400
            } catch (Exception e) {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   401
                mediator = false;
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   402
            }
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   403
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   404
            isAvailable = mediator;
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   405
        }
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   406
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
}