security/manager/ssl/src/nsCertVerificationThread.cpp

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

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 "nsCertVerificationThread.h"
michael@0 6 #include "nsThreadUtils.h"
michael@0 7 #include "nsProxyRelease.h"
michael@0 8
michael@0 9 using namespace mozilla;
michael@0 10
michael@0 11 nsCertVerificationThread *nsCertVerificationThread::verification_thread_singleton;
michael@0 12
michael@0 13 NS_IMPL_ISUPPORTS(nsCertVerificationResult, nsICertVerificationResult)
michael@0 14
michael@0 15 namespace {
michael@0 16 class DispatchCertVerificationResult : public nsRunnable
michael@0 17 {
michael@0 18 public:
michael@0 19 DispatchCertVerificationResult(const nsMainThreadPtrHandle<nsICertVerificationListener>& aListener,
michael@0 20 nsIX509Cert3* aCert,
michael@0 21 nsICertVerificationResult* aResult)
michael@0 22 : mListener(aListener)
michael@0 23 , mCert(aCert)
michael@0 24 , mResult(aResult)
michael@0 25 { }
michael@0 26
michael@0 27 NS_IMETHOD Run() {
michael@0 28 mListener->Notify(mCert, mResult);
michael@0 29 return NS_OK;
michael@0 30 }
michael@0 31
michael@0 32 private:
michael@0 33 nsMainThreadPtrHandle<nsICertVerificationListener> mListener;
michael@0 34 nsCOMPtr<nsIX509Cert3> mCert;
michael@0 35 nsCOMPtr<nsICertVerificationResult> mResult;
michael@0 36 };
michael@0 37 } // anonymous namespace
michael@0 38
michael@0 39 void nsCertVerificationJob::Run()
michael@0 40 {
michael@0 41 if (!mListener || !mCert)
michael@0 42 return;
michael@0 43
michael@0 44 uint32_t verified;
michael@0 45 uint32_t count;
michael@0 46 char16_t **usages;
michael@0 47
michael@0 48 nsCOMPtr<nsICertVerificationResult> ires;
michael@0 49 RefPtr<nsCertVerificationResult> vres(new nsCertVerificationResult);
michael@0 50 if (vres)
michael@0 51 {
michael@0 52 nsresult rv = mCert->GetUsagesArray(false, // do not ignore OCSP
michael@0 53 &verified,
michael@0 54 &count,
michael@0 55 &usages);
michael@0 56 vres->mRV = rv;
michael@0 57 if (NS_SUCCEEDED(rv))
michael@0 58 {
michael@0 59 vres->mVerified = verified;
michael@0 60 vres->mCount = count;
michael@0 61 vres->mUsages = usages;
michael@0 62 }
michael@0 63
michael@0 64 ires = vres;
michael@0 65 }
michael@0 66
michael@0 67 nsCOMPtr<nsIX509Cert3> c3 = do_QueryInterface(mCert);
michael@0 68 nsCOMPtr<nsIRunnable> r = new DispatchCertVerificationResult(mListener, c3, ires);
michael@0 69 NS_DispatchToMainThread(r);
michael@0 70 }
michael@0 71
michael@0 72 void nsSMimeVerificationJob::Run()
michael@0 73 {
michael@0 74 if (!mMessage || !mListener)
michael@0 75 return;
michael@0 76
michael@0 77 nsresult rv;
michael@0 78
michael@0 79 if (digest_data)
michael@0 80 rv = mMessage->VerifyDetachedSignature(digest_data, digest_len);
michael@0 81 else
michael@0 82 rv = mMessage->VerifySignature();
michael@0 83
michael@0 84 nsCOMPtr<nsICMSMessage2> m2 = do_QueryInterface(mMessage);
michael@0 85 mListener->Notify(m2, rv);
michael@0 86 }
michael@0 87
michael@0 88 nsCertVerificationThread::nsCertVerificationThread()
michael@0 89 : mJobQ(nullptr)
michael@0 90 {
michael@0 91 NS_ASSERTION(!verification_thread_singleton,
michael@0 92 "nsCertVerificationThread is a singleton, caller attempts"
michael@0 93 " to create another instance!");
michael@0 94
michael@0 95 verification_thread_singleton = this;
michael@0 96 }
michael@0 97
michael@0 98 nsCertVerificationThread::~nsCertVerificationThread()
michael@0 99 {
michael@0 100 verification_thread_singleton = nullptr;
michael@0 101 }
michael@0 102
michael@0 103 nsresult nsCertVerificationThread::addJob(nsBaseVerificationJob *aJob)
michael@0 104 {
michael@0 105 if (!aJob || !verification_thread_singleton)
michael@0 106 return NS_ERROR_FAILURE;
michael@0 107
michael@0 108 if (!verification_thread_singleton->mThreadHandle)
michael@0 109 return NS_ERROR_OUT_OF_MEMORY;
michael@0 110
michael@0 111 MutexAutoLock threadLock(verification_thread_singleton->mMutex);
michael@0 112
michael@0 113 verification_thread_singleton->mJobQ.Push(aJob);
michael@0 114 verification_thread_singleton->mCond.NotifyAll();
michael@0 115
michael@0 116 return NS_OK;
michael@0 117 }
michael@0 118
michael@0 119 void nsCertVerificationThread::Run(void)
michael@0 120 {
michael@0 121 while (true) {
michael@0 122
michael@0 123 nsBaseVerificationJob *job = nullptr;
michael@0 124
michael@0 125 {
michael@0 126 MutexAutoLock threadLock(verification_thread_singleton->mMutex);
michael@0 127
michael@0 128 while (!exitRequested(threadLock) &&
michael@0 129 0 == verification_thread_singleton->mJobQ.GetSize()) {
michael@0 130 // no work to do ? let's wait a moment
michael@0 131
michael@0 132 mCond.Wait();
michael@0 133 }
michael@0 134
michael@0 135 if (exitRequested(threadLock))
michael@0 136 break;
michael@0 137
michael@0 138 job = static_cast<nsBaseVerificationJob*>(mJobQ.PopFront());
michael@0 139 }
michael@0 140
michael@0 141 if (job)
michael@0 142 {
michael@0 143 job->Run();
michael@0 144 delete job;
michael@0 145 }
michael@0 146 }
michael@0 147
michael@0 148 {
michael@0 149 MutexAutoLock threadLock(verification_thread_singleton->mMutex);
michael@0 150
michael@0 151 while (verification_thread_singleton->mJobQ.GetSize()) {
michael@0 152 nsCertVerificationJob *job =
michael@0 153 static_cast<nsCertVerificationJob*>(mJobQ.PopFront());
michael@0 154 delete job;
michael@0 155 }
michael@0 156 postStoppedEventToMainThread(threadLock);
michael@0 157 }
michael@0 158 }
michael@0 159
michael@0 160 nsCertVerificationResult::nsCertVerificationResult()
michael@0 161 : mRV(NS_OK),
michael@0 162 mVerified(0),
michael@0 163 mCount(0),
michael@0 164 mUsages(0)
michael@0 165 {
michael@0 166 }
michael@0 167
michael@0 168 nsCertVerificationResult::~nsCertVerificationResult()
michael@0 169 {
michael@0 170 if (mUsages)
michael@0 171 {
michael@0 172 NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(mCount, mUsages);
michael@0 173 }
michael@0 174 }
michael@0 175
michael@0 176 NS_IMETHODIMP
michael@0 177 nsCertVerificationResult::GetUsagesArrayResult(uint32_t *aVerified,
michael@0 178 uint32_t *aCount,
michael@0 179 char16_t ***aUsages)
michael@0 180 {
michael@0 181 if (NS_FAILED(mRV))
michael@0 182 return mRV;
michael@0 183
michael@0 184 // transfer ownership
michael@0 185
michael@0 186 *aVerified = mVerified;
michael@0 187 *aCount = mCount;
michael@0 188 *aUsages = mUsages;
michael@0 189
michael@0 190 mVerified = 0;
michael@0 191 mCount = 0;
michael@0 192 mUsages = 0;
michael@0 193
michael@0 194 nsresult rv = mRV;
michael@0 195
michael@0 196 mRV = NS_ERROR_FAILURE; // this object works only once...
michael@0 197
michael@0 198 return rv;
michael@0 199 }

mercurial