2 * userTrustTest.cpp - simple test of SecTrustSetUserTrustLegacy() and
3 * SecTrustGetUserTrust()
10 #include <Security/Security.h>
11 #include <Security/SecTrustPriv.h>
12 #include <Security/SecPolicyPriv.h>
13 #include <clAppUtils/tpUtils.h>
14 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
16 #define IGNORE_EXISTING_STATE 0
18 static void usage(char **argv
)
20 printf("usage: %s [options] known_good_leaf_cert [ca_cert...]\n", argv
[0]);
22 printf(" -q -- quiet\n");
26 static char *secTrustResultStr(
27 SecTrustResultType result
)
29 static char unknownStr
[100];
32 case kSecTrustResultInvalid
: return "kSecTrustResultInvalid";
33 case kSecTrustResultProceed
: return "kSecTrustResultProceed";
34 case kSecTrustResultConfirm
: return "kSecTrustResultConfirm";
35 case kSecTrustResultDeny
: return "kSecTrustResultDeny";
36 case kSecTrustResultUnspecified
: return "kSecTrustResultUnspecified";
37 case kSecTrustResultRecoverableTrustFailure
:
38 return "kSecTrustResultRecoverableTrustFailure";
39 case kSecTrustResultFatalTrustFailure
: return "kSecTrustResultFatalTrustFailure";
40 case kSecTrustResultOtherError
: return "kSecTrustResultOtherError";
42 sprintf(unknownStr
, "UNKNOWN ResultType (%d)\n",
48 /* do a SecTrustEvaluate, ensure resultType is as specified */
52 SecTrustResultType expectedResult
,
56 SecTrustRef trustRef
= NULL
;
57 SecTrustResultType result
;
60 ortn
= SecTrustCreateWithCertificates(certs
, policy
, &trustRef
);
62 cssmPerror("SecTrustCreateWithCertificates", ortn
);
65 ortn
= SecTrustEvaluate(trustRef
, &result
);
67 /* shouldn't fail no matter what resultType we expect */
68 cssmPerror("SecTrustEvaluate", ortn
);
72 if(expectedResult
== result
) {
74 printf("...got %s as expected\n", secTrustResultStr(result
));
78 printf("***Expected %s, got %s\n", secTrustResultStr(expectedResult
),
79 secTrustResultStr(result
));
87 /* Do a SecTrustGetUserTrust(), ensure result is as specified */
88 static int doGetUserTrust(
89 SecCertificateRef certRef
,
91 SecTrustResultType expectedResult
)
93 SecTrustResultType foundResult
;
94 OSStatus ortn
= SecTrustGetUserTrust(certRef
, policy
, &foundResult
);
96 cssmPerror("SecTrustGetUserTrust", ortn
);
99 if(foundResult
!= expectedResult
) {
100 printf("***Expected current resultType %s; found %s\n",
101 secTrustResultStr(expectedResult
), secTrustResultStr(foundResult
));
107 /* Do SecTrustSetUserTrustLegacy() followed by SecTrustGetUserTrust() */
108 static int doSetVerifyUserTrust(
109 SecCertificateRef certRef
,
111 SecTrustResultType result
)
114 ortn
= SecTrustSetUserTrustLegacy(certRef
, policy
, result
);
116 cssmPerror("SecTrustSetUserTrustLegacy", ortn
);
119 return doGetUserTrust(certRef
, policy
, result
);
123 CFArrayRef certArray
,
128 SecCertificateRef leafCert
= (SecCertificateRef
)CFArrayGetValueAtIndex(
132 printf("Verifying cert is good as is...\n");
134 ourRtn
= doEval(certArray
, policy
, kSecTrustResultUnspecified
, quiet
);
135 if(ourRtn
&& !IGNORE_EXISTING_STATE
) {
140 printf("Verifying cert currently has kSecTrustResultUnspecified...\n");
142 if(doGetUserTrust(leafCert
, policy
, kSecTrustResultUnspecified
)) {
148 printf("setting and verifying SecTrustResultDeny...\n");
150 if(doSetVerifyUserTrust(leafCert
, policy
, kSecTrustResultDeny
)) {
155 printf("Verify cert with SecTrustResultDeny...\n");
157 ourRtn
= doEval(certArray
, policy
, kSecTrustResultDeny
, quiet
);
163 printf("setting and verifying kSecTrustResultConfirm...\n");
165 if(doSetVerifyUserTrust(leafCert
, policy
, kSecTrustResultConfirm
)) {
170 printf("Verify cert with kSecTrustResultConfirm...\n");
172 ourRtn
= doEval(certArray
, policy
, kSecTrustResultConfirm
, quiet
);
178 printf("setting and verifying kSecTrustResultUnspecified...\n");
180 if(doSetVerifyUserTrust(leafCert
, policy
, kSecTrustResultUnspecified
)) {
185 printf("Verify cert with kSecTrustResultUnspecified...\n");
187 ourRtn
= doEval(certArray
, policy
, kSecTrustResultUnspecified
, quiet
);
190 printf("Verify SecTrustSetUserTrust(kSecTrustResultConfirm) fails...\n");
192 /* verify Radar 4642125 - this should fail, not crash */
193 OSStatus ortn
= SecTrustSetUserTrust(leafCert
, policy
, kSecTrustResultConfirm
);
194 if(ortn
!= unimpErr
) {
195 printf("***SecTrustSetUserTrust returned %ld; expected %ld (unimpErr)\n",
196 (long)ortn
, (long)unimpErr
);
202 int main(int argc
, char **argv
)
207 while ((arg
= getopt(argc
, argv
, "qh")) != -1) {
217 unsigned numCerts
= argc
- optind
;
221 CFMutableArrayRef certArray
= CFArrayCreateMutable(NULL
, 0,
222 &kCFTypeArrayCallBacks
);
223 for(int dex
=optind
; dex
<argc
; dex
++) {
224 SecCertificateRef certRef
= certFromFile(argv
[dex
]);
225 if(certRef
== NULL
) {
228 CFArrayAppendValue(certArray
, certRef
);
233 SecPolicyRef policyRef
= NULL
;
234 ortn
= SecPolicyCopy(CSSM_CERT_X_509v3
, &CSSMOID_APPLE_TP_SSL
, &policyRef
);
236 cssmPerror("SecPolicyCopy", ortn
);
240 int ourRtn
= doTest(certArray
, policyRef
, quiet
);
241 CFRelease(policyRef
);
242 CFRelease(certArray
);