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 // The JCE crypto provider uses different alg names 057 058 final String jcaAlg; 059 060 PSSParameterSpec pssSpec = null; 061 062 if (alg.equals(JWSAlgorithm.RS256)) { 063 jcaAlg = "SHA256withRSA"; 064 } else if (alg.equals(JWSAlgorithm.RS384)) { 065 jcaAlg = "SHA384withRSA"; 066 } else if (alg.equals(JWSAlgorithm.RS512)) { 067 jcaAlg = "SHA512withRSA"; 068 } else if (alg.equals(JWSAlgorithm.PS256)) { 069 jcaAlg = "SHA256withRSAandMGF1"; 070 // JWA mandates salt length must equal hash 071 pssSpec = new PSSParameterSpec("SHA256", "MGF1", MGF1ParameterSpec.SHA256, 32, 1); 072 } else if (alg.equals(JWSAlgorithm.PS384)) { 073 jcaAlg = "SHA384withRSAandMGF1"; 074 // JWA mandates salt length must equal hash 075 pssSpec = new PSSParameterSpec("SHA384", "MGF1", MGF1ParameterSpec.SHA384, 48, 1); 076 } else if (alg.equals(JWSAlgorithm.PS512)) { 077 jcaAlg = "SHA512withRSAandMGF1"; 078 // JWA mandates salt length must equal hash 079 pssSpec = new PSSParameterSpec("SHA512", "MGF1", MGF1ParameterSpec.SHA512, 64, 1); 080 } else { 081 throw new JOSEException(AlgorithmSupportMessage.unsupportedJWSAlgorithm(alg, RSASSAProvider.SUPPORTED_ALGORITHMS)); 082 } 083 084 final Signature signature; 085 try { 086 if (provider != null) { 087 signature = Signature.getInstance(jcaAlg, provider); 088 } else { 089 signature = Signature.getInstance(jcaAlg); 090 } 091 } catch (NoSuchAlgorithmException e) { 092 throw new JOSEException("Unsupported RSASSA algorithm: " + e.getMessage(), e); 093 } 094 095 096 if (pssSpec != null) { 097 try { 098 signature.setParameter(pssSpec); 099 } catch (InvalidAlgorithmParameterException e) { 100 throw new JOSEException("Invalid RSASSA-PSS salt length parameter: " + e.getMessage(), e); 101 } 102 } 103 104 return signature; 105 } 106 107 108 /** 109 * Prevents public instantiation. 110 */ 111 private RSASSA() { 112 113 } 114}