Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/crypto/provider/ConstructKeys.java
38922 views
/*1* Copyright (c) 1999, 2013, 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 java.security.Key;28import java.security.PublicKey;29import java.security.PrivateKey;30import java.security.KeyFactory;31import java.security.InvalidKeyException;32import java.security.NoSuchAlgorithmException;33import java.security.spec.PKCS8EncodedKeySpec;34import java.security.spec.X509EncodedKeySpec;35import java.security.spec.InvalidKeySpecException;3637import javax.crypto.SecretKey;38import javax.crypto.Cipher;39import javax.crypto.spec.SecretKeySpec;4041/**42* This class is a helper class which construct key objects43* from encoded keys.44*45* @author Sharon Liu46*47*/4849final class ConstructKeys {50/**51* Construct a public key from its encoding.52*53* @param encodedKey the encoding of a public key.54*55* @param encodedKeyAlgorithm the algorithm the encodedKey is for.56*57* @return a public key constructed from the encodedKey.58*/59private static final PublicKey constructPublicKey(byte[] encodedKey,60String encodedKeyAlgorithm)61throws InvalidKeyException, NoSuchAlgorithmException62{63PublicKey key = null;6465try {66KeyFactory keyFactory =67KeyFactory.getInstance(encodedKeyAlgorithm,68SunJCE.getInstance());69X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);70key = keyFactory.generatePublic(keySpec);71} catch (NoSuchAlgorithmException nsae) {72// Try to see whether there is another73// provider which supports this algorithm74try {75KeyFactory keyFactory =76KeyFactory.getInstance(encodedKeyAlgorithm);77X509EncodedKeySpec keySpec =78new X509EncodedKeySpec(encodedKey);79key = keyFactory.generatePublic(keySpec);80} catch (NoSuchAlgorithmException nsae2) {81throw new NoSuchAlgorithmException("No installed providers " +82"can create keys for the " +83encodedKeyAlgorithm +84"algorithm");85} catch (InvalidKeySpecException ikse2) {86InvalidKeyException ike =87new InvalidKeyException("Cannot construct public key");88ike.initCause(ikse2);89throw ike;90}91} catch (InvalidKeySpecException ikse) {92InvalidKeyException ike =93new InvalidKeyException("Cannot construct public key");94ike.initCause(ikse);95throw ike;96}9798return key;99}100101/**102* Construct a private key from its encoding.103*104* @param encodedKey the encoding of a private key.105*106* @param encodedKeyAlgorithm the algorithm the wrapped key is for.107*108* @return a private key constructed from the encodedKey.109*/110private static final PrivateKey constructPrivateKey(byte[] encodedKey,111String encodedKeyAlgorithm)112throws InvalidKeyException, NoSuchAlgorithmException113{114PrivateKey key = null;115116try {117KeyFactory keyFactory =118KeyFactory.getInstance(encodedKeyAlgorithm,119SunJCE.getInstance());120PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);121return keyFactory.generatePrivate(keySpec);122} catch (NoSuchAlgorithmException nsae) {123// Try to see whether there is another124// provider which supports this algorithm125try {126KeyFactory keyFactory =127KeyFactory.getInstance(encodedKeyAlgorithm);128PKCS8EncodedKeySpec keySpec =129new PKCS8EncodedKeySpec(encodedKey);130key = keyFactory.generatePrivate(keySpec);131} catch (NoSuchAlgorithmException nsae2) {132throw new NoSuchAlgorithmException("No installed providers " +133"can create keys for the " +134encodedKeyAlgorithm +135"algorithm");136} catch (InvalidKeySpecException ikse2) {137InvalidKeyException ike =138new InvalidKeyException("Cannot construct private key");139ike.initCause(ikse2);140throw ike;141}142} catch (InvalidKeySpecException ikse) {143InvalidKeyException ike =144new InvalidKeyException("Cannot construct private key");145ike.initCause(ikse);146throw ike;147}148149return key;150}151152/**153* Construct a secret key from its encoding.154*155* @param encodedKey the encoding of a secret key.156*157* @param encodedKeyAlgorithm the algorithm the secret key is for.158*159* @return a secret key constructed from the encodedKey.160*/161private static final SecretKey constructSecretKey(byte[] encodedKey,162String encodedKeyAlgorithm)163{164return (new SecretKeySpec(encodedKey, encodedKeyAlgorithm));165}166167static final Key constructKey(byte[] encoding, String keyAlgorithm,168int keyType)169throws InvalidKeyException, NoSuchAlgorithmException {170Key result = null;171switch (keyType) {172case Cipher.SECRET_KEY:173result = ConstructKeys.constructSecretKey(encoding,174keyAlgorithm);175break;176case Cipher.PRIVATE_KEY:177result = ConstructKeys.constructPrivateKey(encoding,178keyAlgorithm);179break;180case Cipher.PUBLIC_KEY:181result = ConstructKeys.constructPublicKey(encoding,182keyAlgorithm);183break;184}185return result;186}187}188189190