1 /* cgConstructThr.cpp - simple version CertGroupConstruct test */
3 #include "testParams.h"
4 #include <Security/cssm.h>
5 #include <utilLib/common.h>
6 #include <utilLib/cspwrap.h>
7 #include <clAppUtils/clutils.h>
8 #include <clAppUtils/tpUtils.h>
9 #include <clAppUtils/timeStr.h>
15 /* for memory leak debug only, with only one thread running */
18 /*** start of code directly copied from ../cgConstruct/cgConstruct.cpp ***/
19 #define NUM_CERTS_MIN 4
21 #define KEYGEN_ALG_DEF CSSM_ALGID_RSA
22 #define SIG_ALG_DEF CSSM_ALGID_SHA1WithRSA
24 #define DB_NAME_BASE "cgConstruct"
25 #define CG_KEY_SIZE_DEFAULT CSP_RSA_KEY_SIZE_DEFAULT
26 #define SECONDS_TO_LIVE (60 * 60 * 24) /* certs are valid for this long */
28 #define CG_CONSTRUCT_TP_DB 0
30 static int testError()
35 printf("Attach via debugger for more info.\n");
36 printf("a to abort, c to continue: ");
41 #if CG_CONSTRUCT_TP_DB
43 CSSM_DL_HANDLE dlHand
,
45 CSSM_DL_DB_HANDLE_PTR dlDbPtr
,
47 CSSM_BOOL publicReadOnly
, // ignored if !PUBLIC_READ_ENABLE
52 CSSM_BOOL doCreate
= (publicReadOnly
? CSSM_FALSE
: CSSM_TRUE
);
54 for(i
=0; i
<numDbs
; i
++) {
55 sprintf(dbName
, "%s%d", dbNameBase
, i
);
56 CSSM_RETURN crtn
= tpKcOpen(dbName
,
61 printf("Can't create %d DBs\n", numDbs
);
62 return testError(quiet
);
70 CSSM_TP_HANDLE tpHand
,
71 CSSM_CL_HANDLE clHand
,
72 CSSM_CSP_HANDLE cspHand
,
73 CSSM_DL_DB_LIST_PTR dbList
,
78 CSSM_BOOL skipFirstDb
,
79 CSSM_BOOL publicRead
) // close/open with public access
81 unsigned certsToUse
; // # of certs we actually use
82 CSSM_CERTGROUP certGroupFrag
; // INPUT to CertGroupConstruct
83 CSSM_CERTGROUP_PTR resultGroup
; // OUTPUT from "
88 #if CG_CONSTRUCT_TP_DB
89 if(publicRead
&& (dbList
!= NULL
)) {
90 /* DBs are closed on entry, open r/w */
96 quiet
)) { // publicReadOnly: this is create/write
100 /* else DBs are already open and stay that way */
104 * Pick a random spot to break the cert chain - half the time use the
105 * whole chain, half the time break it.
107 certsToUse
= genRand(1, numCerts
* 2);
108 if(certsToUse
> numCerts
) {
109 /* use the whole chain */
110 certsToUse
= numCerts
;
113 printf(" ...numCerts %d certsToUse %d\n", numCerts
, certsToUse
);
116 if(tpMakeRandCertGroup(clHand
,
117 #if CG_CONSTRUCT_TP_DB
125 CSSM_TRUE
, // firstCertIsSubject
129 printf("\nError in tpMakeRandCertGroup\n");
133 if(certGroupFrag
.NumCerts
> certsToUse
) {
134 printf("Error NOMAD sterlize\n");
138 #if CG_CONSTRUCT_TP_DB
140 /* close existing DBs and open again read-only */
146 printf(" ...closing DBs\n");
148 for(i
=0; i
<dbList
->NumHandles
; i
++) {
149 crtn
= CSSM_DL_DbClose(dbList
->DLDBHandle
[i
]);
151 printError("CSSM_DL_DbClose");
158 printf(" ...opening DBs read-only\n");
164 CSSM_TRUE
, // publicReadOnly: this is read only
172 * Okay, some of the certs we were given are in the DB, some are in
173 * random places in certGroupFrag, some are nowhere (if certsToUse is
174 * less than numCerts). Have the TP construct us an ordered verified
177 crtn
= CSSM_TP_CertGroupConstruct(
182 NULL
, // ConstructParams
186 printError("CSSM_TP_CertGroupConstruct", crtn
);
190 /* vfy resultGroup is identical to unbroken part of chain */
192 printf(" ...CSSM_TP_CertGroupConstruct returned %u certs\n",
193 (unsigned)resultGroup
->NumCerts
);
195 if(resultGroup
->NumCerts
!= certsToUse
) {
196 printf("\n***cgConstruct: resultGroup->NumCerts was %u, expected %u\n",
197 (unsigned)resultGroup
->NumCerts
, (unsigned)certsToUse
);
201 for(certDex
=0; certDex
<certsToUse
; certDex
++) {
202 if(!appCompareCssmData(&certs
[certDex
],
203 &resultGroup
->GroupList
.CertList
[certDex
])) {
204 printf("\ncgConstruct: ***certs[%d] miscompare\n", certDex
);
211 tpFreeCertGroup(&certGroupFrag
,
212 CSSM_FALSE
, // caller malloc'd the actual certs
213 CSSM_FALSE
); // struct is on stack
214 tpFreeCertGroup(resultGroup
,
215 CSSM_TRUE
, // mallocd by TP
217 #if CG_CONSTRUCT_TP_DB
223 printf(" ...deleting all certs from DBs\n");
225 for(i
=0; i
<dbList
->NumHandles
; i
++) {
226 clDeleteAllCerts(dbList
->DLDBHandle
[i
]);
230 printf(" ...closing DBs\n");
232 for(i
=0; i
<dbList
->NumHandles
; i
++) {
233 crtn
= CSSM_DL_DbClose(dbList
->DLDBHandle
[i
]);
235 printError("CSSM_DL_DbClose");
246 /*** end of code directly copied from ../cgConstruct/cgConstruct.cpp ***/
249 * key pairs - created in cgConstructInit, stored in testParams->perThread
252 CSSM_KEY_PTR pubKeys
;
253 CSSM_KEY_PTR privKeys
;
255 char *notBeforeStr
; // to use thread-safe tpGenCerts()
256 char *notAfterStr
; // to use thread-safe tpGenCerts()
259 int cgConstructInit(TestParams
*testParams
)
261 unsigned numKeys
= NUM_CERTS_MIN
+ testParams
->threadNum
;
262 TT_KeyPairs
*keyPairs
;
264 if(testParams
->verbose
) {
265 printf("cgConstruct thread %d: generating keys...\n",
266 testParams
->threadNum
);
268 keyPairs
= (TT_KeyPairs
*)CSSM_MALLOC(sizeof(TT_KeyPairs
));
269 keyPairs
->numKeys
= numKeys
;
270 keyPairs
->pubKeys
= (CSSM_KEY_PTR
)CSSM_CALLOC(numKeys
, sizeof(CSSM_KEY
));
271 keyPairs
->privKeys
= (CSSM_KEY_PTR
)CSSM_CALLOC(numKeys
, sizeof(CSSM_KEY
));
272 CSSM_DL_DB_HANDLE nullDb
= {0, 0};
273 if(tpGenKeys(testParams
->cspHand
,
278 "cgConstruct", // keyLabelBase
280 keyPairs
->privKeys
)) {
283 keyPairs
->notBeforeStr
= genTimeAtNowPlus(0);
284 keyPairs
->notAfterStr
= genTimeAtNowPlus(SECONDS_TO_LIVE
);
286 testParams
->perThread
= keyPairs
;
290 printf("Error generating keys; aborting\n");
291 CSSM_FREE(keyPairs
->pubKeys
);
292 CSSM_FREE(keyPairs
->privKeys
);
297 int cgConstruct(TestParams
*testParams
)
300 int status
= -1; // exit status, default = error
301 TT_KeyPairs
*keyPairs
= (TT_KeyPairs
*)testParams
->perThread
;
304 /* all three of these are arrays with numCert elements */
305 CSSM_KEY_PTR pubKeys
= keyPairs
->pubKeys
;
306 CSSM_KEY_PTR privKeys
= keyPairs
->privKeys
;
307 CSSM_DATA_PTR certs
= NULL
;
309 unsigned numCerts
= keyPairs
->numKeys
;
310 uint32 sigAlg
= SIG_ALG_DEF
;
311 CSSM_DL_DB_LIST dbList
= {0, NULL
}; /* for storing certs */
312 CSSM_DL_DB_LIST_PTR dbListPtr
; /* pts to dbList or NULL */
313 CSSM_BOOL publicRead
= CSSM_FALSE
;
314 CSSM_BOOL allInDbs
= CSSM_FALSE
;
315 CSSM_BOOL skipFirstDb
= CSSM_FALSE
;
317 /* malloc empty certs */
318 certs
= (CSSM_DATA_PTR
)CSSM_CALLOC(numCerts
, sizeof(CSSM_DATA
));
320 printf("not enough memory for %u certs.\n", numCerts
);
323 memset(certs
, 0, numCerts
* sizeof(CSSM_DATA
));
325 dbList
.NumHandles
= 0;
326 dbList
.DLDBHandle
= NULL
;
328 for(loopNum
=0; loopNum
<testParams
->numLoops
; loopNum
++) {
331 if(testParams
->verbose
) {
332 printf("cgConstruct thread %d: generating certs...\n",
333 testParams
->threadNum
);
335 else if(!testParams
->quiet
) {
336 printChar(testParams
->progressChar
);
338 if(tpGenCerts(testParams
->cspHand
,
342 "cgConstruct", // nameBase
346 keyPairs
->notBeforeStr
,
347 keyPairs
->notAfterStr
)) {
352 status
= doTest(testParams
->tpHand
,
367 for(dex
=0; dex
<numCerts
; dex
++) {
368 CSSM_FREE(certs
[dex
].Data
);
370 memset(certs
, 0, numCerts
* sizeof(CSSM_DATA
));
374 printf("Hit CR to proceed: ");
380 for(dex
=0; dex
<numCerts
; dex
++) {
381 if(certs
[dex
].Data
) {
382 CSSM_FREE(certs
[dex
].Data
);
385 CSSM_FREE(keyPairs
->pubKeys
);
386 CSSM_FREE(keyPairs
->privKeys
);