Back to index

lightning-sunbird  0.9+nobinonly
secport.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 /*
00038  * secport.c - portability interfaces for security libraries
00039  *
00040  * This file abstracts out libc functionality that libsec depends on
00041  * 
00042  * NOTE - These are not public interfaces
00043  *
00044  * $Id: secport.c,v 1.18.28.1 2006/08/16 00:08:19 wtchang%redhat.com Exp $
00045  */
00046 
00047 #include "seccomon.h"
00048 #include "prmem.h"
00049 #include "prerror.h"
00050 #include "plarena.h"
00051 #include "secerr.h"
00052 #include "prmon.h"
00053 #include "nsslocks.h"
00054 #include "secport.h"
00055 #include "prvrsion.h"
00056 #include "prenv.h"
00057 
00058 #ifdef DEBUG
00059 #define THREADMARK
00060 #endif /* DEBUG */
00061 
00062 #ifdef THREADMARK
00063 #include "prthread.h"
00064 #endif /* THREADMARK */
00065 
00066 #if defined(XP_UNIX) || defined(XP_MAC) || defined(XP_OS2) || defined(XP_BEOS)
00067 #include <stdlib.h>
00068 #else
00069 #include "wtypes.h"
00070 #endif
00071 
00072 #define SET_ERROR_CODE      /* place holder for code to set PR error code. */
00073 
00074 #ifdef THREADMARK
00075 typedef struct threadmark_mark_str {
00076   struct threadmark_mark_str *next;
00077   void *mark;
00078 } threadmark_mark;
00079 
00080 #endif /* THREADMARK */
00081 
00082 /* The value of this magic must change each time PORTArenaPool changes. */
00083 #define ARENAPOOL_MAGIC 0xB8AC9BDF 
00084 
00085 typedef struct PORTArenaPool_str {
00086   PLArenaPool arena;
00087   PRUint32    magic;
00088   PRLock *    lock;
00089 #ifdef THREADMARK
00090   PRThread *marking_thread;
00091   threadmark_mark *first_mark;
00092 #endif
00093 } PORTArenaPool;
00094 
00095 
00096 /* count of allocation failures. */
00097 unsigned long port_allocFailures;
00098 
00099 /* locations for registering Unicode conversion functions.  
00100  * XXX is this the appropriate location?  or should they be
00101  *     moved to client/server specific locations?
00102  */
00103 PORTCharConversionFunc ucs4Utf8ConvertFunc;
00104 PORTCharConversionFunc ucs2Utf8ConvertFunc;
00105 PORTCharConversionWSwapFunc  ucs2AsciiConvertFunc;
00106 
00107 void *
00108 PORT_Alloc(size_t bytes)
00109 {
00110     void *rv;
00111 
00112     /* Always allocate a non-zero amount of bytes */
00113     rv = (void *)PR_Malloc(bytes ? bytes : 1);
00114     if (!rv) {
00115        ++port_allocFailures;
00116        PORT_SetError(SEC_ERROR_NO_MEMORY);
00117     }
00118     return rv;
00119 }
00120 
00121 void *
00122 PORT_Realloc(void *oldptr, size_t bytes)
00123 {
00124     void *rv;
00125 
00126     rv = (void *)PR_Realloc(oldptr, bytes);
00127     if (!rv) {
00128        ++port_allocFailures;
00129        PORT_SetError(SEC_ERROR_NO_MEMORY);
00130     }
00131     return rv;
00132 }
00133 
00134 void *
00135 PORT_ZAlloc(size_t bytes)
00136 {
00137     void *rv;
00138 
00139     /* Always allocate a non-zero amount of bytes */
00140     rv = (void *)PR_Calloc(1, bytes ? bytes : 1);
00141     if (!rv) {
00142        ++port_allocFailures;
00143        PORT_SetError(SEC_ERROR_NO_MEMORY);
00144     }
00145     return rv;
00146 }
00147 
00148 void
00149 PORT_Free(void *ptr)
00150 {
00151     if (ptr) {
00152        PR_Free(ptr);
00153     }
00154 }
00155 
00156 void
00157 PORT_ZFree(void *ptr, size_t len)
00158 {
00159     if (ptr) {
00160        memset(ptr, 0, len);
00161        PR_Free(ptr);
00162     }
00163 }
00164 
00165 char *
00166 PORT_Strdup(const char *str)
00167 {
00168     size_t len = PORT_Strlen(str)+1;
00169     char *newstr;
00170 
00171     newstr = (char *)PORT_Alloc(len);
00172     if (newstr) {
00173         PORT_Memcpy(newstr, str, len);
00174     }
00175     return newstr;
00176 }
00177 
00178 void
00179 PORT_SetError(int value)
00180 {      
00181     PR_SetError(value, 0);
00182     return;
00183 }
00184 
00185 int
00186 PORT_GetError(void)
00187 {
00188     return(PR_GetError());
00189 }
00190 
00191 /********************* Arena code follows *****************************/
00192 
00193 PLArenaPool *
00194 PORT_NewArena(unsigned long chunksize)
00195 {
00196     PORTArenaPool *pool;
00197     
00198     pool = PORT_ZNew(PORTArenaPool);
00199     if (!pool) {
00200        return NULL;
00201     }
00202     pool->magic = ARENAPOOL_MAGIC;
00203     pool->lock = PZ_NewLock(nssILockArena);
00204     if (!pool->lock) {
00205        ++port_allocFailures;
00206        PORT_Free(pool);
00207        return NULL;
00208     }
00209     PL_InitArenaPool(&pool->arena, "security", chunksize, sizeof(double));
00210     return(&pool->arena);
00211 }
00212 
00213 #define MAX_SIZE 0x7fffffffUL
00214 
00215 void *
00216 PORT_ArenaAlloc(PLArenaPool *arena, size_t size)
00217 {
00218     void *p = NULL;
00219 
00220     PORTArenaPool *pool = (PORTArenaPool *)arena;
00221 
00222     if (size <= 0) {
00223        size = 1;
00224     }
00225 
00226     if (size > MAX_SIZE) {
00227        /* you lose. */
00228     } else 
00229     /* Is it one of ours?  Assume so and check the magic */
00230     if (ARENAPOOL_MAGIC == pool->magic ) {
00231        PZ_Lock(pool->lock);
00232 #ifdef THREADMARK
00233         /* Most likely one of ours.  Is there a thread id? */
00234        if (pool->marking_thread  &&
00235            pool->marking_thread != PR_GetCurrentThread() ) {
00236            /* Another thread holds a mark in this arena */
00237            PZ_Unlock(pool->lock);
00238            PORT_SetError(SEC_ERROR_NO_MEMORY);
00239            PORT_Assert(0);
00240            return NULL;
00241        } /* tid != null */
00242 #endif /* THREADMARK */
00243        PL_ARENA_ALLOCATE(p, arena, size);
00244        PZ_Unlock(pool->lock);
00245     } else {
00246        PL_ARENA_ALLOCATE(p, arena, size);
00247     }
00248 
00249     if (!p) {
00250        ++port_allocFailures;
00251        PORT_SetError(SEC_ERROR_NO_MEMORY);
00252     }
00253 
00254     return(p);
00255 }
00256 
00257 void *
00258 PORT_ArenaZAlloc(PLArenaPool *arena, size_t size)
00259 {
00260     void *p;
00261 
00262     if (size <= 0)
00263         size = 1;
00264 
00265     p = PORT_ArenaAlloc(arena, size);
00266 
00267     if (p) {
00268        PORT_Memset(p, 0, size);
00269     }
00270 
00271     return(p);
00272 }
00273 
00274 /*
00275  * If zero is true, zeroize the arena memory before freeing it.
00276  */
00277 void
00278 PORT_FreeArena(PLArenaPool *arena, PRBool zero)
00279 {
00280     PORTArenaPool *pool = (PORTArenaPool *)arena;
00281     PRLock *       lock = (PRLock *)0;
00282     size_t         len  = sizeof *arena;
00283     extern const PRVersionDescription * libVersionPoint(void);
00284     static const PRVersionDescription * pvd;
00285     static PRBool  doFreeArenaPool = PR_FALSE;
00286 
00287     if (ARENAPOOL_MAGIC == pool->magic ) {
00288        len  = sizeof *pool;
00289        lock = pool->lock;
00290        PZ_Lock(lock);
00291     }
00292     if (!pvd) {
00293        /* Each of NSPR's DLLs has a function libVersionPoint().
00294        ** We could do a lot of extra work to be sure we're calling the
00295        ** one in the DLL that holds PR_FreeArenaPool, but instead we
00296        ** rely on the fact that ALL NSPR DLLs in the same directory
00297        ** must be from the same release, and we call which ever one we get. 
00298        */
00299        /* no need for thread protection here */
00300        pvd = libVersionPoint();
00301        if ((pvd->vMajor > 4) || 
00302            (pvd->vMajor == 4 && pvd->vMinor > 1) ||
00303            (pvd->vMajor == 4 && pvd->vMinor == 1 && pvd->vPatch >= 1)) {
00304            const char *ev = PR_GetEnv("NSS_DISABLE_ARENA_FREE_LIST");
00305            if (!ev) doFreeArenaPool = PR_TRUE;
00306        }
00307     }
00308     if (zero) {
00309        PLArena *a;
00310        for (a = arena->first.next; a; a = a->next) {
00311            PR_ASSERT(a->base <= a->avail && a->avail <= a->limit);
00312            memset((void *)a->base, 0, a->avail - a->base);
00313        }
00314     }
00315     if (doFreeArenaPool) {
00316        PL_FreeArenaPool(arena);
00317     } else {
00318        PL_FinishArenaPool(arena);
00319     }
00320     PORT_ZFree(arena, len);
00321     if (lock) {
00322        PZ_Unlock(lock);
00323        PZ_DestroyLock(lock);
00324     }
00325 }
00326 
00327 void *
00328 PORT_ArenaGrow(PLArenaPool *arena, void *ptr, size_t oldsize, size_t newsize)
00329 {
00330     PORTArenaPool *pool = (PORTArenaPool *)arena;
00331     PORT_Assert(newsize >= oldsize);
00332     
00333     if (ARENAPOOL_MAGIC == pool->magic ) {
00334        PZ_Lock(pool->lock);
00335        /* Do we do a THREADMARK check here? */
00336        PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) );
00337        PZ_Unlock(pool->lock);
00338     } else {
00339        PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) );
00340     }
00341     
00342     return(ptr);
00343 }
00344 
00345 void *
00346 PORT_ArenaMark(PLArenaPool *arena)
00347 {
00348     void * result;
00349 
00350     PORTArenaPool *pool = (PORTArenaPool *)arena;
00351     if (ARENAPOOL_MAGIC == pool->magic ) {
00352        PZ_Lock(pool->lock);
00353 #ifdef THREADMARK
00354        {
00355          threadmark_mark *tm, **pw;
00356          PRThread * currentThread = PR_GetCurrentThread();
00357 
00358            if (! pool->marking_thread ) {
00359               /* First mark */
00360               pool->marking_thread = currentThread;
00361            } else if (currentThread != pool->marking_thread ) {
00362               PZ_Unlock(pool->lock);
00363               PORT_SetError(SEC_ERROR_NO_MEMORY);
00364               PORT_Assert(0);
00365               return NULL;
00366            }
00367 
00368            result = PL_ARENA_MARK(arena);
00369            PL_ARENA_ALLOCATE(tm, arena, sizeof(threadmark_mark));
00370            if (!tm) {
00371               PZ_Unlock(pool->lock);
00372               PORT_SetError(SEC_ERROR_NO_MEMORY);
00373               return NULL;
00374            }
00375 
00376            tm->mark = result;
00377            tm->next = (threadmark_mark *)NULL;
00378 
00379            pw = &pool->first_mark;
00380            while( *pw ) {
00381                pw = &(*pw)->next;
00382            }
00383 
00384            *pw = tm;
00385        }
00386 #else /* THREADMARK */
00387        result = PL_ARENA_MARK(arena);
00388 #endif /* THREADMARK */
00389        PZ_Unlock(pool->lock);
00390     } else {
00391        /* a "pure" NSPR arena */
00392        result = PL_ARENA_MARK(arena);
00393     }
00394     return result;
00395 }
00396 
00397 static void
00398 port_ArenaZeroAfterMark(PLArenaPool *arena, void *mark)
00399 {
00400     PLArena *a = arena->current;
00401     if (a->base <= (PRUword)mark && (PRUword)mark <= a->avail) {
00402        /* fast path: mark falls in the current arena */
00403        memset(mark, 0, a->avail - (PRUword)mark);
00404     } else {
00405        /* slow path: need to find the arena that mark falls in */
00406        for (a = arena->first.next; a; a = a->next) {
00407            PR_ASSERT(a->base <= a->avail && a->avail <= a->limit);
00408            if (a->base <= (PRUword)mark && (PRUword)mark <= a->avail) {
00409               memset(mark, 0, a->avail - (PRUword)mark);
00410               a = a->next;
00411               break;
00412            }
00413        }
00414        for (; a; a = a->next) {
00415            PR_ASSERT(a->base <= a->avail && a->avail <= a->limit);
00416            memset((void *)a->base, 0, a->avail - a->base);
00417        }
00418     }
00419 }
00420 
00421 static void
00422 port_ArenaRelease(PLArenaPool *arena, void *mark, PRBool zero)
00423 {
00424     PORTArenaPool *pool = (PORTArenaPool *)arena;
00425     if (ARENAPOOL_MAGIC == pool->magic ) {
00426        PZ_Lock(pool->lock);
00427 #ifdef THREADMARK
00428        {
00429            threadmark_mark **pw, *tm;
00430 
00431            if (PR_GetCurrentThread() != pool->marking_thread ) {
00432               PZ_Unlock(pool->lock);
00433               PORT_SetError(SEC_ERROR_NO_MEMORY);
00434               PORT_Assert(0);
00435               return /* no error indication available */ ;
00436            }
00437 
00438            pw = &pool->first_mark;
00439            while( *pw && (mark != (*pw)->mark) ) {
00440               pw = &(*pw)->next;
00441            }
00442 
00443            if (! *pw ) {
00444               /* bad mark */
00445               PZ_Unlock(pool->lock);
00446               PORT_SetError(SEC_ERROR_NO_MEMORY);
00447               PORT_Assert(0);
00448               return /* no error indication available */ ;
00449            }
00450 
00451            tm = *pw;
00452            *pw = (threadmark_mark *)NULL;
00453 
00454            if (zero) {
00455               port_ArenaZeroAfterMark(arena, mark);
00456            }
00457            PL_ARENA_RELEASE(arena, mark);
00458 
00459            if (! pool->first_mark ) {
00460               pool->marking_thread = (PRThread *)NULL;
00461            }
00462        }
00463 #else /* THREADMARK */
00464        if (zero) {
00465            port_ArenaZeroAfterMark(arena, mark);
00466        }
00467        PL_ARENA_RELEASE(arena, mark);
00468 #endif /* THREADMARK */
00469        PZ_Unlock(pool->lock);
00470     } else {
00471        if (zero) {
00472            port_ArenaZeroAfterMark(arena, mark);
00473        }
00474        PL_ARENA_RELEASE(arena, mark);
00475     }
00476 }
00477 
00478 void
00479 PORT_ArenaRelease(PLArenaPool *arena, void *mark)
00480 {
00481     port_ArenaRelease(arena, mark, PR_FALSE);
00482 }
00483 
00484 /*
00485  * Zeroize the arena memory before releasing it.
00486  */
00487 void
00488 PORT_ArenaZRelease(PLArenaPool *arena, void *mark)
00489 {
00490     port_ArenaRelease(arena, mark, PR_TRUE);
00491 }
00492 
00493 void
00494 PORT_ArenaUnmark(PLArenaPool *arena, void *mark)
00495 {
00496 #ifdef THREADMARK
00497     PORTArenaPool *pool = (PORTArenaPool *)arena;
00498     if (ARENAPOOL_MAGIC == pool->magic ) {
00499        threadmark_mark **pw, *tm;
00500 
00501        PZ_Lock(pool->lock);
00502 
00503        if (PR_GetCurrentThread() != pool->marking_thread ) {
00504            PZ_Unlock(pool->lock);
00505            PORT_SetError(SEC_ERROR_NO_MEMORY);
00506            PORT_Assert(0);
00507            return /* no error indication available */ ;
00508        }
00509 
00510        pw = &pool->first_mark;
00511        while( ((threadmark_mark *)NULL != *pw) && (mark != (*pw)->mark) ) {
00512            pw = &(*pw)->next;
00513        }
00514 
00515        if ((threadmark_mark *)NULL == *pw ) {
00516            /* bad mark */
00517            PZ_Unlock(pool->lock);
00518            PORT_SetError(SEC_ERROR_NO_MEMORY);
00519            PORT_Assert(0);
00520            return /* no error indication available */ ;
00521        }
00522 
00523        tm = *pw;
00524        *pw = (threadmark_mark *)NULL;
00525 
00526        if (! pool->first_mark ) {
00527            pool->marking_thread = (PRThread *)NULL;
00528        }
00529 
00530        PZ_Unlock(pool->lock);
00531     }
00532 #endif /* THREADMARK */
00533 }
00534 
00535 char *
00536 PORT_ArenaStrdup(PLArenaPool *arena, const char *str) {
00537     int len = PORT_Strlen(str)+1;
00538     char *newstr;
00539 
00540     newstr = (char*)PORT_ArenaAlloc(arena,len);
00541     if (newstr) {
00542         PORT_Memcpy(newstr,str,len);
00543     }
00544     return newstr;
00545 }
00546 
00547 /********************** end of arena functions ***********************/
00548 
00549 /****************** unicode conversion functions ***********************/
00550 /*
00551  * NOTE: These conversion functions all assume that the multibyte
00552  * characters are going to be in NETWORK BYTE ORDER, not host byte
00553  * order.  This is because the only time we deal with UCS-2 and UCS-4
00554  * are when the data was received from or is going to be sent out
00555  * over the wire (in, e.g. certificates).
00556  */
00557 
00558 void
00559 PORT_SetUCS4_UTF8ConversionFunction(PORTCharConversionFunc convFunc)
00560 { 
00561     ucs4Utf8ConvertFunc = convFunc;
00562 }
00563 
00564 void
00565 PORT_SetUCS2_ASCIIConversionFunction(PORTCharConversionWSwapFunc convFunc)
00566 { 
00567     ucs2AsciiConvertFunc = convFunc;
00568 }
00569 
00570 void
00571 PORT_SetUCS2_UTF8ConversionFunction(PORTCharConversionFunc convFunc)
00572 { 
00573     ucs2Utf8ConvertFunc = convFunc;
00574 }
00575 
00576 PRBool 
00577 PORT_UCS4_UTF8Conversion(PRBool toUnicode, unsigned char *inBuf,
00578                       unsigned int inBufLen, unsigned char *outBuf,
00579                       unsigned int maxOutBufLen, unsigned int *outBufLen)
00580 {
00581     if(!ucs4Utf8ConvertFunc) {
00582       return sec_port_ucs4_utf8_conversion_function(toUnicode,
00583         inBuf, inBufLen, outBuf, maxOutBufLen, outBufLen);
00584     }
00585 
00586     return (*ucs4Utf8ConvertFunc)(toUnicode, inBuf, inBufLen, outBuf, 
00587                               maxOutBufLen, outBufLen);
00588 }
00589 
00590 PRBool 
00591 PORT_UCS2_UTF8Conversion(PRBool toUnicode, unsigned char *inBuf,
00592                       unsigned int inBufLen, unsigned char *outBuf,
00593                       unsigned int maxOutBufLen, unsigned int *outBufLen)
00594 {
00595     if(!ucs2Utf8ConvertFunc) {
00596       return sec_port_ucs2_utf8_conversion_function(toUnicode,
00597         inBuf, inBufLen, outBuf, maxOutBufLen, outBufLen);
00598     }
00599 
00600     return (*ucs2Utf8ConvertFunc)(toUnicode, inBuf, inBufLen, outBuf, 
00601                               maxOutBufLen, outBufLen);
00602 }
00603 
00604 PRBool 
00605 PORT_ISO88591_UTF8Conversion(const unsigned char *inBuf,
00606                       unsigned int inBufLen, unsigned char *outBuf,
00607                       unsigned int maxOutBufLen, unsigned int *outBufLen)
00608 {
00609     return sec_port_iso88591_utf8_conversion_function(inBuf, inBufLen,
00610       outBuf, maxOutBufLen, outBufLen);
00611 }
00612 
00613 PRBool 
00614 PORT_UCS2_ASCIIConversion(PRBool toUnicode, unsigned char *inBuf,
00615                        unsigned int inBufLen, unsigned char *outBuf,
00616                        unsigned int maxOutBufLen, unsigned int *outBufLen,
00617                        PRBool swapBytes)
00618 {
00619     if(!ucs2AsciiConvertFunc) {
00620        return PR_FALSE;
00621     }
00622 
00623     return (*ucs2AsciiConvertFunc)(toUnicode, inBuf, inBufLen, outBuf, 
00624                               maxOutBufLen, outBufLen, swapBytes);
00625 }
00626 
00627 
00628 /* Portable putenv.  Creates/replaces an environment variable of the form
00629  *  envVarName=envValue
00630  */
00631 int
00632 NSS_PutEnv(const char * envVarName, const char * envValue)
00633 {
00634 #if  defined(XP_MAC) || defined(_WIN32_WCE)
00635     return SECFailure;
00636 #else
00637     SECStatus result = SECSuccess;
00638     char *    encoded;
00639     int       putEnvFailed;
00640 #ifdef _WIN32
00641     PRBool      setOK;
00642 
00643     setOK = SetEnvironmentVariable(envVarName, envValue);
00644     if (!setOK) {
00645         SET_ERROR_CODE
00646         return SECFailure;
00647     }
00648 #endif
00649 
00650     encoded = (char *)PORT_ZAlloc(strlen(envVarName) + 2 + strlen(envValue));
00651     strcpy(encoded, envVarName);
00652     strcat(encoded, "=");
00653     strcat(encoded, envValue);
00654 
00655     putEnvFailed = putenv(encoded); /* adopt. */
00656     if (putEnvFailed) {
00657         SET_ERROR_CODE
00658         result = SECFailure;
00659         PORT_Free(encoded);
00660     }
00661     return result;
00662 #endif
00663 }
00664