security/nss/lib/ckfw/mutex.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 /*
     6  * mutex.c
     7  *
     8  * This file implements a mutual-exclusion locking facility for Modules
     9  * using the NSS Cryptoki Framework.
    10  */
    12 #ifndef CK_T
    13 #include "ck.h"
    14 #endif /* CK_T */
    16 /*
    17  * NSSCKFWMutex
    18  *
    19  *  NSSCKFWMutex_Destroy
    20  *  NSSCKFWMutex_Lock
    21  *  NSSCKFWMutex_Unlock
    22  *
    23  *  nssCKFWMutex_Create
    24  *  nssCKFWMutex_Destroy
    25  *  nssCKFWMutex_Lock
    26  *  nssCKFWMutex_Unlock
    27  *
    28  *  -- debugging versions only --
    29  *  nssCKFWMutex_verifyPointer
    30  *
    31  */
    33 struct NSSCKFWMutexStr {
    34   PRLock *lock;
    35 };
    37 #ifdef DEBUG
    38 /*
    39  * But first, the pointer-tracking stuff.
    40  *
    41  * NOTE: the pointer-tracking support in NSS/base currently relies
    42  * upon NSPR's CallOnce support.  That, however, relies upon NSPR's
    43  * locking, which is tied into the runtime.  We need a pointer-tracker
    44  * implementation that uses the locks supplied through C_Initialize.
    45  * That support, however, can be filled in later.  So for now, I'll
    46  * just do this routines as no-ops.
    47  */
    49 static CK_RV
    50 mutex_add_pointer
    51 (
    52   const NSSCKFWMutex *fwMutex
    53 )
    54 {
    55   return CKR_OK;
    56 }
    58 static CK_RV
    59 mutex_remove_pointer
    60 (
    61   const NSSCKFWMutex *fwMutex
    62 )
    63 {
    64   return CKR_OK;
    65 }
    67 NSS_IMPLEMENT CK_RV
    68 nssCKFWMutex_verifyPointer
    69 (
    70   const NSSCKFWMutex *fwMutex
    71 )
    72 {
    73   return CKR_OK;
    74 }
    76 #endif /* DEBUG */
    78 /*
    79  * nssCKFWMutex_Create
    80  *
    81  */
    82 NSS_EXTERN NSSCKFWMutex *
    83 nssCKFWMutex_Create
    84 (
    85   CK_C_INITIALIZE_ARGS_PTR pInitArgs,
    86   CryptokiLockingState LockingState,
    87   NSSArena *arena,
    88   CK_RV *pError
    89 )
    90 {
    91   NSSCKFWMutex *mutex;
    93   mutex = nss_ZNEW(arena, NSSCKFWMutex);
    94   if (!mutex) {
    95     *pError = CKR_HOST_MEMORY;
    96     return (NSSCKFWMutex *)NULL;
    97   }
    98   *pError = CKR_OK;
    99   mutex->lock = NULL;
   100   if (LockingState == MultiThreaded) {
   101     mutex->lock = PR_NewLock();
   102     if (!mutex->lock) {
   103       *pError = CKR_HOST_MEMORY; /* we couldn't get the resource */
   104     }
   105   }
   107   if( CKR_OK != *pError ) {
   108     (void)nss_ZFreeIf(mutex);
   109     return (NSSCKFWMutex *)NULL;
   110   }
   112 #ifdef DEBUG
   113   *pError = mutex_add_pointer(mutex);
   114   if( CKR_OK != *pError ) {
   115     if (mutex->lock) {
   116       PR_DestroyLock(mutex->lock);
   117     }
   118     (void)nss_ZFreeIf(mutex);
   119     return (NSSCKFWMutex *)NULL;
   120   }
   121 #endif /* DEBUG */
   123   return mutex;
   124 }  
   126 /*
   127  * nssCKFWMutex_Destroy
   128  *
   129  */
   130 NSS_EXTERN CK_RV
   131 nssCKFWMutex_Destroy
   132 (
   133   NSSCKFWMutex *mutex
   134 )
   135 {
   136   CK_RV rv = CKR_OK;
   138 #ifdef NSSDEBUG
   139   rv = nssCKFWMutex_verifyPointer(mutex);
   140   if( CKR_OK != rv ) {
   141     return rv;
   142   }
   143 #endif /* NSSDEBUG */
   145   if (mutex->lock) {
   146     PR_DestroyLock(mutex->lock);
   147   } 
   149 #ifdef DEBUG
   150   (void)mutex_remove_pointer(mutex);
   151 #endif /* DEBUG */
   153   (void)nss_ZFreeIf(mutex);
   154   return rv;
   155 }
   157 /*
   158  * nssCKFWMutex_Lock
   159  *
   160  */
   161 NSS_EXTERN CK_RV
   162 nssCKFWMutex_Lock
   163 (
   164   NSSCKFWMutex *mutex
   165 )
   166 {
   167 #ifdef NSSDEBUG
   168   CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
   169   if( CKR_OK != rv ) {
   170     return rv;
   171   }
   172 #endif /* NSSDEBUG */
   173   if (mutex->lock) {
   174     PR_Lock(mutex->lock);
   175   }
   177   return CKR_OK;
   178 }
   180 /*
   181  * nssCKFWMutex_Unlock
   182  *
   183  */
   184 NSS_EXTERN CK_RV
   185 nssCKFWMutex_Unlock
   186 (
   187   NSSCKFWMutex *mutex
   188 )
   189 {
   190   PRStatus nrv;
   191 #ifdef NSSDEBUG
   192   CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
   194   if( CKR_OK != rv ) {
   195     return rv;
   196   }
   197 #endif /* NSSDEBUG */
   199   if (!mutex->lock) 
   200     return CKR_OK;
   202   nrv =  PR_Unlock(mutex->lock);
   204   /* if unlock fails, either we have a programming error, or we have
   205    * some sort of hardware failure... in either case return CKR_DEVICE_ERROR.
   206    */
   207   return nrv == PR_SUCCESS ? CKR_OK : CKR_DEVICE_ERROR;
   208 }
   210 /*
   211  * NSSCKFWMutex_Destroy
   212  *
   213  */
   214 NSS_EXTERN CK_RV
   215 NSSCKFWMutex_Destroy
   216 (
   217   NSSCKFWMutex *mutex
   218 )
   219 {
   220 #ifdef DEBUG
   221   CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
   222   if( CKR_OK != rv ) {
   223     return rv;
   224   }
   225 #endif /* DEBUG */
   227   return nssCKFWMutex_Destroy(mutex);
   228 }
   230 /*
   231  * NSSCKFWMutex_Lock
   232  *
   233  */
   234 NSS_EXTERN CK_RV
   235 NSSCKFWMutex_Lock
   236 (
   237   NSSCKFWMutex *mutex
   238 )
   239 {
   240 #ifdef DEBUG
   241   CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
   242   if( CKR_OK != rv ) {
   243     return rv;
   244   }
   245 #endif /* DEBUG */
   247   return nssCKFWMutex_Lock(mutex);
   248 }
   250 /*
   251  * NSSCKFWMutex_Unlock
   252  *
   253  */
   254 NSS_EXTERN CK_RV
   255 NSSCKFWMutex_Unlock
   256 (
   257   NSSCKFWMutex *mutex
   258 )
   259 {
   260 #ifdef DEBUG
   261   CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
   262   if( CKR_OK != rv ) {
   263     return rv;
   264   }
   265 #endif /* DEBUG */
   267   return nssCKFWMutex_Unlock(mutex);
   268 }

mercurial