]> git.saurik.com Git - apple/security.git/blob - SecurityTests/clxutils/threadTest/getFields.cpp
Security-57031.10.10.tar.gz
[apple/security.git] / SecurityTests / clxutils / threadTest / getFields.cpp
1 /*
2 * getFields.cpp
3 *
4 * do a "GetAllFields" 'n' times on a known good cert; verify same results
5 * each time
6 */
7 #include "testParams.h"
8 #include <Security/cssm.h>
9 #include <utilLib/common.h>
10 #include <utilLib/cspwrap.h>
11 #include <clAppUtils/clutils.h>
12 #include <clAppUtils/tpUtils.h>
13 #include <security_cdsa_utils/cuFileIo.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <time.h>
17 #include <string.h>
18 #include <Security/oidscert.h>
19 #include <Security/x509defs.h>
20 #include <Security/oidsattr.h>
21
22 #define DO_PAUSE 0
23
24 //static const char *CERT_FILE = "serverpremium.crt";
25 static const char *CERT_FILE = "mypage.apple_v3.100.cer";
26
27 #define NUM_INNER_LOOPS 10
28
29 /* common data, our known good cert, shared by all threads */
30 static unsigned char *certData = NULL;
31 static unsigned certLength = 0;
32
33 /* read in our known good cert file, just once */
34 int getFieldsInit(TestParams *testParams)
35 {
36 if(certData != NULL) {
37 return 0;
38 }
39 if(testParams->verbose) {
40 printf("getFields thread %d: reading cert file %s...\n",
41 testParams->threadNum, CERT_FILE);
42 }
43 if(readFile(CERT_FILE, &certData, &certLength)) {
44 printf("Error reading %s; aborting\n", CERT_FILE);
45 printf("***This test must be run from the clxutils/threadTest directory.\n");
46 return 1;
47 }
48 return 0;
49 }
50
51 static int compareFields(
52 const CSSM_FIELD *fieldPtr1,
53 const CSSM_FIELD *fieldPtr2)
54 {
55 const CSSM_DATA *val1 = &fieldPtr1->FieldValue;
56 const CSSM_DATA *val2 = &fieldPtr2->FieldValue;
57
58 /* OIDs must match exactly */
59 if(!appCompareCssmData(&fieldPtr1->FieldOid, &fieldPtr2->FieldOid)) {
60 printf("***FieldOid miscompare\n");
61 return 1;
62 }
63
64 /* data length must match */
65 if(val1->Length != val2->Length) {
66 printf("***FieldValue.Length miscompare\n");
67 return 1;
68 }
69
70 /*
71 * The hard part. Most OIDs have some kind of C struct pointer in their
72 * FieldValue.Data pointers, so comparison is on an oid-by-oid basis.
73 * We'll just do the easy ones, and the ones we suspect may be causing
74 * trouble.
75 */
76 const CSSM_OID *thisOid = &fieldPtr1->FieldOid;
77 if(appCompareCssmData(thisOid, &CSSMOID_X509V1Version)) {
78 if(!appCompareCssmData(val1, val2)) {
79 printf("***CSSMOID_X509V1Version mismatch\n");
80 return 1;
81 }
82 }
83 else if(appCompareCssmData(thisOid, &CSSMOID_X509V1SerialNumber)) {
84 if(!appCompareCssmData(val1, val2)) {
85 printf("***CSSMOID_X509V1SerialNumber mismatch\n");
86 return 1;
87 }
88 }
89 else if(appCompareCssmData(thisOid, &CSSMOID_X509V1ValidityNotBefore)) {
90 CSSM_X509_TIME *cssmTime1 = (CSSM_X509_TIME *)val1->Data;
91 CSSM_X509_TIME *cssmTime2 = (CSSM_X509_TIME *)val2->Data;
92 if(!appCompareCssmData(&cssmTime1->time, &cssmTime2->time)) {
93 printf("***CSSMOID_X509V1ValidityNotBefore mismatch\n");
94 return 1;
95 }
96 }
97 else if(appCompareCssmData(thisOid, &CSSMOID_X509V1ValidityNotAfter)) {
98 CSSM_X509_TIME *cssmTime1 = (CSSM_X509_TIME *)val1->Data;
99 CSSM_X509_TIME *cssmTime2 = (CSSM_X509_TIME *)val2->Data;
100 if(!appCompareCssmData(&cssmTime1->time, &cssmTime2->time)) {
101 printf("***CSSMOID_X509V1ValidityNotAfter mismatch\n");
102 return 1;
103 }
104 }
105 else if(appCompareCssmData(thisOid, &CSSMOID_X509V1CertificateIssuerUniqueId)) {
106 if(!appCompareCssmData(val1, val2)) {
107 printf("***CSSMOID_X509V1CertificateIssuerUniqueId mismatch\n");
108 return 1;
109 }
110 }
111 else if(appCompareCssmData(thisOid, &CSSMOID_X509V1CertificateSubjectUniqueId)) {
112 if(!appCompareCssmData(val1, val2)) {
113 printf("***CSSMOID_X509V1CertificateSubjectUniqueId mismatch\n");
114 return 1;
115 }
116 }
117 else if(appCompareCssmData(thisOid, &CSSMOID_X509V1Signature)) {
118 if(!appCompareCssmData(val1, val2)) {
119 printf("***CSSMOID_X509V1Signature mismatch\n");
120 return 1;
121 }
122 }
123 return 0;
124 }
125
126 int getFields(TestParams *testParams)
127 {
128 CSSM_RETURN crtn;
129 CSSM_FIELD_PTR fieldPtr1; // reference - mallocd by CL
130 CSSM_FIELD_PTR fieldPtr2; // mallocd by CL
131 uint32 i;
132 uint32 numFields1;
133 uint32 numFields2;
134 unsigned loopNum;
135 CSSM_DATA cert;
136 unsigned dex;
137
138 for(loopNum=0; loopNum<testParams->numLoops; loopNum++) {
139 if(testParams->verbose) {
140 printf("getFields loop %d\n", loopNum);
141 }
142 else if(!testParams->quiet) {
143 printChar(testParams->progressChar);
144 }
145
146 /* get reference fields */
147 cert.Data = certData;
148 cert.Length = certLength;
149 crtn = CSSM_CL_CertGetAllFields(testParams->clHand,
150 &cert,
151 &numFields1,
152 &fieldPtr1);
153 if(crtn) {
154 printError("CSSM_CL_CertGetAllFields(1)", crtn);
155 return 1;
156 }
157
158 for(dex=0; dex<NUM_INNER_LOOPS; dex++) {
159 /* get all fields again */
160 crtn = CSSM_CL_CertGetAllFields(testParams->clHand,
161 &cert,
162 &numFields2,
163 &fieldPtr2);
164 if(crtn) {
165 printError("CSSM_CL_CertGetAllFields(2)", crtn);
166 return 1;
167 }
168
169 /* compare to reference fields */
170 if(numFields1 != numFields2) {
171 printf("***CSSM_CL_CertGetAllFields returned differing numFields "
172 "(%u, %u)\n", (unsigned)numFields1, (unsigned)numFields2);
173 return 1;
174 }
175 for(i=0; i<numFields1; i++) {
176 if(compareFields(&fieldPtr1[i], &fieldPtr2[i])) {
177 return 1;
178 }
179 }
180 crtn = CSSM_CL_FreeFields(testParams->clHand, numFields1, &fieldPtr2);
181 if(crtn) {
182 printError("CSSM_CL_FreeFields", crtn);
183 return 1;
184 }
185 /* leak debug */
186 #if DO_PAUSE
187 fpurge(stdin);
188 printf("Hit CR to continue: ");
189 getchar();
190 #endif
191 } /* inner loop */
192
193 crtn = CSSM_CL_FreeFields(testParams->clHand, numFields1, &fieldPtr1);
194 if(crtn) {
195 printError("CSSM_CL_FreeFields", crtn);
196 return 1;
197 }
198 } /* outer loop */
199 return 0;
200 }