javajwtdigital-signaturemscapijjwt

How do I use mscapi.RSAPrivateKey for JWT signing?


I'm not sure how to ask this but I'm trying to use various Json Web Token implementations from the web (whether is java-jwt or jjwt etc), they all takes in a RSAPrivateKey as part of their signing process.

However the problem I have is that, apparently the mscapi.RSAPrivateKey doesn't provide the same API as the java.security.interfaces.RSAPrivateKey and when I try to write my wrapper class for it, I have no idea on how to write the getEncoded or the getFormat (I'm guessing I should return "RS256" or something similar) function.

  1. How can I wrap the mscapi.RSAPrivateKey?
  2. What is the correct way to convert mscapi.RSAPrivateKey to the java.security.interfaces.RSAPrivateKey?

Solution

  • It is not needed to get the content or wrap the private key to sign a JWT. The cryptographic provider, in your case mscapi, intentionally hides the content to protect it from extraction, or because it is physically unavailable, for example, using a smartcard. But mscapi allows to use that key for signing or encryption, so there is no reason why your library can not sign the jwt with an instance of PrivateKey (not RSAPrivate Key)

    The issue you describe is similar tothis. It seems jjwt tries to cast the PrivateKey and the mscapi wrapper does not implement the usual interface.

    I do not think you could create a wrapper on a mscapi key if getEncoded is needed, because as I said, you have not access to the keying material. So I suggest to use jjwt to build the token (header.payload) and sign it yourself using the standard signature api