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