0N/A/*
3988N/A * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.ssl;
0N/A
0N/Aimport java.util.*;
0N/Aimport java.math.BigInteger;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.interfaces.RSAPublicKey;
0N/Aimport java.security.spec.RSAPublicKeySpec;
0N/Aimport java.security.spec.*;
0N/A
0N/Aimport javax.crypto.*;
0N/A
0N/A// explicit import to override the Provider class in this package
0N/Aimport java.security.Provider;
0N/A
0N/A// need internal Sun classes for FIPS tricks
0N/Aimport sun.security.jca.Providers;
0N/Aimport sun.security.jca.ProviderList;
0N/A
0N/Aimport sun.security.ec.ECParameters;
0N/Aimport sun.security.ec.NamedCurve;
0N/A
0N/Aimport static sun.security.ssl.SunJSSE.cryptoProvider;
0N/A
0N/A/**
0N/A * This class contains a few static methods for interaction with the JCA/JCE
0N/A * to obtain implementations, etc.
0N/A *
0N/A * @author Andreas Sterbenz
0N/A */
0N/Afinal class JsseJce {
0N/A
0N/A private final static Debug debug = Debug.getInstance("ssl");
0N/A
0N/A private final static ProviderList fipsProviderList;
0N/A
0N/A // Flag indicating whether EC crypto is available.
0N/A // If null, then we have not checked yet.
0N/A // If yes, then all the EC based crypto we need is available.
4228N/A private static Boolean ecAvailable;
0N/A
1715N/A // Flag indicating whether Kerberos crypto is available.
1715N/A // If true, then all the Kerberos-based crypto we need is available.
1715N/A private final static boolean kerberosAvailable;
1715N/A static {
1715N/A boolean temp;
1715N/A try {
1715N/A AccessController.doPrivileged(
1715N/A new PrivilegedExceptionAction<Void>() {
1715N/A public Void run() throws Exception {
1715N/A // Test for Kerberos using the bootstrap class loader
1715N/A Class.forName("sun.security.krb5.PrincipalName", true,
1715N/A null);
1715N/A return null;
1715N/A }
1715N/A });
1715N/A temp = true;
1715N/A
1715N/A } catch (Exception e) {
1715N/A temp = false;
1715N/A }
1715N/A kerberosAvailable = temp;
1715N/A }
1715N/A
0N/A static {
0N/A // force FIPS flag initialization
0N/A // Because isFIPS() is synchronized and cryptoProvider is not modified
0N/A // after it completes, this also eliminates the need for any further
0N/A // synchronization when accessing cryptoProvider
0N/A if (SunJSSE.isFIPS() == false) {
0N/A fipsProviderList = null;
0N/A } else {
0N/A // Setup a ProviderList that can be used by the trust manager
0N/A // during certificate chain validation. All the crypto must be
0N/A // from the FIPS provider, but we also allow the required
0N/A // certificate related services from the SUN provider.
0N/A Provider sun = Security.getProvider("SUN");
0N/A if (sun == null) {
0N/A throw new RuntimeException
0N/A ("FIPS mode: SUN provider must be installed");
0N/A }
0N/A Provider sunCerts = new SunCertificates(sun);
0N/A fipsProviderList = ProviderList.newList(cryptoProvider, sunCerts);
0N/A }
0N/A }
0N/A
0N/A private static final class SunCertificates extends Provider {
0N/A SunCertificates(final Provider p) {
0N/A super("SunCertificates", 1.0d, "SunJSSE internal");
0N/A AccessController.doPrivileged(new PrivilegedAction<Object>() {
0N/A public Object run() {
0N/A // copy certificate related services from the Sun provider
0N/A for (Map.Entry<Object,Object> entry : p.entrySet()) {
0N/A String key = (String)entry.getKey();
0N/A if (key.startsWith("CertPathValidator.")
0N/A || key.startsWith("CertPathBuilder.")
0N/A || key.startsWith("CertStore.")
0N/A || key.startsWith("CertificateFactory.")) {
0N/A put(key, entry.getValue());
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A });
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * JCE transformation string for RSA with PKCS#1 v1.5 padding.
0N/A * Can be used for encryption, decryption, signing, verifying.
0N/A */
0N/A final static String CIPHER_RSA_PKCS1 = "RSA/ECB/PKCS1Padding";
0N/A /**
0N/A * JCE transformation string for the stream cipher RC4.
0N/A */
0N/A final static String CIPHER_RC4 = "RC4";
0N/A /**
0N/A * JCE transformation string for DES in CBC mode without padding.
0N/A */
0N/A final static String CIPHER_DES = "DES/CBC/NoPadding";
0N/A /**
0N/A * JCE transformation string for (3-key) Triple DES in CBC mode
0N/A * without padding.
0N/A */
0N/A final static String CIPHER_3DES = "DESede/CBC/NoPadding";
0N/A /**
0N/A * JCE transformation string for AES in CBC mode
0N/A * without padding.
0N/A */
0N/A final static String CIPHER_AES = "AES/CBC/NoPadding";
0N/A /**
0N/A * JCA identifier string for DSA, i.e. a DSA with SHA-1.
0N/A */
0N/A final static String SIGNATURE_DSA = "DSA";
0N/A /**
0N/A * JCA identifier string for ECDSA, i.e. a ECDSA with SHA-1.
0N/A */
0N/A final static String SIGNATURE_ECDSA = "SHA1withECDSA";
0N/A /**
0N/A * JCA identifier string for Raw DSA, i.e. a DSA signature without
0N/A * hashing where the application provides the SHA-1 hash of the data.
0N/A * Note that the standard name is "NONEwithDSA" but we use "RawDSA"
0N/A * for compatibility.
0N/A */
0N/A final static String SIGNATURE_RAWDSA = "RawDSA";
0N/A /**
0N/A * JCA identifier string for Raw ECDSA, i.e. a DSA signature without
0N/A * hashing where the application provides the SHA-1 hash of the data.
0N/A */
0N/A final static String SIGNATURE_RAWECDSA = "NONEwithECDSA";
0N/A /**
0N/A * JCA identifier string for Raw RSA, i.e. a RSA PKCS#1 v1.5 signature
0N/A * without hashing where the application provides the hash of the data.
0N/A * Used for RSA client authentication with a 36 byte hash.
0N/A */
0N/A final static String SIGNATURE_RAWRSA = "NONEwithRSA";
0N/A /**
0N/A * JCA identifier string for the SSL/TLS style RSA Signature. I.e.
0N/A * an signature using RSA with PKCS#1 v1.5 padding signing a
0N/A * concatenation of an MD5 and SHA-1 digest.
0N/A */
0N/A final static String SIGNATURE_SSLRSA = "MD5andSHA1withRSA";
0N/A
0N/A private JsseJce() {
0N/A // no instantiation of this class
0N/A }
0N/A
4228N/A synchronized static boolean isEcAvailable() {
0N/A if (ecAvailable == null) {
0N/A try {
0N/A JsseJce.getSignature(SIGNATURE_ECDSA);
0N/A JsseJce.getSignature(SIGNATURE_RAWECDSA);
0N/A JsseJce.getKeyAgreement("ECDH");
0N/A JsseJce.getKeyFactory("EC");
0N/A JsseJce.getKeyPairGenerator("EC");
0N/A ecAvailable = true;
0N/A } catch (Exception e) {
0N/A ecAvailable = false;
0N/A }
0N/A }
0N/A return ecAvailable;
0N/A }
0N/A
4228N/A synchronized static void clearEcAvailable() {
0N/A ecAvailable = null;
0N/A }
0N/A
1715N/A static boolean isKerberosAvailable() {
1715N/A return kerberosAvailable;
1715N/A }
1715N/A
0N/A /**
0N/A * Return an JCE cipher implementation for the specified algorithm.
0N/A */
0N/A static Cipher getCipher(String transformation)
0N/A throws NoSuchAlgorithmException {
0N/A try {
0N/A if (cryptoProvider == null) {
0N/A return Cipher.getInstance(transformation);
0N/A } else {
0N/A return Cipher.getInstance(transformation, cryptoProvider);
0N/A }
0N/A } catch (NoSuchPaddingException e) {
0N/A throw new NoSuchAlgorithmException(e);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return an JCA signature implementation for the specified algorithm.
0N/A * The algorithm string should be one of the constants defined
0N/A * in this class.
0N/A */
0N/A static Signature getSignature(String algorithm)
0N/A throws NoSuchAlgorithmException {
0N/A if (cryptoProvider == null) {
0N/A return Signature.getInstance(algorithm);
0N/A } else {
0N/A // reference equality
0N/A if (algorithm == SIGNATURE_SSLRSA) {
0N/A // The SunPKCS11 provider currently does not support this
0N/A // special algorithm. We allow a fallback in this case because
0N/A // the SunJSSE implementation does the actual crypto using
0N/A // a NONEwithRSA signature obtained from the cryptoProvider.
0N/A if (cryptoProvider.getService("Signature", algorithm) == null) {
3988N/A // Calling Signature.getInstance() and catching the
3988N/A // exception would be cleaner, but exceptions are a little
3988N/A // expensive. So we check directly via getService().
0N/A try {
0N/A return Signature.getInstance(algorithm, "SunJSSE");
0N/A } catch (NoSuchProviderException e) {
0N/A throw new NoSuchAlgorithmException(e);
0N/A }
0N/A }
0N/A }
0N/A return Signature.getInstance(algorithm, cryptoProvider);
0N/A }
0N/A }
0N/A
0N/A static KeyGenerator getKeyGenerator(String algorithm)
0N/A throws NoSuchAlgorithmException {
0N/A if (cryptoProvider == null) {
0N/A return KeyGenerator.getInstance(algorithm);
0N/A } else {
0N/A return KeyGenerator.getInstance(algorithm, cryptoProvider);
0N/A }
0N/A }
0N/A
0N/A static KeyPairGenerator getKeyPairGenerator(String algorithm)
0N/A throws NoSuchAlgorithmException {
0N/A if (cryptoProvider == null) {
0N/A return KeyPairGenerator.getInstance(algorithm);
0N/A } else {
0N/A return KeyPairGenerator.getInstance(algorithm, cryptoProvider);
0N/A }
0N/A }
0N/A
0N/A static KeyAgreement getKeyAgreement(String algorithm)
0N/A throws NoSuchAlgorithmException {
0N/A if (cryptoProvider == null) {
0N/A return KeyAgreement.getInstance(algorithm);
0N/A } else {
0N/A return KeyAgreement.getInstance(algorithm, cryptoProvider);
0N/A }
0N/A }
0N/A
0N/A static Mac getMac(String algorithm)
0N/A throws NoSuchAlgorithmException {
0N/A if (cryptoProvider == null) {
0N/A return Mac.getInstance(algorithm);
0N/A } else {
0N/A return Mac.getInstance(algorithm, cryptoProvider);
0N/A }
0N/A }
0N/A
0N/A static KeyFactory getKeyFactory(String algorithm)
0N/A throws NoSuchAlgorithmException {
0N/A if (cryptoProvider == null) {
0N/A return KeyFactory.getInstance(algorithm);
0N/A } else {
0N/A return KeyFactory.getInstance(algorithm, cryptoProvider);
0N/A }
0N/A }
0N/A
0N/A static SecureRandom getSecureRandom() throws KeyManagementException {
0N/A if (cryptoProvider == null) {
0N/A return new SecureRandom();
0N/A }
0N/A // Try "PKCS11" first. If that is not supported, iterate through
0N/A // the provider and return the first working implementation.
0N/A try {
0N/A return SecureRandom.getInstance("PKCS11", cryptoProvider);
0N/A } catch (NoSuchAlgorithmException e) {
0N/A // ignore
0N/A }
0N/A for (Provider.Service s : cryptoProvider.getServices()) {
0N/A if (s.getType().equals("SecureRandom")) {
0N/A try {
0N/A return SecureRandom.getInstance(s.getAlgorithm(), cryptoProvider);
0N/A } catch (NoSuchAlgorithmException ee) {
0N/A // ignore
0N/A }
0N/A }
0N/A }
0N/A throw new KeyManagementException("FIPS mode: no SecureRandom "
0N/A + " implementation found in provider " + cryptoProvider.getName());
0N/A }
0N/A
0N/A static MessageDigest getMD5() {
0N/A return getMessageDigest("MD5");
0N/A }
0N/A
0N/A static MessageDigest getSHA() {
0N/A return getMessageDigest("SHA");
0N/A }
0N/A
0N/A static MessageDigest getMessageDigest(String algorithm) {
0N/A try {
0N/A if (cryptoProvider == null) {
0N/A return MessageDigest.getInstance(algorithm);
0N/A } else {
0N/A return MessageDigest.getInstance(algorithm, cryptoProvider);
0N/A }
0N/A } catch (NoSuchAlgorithmException e) {
0N/A throw new RuntimeException
0N/A ("Algorithm " + algorithm + " not available", e);
0N/A }
0N/A }
0N/A
0N/A static int getRSAKeyLength(PublicKey key) {
0N/A BigInteger modulus;
0N/A if (key instanceof RSAPublicKey) {
0N/A modulus = ((RSAPublicKey)key).getModulus();
0N/A } else {
0N/A RSAPublicKeySpec spec = getRSAPublicKeySpec(key);
0N/A modulus = spec.getModulus();
0N/A }
0N/A return modulus.bitLength();
0N/A }
0N/A
0N/A static RSAPublicKeySpec getRSAPublicKeySpec(PublicKey key) {
0N/A if (key instanceof RSAPublicKey) {
0N/A RSAPublicKey rsaKey = (RSAPublicKey)key;
0N/A return new RSAPublicKeySpec(rsaKey.getModulus(),
0N/A rsaKey.getPublicExponent());
0N/A }
0N/A try {
0N/A KeyFactory factory = JsseJce.getKeyFactory("RSA");
28N/A return factory.getKeySpec(key, RSAPublicKeySpec.class);
0N/A } catch (Exception e) {
0N/A throw (RuntimeException)new RuntimeException().initCause(e);
0N/A }
0N/A }
0N/A
0N/A static ECParameterSpec getECParameterSpec(String namedCurveOid) {
0N/A return NamedCurve.getECParameterSpec(namedCurveOid);
0N/A }
0N/A
0N/A static String getNamedCurveOid(ECParameterSpec params) {
0N/A return ECParameters.getCurveName(params);
0N/A }
0N/A
0N/A static ECPoint decodePoint(byte[] encoded, EllipticCurve curve)
0N/A throws java.io.IOException {
0N/A return ECParameters.decodePoint(encoded, curve);
0N/A }
0N/A
0N/A static byte[] encodePoint(ECPoint point, EllipticCurve curve) {
0N/A return ECParameters.encodePoint(point, curve);
0N/A }
0N/A
0N/A // In FIPS mode, set thread local providers; otherwise a no-op.
0N/A // Must be paired with endFipsProvider.
0N/A static Object beginFipsProvider() {
0N/A if (fipsProviderList == null) {
0N/A return null;
0N/A } else {
0N/A return Providers.beginThreadProviderList(fipsProviderList);
0N/A }
0N/A }
0N/A
0N/A static void endFipsProvider(Object o) {
0N/A if (fipsProviderList != null) {
0N/A Providers.endThreadProviderList((ProviderList)o);
0N/A }
0N/A }
0N/A
0N/A}