]> git.saurik.com Git - apple/security.git/blob - SecurityTests/clxutils/threadTest/secTrustEval.cpp
Security-57031.10.10.tar.gz
[apple/security.git] / SecurityTests / clxutils / threadTest / secTrustEval.cpp
1 /*
2 * secTrustEval.cpp
3 *
4 * doSet up SecTrust object, do a SecTrustEvaluate, release.
5 */
6 #include "testParams.h"
7 #include <Security/cssm.h>
8 #include <utilLib/common.h>
9 #include <utilLib/cspwrap.h>
10 #include <clAppUtils/clutils.h>
11 #include <clAppUtils/tpUtils.h>
12 #include <security_cdsa_utils/cuFileIo.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <time.h>
16 #include <string.h>
17 #include <Security/Security.h>
18
19 #define HOLD_SEARCH_LIST 0
20
21 /* for malloc debug */
22 #define DO_PAUSE 0
23
24 //static const char *CERT_FILE = "amazon_v3.100.cer";
25 static const char *CERT_FILE = "cduniverse_v3.100.cer";
26
27 /* common data, our known good cert, shared by all threads */
28 static unsigned char *certData = NULL;
29 static unsigned certLength = 0;
30
31 /* read in our known good cert file, just once */
32 int secTrustEvalInit(TestParams *testParams)
33 {
34 if(certData != NULL) {
35 return 0;
36 }
37 if(testParams->verbose) {
38 printf("secTrusEval thread %d: reading cert file %s...\n",
39 testParams->threadNum, CERT_FILE);
40 }
41 if(readFile(CERT_FILE, &certData, &certLength)) {
42 printf("Error reading %s; aborting\n", CERT_FILE);
43 printf("***This test must be run from the clxutils/threadTest directory.\n");
44 return 1;
45 }
46 return 0;
47 }
48
49
50 int secTrustEval(TestParams *testParams)
51 {
52 unsigned loopNum;
53 SecCertificateRef certRef;
54 const CSSM_DATA cdata = {certLength, (uint8 *)certData};
55
56 OSStatus ortn = SecCertificateCreateFromData(&cdata,
57 CSSM_CERT_X_509v3,
58 CSSM_CERT_ENCODING_DER,
59 &certRef);
60 if(ortn) {
61 cssmPerror("SecCertificateCreateFromData", ortn);
62 return (int)ortn;
63 }
64
65 #if HOLD_SEARCH_LIST
66 CFArrayRef sl;
67 ortn = SecKeychainCopySearchList(&sl);
68 if(ortn) {
69 cssmPerror("SecPolicySearchCreate", ortn);
70 return (int)ortn;
71 }
72 #endif
73
74 for(loopNum=0; loopNum<testParams->numLoops; loopNum++) {
75 if(testParams->verbose) {
76 printf("secTrustEval loop %d\n", loopNum);
77 }
78 else if(!testParams->quiet) {
79 printChar(testParams->progressChar);
80 }
81
82 /* from here on emulate exactly what SecureTransport does */
83 CFMutableArrayRef certs;
84 certs = CFArrayCreateMutable(NULL, 1, &kCFTypeArrayCallBacks);
85 CFArrayInsertValueAtIndex(certs, 0, certRef);
86
87 SecPolicyRef policy = NULL;
88 SecPolicySearchRef policySearch = NULL;
89
90 OSStatus ortn = SecPolicySearchCreate(CSSM_CERT_X_509v3,
91 &CSSMOID_APPLE_TP_SSL,
92 NULL, // policy opts
93 &policySearch);
94 if(ortn) {
95 cssmPerror("SecPolicySearchCreate", ortn);
96 return (int)ortn;
97 }
98
99 ortn = SecPolicySearchCopyNext(policySearch, &policy);
100 if(ortn) {
101 cssmPerror("SecPolicySearchCopyNext", ortn);
102 return (int)ortn;
103 }
104 CFRelease(policySearch);
105
106 SecTrustRef secTrust;
107 ortn = SecTrustCreateWithCertificates(certs, policy, &secTrust);
108 if(ortn) {
109 cssmPerror("SecTrustCreateWithCertificates", ortn);
110 return (int)ortn;
111 }
112 /* no action data for now */
113
114 SecTrustResultType secTrustResult;
115 ortn = SecTrustEvaluate(secTrust, &secTrustResult);
116 if(ortn) {
117 cssmPerror("SecTrustEvaluate", ortn);
118 return (int)ortn;
119 }
120
121 CFRelease(certs);
122 CFRelease(secTrust);
123 CFRelease(policy);
124
125 #if DO_PAUSE
126 fpurge(stdin);
127 printf("Hit CR to continue: ");
128 getchar();
129 #endif
130 } /* outer loop */
131 #if HOLD_SEARCH_LIST
132 CFRelease(sl);
133 #endif
134 return 0;
135 }