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/. */
5 #include "jsfriendapi.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
;
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
)
29 NS_IMPL_CYCLE_COLLECTING_ADDREF(Crypto
)
30 NS_IMPL_CYCLE_COLLECTING_RELEASE(Crypto
)
32 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Crypto
, mWindow
, mSubtle
)
36 MOZ_COUNT_CTOR(Crypto
);
42 MOZ_COUNT_DTOR(Crypto
);
46 Crypto::Init(nsIDOMWindow
* aWindow
)
48 mWindow
= do_QueryInterface(aWindow
);
52 /* virtual */ JSObject
*
53 Crypto::WrapObject(JSContext
* aCx
)
55 return CryptoBinding::Wrap(aCx
, this);
59 Crypto::GetRandomValues(JSContext
* aCx
, const ArrayBufferView
& aArray
,
60 JS::MutableHandle
<JSObject
*> aRetval
,
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
)) {
72 case TYPE_UINT8_CLAMPED
:
79 aRv
.Throw(NS_ERROR_DOM_TYPE_MISMATCH_ERR
);
83 aArray
.ComputeLengthAndData();
84 uint32_t dataLen
= aArray
.Length();
86 NS_WARNING("ArrayBufferView length is 0, cannot continue");
89 } else if (dataLen
> 65536) {
90 aRv
.Throw(NS_ERROR_DOM_QUOTA_EXCEEDED_ERR
);
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
);
105 NS_ASSERTION(dataLen
== randomValues
.Length(),
106 "Invalid length returned from parent process!");
107 memcpy(data
, randomValues
.Elements(), dataLen
);
109 uint8_t *buf
= GetRandomValues(dataLen
);
112 aRv
.Throw(NS_ERROR_FAILURE
);
116 memcpy(data
, buf
, dataLen
);
127 mSubtle
= new SubtleCrypto(GetParentObject());
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}
137 Crypto::GetEnableSmartCardEvents(bool *aEnableSmartCardEvents
)
139 return NS_ERROR_NOT_IMPLEMENTED
;
143 Crypto::SetEnableSmartCardEvents(bool aEnableSmartCardEvents
)
145 return NS_ERROR_NOT_IMPLEMENTED
;
149 Crypto::EnableSmartCardEvents()
155 Crypto::SetEnableSmartCardEvents(bool aEnable
, ErrorResult
& aRv
)
157 aRv
.Throw(NS_ERROR_NOT_IMPLEMENTED
);
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
,
175 aRv
.Throw(NS_ERROR_NOT_IMPLEMENTED
);
180 Crypto::ImportUserCertificates(const nsAString
& aNickname
,
181 const nsAString
& aCmmfResponse
,
182 bool aDoForcedBackup
,
186 aRv
.Throw(NS_ERROR_NOT_IMPLEMENTED
);
190 Crypto::SignText(JSContext
* aContext
,
191 const nsAString
& aStringToSign
,
192 const nsAString
& aCaOption
,
193 const Sequence
<nsCString
>& aArgs
,
197 aReturn
.AssignLiteral("error:internalError");
201 Crypto::Logout(ErrorResult
& aRv
)
203 aRv
.Throw(NS_ERROR_NOT_IMPLEMENTED
);
208 /* static */ uint8_t*
209 Crypto::GetRandomValues(uint32_t aLength
)
211 nsCOMPtr
<nsIRandomGenerator
> randomGenerator
;
213 randomGenerator
= do_GetService("@mozilla.org/security/random-generator;1");
214 NS_ENSURE_TRUE(randomGenerator
, nullptr);
217 rv
= randomGenerator
->GenerateRandomBytes(aLength
, &buf
);
219 NS_ENSURE_SUCCESS(rv
, nullptr);
225 } // namespace mozilla