Path: blob/master/src/java.base/share/classes/sun/security/rsa/RSAUtil.java
41159 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 1138*/39public class RSAUtil {4041public enum KeyType {42RSA ("RSA", AlgorithmId.RSAEncryption_oid, null),43PSS ("RSASSA-PSS", AlgorithmId.RSASSA_PSS_oid, PSSParameterSpec.class)44;4546final String keyAlgo;47final ObjectIdentifier oid;48final Class<? extends AlgorithmParameterSpec> paramSpecCls;4950KeyType(String keyAlgo, ObjectIdentifier oid,51Class<? extends AlgorithmParameterSpec> paramSpecCls) {52this.keyAlgo = keyAlgo;53this.oid = oid;54this.paramSpecCls = paramSpecCls;55}5657public static KeyType lookup(String name) throws ProviderException {5859requireNonNull(name, "Key algorithm should not be null");6061// match loosely in order to work with 3rd party providers which62// may not follow the standard names63if (name.indexOf("PSS") != -1) {64return PSS;65} else if (name.indexOf("RSA") != -1) {66return RSA;67} else { // no match68throw new ProviderException("Unsupported algorithm " + name);69}70}71}7273private static void requireNonNull(Object obj, String msg) {74if (obj == null) throw new ProviderException(msg);75}7677public static AlgorithmParameterSpec checkParamsAgainstType(KeyType type,78AlgorithmParameterSpec paramSpec) throws ProviderException {7980// currently no check for null parameter spec81// assumption is parameter spec is optional and can be null82if (paramSpec == null) return null;8384Class<? extends AlgorithmParameterSpec> expCls = type.paramSpecCls;85if (expCls == null) {86throw new ProviderException("null params expected for " +87type.keyAlgo);88} else if (!expCls.isInstance(paramSpec)) {89throw new ProviderException90(expCls + " expected for " + type.keyAlgo);91}92return paramSpec;93}9495public static AlgorithmParameters getParams(KeyType type,96AlgorithmParameterSpec spec) throws ProviderException {9798if (spec == null) return null;99100try {101AlgorithmParameters params =102AlgorithmParameters.getInstance(type.keyAlgo);103params.init(spec);104return params;105} catch (NoSuchAlgorithmException | InvalidParameterSpecException ex) {106throw new ProviderException(ex);107}108}109110public static AlgorithmId createAlgorithmId(KeyType type,111AlgorithmParameterSpec paramSpec) throws ProviderException {112113checkParamsAgainstType(type, paramSpec);114115ObjectIdentifier oid = type.oid;116AlgorithmParameters params = getParams(type, paramSpec);117return new AlgorithmId(oid, params);118}119120public static AlgorithmParameterSpec getParamSpec(121AlgorithmParameters params) throws ProviderException {122123if (params == null) return null;124125String algName = params.getAlgorithm();126127KeyType type = KeyType.lookup(algName);128Class<? extends AlgorithmParameterSpec> specCls = type.paramSpecCls;129if (specCls == null) {130throw new ProviderException("No params accepted for " +131type.keyAlgo);132}133try {134return params.getParameterSpec(specCls);135} catch (InvalidParameterSpecException ex) {136throw new ProviderException(ex);137}138}139140public static Object[] getTypeAndParamSpec(AlgorithmId algid)141throws ProviderException {142143requireNonNull(algid, "AlgorithmId should not be null");144145Object[] result = new Object[2];146147String algName = algid.getName();148try {149result[0] = KeyType.lookup(algName);150} catch (ProviderException pe) {151// accommodate RSA keys encoded with various RSA signature oids152// for backward compatibility153if (algName.indexOf("RSA") != -1) {154result[0] = KeyType.RSA;155} else {156// pass it up157throw pe;158}159}160161result[1] = getParamSpec(algid.getParameters());162return result;163}164}165166167