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
;
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
)
27 NS_IMPL_CYCLE_COLLECTING_ADDREF(Crypto
)
28 NS_IMPL_CYCLE_COLLECTING_RELEASE(Crypto
)
30 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Crypto
, mWindow
, mSubtle
)
34 MOZ_COUNT_CTOR(Crypto
);
40 MOZ_COUNT_DTOR(Crypto
);
44 Crypto::Init(nsIDOMWindow
* aWindow
)
46 mWindow
= do_QueryInterface(aWindow
);
50 /* virtual */ JSObject
*
51 Crypto::WrapObject(JSContext
* aCx
)
53 return CryptoBinding::Wrap(aCx
, this);
57 Crypto::GetRandomValues(JSContext
* aCx
, const ArrayBufferView
& aArray
,
58 JS::MutableHandle
<JSObject
*> aRetval
,
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
:
77 aRv
.Throw(NS_ERROR_DOM_TYPE_MISMATCH_ERR
);
81 aArray
.ComputeLengthAndData();
82 uint32_t dataLen
= aArray
.Length();
84 NS_WARNING("ArrayBufferView length is 0, cannot continue");
87 } else if (dataLen
> 65536) {
88 aRv
.Throw(NS_ERROR_DOM_QUOTA_EXCEEDED_ERR
);
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
);
103 NS_ASSERTION(dataLen
== randomValues
.Length(),
104 "Invalid length returned from parent process!");
105 memcpy(data
, randomValues
.Elements(), dataLen
);
107 uint8_t *buf
= GetRandomValues(dataLen
);
110 aRv
.Throw(NS_ERROR_FAILURE
);
114 memcpy(data
, buf
, dataLen
);
125 mSubtle
= new SubtleCrypto(GetParentObject());
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}
135 Crypto::GetEnableSmartCardEvents(bool *aEnableSmartCardEvents
)
137 return NS_ERROR_NOT_IMPLEMENTED
;
141 Crypto::SetEnableSmartCardEvents(bool aEnableSmartCardEvents
)
143 return NS_ERROR_NOT_IMPLEMENTED
;
147 Crypto::EnableSmartCardEvents()
153 Crypto::SetEnableSmartCardEvents(bool aEnable
, ErrorResult
& aRv
)
155 aRv
.Throw(NS_ERROR_NOT_IMPLEMENTED
);
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
,
173 aRv
.Throw(NS_ERROR_NOT_IMPLEMENTED
);
178 Crypto::ImportUserCertificates(const nsAString
& aNickname
,
179 const nsAString
& aCmmfResponse
,
180 bool aDoForcedBackup
,
184 aRv
.Throw(NS_ERROR_NOT_IMPLEMENTED
);
188 Crypto::SignText(JSContext
* aContext
,
189 const nsAString
& aStringToSign
,
190 const nsAString
& aCaOption
,
191 const Sequence
<nsCString
>& aArgs
,
195 aReturn
.AssignLiteral("error:internalError");
199 Crypto::Logout(ErrorResult
& aRv
)
201 aRv
.Throw(NS_ERROR_NOT_IMPLEMENTED
);
206 /* static */ uint8_t*
207 Crypto::GetRandomValues(uint32_t aLength
)
209 nsCOMPtr
<nsIRandomGenerator
> randomGenerator
;
211 randomGenerator
= do_GetService("@mozilla.org/security/random-generator;1");
212 NS_ENSURE_TRUE(randomGenerator
, nullptr);
215 rv
= randomGenerator
->GenerateRandomBytes(aLength
, &buf
);
217 NS_ENSURE_SUCCESS(rv
, nullptr);
223 } // namespace mozilla