dom/base/Crypto.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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_1(Crypto, mWindow)
    34 Crypto::Crypto()
    35 {
    36   MOZ_COUNT_CTOR(Crypto);
    37   SetIsDOMBinding();
    38 }
    40 Crypto::~Crypto()
    41 {
    42   MOZ_COUNT_DTOR(Crypto);
    43 }
    45 void
    46 Crypto::Init(nsIDOMWindow* aWindow)
    47 {
    48   mWindow = do_QueryInterface(aWindow);
    49   MOZ_ASSERT(mWindow);
    50 }
    52 /* virtual */ JSObject*
    53 Crypto::WrapObject(JSContext* aCx)
    54 {
    55   return CryptoBinding::Wrap(aCx, this);
    56 }
    58 void
    59 Crypto::GetRandomValues(JSContext* aCx, const ArrayBufferView& aArray,
    60 			JS::MutableHandle<JSObject*> aRetval,
    61 			ErrorResult& aRv)
    62 {
    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;
    81   }
    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;
    92   }
    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;
   104     }
   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;
   114     }
   116     memcpy(data, buf, dataLen);
   117     NS_Free(buf);
   118   }
   120   aRetval.set(view);
   121 }
   123 #ifndef MOZ_DISABLE_CRYPTOLEGACY
   124 // Stub out the legacy nsIDOMCrypto methods. The actual
   125 // implementations are in security/manager/ssl/src/nsCrypto.{cpp,h}
   127 NS_IMETHODIMP
   128 Crypto::GetEnableSmartCardEvents(bool *aEnableSmartCardEvents)
   129 {
   130   return NS_ERROR_NOT_IMPLEMENTED;
   131 }
   133 NS_IMETHODIMP
   134 Crypto::SetEnableSmartCardEvents(bool aEnableSmartCardEvents)
   135 {
   136   return NS_ERROR_NOT_IMPLEMENTED;
   137 }
   139 bool
   140 Crypto::EnableSmartCardEvents()
   141 {
   142   return false;
   143 }
   145 void
   146 Crypto::SetEnableSmartCardEvents(bool aEnable, ErrorResult& aRv)
   147 {
   148   aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
   149 }
   151 void
   152 Crypto::GetVersion(nsString& aVersion)
   153 {
   154 }
   156 mozilla::dom::CRMFObject*
   157 Crypto::GenerateCRMFRequest(JSContext* aContext,
   158                             const nsCString& aReqDN,
   159                             const nsCString& aRegToken,
   160                             const nsCString& aAuthenticator,
   161                             const nsCString& aEaCert,
   162                             const nsCString& aJsCallback,
   163                             const Sequence<JS::Value>& aArgs,
   164                             ErrorResult& aRv)
   165 {
   166   aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
   167   return nullptr;
   168 }
   170 void
   171 Crypto::ImportUserCertificates(const nsAString& aNickname,
   172                                const nsAString& aCmmfResponse,
   173                                bool aDoForcedBackup,
   174                                nsAString& aReturn,
   175                                ErrorResult& aRv)
   176 {
   177   aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
   178 }
   180 void
   181 Crypto::SignText(JSContext* aContext,
   182                  const nsAString& aStringToSign,
   183                  const nsAString& aCaOption,
   184                  const Sequence<nsCString>& aArgs,
   185                  nsAString& aReturn)
   187 {
   188   aReturn.AssignLiteral("error:internalError");
   189 }
   191 void
   192 Crypto::Logout(ErrorResult& aRv)
   193 {
   194   aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
   195 }
   197 #endif
   199 /* static */ uint8_t*
   200 Crypto::GetRandomValues(uint32_t aLength)
   201 {
   202   nsCOMPtr<nsIRandomGenerator> randomGenerator;
   203   nsresult rv;
   204   randomGenerator = do_GetService("@mozilla.org/security/random-generator;1");
   205   NS_ENSURE_TRUE(randomGenerator, nullptr);
   207   uint8_t* buf;
   208   rv = randomGenerator->GenerateRandomBytes(aLength, &buf);
   210   NS_ENSURE_SUCCESS(rv, nullptr);
   212   return buf;
   213 }
   215 } // namespace dom
   216 } // namespace mozilla

mercurial