/security/nss/lib/libpkix/pkix/results/pkix_buildresult.c

http://github.com/zpao/v8monkey · C · 395 lines · 206 code · 79 blank · 110 comment · 4 complexity · 170716070b47f575bbd824e40bd3b500 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_buildresult.c
  39. *
  40. * BuildResult Object Functions
  41. *
  42. */
  43. #include "pkix_buildresult.h"
  44. /* --Private-Functions-------------------------------------------- */
  45. /*
  46. * FUNCTION: pkix_BuildResult_Destroy
  47. * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
  48. */
  49. static PKIX_Error *
  50. pkix_BuildResult_Destroy(
  51. PKIX_PL_Object *object,
  52. void *plContext)
  53. {
  54. PKIX_BuildResult *result = NULL;
  55. PKIX_ENTER(BUILDRESULT, "pkix_BuildResult_Destroy");
  56. PKIX_NULLCHECK_ONE(object);
  57. /* Check that this object is a build result object */
  58. PKIX_CHECK(pkix_CheckType(object, PKIX_BUILDRESULT_TYPE, plContext),
  59. PKIX_OBJECTNOTBUILDRESULT);
  60. result = (PKIX_BuildResult *)object;
  61. PKIX_DECREF(result->valResult);
  62. PKIX_DECREF(result->certChain);
  63. cleanup:
  64. PKIX_RETURN(BUILDRESULT);
  65. }
  66. /*
  67. * FUNCTION: pkix_BuildResult_Equals
  68. * (see comments for PKIX_PL_EqualsCallback in pkix_pl_system.h)
  69. */
  70. static PKIX_Error *
  71. pkix_BuildResult_Equals(
  72. PKIX_PL_Object *first,
  73. PKIX_PL_Object *second,
  74. PKIX_Boolean *pResult,
  75. void *plContext)
  76. {
  77. PKIX_UInt32 secondType;
  78. PKIX_Boolean cmpResult;
  79. PKIX_BuildResult *firstBuildResult = NULL;
  80. PKIX_BuildResult *secondBuildResult = NULL;
  81. PKIX_ENTER(BUILDRESULT, "pkix_BuildResult_Equals");
  82. PKIX_NULLCHECK_THREE(first, second, pResult);
  83. PKIX_CHECK(pkix_CheckType(first, PKIX_BUILDRESULT_TYPE, plContext),
  84. PKIX_FIRSTOBJECTNOTBUILDRESULT);
  85. PKIX_CHECK(PKIX_PL_Object_GetType(second, &secondType, plContext),
  86. PKIX_COULDNOTGETTYPEOFSECONDARGUMENT);
  87. *pResult = PKIX_FALSE;
  88. if (secondType != PKIX_BUILDRESULT_TYPE) goto cleanup;
  89. firstBuildResult = (PKIX_BuildResult *)first;
  90. secondBuildResult = (PKIX_BuildResult *)second;
  91. PKIX_CHECK(PKIX_PL_Object_Equals
  92. ((PKIX_PL_Object *)firstBuildResult->valResult,
  93. (PKIX_PL_Object *)secondBuildResult->valResult,
  94. &cmpResult,
  95. plContext),
  96. PKIX_OBJECTEQUALSFAILED);
  97. if (!cmpResult) goto cleanup;
  98. PKIX_CHECK(PKIX_PL_Object_Equals
  99. ((PKIX_PL_Object *)firstBuildResult->certChain,
  100. (PKIX_PL_Object *)secondBuildResult->certChain,
  101. &cmpResult,
  102. plContext),
  103. PKIX_OBJECTEQUALSFAILED);
  104. if (!cmpResult) goto cleanup;
  105. /*
  106. * The remaining case is that both are null,
  107. * which we consider equality.
  108. * cmpResult = PKIX_TRUE;
  109. */
  110. *pResult = cmpResult;
  111. cleanup:
  112. PKIX_RETURN(BUILDRESULT);
  113. }
  114. /*
  115. * FUNCTION: pkix_BuildResult_Hashcode
  116. * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h)
  117. */
  118. static PKIX_Error *
  119. pkix_BuildResult_Hashcode(
  120. PKIX_PL_Object *object,
  121. PKIX_UInt32 *pHashcode,
  122. void *plContext)
  123. {
  124. PKIX_BuildResult *buildResult = NULL;
  125. PKIX_UInt32 hash = 0;
  126. PKIX_UInt32 valResultHash = 0;
  127. PKIX_UInt32 certChainHash = 0;
  128. PKIX_ENTER(BUILDRESULT, "pkix_BuildResult_Hashcode");
  129. PKIX_NULLCHECK_TWO(object, pHashcode);
  130. PKIX_CHECK(pkix_CheckType(object, PKIX_BUILDRESULT_TYPE, plContext),
  131. PKIX_OBJECTNOTBUILDRESULT);
  132. buildResult = (PKIX_BuildResult*)object;
  133. PKIX_CHECK(PKIX_PL_Object_Hashcode
  134. ((PKIX_PL_Object *)buildResult->valResult,
  135. &valResultHash,
  136. plContext),
  137. PKIX_OBJECTHASHCODEFAILED);
  138. PKIX_CHECK(PKIX_PL_Object_Hashcode
  139. ((PKIX_PL_Object *)buildResult->certChain,
  140. &certChainHash,
  141. plContext),
  142. PKIX_OBJECTHASHCODEFAILED);
  143. hash = 31*(31 * valResultHash + certChainHash);
  144. *pHashcode = hash;
  145. cleanup:
  146. PKIX_RETURN(BUILDRESULT);
  147. }
  148. /*
  149. * FUNCTION: pkix_BuildResult_ToString
  150. * (see comments for PKIX_PL_ToStringCallback in pkix_pl_system.h)
  151. */
  152. static PKIX_Error *
  153. pkix_BuildResult_ToString(
  154. PKIX_PL_Object *object,
  155. PKIX_PL_String **pString,
  156. void *plContext)
  157. {
  158. PKIX_BuildResult *buildResult = NULL;
  159. PKIX_PL_String *formatString = NULL;
  160. PKIX_PL_String *buildResultString = NULL;
  161. PKIX_ValidateResult *valResult = NULL;
  162. PKIX_List *certChain = NULL;
  163. PKIX_PL_String *valResultString = NULL;
  164. PKIX_PL_String *certChainString = NULL;
  165. char *asciiFormat =
  166. "[\n"
  167. "\tValidateResult: \t\t%s"
  168. "\tCertChain: \t\t%s\n"
  169. "]\n";
  170. PKIX_ENTER(BUILDRESULT, "pkix_BuildResult_ToString");
  171. PKIX_NULLCHECK_TWO(object, pString);
  172. PKIX_CHECK(pkix_CheckType(object, PKIX_BUILDRESULT_TYPE, plContext),
  173. PKIX_OBJECTNOTBUILDRESULT);
  174. buildResult = (PKIX_BuildResult*)object;
  175. valResult = buildResult->valResult;
  176. PKIX_CHECK(PKIX_PL_String_Create
  177. (PKIX_ESCASCII, asciiFormat, 0, &formatString, plContext),
  178. PKIX_STRINGCREATEFAILED);
  179. PKIX_CHECK(PKIX_PL_Object_ToString
  180. ((PKIX_PL_Object *)valResult, &valResultString, plContext),
  181. PKIX_OBJECTTOSTRINGFAILED);
  182. certChain = buildResult->certChain;
  183. PKIX_CHECK(PKIX_PL_Object_ToString
  184. ((PKIX_PL_Object *)certChain, &certChainString, plContext),
  185. PKIX_OBJECTTOSTRINGFAILED);
  186. PKIX_CHECK(PKIX_PL_Sprintf
  187. (&buildResultString,
  188. plContext,
  189. formatString,
  190. valResultString,
  191. certChainString),
  192. PKIX_SPRINTFFAILED);
  193. *pString = buildResultString;
  194. cleanup:
  195. PKIX_DECREF(formatString);
  196. PKIX_DECREF(valResultString);
  197. PKIX_DECREF(certChainString);
  198. PKIX_RETURN(BUILDRESULT);
  199. }
  200. /*
  201. * FUNCTION: pkix_BuildResult_RegisterSelf
  202. * DESCRIPTION:
  203. * Registers PKIX_BUILDRESULT_TYPE and its related functions with
  204. * systemClasses[]
  205. * THREAD SAFETY:
  206. * Not Thread Safe - for performance and complexity reasons
  207. *
  208. * Since this function is only called by PKIX_PL_Initialize, which should
  209. * only be called once, it is acceptable that this function is not
  210. * thread-safe.
  211. */
  212. PKIX_Error *
  213. pkix_BuildResult_RegisterSelf(void *plContext)
  214. {
  215. extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
  216. pkix_ClassTable_Entry entry;
  217. PKIX_ENTER(BUILDRESULT, "pkix_BuildResult_RegisterSelf");
  218. entry.description = "BuildResult";
  219. entry.objCounter = 0;
  220. entry.typeObjectSize = sizeof(PKIX_BuildResult);
  221. entry.destructor = pkix_BuildResult_Destroy;
  222. entry.equalsFunction = pkix_BuildResult_Equals;
  223. entry.hashcodeFunction = pkix_BuildResult_Hashcode;
  224. entry.toStringFunction = pkix_BuildResult_ToString;
  225. entry.comparator = NULL;
  226. entry.duplicateFunction = pkix_duplicateImmutable;
  227. systemClasses[PKIX_BUILDRESULT_TYPE] = entry;
  228. PKIX_RETURN(BUILDRESULT);
  229. }
  230. /*
  231. * FUNCTION: pkix_BuildResult_Create
  232. * DESCRIPTION:
  233. *
  234. * Creates a new BuildResult Object using the ValidateResult pointed to by
  235. * "valResult" and the List pointed to by "certChain", and stores it at
  236. * "pResult".
  237. *
  238. * PARAMETERS
  239. * "valResult"
  240. * Address of ValidateResult component. Must be non-NULL.
  241. * "certChain
  242. * Address of List component. Must be non-NULL.
  243. * "pResult"
  244. * Address where object pointer will be stored. Must be non-NULL.
  245. * "plContext"
  246. * Platform-specific context pointer.
  247. * THREAD SAFETY:
  248. * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
  249. * RETURNS:
  250. * Returns NULL if the function succeeds.
  251. * Returns a Fatal Error if the function fails in an unrecoverable way.
  252. */
  253. PKIX_Error *
  254. pkix_BuildResult_Create(
  255. PKIX_ValidateResult *valResult,
  256. PKIX_List *certChain,
  257. PKIX_BuildResult **pResult,
  258. void *plContext)
  259. {
  260. PKIX_BuildResult *result = NULL;
  261. PKIX_ENTER(BUILDRESULT, "pkix_BuildResult_Create");
  262. PKIX_NULLCHECK_THREE(valResult, certChain, pResult);
  263. PKIX_CHECK(PKIX_PL_Object_Alloc
  264. (PKIX_BUILDRESULT_TYPE,
  265. sizeof (PKIX_BuildResult),
  266. (PKIX_PL_Object **)&result,
  267. plContext),
  268. PKIX_COULDNOTCREATEBUILDRESULTOBJECT);
  269. /* initialize fields */
  270. PKIX_INCREF(valResult);
  271. result->valResult = valResult;
  272. PKIX_INCREF(certChain);
  273. result->certChain = certChain;
  274. PKIX_CHECK(PKIX_List_SetImmutable(result->certChain, plContext),
  275. PKIX_LISTSETIMMUTABLEFAILED);
  276. *pResult = result;
  277. result = NULL;
  278. cleanup:
  279. PKIX_DECREF(result);
  280. PKIX_RETURN(BUILDRESULT);
  281. }
  282. /* --Public-Functions--------------------------------------------- */
  283. /*
  284. * FUNCTION: PKIX_BuildResult_GetValidateResult
  285. * (see comments in pkix_result.h)
  286. */
  287. PKIX_Error *
  288. PKIX_BuildResult_GetValidateResult(
  289. PKIX_BuildResult *result,
  290. PKIX_ValidateResult **pResult,
  291. void *plContext)
  292. {
  293. PKIX_ENTER(BUILDRESULT, "PKIX_BuildResult_GetValidateResult");
  294. PKIX_NULLCHECK_TWO(result, pResult);
  295. PKIX_INCREF(result->valResult);
  296. *pResult = result->valResult;
  297. cleanup:
  298. PKIX_RETURN(BUILDRESULT);
  299. }
  300. /*
  301. * FUNCTION: PKIX_BuildResult_GetCertChain
  302. * (see comments in pkix_result.h)
  303. */
  304. PKIX_Error *
  305. PKIX_BuildResult_GetCertChain(
  306. PKIX_BuildResult *result,
  307. PKIX_List **pChain,
  308. void *plContext)
  309. {
  310. PKIX_ENTER(BUILDRESULT, "PKIX_BuildResult_GetCertChain");
  311. PKIX_NULLCHECK_TWO(result, pChain);
  312. PKIX_INCREF(result->certChain);
  313. *pChain = result->certChain;
  314. cleanup:
  315. PKIX_RETURN(BUILDRESULT);
  316. }