6

I have the following JavaScript code to implement public key encryption using the Web Cryptography API. It works for Firefox and Chrome but fails for Microsoft Edge. The error I am getting from Edge is "Could not complete the operation due to error 80700011." What have I missed?

<script>
    var data = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);

    var crypto = window.crypto || window.msCrypto;
    var cryptoSubtle = crypto.subtle;

    cryptoSubtle.generateKey(
        {
            name: "RSA-OAEP",
            modulusLength: 2048, 
            publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
            hash: { name: "SHA-256" }, 
        },
        true, 
        ["encrypt", "decrypt"]
    ).then(function (key) { 
        console.log(key);
        console.log(key.publicKey);
        return cryptoSubtle.encrypt(
            {
                name: "RSA-OAEP"
            },
            key.publicKey,
            data
            );
    }).then(function (encrypted) { 
        console.log(new Uint8Array(encrypted));
    }).catch(function (err) {
        console.error(err);
    });
</script>
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
FengHuang
  • 241
  • 1
  • 8
  • 1
    "W3CException_DOM_TYPE_MISMATCH_ERR: The node type is incompatible with the expected parameter type." Not very helpful perhaps, but that's all I could find. Perhaps it suggests the problem is elsewhere in your code. – President James K. Polk Oct 09 '15 at 17:03
  • Thanks @JamesKPolk. There is nothing else. It is an empty page with the above code. Also it works fine with Firefox and Chrome. – FengHuang Oct 09 '15 at 21:33

2 Answers2

10

I've found the cause of this issue. I have to add the hash field when invoking the encrypt function:

        return cryptoSubtle.encrypt(
            {
                name: "RSA-OAEP",
                hash: { name: "SHA-256" }
            },
            key.publicKey,
            data
            );

This does not match the Web Cryptography API Spec but it works.

FengHuang
  • 241
  • 1
  • 8
  • 2
    If that Edge behavior here is not per-spec, please consider filing a bug against Edge, and posting a link to that bug here if possible. – sideshowbarker Oct 17 '15 at 00:07
2

Same problem with crypto.subtle.sign. Needed to add the hashing algorithm (same issue in Safari)

Replace

crypto.subtle.sign(
            {
                 name: "RSASSA-PKCS1-v1_5"
            },
            cryptoKey,
            digestToSignBuf);

with

crypto.subtle.sign(
            {
                 name: "RSASSA-PKCS1-v1_5", 
                 hash: "SHA-256"
            },
            cryptoKey,
            digestToSignBuf);
pedrofb
  • 37,271
  • 5
  • 94
  • 142