Back to index

lightning-sunbird  0.9+nobinonly
nsprstub.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 /*
00037  * secport.c - portability interfaces for security libraries
00038  *
00039  * This file abstracts out libc functionality that libsec depends on
00040  * 
00041  * NOTE - These are not public interfaces. These stubs are to allow the 
00042  * SW FORTEZZA to link with some low level security functions without dragging
00043  * in NSPR.
00044  *
00045  * $Id: nsprstub.c,v 1.6.28.1 2007/07/26 06:47:36 biswatosh.chakraborty%sun.com Exp $
00046  */
00047 
00048 #include "seccomon.h"
00049 #include "prmem.h"
00050 #include "prerror.h"
00051 #include "plarena.h"
00052 #include "secerr.h"
00053 #include "prmon.h"
00054 #include "prbit.h"
00055 #include "ck.h"
00056 
00057 #ifdef notdef
00058 unsigned long port_allocFailures;
00059 
00060 /* locations for registering Unicode conversion functions.  
00061  *  Is this the appropriate location?  or should they be
00062  *     moved to client/server specific locations?
00063  */
00064 PORTCharConversionFunc ucs4Utf8ConvertFunc;
00065 PORTCharConversionFunc ucs2Utf8ConvertFunc;
00066 PORTCharConversionWSwapFunc  ucs2AsciiConvertFunc;
00067 
00068 void *
00069 PORT_Alloc(size_t bytes)
00070 {
00071     void *rv;
00072 
00073     /* Always allocate a non-zero amount of bytes */
00074     rv = (void *)malloc(bytes ? bytes : 1);
00075     if (!rv) {
00076        ++port_allocFailures;
00077        PORT_SetError(SEC_ERROR_NO_MEMORY);
00078     }
00079     return rv;
00080 }
00081 
00082 void *
00083 PORT_Realloc(void *oldptr, size_t bytes)
00084 {
00085     void *rv;
00086 
00087     rv = (void *)realloc(oldptr, bytes);
00088     if (!rv) {
00089        ++port_allocFailures;
00090        PORT_SetError(SEC_ERROR_NO_MEMORY);
00091     }
00092     return rv;
00093 }
00094 
00095 void *
00096 PORT_ZAlloc(size_t bytes)
00097 {
00098     void *rv;
00099 
00100     /* Always allocate a non-zero amount of bytes */
00101     rv = (void *)calloc(1, bytes ? bytes : 1);
00102     if (!rv) {
00103        ++port_allocFailures;
00104        PORT_SetError(SEC_ERROR_NO_MEMORY);
00105     }
00106     return rv;
00107 }
00108 
00109 void
00110 PORT_Free(void *ptr)
00111 {
00112     if (ptr) {
00113        free(ptr);
00114     }
00115 }
00116 
00117 void
00118 PORT_ZFree(void *ptr, size_t len)
00119 {
00120     if (ptr) {
00121        memset(ptr, 0, len);
00122        free(ptr);
00123     }
00124 }
00125 
00126 /********************* Arena code follows *****************************/
00127 
00128 
00129 PLArenaPool *
00130 PORT_NewArena(unsigned long chunksize)
00131 {
00132     PLArenaPool *arena;
00133     
00134     arena = (PLArenaPool*)PORT_ZAlloc(sizeof(PLArenaPool));
00135     if ( arena != NULL ) {
00136        PR_InitArenaPool(arena, "security", chunksize, sizeof(double));
00137     }
00138     return(arena);
00139 }
00140 
00141 void *
00142 PORT_ArenaAlloc(PLArenaPool *arena, size_t size)
00143 {
00144     void *p;
00145 
00146     PL_ARENA_ALLOCATE(p, arena, size);
00147     if (p == NULL) {
00148        ++port_allocFailures;
00149        PORT_SetError(SEC_ERROR_NO_MEMORY);
00150     }
00151 
00152     return(p);
00153 }
00154 
00155 void *
00156 PORT_ArenaZAlloc(PLArenaPool *arena, size_t size)
00157 {
00158     void *p;
00159 
00160     PL_ARENA_ALLOCATE(p, arena, size);
00161     if (p == NULL) {
00162        ++port_allocFailures;
00163        PORT_SetError(SEC_ERROR_NO_MEMORY);
00164     } else {
00165        PORT_Memset(p, 0, size);
00166     }
00167 
00168     return(p);
00169 }
00170 
00171 /* need to zeroize!! */
00172 void
00173 PORT_FreeArena(PLArenaPool *arena, PRBool zero)
00174 {
00175     PR_FinishArenaPool(arena);
00176     PORT_Free(arena);
00177 }
00178 
00179 void *
00180 PORT_ArenaGrow(PLArenaPool *arena, void *ptr, size_t oldsize, size_t newsize)
00181 {
00182     PORT_Assert(newsize >= oldsize);
00183     
00184     PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) );
00185     if (ptr == NULL) { 
00186         ++port_allocFailures;
00187         PORT_SetError(SEC_ERROR_NO_MEMORY);
00188     }   
00189 
00190     return(ptr);
00191 }
00192 
00193 void *
00194 PORT_ArenaMark(PLArenaPool *arena)
00195 {
00196     void * result;
00197 
00198     result = PL_ARENA_MARK(arena);
00199     return result;
00200 }
00201 
00202 void
00203 PORT_ArenaRelease(PLArenaPool *arena, void *mark)
00204 {
00205     PL_ARENA_RELEASE(arena, mark);
00206 }
00207 
00208 void
00209 PORT_ArenaUnmark(PLArenaPool *arena, void *mark)
00210 {
00211     /* do nothing */
00212 }
00213 
00214 char *
00215 PORT_ArenaStrdup(PLArenaPool *arena,const char *str) {
00216     int len = PORT_Strlen(str)+1;
00217     char *newstr;
00218 
00219     newstr = (char*)PORT_ArenaAlloc(arena,len);
00220     if (newstr) {
00221         PORT_Memcpy(newstr,str,len);
00222     }
00223     return newstr;
00224 }
00225 #endif
00226 
00227 /*
00228  * replace the nice thread-safe Error stack code with something
00229  * that will work without all the NSPR features.
00230  */
00231 static PRInt32 stack[2] = {0, 0};
00232 
00233 PR_IMPLEMENT(void)
00234 nss_SetError(PRUint32 value)
00235 {      
00236     stack[0] = value;
00237     return;
00238 }
00239 
00240 PR_IMPLEMENT(PRInt32)
00241 NSS_GetError(void)
00242 {
00243     return(stack[0]);
00244 }
00245 
00246 
00247 PR_IMPLEMENT(PRInt32 *)
00248 NSS_GetErrorStack(void)
00249 {
00250     return(&stack[0]);
00251 }
00252 
00253 PR_IMPLEMENT(void)
00254 nss_ClearErrorStack(void)
00255 {
00256     stack[0] = 0;
00257     return;
00258 }
00259 
00260 #ifdef DEBUG
00261 /*
00262  * replace the pointer tracking stuff for the same reasons.
00263  *  If you want to turn pointer tracking on, simply ifdef out this code and 
00264  *  link with real NSPR.
00265  */
00266 PR_IMPLEMENT(PRStatus)
00267 nssPointerTracker_initialize(nssPointerTracker *tracker)
00268 {
00269     return PR_SUCCESS;
00270 }
00271 
00272 
00273 PR_IMPLEMENT(PRStatus)
00274 nssPointerTracker_finalize(nssPointerTracker *tracker)
00275 {
00276     return PR_SUCCESS;
00277 }
00278 
00279 PR_IMPLEMENT(PRStatus)
00280 nssPointerTracker_add(nssPointerTracker *tracker, const void *pointer)
00281 {
00282      return PR_SUCCESS;
00283 }
00284 
00285 PR_IMPLEMENT(PRStatus)
00286 nssPointerTracker_remove(nssPointerTracker *tracker, const void *pointer)
00287 {
00288      return PR_SUCCESS;
00289 }
00290 
00291 PR_IMPLEMENT(PRStatus)
00292 nssPointerTracker_verify(nssPointerTracker *tracker, const void *pointer)
00293 {
00294      return PR_SUCCESS;
00295 }
00296 #endif
00297 
00298 /*
00299  * Do not use NSPR stubs for MinGW because they can't resolve references
00300  * to the _imp__PR_XXX symbols.  This is merely an expedient hack and not
00301  * the right solution.
00302  */
00303 #if !(defined(WIN32) && defined(__GNUC__))
00304 PR_IMPLEMENT(PRThread *)
00305 PR_GetCurrentThread(void)
00306 {
00307      return (PRThread *)1;
00308 }
00309 
00310 
00311 
00312 PR_IMPLEMENT(void)
00313 PR_Assert(const char *expr, const char *file, int line) {
00314     return; 
00315 }
00316 
00317 PR_IMPLEMENT(void *)
00318 PR_Alloc(PRUint32 bytes) { return malloc(bytes); }
00319 
00320 PR_IMPLEMENT(void *)
00321 PR_Malloc(PRUint32 bytes) { return malloc(bytes); }
00322 
00323 PR_IMPLEMENT(void *)
00324 PR_Calloc(PRUint32 blocks, PRUint32 bytes) { return calloc(blocks,bytes); }
00325 
00326 PR_IMPLEMENT(void *)
00327 PR_Realloc(void * blocks, PRUint32 bytes) { return realloc(blocks,bytes); }
00328 
00329 PR_IMPLEMENT(void)
00330 PR_Free(void *ptr) { free(ptr); }
00331 
00332 #ifdef notdef
00333 /* Old template; want to expunge it eventually. */
00334 #include "secasn1.h"
00335 #include "secoid.h"
00336 
00337 const SEC_ASN1Template SECOID_AlgorithmIDTemplate[] = {
00338     { SEC_ASN1_SEQUENCE,
00339          0, NULL, sizeof(SECAlgorithmID) },
00340     { SEC_ASN1_OBJECT_ID,
00341          offsetof(SECAlgorithmID,algorithm), },
00342     { SEC_ASN1_OPTIONAL | SEC_ASN1_ANY,
00343          offsetof(SECAlgorithmID,parameters), },
00344     { 0, }
00345 };
00346 
00347 PR_IMPLEMENT(PRStatus) PR_Sleep(PRIntervalTime ticks) { return PR_SUCCESS; }
00348 
00349 /* This is not atomic! */
00350 PR_IMPLEMENT(PRInt32) PR_AtomicDecrement(PRInt32 *val) { return --(*val); }
00351 
00352 PR_IMPLEMENT(PRInt32) PR_AtomicSet(PRInt32 *val) { return ++(*val); }
00353 
00354 #endif
00355 
00356 /* now make the RNG happy */ /* This is not atomic! */
00357 PR_IMPLEMENT(PRInt32) PR_AtomicIncrement(PRInt32 *val) { return ++(*val); }
00358 #endif /* ! (WIN32 && GCC) */
00359 
00360 static CK_C_INITIALIZE_ARGS_PTR nssstub_pInitArgs = NULL;
00361 static CK_C_INITIALIZE_ARGS nssstub_initArgs;
00362 static NSSArena *nssstub_arena = NULL;
00363 static CryptokiLockingState nssstub_LockingState = SingleThreaded;
00364 
00365 PR_IMPLEMENT(CK_RV)
00366 nssSetLockArgs(CK_C_INITIALIZE_ARGS_PTR pInitArgs, CryptokiLockingState* returned)
00367 {
00368     CK_ULONG count = (CK_ULONG)0;
00369     CK_BBOOL os_ok = CK_FALSE;
00370     CK_RV rv = CKR_OK;
00371     if (nssstub_pInitArgs == NULL) {
00372        if (pInitArgs != NULL) {
00373            nssstub_initArgs = *pInitArgs;
00374            nssstub_pInitArgs = &nssstub_initArgs;
00375             if( (CK_CREATEMUTEX )NULL != pInitArgs->CreateMutex  ) count++;
00376             if( (CK_DESTROYMUTEX)NULL != pInitArgs->DestroyMutex ) count++;
00377             if( (CK_LOCKMUTEX   )NULL != pInitArgs->LockMutex    ) count++;
00378             if( (CK_UNLOCKMUTEX )NULL != pInitArgs->UnlockMutex  ) count++;
00379             os_ok = (pInitArgs->flags & CKF_OS_LOCKING_OK) ? CK_TRUE : CK_FALSE;
00380 
00381             if( (0 != count) && (4 != count) ) {
00382                 rv = CKR_ARGUMENTS_BAD;
00383                 goto loser;
00384             }
00385        } else {
00386            nssstub_pInitArgs = pInitArgs;
00387        }
00388        /* nssstub_arena = NSSArena_Create(); */
00389     }
00390 
00391     if( (0 == count) && (CK_TRUE == os_ok) ) {
00392       /*
00393        * This is case #2 in the description of C_Initialize:
00394        * The library will be called in a multithreaded way, but
00395        * no routines were specified: os locking calls should be
00396        * used.  Unfortunately, this can be hard.. like, I think
00397        * I may have to dynamically look up the entry points in
00398        * the instance of NSPR already going in the application.
00399        *
00400        * I know that *we* always specify routines, so this only
00401        * comes up if someone is using NSS to create their own
00402        * PCKS#11 modules for other products.  Oh, heck, I'll 
00403        * worry about this then.
00404        */
00405       rv = CKR_CANT_LOCK;
00406       goto loser;
00407     }
00408 
00409     if( 0 == count ) {
00410       /*
00411        * With the above test out of the way, we know this is case
00412        * #1 in the description of C_Initialize: this library will
00413        * not be called in a multithreaded way.
00414        */
00415 
00416       nssstub_LockingState = SingleThreaded;
00417     } else {
00418       /*
00419        * We know that we're in either case #3 or #4 in the description
00420        * of C_Initialize.  Case #3 says we should use the specified
00421        * functions, case #4 cays we can use either the specified ones
00422        * or the OS ones.  I'll use the specified ones.
00423        */
00424       nssstub_LockingState = MultiThreaded;
00425     }
00426 
00427     loser:
00428     *returned = nssstub_LockingState;
00429     return rv;
00430 }
00431 
00432 /*
00433  * Do not use NSPR stubs for MinGW because they can't resolve references
00434  * to the _imp__PR_XXX symbols.  This is merely an expedient hack and not
00435  * the right solution.
00436  */
00437 #if !(defined(WIN32) && defined(__GNUC__))
00438 #include "prlock.h"
00439 PR_IMPLEMENT(PRLock *)
00440 PR_NewLock(void) {
00441        PRLock *lock = NULL;
00442        NSSCKFWMutex *mlock = NULL;
00443        CK_RV error;
00444 
00445        mlock = nssCKFWMutex_Create(nssstub_pInitArgs,nssstub_LockingState,nssstub_arena,&error);
00446        lock = (PRLock *)mlock;
00447 
00448        /* if we don't have a lock, nssCKFWMutex can deal with things */
00449        if (lock == NULL) lock=(PRLock *) 1;
00450        return lock;
00451 }
00452 
00453 PR_IMPLEMENT(void) 
00454 PR_DestroyLock(PRLock *lock) {
00455        NSSCKFWMutex *mlock = (NSSCKFWMutex *)lock;
00456        if (lock == (PRLock *)1) return;
00457        nssCKFWMutex_Destroy(mlock);
00458 }
00459 
00460 PR_IMPLEMENT(void) 
00461 PR_Lock(PRLock *lock) {
00462        NSSCKFWMutex *mlock = (NSSCKFWMutex *)lock;
00463        if (lock == (PRLock *)1) return;
00464        nssCKFWMutex_Lock(mlock);
00465 }
00466 
00467 PR_IMPLEMENT(PRStatus) 
00468 PR_Unlock(PRLock *lock) {
00469        NSSCKFWMutex *mlock = (NSSCKFWMutex *)lock;
00470        if (lock == (PRLock *)1) return PR_SUCCESS;
00471        nssCKFWMutex_Unlock(mlock);
00472        return PR_SUCCESS;
00473 }
00474 
00475 #ifdef notdef
00476 #endif
00477 /* this implementation is here to satisfy the PRMonitor use in plarena.c.
00478 ** It appears that it doesn't need re-entrant locks.  It could have used
00479 ** PRLock instead of PRMonitor.  So, this implementation just uses 
00480 ** PRLock for a PRMonitor.
00481 */
00482 PR_IMPLEMENT(PRMonitor*) 
00483 PR_NewMonitor(void)
00484 {
00485     return (PRMonitor *) PR_NewLock();
00486 }
00487 
00488 
00489 PR_IMPLEMENT(void) 
00490 PR_EnterMonitor(PRMonitor *mon)
00491 {
00492     PR_Lock( (PRLock *)mon );
00493 }
00494 
00495 PR_IMPLEMENT(PRStatus) 
00496 PR_ExitMonitor(PRMonitor *mon)
00497 {
00498     return PR_Unlock( (PRLock *)mon );
00499 }
00500 
00501 #include "prinit.h"
00502 
00503 /* This is NOT threadsafe.  It is merely a pseudo-functional stub.
00504 */
00505 PR_IMPLEMENT(PRStatus) PR_CallOnce(
00506     PRCallOnceType *once,
00507     PRCallOnceFN    func)
00508 {
00509     /* This is not really atomic! */
00510     if (1 == PR_AtomicIncrement(&once->initialized)) {
00511        once->status = (*func)();
00512     }  else {
00513        /* Should wait to be sure that func has finished before returning. */
00514     }
00515     return once->status;
00516 }
00517 
00518 /*
00519 ** Compute the log of the least power of 2 greater than or equal to n
00520 */
00521 PRIntn PR_CeilingLog2(PRUint32 i) {
00522        PRIntn log2;
00523        PR_CEILING_LOG2(log2,i);
00524        return log2;
00525 }
00526 #endif /* ! (WIN32 && GCC) */
00527 
00528 /********************** end of arena functions ***********************/
00529