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.security.InvalidAlgorithmParameterException;
022import java.security.NoSuchAlgorithmException;
023import java.security.Provider;
024import java.security.Signature;
025import java.security.spec.MGF1ParameterSpec;
026import java.security.spec.PSSParameterSpec;
027
028import com.nimbusds.jose.JOSEException;
029import com.nimbusds.jose.JWSAlgorithm;
030
031
032/**
033 * RSA-SSA functions and utilities.
034 *
035 * @author Vladimir Dzhuvinov
036 * @version 2015-05-31
037 */
038public class RSASSA {
039
040
041        /**
042         * Returns a signer and verifier for the specified RSASSA-based JSON
043         * Web Algorithm (JWA).
044         *
045         * @param alg The JSON Web Algorithm (JWA). Must be supported and not
046         *            {@code null}.
047         *
048         * @return A signer and verifier instance.
049         *
050         * @throws JOSEException If the algorithm is not supported.
051         */
052        public static Signature getSignerAndVerifier(final JWSAlgorithm alg,
053                                                        final Provider provider)
054                throws JOSEException {
055
056                final String jcaAlg;
057
058                PSSParameterSpec pssSpec = null;
059
060                if (alg.equals(JWSAlgorithm.RS256)) {
061                        jcaAlg = "SHA256withRSA";
062                } else if (alg.equals(JWSAlgorithm.RS384)) {
063                        jcaAlg = "SHA384withRSA";
064                } else if (alg.equals(JWSAlgorithm.RS512)) {
065                        jcaAlg = "SHA512withRSA";
066                } else if (alg.equals(JWSAlgorithm.PS256)) {
067                        jcaAlg = "RSASSA-PSS";
068                        // JWA mandates salt length must equal hash
069                        pssSpec = new PSSParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, 32, 1);
070                } else if (alg.equals(JWSAlgorithm.PS384)) {
071                        jcaAlg = "RSASSA-PSS";
072                        // JWA mandates salt length must equal hash
073                        pssSpec = new PSSParameterSpec("SHA-384", "MGF1", MGF1ParameterSpec.SHA384, 48, 1);
074                } else if (alg.equals(JWSAlgorithm.PS512)) {
075                        jcaAlg = "RSASSA-PSS";
076                        // JWA mandates salt length must equal hash
077                        pssSpec = new PSSParameterSpec("SHA-512", "MGF1", MGF1ParameterSpec.SHA512, 64, 1);
078                } else {
079                        throw new JOSEException(AlgorithmSupportMessage.unsupportedJWSAlgorithm(alg, RSASSAProvider.SUPPORTED_ALGORITHMS));
080                }
081
082                final Signature signature;
083                try {
084                        if (provider != null) {
085                                signature = Signature.getInstance(jcaAlg, provider);
086                        } else {
087                                signature = Signature.getInstance(jcaAlg);
088                        }
089                } catch (NoSuchAlgorithmException e) {
090                        throw new JOSEException("Unsupported RSASSA algorithm: " + e.getMessage(), e);
091                }
092
093
094                if (pssSpec != null) {
095                        try {
096                                signature.setParameter(pssSpec);
097                        } catch (InvalidAlgorithmParameterException e) {
098                                throw new JOSEException("Invalid RSASSA-PSS salt length parameter: " + e.getMessage(), e);
099                        }
100                }
101
102                return signature;
103        }
104
105
106        /**
107         * Prevents public instantiation.
108         */
109        private RSASSA() {
110
111        }
112}