PeculiarVentures / webcrypto-core

A input validation layer for WebCrypto polyfills.
MIT License
28 stars 13 forks source link

[Bug] 'CryptoKey has wrong key usage. Must be \'decrypt\'' When using unwrapKey() with RSA-OAEP and keyStorage #22

Closed NickLD closed 4 years ago

NickLD commented 5 years ago

I am using https://github.com/PeculiarVentures/node-webcrypto-ossl/ in my Node.js project. I have a bug that seems to trace back to this Pollyfill using in node-webcrypto-ossl , so I'm creating the issue here.

I have been facing an issue where when using unwrapKey() with RSA-OAEP, it fails when using RSA key being imported from keyStorage. When inspecting the keyStorage JSON file stored, it contains the correct usages ("usages":["unwrapKey"]) , but is still failing.

Exact error:

{ Error: CryptoKey has wrong key usage. Must be 'decrypt' at CryptoKeyError.WebCryptoError [as constructor] (/home/primary/BeamUpScotty_Server/Main_Server/node_modules/webcrypto-core/dist/webcrypto-core.js:38:21) at new CryptoKeyError (/home/primary/BeamUpScotty_Server/Main_Server/node_modules/webcrypto-core/dist/webcrypto-core.js:64:47) at Function.BaseCrypto.checkKey (/home/primary/BeamUpScotty_Server/Main_Server/node_modules/webcrypto-core/dist/webcrypto-core.js:140:23) at /home/primary/BeamUpScotty_Server/Main_Server/node_modules/webcrypto-core/dist/webcrypto-core.js:1378:19 at new Promise (<anonymous>) at Function.RsaOAEP.decrypt (/home/primary/BeamUpScotty_Server/Main_Server/node_modules/webcrypto-core/dist/webcrypto-core.js:1376:16) at /home/primary/BeamUpScotty_Server/Main_Server/node_modules/webcrypto-core/dist/webcrypto-core.js:1610:19 at new Promise (<anonymous>) at SubtleCrypto.decrypt (/home/primary/BeamUpScotty_Server/Main_Server/node_modules/webcrypto-core/dist/webcrypto-core.js:1578:16) at SubtleCrypto.decrypt (/home/primary/BeamUpScotty_Server/Main_Server/node_modules/node-webcrypto-ossl/buildjs/subtle.js:168:30) at Promise.resolve.then (/home/primary/BeamUpScotty_Server/Main_Server/node_modules/node-webcrypto-ossl/buildjs/subtle.js:231:33) at process._tickCallback (internal/process/next_tick.js:68:7) code: 3, message: 'CryptoKey has wrong key usage. Must be \'decrypt\'' }

I was able to temporarily remove this issue (and verify it's not an issue with my code), by going into the dist/webcrypto-core.js and modifying line 140 to comment out the throw error.

if (usage) { if (!key.usages.some(function (keyUsage) { return usage.toUpperCase() === keyUsage.toUpperCase(); })) { //throw new CryptoKeyError(CryptoKeyError.WRONG_KEY_USAGE, usage); } }

I should be able to post a sample code to reproduce this issue if necessary late tomorrow (today?), i'm just creating this issue now so I wont forget to later, as it's currently 3:00 AM.

microshine commented 5 years ago

This is node-webcrypto-ossl bug. Cause it calls encrypt/decrypt functions from wrapKey/unwrapKey functions

https://github.com/PeculiarVentures/node-webcrypto-ossl/blob/81e98004ba6115a5fa6410d32c870084fafeb5fe/lib/subtle.ts#L257-L261