Bumping manifests a=b2g-bump
[gecko.git] / dom / base / Crypto.cpp
blobddff2aea59b8286fabbab7fa23195486d4fe302d
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 namespace mozilla {
19 namespace dom {
21 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Crypto)
22 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
23 NS_INTERFACE_MAP_ENTRY(nsISupports)
24 NS_INTERFACE_MAP_ENTRY(nsIDOMCrypto)
25 NS_INTERFACE_MAP_END
27 NS_IMPL_CYCLE_COLLECTING_ADDREF(Crypto)
28 NS_IMPL_CYCLE_COLLECTING_RELEASE(Crypto)
30 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Crypto, mWindow, mSubtle)
32 Crypto::Crypto()
34 MOZ_COUNT_CTOR(Crypto);
35 SetIsDOMBinding();
38 Crypto::~Crypto()
40 MOZ_COUNT_DTOR(Crypto);
43 void
44 Crypto::Init(nsIDOMWindow* aWindow)
46 mWindow = do_QueryInterface(aWindow);
47 MOZ_ASSERT(mWindow);
50 /* virtual */ JSObject*
51 Crypto::WrapObject(JSContext* aCx)
53 return CryptoBinding::Wrap(aCx, this);
56 void
57 Crypto::GetRandomValues(JSContext* aCx, const ArrayBufferView& aArray,
58 JS::MutableHandle<JSObject*> aRetval,
59 ErrorResult& aRv)
61 NS_ABORT_IF_FALSE(NS_IsMainThread(), "Called on the wrong thread");
63 JS::Rooted<JSObject*> view(aCx, aArray.Obj());
65 // Throw if the wrong type of ArrayBufferView is passed in
66 // (Part of the Web Crypto API spec)
67 switch (JS_GetArrayBufferViewType(view)) {
68 case js::Scalar::Int8:
69 case js::Scalar::Uint8:
70 case js::Scalar::Uint8Clamped:
71 case js::Scalar::Int16:
72 case js::Scalar::Uint16:
73 case js::Scalar::Int32:
74 case js::Scalar::Uint32:
75 break;
76 default:
77 aRv.Throw(NS_ERROR_DOM_TYPE_MISMATCH_ERR);
78 return;
81 aArray.ComputeLengthAndData();
82 uint32_t dataLen = aArray.Length();
83 if (dataLen == 0) {
84 NS_WARNING("ArrayBufferView length is 0, cannot continue");
85 aRetval.set(view);
86 return;
87 } else if (dataLen > 65536) {
88 aRv.Throw(NS_ERROR_DOM_QUOTA_EXCEEDED_ERR);
89 return;
92 uint8_t* data = aArray.Data();
94 if (XRE_GetProcessType() != GeckoProcessType_Default) {
95 InfallibleTArray<uint8_t> randomValues;
96 // Tell the parent process to generate random values via PContent
97 ContentChild* cc = ContentChild::GetSingleton();
98 if (!cc->SendGetRandomValues(dataLen, &randomValues) ||
99 randomValues.Length() == 0) {
100 aRv.Throw(NS_ERROR_FAILURE);
101 return;
103 NS_ASSERTION(dataLen == randomValues.Length(),
104 "Invalid length returned from parent process!");
105 memcpy(data, randomValues.Elements(), dataLen);
106 } else {
107 uint8_t *buf = GetRandomValues(dataLen);
109 if (!buf) {
110 aRv.Throw(NS_ERROR_FAILURE);
111 return;
114 memcpy(data, buf, dataLen);
115 NS_Free(buf);
118 aRetval.set(view);
121 SubtleCrypto*
122 Crypto::Subtle()
124 if(!mSubtle) {
125 mSubtle = new SubtleCrypto(GetParentObject());
127 return mSubtle;
130 #ifndef MOZ_DISABLE_CRYPTOLEGACY
131 // Stub out the legacy nsIDOMCrypto methods. The actual
132 // implementations are in security/manager/ssl/src/nsCrypto.{cpp,h}
134 NS_IMETHODIMP
135 Crypto::GetEnableSmartCardEvents(bool *aEnableSmartCardEvents)
137 return NS_ERROR_NOT_IMPLEMENTED;
140 NS_IMETHODIMP
141 Crypto::SetEnableSmartCardEvents(bool aEnableSmartCardEvents)
143 return NS_ERROR_NOT_IMPLEMENTED;
146 bool
147 Crypto::EnableSmartCardEvents()
149 return false;
152 void
153 Crypto::SetEnableSmartCardEvents(bool aEnable, ErrorResult& aRv)
155 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
158 void
159 Crypto::GetVersion(nsString& aVersion)
163 mozilla::dom::CRMFObject*
164 Crypto::GenerateCRMFRequest(JSContext* aContext,
165 const nsCString& aReqDN,
166 const nsCString& aRegToken,
167 const nsCString& aAuthenticator,
168 const nsCString& aEaCert,
169 const nsCString& aJsCallback,
170 const Sequence<JS::Value>& aArgs,
171 ErrorResult& aRv)
173 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
174 return nullptr;
177 void
178 Crypto::ImportUserCertificates(const nsAString& aNickname,
179 const nsAString& aCmmfResponse,
180 bool aDoForcedBackup,
181 nsAString& aReturn,
182 ErrorResult& aRv)
184 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
187 void
188 Crypto::SignText(JSContext* aContext,
189 const nsAString& aStringToSign,
190 const nsAString& aCaOption,
191 const Sequence<nsCString>& aArgs,
192 nsAString& aReturn)
195 aReturn.AssignLiteral("error:internalError");
198 void
199 Crypto::Logout(ErrorResult& aRv)
201 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
204 #endif
206 /* static */ uint8_t*
207 Crypto::GetRandomValues(uint32_t aLength)
209 nsCOMPtr<nsIRandomGenerator> randomGenerator;
210 nsresult rv;
211 randomGenerator = do_GetService("@mozilla.org/security/random-generator;1");
212 NS_ENSURE_TRUE(randomGenerator, nullptr);
214 uint8_t* buf;
215 rv = randomGenerator->GenerateRandomBytes(aLength, &buf);
217 NS_ENSURE_SUCCESS(rv, nullptr);
219 return buf;
222 } // namespace dom
223 } // namespace mozilla