1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/libpkix/pkix/store/pkix_store.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,415 @@ 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_store.c 1.9 + * 1.10 + * CertStore Function Definitions 1.11 + * 1.12 + */ 1.13 + 1.14 +#include "pkix_store.h" 1.15 + 1.16 +/* --CertStore-Private-Functions----------------------------------------- */ 1.17 + 1.18 +/* 1.19 + * FUNCTION: pkix_CertStore_Destroy 1.20 + * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h) 1.21 + */ 1.22 +static PKIX_Error * 1.23 +pkix_CertStore_Destroy( 1.24 + PKIX_PL_Object *object, 1.25 + void *plContext) 1.26 +{ 1.27 + PKIX_CertStore *certStore = NULL; 1.28 + 1.29 + PKIX_ENTER(CERTSTORE, "pkix_CertStore_Destroy"); 1.30 + PKIX_NULLCHECK_ONE(object); 1.31 + 1.32 + /* Check that this object is a CertStore object */ 1.33 + PKIX_CHECK(pkix_CheckType(object, PKIX_CERTSTORE_TYPE, plContext), 1.34 + PKIX_OBJECTNOTCERTSTORE); 1.35 + 1.36 + certStore = (PKIX_CertStore *)object; 1.37 + 1.38 + certStore->certCallback = NULL; 1.39 + certStore->crlCallback = NULL; 1.40 + certStore->certContinue = NULL; 1.41 + certStore->crlContinue = NULL; 1.42 + certStore->trustCallback = NULL; 1.43 + 1.44 + PKIX_DECREF(certStore->certStoreContext); 1.45 + 1.46 +cleanup: 1.47 + 1.48 + PKIX_RETURN(CERTSTORE); 1.49 +} 1.50 + 1.51 +/* 1.52 + * FUNCTION: pkix_CertStore_Hashcode 1.53 + * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h) 1.54 + */ 1.55 +static PKIX_Error * 1.56 +pkix_CertStore_Hashcode( 1.57 + PKIX_PL_Object *object, 1.58 + PKIX_UInt32 *pHashcode, 1.59 + void *plContext) 1.60 +{ 1.61 + PKIX_CertStore *certStore = NULL; 1.62 + PKIX_UInt32 tempHash = 0; 1.63 + 1.64 + PKIX_ENTER(CERTSTORE, "pkix_CertStore_Hashcode"); 1.65 + PKIX_NULLCHECK_TWO(object, pHashcode); 1.66 + 1.67 + PKIX_CHECK(pkix_CheckType(object, PKIX_CERTSTORE_TYPE, plContext), 1.68 + PKIX_OBJECTNOTCERTSTORE); 1.69 + 1.70 + certStore = (PKIX_CertStore *)object; 1.71 + 1.72 + if (certStore->certStoreContext) { 1.73 + PKIX_CHECK(PKIX_PL_Object_Hashcode 1.74 + ((PKIX_PL_Object *) certStore->certStoreContext, 1.75 + &tempHash, 1.76 + plContext), 1.77 + PKIX_CERTSTOREHASHCODEFAILED); 1.78 + } 1.79 + 1.80 + *pHashcode = (PKIX_UInt32) certStore->certCallback + 1.81 + (PKIX_UInt32) certStore->crlCallback + 1.82 + (PKIX_UInt32) certStore->certContinue + 1.83 + (PKIX_UInt32) certStore->crlContinue + 1.84 + (PKIX_UInt32) certStore->trustCallback + 1.85 + (tempHash << 7); 1.86 + 1.87 +cleanup: 1.88 + 1.89 + PKIX_RETURN(CERTSTORE); 1.90 +} 1.91 + 1.92 +/* 1.93 + * FUNCTION: pkix_CertStore_Equals 1.94 + * (see comments for PKIX_PL_EqualsCallback in pkix_pl_system.h) 1.95 + */ 1.96 +static PKIX_Error * 1.97 +pkix_CertStore_Equals( 1.98 + PKIX_PL_Object *firstObject, 1.99 + PKIX_PL_Object *secondObject, 1.100 + PKIX_Int32 *pResult, 1.101 + void *plContext) 1.102 +{ 1.103 + PKIX_CertStore *firstCS = NULL; 1.104 + PKIX_CertStore *secondCS = NULL; 1.105 + PKIX_Boolean cmpResult = PKIX_FALSE; 1.106 + 1.107 + PKIX_ENTER(CERTSTORE, "pkix_CertStore_Equals"); 1.108 + PKIX_NULLCHECK_THREE(firstObject, secondObject, pResult); 1.109 + 1.110 + PKIX_CHECK(pkix_CheckTypes 1.111 + (firstObject, secondObject, PKIX_CERTSTORE_TYPE, plContext), 1.112 + PKIX_ARGUMENTSNOTDATES); 1.113 + 1.114 + firstCS = (PKIX_CertStore *)firstObject; 1.115 + secondCS = (PKIX_CertStore *)secondObject; 1.116 + 1.117 + cmpResult = (firstCS->certCallback == secondCS->certCallback) && 1.118 + (firstCS->crlCallback == secondCS->crlCallback) && 1.119 + (firstCS->certContinue == secondCS->certContinue) && 1.120 + (firstCS->crlContinue == secondCS->crlContinue) && 1.121 + (firstCS->trustCallback == secondCS->trustCallback); 1.122 + 1.123 + if (cmpResult && 1.124 + (firstCS->certStoreContext != secondCS->certStoreContext)) { 1.125 + 1.126 + PKIX_CHECK(PKIX_PL_Object_Equals 1.127 + ((PKIX_PL_Object *) firstCS->certStoreContext, 1.128 + (PKIX_PL_Object *) secondCS->certStoreContext, 1.129 + &cmpResult, 1.130 + plContext), 1.131 + PKIX_CERTSTOREEQUALSFAILED); 1.132 + } 1.133 + 1.134 + *pResult = cmpResult; 1.135 + 1.136 +cleanup: 1.137 + 1.138 + PKIX_RETURN(CERTSTORE); 1.139 +} 1.140 + 1.141 +/* 1.142 + * FUNCTION: pkix_CertStore_RegisterSelf 1.143 + * DESCRIPTION: 1.144 + * Registers PKIX_CERTSTORE_TYPE and its related functions with 1.145 + * systemClasses[] 1.146 + * THREAD SAFETY: 1.147 + * Not Thread Safe - for performance and complexity reasons 1.148 + * 1.149 + * Since this function is only called by PKIX_PL_Initialize, which should 1.150 + * only be called once, it is acceptable that this function is not 1.151 + * thread-safe. 1.152 + */ 1.153 +PKIX_Error * 1.154 +pkix_CertStore_RegisterSelf(void *plContext) 1.155 +{ 1.156 + extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES]; 1.157 + pkix_ClassTable_Entry entry; 1.158 + 1.159 + PKIX_ENTER(CERTSTORE, "pkix_CertStore_RegisterSelf"); 1.160 + 1.161 + entry.description = "CertStore"; 1.162 + entry.objCounter = 0; 1.163 + entry.typeObjectSize = sizeof(PKIX_CertStore); 1.164 + entry.destructor = pkix_CertStore_Destroy; 1.165 + entry.equalsFunction = pkix_CertStore_Equals; 1.166 + entry.hashcodeFunction = pkix_CertStore_Hashcode; 1.167 + entry.toStringFunction = NULL; 1.168 + entry.comparator = NULL; 1.169 + entry.duplicateFunction = pkix_duplicateImmutable; 1.170 + 1.171 + systemClasses[PKIX_CERTSTORE_TYPE] = entry; 1.172 + 1.173 + PKIX_RETURN(CERTSTORE); 1.174 +} 1.175 + 1.176 +/* --CertStore-Public-Functions------------------------------------------ */ 1.177 + 1.178 +/* 1.179 + * FUNCTION: PKIX_CertStore_Create (see comments in pkix_certstore.h) 1.180 + */ 1.181 +PKIX_Error * 1.182 +PKIX_CertStore_Create( 1.183 + PKIX_CertStore_CertCallback certCallback, 1.184 + PKIX_CertStore_CRLCallback crlCallback, 1.185 + PKIX_CertStore_CertContinueFunction certContinue, 1.186 + PKIX_CertStore_CrlContinueFunction crlContinue, 1.187 + PKIX_CertStore_CheckTrustCallback trustCallback, 1.188 + PKIX_CertStore_ImportCrlCallback importCrlCallback, 1.189 + PKIX_CertStore_CheckRevokationByCrlCallback checkRevByCrlCallback, 1.190 + PKIX_PL_Object *certStoreContext, 1.191 + PKIX_Boolean cacheFlag, 1.192 + PKIX_Boolean localFlag, 1.193 + PKIX_CertStore **pStore, 1.194 + void *plContext) 1.195 +{ 1.196 + PKIX_CertStore *certStore = NULL; 1.197 + 1.198 + PKIX_ENTER(CERTSTORE, "PKIX_CertStore_Create"); 1.199 + PKIX_NULLCHECK_THREE(certCallback, crlCallback, pStore); 1.200 + 1.201 + PKIX_CHECK(PKIX_PL_Object_Alloc 1.202 + (PKIX_CERTSTORE_TYPE, 1.203 + sizeof (PKIX_CertStore), 1.204 + (PKIX_PL_Object **)&certStore, 1.205 + plContext), 1.206 + PKIX_COULDNOTCREATECERTSTOREOBJECT); 1.207 + 1.208 + certStore->certCallback = certCallback; 1.209 + certStore->crlCallback = crlCallback; 1.210 + certStore->certContinue = certContinue; 1.211 + certStore->crlContinue = crlContinue; 1.212 + certStore->trustCallback = trustCallback; 1.213 + certStore->importCrlCallback = importCrlCallback; 1.214 + certStore->checkRevByCrlCallback = checkRevByCrlCallback; 1.215 + certStore->cacheFlag = cacheFlag; 1.216 + certStore->localFlag = localFlag; 1.217 + 1.218 + PKIX_INCREF(certStoreContext); 1.219 + certStore->certStoreContext = certStoreContext; 1.220 + 1.221 + *pStore = certStore; 1.222 + certStore = NULL; 1.223 + 1.224 +cleanup: 1.225 + 1.226 + PKIX_DECREF(certStore); 1.227 + 1.228 + PKIX_RETURN(CERTSTORE); 1.229 +} 1.230 + 1.231 +/* 1.232 + * FUNCTION: PKIX_CertStore_GetCertCallback (see comments in pkix_certstore.h) 1.233 + */ 1.234 +PKIX_Error * 1.235 +PKIX_CertStore_GetCertCallback( 1.236 + PKIX_CertStore *store, 1.237 + PKIX_CertStore_CertCallback *pCallback, 1.238 + void *plContext) 1.239 +{ 1.240 + PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetCertCallback"); 1.241 + PKIX_NULLCHECK_TWO(store, pCallback); 1.242 + 1.243 + *pCallback = store->certCallback; 1.244 + 1.245 + PKIX_RETURN(CERTSTORE); 1.246 +} 1.247 + 1.248 +/* 1.249 + * FUNCTION: PKIX_CertStore_GetCRLCallback (see comments in pkix_certstore.h) 1.250 + */ 1.251 +PKIX_Error * 1.252 +PKIX_CertStore_GetCRLCallback( 1.253 + PKIX_CertStore *store, 1.254 + PKIX_CertStore_CRLCallback *pCallback, 1.255 + void *plContext) 1.256 +{ 1.257 + PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetCRLCallback"); 1.258 + PKIX_NULLCHECK_TWO(store, pCallback); 1.259 + 1.260 + *pCallback = store->crlCallback; 1.261 + 1.262 + PKIX_RETURN(CERTSTORE); 1.263 +} 1.264 + 1.265 +/* 1.266 + * FUNCTION: PKIX_CertStore_CertContinue (see comments in pkix_certstore.h) 1.267 + */ 1.268 +PKIX_Error * 1.269 +PKIX_CertStore_CertContinue( 1.270 + PKIX_CertStore *store, 1.271 + PKIX_CertSelector *selector, 1.272 + PKIX_VerifyNode *verifyNode, 1.273 + void **pNBIOContext, 1.274 + PKIX_List **pCertList, 1.275 + void *plContext) 1.276 +{ 1.277 + PKIX_ENTER(CERTSTORE, "PKIX_CertStore_CertContinue"); 1.278 + PKIX_NULLCHECK_FOUR(store, selector, pNBIOContext, pCertList); 1.279 + 1.280 + PKIX_CHECK(store->certContinue 1.281 + (store, selector, verifyNode, 1.282 + pNBIOContext, pCertList, plContext), 1.283 + PKIX_CERTSTORECERTCONTINUEFUNCTIONFAILED); 1.284 + 1.285 +cleanup: 1.286 + 1.287 + PKIX_RETURN(CERTSTORE); 1.288 +} 1.289 + 1.290 +/* 1.291 + * FUNCTION: PKIX_CertStore_CrlContinue (see comments in pkix_certstore.h) 1.292 + */ 1.293 +PKIX_Error * 1.294 +PKIX_CertStore_CrlContinue( 1.295 + PKIX_CertStore *store, 1.296 + PKIX_CRLSelector *selector, 1.297 + void **pNBIOContext, 1.298 + PKIX_List **pCrlList, 1.299 + void *plContext) 1.300 +{ 1.301 + PKIX_ENTER(CERTSTORE, "PKIX_CertStore_CrlContinue"); 1.302 + PKIX_NULLCHECK_FOUR(store, selector, pNBIOContext, pCrlList); 1.303 + 1.304 + PKIX_CHECK(store->crlContinue 1.305 + (store, selector, pNBIOContext, pCrlList, plContext), 1.306 + PKIX_CERTSTORECRLCONTINUEFAILED); 1.307 + 1.308 +cleanup: 1.309 + 1.310 + PKIX_RETURN(CERTSTORE); 1.311 +} 1.312 + 1.313 +/* 1.314 + * FUNCTION: PKIX_CertStore_GetTrustCallback (see comments in pkix_certstore.h) 1.315 + */ 1.316 +PKIX_Error * 1.317 +PKIX_CertStore_GetTrustCallback( 1.318 + PKIX_CertStore *store, 1.319 + PKIX_CertStore_CheckTrustCallback *pCallback, 1.320 + void *plContext) 1.321 +{ 1.322 + PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetTrustCallback"); 1.323 + PKIX_NULLCHECK_TWO(store, pCallback); 1.324 + 1.325 + *pCallback = store->trustCallback; 1.326 + 1.327 + PKIX_RETURN(CERTSTORE); 1.328 +} 1.329 + 1.330 +/* 1.331 + * FUNCTION: PKIX_CertStore_GetImportCrlCallback (see comments in pkix_certstore.h) 1.332 + */ 1.333 +PKIX_Error * 1.334 +PKIX_CertStore_GetImportCrlCallback( 1.335 + PKIX_CertStore *store, 1.336 + PKIX_CertStore_ImportCrlCallback *pCallback, 1.337 + void *plContext) 1.338 +{ 1.339 + PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetTrustCallback"); 1.340 + PKIX_NULLCHECK_TWO(store, pCallback); 1.341 + 1.342 + *pCallback = store->importCrlCallback; 1.343 + 1.344 + PKIX_RETURN(CERTSTORE); 1.345 +} 1.346 + 1.347 +/* 1.348 + * FUNCTION: PKIX_CertStore_GetCheckRevByCrl (see comments in pkix_certstore.h) 1.349 + */ 1.350 +PKIX_Error * 1.351 +PKIX_CertStore_GetCrlCheckerFn( 1.352 + PKIX_CertStore *store, 1.353 + PKIX_CertStore_CheckRevokationByCrlCallback *pCallback, 1.354 + void *plContext) 1.355 +{ 1.356 + PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetTrustCallback"); 1.357 + PKIX_NULLCHECK_TWO(store, pCallback); 1.358 + 1.359 + *pCallback = store->checkRevByCrlCallback; 1.360 + 1.361 + PKIX_RETURN(CERTSTORE); 1.362 +} 1.363 + 1.364 +/* 1.365 + * FUNCTION: PKIX_CertStore_GetCertStoreContext 1.366 + * (see comments in pkix_certstore.h) 1.367 + */ 1.368 +PKIX_Error * 1.369 +PKIX_CertStore_GetCertStoreContext( 1.370 + PKIX_CertStore *store, 1.371 + PKIX_PL_Object **pCertStoreContext, 1.372 + void *plContext) 1.373 +{ 1.374 + PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetCertStoreContext"); 1.375 + PKIX_NULLCHECK_TWO(store, pCertStoreContext); 1.376 + 1.377 + PKIX_INCREF(store->certStoreContext); 1.378 + *pCertStoreContext = store->certStoreContext; 1.379 + 1.380 +cleanup: 1.381 + PKIX_RETURN(CERTSTORE); 1.382 +} 1.383 + 1.384 +/* 1.385 + * FUNCTION: PKIX_CertStore_GetCertStoreCacheFlag 1.386 + * (see comments in pkix_certstore.h) 1.387 + */ 1.388 +PKIX_Error * 1.389 +PKIX_CertStore_GetCertStoreCacheFlag( 1.390 + PKIX_CertStore *store, 1.391 + PKIX_Boolean *pCacheFlag, 1.392 + void *plContext) 1.393 +{ 1.394 + PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetCertStoreCacheFlag"); 1.395 + PKIX_NULLCHECK_TWO(store, pCacheFlag); 1.396 + 1.397 + *pCacheFlag = store->cacheFlag; 1.398 + 1.399 + PKIX_RETURN(CERTSTORE); 1.400 +} 1.401 + 1.402 +/* 1.403 + * FUNCTION: PKIX_CertStore_GetLocalFlag 1.404 + * (see comments in pkix_certstore.h) 1.405 + */ 1.406 +PKIX_Error * 1.407 +PKIX_CertStore_GetLocalFlag( 1.408 + PKIX_CertStore *store, 1.409 + PKIX_Boolean *pLocalFlag, 1.410 + void *plContext) 1.411 +{ 1.412 + PKIX_ENTER(CERTSTORE, "PKIX_CertStore_GetLocalFlag"); 1.413 + PKIX_NULLCHECK_TWO(store, pLocalFlag); 1.414 + 1.415 + *pLocalFlag = store->localFlag; 1.416 + 1.417 + PKIX_RETURN(CERTSTORE); 1.418 +}