1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is mozilla.org code.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Red Hat, Inc.
18 : * Portions created by the Initial Developer are Copyright (C) 2006
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Kai Engert <kengert@redhat.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsMemory.h"
39 : #include "nsAutoPtr.h"
40 : #include "nsCertVerificationThread.h"
41 : #include "nsThreadUtils.h"
42 :
43 : using namespace mozilla;
44 :
45 : nsCertVerificationThread *nsCertVerificationThread::verification_thread_singleton;
46 :
47 0 : NS_IMPL_THREADSAFE_ISUPPORTS1(nsCertVerificationResult, nsICertVerificationResult)
48 :
49 : namespace {
50 : class DispatchCertVerificationResult : public nsRunnable
51 0 : {
52 : public:
53 0 : DispatchCertVerificationResult(nsICertVerificationListener* aListener,
54 : nsIX509Cert3* aCert,
55 : nsICertVerificationResult* aResult)
56 : : mListener(aListener)
57 : , mCert(aCert)
58 0 : , mResult(aResult)
59 0 : { }
60 :
61 0 : NS_IMETHOD Run() {
62 0 : mListener->Notify(mCert, mResult);
63 0 : return NS_OK;
64 : }
65 :
66 : private:
67 : nsCOMPtr<nsICertVerificationListener> mListener;
68 : nsCOMPtr<nsIX509Cert3> mCert;
69 : nsCOMPtr<nsICertVerificationResult> mResult;
70 : };
71 : } // anonymous namespace
72 :
73 0 : void nsCertVerificationJob::Run()
74 : {
75 0 : if (!mListener || !mCert)
76 0 : return;
77 :
78 : PRUint32 verified;
79 : PRUint32 count;
80 : PRUnichar **usages;
81 :
82 0 : nsCOMPtr<nsICertVerificationResult> ires;
83 0 : nsRefPtr<nsCertVerificationResult> vres = new nsCertVerificationResult;
84 0 : if (vres)
85 : {
86 0 : nsresult rv = mCert->GetUsagesArray(false, // do not ignore OCSP
87 : &verified,
88 : &count,
89 0 : &usages);
90 0 : vres->mRV = rv;
91 0 : if (NS_SUCCEEDED(rv))
92 : {
93 0 : vres->mVerified = verified;
94 0 : vres->mCount = count;
95 0 : vres->mUsages = usages;
96 : }
97 :
98 0 : ires = vres;
99 : }
100 :
101 0 : nsCOMPtr<nsIX509Cert3> c3 = do_QueryInterface(mCert);
102 0 : nsCOMPtr<nsIRunnable> r = new DispatchCertVerificationResult(mListener, c3, ires);
103 0 : NS_DispatchToMainThread(r);
104 : }
105 :
106 0 : void nsSMimeVerificationJob::Run()
107 : {
108 0 : if (!mMessage || !mListener)
109 0 : return;
110 :
111 : nsresult rv;
112 :
113 0 : if (digest_data)
114 0 : rv = mMessage->VerifyDetachedSignature(digest_data, digest_len);
115 : else
116 0 : rv = mMessage->VerifySignature();
117 :
118 0 : nsCOMPtr<nsICMSMessage2> m2 = do_QueryInterface(mMessage);
119 0 : mListener->Notify(m2, rv);
120 : }
121 :
122 328 : nsCertVerificationThread::nsCertVerificationThread()
123 328 : : mJobQ(nsnull)
124 : {
125 328 : NS_ASSERTION(!verification_thread_singleton,
126 : "nsCertVerificationThread is a singleton, caller attempts"
127 : " to create another instance!");
128 :
129 328 : verification_thread_singleton = this;
130 328 : }
131 :
132 984 : nsCertVerificationThread::~nsCertVerificationThread()
133 : {
134 328 : verification_thread_singleton = nsnull;
135 1312 : }
136 :
137 0 : nsresult nsCertVerificationThread::addJob(nsBaseVerificationJob *aJob)
138 : {
139 0 : if (!aJob || !verification_thread_singleton)
140 0 : return NS_ERROR_FAILURE;
141 :
142 0 : if (!verification_thread_singleton->mThreadHandle)
143 0 : return NS_ERROR_OUT_OF_MEMORY;
144 :
145 0 : MutexAutoLock threadLock(verification_thread_singleton->mMutex);
146 :
147 0 : verification_thread_singleton->mJobQ.Push(aJob);
148 0 : verification_thread_singleton->mCond.NotifyAll();
149 :
150 0 : return NS_OK;
151 : }
152 :
153 328 : void nsCertVerificationThread::Run(void)
154 : {
155 0 : while (true) {
156 :
157 328 : nsBaseVerificationJob *job = nsnull;
158 :
159 : {
160 656 : MutexAutoLock threadLock(verification_thread_singleton->mMutex);
161 :
162 1312 : while (!exitRequested(threadLock) &&
163 328 : 0 == verification_thread_singleton->mJobQ.GetSize()) {
164 : // no work to do ? let's wait a moment
165 :
166 328 : mCond.Wait();
167 : }
168 :
169 328 : if (exitRequested(threadLock))
170 : break;
171 :
172 328 : job = static_cast<nsBaseVerificationJob*>(mJobQ.PopFront());
173 : }
174 :
175 0 : if (job)
176 : {
177 0 : job->Run();
178 0 : delete job;
179 : }
180 : }
181 :
182 : {
183 656 : MutexAutoLock threadLock(verification_thread_singleton->mMutex);
184 :
185 656 : while (verification_thread_singleton->mJobQ.GetSize()) {
186 : nsCertVerificationJob *job =
187 0 : static_cast<nsCertVerificationJob*>(mJobQ.PopFront());
188 0 : delete job;
189 : }
190 328 : postStoppedEventToMainThread(threadLock);
191 : }
192 328 : }
193 :
194 0 : nsCertVerificationResult::nsCertVerificationResult()
195 : : mRV(0),
196 : mVerified(0),
197 : mCount(0),
198 0 : mUsages(0)
199 : {
200 0 : }
201 :
202 0 : nsCertVerificationResult::~nsCertVerificationResult()
203 : {
204 0 : if (mUsages)
205 : {
206 0 : NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(mCount, mUsages);
207 : }
208 0 : }
209 :
210 : NS_IMETHODIMP
211 0 : nsCertVerificationResult::GetUsagesArrayResult(PRUint32 *aVerified,
212 : PRUint32 *aCount,
213 : PRUnichar ***aUsages)
214 : {
215 0 : if (NS_FAILED(mRV))
216 0 : return mRV;
217 :
218 : // transfer ownership
219 :
220 0 : *aVerified = mVerified;
221 0 : *aCount = mCount;
222 0 : *aUsages = mUsages;
223 :
224 0 : mVerified = 0;
225 0 : mCount = 0;
226 0 : mUsages = 0;
227 :
228 0 : nsresult rv = mRV;
229 :
230 0 : mRV = NS_ERROR_FAILURE; // this object works only once...
231 :
232 0 : return rv;
233 : }
|