javascriptgoogle-chromecryptographywebcrypto-apiwebcrypto

The JWK "key_ops" member was inconsistent with that specified by the Web Crypto call


On NodeJS, I generate key and export it as follows,

const { Crypto } = require("@peculiar/webcrypto");
const crypto = new Crypto();
....
....
....

    let KeyPair = await crypto.subtle.generateKey(
        {
          name: "ECDH",
          namedCurve: "P-384"
        },
        false,
        ["deriveKey"]
    );


    let exportPubKey = await crypto.subtle.exportKey(
        "jwk",
        KeyPair.publicKey
    )

Now, When I try to import the exported key on client which use Chrome Browser, it throws a DOM exception, I import it like this,

window.crypto.subtle.importKey("jwk", {"kty":"EC","crv":"P-384","key_ops":[],"ext":true,"x":"mbLH4QWKwgYu3cwaUsk59M9awwhJydGdJOH0z8WapKuW6DAlnI8bcUg7kOl9ZCdb","y":"-PQ_HUtA45oC8YL3Zk2dExIWxykhimjfqFAc2pQrPTmrDKa-5AVn4Noz3PitZw2W"}, {"name": "ECDH", "namedCurve": "P-384"}, false, ["deriveKey"])

The error is,

DOMException: The JWK "key_ops" member was inconsistent with that specified by the Web Crypto call. The JWK usage must be a superset of those requested

What's surprising me that I could import same thing on Node without any errors. I mean the client generate JWK in prety much same format.


Solution

  • After bit fuzzing, I figured out that you have to pass an empty array to overcome the error.

    window.crypto.subtle.importKey("jwk", {"kty":"EC","crv":"P-384","key_ops":[],"ext":true,"x":"mbLH4QWKwgYu3cwaUsk59M9awwhJydGdJOH0z8WapKuW6DAlnI8bcUg7kOl9ZCdb","y":"-PQ_HUtA45oC8YL3Zk2dExIWxykhimjfqFAc2pQrPTmrDKa-5AVn4Noz3PitZw2W"}, {"name": "ECDH", "namedCurve": "P-384"}, false, ["deriveKey"])
    

    Becomes,

    window.crypto.subtle.importKey("jwk", {"kty":"EC","crv":"P-384","key_ops":[],"ext":true,"x":"mbLH4QWKwgYu3cwaUsk59M9awwhJydGdJOH0z8WapKuW6DAlnI8bcUg7kOl9ZCdb","y":"-PQ_HUtA45oC8YL3Zk2dExIWxykhimjfqFAc2pQrPTmrDKa-5AVn4Noz3PitZw2W"}, {"name": "ECDH", "namedCurve": "P-384"}, false, [])