1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsPgpMimeProxy.h"
#include "nspr.h"
#include "plstr.h"
#include "nsCOMPtr.h"
#include "nsStringGlue.h"
#include "mozilla/Services.h"
#include "nsIRequest.h"
#include "nsIStringBundle.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
#include "nsIURI.h"
#include "mimexpcom.h"
#include "nsMsgUtils.h"
#include "nsMsgMimeCID.h"
#include "mimecth.h"
#include "mimemoz2.h"
#include "nspr.h"
#include "plstr.h"
#include "nsIPgpMimeProxy.h"
#include "nsComponentManagerUtils.h"
#define MIME_SUPERCLASS mimeEncryptedClass
MimeDefClass(MimeEncryptedPgp, MimeEncryptedPgpClass,
mimeEncryptedPgpClass, &MIME_SUPERCLASS);
#define kCharMax 1024
extern "C" MimeObjectClass *
MIME_PgpMimeCreateContentTypeHandlerClass(
const char *content_type,
contentTypeHandlerInitStruct *initStruct)
{
MimeObjectClass *objClass = (MimeObjectClass *) &mimeEncryptedPgpClass;
initStruct->force_inline_display = false;
return objClass;
}
static void *MimePgpe_init(MimeObject *,
int (*output_fn) (const char *, int32_t, void *),
void *);
static int MimePgpe_write (const char *, int32_t, void *);
static int MimePgpe_eof (void *, bool);
static char* MimePgpe_generate (void *);
static void MimePgpe_free (void *);
/* Returns a string describing the location of the part (like "2.5.3").
This is not a full URL, just a part-number.
*/
static nsCString determineMimePart(MimeObject* obj);
#define PGPMIME_PROPERTIES_URL "chrome://messenger/locale/pgpmime.properties"
#define PGPMIME_STR_NOT_SUPPORTED_ID u"pgpMimeNeedsAddon"
#define PGPMIME_URL_PREF "mail.pgpmime.addon_url"
static void PgpMimeGetNeedsAddonString(nsCString &aResult)
{
aResult.AssignLiteral("???");
nsCOMPtr<nsIStringBundleService> stringBundleService =
mozilla::services::GetStringBundleService();
nsCOMPtr<nsIStringBundle> stringBundle;
nsresult rv = stringBundleService->CreateBundle(PGPMIME_PROPERTIES_URL,
getter_AddRefs(stringBundle));
if (NS_FAILED(rv))
return;
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
if (NS_FAILED(rv))
return;
nsCString url;
if (NS_FAILED(prefs->GetCharPref("mail.pgpmime.addon_url",
getter_Copies(url))))
return;
NS_ConvertUTF8toUTF16 url16(url);
const char16_t *formatStrings[] = { url16.get() };
nsString result;
rv = stringBundle->FormatStringFromName(PGPMIME_STR_NOT_SUPPORTED_ID,
formatStrings, 1, getter_Copies(result));
if (NS_FAILED(rv))
return;
aResult = NS_ConvertUTF16toUTF8(result);
}
static int
MimeEncryptedPgpClassInitialize(MimeEncryptedPgpClass *clazz)
{
mozilla::DebugOnly<MimeObjectClass *> oclass = (MimeObjectClass *) clazz;
NS_ASSERTION(!oclass->class_initialized, "oclass is not initialized");
MimeEncryptedClass *eclass = (MimeEncryptedClass *) clazz;
eclass->crypto_init = MimePgpe_init;
eclass->crypto_write = MimePgpe_write;
eclass->crypto_eof = MimePgpe_eof;
eclass->crypto_generate_html = MimePgpe_generate;
eclass->crypto_free = MimePgpe_free;
return 0;
}
class MimePgpeData : public nsISupports
{
public:
NS_DECL_ISUPPORTS
int (*output_fn) (const char *buf, int32_t buf_size, void *output_closure);
void *output_closure;
MimeObject *self;
nsCOMPtr<nsIPgpMimeProxy> mimeDecrypt;
MimePgpeData()
: output_fn(nullptr),
output_closure(nullptr)
{
}
private:
virtual ~MimePgpeData()
{
}
};
NS_IMPL_ISUPPORTS0(MimePgpeData)
static void*
MimePgpe_init(MimeObject *obj,
int (*output_fn) (const char *buf, int32_t buf_size,
void *output_closure),
void *output_closure)
{
if (!(obj && obj->options && output_fn))
return nullptr;
MimePgpeData* data = new MimePgpeData();
NS_ENSURE_TRUE(data, nullptr);
data->self = obj;
data->output_fn = output_fn;
data->output_closure = output_closure;
data->mimeDecrypt = nullptr;
nsresult rv;
data->mimeDecrypt = do_CreateInstance(NS_PGPMIMEPROXY_CONTRACTID, &rv);
if (NS_FAILED(rv))
return data;
char *ct = MimeHeaders_get(obj->headers, HEADER_CONTENT_TYPE, false, false);
rv = (ct ? data->mimeDecrypt->SetContentType(nsDependentCString(ct))
: data->mimeDecrypt->SetContentType(EmptyCString()));
PR_Free(ct);
if (NS_FAILED(rv))
return nullptr;
nsCString mimePart = determineMimePart(obj);
rv = data->mimeDecrypt->SetMimePart(mimePart);
if (NS_FAILED(rv))
return nullptr;
mime_stream_data *msd = (mime_stream_data *) (data->self->options->stream_closure);
nsIChannel *channel = msd->channel;
nsCOMPtr<nsIURI> uri;
if (channel)
channel->GetURI(getter_AddRefs(uri));
if (NS_FAILED(data->mimeDecrypt->SetMimeCallback(output_fn, output_closure, uri)))
return nullptr;
return data;
}
static int
MimePgpe_write(const char *buf, int32_t buf_size, void *output_closure)
{
MimePgpeData* data = (MimePgpeData *) output_closure;
if (!data || !data->output_fn)
return -1;
if (!data->mimeDecrypt)
return 0;
return (NS_SUCCEEDED(data->mimeDecrypt->Write(buf, buf_size)) ? 0 : -1);
}
static int
MimePgpe_eof(void* output_closure, bool abort_p)
{
MimePgpeData* data = (MimePgpeData *) output_closure;
if (!data || !data->output_fn)
return -1;
if (NS_FAILED(data->mimeDecrypt->Finish()))
return -1;
data->mimeDecrypt = nullptr;
return 0;
}
static char*
MimePgpe_generate(void *output_closure)
{
const char htmlMsg[] = "<html><body><b>GEN MSG<b></body></html>";
char* msg = (char *) PR_MALLOC(strlen(htmlMsg) + 1);
if (msg)
PL_strcpy(msg, htmlMsg);
return msg;
}
static void
MimePgpe_free(void *output_closure)
{
}
/* Returns a string describing the location of the part (like "2.5.3").
This is not a full URL, just a part-number.
*/
static nsCString
determineMimePart(MimeObject* obj)
{
char mimePartNum[20];
MimeObject *kid;
MimeContainer *cont;
int32_t i;
nsCString mimePart;
while (obj->parent) {
cont = (MimeContainer *) obj->parent;
for (i = 0; i < cont->nchildren; i++) {
kid = cont->children[i];
if (kid == obj) {
sprintf(mimePartNum, ".%d", i + 1);
mimePart.Insert(mimePartNum, 0);
}
}
obj = obj->parent;
}
// remove leading "."
if (mimePart.Length() > 0)
mimePart.Cut(0, 1);
return mimePart;
}
////////////////////////////////////////////////////////////////////////////
NS_IMPL_ISUPPORTS(nsPgpMimeProxy,
nsIPgpMimeProxy,
nsIRequestObserver,
nsIStreamListener,
nsIRequest,
nsIInputStream)
// nsPgpMimeProxy implementation
nsPgpMimeProxy::nsPgpMimeProxy()
: mInitialized(false),
mDecryptor(nullptr),
mLoadGroup(nullptr),
mLoadFlags(LOAD_NORMAL),
mCancelStatus(NS_OK)
{
}
nsPgpMimeProxy::~nsPgpMimeProxy()
{
Finalize();
}
nsresult
nsPgpMimeProxy::Finalize()
{
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::SetMimeCallback(MimeDecodeCallbackFun outputFun,
void* outputClosure,
nsIURI* myUri)
{
if (!outputFun || !outputClosure)
return NS_ERROR_NULL_POINTER;
mOutputFun = outputFun;
mOutputClosure = outputClosure;
mInitialized = true;
mStreamOffset = 0;
mByteBuf.Truncate();
if (mDecryptor)
return mDecryptor->OnStartRequest((nsIRequest*) this, myUri);
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::Init()
{
mByteBuf.Truncate();
nsresult rv;
nsCOMPtr<nsIPrefBranch> pbi(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
if (NS_FAILED(rv))
return rv;
mDecryptor = do_CreateInstance(PGPMIME_JS_DECRYPTOR_CONTRACTID, &rv);
if (NS_FAILED(rv))
mDecryptor = nullptr;
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::Write(const char *buf, uint32_t buf_size)
{
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
mByteBuf.Assign(buf, buf_size);
mStreamOffset = 0;
if (mDecryptor)
return mDecryptor->OnDataAvailable((nsIRequest*) this, nullptr, (nsIInputStream*) this,
0, buf_size);
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::Finish() {
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
if (mDecryptor) {
return mDecryptor->OnStopRequest((nsIRequest*) this, nullptr, NS_OK);
}
else {
nsCString temp;
temp.Append("Content-Type: text/html\r\nCharset: UTF-8\r\n\r\n<html><body>");
temp.Append("<BR><text=\"#000000\" bgcolor=\"#FFFFFF\" link=\"#FF0000\" vlink=\"#800080\" alink=\"#0000FF\">");
temp.Append("<center><table BORDER=1 ><tr><td><CENTER>");
nsCString tString;
PgpMimeGetNeedsAddonString(tString);
temp.Append(tString);
temp.Append("</CENTER></td></tr></table></center><BR></body></html>\r\n");
PR_SetError(0,0);
int status = mOutputFun(temp.get(), temp.Length(), mOutputClosure);
if (status < 0) {
PR_SetError(status, 0);
mOutputFun = nullptr;
return NS_ERROR_FAILURE;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::GetDecryptor(nsIStreamListener **aDecryptor)
{
NS_IF_ADDREF(*aDecryptor = mDecryptor);
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::SetDecryptor(nsIStreamListener *aDecryptor)
{
mDecryptor = aDecryptor;
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::GetContentType(nsACString &aContentType)
{
aContentType = mContentType;
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::SetContentType(const nsACString &aContentType)
{
mContentType = aContentType;
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::GetMimePart(nsACString &aMimePart)
{
aMimePart = mMimePart;
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::SetMimePart(const nsACString &aMimePart)
{
mMimePart = aMimePart;
return NS_OK;
}
///////////////////////////////////////////////////////////////////////////////
// nsIRequest methods
///////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP
nsPgpMimeProxy::GetName(nsACString &result)
{
result = "pgpmimeproxy";
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::IsPending(bool *result)
{
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
*result = NS_SUCCEEDED(mCancelStatus);
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::GetStatus(nsresult *status)
{
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
*status = mCancelStatus;
return NS_OK;
}
// NOTE: We assume that OnStopRequest should not be called if
// request is canceled. This may be wrong!
NS_IMETHODIMP
nsPgpMimeProxy::Cancel(nsresult status)
{
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
// Need a non-zero status code to cancel
if (NS_SUCCEEDED(status))
return NS_ERROR_FAILURE;
if (NS_SUCCEEDED(mCancelStatus))
mCancelStatus = status;
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::Suspend(void)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsPgpMimeProxy::Resume(void)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsPgpMimeProxy::GetLoadGroup(nsILoadGroup * *aLoadGroup)
{
NS_IF_ADDREF(*aLoadGroup = mLoadGroup);
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::SetLoadGroup(nsILoadGroup* aLoadGroup)
{
mLoadGroup = aLoadGroup;
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::GetLoadFlags(nsLoadFlags *aLoadFlags)
{
*aLoadFlags = mLoadFlags;
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::SetLoadFlags(nsLoadFlags aLoadFlags)
{
mLoadFlags = aLoadFlags;
return NS_OK;
}
///////////////////////////////////////////////////////////////////////////////
// nsIInputStream methods
///////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP
nsPgpMimeProxy::Available(uint64_t* _retval)
{
NS_ENSURE_ARG(_retval);
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
*_retval = (mByteBuf.Length() > mStreamOffset) ?
mByteBuf.Length() - mStreamOffset : 0;
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::Read(char* buf, uint32_t count,
uint32_t *readCount)
{
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
if (!buf || !readCount)
return NS_ERROR_NULL_POINTER;
int32_t avail = (mByteBuf.Length() > mStreamOffset) ?
mByteBuf.Length() - mStreamOffset : 0;
uint32_t readyCount = ((uint32_t) avail > count) ? count : avail;
if (readyCount) {
memcpy(buf, mByteBuf.get()+mStreamOffset, readyCount);
*readCount = readyCount;
}
mStreamOffset += *readCount;
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::ReadSegments(nsWriteSegmentFun writer,
void * aClosure, uint32_t count,
uint32_t *readCount)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsPgpMimeProxy::IsNonBlocking(bool *aNonBlocking)
{
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
*aNonBlocking = true;
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::Close()
{
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
mStreamOffset = 0;
mByteBuf.Truncate();
return NS_OK;
}
///////////////////////////////////////////////////////////////////////////////
// nsIStreamListener methods
///////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP
nsPgpMimeProxy::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext)
{
return NS_OK;
}
NS_IMETHODIMP
nsPgpMimeProxy::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext,
nsresult aStatus)
{
return NS_OK;
}
///////////////////////////////////////////////////////////////////////////////
// nsIStreamListener method
///////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP
nsPgpMimeProxy::OnDataAvailable(nsIRequest* aRequest, nsISupports* aContext,
nsIInputStream *aInputStream,
uint64_t aSourceOffset,
uint32_t aLength)
{
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
NS_ENSURE_ARG(aInputStream);
char buf[kCharMax];
uint32_t readCount, readMax;
while (aLength > 0) {
readMax = (aLength < kCharMax) ? aLength : kCharMax;
nsresult rv;
rv = aInputStream->Read((char *) buf, readMax, &readCount);
NS_ENSURE_SUCCESS(rv, rv);
int status = mOutputFun(buf, readCount, mOutputClosure);
if (status < 0) {
PR_SetError(status, 0);
mOutputFun = nullptr;
return NS_ERROR_FAILURE;
}
aLength -= readCount;
}
return NS_OK;
}
|