/src/mozilla-central/dom/base/Crypto.cpp
Line | Count | Source (jump to first uncovered line) |
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 "jsfriendapi.h" |
8 | | #include "nsCOMPtr.h" |
9 | | #include "nsIRandomGenerator.h" |
10 | | #include "MainThreadUtils.h" |
11 | | #include "nsXULAppAPI.h" |
12 | | |
13 | | #include "mozilla/dom/ContentChild.h" |
14 | | #include "mozilla/dom/CryptoBinding.h" |
15 | | #include "nsServiceManagerUtils.h" |
16 | | |
17 | | using mozilla::dom::ContentChild; |
18 | | |
19 | | namespace mozilla { |
20 | | namespace dom { |
21 | | |
22 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Crypto) |
23 | 0 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
24 | 0 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
25 | 0 | NS_INTERFACE_MAP_END |
26 | | |
27 | | NS_IMPL_CYCLE_COLLECTING_ADDREF(Crypto) |
28 | | NS_IMPL_CYCLE_COLLECTING_RELEASE(Crypto) |
29 | | |
30 | | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Crypto, mParent, mSubtle) |
31 | | |
32 | | Crypto::Crypto(nsIGlobalObject* aParent) |
33 | | : mParent(aParent) |
34 | 0 | { |
35 | 0 | } |
36 | | |
37 | | Crypto::~Crypto() |
38 | 0 | { |
39 | 0 | } |
40 | | |
41 | | /* virtual */ JSObject* |
42 | | Crypto::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
43 | 0 | { |
44 | 0 | return Crypto_Binding::Wrap(aCx, this, aGivenProto); |
45 | 0 | } |
46 | | |
47 | | void |
48 | | Crypto::GetRandomValues(JSContext* aCx, const ArrayBufferView& aArray, |
49 | | JS::MutableHandle<JSObject*> aRetval, |
50 | | ErrorResult& aRv) |
51 | 0 | { |
52 | 0 | JS::Rooted<JSObject*> view(aCx, aArray.Obj()); |
53 | 0 |
|
54 | 0 | if (JS_IsTypedArrayObject(view) && JS_GetTypedArraySharedness(view)) { |
55 | 0 | // Throw if the object is mapping shared memory (must opt in). |
56 | 0 | aRv.ThrowTypeError<MSG_TYPEDARRAY_IS_SHARED>(NS_LITERAL_STRING("Argument of Crypto.getRandomValues")); |
57 | 0 | return; |
58 | 0 | } |
59 | 0 |
|
60 | 0 | // Throw if the wrong type of ArrayBufferView is passed in |
61 | 0 | // (Part of the Web Crypto API spec) |
62 | 0 | switch (JS_GetArrayBufferViewType(view)) { |
63 | 0 | case js::Scalar::Int8: |
64 | 0 | case js::Scalar::Uint8: |
65 | 0 | case js::Scalar::Uint8Clamped: |
66 | 0 | case js::Scalar::Int16: |
67 | 0 | case js::Scalar::Uint16: |
68 | 0 | case js::Scalar::Int32: |
69 | 0 | case js::Scalar::Uint32: |
70 | 0 | break; |
71 | 0 | default: |
72 | 0 | aRv.Throw(NS_ERROR_DOM_TYPE_MISMATCH_ERR); |
73 | 0 | return; |
74 | 0 | } |
75 | 0 | |
76 | 0 | aArray.ComputeLengthAndData(); |
77 | 0 | uint32_t dataLen = aArray.Length(); |
78 | 0 | if (dataLen == 0) { |
79 | 0 | NS_WARNING("ArrayBufferView length is 0, cannot continue"); |
80 | 0 | aRetval.set(view); |
81 | 0 | return; |
82 | 0 | } else if (dataLen > 65536) { |
83 | 0 | aRv.Throw(NS_ERROR_DOM_QUOTA_EXCEEDED_ERR); |
84 | 0 | return; |
85 | 0 | } |
86 | 0 | |
87 | 0 | nsCOMPtr<nsIRandomGenerator> randomGenerator = |
88 | 0 | do_GetService("@mozilla.org/security/random-generator;1"); |
89 | 0 | if (!randomGenerator) { |
90 | 0 | aRv.Throw(NS_ERROR_DOM_OPERATION_ERR); |
91 | 0 | return; |
92 | 0 | } |
93 | 0 | |
94 | 0 | uint8_t* buf; |
95 | 0 | nsresult rv = randomGenerator->GenerateRandomBytes(dataLen, &buf); |
96 | 0 | if (NS_FAILED(rv) || !buf) { |
97 | 0 | aRv.Throw(NS_ERROR_DOM_OPERATION_ERR); |
98 | 0 | return; |
99 | 0 | } |
100 | 0 | |
101 | 0 | // Copy random bytes to ABV. |
102 | 0 | memcpy(aArray.Data(), buf, dataLen); |
103 | 0 | free(buf); |
104 | 0 |
|
105 | 0 | aRetval.set(view); |
106 | 0 | } |
107 | | |
108 | | SubtleCrypto* |
109 | | Crypto::Subtle() |
110 | 0 | { |
111 | 0 | if(!mSubtle) { |
112 | 0 | mSubtle = new SubtleCrypto(GetParentObject()); |
113 | 0 | } |
114 | 0 | return mSubtle; |
115 | 0 | } |
116 | | |
117 | | } // namespace dom |
118 | | } // namespace mozilla |