1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the PKIX-C library.
16 * The Initial Developer of the Original Code is
17 * Sun Microsystems, Inc.
18 * Portions created by the Initial Developer are
19 * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
22 * Sun Microsystems, Inc.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
40 * Top level initialize and shutdown functions
44 #include "pkix_lifecycle.h"
46 static PKIX_Boolean pkixIsInitialized;
48 /* Lock used by Logger - is reentrant by the same thread */
49 extern PKIX_PL_MonitorLock *pkixLoggerLock;
52 * Following pkix_* variables are for debugging purpose. They should be taken
53 * out eventually. The purpose is to verify cache tables usage (via debugger).
55 int pkix_ccAddCount = 0;
56 int pkix_ccLookupCount = 0;
57 int pkix_ccRemoveCount = 0;
58 int pkix_cAddCount = 0;
59 int pkix_cLookupCount = 0;
60 int pkix_cRemoveCount = 0;
61 int pkix_ceAddCount = 0;
62 int pkix_ceLookupCount = 0;
64 PKIX_PL_HashTable *cachedCrlSigTable = NULL;
65 PKIX_PL_HashTable *cachedCertSigTable = NULL;
66 PKIX_PL_HashTable *cachedCertChainTable = NULL;
67 PKIX_PL_HashTable *cachedCertTable = NULL;
68 PKIX_PL_HashTable *cachedCrlEntryTable = NULL;
69 PKIX_PL_HashTable *aiaConnectionCache = NULL;
70 PKIX_PL_HashTable *httpSocketCache = NULL;
72 extern PKIX_List *pkixLoggers;
73 extern PKIX_List *pkixLoggersErrors;
74 extern PKIX_List *pkixLoggersDebugTrace;
76 /* --Public-Functions--------------------------------------------- */
79 * FUNCTION: PKIX_Initialize (see comments in pkix.h)
83 PKIX_Boolean platformInitNeeded,
84 PKIX_UInt32 desiredMajorVersion,
85 PKIX_UInt32 minDesiredMinorVersion,
86 PKIX_UInt32 maxDesiredMinorVersion,
87 PKIX_UInt32 *pActualMinorVersion,
90 void *plContext = NULL;
92 PKIX_ENTER(LIFECYCLE, "PKIX_Initialize");
93 PKIX_NULLCHECK_ONE(pPlContext);
96 * If we are called a second time other than in the situation handled
97 * above, we return a positive status.
99 if (pkixIsInitialized){
100 /* Already initialized */
101 PKIX_RETURN(LIFECYCLE);
104 PKIX_CHECK(PKIX_PL_Initialize
105 (platformInitNeeded, PKIX_FALSE, &plContext),
106 PKIX_INITIALIZEFAILED);
108 *pPlContext = plContext;
110 if (desiredMajorVersion != PKIX_MAJOR_VERSION){
111 PKIX_ERROR(PKIX_MAJORVERSIONSDONTMATCH);
114 if ((minDesiredMinorVersion > PKIX_MINOR_VERSION) ||
115 (maxDesiredMinorVersion < PKIX_MINOR_VERSION)){
116 PKIX_ERROR(PKIX_MINORVERSIONNOTBETWEENDESIREDMINANDMAX);
119 *pActualMinorVersion = PKIX_MINOR_VERSION;
121 /* Create Cache Tables
122 * Do not initialize hash tables for object leak test */
123 #if !defined(PKIX_OBJECT_LEAK_TEST)
124 PKIX_CHECK(PKIX_PL_HashTable_Create
125 (32, 0, &cachedCertSigTable, plContext),
126 PKIX_HASHTABLECREATEFAILED);
128 PKIX_CHECK(PKIX_PL_HashTable_Create
129 (32, 0, &cachedCrlSigTable, plContext),
130 PKIX_HASHTABLECREATEFAILED);
132 PKIX_CHECK(PKIX_PL_HashTable_Create
133 (32, 10, &cachedCertChainTable, plContext),
134 PKIX_HASHTABLECREATEFAILED);
136 PKIX_CHECK(PKIX_PL_HashTable_Create
137 (32, 10, &cachedCertTable, plContext),
138 PKIX_HASHTABLECREATEFAILED);
140 PKIX_CHECK(PKIX_PL_HashTable_Create
141 (32, 10, &cachedCrlEntryTable, plContext),
142 PKIX_HASHTABLECREATEFAILED);
144 PKIX_CHECK(PKIX_PL_HashTable_Create
145 (5, 5, &aiaConnectionCache, plContext),
146 PKIX_HASHTABLECREATEFAILED);
148 #ifdef PKIX_SOCKETCACHE
149 PKIX_CHECK(PKIX_PL_HashTable_Create
150 (5, 5, &httpSocketCache, plContext),
151 PKIX_HASHTABLECREATEFAILED);
153 if (pkixLoggerLock == NULL) {
154 PKIX_CHECK(PKIX_PL_MonitorLock_Create
155 (&pkixLoggerLock, plContext),
156 PKIX_MONITORLOCKCREATEFAILED);
159 fnInvTable = PL_NewHashTable(0, pkix_ErrorGen_Hash,
161 PL_CompareValues, NULL, NULL);
163 PKIX_ERROR(PKIX_HASHTABLECREATEFAILED);
166 fnStackNameArr = PORT_ZNewArray(char*, MAX_STACK_DEPTH);
167 if (!fnStackNameArr) {
168 PKIX_ERROR(PKIX_HASHTABLECREATEFAILED);
171 fnStackInvCountArr = PORT_ZNewArray(PKIX_UInt32, MAX_STACK_DEPTH);
172 if (!fnStackInvCountArr) {
173 PKIX_ERROR(PKIX_HASHTABLECREATEFAILED);
175 #endif /* PKIX_OBJECT_LEAK_TEST */
177 pkixIsInitialized = PKIX_TRUE;
181 PKIX_RETURN(LIFECYCLE);
185 * FUNCTION: PKIX_Shutdown (see comments in pkix.h)
188 PKIX_Shutdown(void *plContext)
190 PKIX_List *savedPkixLoggers = NULL;
191 PKIX_List *savedPkixLoggersErrors = NULL;
192 PKIX_List *savedPkixLoggersDebugTrace = NULL;
194 PKIX_ENTER(LIFECYCLE, "PKIX_Shutdown");
196 if (!pkixIsInitialized){
197 /* The library was not initialized */
198 PKIX_RETURN(LIFECYCLE);
201 pkixIsInitialized = PKIX_FALSE;
204 savedPkixLoggers = pkixLoggers;
205 savedPkixLoggersErrors = pkixLoggersErrors;
206 savedPkixLoggersDebugTrace = pkixLoggersDebugTrace;
208 pkixLoggersErrors = NULL;
209 pkixLoggersDebugTrace = NULL;
210 PKIX_DECREF(savedPkixLoggers);
211 PKIX_DECREF(savedPkixLoggersErrors);
212 PKIX_DECREF(savedPkixLoggersDebugTrace);
214 PKIX_DECREF(pkixLoggerLock);
216 /* Destroy Cache Tables */
217 PKIX_DECREF(cachedCertSigTable);
218 PKIX_DECREF(cachedCrlSigTable);
219 PKIX_DECREF(cachedCertChainTable);
220 PKIX_DECREF(cachedCertTable);
221 PKIX_DECREF(cachedCrlEntryTable);
222 PKIX_DECREF(aiaConnectionCache);
223 PKIX_DECREF(httpSocketCache);
225 /* Clean up any temporary errors that happened during shutdown */
227 PKIX_PL_Object_DecRef((PKIX_PL_Object*)pkixErrorList, plContext);
228 pkixErrorList = NULL;
231 PKIX_CHECK(PKIX_PL_Shutdown(plContext),
232 PKIX_SHUTDOWNFAILED);
234 #ifdef PKIX_OBJECT_LEAK_TEST
235 PORT_Free(fnStackInvCountArr);
236 PORT_Free(fnStackNameArr);
237 PL_HashTableDestroy(fnInvTable);
242 PKIX_RETURN(LIFECYCLE);