Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/ParametersMap.java
41161 views
/*1* Copyright (c) 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.ec;2627import sun.security.util.ObjectIdentifier;28import sun.security.x509.AlgorithmId;2930import java.security.spec.AlgorithmParameterSpec;31import java.security.spec.NamedParameterSpec;32import java.util.Collections;33import java.util.Map;34import java.util.HashMap;35import java.util.Optional;36import java.util.function.Function;37import java.util.function.Supplier;3839public class ParametersMap<T> {4041private Map<Integer, T> sizeMap = new HashMap<Integer, T>();42private Map<ObjectIdentifier, T> oidMap =43new HashMap<ObjectIdentifier, T>();44private Map<String, T> nameMap = new HashMap<String, T>();454647public void fix() {4849sizeMap = Collections.unmodifiableMap(sizeMap);50oidMap = Collections.unmodifiableMap(oidMap);51nameMap = Collections.unmodifiableMap(nameMap);52}5354public void put(String name, ObjectIdentifier oid, int size, T params) {55nameMap.put(name.toLowerCase(), params);56oidMap.put(oid, params);57sizeMap.put(size, params);58}5960public Optional<T> getByOid(ObjectIdentifier id) {61return Optional.ofNullable(oidMap.get(id));62}63public Optional<T> getBySize(int size) {64return Optional.ofNullable(sizeMap.get(size));65}66public Optional<T> getByName(String name) {67return Optional.ofNullable(nameMap.get(name.toLowerCase()));68}6970// Utility method that is used by the methods below to handle exception71// suppliers72private static73<A, B> Supplier<B> apply(final Function<A, B> func, final A a) {74return new Supplier<B>() {75@Override76public B get() {77return func.apply(a);78}79};80}8182/**83* Get parameters by key size, or throw an exception if no parameters are84* defined for the specified key size. This method is used in several85* contexts that should throw different exceptions when the parameters86* are not found. The first argument is a function that produces the87* desired exception.88*89* @param exception a function that produces an exception from a string90* @param size the desired key size91* @param <E> the type of exception that is thrown92* @return the parameters for the specified key size93* @throws T when suitable parameters do not exist94*/95public96<E extends Throwable>97T getBySize(Function<String, E> exception,98int size) throws E {99100Optional<T> paramsOpt = getBySize(size);101return paramsOpt.orElseThrow(102apply(exception, "Unsupported size: " + size));103}104105/**106* Get parameters by algorithm ID, or throw an exception if no107* parameters are defined for the specified ID. This method is used in108* several contexts that should throw different exceptions when the109* parameters are not found. The first argument is a function that produces110* the desired exception.111*112* @param exception a function that produces an exception from a string113* @param algId the algorithm ID114* @param <E> the type of exception that is thrown115* @return the parameters for the specified algorithm ID116* @throws E when suitable parameters do not exist117*/118public119<E extends Throwable>120T get(Function<String, E> exception,121AlgorithmId algId) throws E {122123Optional<T> paramsOpt = getByOid(algId.getOID());124return paramsOpt.orElseThrow(125apply(exception, "Unsupported OID: " + algId.getOID()));126}127128/**129* Get parameters by algorithm parameter spec, or throw an exception if no130* parameters are defined for the spec. This method is used in131* several contexts that should throw different exceptions when the132* parameters are not found. The first argument is a function that produces133* the desired exception.134*135* @param exception a function that produces an exception from a string136* @param params the algorithm parameters spec137* @param <E> the type of exception that is thrown138* @return the parameters for the spec139* @throws E when suitable parameters do not exist140*/141public142<E extends Throwable>143T get(Function<String, E> exception,144AlgorithmParameterSpec params) throws E {145146if (params instanceof NamedParameterSpec) {147NamedParameterSpec namedParams = (NamedParameterSpec) params;148Optional<T> paramsOpt = getByName(namedParams.getName());149return paramsOpt.orElseThrow(150apply(exception, "Unsupported name: " + namedParams.getName()));151} else {152throw exception.apply("Only NamedParameterSpec is supported.");153}154}155}156157158