security/nss/lib/freebl/stubs.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/stubs.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,659 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*
     1.9 + * Allow freebl and softoken to be loaded without util or NSPR.
    1.10 + *
    1.11 + * These symbols are overridden once real NSPR, and libutil are attached.
    1.12 + */
    1.13 +#define _GNU_SOURCE 1
    1.14 +#include <stdlib.h>
    1.15 +#include <stdio.h>
    1.16 +#include <stdarg.h>
    1.17 +#include <fcntl.h>
    1.18 +#include <time.h>
    1.19 +#include <unistd.h>
    1.20 +#include <sys/time.h>
    1.21 +#include <dlfcn.h>
    1.22 +#include <prio.h>
    1.23 +#include <prlink.h>
    1.24 +#include <prlog.h>
    1.25 +#include <prthread.h>
    1.26 +#include <plstr.h>
    1.27 +#include <prinit.h>
    1.28 +#include <prlock.h>
    1.29 +#include <prmem.h>
    1.30 +#include <prerror.h>
    1.31 +#include <prmon.h>
    1.32 +#include <pratom.h>
    1.33 +#include <prsystem.h>
    1.34 +#include <prinrval.h>
    1.35 +#include <prtime.h>
    1.36 +#include <prcvar.h>
    1.37 +#include <secasn1.h>
    1.38 +#include <secdig.h>
    1.39 +#include <secport.h>
    1.40 +#include <secitem.h>
    1.41 +#include <blapi.h>
    1.42 +#include <private/pprio.h>
    1.43 +
    1.44 +#define FREEBL_NO_WEAK 1
    1.45 +
    1.46 +#define WEAK __attribute__((weak))
    1.47 +
    1.48 +#ifdef FREEBL_NO_WEAK
    1.49 +
    1.50 +/*
    1.51 + * This uses function pointers.
    1.52 + * 
    1.53 + * CONS:  A separate function is needed to
    1.54 + * fill in the function pointers.
    1.55 + *
    1.56 + * PROS: it works on all platforms.
    1.57 + *  it allows for dynamically finding nspr and libutil, even once
    1.58 + *  softoken is loaded and running. (NOTE: this may be a problem if
    1.59 + *  we switch between the stubs and real NSPR on the fly. NSPR will
    1.60 + *  do bad things if passed an _FakeArena to free or allocate from).
    1.61 + */
    1.62 +#define STUB_DECLARE(ret, fn,  args) \
    1.63 +   typedef ret (*type_##fn) args; \
    1.64 +   static type_##fn ptr_##fn = NULL
    1.65 +
    1.66 +#define STUB_SAFE_CALL0(fn) \
    1.67 +    if (ptr_##fn) { return ptr_##fn(); }
    1.68 +#define STUB_SAFE_CALL1(fn,a1) \
    1.69 +    if (ptr_##fn) { return ptr_##fn(a1); }
    1.70 +#define STUB_SAFE_CALL2(fn,a1,a2) \
    1.71 +    if (ptr_##fn) { return ptr_##fn(a1,a2); }
    1.72 +#define STUB_SAFE_CALL3(fn,a1,a2,a3) \
    1.73 +    if (ptr_##fn) { return ptr_##fn(a1,a2,a3); }
    1.74 +#define STUB_SAFE_CALL4(fn,a1,a2,a3,a4) \
    1.75 +    if (ptr_##fn) { return ptr_##fn(a1,a2,a3,a4); }
    1.76 +#define STUB_SAFE_CALL6(fn,a1,a2,a3,a4,a5,a6) \
    1.77 +    if (ptr_##fn) { return ptr_##fn(a1,a2,a3,a4,a5,a6); }
    1.78 +
    1.79 +#define STUB_FETCH_FUNCTION(fn) \
    1.80 +    ptr_##fn = (type_##fn) dlsym(lib,#fn); \
    1.81 +    if (ptr_##fn == NULL) { \
    1.82 +        return SECFailure; \
    1.83 +    }
    1.84 +
    1.85 +#else
    1.86 +/*
    1.87 + * this uses the loader weak attribute. it works automatically, but once
    1.88 + * freebl is loaded, the symbols are 'fixed' (later loading of NSPR or 
    1.89 + * libutil will not resolve these symbols.
    1.90 + */
    1.91 +
    1.92 +#define STUB_DECLARE(ret, fn,  args) \
    1.93 +   WEAK extern ret fn args
    1.94 +
    1.95 +#define STUB_SAFE_CALL0(fn) \
    1.96 +    if (fn) { return fn(); }
    1.97 +#define STUB_SAFE_CALL1(fn,a1) \
    1.98 +    if (fn) { return fn(a1); }
    1.99 +#define STUB_SAFE_CALL2(fn,a1,a2) \
   1.100 +    if (fn) { return fn(a1,a2); }
   1.101 +#define STUB_SAFE_CALL3(fn,a1,a2,a3) \
   1.102 +    if (fn) { return fn(a1,a2,a3); }
   1.103 +#define STUB_SAFE_CALL4(fn,a1,a2,a3,a4) \
   1.104 +    if (fn) { return fn(a1,a2,a3,a4); }
   1.105 +#define STUB_SAFE_CALL6(fn,a1,a2,a3,a4,a5,a6) \
   1.106 +    if (fn) { return fn(a1,a2,a3,a4,a5,a6); }
   1.107 +#endif
   1.108 +
   1.109 +
   1.110 +STUB_DECLARE(void *,PORT_Alloc_Util,(size_t len));
   1.111 +STUB_DECLARE(void *,PORT_ArenaAlloc_Util,(PLArenaPool *arena, size_t size));
   1.112 +STUB_DECLARE(void *,PORT_ArenaZAlloc_Util,(PLArenaPool *arena, size_t size));
   1.113 +STUB_DECLARE(void ,PORT_Free_Util,(void *ptr));
   1.114 +STUB_DECLARE(void ,PORT_FreeArena_Util,(PLArenaPool *arena, PRBool zero));
   1.115 +STUB_DECLARE(int,PORT_GetError_Util,(void));
   1.116 +STUB_DECLARE(PLArenaPool *,PORT_NewArena_Util,(unsigned long chunksize));
   1.117 +STUB_DECLARE(void,PORT_SetError_Util,(int value));
   1.118 +STUB_DECLARE(void *,PORT_ZAlloc_Util,(size_t len));
   1.119 +STUB_DECLARE(void,PORT_ZFree_Util,(void *ptr, size_t len));
   1.120 +
   1.121 +STUB_DECLARE(void,PR_Assert,(const char *s, const char *file, PRIntn ln));
   1.122 +STUB_DECLARE(PRStatus,PR_CallOnce,(PRCallOnceType *once, PRCallOnceFN func));
   1.123 +STUB_DECLARE(PRStatus,PR_Close,(PRFileDesc *fd));
   1.124 +STUB_DECLARE(void,PR_DestroyLock,(PRLock *lock));
   1.125 +STUB_DECLARE(void,PR_DestroyCondVar,(PRCondVar *cvar));
   1.126 +STUB_DECLARE(void,PR_Free,(void *ptr));
   1.127 +STUB_DECLARE(char * ,PR_GetLibraryFilePathname,(const char *name,
   1.128 +			PRFuncPtr addr));
   1.129 +STUB_DECLARE(PRFileDesc *,PR_ImportPipe,(PROsfd osfd));
   1.130 +STUB_DECLARE(void,PR_Lock,(PRLock *lock));
   1.131 +STUB_DECLARE(PRCondVar *,PR_NewCondVar,(PRLock *lock));
   1.132 +STUB_DECLARE(PRLock *,PR_NewLock,(void));
   1.133 +STUB_DECLARE(PRStatus,PR_NotifyCondVar,(PRCondVar *cvar));
   1.134 +STUB_DECLARE(PRStatus,PR_NotifyAllCondVar,(PRCondVar *cvar));
   1.135 +STUB_DECLARE(PRFileDesc *,PR_Open,(const char *name, PRIntn flags,
   1.136 +			 PRIntn mode));
   1.137 +STUB_DECLARE(PRInt32,PR_Read,(PRFileDesc *fd, void *buf, PRInt32 amount));
   1.138 +STUB_DECLARE(PROffset32,PR_Seek,(PRFileDesc *fd, PROffset32 offset, 
   1.139 +			PRSeekWhence whence));
   1.140 +STUB_DECLARE(PRStatus,PR_Sleep,(PRIntervalTime ticks));
   1.141 +STUB_DECLARE(PRStatus,PR_Unlock,(PRLock *lock));
   1.142 +STUB_DECLARE(PRStatus,PR_WaitCondVar,(PRCondVar *cvar,
   1.143 +			PRIntervalTime timeout));
   1.144 +
   1.145 +
   1.146 +STUB_DECLARE(SECItem *,SECITEM_AllocItem_Util,(PLArenaPool *arena,
   1.147 +			SECItem *item,unsigned int len));
   1.148 +STUB_DECLARE(SECComparison,SECITEM_CompareItem_Util,(const SECItem *a,
   1.149 +			const SECItem *b));
   1.150 +STUB_DECLARE(SECStatus,SECITEM_CopyItem_Util,(PLArenaPool *arena,
   1.151 +			SECItem *to,const SECItem *from));
   1.152 +STUB_DECLARE(void,SECITEM_FreeItem_Util,(SECItem *zap, PRBool freeit));
   1.153 +STUB_DECLARE(void,SECITEM_ZfreeItem_Util,(SECItem *zap, PRBool freeit));
   1.154 +STUB_DECLARE(SECOidTag,SECOID_FindOIDTag_Util,(const SECItem *oid));
   1.155 +STUB_DECLARE(int, NSS_SecureMemcmp,(const void *a, const void *b, size_t n));
   1.156 +
   1.157 +
   1.158 +#define PORT_ZNew_stub(type) (type*)PORT_ZAlloc_stub(sizeof(type))
   1.159 +#define PORT_New_stub(type) (type*)PORT_Alloc_stub(sizeof(type))
   1.160 +#define PORT_ZNewArray_stub(type, num)       \
   1.161 +                (type*) PORT_ZAlloc_stub (sizeof(type)*(num))
   1.162 +
   1.163 +
   1.164 +/*
   1.165 + * NOTE: in order to support hashing only the memory allocation stubs,
   1.166 + * the get library name stubs, and the file io stubs are needed (the latter
   1.167 + * two are for the library verification). The remaining stubs are simply to
   1.168 + * compile. Attempts to use the library for other operations without NSPR
   1.169 + * will most likely fail.
   1.170 + */
   1.171 +
   1.172 + 
   1.173 +/* memory */
   1.174 +extern void *
   1.175 +PORT_Alloc_stub(size_t len)
   1.176 +{
   1.177 +    STUB_SAFE_CALL1(PORT_Alloc_Util, len);
   1.178 +    return malloc(len);
   1.179 +}
   1.180 +
   1.181 +extern void
   1.182 +PORT_Free_stub(void *ptr)
   1.183 +{
   1.184 +    STUB_SAFE_CALL1(PORT_Free_Util, ptr);
   1.185 +    return free(ptr);
   1.186 +}
   1.187 +
   1.188 +extern void *
   1.189 +PORT_ZAlloc_stub(size_t len)
   1.190 +{
   1.191 +    STUB_SAFE_CALL1(PORT_ZAlloc_Util, len);
   1.192 +    void *ptr = malloc(len);
   1.193 +    if (ptr) {
   1.194 +	memset(ptr, 0, len);
   1.195 +    }
   1.196 +    return ptr;
   1.197 +}
   1.198 +
   1.199 +
   1.200 +extern void
   1.201 +PORT_ZFree_stub(void *ptr, size_t len)
   1.202 +{
   1.203 +    STUB_SAFE_CALL2(PORT_ZFree_Util, ptr, len);
   1.204 +    memset(ptr, 0, len);
   1.205 +    return free(ptr);
   1.206 +}
   1.207 +
   1.208 +extern void
   1.209 +PR_Free_stub(void *ptr)
   1.210 +{
   1.211 +    STUB_SAFE_CALL1(PR_Free, ptr);
   1.212 +    return free(ptr);
   1.213 +}
   1.214 +
   1.215 +/*
   1.216 + * arenas
   1.217 + *
   1.218 + */
   1.219 +extern PLArenaPool *
   1.220 +PORT_NewArena_stub(unsigned long chunksize)
   1.221 +{
   1.222 +    STUB_SAFE_CALL1(PORT_NewArena_Util, chunksize);
   1.223 +    abort();
   1.224 +    return NULL;
   1.225 +}
   1.226 +
   1.227 +extern void *
   1.228 +PORT_ArenaAlloc_stub(PLArenaPool *arena, size_t size)
   1.229 +{
   1.230 +
   1.231 +    STUB_SAFE_CALL2(PORT_ArenaZAlloc_Util, arena, size);
   1.232 +    abort();
   1.233 +    return NULL;
   1.234 +}
   1.235 +
   1.236 +extern void *
   1.237 +PORT_ArenaZAlloc_stub(PLArenaPool *arena, size_t size)
   1.238 +{
   1.239 +
   1.240 +    STUB_SAFE_CALL2(PORT_ArenaZAlloc_Util, arena, size);
   1.241 +    abort();
   1.242 +    return NULL;
   1.243 +}
   1.244 +
   1.245 +extern void    
   1.246 +PORT_FreeArena_stub(PLArenaPool *arena, PRBool zero)
   1.247 +{
   1.248 +
   1.249 +    STUB_SAFE_CALL2(PORT_FreeArena_Util, arena, zero);
   1.250 +    abort();
   1.251 +}
   1.252 +
   1.253 +
   1.254 +/* io */
   1.255 +extern PRFileDesc *
   1.256 +PR_Open_stub(const char *name, PRIntn flags, PRIntn mode)
   1.257 +{
   1.258 +    int *lfd = NULL;
   1.259 +    int fd;
   1.260 +    int lflags = 0;
   1.261 +
   1.262 +    STUB_SAFE_CALL3(PR_Open, name, flags, mode);
   1.263 +
   1.264 +    if (flags & PR_RDWR) {
   1.265 +        lflags = O_RDWR;
   1.266 +    } else if (flags & PR_WRONLY) {
   1.267 +        lflags = O_WRONLY;
   1.268 +    } else {
   1.269 +        lflags = O_RDONLY;
   1.270 +    }
   1.271 +
   1.272 +    if (flags & PR_EXCL)
   1.273 +        lflags |= O_EXCL;
   1.274 +    if (flags & PR_APPEND)
   1.275 +        lflags |= O_APPEND;
   1.276 +    if (flags & PR_TRUNCATE)
   1.277 +        lflags |= O_TRUNC;
   1.278 +
   1.279 +    fd = open(name, lflags, mode);
   1.280 +    if (fd >= 0) {
   1.281 +	lfd = PORT_New_stub(int);
   1.282 +	if (lfd != NULL) {
   1.283 +	    *lfd = fd;
   1.284 +	}
   1.285 +    }
   1.286 +    return (PRFileDesc *)lfd;
   1.287 +}
   1.288 +
   1.289 +extern PRFileDesc *
   1.290 +PR_ImportPipe_stub(PROsfd fd)
   1.291 +{
   1.292 +    int *lfd = NULL;
   1.293 +
   1.294 +    STUB_SAFE_CALL1(PR_ImportPipe, fd);
   1.295 +
   1.296 +    lfd = PORT_New_stub(int);
   1.297 +    if (lfd != NULL) {
   1.298 +	*lfd = fd;
   1.299 +    }
   1.300 +    return (PRFileDesc *)lfd;
   1.301 +}
   1.302 +
   1.303 +extern PRStatus
   1.304 +PR_Close_stub(PRFileDesc *fd)
   1.305 +{
   1.306 +    int *lfd;
   1.307 +    STUB_SAFE_CALL1(PR_Close, fd);
   1.308 +
   1.309 +    lfd = (int *)fd;
   1.310 +    close(*lfd);
   1.311 +    PORT_Free_stub(lfd);
   1.312 +    
   1.313 +    return PR_SUCCESS;
   1.314 +}
   1.315 +
   1.316 +extern PRInt32
   1.317 +PR_Read_stub(PRFileDesc *fd, void *buf, PRInt32 amount)
   1.318 +{
   1.319 +    int *lfd;
   1.320 +    STUB_SAFE_CALL3(PR_Read, fd, buf, amount);
   1.321 +    
   1.322 +    lfd = (int *)fd;
   1.323 +    return read(*lfd, buf, amount);
   1.324 +}
   1.325 +
   1.326 +extern PROffset32
   1.327 +PR_Seek_stub(PRFileDesc *fd, PROffset32 offset, PRSeekWhence whence)
   1.328 +{
   1.329 +    int *lfd;
   1.330 +    int lwhence = SEEK_SET;;
   1.331 +    STUB_SAFE_CALL3(PR_Seek, fd, offset, whence);
   1.332 +    lfd = (int *)fd;
   1.333 +    switch (whence) {
   1.334 +        case PR_SEEK_CUR:
   1.335 +            lwhence = SEEK_CUR;
   1.336 +            break;
   1.337 +        case PR_SEEK_END:
   1.338 +            lwhence = SEEK_END;
   1.339 +            break;
   1.340 +    }
   1.341 +
   1.342 +    return lseek(*lfd, offset, lwhence);
   1.343 +}
   1.344 +
   1.345 +
   1.346 +/*
   1.347 + * library 
   1.348 + */
   1.349 +extern char *
   1.350 +PR_GetLibraryFilePathname_stub(const char *name, PRFuncPtr addr)
   1.351 +{
   1.352 +    Dl_info dli;
   1.353 +    char *result;
   1.354 +
   1.355 +    STUB_SAFE_CALL2(PR_GetLibraryFilePathname, name, addr);
   1.356 +
   1.357 +    if (dladdr((void *)addr, &dli) == 0) {
   1.358 +        return NULL;
   1.359 +    }
   1.360 +    result = PORT_Alloc_stub(strlen(dli.dli_fname)+1);
   1.361 +    if (result != NULL) {
   1.362 +        strcpy(result, dli.dli_fname);
   1.363 +    }
   1.364 +    return result;
   1.365 +}
   1.366 +
   1.367 +
   1.368 +#include <errno.h>
   1.369 +
   1.370 +/* errors */
   1.371 +extern int
   1.372 +PORT_GetError_stub(void)
   1.373 +{ 
   1.374 +    STUB_SAFE_CALL0(PORT_GetError_Util);
   1.375 +    return errno;
   1.376 +}
   1.377 +
   1.378 +extern void 
   1.379 +PORT_SetError_stub(int value)
   1.380 +{ 
   1.381 +    STUB_SAFE_CALL1(PORT_SetError_Util, value);
   1.382 +    errno = value;
   1.383 +}
   1.384 +
   1.385 +/* misc */
   1.386 +extern void
   1.387 +PR_Assert_stub(const char *s, const char *file, PRIntn ln)
   1.388 +{
   1.389 +    STUB_SAFE_CALL3(PR_Assert, s, file, ln);
   1.390 +    fprintf(stderr, "%s line %d: %s\n", file, ln, s);
   1.391 +    abort();
   1.392 +}
   1.393 +
   1.394 +/* time */
   1.395 +extern PRStatus
   1.396 +PR_Sleep_stub(PRIntervalTime ticks)
   1.397 +{
   1.398 +    STUB_SAFE_CALL1(PR_Sleep, ticks);
   1.399 +    usleep(ticks*1000); 
   1.400 +    return PR_SUCCESS;
   1.401 +}
   1.402 +
   1.403 +
   1.404 +/* locking */
   1.405 +extern PRLock *
   1.406 +PR_NewLock_stub(void)
   1.407 +{
   1.408 +    STUB_SAFE_CALL0(PR_NewLock);
   1.409 +    abort();
   1.410 +    return NULL;
   1.411 +}
   1.412 +
   1.413 +extern PRStatus
   1.414 +PR_Unlock_stub(PRLock *lock)
   1.415 +{
   1.416 +    STUB_SAFE_CALL1(PR_Unlock, lock);
   1.417 +    abort();
   1.418 +    return PR_FAILURE;
   1.419 +}
   1.420 +
   1.421 +extern void
   1.422 +PR_Lock_stub(PRLock *lock)
   1.423 +{
   1.424 +    STUB_SAFE_CALL1(PR_Lock, lock);
   1.425 +    abort();
   1.426 +    return;
   1.427 +}
   1.428 +
   1.429 +extern void
   1.430 +PR_DestroyLock_stub(PRLock *lock)
   1.431 +{
   1.432 +    STUB_SAFE_CALL1(PR_DestroyLock, lock);
   1.433 +    abort();
   1.434 +    return;
   1.435 +}
   1.436 +
   1.437 +extern PRCondVar *
   1.438 +PR_NewCondVar_stub(PRLock *lock)
   1.439 +{
   1.440 +    STUB_SAFE_CALL1(PR_NewCondVar, lock);
   1.441 +    abort();
   1.442 +    return NULL;
   1.443 +}
   1.444 +
   1.445 +extern PRStatus
   1.446 +PR_NotifyCondVar_stub(PRCondVar *cvar)
   1.447 +{
   1.448 +    STUB_SAFE_CALL1(PR_NotifyCondVar, cvar);
   1.449 +    abort();
   1.450 +    return PR_FAILURE;
   1.451 +}
   1.452 +
   1.453 +extern PRStatus
   1.454 +PR_NotifyAllCondVar_stub(PRCondVar *cvar)
   1.455 +{
   1.456 +    STUB_SAFE_CALL1(PR_NotifyAllCondVar, cvar);
   1.457 +    abort();
   1.458 +    return PR_FAILURE;
   1.459 +}
   1.460 +
   1.461 +extern PRStatus
   1.462 +PR_WaitCondVar_stub(PRCondVar *cvar, PRIntervalTime timeout)
   1.463 +{
   1.464 +    STUB_SAFE_CALL2(PR_WaitCondVar, cvar, timeout);
   1.465 +    abort();
   1.466 +    return PR_FAILURE;
   1.467 +}
   1.468 +
   1.469 +
   1.470 +
   1.471 +extern void
   1.472 +PR_DestroyCondVar_stub(PRCondVar *cvar)
   1.473 +{
   1.474 +    STUB_SAFE_CALL1(PR_DestroyCondVar, cvar);
   1.475 +    abort();
   1.476 +    return;
   1.477 +}
   1.478 +
   1.479 +/*
   1.480 + * NOTE: this presupposes GCC 4.1
   1.481 + */
   1.482 +extern PRStatus
   1.483 +PR_CallOnce_stub(PRCallOnceType *once, PRCallOnceFN func)
   1.484 +{
   1.485 +    STUB_SAFE_CALL2(PR_CallOnce, once, func);
   1.486 +    abort();
   1.487 +    return PR_FAILURE;
   1.488 +}
   1.489 +
   1.490 +
   1.491 +/*
   1.492 + * SECITEMS implement Item Utilities
   1.493 + */
   1.494 +extern void
   1.495 +SECITEM_FreeItem_stub(SECItem *zap, PRBool freeit)
   1.496 +{
   1.497 +    STUB_SAFE_CALL2(SECITEM_FreeItem_Util, zap, freeit);
   1.498 +    abort();
   1.499 +}
   1.500 +
   1.501 +extern SECItem *
   1.502 +SECITEM_AllocItem_stub(PLArenaPool *arena, SECItem *item, unsigned int len)
   1.503 +{
   1.504 +    STUB_SAFE_CALL3(SECITEM_AllocItem_Util, arena, item, len); 
   1.505 +    abort();
   1.506 +    return NULL;
   1.507 +}
   1.508 +
   1.509 +extern SECComparison
   1.510 +SECITEM_CompareItem_stub(const SECItem *a, const SECItem *b) 
   1.511 +{
   1.512 +    STUB_SAFE_CALL2(SECITEM_CompareItem_Util, a, b);
   1.513 +    abort();
   1.514 +    return SECEqual;
   1.515 +}
   1.516 +
   1.517 +extern SECStatus
   1.518 +SECITEM_CopyItem_stub(PLArenaPool *arena, SECItem *to, const SECItem *from)
   1.519 +{
   1.520 +    STUB_SAFE_CALL3(SECITEM_CopyItem_Util, arena, to, from);
   1.521 +    abort();
   1.522 +    return SECFailure;
   1.523 +}
   1.524 +
   1.525 +extern SECOidTag
   1.526 +SECOID_FindOIDTag_stub(const SECItem *oid)
   1.527 +{
   1.528 +    STUB_SAFE_CALL1(SECOID_FindOIDTag_Util, oid);
   1.529 +    abort();
   1.530 +    return SEC_OID_UNKNOWN;
   1.531 +}
   1.532 +
   1.533 +extern void
   1.534 +SECITEM_ZfreeItem_stub(SECItem *zap, PRBool freeit)
   1.535 +{
   1.536 +    STUB_SAFE_CALL2(SECITEM_ZfreeItem_Util, zap, freeit);
   1.537 +    abort();
   1.538 +}
   1.539 +
   1.540 +extern int
   1.541 +NSS_SecureMemcmp_stub(const void *a, const void *b, size_t n)
   1.542 +{
   1.543 +    STUB_SAFE_CALL3(NSS_SecureMemcmp, a, b, n);
   1.544 +    abort();
   1.545 +}
   1.546 +
   1.547 +#ifdef FREEBL_NO_WEAK
   1.548 +
   1.549 +static const char *nsprLibName = SHLIB_PREFIX"nspr4."SHLIB_SUFFIX;
   1.550 +static const char *nssutilLibName = SHLIB_PREFIX"nssutil3."SHLIB_SUFFIX;
   1.551 +
   1.552 +static SECStatus
   1.553 +freebl_InitNSPR(void *lib)
   1.554 +{
   1.555 +    STUB_FETCH_FUNCTION(PR_Free);
   1.556 +    STUB_FETCH_FUNCTION(PR_Open);
   1.557 +    STUB_FETCH_FUNCTION(PR_ImportPipe);
   1.558 +    STUB_FETCH_FUNCTION(PR_Close);
   1.559 +    STUB_FETCH_FUNCTION(PR_Read);
   1.560 +    STUB_FETCH_FUNCTION(PR_Seek);
   1.561 +    STUB_FETCH_FUNCTION(PR_GetLibraryFilePathname);
   1.562 +    STUB_FETCH_FUNCTION(PR_Assert);
   1.563 +    STUB_FETCH_FUNCTION(PR_Sleep);
   1.564 +    STUB_FETCH_FUNCTION(PR_CallOnce);
   1.565 +    STUB_FETCH_FUNCTION(PR_NewCondVar);
   1.566 +    STUB_FETCH_FUNCTION(PR_NotifyCondVar);
   1.567 +    STUB_FETCH_FUNCTION(PR_NotifyAllCondVar);
   1.568 +    STUB_FETCH_FUNCTION(PR_WaitCondVar);
   1.569 +    STUB_FETCH_FUNCTION(PR_DestroyCondVar);
   1.570 +    STUB_FETCH_FUNCTION(PR_NewLock);
   1.571 +    STUB_FETCH_FUNCTION(PR_Unlock);
   1.572 +    STUB_FETCH_FUNCTION(PR_Lock);
   1.573 +    STUB_FETCH_FUNCTION(PR_DestroyLock);
   1.574 +    return SECSuccess;
   1.575 +}
   1.576 +
   1.577 +static SECStatus
   1.578 +freebl_InitNSSUtil(void *lib)
   1.579 +{
   1.580 +    STUB_FETCH_FUNCTION(PORT_Alloc_Util);
   1.581 +    STUB_FETCH_FUNCTION(PORT_Free_Util);
   1.582 +    STUB_FETCH_FUNCTION(PORT_ZAlloc_Util);
   1.583 +    STUB_FETCH_FUNCTION(PORT_ZFree_Util);
   1.584 +    STUB_FETCH_FUNCTION(PORT_NewArena_Util);
   1.585 +    STUB_FETCH_FUNCTION(PORT_ArenaAlloc_Util);
   1.586 +    STUB_FETCH_FUNCTION(PORT_ArenaZAlloc_Util);
   1.587 +    STUB_FETCH_FUNCTION(PORT_FreeArena_Util);
   1.588 +    STUB_FETCH_FUNCTION(PORT_GetError_Util);
   1.589 +    STUB_FETCH_FUNCTION(PORT_SetError_Util);
   1.590 +    STUB_FETCH_FUNCTION(SECITEM_FreeItem_Util);
   1.591 +    STUB_FETCH_FUNCTION(SECITEM_AllocItem_Util);
   1.592 +    STUB_FETCH_FUNCTION(SECITEM_CompareItem_Util);
   1.593 +    STUB_FETCH_FUNCTION(SECITEM_CopyItem_Util);
   1.594 +    STUB_FETCH_FUNCTION(SECITEM_ZfreeItem_Util);
   1.595 +    STUB_FETCH_FUNCTION(SECOID_FindOIDTag_Util);
   1.596 +    STUB_FETCH_FUNCTION(NSS_SecureMemcmp);
   1.597 +    return SECSuccess;
   1.598 +}
   1.599 +
   1.600 +/*
   1.601 + * fetch the library if it's loaded. For NSS it should already be loaded
   1.602 + */
   1.603 +#define freebl_getLibrary(libName)  \
   1.604 +    dlopen (libName, RTLD_LAZY|RTLD_NOLOAD)
   1.605 +
   1.606 +#define freebl_releaseLibrary(lib) \
   1.607 +    if (lib) dlclose(lib)
   1.608 +
   1.609 +static void * FREEBLnsprGlobalLib = NULL;
   1.610 +static void * FREEBLnssutilGlobalLib = NULL;
   1.611 +
   1.612 +void __attribute ((destructor)) FREEBL_unload()
   1.613 +{
   1.614 +    freebl_releaseLibrary(FREEBLnsprGlobalLib);
   1.615 +    freebl_releaseLibrary(FREEBLnssutilGlobalLib);
   1.616 +}
   1.617 +#endif
   1.618 +
   1.619 +/*
   1.620 + * load the symbols from the real libraries if available.
   1.621 + * 
   1.622 + * if force is set, explicitly load the libraries if they are not already
   1.623 + * loaded. If we could not use the real libraries, return failure.
   1.624 + */
   1.625 +extern SECStatus
   1.626 +FREEBL_InitStubs()
   1.627 +{
   1.628 +    SECStatus rv = SECSuccess;
   1.629 +#ifdef FREEBL_NO_WEAK
   1.630 +    void *nspr = NULL; 
   1.631 +    void *nssutil = NULL; 
   1.632 +
   1.633 +    /* NSPR should be first */
   1.634 +    if (!FREEBLnsprGlobalLib) {
   1.635 +	nspr = freebl_getLibrary(nsprLibName);
   1.636 +	if (!nspr) {
   1.637 +	    return SECFailure;
   1.638 +	}
   1.639 +	rv = freebl_InitNSPR(nspr);
   1.640 +	if (rv != SECSuccess) {
   1.641 +	    freebl_releaseLibrary(nspr);
   1.642 +	    return rv;
   1.643 +	}
   1.644 +	FREEBLnsprGlobalLib = nspr; /* adopt */
   1.645 +    }
   1.646 +    /* now load NSSUTIL */
   1.647 +    if (!FREEBLnssutilGlobalLib) {
   1.648 +	nssutil= freebl_getLibrary(nssutilLibName);
   1.649 +	if (!nssutil) {
   1.650 +	    return SECFailure;
   1.651 +	}
   1.652 +	rv = freebl_InitNSSUtil(nssutil);
   1.653 +	if (rv != SECSuccess) {
   1.654 +	    freebl_releaseLibrary(nssutil);
   1.655 +	    return rv;
   1.656 +	}
   1.657 +	FREEBLnssutilGlobalLib = nssutil; /* adopt */
   1.658 +    }
   1.659 +#endif
   1.660 +
   1.661 +    return rv;
   1.662 +}

mercurial