security/nss/lib/libpkix/pkix/top/pkix_lifecycle.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rwxr-xr-x

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     4 /*
     5  * pkix_lifecycle.c
     6  *
     7  * Top level initialize and shutdown functions
     8  *
     9  */
    11 #include "pkix_lifecycle.h"
    13 static PKIX_Boolean pkixIsInitialized;
    15 /* Lock used by Logger - is reentrant by the same thread */
    16 extern PKIX_PL_MonitorLock *pkixLoggerLock;
    18 /* 
    19  * Following pkix_* variables are for debugging purpose. They should be taken
    20  * out eventually. The purpose is to verify cache tables usage (via debugger).
    21  */
    22 int pkix_ccAddCount = 0;
    23 int pkix_ccLookupCount = 0;
    24 int pkix_ccRemoveCount = 0;
    25 int pkix_cAddCount = 0;
    26 int pkix_cLookupCount = 0;
    27 int pkix_cRemoveCount = 0;
    28 int pkix_ceAddCount = 0;
    29 int pkix_ceLookupCount = 0;
    31 PKIX_PL_HashTable *cachedCrlSigTable = NULL;
    32 PKIX_PL_HashTable *cachedCertSigTable = NULL;
    33 PKIX_PL_HashTable *cachedCertChainTable = NULL;
    34 PKIX_PL_HashTable *cachedCertTable = NULL;
    35 PKIX_PL_HashTable *cachedCrlEntryTable = NULL;
    36 PKIX_PL_HashTable *aiaConnectionCache = NULL;
    37 PKIX_PL_HashTable *httpSocketCache = NULL;
    39 extern PKIX_List *pkixLoggers;
    40 extern PKIX_List *pkixLoggersErrors;
    41 extern PKIX_List *pkixLoggersDebugTrace;
    43 /* --Public-Functions--------------------------------------------- */
    45 /*
    46  * FUNCTION: PKIX_Initialize (see comments in pkix.h)
    47  */
    48 PKIX_Error *
    49 PKIX_Initialize(
    50         PKIX_Boolean platformInitNeeded,
    51         PKIX_UInt32 desiredMajorVersion,
    52         PKIX_UInt32 minDesiredMinorVersion,
    53         PKIX_UInt32 maxDesiredMinorVersion,
    54         PKIX_UInt32 *pActualMinorVersion,
    55         void **pPlContext)
    56 {
    57         void *plContext = NULL;
    59         PKIX_ENTER(LIFECYCLE, "PKIX_Initialize");
    60         PKIX_NULLCHECK_ONE(pPlContext);
    62         /*
    63          * If we are called a second time other than in the situation handled
    64          * above, we return a positive status.
    65          */
    66         if (pkixIsInitialized){
    67                 /* Already initialized */
    68                 PKIX_RETURN(LIFECYCLE);
    69         }
    71         PKIX_CHECK(PKIX_PL_Initialize
    72                 (platformInitNeeded, PKIX_FALSE, &plContext),
    73                 PKIX_INITIALIZEFAILED);
    75         *pPlContext = plContext;
    77         if (desiredMajorVersion != PKIX_MAJOR_VERSION){
    78                 PKIX_ERROR(PKIX_MAJORVERSIONSDONTMATCH);
    79         }
    81         if ((minDesiredMinorVersion > PKIX_MINOR_VERSION) ||
    82             (maxDesiredMinorVersion < PKIX_MINOR_VERSION)){
    83                 PKIX_ERROR(PKIX_MINORVERSIONNOTBETWEENDESIREDMINANDMAX);
    84         }
    86         *pActualMinorVersion = PKIX_MINOR_VERSION;
    88         /* Create Cache Tables
    89          * Do not initialize hash tables for object leak test */
    90 #if !defined(PKIX_OBJECT_LEAK_TEST)
    91         PKIX_CHECK(PKIX_PL_HashTable_Create
    92                    (32, 0, &cachedCertSigTable, plContext),
    93                    PKIX_HASHTABLECREATEFAILED);
    95         PKIX_CHECK(PKIX_PL_HashTable_Create
    96                    (32, 0, &cachedCrlSigTable, plContext),
    97                    PKIX_HASHTABLECREATEFAILED);
    99         PKIX_CHECK(PKIX_PL_HashTable_Create
   100                        (32, 10, &cachedCertChainTable, plContext),
   101                    PKIX_HASHTABLECREATEFAILED);
   103         PKIX_CHECK(PKIX_PL_HashTable_Create
   104                        (32, 10, &cachedCertTable, plContext),
   105                    PKIX_HASHTABLECREATEFAILED);
   107         PKIX_CHECK(PKIX_PL_HashTable_Create
   108                    (32, 10, &cachedCrlEntryTable, plContext),
   109                    PKIX_HASHTABLECREATEFAILED);
   111         PKIX_CHECK(PKIX_PL_HashTable_Create
   112                    (5, 5, &aiaConnectionCache, plContext),
   113                    PKIX_HASHTABLECREATEFAILED);
   115 #ifdef PKIX_SOCKETCACHE
   116         PKIX_CHECK(PKIX_PL_HashTable_Create
   117                    (5, 5, &httpSocketCache, plContext),
   118                    PKIX_HASHTABLECREATEFAILED);
   119 #endif
   120         if (pkixLoggerLock == NULL) {
   121             PKIX_CHECK(PKIX_PL_MonitorLock_Create
   122                        (&pkixLoggerLock, plContext),
   123                        PKIX_MONITORLOCKCREATEFAILED);
   124         }
   125 #else
   126         fnInvTable = PL_NewHashTable(0, pkix_ErrorGen_Hash,
   127                                      PL_CompareValues,
   128                                      PL_CompareValues, NULL, NULL);
   129         if (!fnInvTable) {
   130             PKIX_ERROR(PKIX_HASHTABLECREATEFAILED);
   131         }
   133         fnStackNameArr = PORT_ZNewArray(char*, MAX_STACK_DEPTH);
   134         if (!fnStackNameArr) {
   135             PKIX_ERROR(PKIX_HASHTABLECREATEFAILED);
   136         }
   138         fnStackInvCountArr = PORT_ZNewArray(PKIX_UInt32, MAX_STACK_DEPTH);
   139         if (!fnStackInvCountArr) {
   140             PKIX_ERROR(PKIX_HASHTABLECREATEFAILED);
   141         }
   142 #endif /* PKIX_OBJECT_LEAK_TEST */
   144         pkixIsInitialized = PKIX_TRUE;
   146 cleanup:
   148         PKIX_RETURN(LIFECYCLE);
   149 }
   151 /*
   152  * FUNCTION: PKIX_Shutdown (see comments in pkix.h)
   153  */
   154 PKIX_Error *
   155 PKIX_Shutdown(void *plContext)
   156 {
   157         PKIX_List *savedPkixLoggers = NULL;
   158         PKIX_List *savedPkixLoggersErrors = NULL;
   159         PKIX_List *savedPkixLoggersDebugTrace = NULL;
   161         PKIX_ENTER(LIFECYCLE, "PKIX_Shutdown");
   163         if (!pkixIsInitialized){
   164                 /* The library was not initialized */
   165                 PKIX_RETURN(LIFECYCLE);
   166         }
   168         pkixIsInitialized = PKIX_FALSE;
   170         if (pkixLoggers) {
   171                 savedPkixLoggers = pkixLoggers;
   172                 savedPkixLoggersErrors = pkixLoggersErrors;
   173                 savedPkixLoggersDebugTrace = pkixLoggersDebugTrace;
   174                 pkixLoggers = NULL;
   175                 pkixLoggersErrors = NULL;
   176                 pkixLoggersDebugTrace = NULL;
   177                 PKIX_DECREF(savedPkixLoggers);
   178                 PKIX_DECREF(savedPkixLoggersErrors);
   179                 PKIX_DECREF(savedPkixLoggersDebugTrace);
   180         }
   181         PKIX_DECREF(pkixLoggerLock);
   183         /* Destroy Cache Tables */
   184         PKIX_DECREF(cachedCertSigTable);
   185         PKIX_DECREF(cachedCrlSigTable);
   186         PKIX_DECREF(cachedCertChainTable);
   187         PKIX_DECREF(cachedCertTable);
   188         PKIX_DECREF(cachedCrlEntryTable);
   189         PKIX_DECREF(aiaConnectionCache);
   190         PKIX_DECREF(httpSocketCache);
   192         /* Clean up any temporary errors that happened during shutdown */
   193         if (pkixErrorList) {
   194             PKIX_PL_Object_DecRef((PKIX_PL_Object*)pkixErrorList, plContext);
   195             pkixErrorList = NULL;
   196         }
   198         PKIX_CHECK(PKIX_PL_Shutdown(plContext),
   199                 PKIX_SHUTDOWNFAILED);
   201 #ifdef PKIX_OBJECT_LEAK_TEST
   202         PORT_Free(fnStackInvCountArr);
   203         PORT_Free(fnStackNameArr);
   204         PL_HashTableDestroy(fnInvTable);
   205 #endif
   207 cleanup:
   209         PKIX_RETURN(LIFECYCLE);
   210 }

mercurial