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
|
|
|
|
|
|
|
using namespace js::ArrayBufferView;
|
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
|
|
|
|
2013-07-31 23:57:25 -07:00
|
|
|
JSObject *
|
2013-08-05 10:40:01 -07:00
|
|
|
Crypto::GetRandomValues(JSContext* aCx, const ArrayBufferView& aArray,
|
|
|
|
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)) {
|
|
|
|
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-07-31 23:57:25 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
|
|
|
|
return nullptr;
|
2013-02-15 15:38:15 -08:00
|
|
|
}
|
|
|
|
|
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");
|
2013-07-31 23:57:25 -07:00
|
|
|
return view;
|
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);
|
|
|
|
return nullptr;
|
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);
|
|
|
|
return nullptr;
|
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);
|
|
|
|
return nullptr;
|
2013-02-27 12:31:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(data, buf, dataLen);
|
|
|
|
NS_Free(buf);
|
|
|
|
}
|
2013-02-15 15:38:15 -08:00
|
|
|
|
2013-07-31 23:57:25 -07:00
|
|
|
return 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-02-16 20:43:16 -08: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;
|
|
|
|
}
|
2013-09-17 09:54:34 -07:00
|
|
|
|
|
|
|
bool
|
|
|
|
Crypto::EnableSmartCardEvents()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Crypto::SetEnableSmartCardEvents(bool aEnable, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Crypto::GetVersion(nsString& aVersion)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-01-27 08:27:06 -08:00
|
|
|
mozilla::dom::CRMFObject*
|
2013-09-17 09:54:34 -07:00
|
|
|
Crypto::GenerateCRMFRequest(JSContext* aContext,
|
|
|
|
const nsCString& aReqDN,
|
|
|
|
const nsCString& aRegToken,
|
|
|
|
const nsCString& aAuthenticator,
|
|
|
|
const nsCString& aEaCert,
|
|
|
|
const nsCString& aJsCallback,
|
|
|
|
const Sequence<JS::Value>& aArgs,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Crypto::ImportUserCertificates(const nsAString& aNickname,
|
|
|
|
const nsAString& aCmmfResponse,
|
|
|
|
bool aDoForcedBackup,
|
|
|
|
nsAString& aReturn,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Crypto::SignText(JSContext* aContext,
|
|
|
|
const nsAString& aStringToSign,
|
|
|
|
const nsAString& aCaOption,
|
|
|
|
const Sequence<nsCString>& aArgs,
|
|
|
|
nsAString& aReturn)
|
|
|
|
|
|
|
|
{
|
|
|
|
aReturn.AssignLiteral("error:internalError");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Crypto::Logout(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
2013-02-16 20:43:16 -08:00
|
|
|
#endif
|
|
|
|
|
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
|