node-jose explanation / example?

ElJackiste picture ElJackiste · Jan 5, 2018 · Viewed 9k times · Source

I want to use token based authentication with JWT (Signed and encrypted if possible). I use NodeJS in server side.

I am trying to use this node-jose module : https://github.com/cisco/node-jose

I don't know a lot about generate / store keys & cryptographic algorithms. Even if the documentation looks clear, i don't understand the part keys and key stores (until the signature part) : https://github.com/cisco/node-jose#keys-and-key-stores

  • How i need to generate and where i need to store keys in my server node app to then allow me to sign and verify my tokens ? I need to use symetric secret key or asymetric public private key pair ?

I suppose that it depends what i need but because of my level in crypto, i don't know what i need...

Then in the signature part, when i need to use the key, i don't understand well this part https://github.com/cisco/node-jose#keys-used-for-signing-and-verifying

  • How can i know which one use between OCT, EC, RSA etc ? And finally, what is the input Buffer they are talking about in the signature part ?

Could i have explanation / example about these questions ?

Thank you in advance.

Answer

Joseph picture Joseph · Oct 31, 2018

I used node-jose in a research proof, for a reflection of my c# code, I only created signed and Encrypted tokens for decryption and verification, on my server ( written in c#).

I need to use symetric secret key or asymetric public private key pair ?

I used RSA keys for Asymmetric signatures and key wrapping the Symmetric encryption details of the content. The Encryption algorithm for content encryption is a Symmetric one. The node-jose package generated the Symmetric key. The Key Wrap algorithm encrypted the Symmetric key.

The C# code I have decrypts and validates the token signature. Please note: I used the functions of the package to do all the work.

Here are my runkit notebooks for my workups:

for signing (JWS) https://runkit.com/archeon2/5bd66a8e7ee3b70012ec2e39

for encrypting (JWE) https://runkit.com/archeon2/5bd6736ff36b39001313262a

In my final, I combined the two, creating a signed token, then used the output as the payload for the encrypted one (JWS + JWE). I was successful using the c# server code in decrypting, and validating the created tokens.

JWS + JWE : https://runkit.com/archeon2/jws-jwe-integration

How i need to generate and where i need to store keys in my server node app to then allow me to sign and verify my tokens ?

var store = jose.JWK.createKeyStore();
await store.generate("RSA",2048,{alg:"RS256", key_ops:["sign", "decrypt", "unwrap"]});
lkey = (await store.get());
var key = lkey.toJSON(); //get public key to exchange
key.use = "sig";
key.key_ops=["encrypt","verify", "wrap"];

var pubKey = await jose.JWK.asKey(key);
key = null;

The Keystore can be serialized to JSON, so my concept would be to store this in Session Storage, or Local storage in a browser. Then retrieve the JSON representation and read in the Keystore.

var store= await jose.JWK.asKeyStore({"keys":[{"kty":"RSA","kid":"h9VHWShTfENF6xwjF3FR_b-9k1MvBvl3gnWnthV0Slk","alg":"RS256","key_ops":["sign","decrypt","unwrap"],"e":"AQAB","n":"l61fUp2hM3QxbFKk182yI5wTtiVS-g4ZxB4SXiY70sn23TalKT_01bgFElICexBXYVBwEndp6Gq60fCbaBeqTEyRvVbIlPlelCIhtYtL32iHvkkh2cXUgrQOscLGBm-8aWVtZE3HrtO-lu23qAoV7cGDU0UkX9z2QgQVmvT0JYxFsxHEYuWBOiWSGcBCgH10GWj40QBryhCPVtkqxBE3CCi9qjMFRaDqUg6kLqY8f0jtpY9ebgYWOmc1m_ujh7K6EDdsdn3D_QHfwtXtPi0ydEWu7pj1vq5AqacOd7AQzs4sWaTmMrpD9Ux43SVHbXK0UUkN5z3hcy6utysiBjqOwQ","d":"AVCHWvfyxbdkFkRBGX225Ygcw59fMLuejYyVLCu4qQMHGLO4irr7LD8EDDyZuOdTWoyP7BkM2e7S367uKeDKoQ6o1LND2cavgykokaI7bhxB0OxhVrnYNanJ1tCRVszxHRi78fqamHFNXZGB3fr4Za8frEEVJ5-KotfWOBmXZBvnoXbYbFXsKuaGo121AUCcEzFCGwuft75kPawzNjcdKhItfFrYh45OQLIO08W0fr_ByhxzWMU7yFUCELHSX5-4GT8ssq1dtvVgY2G14PbT67aYWJ2V571aSxM8DTwHrnB9tI8btbkXWt9JyVoQq13wDdo5fVN-c_5t07HBIaPoAQ","p":"8nLGa9_bRnke1w4paNCMjpdJ--eOUpZYbqEa8jnbsiaSWFwxZiOzUakIcpJ3iO0Bl28JEcdVbo7DE7mZ4M3BkOtm577cNuuK8243L7-k1a71X_ko2mQ3yF4rG2PzWAH_5P4wca1uk0Jj3PmhbkXDI6f_btm1X7Vw_U1K6jRhNbE","q":"oCe94B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HfwtXtPi0ydEWu7pj1vq5AqacOd7AQzs4sWaTmMrpD9Ux43SVHbXK0UUkN5z3hcy6utysiBjqOwQ","use":"sig"}]});

How can i know which one use between OCT, EC, RSA etc ?

For this, the need your token serves may dictate this. I needed the receiver to be the one who could see the contents, so I chose RSA, for Asymmetric keys. Forgery is a bit harder.

These notebooks are somewhat a work in progress. Please review with care, as this is my interpretation and how I worked out what I needed. My hope is that they give some guidance.