All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nimbusds.jose.crypto.RSASSASigner Maven / Gradle / Ivy

Go to download

Java library for Javascript Object Signing and Encryption (JOSE) and JSON Web Tokens (JWT)

There is a newer version: 9.48
Show newest version
/*
 * nimbus-jose-jwt
 *
 * Copyright 2012-2016, Connect2id Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.nimbusds.jose.crypto;


import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.SignatureException;

import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.JWSSigner;
import com.nimbusds.jose.jwk.RSAKey;
import com.nimbusds.jose.util.Base64URL;
import net.jcip.annotations.ThreadSafe;



/**
 * RSA Signature-Scheme-with-Appendix (RSASSA) signer of 
 * {@link com.nimbusds.jose.JWSObject JWS objects}. Expects a private RSA key.
 *
 * 

See RFC 7518, sections * 3.3 and * 3.5 for more * information. * *

This class is thread-safe. * *

Supports the following algorithms: * *

    *
  • {@link com.nimbusds.jose.JWSAlgorithm#RS256} *
  • {@link com.nimbusds.jose.JWSAlgorithm#RS384} *
  • {@link com.nimbusds.jose.JWSAlgorithm#RS512} *
  • {@link com.nimbusds.jose.JWSAlgorithm#PS256} *
  • {@link com.nimbusds.jose.JWSAlgorithm#PS384} *
  • {@link com.nimbusds.jose.JWSAlgorithm#PS512} *
* * @author Vladimir Dzhuvinov * @author Omer Levi Hevroni * @version 2016-11-30 */ @ThreadSafe public class RSASSASigner extends RSASSAProvider implements JWSSigner { /** * The private RSA key. Represented by generic private key interface to * support key stores that prevent exposure of the private key * parameters via the {@link java.security.interfaces.RSAPrivateKey} * API. * * See https://bitbucket.org/connect2id/nimbus-jose-jwt/issues/169 */ private final PrivateKey privateKey; /** * Creates a new RSA Signature-Scheme-with-Appendix (RSASSA) signer. * This constructor can also accept a private RSA key located in a * PKCS#11 store that doesn't expose the private key parameters (such * as a smart card or HSM). * * @param privateKey The private RSA key. Its algorithm must be "RSA". * Must not be {@code null}. */ public RSASSASigner(final PrivateKey privateKey) { if (! "RSA".equalsIgnoreCase(privateKey.getAlgorithm())) { throw new IllegalArgumentException("The private key algorithm must be RSA"); } this.privateKey = privateKey; } /** * Creates a new RSA Signature-Scheme-with-Appendix (RSASSA) signer. * * @param rsaJWK The RSA JSON Web Key (JWK). Must contain or reference * a private part. Must not be {@code null}. * * @throws JOSEException If the RSA JWK doesn't contain a private part * or its extraction failed. */ public RSASSASigner(final RSAKey rsaJWK) throws JOSEException { if (! rsaJWK.isPrivate()) { throw new JOSEException("The RSA JWK doesn't contain a private part"); } privateKey = rsaJWK.toPrivateKey(); } /** * Gets the private RSA key. * * @return The private RSA key. Casting to * {@link java.security.interfaces.RSAPrivateKey} may not be * possible if the key is located in a PKCS#11 store that * doesn't expose the private key parameters. */ public PrivateKey getPrivateKey() { return privateKey; } @Override public Base64URL sign(final JWSHeader header, final byte[] signingInput) throws JOSEException { Signature signer = RSASSA.getSignerAndVerifier(header.getAlgorithm(), getJCAContext().getProvider()); try { signer.initSign(privateKey); signer.update(signingInput); return Base64URL.encode(signer.sign()); } catch (InvalidKeyException e) { throw new JOSEException("Invalid private RSA key: " + e.getMessage(), e); } catch (SignatureException e) { throw new JOSEException("RSA signature exception: " + e.getMessage(), e); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy