Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/rsa/RSAUtil.java
38830 views
/*1* Copyright (c) 2018, 2020, 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.rsa;2627import java.io.IOException;28import java.security.*;29import java.security.spec.*;30import sun.security.util.ObjectIdentifier;31import sun.security.x509.AlgorithmId;3233/**34* Utility class for SunRsaSign provider.35* Currently used by RSAKeyPairGenerator and RSAKeyFactory.36*37* @since 838*/39public class RSAUtil {4041public enum KeyType {42RSA ("RSA"),43PSS ("RSASSA-PSS")44;4546private final String algo;4748KeyType(String keyAlgo) {49this.algo = keyAlgo;50}51public String keyAlgo() {52return algo;53}54public static KeyType lookup(String name)55throws InvalidKeyException, ProviderException {56if (name == null) {57throw new InvalidKeyException("Null key algorithm");58}59for (KeyType kt : KeyType.values()) {60if (kt.keyAlgo().equalsIgnoreCase(name)) {61return kt;62}63}64// no match65throw new ProviderException("Unsupported algorithm " + name);66}67}6869public static void checkParamsAgainstType(KeyType type,70AlgorithmParameterSpec paramSpec) throws ProviderException {71switch (type) {72case RSA:73if (paramSpec != null) {74throw new ProviderException("null params expected for " +75type.keyAlgo());76}77break;78case PSS:79if ((paramSpec != null) &&80!(paramSpec instanceof PSSParameterSpec)) {81throw new ProviderException82("PSSParmeterSpec expected for " + type.keyAlgo());83}84break;85default:86throw new ProviderException87("Unsupported RSA algorithm " + type);88}89}9091public static AlgorithmId createAlgorithmId(KeyType type,92AlgorithmParameterSpec paramSpec) throws ProviderException {9394checkParamsAgainstType(type, paramSpec);9596ObjectIdentifier oid = null;97AlgorithmParameters params = null;98try {99switch (type) {100case RSA:101oid = AlgorithmId.RSAEncryption_oid;102break;103case PSS:104if (paramSpec != null) {105params = AlgorithmParameters.getInstance(type.keyAlgo());106params.init(paramSpec);107}108oid = AlgorithmId.RSASSA_PSS_oid;109break;110default:111throw new ProviderException112("Unsupported RSA algorithm " + type);113}114AlgorithmId result;115if (params == null) {116result = new AlgorithmId(oid);117} else {118result = new AlgorithmId(oid, params);119}120return result;121} catch (NoSuchAlgorithmException | InvalidParameterSpecException e) {122// should not happen123throw new ProviderException(e);124}125}126127public static AlgorithmParameterSpec getParamSpec(AlgorithmId algid)128throws ProviderException {129if (algid == null) {130throw new ProviderException("AlgorithmId should not be null");131}132return getParamSpec(algid.getParameters());133}134135public static AlgorithmParameterSpec getParamSpec(AlgorithmParameters params)136throws ProviderException {137if (params == null) return null;138139try {140String algName = params.getAlgorithm();141KeyType type = KeyType.lookup(algName);142Class<? extends AlgorithmParameterSpec> specCls;143switch (type) {144case RSA:145throw new ProviderException("No params accepted for " +146type.keyAlgo());147case PSS:148specCls = PSSParameterSpec.class;149break;150default:151throw new ProviderException("Unsupported RSA algorithm: " + algName);152}153return params.getParameterSpec(specCls);154} catch (ProviderException pe) {155// pass it up156throw pe;157} catch (Exception e) {158throw new ProviderException(e);159}160}161}162163164