3

I am working on ServiceNow - BOX integration using rest API. for generating access tokens, I need to generate a JWT.

I need to have a private key to sign that JWT.

They have provided the private key but it is in an encrypted format.

-----BEGIN ENCRYPTED PRIVATE KEY-----
*******Key here********** 
-----END ENCRYPTED PRIVATE KEY-----

I have also been provided with a pass code to decrypt it.

I am not sure how to decrypt above private key in ServiceNow.

They gave examples in other languages here: https://developer.box.com/docs/construct-jwt-claim-manually#section-2-decrypt-private-key

Please help me in getting this done in javascript pure implementation.

Thanks,

Ali

Ellery
  • 1,356
  • 1
  • 14
  • 22
Ahmed Ali
  • 33
  • 3
  • 1
    Adding the privateKey and passphrase in the front end is a bad idea. I suggest you use your backend as a middle layer and send request to servicenow-box from the backend. – nightgaunt Nov 30 '18 at 06:07
  • Yes. I am not using these keys in frond end. These will be processed at server side in servicenow. How can I get decrypted private key with pure javascript ? – Ahmed Ali Nov 30 '18 at 06:16
  • Which backend technology are you using? To be more precise, which backed tech servicenow uses? – nightgaunt Nov 30 '18 at 06:39
  • ServiceNow uses javascript in backend as well. Recently It upgraded to ECMAScript5 engine. I need to have javascript code for decryption. I can have CryptoJS in servicenow for signing the jwt with rs256. But just not sure about how to have decrypted key from above encrypted form with the passcode. – Ahmed Ali Nov 30 '18 at 06:47

1 Answers1

1

From what you posted above it looks like you are receiving the key in PKCS#8 format https://en.wikipedia.org/wiki/PKCS_8

From some quick googling looks like this library may be your best bet for extracting this value. https://github.com/kjur/jsrsasign

Here is code in that library that decodes this:

https://github.com/kjur/jsrsasign/blob/d282c71cee92000c4807bcbf2212fedf3f22bd84/src/keyutil-1.0.js#L77

https://github.com/kjur/jsrsasign/blob/d282c71cee92000c4807bcbf2212fedf3f22bd84/src/keyutil-1.0.js#L557-L571

Here they are using in a unit test. Probably similar how you will need to call.

https://github.com/kjur/jsrsasign/blob/d282c71cee92000c4807bcbf2212fedf3f22bd84/test/qunit-do-crypto-sigini.html#L222

Ellery
  • 1,356
  • 1
  • 14
  • 22