Path: blob/master/src/java.base/share/classes/sun/security/ssl/JsseJce.java
41159 views
/*1* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.ssl;2627import java.math.BigInteger;28import java.security.*;29import java.security.interfaces.RSAPublicKey;30import java.security.spec.*;31import javax.crypto.*;3233/**34* This class contains a few static methods for interaction with the JCA/JCE35* to obtain implementations, etc.36*37* @author Andreas Sterbenz38*/39final class JsseJce {40static final boolean ALLOW_ECC =41Utilities.getBooleanProperty("com.sun.net.ssl.enableECC", true);4243/**44* JCE transformation string for RSA with PKCS#1 v1.5 padding.45* Can be used for encryption, decryption, signing, verifying.46*/47static final String CIPHER_RSA_PKCS1 = "RSA/ECB/PKCS1Padding";4849/**50* JCE transformation string for the stream cipher RC4.51*/52static final String CIPHER_RC4 = "RC4";5354/**55* JCE transformation string for DES in CBC mode without padding.56*/57static final String CIPHER_DES = "DES/CBC/NoPadding";5859/**60* JCE transformation string for (3-key) Triple DES in CBC mode61* without padding.62*/63static final String CIPHER_3DES = "DESede/CBC/NoPadding";6465/**66* JCE transformation string for AES in CBC mode67* without padding.68*/69static final String CIPHER_AES = "AES/CBC/NoPadding";7071/**72* JCE transformation string for AES in GCM mode73* without padding.74*/75static final String CIPHER_AES_GCM = "AES/GCM/NoPadding";7677/**78* JCE transformation string for ChaCha20-Poly130579*/80static final String CIPHER_CHACHA20_POLY1305 = "ChaCha20-Poly1305";8182/**83* JCA identifier string for DSA, i.e. a DSA with SHA-1.84*/85static final String SIGNATURE_DSA = "DSA";8687/**88* JCA identifier string for ECDSA, i.e. a ECDSA with SHA-1.89*/90static final String SIGNATURE_ECDSA = "SHA1withECDSA";9192/**93* JCA identifier for EdDSA signatures.94*/95static final String SIGNATURE_EDDSA = "EdDSA";9697/**98* JCA identifier string for Raw DSA, i.e. a DSA signature without99* hashing where the application provides the SHA-1 hash of the data.100* Note that the standard name is "NONEwithDSA" but we use "RawDSA"101* for compatibility.102*/103static final String SIGNATURE_RAWDSA = "RawDSA";104105/**106* JCA identifier string for Raw ECDSA, i.e. a DSA signature without107* hashing where the application provides the SHA-1 hash of the data.108*/109static final String SIGNATURE_RAWECDSA = "NONEwithECDSA";110111/**112* JCA identifier string for Raw RSA, i.e. a RSA PKCS#1 v1.5 signature113* without hashing where the application provides the hash of the data.114* Used for RSA client authentication with a 36 byte hash.115*/116static final String SIGNATURE_RAWRSA = "NONEwithRSA";117118/**119* JCA identifier string for the SSL/TLS style RSA Signature. I.e.120* an signature using RSA with PKCS#1 v1.5 padding signing a121* concatenation of an MD5 and SHA-1 digest.122*/123static final String SIGNATURE_SSLRSA = "MD5andSHA1withRSA";124125private JsseJce() {126// no instantiation of this class127}128129static boolean isEcAvailable() {130return EcAvailability.isAvailable;131}132133static int getRSAKeyLength(PublicKey key) {134BigInteger modulus;135if (key instanceof RSAPublicKey) {136modulus = ((RSAPublicKey)key).getModulus();137} else {138RSAPublicKeySpec spec = getRSAPublicKeySpec(key);139modulus = spec.getModulus();140}141return modulus.bitLength();142}143144static RSAPublicKeySpec getRSAPublicKeySpec(PublicKey key) {145if (key instanceof RSAPublicKey) {146RSAPublicKey rsaKey = (RSAPublicKey)key;147return new RSAPublicKeySpec(rsaKey.getModulus(),148rsaKey.getPublicExponent());149}150try {151KeyFactory factory = KeyFactory.getInstance("RSA");152return factory.getKeySpec(key, RSAPublicKeySpec.class);153} catch (Exception e) {154throw new RuntimeException(e);155}156}157158// lazy initialization holder class idiom for static default parameters159//160// See Effective Java Second Edition: Item 71.161private static class EcAvailability {162// Is EC crypto available?163private static final boolean isAvailable;164165static {166boolean mediator = true;167try {168Signature.getInstance(SIGNATURE_ECDSA);169Signature.getInstance(SIGNATURE_RAWECDSA);170KeyAgreement.getInstance("ECDH");171KeyFactory.getInstance("EC");172KeyPairGenerator.getInstance("EC");173AlgorithmParameters.getInstance("EC");174} catch (Exception e) {175mediator = false;176}177178isAvailable = mediator;179}180}181}182183184