diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /dom/base/Crypto.cpp | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/base/Crypto.cpp')
-rw-r--r-- | dom/base/Crypto.cpp | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/dom/base/Crypto.cpp b/dom/base/Crypto.cpp new file mode 100644 index 0000000000..2b8bfbbe70 --- /dev/null +++ b/dom/base/Crypto.cpp @@ -0,0 +1,98 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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" +#include "js/ScalarType.h" +#include "js/experimental/TypedData.h" // JS_GetArrayBufferViewType +#include "nsCOMPtr.h" +#include "nsIRandomGenerator.h" + +#include "mozilla/dom/CryptoBinding.h" +#include "mozilla/dom/SubtleCrypto.h" +#include "nsServiceManagerUtils.h" + +namespace mozilla::dom { + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Crypto) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY + NS_INTERFACE_MAP_ENTRY(nsISupports) +NS_INTERFACE_MAP_END + +NS_IMPL_CYCLE_COLLECTING_ADDREF(Crypto) +NS_IMPL_CYCLE_COLLECTING_RELEASE(Crypto) + +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Crypto, mParent, mSubtle) + +Crypto::Crypto(nsIGlobalObject* aParent) : mParent(aParent) {} + +Crypto::~Crypto() = default; + +/* virtual */ +JSObject* Crypto::WrapObject(JSContext* aCx, + JS::Handle<JSObject*> aGivenProto) { + return Crypto_Binding::Wrap(aCx, this, aGivenProto); +} + +void Crypto::GetRandomValues(JSContext* aCx, const ArrayBufferView& aArray, + JS::MutableHandle<JSObject*> aRetval, + ErrorResult& aRv) { + JS::Rooted<JSObject*> view(aCx, aArray.Obj()); + + // Throw if the wrong type of ArrayBufferView is passed in + // (Part of the Web Crypto API spec) + switch (JS_GetArrayBufferViewType(view)) { + 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: + break; + default: + aRv.Throw(NS_ERROR_DOM_TYPE_MISMATCH_ERR); + return; + } + + aArray.ComputeState(); + uint32_t dataLen = aArray.Length(); + if (dataLen == 0) { + NS_WARNING("ArrayBufferView length is 0, cannot continue"); + aRetval.set(view); + return; + } else if (dataLen > 65536) { + aRv.Throw(NS_ERROR_DOM_QUOTA_EXCEEDED_ERR); + return; + } + + nsCOMPtr<nsIRandomGenerator> randomGenerator = + do_GetService("@mozilla.org/security/random-generator;1"); + if (!randomGenerator) { + aRv.Throw(NS_ERROR_DOM_OPERATION_ERR); + return; + } + + uint8_t* buf; + nsresult rv = randomGenerator->GenerateRandomBytes(dataLen, &buf); + if (NS_FAILED(rv) || !buf) { + aRv.Throw(NS_ERROR_DOM_OPERATION_ERR); + return; + } + + // Copy random bytes to ABV. + memcpy(aArray.Data(), buf, dataLen); + free(buf); + + aRetval.set(view); +} + +SubtleCrypto* Crypto::Subtle() { + if (!mSubtle) { + mSubtle = new SubtleCrypto(GetParentObject()); + } + return mSubtle; +} + +} // namespace mozilla::dom |