jdk/src/java.base/share/classes/sun/security/ssl/JsseJce.java
author xuelei
Wed, 23 Dec 2015 03:51:35 +0000
changeset 34826 4bbdce2630f8
parent 34006 d5514a103cce
child 39563 1449ed425710
permissions -rw-r--r--
8133070: Hot lock on BulkCipher.isAvailable Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
33991
619bfc4d582d 8130696: Security Providers need to have their version numbers updated for JDK 9
iris
parents: 30905
diff changeset
     2
 * Copyright (c) 2001, 2015, 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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.interfaces.RSAPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
// explicit import to override the Provider class in this package
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
// need internal Sun classes for FIPS tricks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.jca.Providers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.jca.ProviderList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
    44
import sun.security.util.ECUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import static sun.security.ssl.SunJSSE.cryptoProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * This class contains a few static methods for interaction with the JCA/JCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * to obtain implementations, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
final class JsseJce {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
    56
    private static final ProviderList fipsProviderList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
3957
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    58
    // Flag indicating whether Kerberos crypto is available.
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    59
    // If true, then all the Kerberos-based crypto we need is available.
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
    60
    private static final boolean kerberosAvailable;
3957
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    61
    static {
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 25859
diff changeset
    62
        ClientKeyExchangeService p =
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 25859
diff changeset
    63
                ClientKeyExchangeService.find("KRB5");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 25859
diff changeset
    64
        kerberosAvailable = (p != null);
3957
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    65
    }
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    66
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // force FIPS flag initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // Because isFIPS() is synchronized and cryptoProvider is not modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // after it completes, this also eliminates the need for any further
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // synchronization when accessing cryptoProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (SunJSSE.isFIPS() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            fipsProviderList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            // Setup a ProviderList that can be used by the trust manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            // during certificate chain validation. All the crypto must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            // from the FIPS provider, but we also allow the required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            // certificate related services from the SUN provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            Provider sun = Security.getProvider("SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (sun == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                throw new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    ("FIPS mode: SUN provider must be installed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            Provider sunCerts = new SunCertificates(sun);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            fipsProviderList = ProviderList.newList(cryptoProvider, sunCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    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
    90
        private static final long serialVersionUID = -3284138292032213752L;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9691
diff changeset
    91
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        SunCertificates(final Provider p) {
33991
619bfc4d582d 8130696: Security Providers need to have their version numbers updated for JDK 9
iris
parents: 30905
diff changeset
    93
            super("SunCertificates", 9.0d, "SunJSSE internal");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            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
    95
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    // copy certificate related services from the Sun provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    for (Map.Entry<Object,Object> entry : p.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        String key = (String)entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        if (key.startsWith("CertPathValidator.")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                || key.startsWith("CertPathBuilder.")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                || key.startsWith("CertStore.")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                || key.startsWith("CertificateFactory.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                            put(key, entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * JCE transformation string for RSA with PKCS#1 v1.5 padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Can be used for encryption, decryption, signing, verifying.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   117
    static final String CIPHER_RSA_PKCS1 = "RSA/ECB/PKCS1Padding";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * JCE transformation string for the stream cipher RC4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   121
    static final String CIPHER_RC4 = "RC4";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * JCE transformation string for DES in CBC mode without padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   125
    static final String CIPHER_DES = "DES/CBC/NoPadding";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * JCE transformation string for (3-key) Triple DES in CBC mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * without padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   130
    static final String CIPHER_3DES = "DESede/CBC/NoPadding";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * JCE transformation string for AES in CBC mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * without padding.
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 CIPHER_AES = "AES/CBC/NoPadding";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   137
     * JCE transformation string for AES in GCM mode
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   138
     * without padding.
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   139
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   140
    static final String CIPHER_AES_GCM = "AES/GCM/NoPadding";
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   141
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * JCA identifier string for DSA, i.e. a DSA with SHA-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   144
    static final String SIGNATURE_DSA = "DSA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * JCA identifier string for ECDSA, i.e. a ECDSA with SHA-1.
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_ECDSA = "SHA1withECDSA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * JCA identifier string for Raw DSA, i.e. a DSA signature without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * hashing where the application provides the SHA-1 hash of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Note that the standard name is "NONEwithDSA" but we use "RawDSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * for compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   155
    static final String SIGNATURE_RAWDSA = "RawDSA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * JCA identifier string for Raw ECDSA, i.e. a DSA signature without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * hashing where the application provides the SHA-1 hash of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   160
    static final String SIGNATURE_RAWECDSA = "NONEwithECDSA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * JCA identifier string for Raw RSA, i.e. a RSA PKCS#1 v1.5 signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * without hashing where the application provides the hash of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Used for RSA client authentication with a 36 byte hash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   166
    static final String SIGNATURE_RAWRSA = "NONEwithRSA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * JCA identifier string for the SSL/TLS style RSA Signature. I.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * an signature using RSA with PKCS#1 v1.5 padding signing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * concatenation of an MD5 and SHA-1 digest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
   172
    static final String SIGNATURE_SSLRSA = "MD5andSHA1withRSA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private JsseJce() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // no instantiation of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
34826
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   178
    static boolean isEcAvailable() {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   179
        return EcAvailability.isAvailable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
3957
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
   182
    static boolean isKerberosAvailable() {
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
   183
        return kerberosAvailable;
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
   184
    }
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
   185
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Return an JCE cipher implementation for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    static Cipher getCipher(String transformation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                return Cipher.getInstance(transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                return Cipher.getInstance(transformation, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } catch (NoSuchPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw new NoSuchAlgorithmException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Return an JCA signature implementation for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * The algorithm string should be one of the constants defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    static Signature getSignature(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return Signature.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            // reference equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (algorithm == SIGNATURE_SSLRSA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                // The SunPKCS11 provider currently does not support this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                // special algorithm. We allow a fallback in this case because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                // the SunJSSE implementation does the actual crypto using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                // a NONEwithRSA signature obtained from the cryptoProvider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                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
   219
                    // Calling Signature.getInstance() and catching the
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 5506
diff changeset
   220
                    // 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
   221
                    // expensive. So we check directly via getService().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        return Signature.getInstance(algorithm, "SunJSSE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    } catch (NoSuchProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        throw new NoSuchAlgorithmException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return Signature.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    static KeyGenerator getKeyGenerator(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return KeyGenerator.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return KeyGenerator.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    static KeyPairGenerator getKeyPairGenerator(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return KeyPairGenerator.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return KeyPairGenerator.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    static KeyAgreement getKeyAgreement(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            return KeyAgreement.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return KeyAgreement.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    static Mac getMac(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return Mac.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return Mac.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    static KeyFactory getKeyFactory(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return KeyFactory.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return KeyFactory.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    static SecureRandom getSecureRandom() throws KeyManagementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // Try "PKCS11" first. If that is not supported, iterate through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // the provider and return the first working implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return SecureRandom.getInstance("PKCS11", cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        for (Provider.Service s : cryptoProvider.getServices()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (s.getType().equals("SecureRandom")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    return SecureRandom.getInstance(s.getAlgorithm(), cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                } catch (NoSuchAlgorithmException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        throw new KeyManagementException("FIPS mode: no SecureRandom "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            + " implementation found in provider " + cryptoProvider.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    static MessageDigest getMD5() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return getMessageDigest("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    static MessageDigest getSHA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return getMessageDigest("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    static MessageDigest getMessageDigest(String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                return MessageDigest.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                return MessageDigest.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            throw new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        ("Algorithm " + algorithm + " not available", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    static int getRSAKeyLength(PublicKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        BigInteger modulus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            modulus = ((RSAPublicKey)key).getModulus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            RSAPublicKeySpec spec = getRSAPublicKeySpec(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            modulus = spec.getModulus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return modulus.bitLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    static RSAPublicKeySpec getRSAPublicKeySpec(PublicKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            RSAPublicKey rsaKey = (RSAPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            return new RSAPublicKeySpec(rsaKey.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                        rsaKey.getPublicExponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            KeyFactory factory = JsseJce.getKeyFactory("RSA");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   342
            return factory.getKeySpec(key, RSAPublicKeySpec.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9691
diff changeset
   344
            throw new RuntimeException(e);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    static ECParameterSpec getECParameterSpec(String namedCurveOid) {
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
   349
        return ECUtil.getECParameterSpec(cryptoProvider, namedCurveOid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    static String getNamedCurveOid(ECParameterSpec params) {
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
   353
        return ECUtil.getCurveName(cryptoProvider, params);
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 ECPoint decodePoint(byte[] encoded, EllipticCurve curve)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            throws java.io.IOException {
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
   358
        return ECUtil.decodePoint(encoded, curve);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    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
   362
        return ECUtil.encodePoint(point, curve);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    // In FIPS mode, set thread local providers; otherwise a no-op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    // Must be paired with endFipsProvider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    static Object beginFipsProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (fipsProviderList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            return Providers.beginThreadProviderList(fipsProviderList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    static void endFipsProvider(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (fipsProviderList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            Providers.endThreadProviderList((ProviderList)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
34826
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   381
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   382
    // lazy initialization holder class idiom for static default parameters
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
    // See Effective Java Second Edition: Item 71.
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   385
    private static class EcAvailability {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   386
        // Is EC crypto available?
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   387
        private final static boolean isAvailable;
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   388
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   389
        static {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   390
            boolean mediator = true;
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   391
            try {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   392
                JsseJce.getSignature(SIGNATURE_ECDSA);
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   393
                JsseJce.getSignature(SIGNATURE_RAWECDSA);
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   394
                JsseJce.getKeyAgreement("ECDH");
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   395
                JsseJce.getKeyFactory("EC");
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   396
                JsseJce.getKeyPairGenerator("EC");
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   397
            } catch (Exception e) {
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   398
                mediator = false;
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   399
            }
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   400
4bbdce2630f8 8133070: Hot lock on BulkCipher.isAvailable
xuelei
parents: 34006
diff changeset
   401
            isAvailable = mediator;
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
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
}