/security/nss/lib/libpkix/pkix/util/pkix_error.c

http://github.com/zpao/v8monkey · C · 598 lines · 343 code · 117 blank · 138 comment · 55 complexity · 2328c471a345019ae9edc3d15eb31e9c 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_error.c
  39. *
  40. * Error Object Functions
  41. *
  42. */
  43. #include "pkix_error.h"
  44. #undef PKIX_ERRORENTRY
  45. #define PKIX_ERRORENTRY(name,desc,nsserr) #desc
  46. #if defined PKIX_ERROR_DESCRIPTION
  47. const char * const PKIX_ErrorText[] =
  48. {
  49. #include "pkix_errorstrings.h"
  50. };
  51. #else
  52. #include "prprf.h"
  53. #endif /* PKIX_ERROR_DESCRIPTION */
  54. extern const int PKIX_PLErrorIndex[];
  55. /* --Private-Functions-------------------------------------------- */
  56. /*
  57. * FUNCTION: pkix_Error_Equals
  58. * (see comments for PKIX_PL_EqualsCallback in pkix_pl_system.h)
  59. */
  60. static PKIX_Error *
  61. pkix_Error_Equals(
  62. PKIX_PL_Object *firstObject,
  63. PKIX_PL_Object *secondObject,
  64. PKIX_Boolean *pResult,
  65. void *plContext)
  66. {
  67. PKIX_Error *firstError = NULL;
  68. PKIX_Error *secondError = NULL;
  69. PKIX_Error *firstCause = NULL;
  70. PKIX_Error *secondCause = NULL;
  71. PKIX_PL_Object *firstInfo = NULL;
  72. PKIX_PL_Object *secondInfo = NULL;
  73. PKIX_ERRORCLASS firstClass, secondClass;
  74. PKIX_UInt32 secondType;
  75. PKIX_Boolean boolResult, unequalFlag;
  76. PKIX_ENTER(ERROR, "pkix_Error_Equals");
  77. PKIX_NULLCHECK_THREE(firstObject, secondObject, pResult);
  78. unequalFlag = PKIX_FALSE;
  79. /* First just compare pointer values to save time */
  80. if (firstObject == secondObject) {
  81. *pResult = PKIX_TRUE;
  82. goto cleanup;
  83. } else {
  84. /* Result will only be set to true if all tests pass */
  85. *pResult = PKIX_FALSE;
  86. }
  87. PKIX_CHECK(pkix_CheckType(firstObject, PKIX_ERROR_TYPE, plContext),
  88. PKIX_FIRSTOBJECTNOTANERROROBJECT);
  89. PKIX_CHECK(PKIX_PL_Object_GetType
  90. (secondObject, &secondType, plContext),
  91. PKIX_ERRORGETTINGSECONDOBJECTTYPE);
  92. /* If types differ, then return false. Result is already set */
  93. if (secondType != PKIX_ERROR_TYPE) goto cleanup;
  94. /* It is safe to cast to PKIX_Error */
  95. firstError = (PKIX_Error *) firstObject;
  96. secondError = (PKIX_Error *) secondObject;
  97. /* Compare error codes */
  98. firstClass = firstError->errClass;
  99. secondClass = secondError->errClass;
  100. /* If codes differ, return false. Result is already set */
  101. if (firstClass != secondClass) goto cleanup;
  102. /* Compare causes */
  103. firstCause = firstError->cause;
  104. secondCause = secondError->cause;
  105. /* Ensure that either both or none of the causes are NULL */
  106. if (((firstCause != NULL) && (secondCause == NULL))||
  107. ((firstCause == NULL) && (secondCause != NULL)))
  108. unequalFlag = PKIX_TRUE;
  109. if ((firstCause != NULL) && (secondCause != NULL)) {
  110. PKIX_CHECK(PKIX_PL_Object_Equals
  111. ((PKIX_PL_Object*)firstCause,
  112. (PKIX_PL_Object*)secondCause,
  113. &boolResult,
  114. plContext),
  115. PKIX_ERRORINRECURSIVEEQUALSCALL);
  116. /* Set the unequalFlag so that we return after dec refing */
  117. if (boolResult == 0) unequalFlag = PKIX_TRUE;
  118. }
  119. /* If the cause errors are not equal, return null */
  120. if (unequalFlag) goto cleanup;
  121. /* Compare info fields */
  122. firstInfo = firstError->info;
  123. secondInfo = secondError->info;
  124. if (firstInfo != secondInfo) goto cleanup;
  125. /* Ensure that either both or none of the infos are NULL */
  126. if (((firstInfo != NULL) && (secondInfo == NULL))||
  127. ((firstInfo == NULL) && (secondInfo != NULL)))
  128. unequalFlag = PKIX_TRUE;
  129. if ((firstInfo != NULL) && (secondInfo != NULL)) {
  130. PKIX_CHECK(PKIX_PL_Object_Equals
  131. ((PKIX_PL_Object*)firstInfo,
  132. (PKIX_PL_Object*)secondInfo,
  133. &boolResult,
  134. plContext),
  135. PKIX_ERRORINRECURSIVEEQUALSCALL);
  136. /* Set the unequalFlag so that we return after dec refing */
  137. if (boolResult == 0) unequalFlag = PKIX_TRUE;
  138. }
  139. /* If the infos are not equal, return null */
  140. if (unequalFlag) goto cleanup;
  141. /* Compare descs */
  142. if (firstError->errCode != secondError->errCode) {
  143. unequalFlag = PKIX_TRUE;
  144. }
  145. if (firstError->plErr != secondError->plErr) {
  146. unequalFlag = PKIX_TRUE;
  147. }
  148. /* If the unequalFlag was set, return false */
  149. if (unequalFlag) goto cleanup;
  150. /* Errors are equal in all fields at this point */
  151. *pResult = PKIX_TRUE;
  152. cleanup:
  153. PKIX_RETURN(ERROR);
  154. }
  155. /*
  156. * FUNCTION: pkix_Error_Destroy
  157. * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
  158. */
  159. static PKIX_Error *
  160. pkix_Error_Destroy(
  161. PKIX_PL_Object *object,
  162. void *plContext)
  163. {
  164. PKIX_Error *error = NULL;
  165. PKIX_ENTER(ERROR, "pkix_Error_Destroy");
  166. PKIX_NULLCHECK_ONE(object);
  167. PKIX_CHECK(pkix_CheckType(object, PKIX_ERROR_TYPE, plContext),
  168. PKIX_OBJECTNOTANERROR);
  169. error = (PKIX_Error *)object;
  170. PKIX_DECREF(error->cause);
  171. PKIX_DECREF(error->info);
  172. cleanup:
  173. PKIX_RETURN(ERROR);
  174. }
  175. /* XXX This is not thread safe */
  176. static PKIX_UInt32 pkix_error_cause_depth = 1;
  177. /*
  178. * FUNCTION: pkix_Error_ToString
  179. * (see comments for PKIX_PL_ToStringCallback in pkix_pl_system.h)
  180. */
  181. static PKIX_Error *
  182. pkix_Error_ToString(
  183. PKIX_PL_Object *object,
  184. PKIX_PL_String **pString,
  185. void *plContext)
  186. {
  187. PKIX_Error *error = NULL;
  188. PKIX_Error *cause = NULL;
  189. PKIX_PL_String *desc = NULL;
  190. PKIX_PL_String *formatString = NULL;
  191. PKIX_PL_String *causeString = NULL;
  192. PKIX_PL_String *optCauseString = NULL;
  193. PKIX_PL_String *errorNameString = NULL;
  194. char *format = NULL;
  195. PKIX_ERRORCLASS errClass;
  196. PKIX_ENTER(ERROR, "pkix_Error_ToString");
  197. PKIX_NULLCHECK_TWO(object, pString);
  198. PKIX_CHECK(pkix_CheckType(object, PKIX_ERROR_TYPE, plContext),
  199. PKIX_OBJECTNOTANERROR);
  200. error = (PKIX_Error *)object;
  201. /* Get this error's errClass, description and the string of its cause */
  202. errClass = error->errClass;
  203. /* Get the description string */
  204. PKIX_Error_GetDescription(error, &desc, plContext);
  205. /* Get the cause */
  206. cause = error->cause;
  207. /* Get the causes's description string */
  208. if (cause != NULL) {
  209. pkix_error_cause_depth++;
  210. /* Get the cause string */
  211. PKIX_CHECK(PKIX_PL_Object_ToString
  212. ((PKIX_PL_Object*)cause, &causeString, plContext),
  213. PKIX_ERRORGETTINGCAUSESTRING);
  214. format = "\n*** Cause (%d): %s";
  215. PKIX_CHECK(PKIX_PL_String_Create
  216. (PKIX_ESCASCII,
  217. format,
  218. 0,
  219. &formatString,
  220. plContext),
  221. PKIX_STRINGCREATEFAILED);
  222. /* Create the optional Cause String */
  223. PKIX_CHECK(PKIX_PL_Sprintf
  224. (&optCauseString,
  225. plContext,
  226. formatString,
  227. pkix_error_cause_depth,
  228. causeString),
  229. PKIX_SPRINTFFAILED);
  230. PKIX_DECREF(formatString);
  231. pkix_error_cause_depth--;
  232. }
  233. /* Create the Format String */
  234. if (optCauseString != NULL) {
  235. format = "*** %s Error- %s%s";
  236. } else {
  237. format = "*** %s Error- %s";
  238. }
  239. /* Ensure that error errClass is known, otherwise default to Object */
  240. if (errClass >= PKIX_NUMERRORCLASSES) {
  241. errClass = 0;
  242. }
  243. PKIX_CHECK(PKIX_PL_String_Create
  244. (PKIX_ESCASCII,
  245. (void *)PKIX_ERRORCLASSNAMES[errClass],
  246. 0,
  247. &errorNameString,
  248. plContext),
  249. PKIX_STRINGCREATEFAILED);
  250. PKIX_CHECK(PKIX_PL_String_Create
  251. (PKIX_ESCASCII,
  252. format,
  253. 0,
  254. &formatString,
  255. plContext),
  256. PKIX_STRINGCREATEFAILED);
  257. /* Create the output String */
  258. PKIX_CHECK(PKIX_PL_Sprintf
  259. (pString,
  260. plContext,
  261. formatString,
  262. errorNameString,
  263. desc,
  264. optCauseString),
  265. PKIX_SPRINTFFAILED);
  266. cleanup:
  267. PKIX_DECREF(desc);
  268. PKIX_DECREF(causeString);
  269. PKIX_DECREF(formatString);
  270. PKIX_DECREF(optCauseString);
  271. PKIX_DECREF(errorNameString);
  272. PKIX_RETURN(ERROR);
  273. }
  274. /*
  275. * FUNCTION: pkix_Error_Hashcode
  276. * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h)
  277. */
  278. static PKIX_Error *
  279. pkix_Error_Hashcode(
  280. PKIX_PL_Object *object,
  281. PKIX_UInt32 *pResult,
  282. void *plContext)
  283. {
  284. PKIX_ENTER(ERROR, "pkix_Error_Hashcode");
  285. PKIX_NULLCHECK_TWO(object, pResult);
  286. /* XXX Unimplemented */
  287. /* XXX Need to make hashcodes equal when two errors are equal */
  288. *pResult = (PKIX_UInt32)object;
  289. PKIX_RETURN(ERROR);
  290. }
  291. /* --Initializers------------------------------------------------- */
  292. /*
  293. * PKIX_ERRORCLASSNAMES is an array of strings, with each string holding a
  294. * descriptive name for an error errClass. This is used by the default
  295. * PKIX_PL_Error_ToString function.
  296. *
  297. * Note: PKIX_ERRORCLASSES is defined in pkixt.h as a list of error types.
  298. * (More precisely, as a list of invocations of ERRMACRO(type).) The
  299. * macro is expanded in pkixt.h to define error numbers, and here to
  300. * provide corresponding strings. For example, since the fifth ERRMACRO
  301. * entry is MUTEX, then PKIX_MUTEX_ERROR is defined in pkixt.h as 4, and
  302. * PKIX_ERRORCLASSNAMES[4] is initialized here with the value "MUTEX".
  303. */
  304. #undef ERRMACRO
  305. #define ERRMACRO(type) #type
  306. const char *
  307. PKIX_ERRORCLASSNAMES[PKIX_NUMERRORCLASSES] =
  308. {
  309. PKIX_ERRORCLASSES
  310. };
  311. /*
  312. * FUNCTION: pkix_Error_RegisterSelf
  313. * DESCRIPTION:
  314. * Registers PKIX_ERROR_TYPE and its related functions with systemClasses[]
  315. * THREAD SAFETY:
  316. * Not Thread Safe - for performance and complexity reasons
  317. *
  318. * Since this function is only called by PKIX_PL_Initialize, which should
  319. * only be called once, it is acceptable that this function is not
  320. * thread-safe.
  321. */
  322. PKIX_Error *
  323. pkix_Error_RegisterSelf(void *plContext)
  324. {
  325. extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
  326. pkix_ClassTable_Entry entry;
  327. PKIX_ENTER(ERROR, "pkix_Error_RegisterSelf");
  328. entry.description = "Error";
  329. entry.objCounter = 0;
  330. entry.typeObjectSize = sizeof(PKIX_Error);
  331. entry.destructor = pkix_Error_Destroy;
  332. entry.equalsFunction = pkix_Error_Equals;
  333. entry.hashcodeFunction = pkix_Error_Hashcode;
  334. entry.toStringFunction = pkix_Error_ToString;
  335. entry.comparator = NULL;
  336. entry.duplicateFunction = pkix_duplicateImmutable;
  337. systemClasses[PKIX_ERROR_TYPE] = entry;
  338. PKIX_RETURN(ERROR);
  339. }
  340. /* --Public-Functions--------------------------------------------- */
  341. /*
  342. * FUNCTION: PKIX_Error_Create (see comments in pkix_util.h)
  343. */
  344. PKIX_Error *
  345. PKIX_Error_Create(
  346. PKIX_ERRORCLASS errClass,
  347. PKIX_Error *cause,
  348. PKIX_PL_Object *info,
  349. PKIX_ERRORCODE errCode,
  350. PKIX_Error **pError,
  351. void *plContext)
  352. {
  353. PKIX_Error *tempCause = NULL;
  354. PKIX_Error *error = NULL;
  355. PKIX_ENTER(ERROR, "PKIX_Error_Create");
  356. PKIX_NULLCHECK_ONE(pError);
  357. /*
  358. * when called here, if PKIX_PL_Object_Alloc returns an error,
  359. * it must be a PKIX_ALLOC_ERROR
  360. */
  361. pkixErrorResult = PKIX_PL_Object_Alloc
  362. (PKIX_ERROR_TYPE,
  363. ((PKIX_UInt32)(sizeof (PKIX_Error))),
  364. (PKIX_PL_Object **)&error,
  365. plContext);
  366. if (pkixErrorResult) return (pkixErrorResult);
  367. error->errClass = errClass;
  368. /* Ensure we don't have a loop. Follow causes until NULL */
  369. for (tempCause = cause;
  370. tempCause != NULL;
  371. tempCause = tempCause->cause) {
  372. /* If we detect a loop, throw a new error */
  373. if (tempCause == error) {
  374. PKIX_ERROR(PKIX_LOOPOFERRORCAUSEDETECTED);
  375. }
  376. }
  377. PKIX_INCREF(cause);
  378. error->cause = cause;
  379. PKIX_INCREF(info);
  380. error->info = info;
  381. error->errCode = errCode;
  382. error->plErr = PKIX_PLErrorIndex[error->errCode];
  383. *pError = error;
  384. error = NULL;
  385. cleanup:
  386. /* PKIX-XXX Fix for leak during error creation */
  387. PKIX_DECREF(error);
  388. PKIX_RETURN(ERROR);
  389. }
  390. /*
  391. * FUNCTION: PKIX_Error_GetErrorClass (see comments in pkix_util.h)
  392. */
  393. PKIX_Error *
  394. PKIX_Error_GetErrorClass(
  395. PKIX_Error *error,
  396. PKIX_ERRORCLASS *pClass,
  397. void *plContext)
  398. {
  399. PKIX_ENTER(ERROR, "PKIX_Error_GetErrorClass");
  400. PKIX_NULLCHECK_TWO(error, pClass);
  401. *pClass = error->errClass;
  402. PKIX_RETURN(ERROR);
  403. }
  404. /*
  405. * FUNCTION: PKIX_Error_GetErrorCode (see comments in pkix_util.h)
  406. */
  407. PKIX_Error *
  408. PKIX_Error_GetErrorCode(
  409. PKIX_Error *error,
  410. PKIX_ERRORCODE *pCode,
  411. void *plContext)
  412. {
  413. PKIX_ENTER(ERROR, "PKIX_Error_GetErrorCode");
  414. PKIX_NULLCHECK_TWO(error, pCode);
  415. *pCode = error->errCode;
  416. PKIX_RETURN(ERROR);
  417. }
  418. /*
  419. * FUNCTION: PKIX_Error_GetCause (see comments in pkix_util.h)
  420. */
  421. PKIX_Error *
  422. PKIX_Error_GetCause(
  423. PKIX_Error *error,
  424. PKIX_Error **pCause,
  425. void *plContext)
  426. {
  427. PKIX_ENTER(ERROR, "PKIX_Error_GetCause");
  428. PKIX_NULLCHECK_TWO(error, pCause);
  429. if (error->cause != PKIX_ALLOC_ERROR()){
  430. PKIX_INCREF(error->cause);
  431. }
  432. *pCause = error->cause;
  433. cleanup:
  434. PKIX_RETURN(ERROR);
  435. }
  436. /*
  437. * FUNCTION: PKIX_Error_GetSupplementaryInfo (see comments in pkix_util.h)
  438. */
  439. PKIX_Error *
  440. PKIX_Error_GetSupplementaryInfo(
  441. PKIX_Error *error,
  442. PKIX_PL_Object **pInfo,
  443. void *plContext)
  444. {
  445. PKIX_ENTER(ERROR, "PKIX_Error_GetSupplementaryInfo");
  446. PKIX_NULLCHECK_TWO(error, pInfo);
  447. PKIX_INCREF(error->info);
  448. *pInfo = error->info;
  449. cleanup:
  450. PKIX_RETURN(ERROR);
  451. }
  452. /*
  453. * FUNCTION: PKIX_Error_GetDescription (see comments in pkix_util.h)
  454. */
  455. PKIX_Error *
  456. PKIX_Error_GetDescription(
  457. PKIX_Error *error,
  458. PKIX_PL_String **pDesc,
  459. void *plContext)
  460. {
  461. PKIX_PL_String *descString = NULL;
  462. #ifndef PKIX_ERROR_DESCRIPTION
  463. char errorStr[32];
  464. #endif
  465. PKIX_ENTER(ERROR, "PKIX_Error_GetDescription");
  466. PKIX_NULLCHECK_TWO(error, pDesc);
  467. #ifndef PKIX_ERROR_DESCRIPTION
  468. PR_snprintf(errorStr, 32, "Error code: %d", error->errCode);
  469. #endif
  470. PKIX_PL_String_Create(PKIX_ESCASCII,
  471. #if defined PKIX_ERROR_DESCRIPTION
  472. (void *)PKIX_ErrorText[error->errCode],
  473. #else
  474. errorStr,
  475. #endif
  476. 0,
  477. &descString,
  478. plContext);
  479. *pDesc = descString;
  480. PKIX_RETURN(ERROR);
  481. }