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 com.nimbusds.jose.JOSEException;
022import com.nimbusds.jose.JWSAlgorithm;
023import com.nimbusds.jose.jwk.Curve;
024
025import java.util.Collections;
026import java.util.LinkedHashSet;
027import java.util.Set;
028
029
030/**
031 * The base abstract class for Elliptic Curve Digital Signature Algorithm 
032 * (ECDSA) signers and validators of {@link com.nimbusds.jose.JWSObject JWS 
033 * objects}.
034 *
035 * <p>Supports the following algorithms:
036 *
037 * <ul>
038 *     <li>{@link com.nimbusds.jose.JWSAlgorithm#ES256}
039 *     <li>{@link com.nimbusds.jose.JWSAlgorithm#ES256K}
040 *     <li>{@link com.nimbusds.jose.JWSAlgorithm#ES384}
041 *     <li>{@link com.nimbusds.jose.JWSAlgorithm#ES512}
042 * </ul>
043 * 
044 * @author Axel Nennker
045 * @author Vladimir Dzhuvinov
046 * @version 2024-05-07
047 */
048public abstract class ECDSAProvider extends BaseJWSProvider {
049
050
051        /**
052         * The supported JWS algorithms by the EC-DSA provider class.
053         */
054        public static final Set<JWSAlgorithm> SUPPORTED_ALGORITHMS;
055
056
057        /**
058         * The supported curves by the EC-DSA provider class.
059         */
060        public static final Set<Curve> SUPPORTED_CURVES;
061
062
063        static {
064                Set<JWSAlgorithm> algs = new LinkedHashSet<>();
065                algs.add(JWSAlgorithm.ES256);
066                algs.add(JWSAlgorithm.ES256K);
067                algs.add(JWSAlgorithm.ES384);
068                algs.add(JWSAlgorithm.ES512);
069                SUPPORTED_ALGORITHMS = Collections.unmodifiableSet(algs);
070
071                Set<Curve> curves = new LinkedHashSet<>();
072                curves.add(Curve.P_256);
073                curves.add(Curve.SECP256K1);
074                curves.add(Curve.P_384);
075                curves.add(Curve.P_521);
076                SUPPORTED_CURVES = Collections.unmodifiableSet(curves);
077        }
078
079
080        /**
081         * Creates a new Elliptic Curve Digital Signature Algorithm (ECDSA) 
082         * provider.
083         *
084         * @param alg The EC-DSA algorithm. Must be supported and not
085         *            {@code null}.
086         *
087         * @throws JOSEException If JWS algorithm is not supported.
088         */
089        protected ECDSAProvider(final JWSAlgorithm alg)
090                throws JOSEException {
091
092                super(Collections.singleton(alg));
093
094                if (! SUPPORTED_ALGORITHMS.contains(alg)) {
095                        throw new JOSEException("Unsupported EC DSA algorithm: " + alg);
096                }
097        }
098        
099        
100        /**
101         * Returns the supported ECDSA algorithm.
102         *
103         * @see #supportedJWSAlgorithms()
104         *
105         * @return The supported ECDSA algorithm.
106         */
107        public JWSAlgorithm supportedECDSAAlgorithm() {
108                
109                return supportedJWSAlgorithms().iterator().next();
110        }
111}
112