1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14
* The Original Code is the Netscape security libraries.
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.
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.
35
* ***** END LICENSE BLOCK ***** */
37
* secport.c - portability interfaces for security libraries
39
* This file abstracts out libc functionality that libsec depends on
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
45
* $Id: nsprstub.c,v 1.6 2004/07/29 22:51:00 jpierre%netscape.com Exp $
58
unsigned long port_allocFailures;
60
/* locations for registering Unicode conversion functions.
61
* Is this the appropriate location? or should they be
62
* moved to client/server specific locations?
64
PORTCharConversionFunc ucs4Utf8ConvertFunc;
65
PORTCharConversionFunc ucs2Utf8ConvertFunc;
66
PORTCharConversionWSwapFunc ucs2AsciiConvertFunc;
69
PORT_Alloc(size_t bytes)
73
/* Always allocate a non-zero amount of bytes */
74
rv = (void *)malloc(bytes ? bytes : 1);
82
PORT_Realloc(void *oldptr, size_t bytes)
86
rv = (void *)realloc(oldptr, bytes);
94
PORT_ZAlloc(size_t bytes)
98
/* Always allocate a non-zero amount of bytes */
99
rv = (void *)calloc(1, bytes ? bytes : 1);
101
++port_allocFailures;
115
PORT_ZFree(void *ptr, size_t len)
123
/********************* Arena code follows *****************************/
127
PORT_NewArena(unsigned long chunksize)
131
arena = (PLArenaPool*)PORT_ZAlloc(sizeof(PLArenaPool));
132
if ( arena != NULL ) {
133
PR_InitArenaPool(arena, "security", chunksize, sizeof(double));
139
PORT_ArenaAlloc(PLArenaPool *arena, size_t size)
143
PL_ARENA_ALLOCATE(p, arena, size);
145
++port_allocFailures;
152
PORT_ArenaZAlloc(PLArenaPool *arena, size_t size)
156
PL_ARENA_ALLOCATE(p, arena, size);
158
++port_allocFailures;
160
PORT_Memset(p, 0, size);
166
/* need to zeroize!! */
168
PORT_FreeArena(PLArenaPool *arena, PRBool zero)
170
PR_FinishArenaPool(arena);
175
PORT_ArenaGrow(PLArenaPool *arena, void *ptr, size_t oldsize, size_t newsize)
177
PORT_Assert(newsize >= oldsize);
179
PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) );
185
PORT_ArenaMark(PLArenaPool *arena)
189
result = PL_ARENA_MARK(arena);
194
PORT_ArenaRelease(PLArenaPool *arena, void *mark)
196
PL_ARENA_RELEASE(arena, mark);
200
PORT_ArenaUnmark(PLArenaPool *arena, void *mark)
206
PORT_ArenaStrdup(PLArenaPool *arena,const char *str) {
207
int len = PORT_Strlen(str)+1;
210
newstr = (char*)PORT_ArenaAlloc(arena,len);
212
PORT_Memcpy(newstr,str,len);
219
* replace the nice thread-safe Error stack code with something
220
* that will work without all the NSPR features.
222
static PRInt32 stack[2] = {0, 0};
225
nss_SetError(PRUint32 value)
231
PR_IMPLEMENT(PRInt32)
238
PR_IMPLEMENT(PRInt32 *)
239
NSS_GetErrorStack(void)
245
nss_ClearErrorStack(void)
253
* replace the pointer tracking stuff for the same reasons.
254
* If you want to turn pointer tracking on, simply ifdef out this code and
255
* link with real NSPR.
257
PR_IMPLEMENT(PRStatus)
258
nssPointerTracker_initialize(nssPointerTracker *tracker)
264
PR_IMPLEMENT(PRStatus)
265
nssPointerTracker_finalize(nssPointerTracker *tracker)
270
PR_IMPLEMENT(PRStatus)
271
nssPointerTracker_add(nssPointerTracker *tracker, const void *pointer)
276
PR_IMPLEMENT(PRStatus)
277
nssPointerTracker_remove(nssPointerTracker *tracker, const void *pointer)
282
PR_IMPLEMENT(PRStatus)
283
nssPointerTracker_verify(nssPointerTracker *tracker, const void *pointer)
290
* Do not use NSPR stubs for MinGW because they can't resolve references
291
* to the _imp__PR_XXX symbols. This is merely an expedient hack and not
292
* the right solution.
294
#if !(defined(WIN32) && defined(__GNUC__))
295
PR_IMPLEMENT(PRThread *)
296
PR_GetCurrentThread(void)
298
return (PRThread *)1;
304
PR_Assert(const char *expr, const char *file, int line) {
309
PR_Alloc(PRUint32 bytes) { return malloc(bytes); }
312
PR_Malloc(PRUint32 bytes) { return malloc(bytes); }
315
PR_Calloc(PRUint32 blocks, PRUint32 bytes) { return calloc(blocks,bytes); }
318
PR_Realloc(void * blocks, PRUint32 bytes) { return realloc(blocks,bytes); }
321
PR_Free(void *ptr) { free(ptr); }
324
/* Old template; want to expunge it eventually. */
328
const SEC_ASN1Template SECOID_AlgorithmIDTemplate[] = {
330
0, NULL, sizeof(SECAlgorithmID) },
331
{ SEC_ASN1_OBJECT_ID,
332
offsetof(SECAlgorithmID,algorithm), },
333
{ SEC_ASN1_OPTIONAL | SEC_ASN1_ANY,
334
offsetof(SECAlgorithmID,parameters), },
338
PR_IMPLEMENT(PRStatus) PR_Sleep(PRIntervalTime ticks) { return PR_SUCCESS; }
340
/* This is not atomic! */
341
PR_IMPLEMENT(PRInt32) PR_AtomicDecrement(PRInt32 *val) { return --(*val); }
343
PR_IMPLEMENT(PRInt32) PR_AtomicSet(PRInt32 *val) { return ++(*val); }
347
/* now make the RNG happy */ /* This is not atomic! */
348
PR_IMPLEMENT(PRInt32) PR_AtomicIncrement(PRInt32 *val) { return ++(*val); }
349
#endif /* ! (WIN32 && GCC) */
351
static CK_C_INITIALIZE_ARGS_PTR nssstub_pInitArgs = NULL;
352
static CK_C_INITIALIZE_ARGS nssstub_initArgs;
353
static NSSArena *nssstub_arena = NULL;
354
static CryptokiLockingState nssstub_LockingState = SingleThreaded;
357
nssSetLockArgs(CK_C_INITIALIZE_ARGS_PTR pInitArgs, CryptokiLockingState* returned)
359
CK_ULONG count = (CK_ULONG)0;
360
CK_BBOOL os_ok = CK_FALSE;
362
if (nssstub_pInitArgs == NULL) {
363
if (pInitArgs != NULL) {
364
nssstub_initArgs = *pInitArgs;
365
nssstub_pInitArgs = &nssstub_initArgs;
366
if( (CK_CREATEMUTEX )NULL != pInitArgs->CreateMutex ) count++;
367
if( (CK_DESTROYMUTEX)NULL != pInitArgs->DestroyMutex ) count++;
368
if( (CK_LOCKMUTEX )NULL != pInitArgs->LockMutex ) count++;
369
if( (CK_UNLOCKMUTEX )NULL != pInitArgs->UnlockMutex ) count++;
370
os_ok = (pInitArgs->flags & CKF_OS_LOCKING_OK) ? CK_TRUE : CK_FALSE;
372
if( (0 != count) && (4 != count) ) {
373
rv = CKR_ARGUMENTS_BAD;
377
nssstub_pInitArgs = pInitArgs;
379
/* nssstub_arena = NSSArena_Create(); */
382
if( (0 == count) && (CK_TRUE == os_ok) ) {
384
* This is case #2 in the description of C_Initialize:
385
* The library will be called in a multithreaded way, but
386
* no routines were specified: os locking calls should be
387
* used. Unfortunately, this can be hard.. like, I think
388
* I may have to dynamically look up the entry points in
389
* the instance of NSPR already going in the application.
391
* I know that *we* always specify routines, so this only
392
* comes up if someone is using NSS to create their own
393
* PCKS#11 modules for other products. Oh, heck, I'll
394
* worry about this then.
402
* With the above test out of the way, we know this is case
403
* #1 in the description of C_Initialize: this library will
404
* not be called in a multithreaded way.
407
nssstub_LockingState = SingleThreaded;
410
* We know that we're in either case #3 or #4 in the description
411
* of C_Initialize. Case #3 says we should use the specified
412
* functions, case #4 cays we can use either the specified ones
413
* or the OS ones. I'll use the specified ones.
415
nssstub_LockingState = MultiThreaded;
419
*returned = nssstub_LockingState;
424
* Do not use NSPR stubs for MinGW because they can't resolve references
425
* to the _imp__PR_XXX symbols. This is merely an expedient hack and not
426
* the right solution.
428
#if !(defined(WIN32) && defined(__GNUC__))
430
PR_IMPLEMENT(PRLock *)
433
NSSCKFWMutex *mlock = NULL;
436
mlock = nssCKFWMutex_Create(nssstub_pInitArgs,nssstub_LockingState,nssstub_arena,&error);
437
lock = (PRLock *)mlock;
439
/* if we don't have a lock, nssCKFWMutex can deal with things */
440
if (lock == NULL) lock=(PRLock *) 1;
445
PR_DestroyLock(PRLock *lock) {
446
NSSCKFWMutex *mlock = (NSSCKFWMutex *)lock;
447
if (lock == (PRLock *)1) return;
448
nssCKFWMutex_Destroy(mlock);
452
PR_Lock(PRLock *lock) {
453
NSSCKFWMutex *mlock = (NSSCKFWMutex *)lock;
454
if (lock == (PRLock *)1) return;
455
nssCKFWMutex_Lock(mlock);
458
PR_IMPLEMENT(PRStatus)
459
PR_Unlock(PRLock *lock) {
460
NSSCKFWMutex *mlock = (NSSCKFWMutex *)lock;
461
if (lock == (PRLock *)1) return PR_SUCCESS;
462
nssCKFWMutex_Unlock(mlock);
468
/* this implementation is here to satisfy the PRMonitor use in plarena.c.
469
** It appears that it doesn't need re-entrant locks. It could have used
470
** PRLock instead of PRMonitor. So, this implementation just uses
471
** PRLock for a PRMonitor.
473
PR_IMPLEMENT(PRMonitor*)
476
return (PRMonitor *) PR_NewLock();
481
PR_EnterMonitor(PRMonitor *mon)
483
PR_Lock( (PRLock *)mon );
486
PR_IMPLEMENT(PRStatus)
487
PR_ExitMonitor(PRMonitor *mon)
489
return PR_Unlock( (PRLock *)mon );
494
/* This is NOT threadsafe. It is merely a pseudo-functional stub.
496
PR_IMPLEMENT(PRStatus) PR_CallOnce(
497
PRCallOnceType *once,
500
/* This is not really atomic! */
501
if (1 == PR_AtomicIncrement(&once->initialized)) {
502
once->status = (*func)();
504
/* Should wait to be sure that func has finished before returning. */
510
** Compute the log of the least power of 2 greater than or equal to n
512
PRIntn PR_CeilingLog2(PRUint32 i) {
514
PR_CEILING_LOG2(log2,i);
517
#endif /* ! (WIN32 && GCC) */
519
/********************** end of arena functions ***********************/