Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/util/AlgorithmDecomposer.java
38830 views
/*1* Copyright (c) 2015, 2017, 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.util;2627import java.util.HashSet;28import java.util.Set;29import java.util.Arrays;30import java.util.Collection;31import java.util.regex.Pattern;3233/**34* The class decomposes standard algorithms into sub-elements.35*/36public class AlgorithmDecomposer {3738private static final Pattern transPattern = Pattern.compile("/");39private static final Pattern pattern =40Pattern.compile("with|and", Pattern.CASE_INSENSITIVE);4142private static Set<String> decomposeImpl(String algorithm) {4344// algorithm/mode/padding45String[] transTockens = transPattern.split(algorithm);4647Set<String> elements = new HashSet<>();48for (String transTocken : transTockens) {49if (transTocken == null || transTocken.length() == 0) {50continue;51}5253// PBEWith<digest>And<encryption>54// PBEWith<prf>And<encryption>55// OAEPWith<digest>And<mgf>Padding56// <digest>with<encryption>57// <digest>with<encryption>and<mgf>58String[] tokens = pattern.split(transTocken);5960for (String token : tokens) {61if (token == null || token.length() == 0) {62continue;63}6465elements.add(token);66}67}68return elements;69}7071/**72* Decompose the standard algorithm name into sub-elements.73* <p>74* For example, we need to decompose "SHA1WithRSA" into "SHA1" and "RSA"75* so that we can check the "SHA1" and "RSA" algorithm constraints76* separately.77* <p>78* Please override the method if need to support more name pattern.79*/80public Set<String> decompose(String algorithm) {81if (algorithm == null || algorithm.length() == 0) {82return new HashSet<>();83}8485Set<String> elements = decomposeImpl(algorithm);8687// In Java standard algorithm name specification, for different88// purpose, the SHA-1 and SHA-2 algorithm names are different. For89// example, for MessageDigest, the standard name is "SHA-256", while90// for Signature, the digest algorithm component is "SHA256" for91// signature algorithm "SHA256withRSA". So we need to check both92// "SHA-256" and "SHA256" to make the right constraint checking.9394// handle special name: SHA-1 and SHA195if (elements.contains("SHA1") && !elements.contains("SHA-1")) {96elements.add("SHA-1");97}98if (elements.contains("SHA-1") && !elements.contains("SHA1")) {99elements.add("SHA1");100}101102// handle special name: SHA-224 and SHA224103if (elements.contains("SHA224") && !elements.contains("SHA-224")) {104elements.add("SHA-224");105}106if (elements.contains("SHA-224") && !elements.contains("SHA224")) {107elements.add("SHA224");108}109110// handle special name: SHA-256 and SHA256111if (elements.contains("SHA256") && !elements.contains("SHA-256")) {112elements.add("SHA-256");113}114if (elements.contains("SHA-256") && !elements.contains("SHA256")) {115elements.add("SHA256");116}117118// handle special name: SHA-384 and SHA384119if (elements.contains("SHA384") && !elements.contains("SHA-384")) {120elements.add("SHA-384");121}122if (elements.contains("SHA-384") && !elements.contains("SHA384")) {123elements.add("SHA384");124}125126// handle special name: SHA-512 and SHA512127if (elements.contains("SHA512") && !elements.contains("SHA-512")) {128elements.add("SHA-512");129}130if (elements.contains("SHA-512") && !elements.contains("SHA512")) {131elements.add("SHA512");132}133134return elements;135}136137/**138* Get aliases of the specified algorithm.139*140* May support more algorithms in the future.141*/142public static Collection<String> getAliases(String algorithm) {143String[] aliases;144if (algorithm.equalsIgnoreCase("DH") ||145algorithm.equalsIgnoreCase("DiffieHellman")) {146aliases = new String[] {"DH", "DiffieHellman"};147} else {148aliases = new String[] {algorithm};149}150151return Arrays.asList(aliases);152}153154private static void hasLoop(Set<String> elements, String find, String replace) {155if (elements.contains(find)) {156if (!elements.contains(replace)) {157elements.add(replace);158}159elements.remove(find);160}161}162163/*164* This decomposes a standard name into sub-elements with a consistent165* message digest algorithm name to avoid overly complicated checking.166*/167public static Set<String> decomposeOneHash(String algorithm) {168if (algorithm == null || algorithm.length() == 0) {169return new HashSet<>();170}171172Set<String> elements = decomposeImpl(algorithm);173174hasLoop(elements, "SHA-1", "SHA1");175hasLoop(elements, "SHA-224", "SHA224");176hasLoop(elements, "SHA-256", "SHA256");177hasLoop(elements, "SHA-384", "SHA384");178hasLoop(elements, "SHA-512", "SHA512");179180return elements;181}182183/*184* The provided message digest algorithm name will return a consistent185* naming scheme.186*/187public static String hashName(String algorithm) {188return algorithm.replace("-", "");189}190}191192193