From d77cb499b1a1c80cc631092a4cdf6f423b9c1269 Mon Sep 17 00:00:00 2001 From: Carter Sellgren Date: Wed, 12 Oct 2022 17:38:37 +0000 Subject: [PATCH] Bug 1285986 - [Web Crypto] Changed error when AES-CBC, AES-CTR has wrong iv length r=keeler Differential Revision: https://phabricator.services.mozilla.com/D158540 --- dom/crypto/WebCryptoTask.cpp | 4 +- dom/crypto/test/test_WebCrypto.html | 105 ++++++++++++++++++++-------- 2 files changed, 77 insertions(+), 32 deletions(-) diff --git a/dom/crypto/WebCryptoTask.cpp b/dom/crypto/WebCryptoTask.cpp index 8a4bbc452e3b..c5c7fdb81f00 100644 --- a/dom/crypto/WebCryptoTask.cpp +++ b/dom/crypto/WebCryptoTask.cpp @@ -506,7 +506,7 @@ class AesTask : public ReturnArrayBufferViewTask, public DeferredData { ATTEMPT_BUFFER_INIT(mIv, params.mIv) if (mIv.Length() != 16) { - mEarlyRv = NS_ERROR_DOM_DATA_ERR; + mEarlyRv = NS_ERROR_DOM_OPERATION_ERR; return; } } else if (algName.EqualsLiteral(WEBCRYPTO_ALG_AES_CTR)) { @@ -523,7 +523,7 @@ class AesTask : public ReturnArrayBufferViewTask, public DeferredData { ATTEMPT_BUFFER_INIT(mIv, params.mCounter) if (mIv.Length() != 16) { - mEarlyRv = NS_ERROR_DOM_DATA_ERR; + mEarlyRv = NS_ERROR_DOM_OPERATION_ERR; return; } diff --git a/dom/crypto/test/test_WebCrypto.html b/dom/crypto/test/test_WebCrypto.html index cee0b71505e1..bc4bbfee6efb 100644 --- a/dom/crypto/test/test_WebCrypto.html +++ b/dom/crypto/test/test_WebCrypto.html @@ -517,17 +517,27 @@ TestArray.addTest( x, tv.aes_cbc_enc.data); } - function doEncrypt(x) { - return encrypt(x, new Uint8Array(15)) - .catch(function() { return encrypt(new Uint8Array(17)); }); + function checkPromises(promises) { + for (var promise of promises) { + if (promise.status != "rejected") { + return false; + } + if (promise.reason.name != "OperationError") { + return false; + } + } + + return true; } crypto.subtle.importKey("raw", tv.aes_cbc_enc.key, "AES-CBC", false, ["encrypt"]) - .then(doEncrypt) - .then( - error(that), - complete(that) - ); + .then(function(key) { + var p1 = encrypt(key, new Uint8Array(15)); + var p2 = encrypt(key, new Uint8Array(17)); + + Promise.allSettled([p1, p2]) + .then(complete(that, checkPromises)); + }); } ); @@ -564,17 +574,27 @@ TestArray.addTest( x, tv.aes_cbc_dec.data); } - function doDecrypt(x) { - return decrypt(x, new Uint8Array(15)) - .catch(function() { return decrypt(x, new Uint8Array(17)); }); + function checkPromises(promises) { + for (var promise of promises) { + if (promise.status != "rejected") { + return false; + } + if (promise.reason.name != "OperationError") { + return false; + } + } + + return true; } crypto.subtle.importKey("raw", tv.aes_cbc_dec.key, "AES-CBC", false, ["decrypt"]) - .then(doDecrypt) - .then( - error(that), - complete(that) - ); + .then(function(key) { + var p1 = decrypt(key, new Uint8Array(15)); + var p2 = decrypt(key, new Uint8Array(17)); + + Promise.allSettled([p1, p2]) + .then(complete(that, checkPromises)); + }); } ); @@ -611,17 +631,27 @@ TestArray.addTest( x, tv.aes_ctr_enc.data); } - function doEncrypt(x) { - return encrypt(x, new Uint8Array(15)) - .catch(function() { return encrypt(x, new Uint8Array(17)); }); + function checkPromises(promises) { + for (var promise of promises) { + if (promise.status != "rejected") { + return false; + } + if (promise.reason.name != "OperationError") { + return false; + } + } + + return true; } crypto.subtle.importKey("raw", tv.aes_ctr_enc.key, "AES-CTR", false, ["encrypt"]) - .then(doEncrypt) - .then( - error(that), - complete(that) - ); + .then(function(key) { + var p1 = encrypt(key, new Uint8Array(15)); + var p2 = encrypt(key, new Uint8Array(17)); + + Promise.allSettled([p1, p2]) + .then(complete(that, checkPromises)); + }); } ); @@ -652,18 +682,33 @@ TestArray.addTest( function() { var that = this; - function doDecrypt(x, iv) { + function decrypt(x, iv) { return crypto.subtle.decrypt( { name: "AES-CTR", counter: iv, length: 32 }, x, tv.aes_ctr_dec.data); } + function checkPromises(promises) { + for (var promise of promises) { + if (promise.status != "rejected") { + return false; + } + if (promise.reason.name != "OperationError") { + return false; + } + } + + return true; + } + crypto.subtle.importKey("raw", tv.aes_ctr_dec.key, "AES-CTR", false, ["decrypt"]) - .then(doDecrypt) - .then( - error(that), - complete(that) - ); + .then(function(key) { + var p1 = decrypt(key, new Uint8Array(15)); + var p2 = decrypt(key, new Uint8Array(17)); + + Promise.allSettled([p1, p2]) + .then(complete(that, checkPromises)); + }); } );