2 * Copyright (c) 2006,2010,2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #include <Security/SecTrust.h>
27 #include <Security/SecKeychain.h>
28 #include <Security/SecPolicy.h>
29 #include <Security/SecPolicySearch.h>
30 #include <Security/cssmapple.h>
31 #include <Security/oidsalg.h>
36 #include "trusted_cert_utils.h"
39 * Read file as a cert, add to a CFArray, creating the array if necessary
41 static int addCertFile(
43 CFMutableArrayRef
*array
)
45 SecCertificateRef certRef
;
47 if(readCertFile(fileName
, &certRef
)) {
51 *array
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
53 CFArrayAppendValue(*array
, certRef
);
59 verify_cert(int argc
, char * const *argv
)
65 CFMutableArrayRef certs
= NULL
;
66 CFMutableArrayRef roots
= NULL
;
67 CFMutableArrayRef keychains
= NULL
;
68 const CSSM_OID
*policy
= &CSSMOID_APPLE_X509_BASIC
;
69 SecKeychainRef kcRef
= NULL
;
72 SecPolicyRef policyRef
= NULL
;
73 SecTrustRef trustRef
= NULL
;
74 SecPolicySearchRef searchRef
= NULL
;
75 const char *emailAddrs
= NULL
;
76 const char *sslHost
= NULL
;
77 CSSM_APPLE_TP_SSL_OPTIONS sslOpts
;
78 CSSM_APPLE_TP_SMIME_OPTIONS smimeOpts
;
79 CSSM_APPLE_TP_ACTION_FLAGS actionFlags
= 0;
80 bool forceActionFlags
= false;
81 CSSM_APPLE_TP_ACTION_DATA actionData
;
83 CFDataRef cfActionData
= NULL
;
84 SecTrustResultType resultType
;
87 CFGregorianDate gregorianDate
;
88 CFDateRef dateRef
= NULL
;
91 return 2; /* @@@ Return 2 triggers usage message. */
93 /* permit network cert fetch unless explicitly turned off with '-L' */
94 actionFlags
|= CSSM_TP_ACTION_FETCH_CERT_FROM_NET
;
96 while ((arg
= getopt(argc
, argv
, "c:r:p:k:e:s:d:Llnq")) != -1) {
99 /* this can be specified multiple times */
100 if(addCertFile(optarg
, &certs
)) {
106 /* this can be specified multiple times */
107 if(addCertFile(optarg
, &roots
)) {
113 policy
= policyStringToOid(optarg
);
120 ortn
= SecKeychainOpen(optarg
, &kcRef
);
122 cssmPerror("SecKeychainOpen", ortn
);
126 /* this can be specified multiple times */
127 if(keychains
== NULL
) {
128 keychains
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
130 CFArrayAppendValue(keychains
, kcRef
);
134 actionFlags
&= ~CSSM_TP_ACTION_FETCH_CERT_FROM_NET
;
135 forceActionFlags
= true;
138 actionFlags
|= CSSM_TP_ACTION_LEAF_IS_CA
;
141 /* No keychains, signalled by empty keychain array */
142 if(keychains
!= NULL
) {
143 fprintf(stderr
, "-k and -n are mutually exclusive\n");
147 keychains
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
159 memset(&time
, 0, sizeof(struct tm
));
160 if (strptime(optarg
, "%Y-%m-%d-%H:%M:%S", &time
) == NULL
) {
161 if (strptime(optarg
, "%Y-%m-%d", &time
) == NULL
) {
162 fprintf(stderr
, "Date processing error\n");
168 gregorianDate
.second
= time
.tm_sec
;
169 gregorianDate
.minute
= time
.tm_min
;
170 gregorianDate
.hour
= time
.tm_hour
;
171 gregorianDate
.day
= time
.tm_mday
;
172 gregorianDate
.month
= time
.tm_mon
+ 1;
173 gregorianDate
.year
= time
.tm_year
+ 1900;
175 if (dateRef
== NULL
) {
176 dateRef
= CFDateCreate(NULL
, CFGregorianDateGetAbsoluteTime(gregorianDate
, NULL
));
191 fprintf(stderr
, "***No certs specified.\n");
195 if(CFArrayGetCount(roots
) != 1) {
196 fprintf(stderr
, "***Multiple roots and no certs not allowed.\n");
201 /* no certs and one root: verify the root */
202 certs
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
203 CFArrayAppendValue(certs
, CFArrayGetValueAtIndex(roots
, 0));
204 actionFlags
|= CSSM_TP_ACTION_LEAF_IS_CA
;
207 /* cook up a SecPolicyRef */
208 ortn
= SecPolicySearchCreate(CSSM_CERT_X_509v3
,
213 cssmPerror("SecPolicySearchCreate", ortn
);
217 ortn
= SecPolicySearchCopyNext(searchRef
, &policyRef
);
219 cssmPerror("SecPolicySearchCopyNext", ortn
);
224 /* per-policy options */
225 if(compareOids(policy
, &CSSMOID_APPLE_TP_SSL
) || compareOids(policy
, &CSSMOID_APPLE_TP_APPLEID_SHARING
)) {
226 if(sslHost
!= NULL
) {
227 memset(&sslOpts
, 0, sizeof(sslOpts
));
228 sslOpts
.Version
= CSSM_APPLE_TP_SSL_OPTS_VERSION
;
229 sslOpts
.ServerName
= sslHost
;
230 sslOpts
.ServerNameLen
= strlen(sslHost
);
231 optionData
.Data
= (uint8
*)&sslOpts
;
232 optionData
.Length
= sizeof(sslOpts
);
233 ortn
= SecPolicySetValue(policyRef
, &optionData
);
235 cssmPerror("SecPolicySetValue", ortn
);
241 if(compareOids(policy
, &CSSMOID_APPLE_TP_SMIME
)) {
242 if(emailAddrs
!= NULL
) {
243 memset(&smimeOpts
, 0, sizeof(smimeOpts
));
244 smimeOpts
.Version
= CSSM_APPLE_TP_SMIME_OPTS_VERSION
;
245 smimeOpts
.SenderEmail
= emailAddrs
;
246 smimeOpts
.SenderEmailLen
= strlen(emailAddrs
);
247 optionData
.Data
= (uint8
*)&smimeOpts
;
248 optionData
.Length
= sizeof(smimeOpts
);
249 ortn
= SecPolicySetValue(policyRef
, &optionData
);
251 cssmPerror("SecPolicySetValue", ortn
);
258 /* Now create a SecTrustRef and set its options */
259 ortn
= SecTrustCreateWithCertificates(certs
, policyRef
, &trustRef
);
261 cssmPerror("SecTrustCreateWithCertificates", ortn
);
266 /* roots (anchors) are optional */
268 ortn
= SecTrustSetAnchorCertificates(trustRef
, roots
);
270 cssmPerror("SecTrustSetAnchorCertificates", ortn
);
275 if(actionFlags
|| forceActionFlags
) {
276 memset(&actionData
, 0, sizeof(actionData
));
277 actionData
.Version
= CSSM_APPLE_TP_ACTION_VERSION
;
278 actionData
.ActionFlags
= actionFlags
;
279 cfActionData
= CFDataCreate(NULL
, (UInt8
*)&actionData
, sizeof(actionData
));
280 ortn
= SecTrustSetParameters(trustRef
, CSSM_TP_ACTION_DEFAULT
, cfActionData
);
282 cssmPerror("SecTrustSetParameters", ortn
);
288 ortn
= SecTrustSetKeychains(trustRef
, keychains
);
290 cssmPerror("SecTrustSetKeychains", ortn
);
295 if(dateRef
!= NULL
) {
296 ortn
= SecTrustSetVerifyDate(trustRef
, dateRef
);
298 cssmPerror("SecTrustSetVerifyDate", ortn
);
305 ortn
= SecTrustEvaluate(trustRef
, &resultType
);
307 /* should never fail - error on this doesn't mean the cert verified badly */
308 cssmPerror("SecTrustEvaluate", ortn
);
313 case kSecTrustResultUnspecified
:
314 /* cert chain valid, no special UserTrust assignments */
315 case kSecTrustResultProceed
:
316 /* cert chain valid AND user explicitly trusts this */
318 case kSecTrustResultDeny
:
320 fprintf(stderr
, "SecTrustEvaluate result: kSecTrustResultDeny\n");
324 case kSecTrustResultConfirm
:
326 * Cert chain may well have verified OK, but user has flagged
327 * one of these certs as untrustable.
330 fprintf(stderr
, "SecTrustEvaluate result: kSecTrustResultConfirm\n");
337 /* See what the TP had to say about this */
338 ortn
= SecTrustGetCssmResultCode(trustRef
, &ocrtn
);
340 cssmPerror("SecTrustGetCssmResultCode", ortn
);
343 cssmPerror("Cert Verify Result", ocrtn
);
349 if((ourRtn
== 0) & !quiet
) {
350 printf("...certificate verification successful.\n");
356 CFRELEASE(keychains
);
357 CFRELEASE(policyRef
);
359 CFRELEASE(searchRef
);
360 CFRELEASE(cfActionData
);