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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rwxr-xr-x

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 /*
michael@0 5 * pkix_valresult.c
michael@0 6 *
michael@0 7 * ValidateResult Object Functions
michael@0 8 *
michael@0 9 */
michael@0 10
michael@0 11 #include "pkix_valresult.h"
michael@0 12
michael@0 13 /* --Private-Functions-------------------------------------------- */
michael@0 14
michael@0 15 /*
michael@0 16 * FUNCTION: pkix_ValidateResult_Destroy
michael@0 17 * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
michael@0 18 */
michael@0 19 static PKIX_Error *
michael@0 20 pkix_ValidateResult_Destroy(
michael@0 21 PKIX_PL_Object *object,
michael@0 22 void *plContext)
michael@0 23 {
michael@0 24 PKIX_ValidateResult *result = NULL;
michael@0 25
michael@0 26 PKIX_ENTER(VALIDATERESULT, "pkix_ValidateResult_Destroy");
michael@0 27 PKIX_NULLCHECK_ONE(object);
michael@0 28
michael@0 29 /* Check that this object is a validate result object */
michael@0 30 PKIX_CHECK(pkix_CheckType(object, PKIX_VALIDATERESULT_TYPE, plContext),
michael@0 31 PKIX_OBJECTNOTVALIDATERESULT);
michael@0 32
michael@0 33 result = (PKIX_ValidateResult *)object;
michael@0 34
michael@0 35 PKIX_DECREF(result->anchor);
michael@0 36 PKIX_DECREF(result->pubKey);
michael@0 37 PKIX_DECREF(result->policyTree);
michael@0 38
michael@0 39 cleanup:
michael@0 40
michael@0 41 PKIX_RETURN(VALIDATERESULT);
michael@0 42 }
michael@0 43
michael@0 44 /*
michael@0 45 * FUNCTION: pkix_ValidateResult_Equals
michael@0 46 * (see comments for PKIX_PL_EqualsCallback in pkix_pl_system.h)
michael@0 47 */
michael@0 48 static PKIX_Error *
michael@0 49 pkix_ValidateResult_Equals(
michael@0 50 PKIX_PL_Object *first,
michael@0 51 PKIX_PL_Object *second,
michael@0 52 PKIX_Boolean *pResult,
michael@0 53 void *plContext)
michael@0 54 {
michael@0 55 PKIX_UInt32 secondType;
michael@0 56 PKIX_Boolean cmpResult;
michael@0 57 PKIX_ValidateResult *firstValResult = NULL;
michael@0 58 PKIX_ValidateResult *secondValResult = NULL;
michael@0 59 PKIX_TrustAnchor *firstAnchor = NULL;
michael@0 60 PKIX_TrustAnchor *secondAnchor = NULL;
michael@0 61 PKIX_PolicyNode *firstTree = NULL;
michael@0 62 PKIX_PolicyNode *secondTree = NULL;
michael@0 63
michael@0 64 PKIX_ENTER(VALIDATERESULT, "pkix_ValidateResult_Equals");
michael@0 65 PKIX_NULLCHECK_THREE(first, second, pResult);
michael@0 66
michael@0 67 PKIX_CHECK(pkix_CheckType(first, PKIX_VALIDATERESULT_TYPE, plContext),
michael@0 68 PKIX_FIRSTOBJECTNOTVALIDATERESULT);
michael@0 69
michael@0 70 PKIX_CHECK(PKIX_PL_Object_GetType(second, &secondType, plContext),
michael@0 71 PKIX_COULDNOTGETTYPEOFSECONDARGUMENT);
michael@0 72
michael@0 73 *pResult = PKIX_FALSE;
michael@0 74
michael@0 75 if (secondType != PKIX_VALIDATERESULT_TYPE) goto cleanup;
michael@0 76
michael@0 77 firstValResult = (PKIX_ValidateResult *)first;
michael@0 78 secondValResult = (PKIX_ValidateResult *)second;
michael@0 79
michael@0 80 PKIX_CHECK(PKIX_PL_Object_Equals
michael@0 81 ((PKIX_PL_Object *)firstValResult->pubKey,
michael@0 82 (PKIX_PL_Object *)secondValResult->pubKey,
michael@0 83 &cmpResult,
michael@0 84 plContext),
michael@0 85 PKIX_OBJECTEQUALSFAILED);
michael@0 86
michael@0 87 if (!cmpResult) goto cleanup;
michael@0 88
michael@0 89 firstAnchor = firstValResult->anchor;
michael@0 90 secondAnchor = secondValResult->anchor;
michael@0 91
michael@0 92 if ((firstAnchor != NULL) && (secondAnchor != NULL)) {
michael@0 93 PKIX_CHECK(PKIX_PL_Object_Equals
michael@0 94 ((PKIX_PL_Object *)firstAnchor,
michael@0 95 (PKIX_PL_Object *)secondAnchor,
michael@0 96 &cmpResult,
michael@0 97 plContext),
michael@0 98 PKIX_OBJECTEQUALSFAILED);
michael@0 99 } else {
michael@0 100 cmpResult = (firstAnchor == secondAnchor);
michael@0 101 }
michael@0 102
michael@0 103 if (!cmpResult) goto cleanup;
michael@0 104
michael@0 105 firstTree = firstValResult->policyTree;
michael@0 106 secondTree = secondValResult->policyTree;
michael@0 107
michael@0 108 if ((firstTree != NULL) && (secondTree != NULL)) {
michael@0 109 PKIX_CHECK(PKIX_PL_Object_Equals
michael@0 110 ((PKIX_PL_Object *)firstTree,
michael@0 111 (PKIX_PL_Object *)secondTree,
michael@0 112 &cmpResult,
michael@0 113 plContext),
michael@0 114 PKIX_OBJECTEQUALSFAILED);
michael@0 115 } else {
michael@0 116 cmpResult = (firstTree == secondTree);
michael@0 117 }
michael@0 118
michael@0 119 *pResult = cmpResult;
michael@0 120
michael@0 121 cleanup:
michael@0 122
michael@0 123 PKIX_RETURN(VALIDATERESULT);
michael@0 124 }
michael@0 125
michael@0 126 /*
michael@0 127 * FUNCTION: pkix_ValidateResult_Hashcode
michael@0 128 * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h)
michael@0 129 */
michael@0 130 static PKIX_Error *
michael@0 131 pkix_ValidateResult_Hashcode(
michael@0 132 PKIX_PL_Object *object,
michael@0 133 PKIX_UInt32 *pHashcode,
michael@0 134 void *plContext)
michael@0 135 {
michael@0 136 PKIX_ValidateResult *valResult = NULL;
michael@0 137 PKIX_UInt32 hash = 0;
michael@0 138 PKIX_UInt32 pubKeyHash = 0;
michael@0 139 PKIX_UInt32 anchorHash = 0;
michael@0 140 PKIX_UInt32 policyTreeHash = 0;
michael@0 141
michael@0 142 PKIX_ENTER(VALIDATERESULT, "pkix_ValidateResult_Hashcode");
michael@0 143 PKIX_NULLCHECK_TWO(object, pHashcode);
michael@0 144
michael@0 145 PKIX_CHECK(pkix_CheckType(object, PKIX_VALIDATERESULT_TYPE, plContext),
michael@0 146 PKIX_OBJECTNOTVALIDATERESULT);
michael@0 147
michael@0 148 valResult = (PKIX_ValidateResult*)object;
michael@0 149
michael@0 150 PKIX_CHECK(PKIX_PL_Object_Hashcode
michael@0 151 ((PKIX_PL_Object *)valResult->pubKey, &pubKeyHash, plContext),
michael@0 152 PKIX_OBJECTHASHCODEFAILED);
michael@0 153
michael@0 154 if (valResult->anchor) {
michael@0 155 PKIX_CHECK(PKIX_PL_Object_Hashcode
michael@0 156 ((PKIX_PL_Object *)valResult->anchor,
michael@0 157 &anchorHash,
michael@0 158 plContext),
michael@0 159 PKIX_OBJECTHASHCODEFAILED);
michael@0 160 }
michael@0 161
michael@0 162 if (valResult->policyTree) {
michael@0 163 PKIX_CHECK(PKIX_PL_Object_Hashcode
michael@0 164 ((PKIX_PL_Object *)valResult->policyTree,
michael@0 165 &policyTreeHash,
michael@0 166 plContext),
michael@0 167 PKIX_OBJECTHASHCODEFAILED);
michael@0 168 }
michael@0 169
michael@0 170 hash = 31*(31 * pubKeyHash + anchorHash) + policyTreeHash;
michael@0 171
michael@0 172 *pHashcode = hash;
michael@0 173
michael@0 174 cleanup:
michael@0 175
michael@0 176 PKIX_RETURN(VALIDATERESULT);
michael@0 177 }
michael@0 178
michael@0 179 /*
michael@0 180 * FUNCTION: pkix_ValidateResult_ToString
michael@0 181 * (see comments for PKIX_PL_ToStringCallback in pkix_pl_system.h)
michael@0 182 */
michael@0 183 static PKIX_Error *
michael@0 184 pkix_ValidateResult_ToString(
michael@0 185 PKIX_PL_Object *object,
michael@0 186 PKIX_PL_String **pString,
michael@0 187 void *plContext)
michael@0 188 {
michael@0 189 PKIX_ValidateResult *valResult = NULL;
michael@0 190 PKIX_PL_String *formatString = NULL;
michael@0 191 PKIX_PL_String *valResultString = NULL;
michael@0 192
michael@0 193 PKIX_TrustAnchor *anchor = NULL;
michael@0 194 PKIX_PL_PublicKey *pubKey = NULL;
michael@0 195 PKIX_PolicyNode *policyTree = NULL;
michael@0 196
michael@0 197 PKIX_PL_String *anchorString = NULL;
michael@0 198 PKIX_PL_String *pubKeyString = NULL;
michael@0 199 PKIX_PL_String *treeString = NULL;
michael@0 200 char *asciiNullString = "(null)";
michael@0 201 char *asciiFormat =
michael@0 202 "[\n"
michael@0 203 "\tTrustAnchor: \t\t%s"
michael@0 204 "\tPubKey: \t\t%s\n"
michael@0 205 "\tPolicyTree: \t\t%s\n"
michael@0 206 "]\n";
michael@0 207
michael@0 208 PKIX_ENTER(VALIDATERESULT, "pkix_ValidateResult_ToString");
michael@0 209 PKIX_NULLCHECK_TWO(object, pString);
michael@0 210
michael@0 211 PKIX_CHECK(pkix_CheckType(object, PKIX_VALIDATERESULT_TYPE, plContext),
michael@0 212 PKIX_OBJECTNOTVALIDATERESULT);
michael@0 213
michael@0 214 PKIX_CHECK(PKIX_PL_String_Create
michael@0 215 (PKIX_ESCASCII, asciiFormat, 0, &formatString, plContext),
michael@0 216 PKIX_STRINGCREATEFAILED);
michael@0 217
michael@0 218 valResult = (PKIX_ValidateResult*)object;
michael@0 219
michael@0 220 anchor = valResult->anchor;
michael@0 221
michael@0 222 if (anchor) {
michael@0 223 PKIX_CHECK(PKIX_PL_Object_ToString
michael@0 224 ((PKIX_PL_Object *)anchor, &anchorString, plContext),
michael@0 225 PKIX_OBJECTTOSTRINGFAILED);
michael@0 226 } else {
michael@0 227 PKIX_CHECK(PKIX_PL_String_Create
michael@0 228 (PKIX_ESCASCII,
michael@0 229 asciiNullString,
michael@0 230 0,
michael@0 231 &anchorString,
michael@0 232 plContext),
michael@0 233 PKIX_STRINGCREATEFAILED);
michael@0 234 }
michael@0 235
michael@0 236 pubKey = valResult->pubKey;
michael@0 237
michael@0 238 PKIX_CHECK(PKIX_PL_Object_ToString
michael@0 239 ((PKIX_PL_Object *)pubKey, &pubKeyString, plContext),
michael@0 240 PKIX_OBJECTTOSTRINGFAILED);
michael@0 241
michael@0 242 policyTree = valResult->policyTree;
michael@0 243
michael@0 244 if (policyTree) {
michael@0 245 PKIX_CHECK(PKIX_PL_Object_ToString
michael@0 246 ((PKIX_PL_Object *)policyTree, &treeString, plContext),
michael@0 247 PKIX_OBJECTTOSTRINGFAILED);
michael@0 248 } else {
michael@0 249 PKIX_CHECK(PKIX_PL_String_Create
michael@0 250 (PKIX_ESCASCII,
michael@0 251 asciiNullString,
michael@0 252 0,
michael@0 253 &treeString,
michael@0 254 plContext),
michael@0 255 PKIX_STRINGCREATEFAILED);
michael@0 256 }
michael@0 257
michael@0 258 PKIX_CHECK(PKIX_PL_Sprintf
michael@0 259 (&valResultString,
michael@0 260 plContext,
michael@0 261 formatString,
michael@0 262 anchorString,
michael@0 263 pubKeyString,
michael@0 264 treeString),
michael@0 265 PKIX_SPRINTFFAILED);
michael@0 266
michael@0 267 *pString = valResultString;
michael@0 268
michael@0 269 cleanup:
michael@0 270
michael@0 271 PKIX_DECREF(formatString);
michael@0 272 PKIX_DECREF(anchorString);
michael@0 273 PKIX_DECREF(pubKeyString);
michael@0 274 PKIX_DECREF(treeString);
michael@0 275
michael@0 276 PKIX_RETURN(VALIDATERESULT);
michael@0 277 }
michael@0 278
michael@0 279 /*
michael@0 280 * FUNCTION: pkix_ValidateResult_RegisterSelf
michael@0 281 * DESCRIPTION:
michael@0 282 * Registers PKIX_VALIDATERESULT_TYPE and its related functions with
michael@0 283 * systemClasses[]
michael@0 284 * THREAD SAFETY:
michael@0 285 * Not Thread Safe - for performance and complexity reasons
michael@0 286 *
michael@0 287 * Since this function is only called by PKIX_PL_Initialize, which should
michael@0 288 * only be called once, it is acceptable that this function is not
michael@0 289 * thread-safe.
michael@0 290 */
michael@0 291 PKIX_Error *
michael@0 292 pkix_ValidateResult_RegisterSelf(void *plContext)
michael@0 293 {
michael@0 294
michael@0 295 extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
michael@0 296 pkix_ClassTable_Entry entry;
michael@0 297
michael@0 298 PKIX_ENTER(VALIDATERESULT, "pkix_ValidateResult_RegisterSelf");
michael@0 299
michael@0 300 entry.description = "ValidateResult";
michael@0 301 entry.objCounter = 0;
michael@0 302 entry.typeObjectSize = sizeof(PKIX_ValidateResult);
michael@0 303 entry.destructor = pkix_ValidateResult_Destroy;
michael@0 304 entry.equalsFunction = pkix_ValidateResult_Equals;
michael@0 305 entry.hashcodeFunction = pkix_ValidateResult_Hashcode;
michael@0 306 entry.toStringFunction = pkix_ValidateResult_ToString;
michael@0 307 entry.comparator = NULL;
michael@0 308 entry.duplicateFunction = pkix_duplicateImmutable;
michael@0 309
michael@0 310 systemClasses[PKIX_VALIDATERESULT_TYPE] = entry;
michael@0 311
michael@0 312 PKIX_RETURN(VALIDATERESULT);
michael@0 313 }
michael@0 314
michael@0 315 /*
michael@0 316 * FUNCTION: pkix_ValidateResult_Create
michael@0 317 * DESCRIPTION:
michael@0 318 *
michael@0 319 * Creates a new ValidateResult Object using the PublicKey pointed to by
michael@0 320 * "pubKey", the TrustAnchor pointed to by "anchor", and the PolicyNode
michael@0 321 * pointed to by "policyTree", and stores it at "pResult".
michael@0 322 *
michael@0 323 * PARAMETERS
michael@0 324 * "pubKey"
michael@0 325 * PublicKey of the desired ValidateResult. Must be non-NULL.
michael@0 326 * "anchor"
michael@0 327 * TrustAnchor of the desired Validateresult. May be NULL.
michael@0 328 * "policyTree"
michael@0 329 * PolicyNode of the desired ValidateResult; may be NULL
michael@0 330 * "pResult"
michael@0 331 * Address where object pointer will be stored. Must be non-NULL.
michael@0 332 * "plContext"
michael@0 333 * Platform-specific context pointer.
michael@0 334 * THREAD SAFETY:
michael@0 335 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
michael@0 336 * RETURNS:
michael@0 337 * Returns NULL if the function succeeds.
michael@0 338 * Returns a Fatal Error if the function fails in an unrecoverable way.
michael@0 339 */
michael@0 340 PKIX_Error *
michael@0 341 pkix_ValidateResult_Create(
michael@0 342 PKIX_PL_PublicKey *pubKey,
michael@0 343 PKIX_TrustAnchor *anchor,
michael@0 344 PKIX_PolicyNode *policyTree,
michael@0 345 PKIX_ValidateResult **pResult,
michael@0 346 void *plContext)
michael@0 347 {
michael@0 348 PKIX_ValidateResult *result = NULL;
michael@0 349
michael@0 350 PKIX_ENTER(VALIDATERESULT, "pkix_ValidateResult_Create");
michael@0 351 PKIX_NULLCHECK_TWO(pubKey, pResult);
michael@0 352
michael@0 353 PKIX_CHECK(PKIX_PL_Object_Alloc
michael@0 354 (PKIX_VALIDATERESULT_TYPE,
michael@0 355 sizeof (PKIX_ValidateResult),
michael@0 356 (PKIX_PL_Object **)&result,
michael@0 357 plContext),
michael@0 358 PKIX_COULDNOTCREATEVALIDATERESULTOBJECT);
michael@0 359
michael@0 360 /* initialize fields */
michael@0 361
michael@0 362 PKIX_INCREF(pubKey);
michael@0 363 result->pubKey = pubKey;
michael@0 364
michael@0 365 PKIX_INCREF(anchor);
michael@0 366 result->anchor = anchor;
michael@0 367
michael@0 368 PKIX_INCREF(policyTree);
michael@0 369 result->policyTree = policyTree;
michael@0 370
michael@0 371 *pResult = result;
michael@0 372 result = NULL;
michael@0 373
michael@0 374 cleanup:
michael@0 375
michael@0 376 PKIX_DECREF(result);
michael@0 377
michael@0 378 PKIX_RETURN(VALIDATERESULT);
michael@0 379
michael@0 380 }
michael@0 381
michael@0 382 /* --Public-Functions--------------------------------------------- */
michael@0 383
michael@0 384 /*
michael@0 385 * FUNCTION: PKIX_ValidateResult_GetPublicKey
michael@0 386 * (see comments in pkix_result.h)
michael@0 387 */
michael@0 388 PKIX_Error *
michael@0 389 PKIX_ValidateResult_GetPublicKey(
michael@0 390 PKIX_ValidateResult *result,
michael@0 391 PKIX_PL_PublicKey **pPublicKey,
michael@0 392 void *plContext)
michael@0 393 {
michael@0 394 PKIX_ENTER(VALIDATERESULT, "PKIX_ValidateResult_GetPublicKey");
michael@0 395 PKIX_NULLCHECK_TWO(result, pPublicKey);
michael@0 396
michael@0 397 PKIX_INCREF(result->pubKey);
michael@0 398 *pPublicKey = result->pubKey;
michael@0 399
michael@0 400 cleanup:
michael@0 401 PKIX_RETURN(VALIDATERESULT);
michael@0 402 }
michael@0 403
michael@0 404 /*
michael@0 405 * FUNCTION: PKIX_ValidateResult_GetTrustAnchor
michael@0 406 * (see comments in pkix_result.h)
michael@0 407 */
michael@0 408 PKIX_Error *
michael@0 409 PKIX_ValidateResult_GetTrustAnchor(
michael@0 410 PKIX_ValidateResult *result,
michael@0 411 PKIX_TrustAnchor **pTrustAnchor,
michael@0 412 void *plContext)
michael@0 413 {
michael@0 414 PKIX_ENTER(VALIDATERESULT, "PKIX_ValidateResult_GetTrustAnchor");
michael@0 415 PKIX_NULLCHECK_TWO(result, pTrustAnchor);
michael@0 416
michael@0 417 PKIX_INCREF(result->anchor);
michael@0 418 *pTrustAnchor = result->anchor;
michael@0 419
michael@0 420 cleanup:
michael@0 421 PKIX_RETURN(VALIDATERESULT);
michael@0 422 }
michael@0 423
michael@0 424 /*
michael@0 425 * FUNCTION: PKIX_ValidateResult_GetPolicyTree
michael@0 426 * (see comments in pkix_result.h)
michael@0 427 */
michael@0 428 PKIX_Error *
michael@0 429 PKIX_ValidateResult_GetPolicyTree(
michael@0 430 PKIX_ValidateResult *result,
michael@0 431 PKIX_PolicyNode **pPolicyTree,
michael@0 432 void *plContext)
michael@0 433 {
michael@0 434 PKIX_ENTER(VALIDATERESULT, "PKIX_ValidateResult_GetPolicyTree");
michael@0 435 PKIX_NULLCHECK_TWO(result, pPolicyTree);
michael@0 436
michael@0 437 PKIX_INCREF(result->policyTree);
michael@0 438 (*pPolicyTree) = result->policyTree;
michael@0 439
michael@0 440 cleanup:
michael@0 441 PKIX_RETURN(VALIDATERESULT);
michael@0 442 }

mercurial