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-08-15 18:17:48 +00:00
|
|
|
#include "nsPIDOMWindow.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)
|
|
|
|
|
2014-05-15 10:20:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Crypto, mWindow, mSubtle)
|
2013-02-17 04:43:16 +00:00
|
|
|
|
|
|
|
Crypto::Crypto()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(Crypto);
|
2013-08-01 06:57:25 +00:00
|
|
|
SetIsDOMBinding();
|
2013-02-17 04:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Crypto::~Crypto()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(Crypto);
|
|
|
|
}
|
|
|
|
|
2013-08-01 06:57:25 +00:00
|
|
|
void
|
|
|
|
Crypto::Init(nsIDOMWindow* aWindow)
|
2013-02-15 23:38:15 +00:00
|
|
|
{
|
2013-08-01 06:57:25 +00:00
|
|
|
mWindow = do_QueryInterface(aWindow);
|
|
|
|
MOZ_ASSERT(mWindow);
|
|
|
|
}
|
2013-02-27 20:31:19 +00:00
|
|
|
|
2013-08-01 06:57:25 +00:00
|
|
|
/* virtual */ JSObject*
|
2014-04-08 22:27:18 +00:00
|
|
|
Crypto::WrapObject(JSContext* aCx)
|
2013-08-01 06:57:25 +00: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 22:27:17 +00:00
|
|
|
return CryptoBinding::Wrap(aCx, this);
|
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,
|
2014-06-11 20:26:52 +00:00
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
2013-08-05 17:40:01 +00:00
|
|
|
ErrorResult& aRv)
|
2013-08-01 06:57:25 +00:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Called on the wrong thread");
|
2013-02-15 23:38:15 +00:00
|
|
|
|
2013-08-01 06:57:25 +00:00
|
|
|
JS::Rooted<JSObject*> view(aCx, aArray.Obj());
|
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
|
|
|
}
|
|
|
|
|
2013-08-01 06:57:25 +00:00
|
|
|
uint8_t* data = aArray.Data();
|
2013-02-15 23:38:15 +00:00
|
|
|
|
2013-02-27 20:31:19 +00: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 16:54:34 +00:00
|
|
|
if (!cc->SendGetRandomValues(dataLen, &randomValues) ||
|
|
|
|
randomValues.Length() == 0) {
|
2013-08-01 06:57:25 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2014-06-11 20:26:52 +00:00
|
|
|
return;
|
2013-02-27 20:31:19 +00: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 06:57:25 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2014-06-11 20:26:52 +00:00
|
|
|
return;
|
2013-02-27 20:31:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(data, buf, dataLen);
|
|
|
|
NS_Free(buf);
|
|
|
|
}
|
2013-02-15 23:38:15 +00:00
|
|
|
|
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
|
|
|
#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 16:54:34 +00: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 16:27:06 +00:00
|
|
|
mozilla::dom::CRMFObject*
|
2013-09-17 16:54:34 +00: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-17 04:43:16 +00:00
|
|
|
#endif
|
|
|
|
|
2013-08-01 06:57:25 +00:00
|
|
|
/* static */ uint8_t*
|
2013-02-27 20:31:19 +00:00
|
|
|
Crypto::GetRandomValues(uint32_t aLength)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRandomGenerator> randomGenerator;
|
|
|
|
nsresult rv;
|
2013-08-01 06:57:25 +00:00
|
|
|
randomGenerator = do_GetService("@mozilla.org/security/random-generator;1");
|
2013-02-27 20:31:19 +00:00
|
|
|
NS_ENSURE_TRUE(randomGenerator, nullptr);
|
|
|
|
|
|
|
|
uint8_t* buf;
|
|
|
|
rv = randomGenerator->GenerateRandomBytes(aLength, &buf);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2013-02-17 04:43:16 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|