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.HashSet;
023import java.util.LinkedHashSet;
024import java.util.Set;
025
026import com.nimbusds.jose.JOSEException;
027import com.nimbusds.jose.JWSAlgorithm;
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 2017-05-13
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        static {
058                Set<JWSAlgorithm> algs = new LinkedHashSet<>();
059                algs.add(JWSAlgorithm.ES256);
060                algs.add(JWSAlgorithm.ES256K);
061                algs.add(JWSAlgorithm.ES384);
062                algs.add(JWSAlgorithm.ES512);
063                SUPPORTED_ALGORITHMS = Collections.unmodifiableSet(algs);
064        }
065
066
067        /**
068         * Creates a new Elliptic Curve Digital Signature Algorithm (ECDSA) 
069         * provider.
070         *
071         * @param alg The EC-DSA algorithm. Must be supported and not
072         *            {@code null}.
073         *
074         * @throws JOSEException If JWS algorithm is not supported.
075         */
076        protected ECDSAProvider(final JWSAlgorithm alg)
077                throws JOSEException {
078
079                super(new HashSet<>(Collections.singletonList(alg)));
080
081                if (! SUPPORTED_ALGORITHMS.contains(alg)) {
082                        throw new JOSEException("Unsupported EC DSA algorithm: " + alg);
083                }
084        }
085        
086        
087        /**
088         * Returns the supported ECDSA algorithm.
089         *
090         * @see #supportedJWSAlgorithms()
091         *
092         * @return The supported ECDSA algorithm.
093         */
094        public JWSAlgorithm supportedECDSAAlgorithm() {
095                
096                return supportedJWSAlgorithms().iterator().next();
097        }
098}
099