Bug 842818 - Enable structured cloning for CryptoKeys across threads r=baku,keeler

This commit is contained in:
Tim Taubert 2016-01-20 23:22:41 +01:00
parent 3d8c7dabb0
commit b89ffb551c
5 changed files with 118 additions and 6 deletions

View File

@ -395,10 +395,6 @@ StructuredCloneHolder::ReadFullySerializableObjects(JSContext* aCx,
}
if (aTag == SCTAG_DOM_WEBCRYPTO_KEY) {
if (!NS_IsMainThread()) {
return nullptr;
}
nsIGlobalObject *global = xpc::NativeGlobal(JS::CurrentGlobalOrNull(aCx));
if (!global) {
return nullptr;
@ -511,7 +507,6 @@ StructuredCloneHolder::WriteFullySerializableObjects(JSContext* aCx,
{
CryptoKey* key = nullptr;
if (NS_SUCCEEDED(UNWRAP_OBJECT(CryptoKey, aObj, key))) {
MOZ_ASSERT(NS_IsMainThread());
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_WEBCRYPTO_KEY, 0) &&
key->WriteStructuredClone(aWriter);
}

View File

@ -9,7 +9,6 @@
#include "nsNSSComponent.h"
#include "ScopedNSSTypes.h"
#include "mozilla/dom/CryptoKey.h"
#include "mozilla/dom/WebCryptoCommon.h"
#include "mozilla/dom/SubtleCryptoBinding.h"
#include "mozilla/dom/ToJSValue.h"

View File

@ -23,4 +23,5 @@ skip-if = toolkit == 'android' # bug 1200570
[test_WebCrypto_RSA_OAEP.html]
[test_WebCrypto_RSA_PSS.html]
[test_WebCrypto_Structured_Cloning.html]
[test_WebCrypto_Workers.html]
[test_WebCrypto_Wrap_Unwrap.html]

View File

@ -286,6 +286,7 @@ TestArray.addTest(
.get(dbkey);
req.onerror = error(that);
req.onsuccess = complete(that, function(e) {
db.close();
return hasKeyFields(e.target.result.val);
});
}

View File

@ -0,0 +1,116 @@
<!DOCTYPE html>
<html>
<head>
<title>WebCrypto Test Suite</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="./test_WebCrypto.css"/>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<!-- Utilities for manipulating ABVs -->
<script src="util.js"></script>
<!-- A simple wrapper around IndexedDB -->
<script src="simpledb.js"></script>
<!-- Test vectors drawn from the literature -->
<script src="./test-vectors.js"></script>
<!-- General testing framework -->
<script src="./test-array.js"></script>
<script>/*<![CDATA[*/
"use strict";
// -----------------------------------------------------------------------------
TestArray.addTest(
"Send a CryptoKey to a Worker and use it to encrypt data",
function () {
var worker = new Worker(`data:text/plain,
onmessage = ({data: {key, data, nonce}}) => {
var alg = { name: "AES-GCM", iv: nonce };
crypto.subtle.encrypt(alg, key, data).then(postMessage);
};
`);
var data = crypto.getRandomValues(new Uint8Array(128));
var nonce = crypto.getRandomValues(new Uint8Array(16));
var alg = { name: "AES-GCM", length: 128 };
var that = this;
// Generate a new AES key.
crypto.subtle.generateKey(alg, false, ["encrypt", "decrypt"]).then(key => {
// Wait for ciphertext, check and decrypt.
worker.addEventListener("message", ({data: ciphertext}) => {
var alg = { name: "AES-GCM", iv: nonce };
crypto.subtle.decrypt(alg, key, ciphertext)
.then(memcmp_complete(that, data), error(that));
});
// Send it to the worker.
worker.postMessage({key, data, nonce});
});
}
);
// -----------------------------------------------------------------------------
TestArray.addTest(
"Get a CryptoKey from a Worker and encrypt/decrypt data",
function () {
var worker = new Worker(`data:text/plain,
var alg = { name: "AES-GCM", length: 128 };
crypto.subtle.generateKey(alg, false, ["encrypt", "decrypt"])
.then(postMessage);
`);
var data = crypto.getRandomValues(new Uint8Array(128));
var nonce = crypto.getRandomValues(new Uint8Array(16));
var alg = { name: "AES-GCM", iv: nonce };
var that = this;
// Wait for the key from the worker.
worker.addEventListener("message", ({data: key}) => {
// Encrypt some data with the key.
crypto.subtle.encrypt(alg, key, data).then(ciphertext => {
// Verify and decrypt.
crypto.subtle.decrypt(alg, key, ciphertext)
.then(memcmp_complete(that, data), error(that));
});
});
}
);
/*]]>*/</script>
</head>
<body>
<div id="content">
<div id="head">
<b>Web</b>Crypto<br>
</div>
<div id="start" onclick="start();">RUN ALL</div>
<div id="resultDiv" class="content">
Summary:
<span class="pass"><span id="passN">0</span> passed, </span>
<span class="fail"><span id="failN">0</span> failed, </span>
<span class="pending"><span id="pendingN">0</span> pending.</span>
<br/>
<br/>
<table id="results">
<tr>
<th>Test</th>
<th>Result</th>
<th>Time</th>
</tr>
</table>
</div>
<div id="foot"></div>
</div>
</body>
</html>