summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/libpkix/pkix/results/pkix_valresult.c
blob: 25b69e59c33ad6d52aa36afbdb4ce957223911de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
 * pkix_valresult.c
 *
 * ValidateResult Object Functions
 *
 */

#include "pkix_valresult.h"

/* --Private-Functions-------------------------------------------- */

/*
 * FUNCTION: pkix_ValidateResult_Destroy
 * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
 */
static PKIX_Error *
pkix_ValidateResult_Destroy(
        PKIX_PL_Object *object,
        void *plContext)
{
        PKIX_ValidateResult *result = NULL;

        PKIX_ENTER(VALIDATERESULT, "pkix_ValidateResult_Destroy");
        PKIX_NULLCHECK_ONE(object);

        /* Check that this object is a validate result object */
        PKIX_CHECK(pkix_CheckType(object, PKIX_VALIDATERESULT_TYPE, plContext),
                PKIX_OBJECTNOTVALIDATERESULT);

        result = (PKIX_ValidateResult *)object;

        PKIX_DECREF(result->anchor);
        PKIX_DECREF(result->pubKey);
        PKIX_DECREF(result->policyTree);

cleanup:

        PKIX_RETURN(VALIDATERESULT);
}

/*
 * FUNCTION: pkix_ValidateResult_Equals
 * (see comments for PKIX_PL_EqualsCallback in pkix_pl_system.h)
 */
static PKIX_Error *
pkix_ValidateResult_Equals(
        PKIX_PL_Object *first,
        PKIX_PL_Object *second,
        PKIX_Boolean *pResult,
        void *plContext)
{
        PKIX_UInt32 secondType;
        PKIX_Boolean cmpResult;
        PKIX_ValidateResult *firstValResult = NULL;
        PKIX_ValidateResult *secondValResult = NULL;
        PKIX_TrustAnchor *firstAnchor = NULL;
        PKIX_TrustAnchor *secondAnchor = NULL;
        PKIX_PolicyNode *firstTree = NULL;
        PKIX_PolicyNode *secondTree = NULL;

        PKIX_ENTER(VALIDATERESULT, "pkix_ValidateResult_Equals");
        PKIX_NULLCHECK_THREE(first, second, pResult);

        PKIX_CHECK(pkix_CheckType(first, PKIX_VALIDATERESULT_TYPE, plContext),
                PKIX_FIRSTOBJECTNOTVALIDATERESULT);

        PKIX_CHECK(PKIX_PL_Object_GetType(second, &secondType, plContext),
                PKIX_COULDNOTGETTYPEOFSECONDARGUMENT);

        *pResult = PKIX_FALSE;

        if (secondType != PKIX_VALIDATERESULT_TYPE) goto cleanup;

        firstValResult = (PKIX_ValidateResult *)first;
        secondValResult = (PKIX_ValidateResult *)second;

        PKIX_CHECK(PKIX_PL_Object_Equals
                ((PKIX_PL_Object *)firstValResult->pubKey,
                (PKIX_PL_Object *)secondValResult->pubKey,
                &cmpResult,
                plContext),
                PKIX_OBJECTEQUALSFAILED);

        if (!cmpResult) goto cleanup;

        firstAnchor = firstValResult->anchor;
        secondAnchor = secondValResult->anchor;

        if ((firstAnchor != NULL) && (secondAnchor != NULL)) {
                PKIX_CHECK(PKIX_PL_Object_Equals
                        ((PKIX_PL_Object *)firstAnchor,
                        (PKIX_PL_Object *)secondAnchor,
                        &cmpResult,
                        plContext),
                        PKIX_OBJECTEQUALSFAILED);
        } else {
                cmpResult = (firstAnchor == secondAnchor);
        }

        if (!cmpResult) goto cleanup;

        firstTree = firstValResult->policyTree;
        secondTree = secondValResult->policyTree;

        if ((firstTree != NULL) && (secondTree != NULL)) {
                PKIX_CHECK(PKIX_PL_Object_Equals
                        ((PKIX_PL_Object *)firstTree,
                        (PKIX_PL_Object *)secondTree,
                        &cmpResult,
                        plContext),
                        PKIX_OBJECTEQUALSFAILED);
        } else {
                cmpResult = (firstTree == secondTree);
        }

        *pResult = cmpResult;

cleanup:

        PKIX_RETURN(VALIDATERESULT);
}

/*
 * FUNCTION: pkix_ValidateResult_Hashcode
 * (see comments for PKIX_PL_HashcodeCallback in pkix_pl_system.h)
 */
static PKIX_Error *
pkix_ValidateResult_Hashcode(
        PKIX_PL_Object *object,
        PKIX_UInt32 *pHashcode,
        void *plContext)
{
        PKIX_ValidateResult *valResult = NULL;
        PKIX_UInt32 hash = 0;
        PKIX_UInt32 pubKeyHash = 0;
        PKIX_UInt32 anchorHash = 0;
        PKIX_UInt32 policyTreeHash = 0;

        PKIX_ENTER(VALIDATERESULT, "pkix_ValidateResult_Hashcode");
        PKIX_NULLCHECK_TWO(object, pHashcode);

        PKIX_CHECK(pkix_CheckType(object, PKIX_VALIDATERESULT_TYPE, plContext),
                PKIX_OBJECTNOTVALIDATERESULT);

        valResult = (PKIX_ValidateResult*)object;

        PKIX_CHECK(PKIX_PL_Object_Hashcode
                ((PKIX_PL_Object *)valResult->pubKey, &pubKeyHash, plContext),
                PKIX_OBJECTHASHCODEFAILED);

        if (valResult->anchor) {
                PKIX_CHECK(PKIX_PL_Object_Hashcode
                        ((PKIX_PL_Object *)valResult->anchor,
                        &anchorHash,
                        plContext),
                        PKIX_OBJECTHASHCODEFAILED);
        }

        if (valResult->policyTree) {
                PKIX_CHECK(PKIX_PL_Object_Hashcode
                        ((PKIX_PL_Object *)valResult->policyTree,
                        &policyTreeHash,
                        plContext),
                        PKIX_OBJECTHASHCODEFAILED);
        }

        hash = 31*(31 * pubKeyHash + anchorHash) + policyTreeHash;

        *pHashcode = hash;

cleanup:

        PKIX_RETURN(VALIDATERESULT);
}

/*
 * FUNCTION: pkix_ValidateResult_ToString
 * (see comments for PKIX_PL_ToStringCallback in pkix_pl_system.h)
 */
static PKIX_Error *
pkix_ValidateResult_ToString(
        PKIX_PL_Object *object,
        PKIX_PL_String **pString,
        void *plContext)
{
        PKIX_ValidateResult *valResult = NULL;
        PKIX_PL_String *formatString = NULL;
        PKIX_PL_String *valResultString = NULL;

        PKIX_TrustAnchor *anchor = NULL;
        PKIX_PL_PublicKey *pubKey = NULL;
        PKIX_PolicyNode *policyTree = NULL;

        PKIX_PL_String *anchorString = NULL;
        PKIX_PL_String *pubKeyString = NULL;
        PKIX_PL_String *treeString = NULL;
        char *asciiNullString = "(null)";
        char *asciiFormat =
                "[\n"
                "\tTrustAnchor: \t\t%s"
                "\tPubKey:    \t\t%s\n"
                "\tPolicyTree:  \t\t%s\n"
                "]\n";

        PKIX_ENTER(VALIDATERESULT, "pkix_ValidateResult_ToString");
        PKIX_NULLCHECK_TWO(object, pString);

        PKIX_CHECK(pkix_CheckType(object, PKIX_VALIDATERESULT_TYPE, plContext),
                PKIX_OBJECTNOTVALIDATERESULT);

        PKIX_CHECK(PKIX_PL_String_Create
                (PKIX_ESCASCII, asciiFormat, 0, &formatString, plContext),
                PKIX_STRINGCREATEFAILED);

        valResult = (PKIX_ValidateResult*)object;

        anchor = valResult->anchor;

        if (anchor) {
                PKIX_CHECK(PKIX_PL_Object_ToString
                        ((PKIX_PL_Object *)anchor, &anchorString, plContext),
                        PKIX_OBJECTTOSTRINGFAILED);
        } else {
                PKIX_CHECK(PKIX_PL_String_Create
                        (PKIX_ESCASCII,
                        asciiNullString,
                        0,
                        &anchorString,
                        plContext),
                        PKIX_STRINGCREATEFAILED);
        }

        pubKey = valResult->pubKey;

        PKIX_CHECK(PKIX_PL_Object_ToString
                ((PKIX_PL_Object *)pubKey, &pubKeyString, plContext),
                PKIX_OBJECTTOSTRINGFAILED);

        policyTree = valResult->policyTree;

        if (policyTree) {
                PKIX_CHECK(PKIX_PL_Object_ToString
                        ((PKIX_PL_Object *)policyTree, &treeString, plContext),
                        PKIX_OBJECTTOSTRINGFAILED);
        } else {
                PKIX_CHECK(PKIX_PL_String_Create
                        (PKIX_ESCASCII,
                        asciiNullString,
                        0,
                        &treeString,
                        plContext),
                        PKIX_STRINGCREATEFAILED);
        }

        PKIX_CHECK(PKIX_PL_Sprintf
                (&valResultString,
                plContext,
                formatString,
                anchorString,
                pubKeyString,
                treeString),
                PKIX_SPRINTFFAILED);

        *pString = valResultString;

cleanup:

        PKIX_DECREF(formatString);
        PKIX_DECREF(anchorString);
        PKIX_DECREF(pubKeyString);
        PKIX_DECREF(treeString);

        PKIX_RETURN(VALIDATERESULT);
}

/*
 * FUNCTION: pkix_ValidateResult_RegisterSelf
 * DESCRIPTION:
 *  Registers PKIX_VALIDATERESULT_TYPE and its related functions with
 *  systemClasses[]
 * THREAD SAFETY:
 *  Not Thread Safe - for performance and complexity reasons
 *
 *  Since this function is only called by PKIX_PL_Initialize, which should
 *  only be called once, it is acceptable that this function is not
 *  thread-safe.
 */
PKIX_Error *
pkix_ValidateResult_RegisterSelf(void *plContext)
{

        extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
        pkix_ClassTable_Entry entry;

        PKIX_ENTER(VALIDATERESULT, "pkix_ValidateResult_RegisterSelf");

        entry.description = "ValidateResult";
        entry.objCounter = 0;
        entry.typeObjectSize = sizeof(PKIX_ValidateResult);
        entry.destructor = pkix_ValidateResult_Destroy;
        entry.equalsFunction = pkix_ValidateResult_Equals;
        entry.hashcodeFunction = pkix_ValidateResult_Hashcode;
        entry.toStringFunction = pkix_ValidateResult_ToString;
        entry.comparator = NULL;
        entry.duplicateFunction = pkix_duplicateImmutable;

        systemClasses[PKIX_VALIDATERESULT_TYPE] = entry;

        PKIX_RETURN(VALIDATERESULT);
}

/*
 * FUNCTION: pkix_ValidateResult_Create
 * DESCRIPTION:
 *
 *  Creates a new ValidateResult Object using the PublicKey pointed to by
 *  "pubKey", the TrustAnchor pointed to by "anchor", and the PolicyNode
 *  pointed to by "policyTree", and stores it at "pResult".
 *
 * PARAMETERS
 *  "pubKey"
 *      PublicKey of the desired ValidateResult. Must be non-NULL.
 *  "anchor"
 *      TrustAnchor of the desired Validateresult. May be NULL.
 *  "policyTree"
 *      PolicyNode of the desired ValidateResult; may be NULL
 *  "pResult"
 *      Address where object pointer will be stored. Must be non-NULL.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns NULL if the function succeeds.
 *  Returns a Fatal Error if the function fails in an unrecoverable way.
 */
PKIX_Error *
pkix_ValidateResult_Create(
        PKIX_PL_PublicKey *pubKey,
        PKIX_TrustAnchor *anchor,
        PKIX_PolicyNode *policyTree,
        PKIX_ValidateResult **pResult,
        void *plContext)
{
        PKIX_ValidateResult *result = NULL;

        PKIX_ENTER(VALIDATERESULT, "pkix_ValidateResult_Create");
        PKIX_NULLCHECK_TWO(pubKey, pResult);

        PKIX_CHECK(PKIX_PL_Object_Alloc
                    (PKIX_VALIDATERESULT_TYPE,
                    sizeof (PKIX_ValidateResult),
                    (PKIX_PL_Object **)&result,
                    plContext),
                    PKIX_COULDNOTCREATEVALIDATERESULTOBJECT);

        /* initialize fields */

        PKIX_INCREF(pubKey);
        result->pubKey = pubKey;

        PKIX_INCREF(anchor);
        result->anchor = anchor;

        PKIX_INCREF(policyTree);
        result->policyTree = policyTree;

        *pResult = result;
        result = NULL;

cleanup:

        PKIX_DECREF(result);

        PKIX_RETURN(VALIDATERESULT);

}

/* --Public-Functions--------------------------------------------- */

/*
 * FUNCTION: PKIX_ValidateResult_GetPublicKey
 *      (see comments in pkix_result.h)
 */
PKIX_Error *
PKIX_ValidateResult_GetPublicKey(
        PKIX_ValidateResult *result,
        PKIX_PL_PublicKey **pPublicKey,
        void *plContext)
{
        PKIX_ENTER(VALIDATERESULT, "PKIX_ValidateResult_GetPublicKey");
        PKIX_NULLCHECK_TWO(result, pPublicKey);

        PKIX_INCREF(result->pubKey);
        *pPublicKey = result->pubKey;

cleanup:
        PKIX_RETURN(VALIDATERESULT);
}

/*
 * FUNCTION: PKIX_ValidateResult_GetTrustAnchor
 *      (see comments in pkix_result.h)
 */
PKIX_Error *
PKIX_ValidateResult_GetTrustAnchor(
        PKIX_ValidateResult *result,
        PKIX_TrustAnchor **pTrustAnchor,
        void *plContext)
{
        PKIX_ENTER(VALIDATERESULT, "PKIX_ValidateResult_GetTrustAnchor");
        PKIX_NULLCHECK_TWO(result, pTrustAnchor);

        PKIX_INCREF(result->anchor);
        *pTrustAnchor = result->anchor;

cleanup:
        PKIX_RETURN(VALIDATERESULT);
}

/*
 * FUNCTION: PKIX_ValidateResult_GetPolicyTree
 *      (see comments in pkix_result.h)
 */
PKIX_Error *
PKIX_ValidateResult_GetPolicyTree(
        PKIX_ValidateResult *result,
        PKIX_PolicyNode **pPolicyTree,
        void *plContext)
{
        PKIX_ENTER(VALIDATERESULT, "PKIX_ValidateResult_GetPolicyTree");
        PKIX_NULLCHECK_TWO(result, pPolicyTree);

        PKIX_INCREF(result->policyTree);
        (*pPolicyTree) = result->policyTree;

cleanup:
        PKIX_RETURN(VALIDATERESULT);
}