Bug 1025824 - Fix mHwcLayerMap handling r=sushil
[gecko.git] / dom / base / Crypto.cpp
blob1967b29d184522d053b2e25bc13c85c70f74b28d
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 #include "Crypto.h"
5 #include "jsfriendapi.h"
6 #include "nsCOMPtr.h"
7 #include "nsIRandomGenerator.h"
8 #include "nsPIDOMWindow.h"
9 #include "MainThreadUtils.h"
10 #include "nsXULAppAPI.h"
12 #include "mozilla/dom/ContentChild.h"
13 #include "mozilla/dom/CryptoBinding.h"
14 #include "nsServiceManagerUtils.h"
16 using mozilla::dom::ContentChild;
18 using namespace js::ArrayBufferView;
20 namespace mozilla {
21 namespace dom {
23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Crypto)
24 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
25 NS_INTERFACE_MAP_ENTRY(nsISupports)
26 NS_INTERFACE_MAP_ENTRY(nsIDOMCrypto)
27 NS_INTERFACE_MAP_END
29 NS_IMPL_CYCLE_COLLECTING_ADDREF(Crypto)
30 NS_IMPL_CYCLE_COLLECTING_RELEASE(Crypto)
32 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Crypto, mWindow, mSubtle)
34 Crypto::Crypto()
36 MOZ_COUNT_CTOR(Crypto);
37 SetIsDOMBinding();
40 Crypto::~Crypto()
42 MOZ_COUNT_DTOR(Crypto);
45 void
46 Crypto::Init(nsIDOMWindow* aWindow)
48 mWindow = do_QueryInterface(aWindow);
49 MOZ_ASSERT(mWindow);
52 /* virtual */ JSObject*
53 Crypto::WrapObject(JSContext* aCx)
55 return CryptoBinding::Wrap(aCx, this);
58 void
59 Crypto::GetRandomValues(JSContext* aCx, const ArrayBufferView& aArray,
60 JS::MutableHandle<JSObject*> aRetval,
61 ErrorResult& aRv)
63 NS_ABORT_IF_FALSE(NS_IsMainThread(), "Called on the wrong thread");
65 JS::Rooted<JSObject*> view(aCx, aArray.Obj());
67 // Throw if the wrong type of ArrayBufferView is passed in
68 // (Part of the Web Crypto API spec)
69 switch (JS_GetArrayBufferViewType(view)) {
70 case TYPE_INT8:
71 case TYPE_UINT8:
72 case TYPE_UINT8_CLAMPED:
73 case TYPE_INT16:
74 case TYPE_UINT16:
75 case TYPE_INT32:
76 case TYPE_UINT32:
77 break;
78 default:
79 aRv.Throw(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
80 return;
83 aArray.ComputeLengthAndData();
84 uint32_t dataLen = aArray.Length();
85 if (dataLen == 0) {
86 NS_WARNING("ArrayBufferView length is 0, cannot continue");
87 aRetval.set(view);
88 return;
89 } else if (dataLen > 65536) {
90 aRv.Throw(NS_ERROR_DOM_QUOTA_EXCEEDED_ERR);
91 return;
94 uint8_t* data = aArray.Data();
96 if (XRE_GetProcessType() != GeckoProcessType_Default) {
97 InfallibleTArray<uint8_t> randomValues;
98 // Tell the parent process to generate random values via PContent
99 ContentChild* cc = ContentChild::GetSingleton();
100 if (!cc->SendGetRandomValues(dataLen, &randomValues) ||
101 randomValues.Length() == 0) {
102 aRv.Throw(NS_ERROR_FAILURE);
103 return;
105 NS_ASSERTION(dataLen == randomValues.Length(),
106 "Invalid length returned from parent process!");
107 memcpy(data, randomValues.Elements(), dataLen);
108 } else {
109 uint8_t *buf = GetRandomValues(dataLen);
111 if (!buf) {
112 aRv.Throw(NS_ERROR_FAILURE);
113 return;
116 memcpy(data, buf, dataLen);
117 NS_Free(buf);
120 aRetval.set(view);
123 SubtleCrypto*
124 Crypto::Subtle()
126 if(!mSubtle) {
127 mSubtle = new SubtleCrypto(GetParentObject());
129 return mSubtle;
132 #ifndef MOZ_DISABLE_CRYPTOLEGACY
133 // Stub out the legacy nsIDOMCrypto methods. The actual
134 // implementations are in security/manager/ssl/src/nsCrypto.{cpp,h}
136 NS_IMETHODIMP
137 Crypto::GetEnableSmartCardEvents(bool *aEnableSmartCardEvents)
139 return NS_ERROR_NOT_IMPLEMENTED;
142 NS_IMETHODIMP
143 Crypto::SetEnableSmartCardEvents(bool aEnableSmartCardEvents)
145 return NS_ERROR_NOT_IMPLEMENTED;
148 bool
149 Crypto::EnableSmartCardEvents()
151 return false;
154 void
155 Crypto::SetEnableSmartCardEvents(bool aEnable, ErrorResult& aRv)
157 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
160 void
161 Crypto::GetVersion(nsString& aVersion)
165 mozilla::dom::CRMFObject*
166 Crypto::GenerateCRMFRequest(JSContext* aContext,
167 const nsCString& aReqDN,
168 const nsCString& aRegToken,
169 const nsCString& aAuthenticator,
170 const nsCString& aEaCert,
171 const nsCString& aJsCallback,
172 const Sequence<JS::Value>& aArgs,
173 ErrorResult& aRv)
175 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
176 return nullptr;
179 void
180 Crypto::ImportUserCertificates(const nsAString& aNickname,
181 const nsAString& aCmmfResponse,
182 bool aDoForcedBackup,
183 nsAString& aReturn,
184 ErrorResult& aRv)
186 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
189 void
190 Crypto::SignText(JSContext* aContext,
191 const nsAString& aStringToSign,
192 const nsAString& aCaOption,
193 const Sequence<nsCString>& aArgs,
194 nsAString& aReturn)
197 aReturn.AssignLiteral("error:internalError");
200 void
201 Crypto::Logout(ErrorResult& aRv)
203 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
206 #endif
208 /* static */ uint8_t*
209 Crypto::GetRandomValues(uint32_t aLength)
211 nsCOMPtr<nsIRandomGenerator> randomGenerator;
212 nsresult rv;
213 randomGenerator = do_GetService("@mozilla.org/security/random-generator;1");
214 NS_ENSURE_TRUE(randomGenerator, nullptr);
216 uint8_t* buf;
217 rv = randomGenerator->GenerateRandomBytes(aLength, &buf);
219 NS_ENSURE_SUCCESS(rv, nullptr);
221 return buf;
224 } // namespace dom
225 } // namespace mozilla