2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-02-17 04:43:16 +00: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 23:38:15 +00:00
|
|
|
#include "jsfriendapi.h"
|
2013-02-27 20:31:19 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIRandomGenerator.h"
|
2013-09-23 21:30:40 +00:00
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
2013-02-27 20:31:19 +00:00
|
|
|
|
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2013-08-01 06:57:25 +00:00
|
|
|
#include "mozilla/dom/CryptoBinding.h"
|
2013-09-10 20:56:05 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-02-27 20:31:19 +00:00
|
|
|
|
|
|
|
using mozilla::dom::ContentChild;
|
2013-02-15 23:38:15 +00:00
|
|
|
|
2013-02-17 04:43:16 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-08-01 06:57:25 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Crypto)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2013-02-17 04:43:16 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMCrypto)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-08-01 06:57:25 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Crypto)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Crypto)
|
|
|
|
|
2015-01-09 18:55:44 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Crypto, mParent, mSubtle)
|
2013-02-17 04:43:16 +00:00
|
|
|
|
|
|
|
Crypto::Crypto()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(Crypto);
|
|
|
|
}
|
|
|
|
|
|
|
|
Crypto::~Crypto()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(Crypto);
|
|
|
|
}
|
|
|
|
|
2013-08-01 06:57:25 +00:00
|
|
|
void
|
2015-01-09 18:55:44 +00:00
|
|
|
Crypto::Init(nsIGlobalObject* aParent)
|
2013-02-15 23:38:15 +00:00
|
|
|
{
|
2015-01-09 18:55:44 +00:00
|
|
|
mParent = do_QueryInterface(aParent);
|
|
|
|
MOZ_ASSERT(mParent);
|
2013-08-01 06:57:25 +00:00
|
|
|
}
|
2013-02-27 20:31:19 +00:00
|
|
|
|
2013-08-01 06:57:25 +00:00
|
|
|
/* virtual */ JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 14:13:33 +00:00
|
|
|
Crypto::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-08-01 06:57:25 +00:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 14:13:33 +00:00
|
|
|
return CryptoBinding::Wrap(aCx, this, aGivenProto);
|
2013-08-01 06:57:25 +00:00
|
|
|
}
|
2013-02-15 23:38:15 +00:00
|
|
|
|
2014-06-11 20:26:52 +00:00
|
|
|
void
|
2013-08-05 17:40:01 +00:00
|
|
|
Crypto::GetRandomValues(JSContext* aCx, const ArrayBufferView& aArray,
|
2015-01-09 18:55:44 +00:00
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
|
|
|
ErrorResult& aRv)
|
2013-08-01 06:57:25 +00:00
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> view(aCx, aArray.Obj());
|
2013-02-15 23:38:15 +00:00
|
|
|
|
2015-11-25 18:04:50 +00:00
|
|
|
if (JS_IsTypedArrayObject(view) && JS_GetTypedArraySharedness(view)) {
|
|
|
|
// Throw if the object is mapping shared memory (must opt in).
|
|
|
|
aRv.ThrowTypeError<MSG_TYPEDARRAY_IS_SHARED>(NS_LITERAL_STRING("Argument of Crypto.getRandomValues"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-15 23:38:15 +00: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 16:36:00 +00: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 23:38:15 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-08-01 06:57:25 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
|
2014-06-11 20:26:52 +00:00
|
|
|
return;
|
2013-02-15 23:38:15 +00: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 21:32:41 +00:00
|
|
|
aArray.ComputeLengthAndData();
|
2013-08-01 06:57:25 +00:00
|
|
|
uint32_t dataLen = aArray.Length();
|
2013-02-15 23:38:15 +00:00
|
|
|
if (dataLen == 0) {
|
|
|
|
NS_WARNING("ArrayBufferView length is 0, cannot continue");
|
2014-06-11 20:26:52 +00:00
|
|
|
aRetval.set(view);
|
|
|
|
return;
|
2013-02-15 23:38:15 +00:00
|
|
|
} else if (dataLen > 65536) {
|
2013-08-01 06:57:25 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_QUOTA_EXCEEDED_ERR);
|
2014-06-11 20:26:52 +00:00
|
|
|
return;
|
2013-02-15 23:38:15 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 08:50:36 +00:00
|
|
|
nsCOMPtr<nsIRandomGenerator> randomGenerator =
|
|
|
|
do_GetService("@mozilla.org/security/random-generator;1");
|
|
|
|
if (!randomGenerator) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_OPERATION_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2013-02-27 20:31:19 +00:00
|
|
|
|
2015-09-22 08:50:36 +00:00
|
|
|
uint8_t* buf;
|
|
|
|
nsresult rv = randomGenerator->GenerateRandomBytes(dataLen, &buf);
|
|
|
|
if (NS_FAILED(rv) || !buf) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_OPERATION_ERR);
|
|
|
|
return;
|
2013-02-27 20:31:19 +00:00
|
|
|
}
|
2013-02-15 23:38:15 +00:00
|
|
|
|
2015-09-22 08:50:36 +00:00
|
|
|
// Copy random bytes to ABV.
|
|
|
|
memcpy(aArray.Data(), buf, dataLen);
|
|
|
|
free(buf);
|
|
|
|
|
2014-06-11 20:26:52 +00:00
|
|
|
aRetval.set(view);
|
2013-02-15 23:38:15 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 10:20:00 +00:00
|
|
|
SubtleCrypto*
|
|
|
|
Crypto::Subtle()
|
|
|
|
{
|
|
|
|
if(!mSubtle) {
|
|
|
|
mSubtle = new SubtleCrypto(GetParentObject());
|
|
|
|
}
|
|
|
|
return mSubtle;
|
|
|
|
}
|
|
|
|
|
2013-02-17 04:43:16 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|