summaryrefslogtreecommitdiff
path: root/security/nss/lib/jar/jarevil.c
blob: feecd43b0b4b97aacb59d1a48657c971bd8b9335 (plain)
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
/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is the Netscape security libraries.
 * 
 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are 
 * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
 * Rights Reserved.
 * 
 * Contributor(s):
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL"), in which case the provisions of the GPL are applicable 
 * instead of those above.  If you wish to allow use of your 
 * version of this file only under the terms of the GPL and not to
 * allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by
 * the GPL.  If you do not delete the provisions above, a recipient
 * may use your version of this file under either the MPL or the
 * GPL.
 */

/*
 *  JAREVIL
 *
 *  Wrappers to callback in the mozilla thread
 *
 *  Certificate code is unsafe when called outside the
 *  mozilla thread. These functions push an event on the
 *  queue to cause the cert function to run in that thread. 
 *
 */

#include "jar.h"
#include "jarint.h"

#include "jarevil.h"

/* from libevent.h */
#ifdef MOZILLA_CLIENT
typedef void (*ETVoidPtrFunc) (void * data);
extern void ET_moz_CallFunction (ETVoidPtrFunc fn, void *data);

extern void *mozilla_event_queue;
#endif


/* Special macros facilitate running on Win 16 */
#if defined(XP_PC) && !defined(_WIN32)   /* then we are win 16 */ 

  /* 
   * Allocate the data passed to the callback functions from the heap...
   *
   * This inter-thread structure cannot reside on a thread stack since the 
   * thread's stack is swapped away with the thread under Win16...
   */

 #define ALLOC_OR_DEFINE(type, pointer_var_name, out_of_memory_return_value) \
         type * pointer_var_name = PORT_ZAlloc (sizeof(type));               \
         do {                                                                \
           if (!pointer_var_name)                                            \
             return (out_of_memory_return_value);                            \
         } while (0)   /* and now a semicolon can follow :-) */

 #define FREE_IF_ALLOC_IS_USED(pointer_var_name) PORT_Free(pointer_var_name)

#else /* not win 16... so we can alloc via auto variables */

 #define ALLOC_OR_DEFINE(type, pointer_var_name, out_of_memory_return_value) \
         type actual_structure_allocated_in_macro;                           \
         type * pointer_var_name = &actual_structure_allocated_in_macro;     \
         PORT_Memset (pointer_var_name, 0, sizeof (*pointer_var_name));      \
         ((void) 0) /* and now a semicolon can follow  */

 #define FREE_IF_ALLOC_IS_USED(pointer_var_name) ((void) 0)

#endif /* not Win 16 */

/* --- --- --- --- --- --- --- --- --- --- --- --- --- */

/*
 *  JAR_MOZ_encode
 *
 *  Call SEC_PKCS7Encode inside
 *  the mozilla thread
 *
 */

struct EVIL_encode
  {
  int error;
  SECStatus status;
  SEC_PKCS7ContentInfo *cinfo;
  SEC_PKCS7EncoderOutputCallback outputfn;
  void *outputarg;
  PK11SymKey *bulkkey;
  SECKEYGetPasswordKey pwfn;
  void *pwfnarg;
  };


/* This is called inside the mozilla thread */

PR_STATIC_CALLBACK(void) jar_moz_encode_fn (void *data)
  {
  SECStatus status;
  struct EVIL_encode *encode_data = (struct EVIL_encode *)data;

  PORT_SetError (encode_data->error);

  status = SEC_PKCS7Encode (encode_data->cinfo, encode_data->outputfn, 
                            encode_data->outputarg, encode_data->bulkkey, 
                            encode_data->pwfn, encode_data->pwfnarg);

  encode_data->status = status;
  encode_data->error = PORT_GetError();
  }


/* Wrapper for the ET_MOZ call */
 
SECStatus jar_moz_encode
      (
      SEC_PKCS7ContentInfo *cinfo,
      SEC_PKCS7EncoderOutputCallback  outputfn,
      void *outputarg,
      PK11SymKey *bulkkey,
      SECKEYGetPasswordKey pwfn,
      void *pwfnarg
      )
  {
  SECStatus ret;
  ALLOC_OR_DEFINE(struct EVIL_encode, encode_data, SECFailure);

  encode_data->error     = PORT_GetError();
  encode_data->cinfo     = cinfo;
  encode_data->outputfn  = outputfn;
  encode_data->outputarg = outputarg;
  encode_data->bulkkey   = bulkkey;
  encode_data->pwfn      = pwfn;
  encode_data->pwfnarg   = pwfnarg;

  /* Synchronously invoke the callback function on the mozilla thread. */
#ifdef MOZILLA_CLIENT
  if (mozilla_event_queue)
    ET_moz_CallFunction (jar_moz_encode_fn, encode_data);
  else
    jar_moz_encode_fn (encode_data);
#else
  jar_moz_encode_fn (encode_data);
#endif

  PORT_SetError (encode_data->error);
  ret = encode_data->status;

  /* Free the data passed to the callback function... */
  FREE_IF_ALLOC_IS_USED(encode_data);
  return ret;
  }

/* --- --- --- --- --- --- --- --- --- --- --- --- --- */

/*
 *  JAR_MOZ_verify
 *
 *  Call SEC_PKCS7VerifyDetachedSignature inside
 *  the mozilla thread
 *
 */

struct EVIL_verify
  {
  int error;
  SECStatus status;
  SEC_PKCS7ContentInfo *cinfo;
  SECCertUsage certusage;
  SECItem *detached_digest;
  HASH_HashType digest_type;
  PRBool keepcerts;
  };

/* This is called inside the mozilla thread */

PR_STATIC_CALLBACK(void) jar_moz_verify_fn (void *data)
  {
	PRBool result;
  struct EVIL_verify *verify_data = (struct EVIL_verify *)data;

  PORT_SetError (verify_data->error);

  result = SEC_PKCS7VerifyDetachedSignature
        (verify_data->cinfo, verify_data->certusage, verify_data->detached_digest, 
         verify_data->digest_type, verify_data->keepcerts);
	

  verify_data->status = result==PR_TRUE ? SECSuccess : SECFailure;
  verify_data->error = PORT_GetError();
  }


/* Wrapper for the ET_MOZ call */
 
SECStatus jar_moz_verify
      (
      SEC_PKCS7ContentInfo *cinfo,
      SECCertUsage certusage,
      SECItem *detached_digest,
      HASH_HashType digest_type,
      PRBool keepcerts
      )
  {
  SECStatus ret;
  ALLOC_OR_DEFINE(struct EVIL_verify, verify_data, SECFailure);

  verify_data->error           = PORT_GetError();
  verify_data->cinfo           = cinfo;
  verify_data->certusage       = certusage;
  verify_data->detached_digest = detached_digest;
  verify_data->digest_type     = digest_type;
  verify_data->keepcerts       = keepcerts;

  /* Synchronously invoke the callback function on the mozilla thread. */
#ifdef MOZILLA_CLIENT
  if (mozilla_event_queue)
    ET_moz_CallFunction (jar_moz_verify_fn, verify_data);
  else
    jar_moz_verify_fn (verify_data);
#else
  jar_moz_verify_fn (verify_data);
#endif

  PORT_SetError (verify_data->error);
  ret = verify_data->status;

  /* Free the data passed to the callback function... */
  FREE_IF_ALLOC_IS_USED(verify_data);
  return ret;
  }

/* --- --- --- --- --- --- --- --- --- --- --- --- --- */

/*
 *  JAR_MOZ_nickname
 *
 *  Call CERT_FindCertByNickname inside
 *  the mozilla thread
 *
 */

struct EVIL_nickname
  {
  int error;
  CERTCertDBHandle *certdb;
  char *nickname;
  CERTCertificate *cert;
  };


/* This is called inside the mozilla thread */

PR_STATIC_CALLBACK(void) jar_moz_nickname_fn (void *data)
  {
  CERTCertificate *cert;
  struct EVIL_nickname *nickname_data = (struct EVIL_nickname *)data;

  PORT_SetError (nickname_data->error);

  cert = CERT_FindCertByNickname (nickname_data->certdb, nickname_data->nickname);

  nickname_data->cert  = cert;
  nickname_data->error = PORT_GetError();
  }


/* Wrapper for the ET_MOZ call */
 
CERTCertificate *jar_moz_nickname (CERTCertDBHandle *certdb, char *nickname)
  {
  CERTCertificate *cert;
  ALLOC_OR_DEFINE(struct EVIL_nickname, nickname_data, NULL );

  nickname_data->error    = PORT_GetError();
  nickname_data->certdb   = certdb;
  nickname_data->nickname = nickname;

  /* Synchronously invoke the callback function on the mozilla thread. */
#ifdef MOZILLA_CLIENT
  if (mozilla_event_queue)
    ET_moz_CallFunction (jar_moz_nickname_fn, nickname_data);
  else
    jar_moz_nickname_fn (nickname_data);
#else
  jar_moz_nickname_fn (nickname_data);
#endif

  PORT_SetError (nickname_data->error);
  cert = nickname_data->cert;

  /* Free the data passed to the callback function... */
  FREE_IF_ALLOC_IS_USED(nickname_data);
  return cert;
  }

/* --- --- --- --- --- --- --- --- --- --- --- --- --- */

/*
 *  JAR_MOZ_perm
 *
 *  Call CERT_AddTempCertToPerm inside
 *  the mozilla thread
 *
 */

struct EVIL_perm
  {
  int error;
  SECStatus status;
  CERTCertificate *cert;
  char *nickname;
  CERTCertTrust *trust;
  };


/* This is called inside the mozilla thread */

PR_STATIC_CALLBACK(void) jar_moz_perm_fn (void *data)
  {
  SECStatus status;
  struct EVIL_perm *perm_data = (struct EVIL_perm *)data;

  PORT_SetError (perm_data->error);

  status = CERT_AddTempCertToPerm (perm_data->cert, perm_data->nickname, perm_data->trust);

  perm_data->status = status;
  perm_data->error = PORT_GetError();
  }


/* Wrapper for the ET_MOZ call */
 
SECStatus jar_moz_perm 
    (CERTCertificate *cert, char *nickname, CERTCertTrust *trust)
  {
  SECStatus ret;
  ALLOC_OR_DEFINE(struct EVIL_perm, perm_data, SECFailure);

  perm_data->error    = PORT_GetError();
  perm_data->cert     = cert;
  perm_data->nickname = nickname;
  perm_data->trust    = trust;

  /* Synchronously invoke the callback function on the mozilla thread. */
#ifdef MOZILLA_CLIENT
  if (mozilla_event_queue)
    ET_moz_CallFunction (jar_moz_perm_fn, perm_data);
  else
    jar_moz_perm_fn (perm_data);
#else
  jar_moz_perm_fn (perm_data);
#endif

  PORT_SetError (perm_data->error);
  ret = perm_data->status;

  /* Free the data passed to the callback function... */
  FREE_IF_ALLOC_IS_USED(perm_data);
  return ret;
  }

/* --- --- --- --- --- --- --- --- --- --- --- --- --- */

/*
 *  JAR_MOZ_certkey
 *
 *  Call CERT_FindCertByKey inside
 *  the mozilla thread
 *
 */

struct EVIL_certkey
  {
  int error;
  CERTCertificate *cert;
  CERTCertDBHandle *certdb;
  SECItem *seckey;
  };


/* This is called inside the mozilla thread */

PR_STATIC_CALLBACK(void) jar_moz_certkey_fn (void *data)
  {
  CERTCertificate *cert;
  struct EVIL_certkey *certkey_data = (struct EVIL_certkey *)data;

  PORT_SetError (certkey_data->error);

  cert = CERT_FindCertByKey (certkey_data->certdb, certkey_data->seckey);

  certkey_data->cert = cert;
  certkey_data->error = PORT_GetError();
  }


/* Wrapper for the ET_MOZ call */
 
CERTCertificate *jar_moz_certkey (CERTCertDBHandle *certdb, SECItem *seckey)
  {
  CERTCertificate *cert;
  ALLOC_OR_DEFINE(struct EVIL_certkey, certkey_data, NULL);

  certkey_data->error  = PORT_GetError();
  certkey_data->certdb = certdb;
  certkey_data->seckey = seckey;

  /* Synchronously invoke the callback function on the mozilla thread. */
#ifdef MOZILLA_CLIENT
  if (mozilla_event_queue)
    ET_moz_CallFunction (jar_moz_certkey_fn, certkey_data);
  else
    jar_moz_certkey_fn (certkey_data);
#else
  jar_moz_certkey_fn (certkey_data);
#endif

  PORT_SetError (certkey_data->error);
  cert = certkey_data->cert;

  /* Free the data passed to the callback function... */
  FREE_IF_ALLOC_IS_USED(certkey_data);
  return cert;
  }

/* --- --- --- --- --- --- --- --- --- --- --- --- --- */

/*
 *  JAR_MOZ_issuer
 *
 *  Call CERT_FindCertIssuer inside
 *  the mozilla thread
 *
 */

struct EVIL_issuer
  {
  int error;
  CERTCertificate *cert;
  CERTCertificate *issuer;
  };


/* This is called inside the mozilla thread */

PR_STATIC_CALLBACK(void) jar_moz_issuer_fn (void *data)
  {
  CERTCertificate *issuer;
  struct EVIL_issuer *issuer_data = (struct EVIL_issuer *)data;

  PORT_SetError (issuer_data->error);

  issuer = CERT_FindCertIssuer (issuer_data->cert, PR_Now(),
				certUsageObjectSigner);

  issuer_data->issuer = issuer;
  issuer_data->error = PORT_GetError();
  }


/* Wrapper for the ET_MOZ call */
 
CERTCertificate *jar_moz_issuer (CERTCertificate *cert)
  {
  CERTCertificate *issuer_cert;
  ALLOC_OR_DEFINE(struct EVIL_issuer, issuer_data, NULL);

  issuer_data->error = PORT_GetError();
  issuer_data->cert  = cert;

  /* Synchronously invoke the callback function on the mozilla thread. */
#ifdef MOZILLA_CLIENT
  if (mozilla_event_queue)
    ET_moz_CallFunction (jar_moz_issuer_fn, issuer_data);
  else
    jar_moz_issuer_fn (issuer_data);
#else
  jar_moz_issuer_fn (issuer_data);
#endif

  PORT_SetError (issuer_data->error);
  issuer_cert = issuer_data->issuer;

  /* Free the data passed to the callback function... */
  FREE_IF_ALLOC_IS_USED(issuer_data);
  return issuer_cert;
  }

/* --- --- --- --- --- --- --- --- --- --- --- --- --- */

/*
 *  JAR_MOZ_dup
 *
 *  Call CERT_DupCertificate inside
 *  the mozilla thread
 *
 */

struct EVIL_dup
  {
  int error;
  CERTCertificate *cert;
  CERTCertificate *return_cert;
  };


/* This is called inside the mozilla thread */

PR_STATIC_CALLBACK(void) jar_moz_dup_fn (void *data)
  {
  CERTCertificate *return_cert;
  struct EVIL_dup *dup_data = (struct EVIL_dup *)data;

  PORT_SetError (dup_data->error);

  return_cert = CERT_DupCertificate (dup_data->cert);

  dup_data->return_cert = return_cert;
  dup_data->error = PORT_GetError();
  }


/* Wrapper for the ET_MOZ call */
 
CERTCertificate *jar_moz_dup (CERTCertificate *cert)
  {
  CERTCertificate *dup_cert;
  ALLOC_OR_DEFINE(struct EVIL_dup, dup_data, NULL);

  dup_data->error = PORT_GetError();
  dup_data->cert  = cert;

  /* Synchronously invoke the callback function on the mozilla thread. */
#ifdef MOZILLA_CLIENT
  if (mozilla_event_queue)
    ET_moz_CallFunction (jar_moz_dup_fn, dup_data);
  else
    jar_moz_dup_fn (dup_data);
#else
  jar_moz_dup_fn (dup_data);
#endif

  PORT_SetError (dup_data->error);
  dup_cert = dup_data->return_cert;

  /* Free the data passed to the callback function... */
  FREE_IF_ALLOC_IS_USED(dup_data);
  return dup_cert;
  }

/* --- --- --- --- --- --- --- --- --- --- --- --- --- */