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