pkix_lifecycle.c (6695B)
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 */ 10 11 #include "pkix_lifecycle.h" 12 13 static PKIX_Boolean pkixIsInitialized; 14 15 /* Lock used by Logger - is reentrant by the same thread */ 16 extern PKIX_PL_MonitorLock *pkixLoggerLock; 17 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; 30 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; 38 39 extern PKIX_List *pkixLoggers; 40 extern PKIX_List *pkixLoggersErrors; 41 extern PKIX_List *pkixLoggersDebugTrace; 42 43 /* --Public-Functions--------------------------------------------- */ 44 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; 58 59 PKIX_ENTER(LIFECYCLE, "PKIX_Initialize"); 60 PKIX_NULLCHECK_ONE(pPlContext); 61 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 } 70 71 PKIX_CHECK(PKIX_PL_Initialize 72 (platformInitNeeded, PKIX_FALSE, &plContext), 73 PKIX_INITIALIZEFAILED); 74 75 *pPlContext = plContext; 76 77 if (desiredMajorVersion != PKIX_MAJOR_VERSION){ 78 PKIX_ERROR(PKIX_MAJORVERSIONSDONTMATCH); 79 } 80 81 if ((minDesiredMinorVersion > PKIX_MINOR_VERSION) || 82 (maxDesiredMinorVersion < PKIX_MINOR_VERSION)){ 83 PKIX_ERROR(PKIX_MINORVERSIONNOTBETWEENDESIREDMINANDMAX); 84 } 85 86 *pActualMinorVersion = PKIX_MINOR_VERSION; 87 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); 94 95 PKIX_CHECK(PKIX_PL_HashTable_Create 96 (32, 0, &cachedCrlSigTable, plContext), 97 PKIX_HASHTABLECREATEFAILED); 98 99 PKIX_CHECK(PKIX_PL_HashTable_Create 100 (32, 10, &cachedCertChainTable, plContext), 101 PKIX_HASHTABLECREATEFAILED); 102 103 PKIX_CHECK(PKIX_PL_HashTable_Create 104 (32, 10, &cachedCertTable, plContext), 105 PKIX_HASHTABLECREATEFAILED); 106 107 PKIX_CHECK(PKIX_PL_HashTable_Create 108 (32, 10, &cachedCrlEntryTable, plContext), 109 PKIX_HASHTABLECREATEFAILED); 110 111 PKIX_CHECK(PKIX_PL_HashTable_Create 112 (5, 5, &aiaConnectionCache, plContext), 113 PKIX_HASHTABLECREATEFAILED); 114 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 } 132 133 fnStackNameArr = PORT_ZNewArray(char*, MAX_STACK_DEPTH); 134 if (!fnStackNameArr) { 135 PKIX_ERROR(PKIX_HASHTABLECREATEFAILED); 136 } 137 138 fnStackInvCountArr = PORT_ZNewArray(PKIX_UInt32, MAX_STACK_DEPTH); 139 if (!fnStackInvCountArr) { 140 PKIX_ERROR(PKIX_HASHTABLECREATEFAILED); 141 } 142 #endif /* PKIX_OBJECT_LEAK_TEST */ 143 144 pkixIsInitialized = PKIX_TRUE; 145 146 cleanup: 147 148 PKIX_RETURN(LIFECYCLE); 149 } 150 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; 160 161 PKIX_ENTER(LIFECYCLE, "PKIX_Shutdown"); 162 163 if (!pkixIsInitialized){ 164 /* The library was not initialized */ 165 PKIX_RETURN(LIFECYCLE); 166 } 167 168 pkixIsInitialized = PKIX_FALSE; 169 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); 182 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); 191 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 } 197 198 PKIX_CHECK(PKIX_PL_Shutdown(plContext), 199 PKIX_SHUTDOWNFAILED); 200 201 #ifdef PKIX_OBJECT_LEAK_TEST 202 PORT_Free(fnStackInvCountArr); 203 PORT_Free(fnStackNameArr); 204 PL_HashTableDestroy(fnInvTable); 205 #endif 206 207 cleanup: 208 209 PKIX_RETURN(LIFECYCLE); 210 }