/security/nss/lib/libpkix/pkix_pl_nss/pki/pkix_pl_certpolicyinfo.c

http://github.com/zpao/v8monkey · C · 404 lines · 204 code · 72 blank · 128 comment · 5 complexity · ff50356e8cd2de1ba7274068524779e5 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_pl_certpolicyinfo.c
  39. *
  40. * CertPolicyInfo Type Functions
  41. *
  42. */
  43. #include "pkix_pl_certpolicyinfo.h"
  44. /*
  45. * FUNCTION: pkix_pl_CertPolicyInfo_Create
  46. * DESCRIPTION:
  47. *
  48. * Creates a new CertPolicyInfo Object using the OID pointed to by "oid" and
  49. * the List of CertPolicyQualifiers pointed to by "qualifiers", and stores it
  50. * at "pObject". If a non-NULL list is provided, the caller is expected to
  51. * have already set it to be immutable. The caller may provide an empty List,
  52. * but a NULL List is preferable so a user does not need to call
  53. * List_GetLength to get the number of qualifiers.
  54. *
  55. * PARAMETERS
  56. * "oid"
  57. * OID of the desired PolicyInfo ID; must be non-NULL
  58. * "qualifiers"
  59. * List of CertPolicyQualifiers; may be NULL or empty
  60. * "pObject"
  61. * Address where object pointer will be stored. Must be non-NULL.
  62. * "plContext"
  63. * Platform-specific context pointer.
  64. * THREAD SAFETY:
  65. * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
  66. * RETURNS:
  67. * Returns NULL if the function succeeds.
  68. * Returns a Fatal Error if the function fails in an unrecoverable way.
  69. */
  70. PKIX_Error *
  71. pkix_pl_CertPolicyInfo_Create(
  72. PKIX_PL_OID *oid,
  73. PKIX_List *qualifiers,
  74. PKIX_PL_CertPolicyInfo **pObject,
  75. void *plContext)
  76. {
  77. PKIX_PL_CertPolicyInfo *policyInfo = NULL;
  78. PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_Create");
  79. PKIX_NULLCHECK_TWO(oid, pObject);
  80. PKIX_CHECK(PKIX_PL_Object_Alloc
  81. (PKIX_CERTPOLICYINFO_TYPE,
  82. sizeof (PKIX_PL_CertPolicyInfo),
  83. (PKIX_PL_Object **)&policyInfo,
  84. plContext),
  85. PKIX_COULDNOTCREATECERTPOLICYINFOOBJECT);
  86. PKIX_INCREF(oid);
  87. policyInfo->cpID = oid;
  88. PKIX_INCREF(qualifiers);
  89. policyInfo->policyQualifiers = qualifiers;
  90. *pObject = policyInfo;
  91. policyInfo = NULL;
  92. cleanup:
  93. PKIX_DECREF(policyInfo);
  94. PKIX_RETURN(CERTPOLICYINFO);
  95. }
  96. /*
  97. * FUNCTION: pkix_pl_CertPolicyInfo_Destroy
  98. * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
  99. */
  100. static PKIX_Error *
  101. pkix_pl_CertPolicyInfo_Destroy(
  102. PKIX_PL_Object *object,
  103. void *plContext)
  104. {
  105. PKIX_PL_CertPolicyInfo *certPI = NULL;
  106. PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_Destroy");
  107. PKIX_NULLCHECK_ONE(object);
  108. PKIX_CHECK(pkix_CheckType(object, PKIX_CERTPOLICYINFO_TYPE, plContext),
  109. PKIX_OBJECTNOTCERTPOLICYINFO);
  110. certPI = (PKIX_PL_CertPolicyInfo*)object;
  111. PKIX_DECREF(certPI->cpID);
  112. PKIX_DECREF(certPI->policyQualifiers);
  113. cleanup:
  114. PKIX_RETURN(CERTPOLICYINFO);
  115. }
  116. /*
  117. * FUNCTION: pkix_pl_CertPolicyInfo_ToString
  118. * (see comments for PKIX_PL_ToStringCallback in pkix_pl_system.h)
  119. */
  120. static PKIX_Error *
  121. pkix_pl_CertPolicyInfo_ToString(
  122. PKIX_PL_Object *object,
  123. PKIX_PL_String **pString,
  124. void *plContext)
  125. {
  126. PKIX_PL_CertPolicyInfo *certPI = NULL;
  127. PKIX_PL_String *oidString = NULL;
  128. PKIX_PL_String *listString = NULL;
  129. PKIX_PL_String *format = NULL;
  130. PKIX_PL_String *outString = NULL;
  131. PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_ToString");
  132. PKIX_NULLCHECK_TWO(object, pString);
  133. PKIX_CHECK(pkix_CheckType(object, PKIX_CERTPOLICYINFO_TYPE, plContext),
  134. PKIX_OBJECTNOTCERTPOLICYINFO);
  135. certPI = (PKIX_PL_CertPolicyInfo *)object;
  136. PKIX_NULLCHECK_ONE(certPI->cpID);
  137. PKIX_TOSTRING
  138. (certPI->cpID,
  139. &oidString,
  140. plContext,
  141. PKIX_OIDTOSTRINGFAILED);
  142. PKIX_TOSTRING
  143. (certPI->policyQualifiers,
  144. &listString,
  145. plContext,
  146. PKIX_LISTTOSTRINGFAILED);
  147. /* Put them together in the form OID[Qualifiers] */
  148. PKIX_CHECK(PKIX_PL_String_Create
  149. (PKIX_ESCASCII, "%s[%s]", 0, &format, plContext),
  150. PKIX_ERRORINSTRINGCREATE);
  151. PKIX_CHECK(PKIX_PL_Sprintf
  152. (&outString, plContext, format, oidString, listString),
  153. PKIX_ERRORINSPRINTF);
  154. *pString = outString;
  155. cleanup:
  156. PKIX_DECREF(oidString);
  157. PKIX_DECREF(listString);
  158. PKIX_DECREF(format);
  159. PKIX_RETURN(CERTPOLICYINFO);
  160. }
  161. /*
  162. * FUNCTION: pkix_pl_CertPolicyInfo_Hashcode
  163. * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h)
  164. */
  165. static PKIX_Error *
  166. pkix_pl_CertPolicyInfo_Hashcode(
  167. PKIX_PL_Object *object,
  168. PKIX_UInt32 *pHashcode,
  169. void *plContext)
  170. {
  171. PKIX_PL_CertPolicyInfo *certPI = NULL;
  172. PKIX_UInt32 oidHash = 0;
  173. PKIX_UInt32 listHash = 0;
  174. PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_Hashcode");
  175. PKIX_NULLCHECK_TWO(object, pHashcode);
  176. PKIX_CHECK(pkix_CheckType(object, PKIX_CERTPOLICYINFO_TYPE, plContext),
  177. PKIX_OBJECTNOTCERTPOLICYINFO);
  178. certPI = (PKIX_PL_CertPolicyInfo *)object;
  179. PKIX_NULLCHECK_ONE(certPI->cpID);
  180. PKIX_HASHCODE
  181. (certPI->cpID,
  182. &oidHash,
  183. plContext,
  184. PKIX_ERRORINOIDHASHCODE);
  185. PKIX_HASHCODE
  186. (certPI->policyQualifiers,
  187. &listHash,
  188. plContext,
  189. PKIX_ERRORINLISTHASHCODE);
  190. *pHashcode = (31 * oidHash) + listHash;
  191. cleanup:
  192. PKIX_RETURN(CERTPOLICYINFO);
  193. }
  194. /*
  195. * FUNCTION: pkix_pl_CertPolicyInfo_Equals
  196. * (see comments for PKIX_PL_Equals_Callback in pkix_pl_system.h)
  197. */
  198. static PKIX_Error *
  199. pkix_pl_CertPolicyInfo_Equals(
  200. PKIX_PL_Object *firstObject,
  201. PKIX_PL_Object *secondObject,
  202. PKIX_Boolean *pResult,
  203. void *plContext)
  204. {
  205. PKIX_PL_CertPolicyInfo *firstCPI = NULL;
  206. PKIX_PL_CertPolicyInfo *secondCPI = NULL;
  207. PKIX_UInt32 secondType = 0;
  208. PKIX_Boolean compare = PKIX_FALSE;
  209. PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_Equals");
  210. PKIX_NULLCHECK_THREE(firstObject, secondObject, pResult);
  211. /* test that firstObject is a CertPolicyInfo */
  212. PKIX_CHECK(pkix_CheckType
  213. (firstObject, PKIX_CERTPOLICYINFO_TYPE, plContext),
  214. PKIX_FIRSTOBJECTNOTCERTPOLICYINFO);
  215. /*
  216. * Since we know firstObject is a CertPolicyInfo,
  217. * if both references are identical, they must be equal
  218. */
  219. if (firstObject == secondObject){
  220. *pResult = PKIX_TRUE;
  221. goto cleanup;
  222. }
  223. /*
  224. * If secondObject isn't a CertPolicyInfo, we
  225. * don't throw an error. We simply return FALSE.
  226. */
  227. PKIX_CHECK(PKIX_PL_Object_GetType
  228. (secondObject, &secondType, plContext),
  229. PKIX_COULDNOTGETTYPEOFSECONDARGUMENT);
  230. if (secondType != PKIX_CERTPOLICYINFO_TYPE) {
  231. *pResult = PKIX_FALSE;
  232. goto cleanup;
  233. }
  234. firstCPI = (PKIX_PL_CertPolicyInfo *)firstObject;
  235. secondCPI = (PKIX_PL_CertPolicyInfo *)secondObject;
  236. /*
  237. * Compare the value of the OID components
  238. */
  239. PKIX_NULLCHECK_TWO(firstCPI->cpID, secondCPI->cpID);
  240. PKIX_EQUALS
  241. (firstCPI->cpID,
  242. secondCPI->cpID,
  243. &compare,
  244. plContext,
  245. PKIX_OIDEQUALSFAILED);
  246. /*
  247. * If the OIDs did not match, we don't need to
  248. * compare the Lists. If the OIDs did match,
  249. * the return value is the value of the
  250. * List comparison.
  251. */
  252. if (compare) {
  253. PKIX_EQUALS
  254. (firstCPI->policyQualifiers,
  255. secondCPI->policyQualifiers,
  256. &compare,
  257. plContext,
  258. PKIX_LISTEQUALSFAILED);
  259. }
  260. *pResult = compare;
  261. cleanup:
  262. PKIX_RETURN(CERTPOLICYINFO);
  263. }
  264. /*
  265. * FUNCTION: pkix_pl_CertPolicyInfo_RegisterSelf
  266. * DESCRIPTION:
  267. * Registers PKIX_CERTPOLICYINFO_TYPE and its related
  268. * functions with systemClasses[]
  269. * THREAD SAFETY:
  270. * Not Thread Safe - for performance and complexity reasons
  271. *
  272. * Since this function is only called by PKIX_PL_Initialize,
  273. * which should only be called once, it is acceptable that
  274. * this function is not thread-safe.
  275. */
  276. PKIX_Error *
  277. pkix_pl_CertPolicyInfo_RegisterSelf(void *plContext)
  278. {
  279. extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
  280. pkix_ClassTable_Entry entry;
  281. PKIX_ENTER(CERTPOLICYINFO, "pkix_pl_CertPolicyInfo_RegisterSelf");
  282. entry.description = "CertPolicyInfo";
  283. entry.objCounter = 0;
  284. entry.typeObjectSize = sizeof(PKIX_PL_CertPolicyInfo);
  285. entry.destructor = pkix_pl_CertPolicyInfo_Destroy;
  286. entry.equalsFunction = pkix_pl_CertPolicyInfo_Equals;
  287. entry.hashcodeFunction = pkix_pl_CertPolicyInfo_Hashcode;
  288. entry.toStringFunction = pkix_pl_CertPolicyInfo_ToString;
  289. entry.comparator = NULL;
  290. entry.duplicateFunction = pkix_duplicateImmutable;
  291. systemClasses[PKIX_CERTPOLICYINFO_TYPE] = entry;
  292. PKIX_RETURN(CERTPOLICYINFO);
  293. }
  294. /* --Public-CertPolicyInfo-Functions------------------------- */
  295. /*
  296. * FUNCTION: PKIX_PL_CertPolicyInfo_GetPolicyId
  297. * (see comments in pkix_pl_pki.h)
  298. */
  299. PKIX_Error *
  300. PKIX_PL_CertPolicyInfo_GetPolicyId(
  301. PKIX_PL_CertPolicyInfo *policyInfo,
  302. PKIX_PL_OID **pPolicyId,
  303. void *plContext)
  304. {
  305. PKIX_ENTER(CERTPOLICYINFO, "PKIX_PL_CertPolicyInfo_GetPolicyId");
  306. PKIX_NULLCHECK_TWO(policyInfo, pPolicyId);
  307. PKIX_INCREF(policyInfo->cpID);
  308. *pPolicyId = policyInfo->cpID;
  309. cleanup:
  310. PKIX_RETURN(CERTPOLICYINFO);
  311. }
  312. /*
  313. * FUNCTION: PKIX_PL_CertPolicyInfo_GetPolQualifiers
  314. * (see comments in pkix_pl_pki.h)
  315. */
  316. PKIX_Error *
  317. PKIX_PL_CertPolicyInfo_GetPolQualifiers(
  318. PKIX_PL_CertPolicyInfo *policyInfo,
  319. PKIX_List **pQuals,
  320. void *plContext)
  321. {
  322. PKIX_ENTER(CERTPOLICYINFO, "PKIX_PL_CertPolicyInfo_GetPolQualifiers");
  323. PKIX_NULLCHECK_TWO(policyInfo, pQuals);
  324. PKIX_INCREF(policyInfo->policyQualifiers);
  325. /*
  326. * This List is created in PKIX_PL_Cert_DecodePolicyInfo
  327. * and is set immutable immediately after being created.
  328. */
  329. *pQuals = policyInfo->policyQualifiers;
  330. cleanup:
  331. PKIX_RETURN(CERTPOLICYINFO);
  332. }