2013-02-17 08:43:16 +04: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-16 03:38:15 +04:00
|
|
|
#include "jsfriendapi.h"
|
2013-02-28 00:31:19 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIRandomGenerator.h"
|
2013-08-15 22:17:48 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2013-02-28 00:31:19 +04:00
|
|
|
|
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2013-08-01 10:57:25 +04:00
|
|
|
#include "mozilla/dom/CryptoBinding.h"
|
2013-02-28 00:31:19 +04:00
|
|
|
|
|
|
|
using mozilla::dom::ContentChild;
|
2013-02-16 03:38:15 +04:00
|
|
|
|
|
|
|
using namespace js::ArrayBufferView;
|
2013-02-17 08:43:16 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-08-01 10:57:25 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Crypto)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2013-02-17 08:43:16 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMCrypto)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-08-01 10:57:25 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Crypto)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Crypto)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(Crypto, mWindow)
|
2013-02-17 08:43:16 +04:00
|
|
|
|
|
|
|
Crypto::Crypto()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(Crypto);
|
2013-08-01 10:57:25 +04:00
|
|
|
SetIsDOMBinding();
|
2013-02-17 08:43:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Crypto::~Crypto()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(Crypto);
|
|
|
|
}
|
|
|
|
|
2013-08-01 10:57:25 +04:00
|
|
|
void
|
|
|
|
Crypto::Init(nsIDOMWindow* aWindow)
|
2013-02-16 03:38:15 +04:00
|
|
|
{
|
2013-08-01 10:57:25 +04:00
|
|
|
mWindow = do_QueryInterface(aWindow);
|
|
|
|
MOZ_ASSERT(mWindow);
|
|
|
|
}
|
2013-02-28 00:31:19 +04:00
|
|
|
|
2013-08-01 10:57:25 +04:00
|
|
|
/* virtual */ JSObject*
|
|
|
|
Crypto::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return CryptoBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
2013-02-16 03:38:15 +04:00
|
|
|
|
2013-08-01 10:57:25 +04:00
|
|
|
JSObject *
|
2013-08-05 21:40:01 +04:00
|
|
|
Crypto::GetRandomValues(JSContext* aCx, const ArrayBufferView& aArray,
|
|
|
|
ErrorResult& aRv)
|
2013-08-01 10:57:25 +04:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Called on the wrong thread");
|
2013-02-16 03:38:15 +04:00
|
|
|
|
2013-08-01 10:57:25 +04:00
|
|
|
JS::Rooted<JSObject*> view(aCx, aArray.Obj());
|
2013-02-16 03:38:15 +04:00
|
|
|
|
|
|
|
// Throw if the wrong type of ArrayBufferView is passed in
|
|
|
|
// (Part of the Web Crypto API spec)
|
|
|
|
switch (JS_GetArrayBufferViewType(view)) {
|
|
|
|
case TYPE_INT8:
|
|
|
|
case TYPE_UINT8:
|
|
|
|
case TYPE_UINT8_CLAMPED:
|
|
|
|
case TYPE_INT16:
|
|
|
|
case TYPE_UINT16:
|
|
|
|
case TYPE_INT32:
|
|
|
|
case TYPE_UINT32:
|
|
|
|
break;
|
|
|
|
default:
|
2013-08-01 10:57:25 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
|
|
|
|
return nullptr;
|
2013-02-16 03:38:15 +04:00
|
|
|
}
|
|
|
|
|
2013-08-01 10:57:25 +04:00
|
|
|
uint32_t dataLen = aArray.Length();
|
2013-02-16 03:38:15 +04:00
|
|
|
if (dataLen == 0) {
|
|
|
|
NS_WARNING("ArrayBufferView length is 0, cannot continue");
|
2013-08-01 10:57:25 +04:00
|
|
|
return view;
|
2013-02-16 03:38:15 +04:00
|
|
|
} else if (dataLen > 65536) {
|
2013-08-01 10:57:25 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_QUOTA_EXCEEDED_ERR);
|
|
|
|
return nullptr;
|
2013-02-16 03:38:15 +04:00
|
|
|
}
|
|
|
|
|
2013-08-01 10:57:25 +04:00
|
|
|
uint8_t* data = aArray.Data();
|
2013-02-16 03:38:15 +04:00
|
|
|
|
2013-02-28 00:31:19 +04:00
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
InfallibleTArray<uint8_t> randomValues;
|
|
|
|
// Tell the parent process to generate random values via PContent
|
|
|
|
ContentChild* cc = ContentChild::GetSingleton();
|
|
|
|
if (!cc->SendGetRandomValues(dataLen, &randomValues)) {
|
2013-08-01 10:57:25 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2013-02-28 00:31:19 +04: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-08-01 10:57:25 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2013-02-28 00:31:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(data, buf, dataLen);
|
|
|
|
NS_Free(buf);
|
|
|
|
}
|
2013-02-16 03:38:15 +04:00
|
|
|
|
2013-08-01 10:57:25 +04:00
|
|
|
return view;
|
2013-02-16 03:38:15 +04:00
|
|
|
}
|
|
|
|
|
2013-02-17 08:43:16 +04:00
|
|
|
#ifndef MOZ_DISABLE_CRYPTOLEGACY
|
|
|
|
// Stub out the legacy nsIDOMCrypto methods. The actual
|
|
|
|
// implementations are in security/manager/ssl/src/nsCrypto.{cpp,h}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Crypto::GetEnableSmartCardEvents(bool *aEnableSmartCardEvents)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Crypto::SetEnableSmartCardEvents(bool aEnableSmartCardEvents)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-08-01 10:57:25 +04:00
|
|
|
/* static */ uint8_t*
|
2013-02-28 00:31:19 +04:00
|
|
|
Crypto::GetRandomValues(uint32_t aLength)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRandomGenerator> randomGenerator;
|
|
|
|
nsresult rv;
|
2013-08-01 10:57:25 +04:00
|
|
|
randomGenerator = do_GetService("@mozilla.org/security/random-generator;1");
|
2013-02-28 00:31:19 +04:00
|
|
|
NS_ENSURE_TRUE(randomGenerator, nullptr);
|
|
|
|
|
|
|
|
uint8_t* buf;
|
|
|
|
rv = randomGenerator->GenerateRandomBytes(aLength, &buf);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2013-02-17 08:43:16 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|