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 2004/07/29 22:51:00 jpierre%netscape.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     }
00078     return rv;
00079 }
00080 
00081 void *
00082 PORT_Realloc(void *oldptr, size_t bytes)
00083 {
00084     void *rv;
00085 
00086     rv = (void *)realloc(oldptr, bytes);
00087     if (!rv) {
00088        ++port_allocFailures;
00089     }
00090     return rv;
00091 }
00092 
00093 void *
00094 PORT_ZAlloc(size_t bytes)
00095 {
00096     void *rv;
00097 
00098     /* Always allocate a non-zero amount of bytes */
00099     rv = (void *)calloc(1, bytes ? bytes : 1);
00100     if (!rv) {
00101        ++port_allocFailures;
00102     }
00103     return rv;
00104 }
00105 
00106 void
00107 PORT_Free(void *ptr)
00108 {
00109     if (ptr) {
00110        free(ptr);
00111     }
00112 }
00113 
00114 void
00115 PORT_ZFree(void *ptr, size_t len)
00116 {
00117     if (ptr) {
00118        memset(ptr, 0, len);
00119        free(ptr);
00120     }
00121 }
00122 
00123 /********************* Arena code follows *****************************/
00124 
00125 
00126 PLArenaPool *
00127 PORT_NewArena(unsigned long chunksize)
00128 {
00129     PLArenaPool *arena;
00130     
00131     arena = (PLArenaPool*)PORT_ZAlloc(sizeof(PLArenaPool));
00132     if ( arena != NULL ) {
00133        PR_InitArenaPool(arena, "security", chunksize, sizeof(double));
00134     }
00135     return(arena);
00136 }
00137 
00138 void *
00139 PORT_ArenaAlloc(PLArenaPool *arena, size_t size)
00140 {
00141     void *p;
00142 
00143     PL_ARENA_ALLOCATE(p, arena, size);
00144     if (p == NULL) {
00145        ++port_allocFailures;
00146     }
00147 
00148     return(p);
00149 }
00150 
00151 void *
00152 PORT_ArenaZAlloc(PLArenaPool *arena, size_t size)
00153 {
00154     void *p;
00155 
00156     PL_ARENA_ALLOCATE(p, arena, size);
00157     if (p == NULL) {
00158        ++port_allocFailures;
00159     } else {
00160        PORT_Memset(p, 0, size);
00161     }
00162 
00163     return(p);
00164 }
00165 
00166 /* need to zeroize!! */
00167 void
00168 PORT_FreeArena(PLArenaPool *arena, PRBool zero)
00169 {
00170     PR_FinishArenaPool(arena);
00171     PORT_Free(arena);
00172 }
00173 
00174 void *
00175 PORT_ArenaGrow(PLArenaPool *arena, void *ptr, size_t oldsize, size_t newsize)
00176 {
00177     PORT_Assert(newsize >= oldsize);
00178     
00179     PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) );
00180     
00181     return(ptr);
00182 }
00183 
00184 void *
00185 PORT_ArenaMark(PLArenaPool *arena)
00186 {
00187     void * result;
00188 
00189     result = PL_ARENA_MARK(arena);
00190     return result;
00191 }
00192 
00193 void
00194 PORT_ArenaRelease(PLArenaPool *arena, void *mark)
00195 {
00196     PL_ARENA_RELEASE(arena, mark);
00197 }
00198 
00199 void
00200 PORT_ArenaUnmark(PLArenaPool *arena, void *mark)
00201 {
00202     /* do nothing */
00203 }
00204 
00205 char *
00206 PORT_ArenaStrdup(PLArenaPool *arena,const char *str) {
00207     int len = PORT_Strlen(str)+1;
00208     char *newstr;
00209 
00210     newstr = (char*)PORT_ArenaAlloc(arena,len);
00211     if (newstr) {
00212         PORT_Memcpy(newstr,str,len);
00213     }
00214     return newstr;
00215 }
00216 #endif
00217 
00218 /*
00219  * replace the nice thread-safe Error stack code with something
00220  * that will work without all the NSPR features.
00221  */
00222 static PRInt32 stack[2] = {0, 0};
00223 
00224 PR_IMPLEMENT(void)
00225 nss_SetError(PRUint32 value)
00226 {      
00227     stack[0] = value;
00228     return;
00229 }
00230 
00231 PR_IMPLEMENT(PRInt32)
00232 NSS_GetError(void)
00233 {
00234     return(stack[0]);
00235 }
00236 
00237 
00238 PR_IMPLEMENT(PRInt32 *)
00239 NSS_GetErrorStack(void)
00240 {
00241     return(&stack[0]);
00242 }
00243 
00244 PR_IMPLEMENT(void)
00245 nss_ClearErrorStack(void)
00246 {
00247     stack[0] = 0;
00248     return;
00249 }
00250 
00251 #ifdef DEBUG
00252 /*
00253  * replace the pointer tracking stuff for the same reasons.
00254  *  If you want to turn pointer tracking on, simply ifdef out this code and 
00255  *  link with real NSPR.
00256  */
00257 PR_IMPLEMENT(PRStatus)
00258 nssPointerTracker_initialize(nssPointerTracker *tracker)
00259 {
00260     return PR_SUCCESS;
00261 }
00262 
00263 
00264 PR_IMPLEMENT(PRStatus)
00265 nssPointerTracker_finalize(nssPointerTracker *tracker)
00266 {
00267     return PR_SUCCESS;
00268 }
00269 
00270 PR_IMPLEMENT(PRStatus)
00271 nssPointerTracker_add(nssPointerTracker *tracker, const void *pointer)
00272 {
00273      return PR_SUCCESS;
00274 }
00275 
00276 PR_IMPLEMENT(PRStatus)
00277 nssPointerTracker_remove(nssPointerTracker *tracker, const void *pointer)
00278 {
00279      return PR_SUCCESS;
00280 }
00281 
00282 PR_IMPLEMENT(PRStatus)
00283 nssPointerTracker_verify(nssPointerTracker *tracker, const void *pointer)
00284 {
00285      return PR_SUCCESS;
00286 }
00287 #endif
00288 
00289 /*
00290  * Do not use NSPR stubs for MinGW because they can't resolve references
00291  * to the _imp__PR_XXX symbols.  This is merely an expedient hack and not
00292  * the right solution.
00293  */
00294 #if !(defined(WIN32) && defined(__GNUC__))
00295 PR_IMPLEMENT(PRThread *)
00296 PR_GetCurrentThread(void)
00297 {
00298      return (PRThread *)1;
00299 }
00300 
00301 
00302 
00303 PR_IMPLEMENT(void)
00304 PR_Assert(const char *expr, const char *file, int line) {
00305     return; 
00306 }
00307 
00308 PR_IMPLEMENT(void *)
00309 PR_Alloc(PRUint32 bytes) { return malloc(bytes); }
00310 
00311 PR_IMPLEMENT(void *)
00312 PR_Malloc(PRUint32 bytes) { return malloc(bytes); }
00313 
00314 PR_IMPLEMENT(void *)
00315 PR_Calloc(PRUint32 blocks, PRUint32 bytes) { return calloc(blocks,bytes); }
00316 
00317 PR_IMPLEMENT(void *)
00318 PR_Realloc(void * blocks, PRUint32 bytes) { return realloc(blocks,bytes); }
00319 
00320 PR_IMPLEMENT(void)
00321 PR_Free(void *ptr) { free(ptr); }
00322 
00323 #ifdef notdef
00324 /* Old template; want to expunge it eventually. */
00325 #include "secasn1.h"
00326 #include "secoid.h"
00327 
00328 const SEC_ASN1Template SECOID_AlgorithmIDTemplate[] = {
00329     { SEC_ASN1_SEQUENCE,
00330          0, NULL, sizeof(SECAlgorithmID) },
00331     { SEC_ASN1_OBJECT_ID,
00332          offsetof(SECAlgorithmID,algorithm), },
00333     { SEC_ASN1_OPTIONAL | SEC_ASN1_ANY,
00334          offsetof(SECAlgorithmID,parameters), },
00335     { 0, }
00336 };
00337 
00338 PR_IMPLEMENT(PRStatus) PR_Sleep(PRIntervalTime ticks) { return PR_SUCCESS; }
00339 
00340 /* This is not atomic! */
00341 PR_IMPLEMENT(PRInt32) PR_AtomicDecrement(PRInt32 *val) { return --(*val); }
00342 
00343 PR_IMPLEMENT(PRInt32) PR_AtomicSet(PRInt32 *val) { return ++(*val); }
00344 
00345 #endif
00346 
00347 /* now make the RNG happy */ /* This is not atomic! */
00348 PR_IMPLEMENT(PRInt32) PR_AtomicIncrement(PRInt32 *val) { return ++(*val); }
00349 #endif /* ! (WIN32 && GCC) */
00350 
00351 static CK_C_INITIALIZE_ARGS_PTR nssstub_pInitArgs = NULL;
00352 static CK_C_INITIALIZE_ARGS nssstub_initArgs;
00353 static NSSArena *nssstub_arena = NULL;
00354 static CryptokiLockingState nssstub_LockingState = SingleThreaded;
00355 
00356 PR_IMPLEMENT(CK_RV)
00357 nssSetLockArgs(CK_C_INITIALIZE_ARGS_PTR pInitArgs, CryptokiLockingState* returned)
00358 {
00359     CK_ULONG count = (CK_ULONG)0;
00360     CK_BBOOL os_ok = CK_FALSE;
00361     CK_RV rv = CKR_OK;
00362     if (nssstub_pInitArgs == NULL) {
00363        if (pInitArgs != NULL) {
00364            nssstub_initArgs = *pInitArgs;
00365            nssstub_pInitArgs = &nssstub_initArgs;
00366             if( (CK_CREATEMUTEX )NULL != pInitArgs->CreateMutex  ) count++;
00367             if( (CK_DESTROYMUTEX)NULL != pInitArgs->DestroyMutex ) count++;
00368             if( (CK_LOCKMUTEX   )NULL != pInitArgs->LockMutex    ) count++;
00369             if( (CK_UNLOCKMUTEX )NULL != pInitArgs->UnlockMutex  ) count++;
00370             os_ok = (pInitArgs->flags & CKF_OS_LOCKING_OK) ? CK_TRUE : CK_FALSE;
00371 
00372             if( (0 != count) && (4 != count) ) {
00373                 rv = CKR_ARGUMENTS_BAD;
00374                 goto loser;
00375             }
00376        } else {
00377            nssstub_pInitArgs = pInitArgs;
00378        }
00379        /* nssstub_arena = NSSArena_Create(); */
00380     }
00381 
00382     if( (0 == count) && (CK_TRUE == os_ok) ) {
00383       /*
00384        * This is case #2 in the description of C_Initialize:
00385        * The library will be called in a multithreaded way, but
00386        * no routines were specified: os locking calls should be
00387        * used.  Unfortunately, this can be hard.. like, I think
00388        * I may have to dynamically look up the entry points in
00389        * the instance of NSPR already going in the application.
00390        *
00391        * I know that *we* always specify routines, so this only
00392        * comes up if someone is using NSS to create their own
00393        * PCKS#11 modules for other products.  Oh, heck, I'll 
00394        * worry about this then.
00395        */
00396       rv = CKR_CANT_LOCK;
00397       goto loser;
00398     }
00399 
00400     if( 0 == count ) {
00401       /*
00402        * With the above test out of the way, we know this is case
00403        * #1 in the description of C_Initialize: this library will
00404        * not be called in a multithreaded way.
00405        */
00406 
00407       nssstub_LockingState = SingleThreaded;
00408     } else {
00409       /*
00410        * We know that we're in either case #3 or #4 in the description
00411        * of C_Initialize.  Case #3 says we should use the specified
00412        * functions, case #4 cays we can use either the specified ones
00413        * or the OS ones.  I'll use the specified ones.
00414        */
00415       nssstub_LockingState = MultiThreaded;
00416     }
00417 
00418     loser:
00419     *returned = nssstub_LockingState;
00420     return rv;
00421 }
00422 
00423 /*
00424  * Do not use NSPR stubs for MinGW because they can't resolve references
00425  * to the _imp__PR_XXX symbols.  This is merely an expedient hack and not
00426  * the right solution.
00427  */
00428 #if !(defined(WIN32) && defined(__GNUC__))
00429 #include "prlock.h"
00430 PR_IMPLEMENT(PRLock *)
00431 PR_NewLock(void) {
00432        PRLock *lock = NULL;
00433        NSSCKFWMutex *mlock = NULL;
00434        CK_RV error;
00435 
00436        mlock = nssCKFWMutex_Create(nssstub_pInitArgs,nssstub_LockingState,nssstub_arena,&error);
00437        lock = (PRLock *)mlock;
00438 
00439        /* if we don't have a lock, nssCKFWMutex can deal with things */
00440        if (lock == NULL) lock=(PRLock *) 1;
00441        return lock;
00442 }
00443 
00444 PR_IMPLEMENT(void) 
00445 PR_DestroyLock(PRLock *lock) {
00446        NSSCKFWMutex *mlock = (NSSCKFWMutex *)lock;
00447        if (lock == (PRLock *)1) return;
00448        nssCKFWMutex_Destroy(mlock);
00449 }
00450 
00451 PR_IMPLEMENT(void) 
00452 PR_Lock(PRLock *lock) {
00453        NSSCKFWMutex *mlock = (NSSCKFWMutex *)lock;
00454        if (lock == (PRLock *)1) return;
00455        nssCKFWMutex_Lock(mlock);
00456 }
00457 
00458 PR_IMPLEMENT(PRStatus) 
00459 PR_Unlock(PRLock *lock) {
00460        NSSCKFWMutex *mlock = (NSSCKFWMutex *)lock;
00461        if (lock == (PRLock *)1) return PR_SUCCESS;
00462        nssCKFWMutex_Unlock(mlock);
00463        return PR_SUCCESS;
00464 }
00465 
00466 #ifdef notdef
00467 #endif
00468 /* this implementation is here to satisfy the PRMonitor use in plarena.c.
00469 ** It appears that it doesn't need re-entrant locks.  It could have used
00470 ** PRLock instead of PRMonitor.  So, this implementation just uses 
00471 ** PRLock for a PRMonitor.
00472 */
00473 PR_IMPLEMENT(PRMonitor*) 
00474 PR_NewMonitor(void)
00475 {
00476     return (PRMonitor *) PR_NewLock();
00477 }
00478 
00479 
00480 PR_IMPLEMENT(void) 
00481 PR_EnterMonitor(PRMonitor *mon)
00482 {
00483     PR_Lock( (PRLock *)mon );
00484 }
00485 
00486 PR_IMPLEMENT(PRStatus) 
00487 PR_ExitMonitor(PRMonitor *mon)
00488 {
00489     return PR_Unlock( (PRLock *)mon );
00490 }
00491 
00492 #include "prinit.h"
00493 
00494 /* This is NOT threadsafe.  It is merely a pseudo-functional stub.
00495 */
00496 PR_IMPLEMENT(PRStatus) PR_CallOnce(
00497     PRCallOnceType *once,
00498     PRCallOnceFN    func)
00499 {
00500     /* This is not really atomic! */
00501     if (1 == PR_AtomicIncrement(&once->initialized)) {
00502        once->status = (*func)();
00503     }  else {
00504        /* Should wait to be sure that func has finished before returning. */
00505     }
00506     return once->status;
00507 }
00508 
00509 /*
00510 ** Compute the log of the least power of 2 greater than or equal to n
00511 */
00512 PRIntn PR_CeilingLog2(PRUint32 i) {
00513        PRIntn log2;
00514        PR_CEILING_LOG2(log2,i);
00515        return log2;
00516 }
00517 #endif /* ! (WIN32 && GCC) */
00518 
00519 /********************** end of arena functions ***********************/
00520