jdk/src/share/classes/sun/security/ssl/JsseJce.java
author vinnie
Tue, 14 May 2013 18:08:18 +0100
changeset 17491 7a33824ec8c5
parent 16913 a6f4d1626ad9
child 20485 8af87f3d549c
permissions -rw-r--r--
7194075: Various classes of sunec.jar are duplicated in rt.jar Reviewed-by: mullan, vinnie Contributed-by: Stephen Flores <stephen.flores@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
     2
 * Copyright (c) 2001, 2013, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final static ProviderList fipsProviderList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // Flag indicating whether EC crypto is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // If null, then we have not checked yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // If yes, then all the EC based crypto we need is available.
9691
98b26ecef4c0 7043514: NPE in sun.security.ssl.JsseJce.isEcAvailable
xuelei
parents: 9246
diff changeset
    61
    private static Boolean ecAvailable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
3957
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    63
    // Flag indicating whether Kerberos crypto is available.
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    64
    // If true, then all the Kerberos-based crypto we need is available.
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    65
    private final static boolean kerberosAvailable;
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    66
    static {
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    67
        boolean temp;
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    68
        try {
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    69
            AccessController.doPrivileged(
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    70
                new PrivilegedExceptionAction<Void>() {
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10336
diff changeset
    71
                    @Override
3957
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    72
                    public Void run() throws Exception {
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    73
                        // Test for Kerberos using the bootstrap class loader
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    74
                        Class.forName("sun.security.krb5.PrincipalName", true,
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    75
                                null);
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    76
                        return null;
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    77
                    }
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    78
                });
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    79
            temp = true;
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    80
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    81
        } catch (Exception e) {
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    82
            temp = false;
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    83
        }
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    84
        kerberosAvailable = temp;
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    85
    }
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // force FIPS flag initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // Because isFIPS() is synchronized and cryptoProvider is not modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // after it completes, this also eliminates the need for any further
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // synchronization when accessing cryptoProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (SunJSSE.isFIPS() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            fipsProviderList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // Setup a ProviderList that can be used by the trust manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // during certificate chain validation. All the crypto must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            // from the FIPS provider, but we also allow the required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            // certificate related services from the SUN provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            Provider sun = Security.getProvider("SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (sun == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                throw new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    ("FIPS mode: SUN provider must be installed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            Provider sunCerts = new SunCertificates(sun);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            fipsProviderList = ProviderList.newList(cryptoProvider, sunCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    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
   110
        private static final long serialVersionUID = -3284138292032213752L;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9691
diff changeset
   111
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        SunCertificates(final Provider p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            super("SunCertificates", 1.0d, "SunJSSE internal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            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
   115
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    // copy certificate related services from the Sun provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    for (Map.Entry<Object,Object> entry : p.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        String key = (String)entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        if (key.startsWith("CertPathValidator.")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                || key.startsWith("CertPathBuilder.")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                || key.startsWith("CertStore.")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                || key.startsWith("CertificateFactory.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                            put(key, entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * JCE transformation string for RSA with PKCS#1 v1.5 padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Can be used for encryption, decryption, signing, verifying.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    final static String CIPHER_RSA_PKCS1 = "RSA/ECB/PKCS1Padding";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * JCE transformation string for the stream cipher RC4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    final static String CIPHER_RC4 = "RC4";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * JCE transformation string for DES in CBC mode without padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    final static String CIPHER_DES = "DES/CBC/NoPadding";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * JCE transformation string for (3-key) Triple DES in CBC mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * without padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    final static String CIPHER_3DES = "DESede/CBC/NoPadding";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * JCE transformation string for AES in CBC mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * without padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    final static String CIPHER_AES = "AES/CBC/NoPadding";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   157
     * JCE transformation string for AES in GCM mode
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   158
     * without padding.
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   159
     */
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   160
    final static String CIPHER_AES_GCM = "AES/GCM/NoPadding";
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16067
diff changeset
   161
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * JCA identifier string for DSA, i.e. a DSA with SHA-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    final static String SIGNATURE_DSA = "DSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * JCA identifier string for ECDSA, i.e. a ECDSA with SHA-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    final static String SIGNATURE_ECDSA = "SHA1withECDSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * JCA identifier string for Raw DSA, i.e. a DSA signature without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * hashing where the application provides the SHA-1 hash of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Note that the standard name is "NONEwithDSA" but we use "RawDSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * for compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    final static String SIGNATURE_RAWDSA = "RawDSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * JCA identifier string for Raw ECDSA, i.e. a DSA signature without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * hashing where the application provides the SHA-1 hash of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    final static String SIGNATURE_RAWECDSA = "NONEwithECDSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * JCA identifier string for Raw RSA, i.e. a RSA PKCS#1 v1.5 signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * without hashing where the application provides the hash of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Used for RSA client authentication with a 36 byte hash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    final static String SIGNATURE_RAWRSA = "NONEwithRSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * JCA identifier string for the SSL/TLS style RSA Signature. I.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * an signature using RSA with PKCS#1 v1.5 padding signing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * concatenation of an MD5 and SHA-1 digest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    final static String SIGNATURE_SSLRSA = "MD5andSHA1withRSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private JsseJce() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // no instantiation of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
9691
98b26ecef4c0 7043514: NPE in sun.security.ssl.JsseJce.isEcAvailable
xuelei
parents: 9246
diff changeset
   198
    synchronized static boolean isEcAvailable() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (ecAvailable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                JsseJce.getSignature(SIGNATURE_ECDSA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                JsseJce.getSignature(SIGNATURE_RAWECDSA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                JsseJce.getKeyAgreement("ECDH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                JsseJce.getKeyFactory("EC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                JsseJce.getKeyPairGenerator("EC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                ecAvailable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                ecAvailable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return ecAvailable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
9691
98b26ecef4c0 7043514: NPE in sun.security.ssl.JsseJce.isEcAvailable
xuelei
parents: 9246
diff changeset
   214
    synchronized static void clearEcAvailable() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        ecAvailable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
3957
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
   218
    static boolean isKerberosAvailable() {
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
   219
        return kerberosAvailable;
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
   220
    }
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 715
diff changeset
   221
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Return an JCE cipher implementation for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    static Cipher getCipher(String transformation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                return Cipher.getInstance(transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                return Cipher.getInstance(transformation, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } catch (NoSuchPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new NoSuchAlgorithmException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Return an JCA signature implementation for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * The algorithm string should be one of the constants defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    static Signature getSignature(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 Signature.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // reference equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (algorithm == SIGNATURE_SSLRSA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                // The SunPKCS11 provider currently does not support this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                // special algorithm. We allow a fallback in this case because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                // the SunJSSE implementation does the actual crypto using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                // a NONEwithRSA signature obtained from the cryptoProvider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                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
   255
                    // Calling Signature.getInstance() and catching the
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 5506
diff changeset
   256
                    // 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
   257
                    // expensive. So we check directly via getService().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        return Signature.getInstance(algorithm, "SunJSSE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    } catch (NoSuchProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        throw new NoSuchAlgorithmException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return Signature.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 KeyGenerator getKeyGenerator(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 KeyGenerator.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return KeyGenerator.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 KeyPairGenerator getKeyPairGenerator(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return KeyPairGenerator.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return KeyPairGenerator.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    static KeyAgreement getKeyAgreement(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return KeyAgreement.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            return KeyAgreement.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    static Mac getMac(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return Mac.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return Mac.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
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 KeyFactory getKeyFactory(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return KeyFactory.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return KeyFactory.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    static SecureRandom getSecureRandom() throws KeyManagementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // Try "PKCS11" first. If that is not supported, iterate through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        // the provider and return the first working implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return SecureRandom.getInstance("PKCS11", cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        for (Provider.Service s : cryptoProvider.getServices()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (s.getType().equals("SecureRandom")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    return SecureRandom.getInstance(s.getAlgorithm(), cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                } catch (NoSuchAlgorithmException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        throw new KeyManagementException("FIPS mode: no SecureRandom "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            + " implementation found in provider " + cryptoProvider.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    static MessageDigest getMD5() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return getMessageDigest("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    static MessageDigest getSHA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return getMessageDigest("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    static MessageDigest getMessageDigest(String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (cryptoProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                return MessageDigest.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                return MessageDigest.getInstance(algorithm, cryptoProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            throw new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        ("Algorithm " + algorithm + " not available", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    static int getRSAKeyLength(PublicKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        BigInteger modulus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            modulus = ((RSAPublicKey)key).getModulus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            RSAPublicKeySpec spec = getRSAPublicKeySpec(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            modulus = spec.getModulus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return modulus.bitLength();
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 RSAPublicKeySpec getRSAPublicKeySpec(PublicKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            RSAPublicKey rsaKey = (RSAPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            return new RSAPublicKeySpec(rsaKey.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                        rsaKey.getPublicExponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            KeyFactory factory = JsseJce.getKeyFactory("RSA");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   378
            return factory.getKeySpec(key, RSAPublicKeySpec.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9691
diff changeset
   380
            throw new RuntimeException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    static ECParameterSpec getECParameterSpec(String namedCurveOid) {
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
   385
        return ECUtil.getECParameterSpec(cryptoProvider, namedCurveOid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    static String getNamedCurveOid(ECParameterSpec params) {
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
   389
        return ECUtil.getCurveName(cryptoProvider, params);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    static ECPoint decodePoint(byte[] encoded, EllipticCurve curve)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            throws java.io.IOException {
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 16913
diff changeset
   394
        return ECUtil.decodePoint(encoded, curve);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    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
   398
        return ECUtil.encodePoint(point, curve);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    // In FIPS mode, set thread local providers; otherwise a no-op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    // Must be paired with endFipsProvider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    static Object beginFipsProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if (fipsProviderList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            return Providers.beginThreadProviderList(fipsProviderList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    static void endFipsProvider(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (fipsProviderList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            Providers.endThreadProviderList((ProviderList)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
}