PageRenderTime 34ms CodeModel.GetById 16ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/security/nss/lib/libpkix/pkix/store/pkix_store.c

http://github.com/zpao/v8monkey
C | 448 lines | 268 code | 75 blank | 105 comment | 8 complexity | 970d9add7c76f532db8b9099e7477d8b MD5 | raw file
  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_store.c
 39 *
 40 * CertStore Function Definitions
 41 *
 42 */
 43
 44#include "pkix_store.h"
 45
 46/* --CertStore-Private-Functions----------------------------------------- */
 47
 48/*
 49 * FUNCTION: pkix_CertStore_Destroy
 50 * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
 51 */
 52static PKIX_Error *
 53pkix_CertStore_Destroy(
 54        PKIX_PL_Object *object,
 55        void *plContext)
 56{
 57        PKIX_CertStore *certStore = NULL;
 58
 59        PKIX_ENTER(CERTSTORE, "pkix_CertStore_Destroy");
 60        PKIX_NULLCHECK_ONE(object);
 61
 62        /* Check that this object is a CertStore object */
 63        PKIX_CHECK(pkix_CheckType(object, PKIX_CERTSTORE_TYPE, plContext),
 64                PKIX_OBJECTNOTCERTSTORE);
 65
 66        certStore = (PKIX_CertStore *)object;
 67
 68        certStore->certCallback = NULL;
 69        certStore->crlCallback = NULL;
 70        certStore->certContinue = NULL;
 71        certStore->crlContinue = NULL;
 72        certStore->trustCallback = NULL;
 73
 74        PKIX_DECREF(certStore->certStoreContext);
 75
 76cleanup:
 77
 78        PKIX_RETURN(CERTSTORE);
 79}
 80
 81/*
 82 * FUNCTION: pkix_CertStore_Hashcode
 83 * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h)
 84 */
 85static PKIX_Error *
 86pkix_CertStore_Hashcode(
 87        PKIX_PL_Object *object,
 88        PKIX_UInt32 *pHashcode,
 89        void *plContext)
 90{
 91        PKIX_CertStore *certStore = NULL;
 92        PKIX_UInt32 tempHash = 0;
 93
 94        PKIX_ENTER(CERTSTORE, "pkix_CertStore_Hashcode");
 95        PKIX_NULLCHECK_TWO(object, pHashcode);
 96
 97        PKIX_CHECK(pkix_CheckType(object, PKIX_CERTSTORE_TYPE, plContext),
 98                    PKIX_OBJECTNOTCERTSTORE);
 99
100        certStore = (PKIX_CertStore *)object;
101
102        if (certStore->certStoreContext) {
103                PKIX_CHECK(PKIX_PL_Object_Hashcode
104                    ((PKIX_PL_Object *) certStore->certStoreContext,
105                    &tempHash,
106                    plContext),
107                   PKIX_CERTSTOREHASHCODEFAILED);
108        }
109
110        *pHashcode = (PKIX_UInt32) certStore->certCallback +
111                     (PKIX_UInt32) certStore->crlCallback +
112                     (PKIX_UInt32) certStore->certContinue +
113                     (PKIX_UInt32) certStore->crlContinue +
114                     (PKIX_UInt32) certStore->trustCallback +
115                     (tempHash << 7);
116
117cleanup:
118
119        PKIX_RETURN(CERTSTORE);
120}
121
122/*
123 * FUNCTION: pkix_CertStore_Equals
124 * (see comments for PKIX_PL_EqualsCallback in pkix_pl_system.h)
125 */
126static PKIX_Error *
127pkix_CertStore_Equals(
128        PKIX_PL_Object *firstObject,
129        PKIX_PL_Object *secondObject,
130        PKIX_Int32 *pResult,
131        void *plContext)
132{
133        PKIX_CertStore *firstCS = NULL;
134        PKIX_CertStore *secondCS = NULL;
135        PKIX_Boolean cmpResult = PKIX_FALSE;
136
137        PKIX_ENTER(CERTSTORE, "pkix_CertStore_Equals");
138        PKIX_NULLCHECK_THREE(firstObject, secondObject, pResult);
139
140        PKIX_CHECK(pkix_CheckTypes
141                    (firstObject, secondObject, PKIX_CERTSTORE_TYPE, plContext),
142                    PKIX_ARGUMENTSNOTDATES);
143
144        firstCS = (PKIX_CertStore *)firstObject;
145        secondCS = (PKIX_CertStore *)secondObject;
146
147        cmpResult = (firstCS->certCallback == secondCS->certCallback) &&
148            (firstCS->crlCallback == secondCS->crlCallback) &&
149            (firstCS->certContinue == secondCS->certContinue) &&
150            (firstCS->crlContinue == secondCS->crlContinue) &&
151            (firstCS->trustCallback == secondCS->trustCallback);
152
153        if (cmpResult &&
154            (firstCS->certStoreContext != secondCS->certStoreContext)) {
155
156                PKIX_CHECK(PKIX_PL_Object_Equals
157                    ((PKIX_PL_Object *) firstCS->certStoreContext,
158                    (PKIX_PL_Object *) secondCS->certStoreContext,
159                    &cmpResult,
160                    plContext),
161                    PKIX_CERTSTOREEQUALSFAILED);
162        }
163
164        *pResult = cmpResult;
165
166cleanup:
167
168        PKIX_RETURN(CERTSTORE);
169}
170
171/*
172 * FUNCTION: pkix_CertStore_RegisterSelf
173 * DESCRIPTION:
174 *  Registers PKIX_CERTSTORE_TYPE and its related functions with
175 *  systemClasses[]
176 * THREAD SAFETY:
177 *  Not Thread Safe - for performance and complexity reasons
178 *
179 *  Since this function is only called by PKIX_PL_Initialize, which should
180 *  only be called once, it is acceptable that this function is not
181 *  thread-safe.
182 */
183PKIX_Error *
184pkix_CertStore_RegisterSelf(void *plContext)
185{
186        extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
187        pkix_ClassTable_Entry entry;
188
189        PKIX_ENTER(CERTSTORE, "pkix_CertStore_RegisterSelf");
190
191        entry.description = "CertStore";
192        entry.objCounter = 0;
193        entry.typeObjectSize = sizeof(PKIX_CertStore);
194        entry.destructor = pkix_CertStore_Destroy;
195        entry.equalsFunction = pkix_CertStore_Equals;
196        entry.hashcodeFunction = pkix_CertStore_Hashcode;
197        entry.toStringFunction = NULL;
198        entry.comparator = NULL;
199        entry.duplicateFunction = pkix_duplicateImmutable;
200
201        systemClasses[PKIX_CERTSTORE_TYPE] = entry;
202
203        PKIX_RETURN(CERTSTORE);
204}
205
206/* --CertStore-Public-Functions------------------------------------------ */
207
208/*
209 * FUNCTION: PKIX_CertStore_Create (see comments in pkix_certstore.h)
210 */
211PKIX_Error *
212PKIX_CertStore_Create(
213        PKIX_CertStore_CertCallback certCallback,
214        PKIX_CertStore_CRLCallback crlCallback,
215        PKIX_CertStore_CertContinueFunction certContinue,
216        PKIX_CertStore_CrlContinueFunction crlContinue,
217        PKIX_CertStore_CheckTrustCallback trustCallback,
218        PKIX_CertStore_ImportCrlCallback importCrlCallback,
219        PKIX_CertStore_CheckRevokationByCrlCallback checkRevByCrlCallback,
220        PKIX_PL_Object *certStoreContext,
221        PKIX_Boolean cacheFlag,
222        PKIX_Boolean localFlag,
223        PKIX_CertStore **pStore,
224        void *plContext)
225{
226        PKIX_CertStore *certStore = NULL;
227
228        PKIX_ENTER(CERTSTORE, "PKIX_CertStore_Create");
229        PKIX_NULLCHECK_THREE(certCallback, crlCallback, pStore);
230
231        PKIX_CHECK(PKIX_PL_Object_Alloc
232                    (PKIX_CERTSTORE_TYPE,
233                    sizeof (PKIX_CertStore),
234                    (PKIX_PL_Object **)&certStore,
235                    plContext),
236                    PKIX_COULDNOTCREATECERTSTOREOBJECT);
237
238        certStore->certCallback = certCallback;
239        certStore->crlCallback = crlCallback;
240        certStore->certContinue = certContinue;
241        certStore->crlContinue = crlContinue;
242        certStore->trustCallback = trustCallback;
243        certStore->importCrlCallback = importCrlCallback;
244        certStore->checkRevByCrlCallback = checkRevByCrlCallback;
245        certStore->cacheFlag = cacheFlag;
246        certStore->localFlag = localFlag;
247
248        PKIX_INCREF(certStoreContext);
249        certStore->certStoreContext = certStoreContext;
250
251        *pStore = certStore;
252        certStore = NULL;
253
254cleanup:
255
256        PKIX_DECREF(certStore);
257
258        PKIX_RETURN(CERTSTORE);
259}
260
261/*
262 * FUNCTION: PKIX_CertStore_GetCertCallback (see comments in pkix_certstore.h)
263 */
264PKIX_Error *
265PKIX_CertStore_GetCertCallback(
266        PKIX_CertStore *store,
267        PKIX_CertStore_CertCallback *pCallback,
268        void *plContext)
269{
270        PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetCertCallback");
271        PKIX_NULLCHECK_TWO(store, pCallback);
272
273        *pCallback = store->certCallback;
274
275        PKIX_RETURN(CERTSTORE);
276}
277
278/*
279 * FUNCTION: PKIX_CertStore_GetCRLCallback (see comments in pkix_certstore.h)
280 */
281PKIX_Error *
282PKIX_CertStore_GetCRLCallback(
283        PKIX_CertStore *store,
284        PKIX_CertStore_CRLCallback *pCallback,
285        void *plContext)
286{
287        PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetCRLCallback");
288        PKIX_NULLCHECK_TWO(store, pCallback);
289
290        *pCallback = store->crlCallback;
291
292        PKIX_RETURN(CERTSTORE);
293}
294
295/*
296 * FUNCTION: PKIX_CertStore_CertContinue (see comments in pkix_certstore.h)
297 */
298PKIX_Error *
299PKIX_CertStore_CertContinue(
300        PKIX_CertStore *store,
301        PKIX_CertSelector *selector,
302        PKIX_VerifyNode *verifyNode,
303        void **pNBIOContext,
304        PKIX_List **pCertList,
305        void *plContext)
306{
307        PKIX_ENTER(CERTSTORE, "PKIX_CertStore_CertContinue");
308        PKIX_NULLCHECK_FOUR(store, selector, pNBIOContext, pCertList);
309
310        PKIX_CHECK(store->certContinue
311                   (store, selector, verifyNode,
312                    pNBIOContext, pCertList, plContext),
313                PKIX_CERTSTORECERTCONTINUEFUNCTIONFAILED);
314
315cleanup:
316
317        PKIX_RETURN(CERTSTORE);
318}
319
320/*
321 * FUNCTION: PKIX_CertStore_CrlContinue (see comments in pkix_certstore.h)
322 */
323PKIX_Error *
324PKIX_CertStore_CrlContinue(
325        PKIX_CertStore *store,
326        PKIX_CRLSelector *selector,
327        void **pNBIOContext,
328        PKIX_List **pCrlList,
329        void *plContext)
330{
331        PKIX_ENTER(CERTSTORE, "PKIX_CertStore_CrlContinue");
332        PKIX_NULLCHECK_FOUR(store, selector, pNBIOContext, pCrlList);
333
334        PKIX_CHECK(store->crlContinue
335                (store, selector, pNBIOContext, pCrlList, plContext),
336                PKIX_CERTSTORECRLCONTINUEFAILED);
337
338cleanup:
339
340        PKIX_RETURN(CERTSTORE);
341}
342
343/*
344 * FUNCTION: PKIX_CertStore_GetTrustCallback (see comments in pkix_certstore.h)
345 */
346PKIX_Error *
347PKIX_CertStore_GetTrustCallback(
348        PKIX_CertStore *store,
349        PKIX_CertStore_CheckTrustCallback *pCallback,
350        void *plContext)
351{
352        PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetTrustCallback");
353        PKIX_NULLCHECK_TWO(store, pCallback);
354
355        *pCallback = store->trustCallback;
356
357        PKIX_RETURN(CERTSTORE);
358}
359
360/*
361 * FUNCTION: PKIX_CertStore_GetImportCrlCallback (see comments in pkix_certstore.h)
362 */
363PKIX_Error *
364PKIX_CertStore_GetImportCrlCallback(
365        PKIX_CertStore *store,
366        PKIX_CertStore_ImportCrlCallback *pCallback,
367        void *plContext)
368{
369        PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetTrustCallback");
370        PKIX_NULLCHECK_TWO(store, pCallback);
371
372        *pCallback = store->importCrlCallback;
373
374        PKIX_RETURN(CERTSTORE);
375}
376
377/*
378 * FUNCTION: PKIX_CertStore_GetCheckRevByCrl (see comments in pkix_certstore.h)
379 */
380PKIX_Error *
381PKIX_CertStore_GetCrlCheckerFn(
382        PKIX_CertStore *store,
383        PKIX_CertStore_CheckRevokationByCrlCallback *pCallback,
384        void *plContext)
385{
386        PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetTrustCallback");
387        PKIX_NULLCHECK_TWO(store, pCallback);
388
389        *pCallback = store->checkRevByCrlCallback;
390
391        PKIX_RETURN(CERTSTORE);
392}
393
394/*
395 * FUNCTION: PKIX_CertStore_GetCertStoreContext
396 * (see comments in pkix_certstore.h)
397 */
398PKIX_Error *
399PKIX_CertStore_GetCertStoreContext(
400        PKIX_CertStore *store,
401        PKIX_PL_Object **pCertStoreContext,
402        void *plContext)
403{
404        PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetCertStoreContext");
405        PKIX_NULLCHECK_TWO(store, pCertStoreContext);
406
407        PKIX_INCREF(store->certStoreContext);
408        *pCertStoreContext = store->certStoreContext;
409
410cleanup:
411        PKIX_RETURN(CERTSTORE);
412}
413
414/*
415 * FUNCTION: PKIX_CertStore_GetCertStoreCacheFlag
416 * (see comments in pkix_certstore.h)
417 */
418PKIX_Error *
419PKIX_CertStore_GetCertStoreCacheFlag(
420        PKIX_CertStore *store,
421        PKIX_Boolean *pCacheFlag,
422        void *plContext)
423{
424        PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetCertStoreCacheFlag");
425        PKIX_NULLCHECK_TWO(store, pCacheFlag);
426
427        *pCacheFlag = store->cacheFlag;
428
429        PKIX_RETURN(CERTSTORE);
430}
431
432/*
433 * FUNCTION: PKIX_CertStore_GetLocalFlag
434 * (see comments in pkix_certstore.h)
435 */
436PKIX_Error *
437PKIX_CertStore_GetLocalFlag(
438        PKIX_CertStore *store,
439        PKIX_Boolean *pLocalFlag,
440        void *plContext)
441{
442        PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetLocalFlag");
443        PKIX_NULLCHECK_TWO(store, pLocalFlag);
444
445        *pLocalFlag = store->localFlag;
446
447        PKIX_RETURN(CERTSTORE);
448}