001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.jose.crypto.impl;
019
020
021import java.util.Collections;
022import java.util.LinkedHashSet;
023import java.util.Set;
024
025import com.nimbusds.jose.JOSEException;
026import com.nimbusds.jose.JWSAlgorithm;
027
028
029/**
030 * The base abstract class for Elliptic Curve Digital Signature Algorithm 
031 * (ECDSA) signers and validators of {@link com.nimbusds.jose.JWSObject JWS 
032 * objects}.
033 *
034 * <p>Supports the following algorithms:
035 *
036 * <ul>
037 *     <li>{@link com.nimbusds.jose.JWSAlgorithm#ES256}
038 *     <li>{@link com.nimbusds.jose.JWSAlgorithm#ES256K}
039 *     <li>{@link com.nimbusds.jose.JWSAlgorithm#ES384}
040 *     <li>{@link com.nimbusds.jose.JWSAlgorithm#ES512}
041 * </ul>
042 * 
043 * @author Axel Nennker
044 * @author Vladimir Dzhuvinov
045 * @version 2022-04-21
046 */
047public abstract class ECDSAProvider extends BaseJWSProvider {
048
049
050        /**
051         * The supported JWS algorithms by the EC-DSA provider class.
052         */
053        public static final Set<JWSAlgorithm> SUPPORTED_ALGORITHMS;
054
055
056        static {
057                Set<JWSAlgorithm> algs = new LinkedHashSet<>();
058                algs.add(JWSAlgorithm.ES256);
059                algs.add(JWSAlgorithm.ES256K);
060                algs.add(JWSAlgorithm.ES384);
061                algs.add(JWSAlgorithm.ES512);
062                SUPPORTED_ALGORITHMS = Collections.unmodifiableSet(algs);
063        }
064
065
066        /**
067         * Creates a new Elliptic Curve Digital Signature Algorithm (ECDSA) 
068         * provider.
069         *
070         * @param alg The EC-DSA algorithm. Must be supported and not
071         *            {@code null}.
072         *
073         * @throws JOSEException If JWS algorithm is not supported.
074         */
075        protected ECDSAProvider(final JWSAlgorithm alg)
076                throws JOSEException {
077
078                super(Collections.singleton(alg));
079
080                if (! SUPPORTED_ALGORITHMS.contains(alg)) {
081                        throw new JOSEException("Unsupported EC DSA algorithm: " + alg);
082                }
083        }
084        
085        
086        /**
087         * Returns the supported ECDSA algorithm.
088         *
089         * @see #supportedJWSAlgorithms()
090         *
091         * @return The supported ECDSA algorithm.
092         */
093        public JWSAlgorithm supportedECDSAAlgorithm() {
094                
095                return supportedJWSAlgorithms().iterator().next();
096        }
097}
098