Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/ConstructKeys.java
67773 views
/*1* Copyright (c) 1999, 2021, 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 com.sun.crypto.provider;2627import jdk.internal.access.SharedSecrets;2829import java.security.Key;30import java.security.PublicKey;31import java.security.PrivateKey;32import java.security.KeyFactory;33import java.security.InvalidKeyException;34import java.security.NoSuchAlgorithmException;35import java.security.spec.PKCS8EncodedKeySpec;36import java.security.spec.X509EncodedKeySpec;37import java.security.spec.InvalidKeySpecException;38import java.util.Arrays;39import javax.crypto.SecretKey;40import javax.crypto.Cipher;41import javax.crypto.spec.SecretKeySpec;4243/**44* This class is a helper class which construct key objects45* from encoded keys.46*47* @author Sharon Liu48*49*/5051final class ConstructKeys {5253private static final PublicKey constructPublicKey(byte[] encodedKey,54int ofs, int len, String encodedKeyAlgorithm)55throws InvalidKeyException, NoSuchAlgorithmException {56PublicKey key = null;57byte[] keyBytes = (ofs == 0 && encodedKey.length == len)58? encodedKey : Arrays.copyOfRange(encodedKey, ofs, ofs + len);59X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);60try {61KeyFactory keyFactory =62KeyFactory.getInstance(encodedKeyAlgorithm,63SunJCE.getInstance());64key = keyFactory.generatePublic(keySpec);65} catch (NoSuchAlgorithmException nsae) {66// Try to see whether there is another67// provider which supports this algorithm68try {69KeyFactory keyFactory =70KeyFactory.getInstance(encodedKeyAlgorithm);71key = keyFactory.generatePublic(keySpec);72} catch (NoSuchAlgorithmException nsae2) {73throw new NoSuchAlgorithmException("No installed providers " +74"can create keys for the " +75encodedKeyAlgorithm +76"algorithm");77} catch (InvalidKeySpecException ikse2) {78InvalidKeyException ike =79new InvalidKeyException("Cannot construct public key");80ike.initCause(ikse2);81throw ike;82}83} catch (InvalidKeySpecException ikse) {84InvalidKeyException ike =85new InvalidKeyException("Cannot construct public key");86ike.initCause(ikse);87throw ike;88}8990return key;91}9293private static final PrivateKey constructPrivateKey(byte[] encodedKey,94int ofs, int len, String encodedKeyAlgorithm)95throws InvalidKeyException, NoSuchAlgorithmException {96PrivateKey key = null;97byte[] keyBytes = (ofs == 0 && encodedKey.length == len)98? encodedKey : Arrays.copyOfRange(encodedKey, ofs, ofs + len);99PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);100try {101KeyFactory keyFactory =102KeyFactory.getInstance(encodedKeyAlgorithm,103SunJCE.getInstance());104return keyFactory.generatePrivate(keySpec);105} catch (NoSuchAlgorithmException nsae) {106// Try to see whether there is another107// provider which supports this algorithm108try {109KeyFactory keyFactory =110KeyFactory.getInstance(encodedKeyAlgorithm);111key = keyFactory.generatePrivate(keySpec);112} catch (NoSuchAlgorithmException nsae2) {113throw new NoSuchAlgorithmException("No installed providers " +114"can create keys for the " +115encodedKeyAlgorithm +116"algorithm");117} catch (InvalidKeySpecException ikse2) {118InvalidKeyException ike =119new InvalidKeyException("Cannot construct private key");120ike.initCause(ikse2);121throw ike;122}123} catch (InvalidKeySpecException ikse) {124InvalidKeyException ike =125new InvalidKeyException("Cannot construct private key");126ike.initCause(ikse);127throw ike;128} finally {129SharedSecrets.getJavaSecuritySpecAccess().clearEncodedKeySpec(keySpec);130if (keyBytes != encodedKey) {131Arrays.fill(keyBytes, (byte)0);132}133}134135return key;136}137138private static final SecretKey constructSecretKey(byte[] encodedKey,139int ofs, int len, String encodedKeyAlgorithm) {140return (new SecretKeySpec(encodedKey, ofs, len, encodedKeyAlgorithm));141}142143static final Key constructKey(byte[] encoding, String keyAlgorithm,144int keyType) throws InvalidKeyException, NoSuchAlgorithmException {145return constructKey(encoding, 0, encoding.length, keyAlgorithm,146keyType);147}148149static final Key constructKey(byte[] encoding, int ofs, int len,150String keyAlgorithm, int keyType)151throws InvalidKeyException, NoSuchAlgorithmException {152return switch (keyType) {153case Cipher.SECRET_KEY -> ConstructKeys.constructSecretKey(154encoding, ofs, len, keyAlgorithm);155case Cipher.PRIVATE_KEY -> ConstructKeys.constructPrivateKey(156encoding, ofs, len, keyAlgorithm);157case Cipher.PUBLIC_KEY -> ConstructKeys.constructPublicKey(158encoding, ofs, len, keyAlgorithm);159default -> throw new NoSuchAlgorithmException("Unsupported key type");160};161}162}163164165