Imported Upstream version 3.13.6
[platform/upstream/nss.git] / mozilla / security / nss / lib / libpkix / pkix / top / pkix_lifecycle.c
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
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/
8  *
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
12  * License.
13  *
14  * The Original Code is the PKIX-C library.
15  *
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.
20  *
21  * Contributor(s):
22  *   Sun Microsystems, Inc.
23  *
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.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 /*
38  * pkix_lifecycle.c
39  *
40  * Top level initialize and shutdown functions
41  *
42  */
43
44 #include "pkix_lifecycle.h"
45
46 static PKIX_Boolean pkixIsInitialized;
47
48 /* Lock used by Logger - is reentrant by the same thread */
49 extern PKIX_PL_MonitorLock *pkixLoggerLock;
50
51 /* 
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).
54  */
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;
63
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;
71
72 extern PKIX_List *pkixLoggers;
73 extern PKIX_List *pkixLoggersErrors;
74 extern PKIX_List *pkixLoggersDebugTrace;
75
76 /* --Public-Functions--------------------------------------------- */
77
78 /*
79  * FUNCTION: PKIX_Initialize (see comments in pkix.h)
80  */
81 PKIX_Error *
82 PKIX_Initialize(
83         PKIX_Boolean platformInitNeeded,
84         PKIX_UInt32 desiredMajorVersion,
85         PKIX_UInt32 minDesiredMinorVersion,
86         PKIX_UInt32 maxDesiredMinorVersion,
87         PKIX_UInt32 *pActualMinorVersion,
88         void **pPlContext)
89 {
90         void *plContext = NULL;
91
92         PKIX_ENTER(LIFECYCLE, "PKIX_Initialize");
93         PKIX_NULLCHECK_ONE(pPlContext);
94
95         /*
96          * If we are called a second time other than in the situation handled
97          * above, we return a positive status.
98          */
99         if (pkixIsInitialized){
100                 /* Already initialized */
101                 PKIX_RETURN(LIFECYCLE);
102         }
103
104         PKIX_CHECK(PKIX_PL_Initialize
105                 (platformInitNeeded, PKIX_FALSE, &plContext),
106                 PKIX_INITIALIZEFAILED);
107
108         *pPlContext = plContext;
109
110         if (desiredMajorVersion != PKIX_MAJOR_VERSION){
111                 PKIX_ERROR(PKIX_MAJORVERSIONSDONTMATCH);
112         }
113
114         if ((minDesiredMinorVersion > PKIX_MINOR_VERSION) ||
115             (maxDesiredMinorVersion < PKIX_MINOR_VERSION)){
116                 PKIX_ERROR(PKIX_MINORVERSIONNOTBETWEENDESIREDMINANDMAX);
117         }
118
119         *pActualMinorVersion = PKIX_MINOR_VERSION;
120
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);
127         
128         PKIX_CHECK(PKIX_PL_HashTable_Create
129                    (32, 0, &cachedCrlSigTable, plContext),
130                    PKIX_HASHTABLECREATEFAILED);
131         
132         PKIX_CHECK(PKIX_PL_HashTable_Create
133                        (32, 10, &cachedCertChainTable, plContext),
134                    PKIX_HASHTABLECREATEFAILED);
135         
136         PKIX_CHECK(PKIX_PL_HashTable_Create
137                        (32, 10, &cachedCertTable, plContext),
138                    PKIX_HASHTABLECREATEFAILED);
139         
140         PKIX_CHECK(PKIX_PL_HashTable_Create
141                    (32, 10, &cachedCrlEntryTable, plContext),
142                    PKIX_HASHTABLECREATEFAILED);
143         
144         PKIX_CHECK(PKIX_PL_HashTable_Create
145                    (5, 5, &aiaConnectionCache, plContext),
146                    PKIX_HASHTABLECREATEFAILED);
147         
148 #ifdef PKIX_SOCKETCACHE
149         PKIX_CHECK(PKIX_PL_HashTable_Create
150                    (5, 5, &httpSocketCache, plContext),
151                    PKIX_HASHTABLECREATEFAILED);
152 #endif
153         if (pkixLoggerLock == NULL) {
154             PKIX_CHECK(PKIX_PL_MonitorLock_Create
155                        (&pkixLoggerLock, plContext),
156                        PKIX_MONITORLOCKCREATEFAILED);
157         }
158 #else
159         fnInvTable = PL_NewHashTable(0, pkix_ErrorGen_Hash,
160                                      PL_CompareValues,
161                                      PL_CompareValues, NULL, NULL);
162         if (!fnInvTable) {
163             PKIX_ERROR(PKIX_HASHTABLECREATEFAILED);
164         }
165         
166         fnStackNameArr = PORT_ZNewArray(char*, MAX_STACK_DEPTH);
167         if (!fnStackNameArr) {
168             PKIX_ERROR(PKIX_HASHTABLECREATEFAILED);
169         }
170
171         fnStackInvCountArr = PORT_ZNewArray(PKIX_UInt32, MAX_STACK_DEPTH);
172         if (!fnStackInvCountArr) {
173             PKIX_ERROR(PKIX_HASHTABLECREATEFAILED);
174         }
175 #endif /* PKIX_OBJECT_LEAK_TEST */
176
177         pkixIsInitialized = PKIX_TRUE;
178
179 cleanup:
180
181         PKIX_RETURN(LIFECYCLE);
182 }
183
184 /*
185  * FUNCTION: PKIX_Shutdown (see comments in pkix.h)
186  */
187 PKIX_Error *
188 PKIX_Shutdown(void *plContext)
189 {
190         PKIX_List *savedPkixLoggers = NULL;
191         PKIX_List *savedPkixLoggersErrors = NULL;
192         PKIX_List *savedPkixLoggersDebugTrace = NULL;
193
194         PKIX_ENTER(LIFECYCLE, "PKIX_Shutdown");
195
196         if (!pkixIsInitialized){
197                 /* The library was not initialized */
198                 PKIX_RETURN(LIFECYCLE);
199         }
200
201         pkixIsInitialized = PKIX_FALSE;
202
203         if (pkixLoggers) {
204                 savedPkixLoggers = pkixLoggers;
205                 savedPkixLoggersErrors = pkixLoggersErrors;
206                 savedPkixLoggersDebugTrace = pkixLoggersDebugTrace;
207                 pkixLoggers = NULL;
208                 pkixLoggersErrors = NULL;
209                 pkixLoggersDebugTrace = NULL;
210                 PKIX_DECREF(savedPkixLoggers);
211                 PKIX_DECREF(savedPkixLoggersErrors);
212                 PKIX_DECREF(savedPkixLoggersDebugTrace);
213         }
214         PKIX_DECREF(pkixLoggerLock);
215
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);
224
225         /* Clean up any temporary errors that happened during shutdown */
226         if (pkixErrorList) {
227             PKIX_PL_Object_DecRef((PKIX_PL_Object*)pkixErrorList, plContext);
228             pkixErrorList = NULL;
229         }
230
231         PKIX_CHECK(PKIX_PL_Shutdown(plContext),
232                 PKIX_SHUTDOWNFAILED);
233
234 #ifdef PKIX_OBJECT_LEAK_TEST
235         PORT_Free(fnStackInvCountArr);
236         PORT_Free(fnStackNameArr);
237         PL_HashTableDestroy(fnInvTable);
238 #endif
239
240 cleanup:
241
242         PKIX_RETURN(LIFECYCLE);
243 }