1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "Crypto.h"
7 #include "js/ScalarType.h"
8 #include "js/experimental/TypedData.h" // JS_GetArrayBufferViewType
9 #include "nsCOMPtr.h"
10 #include "nsIRandomGenerator.h"
11
12 #include "mozilla/dom/CryptoBinding.h"
13 #include "mozilla/dom/SubtleCrypto.h"
14 #include "nsServiceManagerUtils.h"
15
16 namespace mozilla::dom {
17
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Crypto)18 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Crypto)
19 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
20 NS_INTERFACE_MAP_ENTRY(nsISupports)
21 NS_INTERFACE_MAP_END
22
23 NS_IMPL_CYCLE_COLLECTING_ADDREF(Crypto)
24 NS_IMPL_CYCLE_COLLECTING_RELEASE(Crypto)
25
26 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Crypto, mParent, mSubtle)
27
28 Crypto::Crypto(nsIGlobalObject* aParent) : mParent(aParent) {}
29
30 Crypto::~Crypto() = default;
31
32 /* virtual */
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)33 JSObject* Crypto::WrapObject(JSContext* aCx,
34 JS::Handle<JSObject*> aGivenProto) {
35 return Crypto_Binding::Wrap(aCx, this, aGivenProto);
36 }
37
GetRandomValues(JSContext * aCx,const ArrayBufferView & aArray,JS::MutableHandle<JSObject * > aRetval,ErrorResult & aRv)38 void Crypto::GetRandomValues(JSContext* aCx, const ArrayBufferView& aArray,
39 JS::MutableHandle<JSObject*> aRetval,
40 ErrorResult& aRv) {
41 JS::Rooted<JSObject*> view(aCx, aArray.Obj());
42
43 // Throw if the wrong type of ArrayBufferView is passed in
44 // (Part of the Web Crypto API spec)
45 switch (JS_GetArrayBufferViewType(view)) {
46 case js::Scalar::Int8:
47 case js::Scalar::Uint8:
48 case js::Scalar::Uint8Clamped:
49 case js::Scalar::Int16:
50 case js::Scalar::Uint16:
51 case js::Scalar::Int32:
52 case js::Scalar::Uint32:
53 case js::Scalar::BigInt64:
54 case js::Scalar::BigUint64:
55 break;
56 default:
57 aRv.Throw(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
58 return;
59 }
60
61 aArray.ComputeState();
62 uint32_t dataLen = aArray.Length();
63 if (dataLen == 0) {
64 NS_WARNING("ArrayBufferView length is 0, cannot continue");
65 aRetval.set(view);
66 return;
67 } else if (dataLen > 65536) {
68 aRv.Throw(NS_ERROR_DOM_QUOTA_EXCEEDED_ERR);
69 return;
70 }
71
72 nsCOMPtr<nsIRandomGenerator> randomGenerator =
73 do_GetService("@mozilla.org/security/random-generator;1");
74 if (!randomGenerator) {
75 aRv.Throw(NS_ERROR_DOM_OPERATION_ERR);
76 return;
77 }
78
79 uint8_t* buf;
80 nsresult rv = randomGenerator->GenerateRandomBytes(dataLen, &buf);
81 if (NS_FAILED(rv) || !buf) {
82 aRv.Throw(NS_ERROR_DOM_OPERATION_ERR);
83 return;
84 }
85
86 // Copy random bytes to ABV.
87 memcpy(aArray.Data(), buf, dataLen);
88 free(buf);
89
90 aRetval.set(view);
91 }
92
Subtle()93 SubtleCrypto* Crypto::Subtle() {
94 if (!mSubtle) {
95 mSubtle = new SubtleCrypto(GetParentObject());
96 }
97 return mSubtle;
98 }
99
100 } // namespace mozilla::dom
101