security/manager/ssl/src/nsProtectedAuthThread.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.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "pk11func.h"
michael@0 6 #include "mozilla/DebugOnly.h"
michael@0 7 #include "mozilla/RefPtr.h"
michael@0 8 #include "nsCOMPtr.h"
michael@0 9 #include "PSMRunnable.h"
michael@0 10 #include "nsString.h"
michael@0 11 #include "nsReadableUtils.h"
michael@0 12 #include "nsPKCS11Slot.h"
michael@0 13 #include "nsProtectedAuthThread.h"
michael@0 14
michael@0 15 using namespace mozilla;
michael@0 16 using namespace mozilla::psm;
michael@0 17
michael@0 18 NS_IMPL_ISUPPORTS(nsProtectedAuthThread, nsIProtectedAuthThread)
michael@0 19
michael@0 20 static void nsProtectedAuthThreadRunner(void *arg)
michael@0 21 {
michael@0 22 PR_SetCurrentThreadName("Protected Auth");
michael@0 23
michael@0 24 nsProtectedAuthThread *self = static_cast<nsProtectedAuthThread *>(arg);
michael@0 25 self->Run();
michael@0 26 }
michael@0 27
michael@0 28 nsProtectedAuthThread::nsProtectedAuthThread()
michael@0 29 : mMutex("nsProtectedAuthThread.mMutex")
michael@0 30 , mIAmRunning(false)
michael@0 31 , mLoginReady(false)
michael@0 32 , mThreadHandle(nullptr)
michael@0 33 , mSlot(0)
michael@0 34 , mLoginResult(SECFailure)
michael@0 35 {
michael@0 36 NS_INIT_ISUPPORTS();
michael@0 37 }
michael@0 38
michael@0 39 nsProtectedAuthThread::~nsProtectedAuthThread()
michael@0 40 {
michael@0 41 }
michael@0 42
michael@0 43 NS_IMETHODIMP nsProtectedAuthThread::Login(nsIObserver *aObserver)
michael@0 44 {
michael@0 45 NS_ENSURE_ARG(aObserver);
michael@0 46
michael@0 47 if (!mSlot)
michael@0 48 // We need pointer to the slot
michael@0 49 return NS_ERROR_FAILURE;
michael@0 50
michael@0 51 MutexAutoLock lock(mMutex);
michael@0 52
michael@0 53 if (mIAmRunning || mLoginReady) {
michael@0 54 return NS_OK;
michael@0 55 }
michael@0 56
michael@0 57 if (aObserver) {
michael@0 58 // We must AddRef aObserver here on the main thread, because it probably
michael@0 59 // does not implement a thread-safe AddRef.
michael@0 60 mNotifyObserver = new NotifyObserverRunnable(aObserver,
michael@0 61 "operation-completed");
michael@0 62 }
michael@0 63
michael@0 64 mIAmRunning = true;
michael@0 65
michael@0 66 mThreadHandle = PR_CreateThread(PR_USER_THREAD, nsProtectedAuthThreadRunner, static_cast<void*>(this),
michael@0 67 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
michael@0 68
michael@0 69 // bool thread_started_ok = (threadHandle != nullptr);
michael@0 70 // we might want to return "thread started ok" to caller in the future
michael@0 71 NS_ASSERTION(mThreadHandle, "Could not create nsProtectedAuthThreadRunner thread\n");
michael@0 72
michael@0 73 return NS_OK;
michael@0 74 }
michael@0 75
michael@0 76 NS_IMETHODIMP nsProtectedAuthThread::GetTokenName(nsAString &_retval)
michael@0 77 {
michael@0 78 MutexAutoLock lock(mMutex);
michael@0 79
michael@0 80 // Get token name
michael@0 81 CopyUTF8toUTF16(nsDependentCString(PK11_GetTokenName(mSlot)), _retval);
michael@0 82
michael@0 83 return NS_OK;
michael@0 84 }
michael@0 85
michael@0 86 NS_IMETHODIMP nsProtectedAuthThread::GetSlot(nsIPKCS11Slot **_retval)
michael@0 87 {
michael@0 88 RefPtr<nsPKCS11Slot> slot;
michael@0 89 {
michael@0 90 MutexAutoLock lock(mMutex);
michael@0 91 slot = new nsPKCS11Slot(mSlot);
michael@0 92 }
michael@0 93
michael@0 94 return CallQueryInterface (slot.get(), _retval);
michael@0 95 }
michael@0 96
michael@0 97 void nsProtectedAuthThread::SetParams(PK11SlotInfo* aSlot)
michael@0 98 {
michael@0 99 MutexAutoLock lock(mMutex);
michael@0 100
michael@0 101 mSlot = (aSlot) ? PK11_ReferenceSlot(aSlot) : 0;
michael@0 102 }
michael@0 103
michael@0 104 SECStatus nsProtectedAuthThread::GetResult()
michael@0 105 {
michael@0 106 return mLoginResult;
michael@0 107 }
michael@0 108
michael@0 109 void nsProtectedAuthThread::Run(void)
michael@0 110 {
michael@0 111 // Login with null password. This call will also do C_Logout() but
michael@0 112 // it is harmless here
michael@0 113 mLoginResult = PK11_CheckUserPassword(mSlot, 0);
michael@0 114
michael@0 115 nsCOMPtr<nsIRunnable> notifyObserver;
michael@0 116 {
michael@0 117 MutexAutoLock lock(mMutex);
michael@0 118
michael@0 119 mLoginReady = true;
michael@0 120 mIAmRunning = false;
michael@0 121
michael@0 122 // Forget the slot
michael@0 123 if (mSlot)
michael@0 124 {
michael@0 125 PK11_FreeSlot(mSlot);
michael@0 126 mSlot = 0;
michael@0 127 }
michael@0 128
michael@0 129 notifyObserver.swap(mNotifyObserver);
michael@0 130 }
michael@0 131
michael@0 132 if (notifyObserver) {
michael@0 133 DebugOnly<nsresult> rv = NS_DispatchToMainThread(notifyObserver);
michael@0 134 NS_ASSERTION(NS_SUCCEEDED(rv),
michael@0 135 "failed to dispatch protected auth observer to main thread");
michael@0 136 }
michael@0 137 }
michael@0 138
michael@0 139 void nsProtectedAuthThread::Join()
michael@0 140 {
michael@0 141 if (!mThreadHandle)
michael@0 142 return;
michael@0 143
michael@0 144 PR_JoinThread(mThreadHandle);
michael@0 145 mThreadHandle = nullptr;
michael@0 146 }

mercurial