~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

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