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/
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.
12
* The Original Code is the Netscape security libraries.
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
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
34
* secport.c - portability interfaces for security libraries
36
* This file abstracts out libc functionality that libsec depends on
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
42
* $Id: nsprstub.c,v 1.4.22.1 2004/10/15 21:13:51 wchang0222%aol.com Exp $
55
unsigned long port_allocFailures;
57
/* locations for registering Unicode conversion functions.
58
* Is this the appropriate location? or should they be
59
* moved to client/server specific locations?
61
PORTCharConversionFunc ucs4Utf8ConvertFunc;
62
PORTCharConversionFunc ucs2Utf8ConvertFunc;
63
PORTCharConversionWSwapFunc ucs2AsciiConvertFunc;
66
PORT_Alloc(size_t bytes)
70
/* Always allocate a non-zero amount of bytes */
71
rv = (void *)malloc(bytes ? bytes : 1);
79
PORT_Realloc(void *oldptr, size_t bytes)
83
rv = (void *)realloc(oldptr, bytes);
91
PORT_ZAlloc(size_t bytes)
95
/* Always allocate a non-zero amount of bytes */
96
rv = (void *)calloc(1, bytes ? bytes : 1);
112
PORT_ZFree(void *ptr, size_t len)
120
/********************* Arena code follows *****************************/
124
PORT_NewArena(unsigned long chunksize)
128
arena = (PLArenaPool*)PORT_ZAlloc(sizeof(PLArenaPool));
129
if ( arena != NULL ) {
130
PR_InitArenaPool(arena, "security", chunksize, sizeof(double));
136
PORT_ArenaAlloc(PLArenaPool *arena, size_t size)
140
PL_ARENA_ALLOCATE(p, arena, size);
142
++port_allocFailures;
149
PORT_ArenaZAlloc(PLArenaPool *arena, size_t size)
153
PL_ARENA_ALLOCATE(p, arena, size);
155
++port_allocFailures;
157
PORT_Memset(p, 0, size);
163
/* need to zeroize!! */
165
PORT_FreeArena(PLArenaPool *arena, PRBool zero)
167
PR_FinishArenaPool(arena);
172
PORT_ArenaGrow(PLArenaPool *arena, void *ptr, size_t oldsize, size_t newsize)
174
PORT_Assert(newsize >= oldsize);
176
PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) );
182
PORT_ArenaMark(PLArenaPool *arena)
186
result = PL_ARENA_MARK(arena);
191
PORT_ArenaRelease(PLArenaPool *arena, void *mark)
193
PL_ARENA_RELEASE(arena, mark);
197
PORT_ArenaUnmark(PLArenaPool *arena, void *mark)
203
PORT_ArenaStrdup(PLArenaPool *arena,const char *str) {
204
int len = PORT_Strlen(str)+1;
207
newstr = (char*)PORT_ArenaAlloc(arena,len);
209
PORT_Memcpy(newstr,str,len);
216
* replace the nice thread-safe Error stack code with something
217
* that will work without all the NSPR features.
219
static PRInt32 stack[2] = {0, 0};
222
nss_SetError(PRUint32 value)
228
PR_IMPLEMENT(PRInt32)
235
PR_IMPLEMENT(PRInt32 *)
236
NSS_GetErrorStack(void)
242
nss_ClearErrorStack(void)
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.
254
PR_IMPLEMENT(PRStatus)
255
nssPointerTracker_initialize(nssPointerTracker *tracker)
261
PR_IMPLEMENT(PRStatus)
262
nssPointerTracker_finalize(nssPointerTracker *tracker)
267
PR_IMPLEMENT(PRStatus)
268
nssPointerTracker_add(nssPointerTracker *tracker, const void *pointer)
273
PR_IMPLEMENT(PRStatus)
274
nssPointerTracker_remove(nssPointerTracker *tracker, const void *pointer)
279
PR_IMPLEMENT(PRStatus)
280
nssPointerTracker_verify(nssPointerTracker *tracker, const void *pointer)
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.
291
#if !(defined(WIN32) && defined(__GNUC__))
292
PR_IMPLEMENT(PRThread *)
293
PR_GetCurrentThread(void)
295
return (PRThread *)1;
301
PR_Assert(const char *expr, const char *file, int line) {
306
PR_Alloc(PRUint32 bytes) { return malloc(bytes); }
309
PR_Malloc(PRUint32 bytes) { return malloc(bytes); }
312
PR_Calloc(PRUint32 blocks, PRUint32 bytes) { return calloc(blocks,bytes); }
315
PR_Realloc(void * blocks, PRUint32 bytes) { return realloc(blocks,bytes); }
318
PR_Free(void *ptr) { free(ptr); }
321
/* Old template; want to expunge it eventually. */
325
const SEC_ASN1Template SECOID_AlgorithmIDTemplate[] = {
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), },
335
PR_IMPLEMENT(PRStatus) PR_Sleep(PRIntervalTime ticks) { return PR_SUCCESS; }
337
/* This is not atomic! */
338
PR_IMPLEMENT(PRInt32) PR_AtomicDecrement(PRInt32 *val) { return --(*val); }
340
PR_IMPLEMENT(PRInt32) PR_AtomicSet(PRInt32 *val) { return ++(*val); }
344
/* now make the RNG happy */ /* This is not atomic! */
345
PR_IMPLEMENT(PRInt32) PR_AtomicIncrement(PRInt32 *val) { return ++(*val); }
346
#endif /* ! (WIN32 && GCC) */
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;
354
nssSetLockArgs(CK_C_INITIALIZE_ARGS_PTR pInitArgs, CryptokiLockingState* returned)
356
CK_ULONG count = (CK_ULONG)0;
357
CK_BBOOL os_ok = CK_FALSE;
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;
369
if( (0 != count) && (4 != count) ) {
370
rv = CKR_ARGUMENTS_BAD;
374
nssstub_pInitArgs = pInitArgs;
376
/* nssstub_arena = NSSArena_Create(); */
379
if( (0 == count) && (CK_TRUE == os_ok) ) {
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.
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.
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.
404
nssstub_LockingState = SingleThreaded;
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.
412
nssstub_LockingState = MultiThreaded;
416
*returned = nssstub_LockingState;
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.
425
#if !(defined(WIN32) && defined(__GNUC__))
427
PR_IMPLEMENT(PRLock *)
430
NSSCKFWMutex *mlock = NULL;
433
mlock = nssCKFWMutex_Create(nssstub_pInitArgs,nssstub_LockingState,nssstub_arena,&error);
434
lock = (PRLock *)mlock;
436
/* if we don't have a lock, nssCKFWMutex can deal with things */
437
if (lock == NULL) lock=(PRLock *) 1;
442
PR_DestroyLock(PRLock *lock) {
443
NSSCKFWMutex *mlock = (NSSCKFWMutex *)lock;
444
if (lock == (PRLock *)1) return;
445
nssCKFWMutex_Destroy(mlock);
449
PR_Lock(PRLock *lock) {
450
NSSCKFWMutex *mlock = (NSSCKFWMutex *)lock;
451
if (lock == (PRLock *)1) return;
452
nssCKFWMutex_Lock(mlock);
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);
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.
470
PR_IMPLEMENT(PRMonitor*)
473
return (PRMonitor *) PR_NewLock();
478
PR_EnterMonitor(PRMonitor *mon)
480
PR_Lock( (PRLock *)mon );
483
PR_IMPLEMENT(PRStatus)
484
PR_ExitMonitor(PRMonitor *mon)
486
return PR_Unlock( (PRLock *)mon );
491
/* This is NOT threadsafe. It is merely a pseudo-functional stub.
493
PR_IMPLEMENT(PRStatus) PR_CallOnce(
494
PRCallOnceType *once,
497
/* This is not really atomic! */
498
if (1 == PR_AtomicIncrement(&once->initialized)) {
499
once->status = (*func)();
501
/* Should wait to be sure that func has finished before returning. */
507
** Compute the log of the least power of 2 greater than or equal to n
509
PRIntn PR_CeilingLog2(PRUint32 i) {
511
PR_CEILING_LOG2(log2,i);
514
#endif /* ! (WIN32 && GCC) */
516
/********************** end of arena functions ***********************/