2013-02-16 20:43:16 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "Crypto.h"
|
2013-02-15 15:38:15 -08:00
|
|
|
#include "jsfriendapi.h"
|
2013-02-27 12:31:19 -08:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIRandomGenerator.h"
|
2013-08-15 11:17:48 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
2013-09-23 14:30:40 -07:00
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
2013-02-27 12:31:19 -08:00
|
|
|
|
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2013-07-31 23:57:25 -07:00
|
|
|
#include "mozilla/dom/CryptoBinding.h"
|
2013-09-10 13:56:05 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-02-27 12:31:19 -08:00
|
|
|
|
|
|
|
using mozilla::dom::ContentChild;
|
2013-02-15 15:38:15 -08:00
|
|
|
|
2013-02-16 20:43:16 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-07-31 23:57:25 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Crypto)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2013-02-16 20:43:16 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMCrypto)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-07-31 23:57:25 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Crypto)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Crypto)
|
|
|
|
|
2014-05-15 03:20:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Crypto, mWindow, mSubtle)
|
2013-02-16 20:43:16 -08:00
|
|
|
|
|
|
|
Crypto::Crypto()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(Crypto);
|
2013-07-31 23:57:25 -07:00
|
|
|
SetIsDOMBinding();
|
2013-02-16 20:43:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Crypto::~Crypto()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(Crypto);
|
|
|
|
}
|
|
|
|
|
2013-07-31 23:57:25 -07:00
|
|
|
void
|
|
|
|
Crypto::Init(nsIDOMWindow* aWindow)
|
2013-02-15 15:38:15 -08:00
|
|
|
{
|
2013-07-31 23:57:25 -07:00
|
|
|
mWindow = do_QueryInterface(aWindow);
|
|
|
|
MOZ_ASSERT(mWindow);
|
|
|
|
}
|
2013-02-27 12:31:19 -08:00
|
|
|
|
2013-07-31 23:57:25 -07:00
|
|
|
/* virtual */ JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
Crypto::WrapObject(JSContext* aCx)
|
2013-07-31 23:57:25 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return CryptoBinding::Wrap(aCx, this);
|
2013-07-31 23:57:25 -07:00
|
|
|
}
|
2013-02-15 15:38:15 -08:00
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
void
|
2013-08-05 10:40:01 -07:00
|
|
|
Crypto::GetRandomValues(JSContext* aCx, const ArrayBufferView& aArray,
|
2014-06-11 13:26:52 -07:00
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
2013-08-05 10:40:01 -07:00
|
|
|
ErrorResult& aRv)
|
2013-07-31 23:57:25 -07:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Called on the wrong thread");
|
2013-02-15 15:38:15 -08:00
|
|
|
|
2013-07-31 23:57:25 -07:00
|
|
|
JS::Rooted<JSObject*> view(aCx, aArray.Obj());
|
2013-02-15 15:38:15 -08:00
|
|
|
|
|
|
|
// Throw if the wrong type of ArrayBufferView is passed in
|
|
|
|
// (Part of the Web Crypto API spec)
|
|
|
|
switch (JS_GetArrayBufferViewType(view)) {
|
2014-06-06 09:36:00 -07:00
|
|
|
case js::Scalar::Int8:
|
|
|
|
case js::Scalar::Uint8:
|
|
|
|
case js::Scalar::Uint8Clamped:
|
|
|
|
case js::Scalar::Int16:
|
|
|
|
case js::Scalar::Uint16:
|
|
|
|
case js::Scalar::Int32:
|
|
|
|
case js::Scalar::Uint32:
|
2013-02-15 15:38:15 -08:00
|
|
|
break;
|
|
|
|
default:
|
2013-07-31 23:57:25 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
|
2014-06-11 13:26:52 -07:00
|
|
|
return;
|
2013-02-15 15:38:15 -08:00
|
|
|
}
|
|
|
|
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-27 14:32:41 -07:00
|
|
|
aArray.ComputeLengthAndData();
|
2013-07-31 23:57:25 -07:00
|
|
|
uint32_t dataLen = aArray.Length();
|
2013-02-15 15:38:15 -08:00
|
|
|
if (dataLen == 0) {
|
|
|
|
NS_WARNING("ArrayBufferView length is 0, cannot continue");
|
2014-06-11 13:26:52 -07:00
|
|
|
aRetval.set(view);
|
|
|
|
return;
|
2013-02-15 15:38:15 -08:00
|
|
|
} else if (dataLen > 65536) {
|
2013-07-31 23:57:25 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_QUOTA_EXCEEDED_ERR);
|
2014-06-11 13:26:52 -07:00
|
|
|
return;
|
2013-02-15 15:38:15 -08:00
|
|
|
}
|
|
|
|
|
2013-07-31 23:57:25 -07:00
|
|
|
uint8_t* data = aArray.Data();
|
2013-02-15 15:38:15 -08:00
|
|
|
|
2013-02-27 12:31:19 -08:00
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
InfallibleTArray<uint8_t> randomValues;
|
|
|
|
// Tell the parent process to generate random values via PContent
|
|
|
|
ContentChild* cc = ContentChild::GetSingleton();
|
2013-09-17 09:54:34 -07:00
|
|
|
if (!cc->SendGetRandomValues(dataLen, &randomValues) ||
|
|
|
|
randomValues.Length() == 0) {
|
2013-07-31 23:57:25 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2014-06-11 13:26:52 -07:00
|
|
|
return;
|
2013-02-27 12:31:19 -08:00
|
|
|
}
|
|
|
|
NS_ASSERTION(dataLen == randomValues.Length(),
|
|
|
|
"Invalid length returned from parent process!");
|
|
|
|
memcpy(data, randomValues.Elements(), dataLen);
|
|
|
|
} else {
|
|
|
|
uint8_t *buf = GetRandomValues(dataLen);
|
|
|
|
|
|
|
|
if (!buf) {
|
2013-07-31 23:57:25 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2014-06-11 13:26:52 -07:00
|
|
|
return;
|
2013-02-27 12:31:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(data, buf, dataLen);
|
|
|
|
NS_Free(buf);
|
|
|
|
}
|
2013-02-15 15:38:15 -08:00
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
aRetval.set(view);
|
2013-02-15 15:38:15 -08:00
|
|
|
}
|
|
|
|
|
2014-05-15 03:20:00 -07:00
|
|
|
SubtleCrypto*
|
|
|
|
Crypto::Subtle()
|
|
|
|
{
|
|
|
|
if(!mSubtle) {
|
|
|
|
mSubtle = new SubtleCrypto(GetParentObject());
|
|
|
|
}
|
|
|
|
return mSubtle;
|
|
|
|
}
|
|
|
|
|
2013-07-31 23:57:25 -07:00
|
|
|
/* static */ uint8_t*
|
2013-02-27 12:31:19 -08:00
|
|
|
Crypto::GetRandomValues(uint32_t aLength)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRandomGenerator> randomGenerator;
|
|
|
|
nsresult rv;
|
2013-07-31 23:57:25 -07:00
|
|
|
randomGenerator = do_GetService("@mozilla.org/security/random-generator;1");
|
2013-02-27 12:31:19 -08:00
|
|
|
NS_ENSURE_TRUE(randomGenerator, nullptr);
|
|
|
|
|
|
|
|
uint8_t* buf;
|
|
|
|
rv = randomGenerator->GenerateRandomBytes(aLength, &buf);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2013-02-16 20:43:16 -08:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|