2 * certsFromDb.cpp - extract all certs from a DB, write to files or parse to stdout.
4 #include <security_cdsa_utils/cuFileIo.h>
5 #include <utilLib/common.h>
6 #include <utilLib/cspwrap.h>
7 #include <security_cdsa_utils/cuPrintCert.h>
11 #include <Security/cssm.h>
14 static void usage(char **argv
)
17 printf(" %s keychainFile f certFileBase [option...]\n", argv
[0]);
18 printf(" %s keychainFile p(arse) [option...]\n", argv
[0]);
20 printf(" R fetch CRLs, not certs\n");
21 printf(" P pause for MallocDebug one each item\n");
26 int main(int argc
, char **argv
)
29 CSSM_DL_DB_HANDLE dlDbHand
;
34 CSSM_DB_UNIQUE_RECORD_PTR record
= NULL
;
35 CSSM_HANDLE resultHand
;
36 CSSM_DATA theData
= {0, NULL
};
37 char *fileBase
= NULL
;
38 CSSM_BOOL doPause
= CSSM_FALSE
;
39 CSSM_BOOL isCrl
= CSSM_FALSE
;
40 CSSM_BOOL quiet
= CSSM_FALSE
;
55 /* default, parse mode */
60 for(int arg
=optarg
; arg
<argc
; arg
++) {
61 switch(argv
[arg
][0]) {
76 /* attach to specified keychain as a DL/DB */
77 dlDbHand
.DLHandle
= dlStartup();
78 if(dlDbHand
.DLHandle
== 0) {
81 crtn
= dbCreateOpen(dlDbHand
.DLHandle
, argv
[1],
82 CSSM_FALSE
, // doCreate
83 CSSM_FALSE
, // deleteExist
91 /* search by record type, no predicates, no returned attributes. We just want
93 query
.RecordType
= isCrl
? CSSM_DL_DB_RECORD_X509_CRL
:
94 CSSM_DL_DB_RECORD_X509_CERTIFICATE
;
95 query
.Conjunctive
= CSSM_DB_NONE
;
96 query
.NumSelectionPredicates
= 0;
97 query
.SelectionPredicate
= NULL
;
98 query
.QueryLimits
.TimeLimit
= 0; // FIXME - meaningful?
99 query
.QueryLimits
.SizeLimit
= 1; // FIXME - meaningful?
100 query
.QueryFlags
= CSSM_QUERY_RETURN_DATA
; // FIXME - used?
102 crtn
= CSSM_DL_DataGetFirst(dlDbHand
,
109 printError("CSSM_DL_DataGetFirst", crtn
);
110 printf("Error fetching certs from %s. Aborting.\n", argv
[1]);
113 CSSM_DL_FreeUniqueRecord(dlDbHand
, record
);
117 printf("set up MallocDebug, then any key to continue: ");
122 sprintf(filePath
, "%s_%d", fileBase
, certNum
);
123 rtn
= writeFile(filePath
, theData
.Data
, theData
.Length
);
126 printf("...wrote %u bytes to %s\n", (unsigned)theData
.Length
,
131 printf("***Error writing %s: %s\n", filePath
, strerror(rtn
));
138 printCrl(theData
.Data
, theData
.Length
, CSSM_FALSE
);
142 printCert(theData
.Data
, theData
.Length
, CSSM_FALSE
);
145 CSSM_FREE(theData
.Data
);
150 crtn
= CSSM_DL_DataGetNext(dlDbHand
,
158 sprintf(filePath
, "%s_%d", fileBase
, certNum
);
159 rtn
= writeFile(filePath
, theData
.Data
, theData
.Length
);
162 printf("...wrote %u bytes to %s\n", (unsigned)theData
.Length
,
167 printf("***Error writing %s: %s\n", filePath
, strerror(rtn
));
174 printCrl(theData
.Data
, theData
.Length
, CSSM_FALSE
);
177 printf("Cert %u:\n", certNum
);
178 printCert(theData
.Data
, theData
.Length
, CSSM_FALSE
);
182 CSSM_FREE(theData
.Data
);
183 CSSM_DL_FreeUniqueRecord(dlDbHand
, record
);
184 break; // and go again
185 case CSSMERR_DL_ENDOFDATA
:
186 /* normal termination */
189 printError("DataGetNext", crtn
);
192 if(crtn
!= CSSM_OK
) {
196 CSSM_DL_DataAbortQuery(dlDbHand
, resultHand
);
199 printf("End of loop, l to loop, enything else to end: ");
206 printf("...%d %s extracted.\n", certNum
, isCrl
? "CRLs" : "certs");