All Implemented Interfaces:
AsymmetricJWK<RSAPublicKey,RSAPrivateKey>, JWK, RSAJWK, X509JWK<RSAPublicKey,RSAPrivateKey>

public class GenericRSAJWK extends AbstractX509JWK<RSAPublicKey,RSAPrivateKey> implements RSAJWK

Generic RSA JSON Web Key implementation.

It supports the following algorithms:

  • RS1
  • RS256
  • RS384
  • RS512
  • PS256
  • PS384
  • PS512
  • RSA1_5
  • RSA-OAEP
  • RSA-OAEP-256
  • RSA-OAEP-384
  • RSA-OAEP-512
Since:
1.5
Author:
Jeremy Kuhn
  • Constructor Details

    • GenericRSAJWK

      public GenericRSAJWK(String n, String e)

      Creates an untrusted public generic RSA JWK with the specified public key parameters.

      Parameters:
      n - the modulus encoded as Base64URL without padding
      e - the public exponent encoded as Base64URL without padding
    • GenericRSAJWK

      public GenericRSAJWK(String n, String e, X509Certificate certificate)

      Creates a public generic RSA JWK with the specified public key parameters and certificate.

      The JWK is considered trusted if the specified certificate, which is assumed to be validated, is not null.

      Parameters:
      n - the modulus encoded as Base64URL without padding
      e - the public exponent encoded as Base64URL without padding
      certificate - an X.509 certificate
    • GenericRSAJWK

      public GenericRSAJWK(String n, String e, String d)

      Creates an untrusted private generic RSA JWK with the specified public key parameters and private exponent.

      Parameters:
      n - the modulus encoded as Base64URL without padding
      e - the public exponent encoded as Base64URL without padding
      d - the private exponent encoded as Base64URL without padding
    • GenericRSAJWK

      public GenericRSAJWK(String n, String e, String d, RSAPrivateKey key, boolean trusted)

      Creates a private generic RSA JWK with the specified public key parameters, private exponent and RSA private key.

      Parameters:
      n - the modulus encoded as Base64URL without padding
      e - the public exponent encoded as Base64URL without padding
      d - the private exponent encoded as Base64URL without padding
      key - an EC private key
      trusted - true to create a trusted JWK, false otherwise
    • GenericRSAJWK

      public GenericRSAJWK(String n, String e, String d, X509Certificate certificate)

      Creates a public generic RSA JWK with the specified public key parameters, private exponent and certificate.

      The JWK is considered trusted if the specified certificate, which is assumed to be validated, is not null.

      Parameters:
      n - the modulus encoded as Base64URL without padding
      e - the public exponent encoded as Base64URL without padding
      d - the private exponent encoded as Base64URL without padding
      certificate - an X.509 certificate
    • GenericRSAJWK

      public GenericRSAJWK(String n, String e, String d, RSAPrivateKey key, X509Certificate certificate, boolean trusted)

      Creates a private generic RSA JWK with the specified public key parameters, private exponent, RSA private key and certificate.

      Parameters:
      n - the modulus encoded as Base64URL without padding
      e - the public exponent encoded as Base64URL without padding
      d - the private exponent encoded as Base64URL without padding
      key - an RSA private key
      certificate - an X.509 certificate
      trusted - true to create a trusted JWK, false otherwise
  • Method Details

    • setAlgorithm

      public void setAlgorithm(RSAAlgorithm rsaAlg)

      Sets the RSA JWA algorithm.

      Parameters:
      rsaAlg - an RSA algorithm
    • setAlgorithm

      public void setAlgorithm(String alg)
      Description copied from class: AbstractJWK

      Sets the algorithm intended for use with the key.

      Overrides:
      setAlgorithm in class AbstractJWK
      Parameters:
      alg - the JWA algorithm
    • getModulus

      public String getModulus()
      Description copied from interface: RSAJWK

      Returns the modulus encoded as Base64URL.

      Specified by:
      getModulus in interface RSAJWK
      Returns:
      the Base64URL encoded modulus with no padding.
    • getPublicExponent

      public String getPublicExponent()
      Description copied from interface: RSAJWK

      Returns the public exponent encoded as Base64URL.

      Specified by:
      getPublicExponent in interface RSAJWK
      Returns:
      the Base64URL encoded public exponent with no padding.
    • getPrivateExponent

      public String getPrivateExponent()
      Description copied from interface: RSAJWK

      Returns the private exponent encoded as Base64URL.

      Specified by:
      getPrivateExponent in interface RSAJWK
      Returns:
      the Base64URL encoded private exponent with no padding.
    • setFirstPrimeFactor

      public void setFirstPrimeFactor(String p)

      Sets the first prime factor encoded as Base64URL.

      Parameters:
      p - a Base64URL encoded first prime factor without padding
    • getFirstPrimeFactor

      public String getFirstPrimeFactor()
      Description copied from interface: RSAJWK

      Returns the first prime factor encoded as Base64URL.

      Specified by:
      getFirstPrimeFactor in interface RSAJWK
      Returns:
      the Base64URL encoded first prime factor with no padding.
    • setSecondPrimeFactor

      public void setSecondPrimeFactor(String q)

      Sets the second prime factor encoded as Base64URL.

      Parameters:
      q - a Base64URL encoded second prime factor without padding
    • getSecondPrimeFactor

      public String getSecondPrimeFactor()
      Description copied from interface: RSAJWK

      Returns the second prime factor encoded as Base64URL.

      Specified by:
      getSecondPrimeFactor in interface RSAJWK
      Returns:
      the Base64URL encoded second prime factor with no padding.
    • setFirstFactorExponent

      public void setFirstFactorExponent(String dp)

      Sets the first factor exponent encoded as Base64URL.

      Parameters:
      dp - a Base64URL encoded first factor exponent without padding
    • getFirstFactorExponent

      public String getFirstFactorExponent()
      Description copied from interface: RSAJWK

      Returns the first factor exponent encoded as Base64URL.

      Specified by:
      getFirstFactorExponent in interface RSAJWK
      Returns:
      the Base64URL encoded first factor exponent with no padding.
    • setSecondFactorExponent

      public void setSecondFactorExponent(String dq)

      Sets the second factor exponent encoded as Base64URL.

      Parameters:
      dq - a Base64URL encoded second factor exponent without padding
    • getSecondFactorExponent

      public String getSecondFactorExponent()
      Description copied from interface: RSAJWK

      Returns the second factor exponent encoded as Base64URL.

      Specified by:
      getSecondFactorExponent in interface RSAJWK
      Returns:
      the Base64URL encoded second factor exponent with no padding.
    • setFirstCoefficient

      public void setFirstCoefficient(String qi)

      Sets the first coefficient encoded as Base64URL.

      Parameters:
      qi - a Base64URL encoded first coefficient without padding
    • getFirstCoefficient

      public String getFirstCoefficient()
      Description copied from interface: RSAJWK

      Returns the first coefficient encoded as Base64URL.

      Specified by:
      getFirstCoefficient in interface RSAJWK
      Returns:
      the Base64URL encoded first coefficient with no padding.
    • setOtherPrimesInfo

      public void setOtherPrimesInfo(List<RSAJWK.OtherPrimeInfo> oth)

      Sets other prime info.

      Parameters:
      oth - a list of other prime info
    • getOtherPrimesInfo

      public List<RSAJWK.OtherPrimeInfo> getOtherPrimesInfo()
      Description copied from interface: RSAJWK

      Returns the list of other primes info.

      Specified by:
      getOtherPrimesInfo in interface RSAJWK
      Returns:
      the list of other primes info or null
    • trust

      public RSAJWK trust()
      Description copied from interface: JWK

      Trusts the key explicitly.

      This should be used with care when the authenticity of an untrusted key has been established through external means.

      Specified by:
      trust in interface AsymmetricJWK<RSAPublicKey,RSAPrivateKey>
      Specified by:
      trust in interface JWK
      Specified by:
      trust in interface RSAJWK
      Specified by:
      trust in interface X509JWK<RSAPublicKey,RSAPrivateKey>
      Returns:
      this JWK
    • toPublicKey

      public RSAPublicKey toPublicKey() throws JWKProcessingException
      Description copied from interface: AsymmetricJWK

      Converts the JWK to its corresponding public key.

      Specified by:
      toPublicKey in interface AsymmetricJWK<RSAPublicKey,RSAPrivateKey>
      Returns:
      a public key
      Throws:
      JWKProcessingException - if there was an error converting the JWK to a public key
    • toPrivateKey

      public Optional<RSAPrivateKey> toPrivateKey() throws JWKProcessingException
      Description copied from interface: AsymmetricJWK

      Converts the JWK to its corresponding private key.

      Specified by:
      toPrivateKey in interface AsymmetricJWK<RSAPublicKey,RSAPrivateKey>
      Returns:
      an optional containing the private key or an empty optional if the key does not contain private information
      Throws:
      JWKProcessingException - if there was an error converting the JWK to a private key
    • toPublicJWK

      public RSAJWK toPublicJWK()
      Description copied from interface: JWK

      Returns a public and safe to share representation of the key.

      Specified by:
      toPublicJWK in interface AsymmetricJWK<RSAPublicKey,RSAPrivateKey>
      Specified by:
      toPublicJWK in interface JWK
      Specified by:
      toPublicJWK in interface RSAJWK
      Specified by:
      toPublicJWK in interface X509JWK<RSAPublicKey,RSAPrivateKey>
      Returns:
      a public representation of this JWK
    • minify

      public RSAJWK minify()
      Description copied from interface: JWK

      Returns a minified representation of the key only containing required data.

      Note that the returned JWK may contain private data.

      Specified by:
      minify in interface JWK
      Specified by:
      minify in interface RSAJWK
      Returns:
      a minified representation of this JWK
    • toJWKThumbprint

      public String toJWKThumbprint(MessageDigest digest)
      Description copied from interface: JWK

      Generates and returns the JWK thumbprint using the specified digest.

      Specified by:
      toJWKThumbprint in interface JWK
      Parameters:
      digest - the message digest to use
      Returns:
      the JWK thumbprint
    • supportsAlgorithm

      public boolean supportsAlgorithm(String alg)
      Description copied from interface: JWK

      Determines whether the JWK supports the specified JWA algorithm.

      Specified by:
      supportsAlgorithm in interface JWK
      Parameters:
      alg - a JWA algorithm
      Returns:
      true if the algorithm is supported, false otherwise
    • signer

      public JWASigner signer() throws JWKProcessingException
      Description copied from interface: JWK

      Returns a signer using this JWK.

      Specified by:
      signer in interface JWK
      Overrides:
      signer in class AbstractJWK
      Returns:
      a signer
      Throws:
      JWKProcessingException - if the JWK does not support signature operations (i.e. missing algorithm, algorithm is not a signature algorithm...)
    • signer

      public JWASigner signer(String alg)
      Description copied from interface: JWK

      Returns a signer using this JWK and the specified algorithm.

      Specified by:
      signer in interface JWK
      Overrides:
      signer in class AbstractJWK
      Parameters:
      alg - a JWA signature algorithm
      Returns:
      a signer
    • keyManager

      public JWAKeyManager keyManager() throws JWKProcessingException
      Description copied from interface: JWK

      Returns a key manager using this JWK.

      Specified by:
      keyManager in interface JWK
      Overrides:
      keyManager in class AbstractJWK
      Returns:
      a key manager
      Throws:
      JWKProcessingException - if the JWK does not support key management operations (i.e. missing algorithm, algorithm is not a key management algorithm...)
    • keyManager

      public JWAKeyManager keyManager(String alg) throws JWKProcessingException
      Description copied from interface: JWK

      Returns a key manager using this JWK and the specified algorithm.

      Specified by:
      keyManager in interface JWK
      Overrides:
      keyManager in class AbstractJWK
      Parameters:
      alg - a JWA key management algorithm
      Returns:
      a key manager
      Throws:
      JWKProcessingException - if the JWK does not support key management operations or if the specified algorithm is not a supported key management algorithm
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface JWK
      Overrides:
      hashCode in class AbstractX509JWK<RSAPublicKey,RSAPrivateKey>
    • equals

      public boolean equals(Object obj)
      Specified by:
      equals in interface JWK
      Overrides:
      equals in class AbstractX509JWK<RSAPublicKey,RSAPrivateKey>