Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | /* |
michael@0 | 5 | * pkix_lifecycle.c |
michael@0 | 6 | * |
michael@0 | 7 | * Top level initialize and shutdown functions |
michael@0 | 8 | * |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "pkix_lifecycle.h" |
michael@0 | 12 | |
michael@0 | 13 | static PKIX_Boolean pkixIsInitialized; |
michael@0 | 14 | |
michael@0 | 15 | /* Lock used by Logger - is reentrant by the same thread */ |
michael@0 | 16 | extern PKIX_PL_MonitorLock *pkixLoggerLock; |
michael@0 | 17 | |
michael@0 | 18 | /* |
michael@0 | 19 | * Following pkix_* variables are for debugging purpose. They should be taken |
michael@0 | 20 | * out eventually. The purpose is to verify cache tables usage (via debugger). |
michael@0 | 21 | */ |
michael@0 | 22 | int pkix_ccAddCount = 0; |
michael@0 | 23 | int pkix_ccLookupCount = 0; |
michael@0 | 24 | int pkix_ccRemoveCount = 0; |
michael@0 | 25 | int pkix_cAddCount = 0; |
michael@0 | 26 | int pkix_cLookupCount = 0; |
michael@0 | 27 | int pkix_cRemoveCount = 0; |
michael@0 | 28 | int pkix_ceAddCount = 0; |
michael@0 | 29 | int pkix_ceLookupCount = 0; |
michael@0 | 30 | |
michael@0 | 31 | PKIX_PL_HashTable *cachedCrlSigTable = NULL; |
michael@0 | 32 | PKIX_PL_HashTable *cachedCertSigTable = NULL; |
michael@0 | 33 | PKIX_PL_HashTable *cachedCertChainTable = NULL; |
michael@0 | 34 | PKIX_PL_HashTable *cachedCertTable = NULL; |
michael@0 | 35 | PKIX_PL_HashTable *cachedCrlEntryTable = NULL; |
michael@0 | 36 | PKIX_PL_HashTable *aiaConnectionCache = NULL; |
michael@0 | 37 | PKIX_PL_HashTable *httpSocketCache = NULL; |
michael@0 | 38 | |
michael@0 | 39 | extern PKIX_List *pkixLoggers; |
michael@0 | 40 | extern PKIX_List *pkixLoggersErrors; |
michael@0 | 41 | extern PKIX_List *pkixLoggersDebugTrace; |
michael@0 | 42 | |
michael@0 | 43 | /* --Public-Functions--------------------------------------------- */ |
michael@0 | 44 | |
michael@0 | 45 | /* |
michael@0 | 46 | * FUNCTION: PKIX_Initialize (see comments in pkix.h) |
michael@0 | 47 | */ |
michael@0 | 48 | PKIX_Error * |
michael@0 | 49 | PKIX_Initialize( |
michael@0 | 50 | PKIX_Boolean platformInitNeeded, |
michael@0 | 51 | PKIX_UInt32 desiredMajorVersion, |
michael@0 | 52 | PKIX_UInt32 minDesiredMinorVersion, |
michael@0 | 53 | PKIX_UInt32 maxDesiredMinorVersion, |
michael@0 | 54 | PKIX_UInt32 *pActualMinorVersion, |
michael@0 | 55 | void **pPlContext) |
michael@0 | 56 | { |
michael@0 | 57 | void *plContext = NULL; |
michael@0 | 58 | |
michael@0 | 59 | PKIX_ENTER(LIFECYCLE, "PKIX_Initialize"); |
michael@0 | 60 | PKIX_NULLCHECK_ONE(pPlContext); |
michael@0 | 61 | |
michael@0 | 62 | /* |
michael@0 | 63 | * If we are called a second time other than in the situation handled |
michael@0 | 64 | * above, we return a positive status. |
michael@0 | 65 | */ |
michael@0 | 66 | if (pkixIsInitialized){ |
michael@0 | 67 | /* Already initialized */ |
michael@0 | 68 | PKIX_RETURN(LIFECYCLE); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | PKIX_CHECK(PKIX_PL_Initialize |
michael@0 | 72 | (platformInitNeeded, PKIX_FALSE, &plContext), |
michael@0 | 73 | PKIX_INITIALIZEFAILED); |
michael@0 | 74 | |
michael@0 | 75 | *pPlContext = plContext; |
michael@0 | 76 | |
michael@0 | 77 | if (desiredMajorVersion != PKIX_MAJOR_VERSION){ |
michael@0 | 78 | PKIX_ERROR(PKIX_MAJORVERSIONSDONTMATCH); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | if ((minDesiredMinorVersion > PKIX_MINOR_VERSION) || |
michael@0 | 82 | (maxDesiredMinorVersion < PKIX_MINOR_VERSION)){ |
michael@0 | 83 | PKIX_ERROR(PKIX_MINORVERSIONNOTBETWEENDESIREDMINANDMAX); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | *pActualMinorVersion = PKIX_MINOR_VERSION; |
michael@0 | 87 | |
michael@0 | 88 | /* Create Cache Tables |
michael@0 | 89 | * Do not initialize hash tables for object leak test */ |
michael@0 | 90 | #if !defined(PKIX_OBJECT_LEAK_TEST) |
michael@0 | 91 | PKIX_CHECK(PKIX_PL_HashTable_Create |
michael@0 | 92 | (32, 0, &cachedCertSigTable, plContext), |
michael@0 | 93 | PKIX_HASHTABLECREATEFAILED); |
michael@0 | 94 | |
michael@0 | 95 | PKIX_CHECK(PKIX_PL_HashTable_Create |
michael@0 | 96 | (32, 0, &cachedCrlSigTable, plContext), |
michael@0 | 97 | PKIX_HASHTABLECREATEFAILED); |
michael@0 | 98 | |
michael@0 | 99 | PKIX_CHECK(PKIX_PL_HashTable_Create |
michael@0 | 100 | (32, 10, &cachedCertChainTable, plContext), |
michael@0 | 101 | PKIX_HASHTABLECREATEFAILED); |
michael@0 | 102 | |
michael@0 | 103 | PKIX_CHECK(PKIX_PL_HashTable_Create |
michael@0 | 104 | (32, 10, &cachedCertTable, plContext), |
michael@0 | 105 | PKIX_HASHTABLECREATEFAILED); |
michael@0 | 106 | |
michael@0 | 107 | PKIX_CHECK(PKIX_PL_HashTable_Create |
michael@0 | 108 | (32, 10, &cachedCrlEntryTable, plContext), |
michael@0 | 109 | PKIX_HASHTABLECREATEFAILED); |
michael@0 | 110 | |
michael@0 | 111 | PKIX_CHECK(PKIX_PL_HashTable_Create |
michael@0 | 112 | (5, 5, &aiaConnectionCache, plContext), |
michael@0 | 113 | PKIX_HASHTABLECREATEFAILED); |
michael@0 | 114 | |
michael@0 | 115 | #ifdef PKIX_SOCKETCACHE |
michael@0 | 116 | PKIX_CHECK(PKIX_PL_HashTable_Create |
michael@0 | 117 | (5, 5, &httpSocketCache, plContext), |
michael@0 | 118 | PKIX_HASHTABLECREATEFAILED); |
michael@0 | 119 | #endif |
michael@0 | 120 | if (pkixLoggerLock == NULL) { |
michael@0 | 121 | PKIX_CHECK(PKIX_PL_MonitorLock_Create |
michael@0 | 122 | (&pkixLoggerLock, plContext), |
michael@0 | 123 | PKIX_MONITORLOCKCREATEFAILED); |
michael@0 | 124 | } |
michael@0 | 125 | #else |
michael@0 | 126 | fnInvTable = PL_NewHashTable(0, pkix_ErrorGen_Hash, |
michael@0 | 127 | PL_CompareValues, |
michael@0 | 128 | PL_CompareValues, NULL, NULL); |
michael@0 | 129 | if (!fnInvTable) { |
michael@0 | 130 | PKIX_ERROR(PKIX_HASHTABLECREATEFAILED); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | fnStackNameArr = PORT_ZNewArray(char*, MAX_STACK_DEPTH); |
michael@0 | 134 | if (!fnStackNameArr) { |
michael@0 | 135 | PKIX_ERROR(PKIX_HASHTABLECREATEFAILED); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | fnStackInvCountArr = PORT_ZNewArray(PKIX_UInt32, MAX_STACK_DEPTH); |
michael@0 | 139 | if (!fnStackInvCountArr) { |
michael@0 | 140 | PKIX_ERROR(PKIX_HASHTABLECREATEFAILED); |
michael@0 | 141 | } |
michael@0 | 142 | #endif /* PKIX_OBJECT_LEAK_TEST */ |
michael@0 | 143 | |
michael@0 | 144 | pkixIsInitialized = PKIX_TRUE; |
michael@0 | 145 | |
michael@0 | 146 | cleanup: |
michael@0 | 147 | |
michael@0 | 148 | PKIX_RETURN(LIFECYCLE); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | /* |
michael@0 | 152 | * FUNCTION: PKIX_Shutdown (see comments in pkix.h) |
michael@0 | 153 | */ |
michael@0 | 154 | PKIX_Error * |
michael@0 | 155 | PKIX_Shutdown(void *plContext) |
michael@0 | 156 | { |
michael@0 | 157 | PKIX_List *savedPkixLoggers = NULL; |
michael@0 | 158 | PKIX_List *savedPkixLoggersErrors = NULL; |
michael@0 | 159 | PKIX_List *savedPkixLoggersDebugTrace = NULL; |
michael@0 | 160 | |
michael@0 | 161 | PKIX_ENTER(LIFECYCLE, "PKIX_Shutdown"); |
michael@0 | 162 | |
michael@0 | 163 | if (!pkixIsInitialized){ |
michael@0 | 164 | /* The library was not initialized */ |
michael@0 | 165 | PKIX_RETURN(LIFECYCLE); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | pkixIsInitialized = PKIX_FALSE; |
michael@0 | 169 | |
michael@0 | 170 | if (pkixLoggers) { |
michael@0 | 171 | savedPkixLoggers = pkixLoggers; |
michael@0 | 172 | savedPkixLoggersErrors = pkixLoggersErrors; |
michael@0 | 173 | savedPkixLoggersDebugTrace = pkixLoggersDebugTrace; |
michael@0 | 174 | pkixLoggers = NULL; |
michael@0 | 175 | pkixLoggersErrors = NULL; |
michael@0 | 176 | pkixLoggersDebugTrace = NULL; |
michael@0 | 177 | PKIX_DECREF(savedPkixLoggers); |
michael@0 | 178 | PKIX_DECREF(savedPkixLoggersErrors); |
michael@0 | 179 | PKIX_DECREF(savedPkixLoggersDebugTrace); |
michael@0 | 180 | } |
michael@0 | 181 | PKIX_DECREF(pkixLoggerLock); |
michael@0 | 182 | |
michael@0 | 183 | /* Destroy Cache Tables */ |
michael@0 | 184 | PKIX_DECREF(cachedCertSigTable); |
michael@0 | 185 | PKIX_DECREF(cachedCrlSigTable); |
michael@0 | 186 | PKIX_DECREF(cachedCertChainTable); |
michael@0 | 187 | PKIX_DECREF(cachedCertTable); |
michael@0 | 188 | PKIX_DECREF(cachedCrlEntryTable); |
michael@0 | 189 | PKIX_DECREF(aiaConnectionCache); |
michael@0 | 190 | PKIX_DECREF(httpSocketCache); |
michael@0 | 191 | |
michael@0 | 192 | /* Clean up any temporary errors that happened during shutdown */ |
michael@0 | 193 | if (pkixErrorList) { |
michael@0 | 194 | PKIX_PL_Object_DecRef((PKIX_PL_Object*)pkixErrorList, plContext); |
michael@0 | 195 | pkixErrorList = NULL; |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | PKIX_CHECK(PKIX_PL_Shutdown(plContext), |
michael@0 | 199 | PKIX_SHUTDOWNFAILED); |
michael@0 | 200 | |
michael@0 | 201 | #ifdef PKIX_OBJECT_LEAK_TEST |
michael@0 | 202 | PORT_Free(fnStackInvCountArr); |
michael@0 | 203 | PORT_Free(fnStackNameArr); |
michael@0 | 204 | PL_HashTableDestroy(fnInvTable); |
michael@0 | 205 | #endif |
michael@0 | 206 | |
michael@0 | 207 | cleanup: |
michael@0 | 208 | |
michael@0 | 209 | PKIX_RETURN(LIFECYCLE); |
michael@0 | 210 | } |