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