1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/libpkix/pkix/top/pkix_lifecycle.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,210 @@ 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 + * pkix_lifecycle.c 1.9 + * 1.10 + * Top level initialize and shutdown functions 1.11 + * 1.12 + */ 1.13 + 1.14 +#include "pkix_lifecycle.h" 1.15 + 1.16 +static PKIX_Boolean pkixIsInitialized; 1.17 + 1.18 +/* Lock used by Logger - is reentrant by the same thread */ 1.19 +extern PKIX_PL_MonitorLock *pkixLoggerLock; 1.20 + 1.21 +/* 1.22 + * Following pkix_* variables are for debugging purpose. They should be taken 1.23 + * out eventually. The purpose is to verify cache tables usage (via debugger). 1.24 + */ 1.25 +int pkix_ccAddCount = 0; 1.26 +int pkix_ccLookupCount = 0; 1.27 +int pkix_ccRemoveCount = 0; 1.28 +int pkix_cAddCount = 0; 1.29 +int pkix_cLookupCount = 0; 1.30 +int pkix_cRemoveCount = 0; 1.31 +int pkix_ceAddCount = 0; 1.32 +int pkix_ceLookupCount = 0; 1.33 + 1.34 +PKIX_PL_HashTable *cachedCrlSigTable = NULL; 1.35 +PKIX_PL_HashTable *cachedCertSigTable = NULL; 1.36 +PKIX_PL_HashTable *cachedCertChainTable = NULL; 1.37 +PKIX_PL_HashTable *cachedCertTable = NULL; 1.38 +PKIX_PL_HashTable *cachedCrlEntryTable = NULL; 1.39 +PKIX_PL_HashTable *aiaConnectionCache = NULL; 1.40 +PKIX_PL_HashTable *httpSocketCache = NULL; 1.41 + 1.42 +extern PKIX_List *pkixLoggers; 1.43 +extern PKIX_List *pkixLoggersErrors; 1.44 +extern PKIX_List *pkixLoggersDebugTrace; 1.45 + 1.46 +/* --Public-Functions--------------------------------------------- */ 1.47 + 1.48 +/* 1.49 + * FUNCTION: PKIX_Initialize (see comments in pkix.h) 1.50 + */ 1.51 +PKIX_Error * 1.52 +PKIX_Initialize( 1.53 + PKIX_Boolean platformInitNeeded, 1.54 + PKIX_UInt32 desiredMajorVersion, 1.55 + PKIX_UInt32 minDesiredMinorVersion, 1.56 + PKIX_UInt32 maxDesiredMinorVersion, 1.57 + PKIX_UInt32 *pActualMinorVersion, 1.58 + void **pPlContext) 1.59 +{ 1.60 + void *plContext = NULL; 1.61 + 1.62 + PKIX_ENTER(LIFECYCLE, "PKIX_Initialize"); 1.63 + PKIX_NULLCHECK_ONE(pPlContext); 1.64 + 1.65 + /* 1.66 + * If we are called a second time other than in the situation handled 1.67 + * above, we return a positive status. 1.68 + */ 1.69 + if (pkixIsInitialized){ 1.70 + /* Already initialized */ 1.71 + PKIX_RETURN(LIFECYCLE); 1.72 + } 1.73 + 1.74 + PKIX_CHECK(PKIX_PL_Initialize 1.75 + (platformInitNeeded, PKIX_FALSE, &plContext), 1.76 + PKIX_INITIALIZEFAILED); 1.77 + 1.78 + *pPlContext = plContext; 1.79 + 1.80 + if (desiredMajorVersion != PKIX_MAJOR_VERSION){ 1.81 + PKIX_ERROR(PKIX_MAJORVERSIONSDONTMATCH); 1.82 + } 1.83 + 1.84 + if ((minDesiredMinorVersion > PKIX_MINOR_VERSION) || 1.85 + (maxDesiredMinorVersion < PKIX_MINOR_VERSION)){ 1.86 + PKIX_ERROR(PKIX_MINORVERSIONNOTBETWEENDESIREDMINANDMAX); 1.87 + } 1.88 + 1.89 + *pActualMinorVersion = PKIX_MINOR_VERSION; 1.90 + 1.91 + /* Create Cache Tables 1.92 + * Do not initialize hash tables for object leak test */ 1.93 +#if !defined(PKIX_OBJECT_LEAK_TEST) 1.94 + PKIX_CHECK(PKIX_PL_HashTable_Create 1.95 + (32, 0, &cachedCertSigTable, plContext), 1.96 + PKIX_HASHTABLECREATEFAILED); 1.97 + 1.98 + PKIX_CHECK(PKIX_PL_HashTable_Create 1.99 + (32, 0, &cachedCrlSigTable, plContext), 1.100 + PKIX_HASHTABLECREATEFAILED); 1.101 + 1.102 + PKIX_CHECK(PKIX_PL_HashTable_Create 1.103 + (32, 10, &cachedCertChainTable, plContext), 1.104 + PKIX_HASHTABLECREATEFAILED); 1.105 + 1.106 + PKIX_CHECK(PKIX_PL_HashTable_Create 1.107 + (32, 10, &cachedCertTable, plContext), 1.108 + PKIX_HASHTABLECREATEFAILED); 1.109 + 1.110 + PKIX_CHECK(PKIX_PL_HashTable_Create 1.111 + (32, 10, &cachedCrlEntryTable, plContext), 1.112 + PKIX_HASHTABLECREATEFAILED); 1.113 + 1.114 + PKIX_CHECK(PKIX_PL_HashTable_Create 1.115 + (5, 5, &aiaConnectionCache, plContext), 1.116 + PKIX_HASHTABLECREATEFAILED); 1.117 + 1.118 +#ifdef PKIX_SOCKETCACHE 1.119 + PKIX_CHECK(PKIX_PL_HashTable_Create 1.120 + (5, 5, &httpSocketCache, plContext), 1.121 + PKIX_HASHTABLECREATEFAILED); 1.122 +#endif 1.123 + if (pkixLoggerLock == NULL) { 1.124 + PKIX_CHECK(PKIX_PL_MonitorLock_Create 1.125 + (&pkixLoggerLock, plContext), 1.126 + PKIX_MONITORLOCKCREATEFAILED); 1.127 + } 1.128 +#else 1.129 + fnInvTable = PL_NewHashTable(0, pkix_ErrorGen_Hash, 1.130 + PL_CompareValues, 1.131 + PL_CompareValues, NULL, NULL); 1.132 + if (!fnInvTable) { 1.133 + PKIX_ERROR(PKIX_HASHTABLECREATEFAILED); 1.134 + } 1.135 + 1.136 + fnStackNameArr = PORT_ZNewArray(char*, MAX_STACK_DEPTH); 1.137 + if (!fnStackNameArr) { 1.138 + PKIX_ERROR(PKIX_HASHTABLECREATEFAILED); 1.139 + } 1.140 + 1.141 + fnStackInvCountArr = PORT_ZNewArray(PKIX_UInt32, MAX_STACK_DEPTH); 1.142 + if (!fnStackInvCountArr) { 1.143 + PKIX_ERROR(PKIX_HASHTABLECREATEFAILED); 1.144 + } 1.145 +#endif /* PKIX_OBJECT_LEAK_TEST */ 1.146 + 1.147 + pkixIsInitialized = PKIX_TRUE; 1.148 + 1.149 +cleanup: 1.150 + 1.151 + PKIX_RETURN(LIFECYCLE); 1.152 +} 1.153 + 1.154 +/* 1.155 + * FUNCTION: PKIX_Shutdown (see comments in pkix.h) 1.156 + */ 1.157 +PKIX_Error * 1.158 +PKIX_Shutdown(void *plContext) 1.159 +{ 1.160 + PKIX_List *savedPkixLoggers = NULL; 1.161 + PKIX_List *savedPkixLoggersErrors = NULL; 1.162 + PKIX_List *savedPkixLoggersDebugTrace = NULL; 1.163 + 1.164 + PKIX_ENTER(LIFECYCLE, "PKIX_Shutdown"); 1.165 + 1.166 + if (!pkixIsInitialized){ 1.167 + /* The library was not initialized */ 1.168 + PKIX_RETURN(LIFECYCLE); 1.169 + } 1.170 + 1.171 + pkixIsInitialized = PKIX_FALSE; 1.172 + 1.173 + if (pkixLoggers) { 1.174 + savedPkixLoggers = pkixLoggers; 1.175 + savedPkixLoggersErrors = pkixLoggersErrors; 1.176 + savedPkixLoggersDebugTrace = pkixLoggersDebugTrace; 1.177 + pkixLoggers = NULL; 1.178 + pkixLoggersErrors = NULL; 1.179 + pkixLoggersDebugTrace = NULL; 1.180 + PKIX_DECREF(savedPkixLoggers); 1.181 + PKIX_DECREF(savedPkixLoggersErrors); 1.182 + PKIX_DECREF(savedPkixLoggersDebugTrace); 1.183 + } 1.184 + PKIX_DECREF(pkixLoggerLock); 1.185 + 1.186 + /* Destroy Cache Tables */ 1.187 + PKIX_DECREF(cachedCertSigTable); 1.188 + PKIX_DECREF(cachedCrlSigTable); 1.189 + PKIX_DECREF(cachedCertChainTable); 1.190 + PKIX_DECREF(cachedCertTable); 1.191 + PKIX_DECREF(cachedCrlEntryTable); 1.192 + PKIX_DECREF(aiaConnectionCache); 1.193 + PKIX_DECREF(httpSocketCache); 1.194 + 1.195 + /* Clean up any temporary errors that happened during shutdown */ 1.196 + if (pkixErrorList) { 1.197 + PKIX_PL_Object_DecRef((PKIX_PL_Object*)pkixErrorList, plContext); 1.198 + pkixErrorList = NULL; 1.199 + } 1.200 + 1.201 + PKIX_CHECK(PKIX_PL_Shutdown(plContext), 1.202 + PKIX_SHUTDOWNFAILED); 1.203 + 1.204 +#ifdef PKIX_OBJECT_LEAK_TEST 1.205 + PORT_Free(fnStackInvCountArr); 1.206 + PORT_Free(fnStackNameArr); 1.207 + PL_HashTableDestroy(fnInvTable); 1.208 +#endif 1.209 + 1.210 +cleanup: 1.211 + 1.212 + PKIX_RETURN(LIFECYCLE); 1.213 +}