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 2021-02-20
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                // Alternative JCA name to try
059                String jcaAlgAlt = null;
060                
061                PSSParameterSpec pssSpec = null;
062
063                if (alg.equals(JWSAlgorithm.RS256)) {
064                        
065                        jcaAlg = "SHA256withRSA";
066                        
067                } else if (alg.equals(JWSAlgorithm.RS384)) {
068                        
069                        jcaAlg = "SHA384withRSA";
070                        
071                } else if (alg.equals(JWSAlgorithm.RS512)) {
072                        
073                        jcaAlg = "SHA512withRSA";
074                        
075                } else if (alg.equals(JWSAlgorithm.PS256)) {
076                        
077                        jcaAlg = "RSASSA-PSS"; // JWA mandates salt length equals hash
078                        pssSpec = new PSSParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, 32, 1);
079                        
080                        jcaAlgAlt = "SHA256withRSAandMGF1";
081                        
082                } else if (alg.equals(JWSAlgorithm.PS384)) {
083                        
084                        jcaAlg = "RSASSA-PSS"; // JWA mandates salt length equals hash
085                        pssSpec = new PSSParameterSpec("SHA-384", "MGF1", MGF1ParameterSpec.SHA384, 48, 1);
086                        
087                        jcaAlgAlt = "SHA384withRSAandMGF1";
088                        
089                } else if (alg.equals(JWSAlgorithm.PS512)) {
090                        
091                        jcaAlg = "RSASSA-PSS"; // JWA mandates salt length equals hash
092                        pssSpec = new PSSParameterSpec("SHA-512", "MGF1", MGF1ParameterSpec.SHA512, 64, 1);
093                        
094                        jcaAlgAlt = "SHA512withRSAandMGF1";
095                        
096                } else {
097                        throw new JOSEException(AlgorithmSupportMessage.unsupportedJWSAlgorithm(alg, RSASSAProvider.SUPPORTED_ALGORITHMS));
098                }
099
100                Signature signature;
101                try {
102                        signature = getSignerAndVerifier(jcaAlg, provider);
103                        
104                } catch (NoSuchAlgorithmException e) {
105                        
106                        if (jcaAlgAlt == null) {
107                                throw new JOSEException("Unsupported RSASSA algorithm: " + e.getMessage(), e);
108                        }
109                        
110                        // Retry with alternative JCA name
111                        try {
112                                signature = getSignerAndVerifier(jcaAlgAlt, provider);
113                        } catch (NoSuchAlgorithmException e2) {
114                                throw new JOSEException("Unsupported RSASSA algorithm (after retry with alternative): " + e2.getMessage(), e2);
115                        }
116                }
117                
118                if (pssSpec != null) {
119                        try {
120                                signature.setParameter(pssSpec);
121                        } catch (InvalidAlgorithmParameterException e) {
122                                throw new JOSEException("Invalid RSASSA-PSS salt length parameter: " + e.getMessage(), e);
123                        }
124                }
125
126                return signature;
127        }
128        
129        
130        private static Signature getSignerAndVerifier(final String jcaAlg, final Provider provider)
131                throws NoSuchAlgorithmException {
132                
133                if (provider != null) {
134                        return Signature.getInstance(jcaAlg, provider);
135                } else {
136                        return Signature.getInstance(jcaAlg);
137                }
138        }
139
140
141        /**
142         * Prevents public instantiation.
143         */
144        private RSASSA() {
145
146        }
147}