~ubuntu-branches/ubuntu/jaunty/xulrunner-1.9/jaunty

« back to all changes in this revision

Viewing changes to mozilla/security/nss/lib/ckfw/nsprstub.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabien Tassin, Sasa Bodiroza, Fabien Tassin, Alexander Sack
  • Date: 2008-09-02 11:54:00 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20080902115400-yfy26crvszpalvg5
Tags: 1.9.0.2+build3+nobinonly-0ubuntu1
[ Sasa Bodiroza ]
* In debian/rules:
  - Set 644 chmod to png files (LP: #252793) [Patch by Paolo Naldini]

[ Fabien Tassin ]
* improve create-build-system.sh to detect build-tree directory
  when embedded tarball is used. Fix un-escaped variables.
  Create build-system.tar.gz in the debian directory to prevent
  cdbs to check and unpack it during the build
  - update debian/create-build-system.sh
* Fix variables when an embedded tarball is used
  - update debian/rules
* Fix buffer overflow in realpath() at runtime and drop -U_FORTIFY_SOURCE
  from CPPFLAGS (LP: #263014)
  - add debian/patches/bz412610_att335369_realpath_overflow.patch
  - update debian/patches/series

[ Alexander Sack <asac@jwsdot.com> ]
* introduce preferred plugins by mime-type experimental feature;
  you can now set a pref to explicitly select a plugin to serve a particilar
  mime-type; patch contains further documentation.
  - add debian/patches/bzXXX_plugin_for_mimetype_pref.patch
  - update debian/patches/series
* drop patches applied upstream
  - drop bz120380_att326044.patch (fixed by bz442629)
  - update debian/patches/series

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 the Netscape security libraries.
15
 
 *
16
 
 * The Initial Developer of the Original Code is
17
 
 * Netscape Communications Corporation.
18
 
 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19
 
 * the Initial Developer. All Rights Reserved.
20
 
 *
21
 
 * Contributor(s):
22
 
 *
23
 
 * Alternatively, the contents of this file may be used under the terms of
24
 
 * either the GNU General Public License Version 2 or later (the "GPL"), or
25
 
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26
 
 * in which case the provisions of the GPL or the LGPL are applicable instead
27
 
 * of those above. If you wish to allow use of your version of this file only
28
 
 * under the terms of either the GPL or the LGPL, and not to allow others to
29
 
 * use your version of this file under the terms of the MPL, indicate your
30
 
 * decision by deleting the provisions above and replace them with the notice
31
 
 * and other provisions required by the GPL or the LGPL. If you do not delete
32
 
 * the provisions above, a recipient may use your version of this file under
33
 
 * the terms of any one of the MPL, the GPL or the LGPL.
34
 
 *
35
 
 * ***** END LICENSE BLOCK ***** */
36
 
/*
37
 
 * secport.c - portability interfaces for security libraries
38
 
 *
39
 
 * This file abstracts out libc functionality that libsec depends on
40
 
 * 
41
 
 * NOTE - These are not public interfaces. These stubs are to allow the 
42
 
 * SW FORTEZZA to link with some low level security functions without dragging
43
 
 * in NSPR.
44
 
 *
45
 
 * $Id: nsprstub.c,v 1.7 2007/07/24 16:14:15 biswatosh.chakraborty%sun.com Exp $
46
 
 */
47
 
 
48
 
#include "seccomon.h"
49
 
#include "prmem.h"
50
 
#include "prerror.h"
51
 
#include "plarena.h"
52
 
#include "secerr.h"
53
 
#include "prmon.h"
54
 
#include "prbit.h"
55
 
#include "ck.h"
56
 
 
57
 
#ifdef notdef
58
 
unsigned long port_allocFailures;
59
 
 
60
 
/* locations for registering Unicode conversion functions.  
61
 
 *  Is this the appropriate location?  or should they be
62
 
 *     moved to client/server specific locations?
63
 
 */
64
 
PORTCharConversionFunc ucs4Utf8ConvertFunc;
65
 
PORTCharConversionFunc ucs2Utf8ConvertFunc;
66
 
PORTCharConversionWSwapFunc  ucs2AsciiConvertFunc;
67
 
 
68
 
void *
69
 
PORT_Alloc(size_t bytes)
70
 
{
71
 
    void *rv;
72
 
 
73
 
    /* Always allocate a non-zero amount of bytes */
74
 
    rv = (void *)malloc(bytes ? bytes : 1);
75
 
    if (!rv) {
76
 
        ++port_allocFailures;
77
 
        PORT_SetError(SEC_ERROR_NO_MEMORY);
78
 
    }
79
 
    return rv;
80
 
}
81
 
 
82
 
void *
83
 
PORT_Realloc(void *oldptr, size_t bytes)
84
 
{
85
 
    void *rv;
86
 
 
87
 
    rv = (void *)realloc(oldptr, bytes);
88
 
    if (!rv) {
89
 
        ++port_allocFailures;
90
 
        PORT_SetError(SEC_ERROR_NO_MEMORY);
91
 
    }
92
 
    return rv;
93
 
}
94
 
 
95
 
void *
96
 
PORT_ZAlloc(size_t bytes)
97
 
{
98
 
    void *rv;
99
 
 
100
 
    /* Always allocate a non-zero amount of bytes */
101
 
    rv = (void *)calloc(1, bytes ? bytes : 1);
102
 
    if (!rv) {
103
 
        ++port_allocFailures;
104
 
        PORT_SetError(SEC_ERROR_NO_MEMORY);
105
 
    }
106
 
    return rv;
107
 
}
108
 
 
109
 
void
110
 
PORT_Free(void *ptr)
111
 
{
112
 
    if (ptr) {
113
 
        free(ptr);
114
 
    }
115
 
}
116
 
 
117
 
void
118
 
PORT_ZFree(void *ptr, size_t len)
119
 
{
120
 
    if (ptr) {
121
 
        memset(ptr, 0, len);
122
 
        free(ptr);
123
 
    }
124
 
}
125
 
 
126
 
/********************* Arena code follows *****************************/
127
 
 
128
 
 
129
 
PLArenaPool *
130
 
PORT_NewArena(unsigned long chunksize)
131
 
{
132
 
    PLArenaPool *arena;
133
 
    
134
 
    arena = (PLArenaPool*)PORT_ZAlloc(sizeof(PLArenaPool));
135
 
    if ( arena != NULL ) {
136
 
        PR_InitArenaPool(arena, "security", chunksize, sizeof(double));
137
 
    }
138
 
    return(arena);
139
 
}
140
 
 
141
 
void *
142
 
PORT_ArenaAlloc(PLArenaPool *arena, size_t size)
143
 
{
144
 
    void *p;
145
 
 
146
 
    PL_ARENA_ALLOCATE(p, arena, size);
147
 
    if (p == NULL) {
148
 
        ++port_allocFailures;
149
 
        PORT_SetError(SEC_ERROR_NO_MEMORY);
150
 
    }
151
 
 
152
 
    return(p);
153
 
}
154
 
 
155
 
void *
156
 
PORT_ArenaZAlloc(PLArenaPool *arena, size_t size)
157
 
{
158
 
    void *p;
159
 
 
160
 
    PL_ARENA_ALLOCATE(p, arena, size);
161
 
    if (p == NULL) {
162
 
        ++port_allocFailures;
163
 
        PORT_SetError(SEC_ERROR_NO_MEMORY);
164
 
    } else {
165
 
        PORT_Memset(p, 0, size);
166
 
    }
167
 
 
168
 
    return(p);
169
 
}
170
 
 
171
 
/* need to zeroize!! */
172
 
void
173
 
PORT_FreeArena(PLArenaPool *arena, PRBool zero)
174
 
{
175
 
    PR_FinishArenaPool(arena);
176
 
    PORT_Free(arena);
177
 
}
178
 
 
179
 
void *
180
 
PORT_ArenaGrow(PLArenaPool *arena, void *ptr, size_t oldsize, size_t newsize)
181
 
{
182
 
    PORT_Assert(newsize >= oldsize);
183
 
    
184
 
    PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) );
185
 
    
186
 
    if (ptr == NULL) { 
187
 
        ++port_allocFailures;
188
 
        PORT_SetError(SEC_ERROR_NO_MEMORY);
189
 
    }
190
 
    return(ptr);
191
 
}
192
 
 
193
 
void *
194
 
PORT_ArenaMark(PLArenaPool *arena)
195
 
{
196
 
    void * result;
197
 
 
198
 
    result = PL_ARENA_MARK(arena);
199
 
    return result;
200
 
}
201
 
 
202
 
void
203
 
PORT_ArenaRelease(PLArenaPool *arena, void *mark)
204
 
{
205
 
    PL_ARENA_RELEASE(arena, mark);
206
 
}
207
 
 
208
 
void
209
 
PORT_ArenaUnmark(PLArenaPool *arena, void *mark)
210
 
{
211
 
    /* do nothing */
212
 
}
213
 
 
214
 
char *
215
 
PORT_ArenaStrdup(PLArenaPool *arena,const char *str) {
216
 
    int len = PORT_Strlen(str)+1;
217
 
    char *newstr;
218
 
 
219
 
    newstr = (char*)PORT_ArenaAlloc(arena,len);
220
 
    if (newstr) {
221
 
        PORT_Memcpy(newstr,str,len);
222
 
    }
223
 
    return newstr;
224
 
}
225
 
#endif
226
 
 
227
 
/*
228
 
 * replace the nice thread-safe Error stack code with something
229
 
 * that will work without all the NSPR features.
230
 
 */
231
 
static PRInt32 stack[2] = {0, 0};
232
 
 
233
 
PR_IMPLEMENT(void)
234
 
nss_SetError(PRUint32 value)
235
 
{       
236
 
    stack[0] = value;
237
 
    return;
238
 
}
239
 
 
240
 
PR_IMPLEMENT(PRInt32)
241
 
NSS_GetError(void)
242
 
{
243
 
    return(stack[0]);
244
 
}
245
 
 
246
 
 
247
 
PR_IMPLEMENT(PRInt32 *)
248
 
NSS_GetErrorStack(void)
249
 
{
250
 
    return(&stack[0]);
251
 
}
252
 
 
253
 
PR_IMPLEMENT(void)
254
 
nss_ClearErrorStack(void)
255
 
{
256
 
    stack[0] = 0;
257
 
    return;
258
 
}
259
 
 
260
 
#ifdef DEBUG
261
 
/*
262
 
 * replace the pointer tracking stuff for the same reasons.
263
 
 *  If you want to turn pointer tracking on, simply ifdef out this code and 
264
 
 *  link with real NSPR.
265
 
 */
266
 
PR_IMPLEMENT(PRStatus)
267
 
nssPointerTracker_initialize(nssPointerTracker *tracker)
268
 
{
269
 
    return PR_SUCCESS;
270
 
}
271
 
 
272
 
 
273
 
PR_IMPLEMENT(PRStatus)
274
 
nssPointerTracker_finalize(nssPointerTracker *tracker)
275
 
{
276
 
    return PR_SUCCESS;
277
 
}
278
 
 
279
 
PR_IMPLEMENT(PRStatus)
280
 
nssPointerTracker_add(nssPointerTracker *tracker, const void *pointer)
281
 
{
282
 
     return PR_SUCCESS;
283
 
}
284
 
 
285
 
PR_IMPLEMENT(PRStatus)
286
 
nssPointerTracker_remove(nssPointerTracker *tracker, const void *pointer)
287
 
{
288
 
     return PR_SUCCESS;
289
 
}
290
 
 
291
 
PR_IMPLEMENT(PRStatus)
292
 
nssPointerTracker_verify(nssPointerTracker *tracker, const void *pointer)
293
 
{
294
 
     return PR_SUCCESS;
295
 
}
296
 
#endif
297
 
 
298
 
/*
299
 
 * Do not use NSPR stubs for MinGW because they can't resolve references
300
 
 * to the _imp__PR_XXX symbols.  This is merely an expedient hack and not
301
 
 * the right solution.
302
 
 */
303
 
#if !(defined(WIN32) && defined(__GNUC__))
304
 
PR_IMPLEMENT(PRThread *)
305
 
PR_GetCurrentThread(void)
306
 
{
307
 
     return (PRThread *)1;
308
 
}
309
 
 
310
 
 
311
 
 
312
 
PR_IMPLEMENT(void)
313
 
PR_Assert(const char *expr, const char *file, int line) {
314
 
    return; 
315
 
}
316
 
 
317
 
PR_IMPLEMENT(void *)
318
 
PR_Alloc(PRUint32 bytes) { return malloc(bytes); }
319
 
 
320
 
PR_IMPLEMENT(void *)
321
 
PR_Malloc(PRUint32 bytes) { return malloc(bytes); }
322
 
 
323
 
PR_IMPLEMENT(void *)
324
 
PR_Calloc(PRUint32 blocks, PRUint32 bytes) { return calloc(blocks,bytes); }
325
 
 
326
 
PR_IMPLEMENT(void *)
327
 
PR_Realloc(void * blocks, PRUint32 bytes) { return realloc(blocks,bytes); }
328
 
 
329
 
PR_IMPLEMENT(void)
330
 
PR_Free(void *ptr) { free(ptr); }
331
 
 
332
 
#ifdef notdef
333
 
/* Old template; want to expunge it eventually. */
334
 
#include "secasn1.h"
335
 
#include "secoid.h"
336
 
 
337
 
const SEC_ASN1Template SECOID_AlgorithmIDTemplate[] = {
338
 
    { SEC_ASN1_SEQUENCE,
339
 
          0, NULL, sizeof(SECAlgorithmID) },
340
 
    { SEC_ASN1_OBJECT_ID,
341
 
          offsetof(SECAlgorithmID,algorithm), },
342
 
    { SEC_ASN1_OPTIONAL | SEC_ASN1_ANY,
343
 
          offsetof(SECAlgorithmID,parameters), },
344
 
    { 0, }
345
 
};
346
 
 
347
 
PR_IMPLEMENT(PRStatus) PR_Sleep(PRIntervalTime ticks) { return PR_SUCCESS; }
348
 
 
349
 
/* This is not atomic! */
350
 
PR_IMPLEMENT(PRInt32) PR_AtomicDecrement(PRInt32 *val) { return --(*val); }
351
 
 
352
 
PR_IMPLEMENT(PRInt32) PR_AtomicSet(PRInt32 *val) { return ++(*val); }
353
 
 
354
 
#endif
355
 
 
356
 
/* now make the RNG happy */ /* This is not atomic! */
357
 
PR_IMPLEMENT(PRInt32) PR_AtomicIncrement(PRInt32 *val) { return ++(*val); }
358
 
#endif /* ! (WIN32 && GCC) */
359
 
 
360
 
static CK_C_INITIALIZE_ARGS_PTR nssstub_pInitArgs = NULL;
361
 
static CK_C_INITIALIZE_ARGS nssstub_initArgs;
362
 
static NSSArena *nssstub_arena = NULL;
363
 
static CryptokiLockingState nssstub_LockingState = SingleThreaded;
364
 
 
365
 
PR_IMPLEMENT(CK_RV)
366
 
nssSetLockArgs(CK_C_INITIALIZE_ARGS_PTR pInitArgs, CryptokiLockingState* returned)
367
 
{
368
 
    CK_ULONG count = (CK_ULONG)0;
369
 
    CK_BBOOL os_ok = CK_FALSE;
370
 
    CK_RV rv = CKR_OK;
371
 
    if (nssstub_pInitArgs == NULL) {
372
 
        if (pInitArgs != NULL) {
373
 
            nssstub_initArgs = *pInitArgs;
374
 
            nssstub_pInitArgs = &nssstub_initArgs;
375
 
            if( (CK_CREATEMUTEX )NULL != pInitArgs->CreateMutex  ) count++;
376
 
            if( (CK_DESTROYMUTEX)NULL != pInitArgs->DestroyMutex ) count++;
377
 
            if( (CK_LOCKMUTEX   )NULL != pInitArgs->LockMutex    ) count++;
378
 
            if( (CK_UNLOCKMUTEX )NULL != pInitArgs->UnlockMutex  ) count++;
379
 
            os_ok = (pInitArgs->flags & CKF_OS_LOCKING_OK) ? CK_TRUE : CK_FALSE;
380
 
 
381
 
            if( (0 != count) && (4 != count) ) {
382
 
                rv = CKR_ARGUMENTS_BAD;
383
 
                goto loser;
384
 
            }
385
 
        } else {
386
 
            nssstub_pInitArgs = pInitArgs;
387
 
        }
388
 
        /* nssstub_arena = NSSArena_Create(); */
389
 
    }
390
 
 
391
 
    if( (0 == count) && (CK_TRUE == os_ok) ) {
392
 
      /*
393
 
       * This is case #2 in the description of C_Initialize:
394
 
       * The library will be called in a multithreaded way, but
395
 
       * no routines were specified: os locking calls should be
396
 
       * used.  Unfortunately, this can be hard.. like, I think
397
 
       * I may have to dynamically look up the entry points in
398
 
       * the instance of NSPR already going in the application.
399
 
       *
400
 
       * I know that *we* always specify routines, so this only
401
 
       * comes up if someone is using NSS to create their own
402
 
       * PCKS#11 modules for other products.  Oh, heck, I'll 
403
 
       * worry about this then.
404
 
       */
405
 
      rv = CKR_CANT_LOCK;
406
 
      goto loser;
407
 
    }
408
 
 
409
 
    if( 0 == count ) {
410
 
      /*
411
 
       * With the above test out of the way, we know this is case
412
 
       * #1 in the description of C_Initialize: this library will
413
 
       * not be called in a multithreaded way.
414
 
       */
415
 
 
416
 
      nssstub_LockingState = SingleThreaded;
417
 
    } else {
418
 
      /*
419
 
       * We know that we're in either case #3 or #4 in the description
420
 
       * of C_Initialize.  Case #3 says we should use the specified
421
 
       * functions, case #4 cays we can use either the specified ones
422
 
       * or the OS ones.  I'll use the specified ones.
423
 
       */
424
 
      nssstub_LockingState = MultiThreaded;
425
 
    }
426
 
 
427
 
    loser:
428
 
    *returned = nssstub_LockingState;
429
 
    return rv;
430
 
}
431
 
 
432
 
/*
433
 
 * Do not use NSPR stubs for MinGW because they can't resolve references
434
 
 * to the _imp__PR_XXX symbols.  This is merely an expedient hack and not
435
 
 * the right solution.
436
 
 */
437
 
#if !(defined(WIN32) && defined(__GNUC__))
438
 
#include "prlock.h"
439
 
PR_IMPLEMENT(PRLock *)
440
 
PR_NewLock(void) {
441
 
        PRLock *lock = NULL;
442
 
        NSSCKFWMutex *mlock = NULL;
443
 
        CK_RV error;
444
 
 
445
 
        mlock = nssCKFWMutex_Create(nssstub_pInitArgs,nssstub_LockingState,nssstub_arena,&error);
446
 
        lock = (PRLock *)mlock;
447
 
 
448
 
        /* if we don't have a lock, nssCKFWMutex can deal with things */
449
 
        if (lock == NULL) lock=(PRLock *) 1;
450
 
        return lock;
451
 
}
452
 
 
453
 
PR_IMPLEMENT(void) 
454
 
PR_DestroyLock(PRLock *lock) {
455
 
        NSSCKFWMutex *mlock = (NSSCKFWMutex *)lock;
456
 
        if (lock == (PRLock *)1) return;
457
 
        nssCKFWMutex_Destroy(mlock);
458
 
}
459
 
 
460
 
PR_IMPLEMENT(void) 
461
 
PR_Lock(PRLock *lock) {
462
 
        NSSCKFWMutex *mlock = (NSSCKFWMutex *)lock;
463
 
        if (lock == (PRLock *)1) return;
464
 
        nssCKFWMutex_Lock(mlock);
465
 
}
466
 
 
467
 
PR_IMPLEMENT(PRStatus) 
468
 
PR_Unlock(PRLock *lock) {
469
 
        NSSCKFWMutex *mlock = (NSSCKFWMutex *)lock;
470
 
        if (lock == (PRLock *)1) return PR_SUCCESS;
471
 
        nssCKFWMutex_Unlock(mlock);
472
 
        return PR_SUCCESS;
473
 
}
474
 
 
475
 
#ifdef notdef
476
 
#endif
477
 
/* this implementation is here to satisfy the PRMonitor use in plarena.c.
478
 
** It appears that it doesn't need re-entrant locks.  It could have used
479
 
** PRLock instead of PRMonitor.  So, this implementation just uses 
480
 
** PRLock for a PRMonitor.
481
 
*/
482
 
PR_IMPLEMENT(PRMonitor*) 
483
 
PR_NewMonitor(void)
484
 
{
485
 
    return (PRMonitor *) PR_NewLock();
486
 
}
487
 
 
488
 
 
489
 
PR_IMPLEMENT(void) 
490
 
PR_EnterMonitor(PRMonitor *mon)
491
 
{
492
 
    PR_Lock( (PRLock *)mon );
493
 
}
494
 
 
495
 
PR_IMPLEMENT(PRStatus) 
496
 
PR_ExitMonitor(PRMonitor *mon)
497
 
{
498
 
    return PR_Unlock( (PRLock *)mon );
499
 
}
500
 
 
501
 
#include "prinit.h"
502
 
 
503
 
/* This is NOT threadsafe.  It is merely a pseudo-functional stub.
504
 
*/
505
 
PR_IMPLEMENT(PRStatus) PR_CallOnce(
506
 
    PRCallOnceType *once,
507
 
    PRCallOnceFN    func)
508
 
{
509
 
    /* This is not really atomic! */
510
 
    if (1 == PR_AtomicIncrement(&once->initialized)) {
511
 
        once->status = (*func)();
512
 
    }  else {
513
 
        /* Should wait to be sure that func has finished before returning. */
514
 
    }
515
 
    return once->status;
516
 
}
517
 
 
518
 
/*
519
 
** Compute the log of the least power of 2 greater than or equal to n
520
 
*/
521
 
PRIntn PR_CeilingLog2(PRUint32 i) {
522
 
        PRIntn log2;
523
 
        PR_CEILING_LOG2(log2,i);
524
 
        return log2;
525
 
}
526
 
#endif /* ! (WIN32 && GCC) */
527
 
 
528
 
/********************** end of arena functions ***********************/
529