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