2 * cmsTime.cpp - measure performance of CMS decode & verify
9 #include <CoreFoundation/CoreFoundation.h>
10 #include <Security/Security.h>
11 #include <Security/CMSDecoder.h>
12 #include <security_cdsa_utils/cuFileIo.h>
13 #include <utilLib/common.h>
16 #define SIGNED_FILE "noRoot.p7"
18 static void usage(char **argv
)
20 printf("usage: %s [options]\n", argv
[0]);
22 printf(" -l loops -- loops; default %d; 0=forever\n", LOOPS_DEF
);
23 printf(" -i inFile -- input file; default is %s\n", SIGNED_FILE
);
24 printf(" -K -- set empty KC list\n");
29 /* perform one CMS decode */
30 static OSStatus
doDecode(
33 SecPolicyRef policyRef
,
34 CFArrayRef kcArray
) /* optional */
38 CMSDecoderRef cmsDecoder
= NULL
;
40 CMSDecoderCreate(&cmsDecoder
);
42 ortn
= CMSDecoderSetSearchKeychain(cmsDecoder
, kcArray
);
44 cssmPerror("CMSDecoderSetSearchKeychain", ortn
);
48 ortn
= CMSDecoderUpdateMessage(cmsDecoder
, cmsData
, cmsDataLen
);
50 cssmPerror("CMSDecoderUpdateMessage", ortn
);
53 ortn
= CMSDecoderFinalizeMessage(cmsDecoder
);
55 cssmPerror("CMSDecoderFinalizeMessage", ortn
);
59 CMSSignerStatus signerStatus
;
60 ortn
= CMSDecoderCopySignerStatus(cmsDecoder
, 0, policyRef
, true, &signerStatus
, NULL
, NULL
);
62 cssmPerror("CMSDecoderCopySignerStatus", ortn
);
65 if(signerStatus
!= kCMSSignerValid
) {
66 printf("***Bad signerStatus (%d)\n", (int)signerStatus
);
69 CFRelease(cmsDecoder
);
73 int main(int argc
, char **argv
)
77 CFArrayRef emptyKCList
= NULL
;
78 unsigned char *blob
= NULL
;
80 SecPolicyRef policyRef
= NULL
;
82 /* user-spec'd variables */
83 unsigned loops
= LOOPS_DEF
;
84 char *blobFile
= SIGNED_FILE
;
85 bool emptyList
= false; /* specify empty KC list */
89 while ((arg
= getopt(argc
, argv
, "l:i:Kh")) != -1) {
99 emptyKCList
= CFArrayCreate(NULL
, NULL
, 0, &kCFTypeArrayCallBacks
);
109 if(readFile(blobFile
, &blob
, &blobLen
)) {
110 printf("***Error reading %s\n", blobFile
);
113 /* cook up reusable policy object */
114 SecPolicySearchRef policySearch
= NULL
;
115 OSStatus ortn
= SecPolicySearchCreate(CSSM_CERT_X_509v3
,
116 &CSSMOID_APPLE_X509_BASIC
,
120 cssmPerror("SecPolicySearchCreate", ortn
);
123 ortn
= SecPolicySearchCopyNext(policySearch
, &policyRef
);
125 cssmPerror("SecPolicySearchCopyNext", ortn
);
128 CFRelease(policySearch
);
130 CFAbsoluteTime startTimeFirst
;
131 CFAbsoluteTime endTimeFirst
;
132 CFAbsoluteTime startTimeMulti
;
133 CFAbsoluteTime endTimeMulti
;
136 startTimeFirst
= CFAbsoluteTimeGetCurrent();
137 if(doDecode(blob
, blobLen
, policyRef
, emptyKCList
)) {
140 endTimeFirst
= CFAbsoluteTimeGetCurrent();
142 startTimeMulti
= CFAbsoluteTimeGetCurrent();
143 for(dex
=0; dex
<loops
; dex
++) {
144 if(doDecode(blob
, blobLen
, policyRef
, emptyKCList
)) {
148 endTimeMulti
= CFAbsoluteTimeGetCurrent();
149 CFTimeInterval elapsed
= endTimeMulti
- startTimeMulti
;
151 printf("First decode = %4.1f ms\n", (endTimeFirst
- startTimeFirst
) * 1000.0);
152 printf("Next decodes = %4.2f ms/op (%f s total for %u loops)\n",
153 elapsed
* 1000.0 / loops
, elapsed
, loops
);