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"
37 #include "verify_cert.h"
40 * Read file as a cert, add to a CFArray, creating the array if necessary
42 static int addCertFile(
44 CFMutableArrayRef
*array
)
46 SecCertificateRef certRef
;
48 if(readCertFile(fileName
, &certRef
)) {
52 *array
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
54 CFArrayAppendValue(*array
, certRef
);
60 verify_cert(int argc
, char * const *argv
)
66 CFMutableArrayRef certs
= NULL
;
67 CFMutableArrayRef roots
= NULL
;
68 CFMutableArrayRef keychains
= NULL
;
69 const CSSM_OID
*policy
= &CSSMOID_APPLE_X509_BASIC
;
70 SecKeychainRef kcRef
= NULL
;
73 SecPolicyRef policyRef
= NULL
;
74 SecTrustRef trustRef
= NULL
;
75 SecPolicySearchRef searchRef
= NULL
;
76 const char *emailAddrs
= NULL
;
77 const char *sslHost
= NULL
;
78 CSSM_APPLE_TP_SSL_OPTIONS sslOpts
;
79 CSSM_APPLE_TP_SMIME_OPTIONS smimeOpts
;
80 CSSM_APPLE_TP_ACTION_FLAGS actionFlags
= 0;
81 bool forceActionFlags
= false;
82 CSSM_APPLE_TP_ACTION_DATA actionData
;
84 CFDataRef cfActionData
= NULL
;
85 SecTrustResultType resultType
;
88 CFGregorianDate gregorianDate
;
89 CFDateRef dateRef
= NULL
;
92 return 2; /* @@@ Return 2 triggers usage message. */
94 /* permit network cert fetch unless explicitly turned off with '-L' */
95 actionFlags
|= CSSM_TP_ACTION_FETCH_CERT_FROM_NET
;
97 while ((arg
= getopt(argc
, argv
, "c:r:p:k:e:s:d:Llnq")) != -1) {
100 /* this can be specified multiple times */
101 if(addCertFile(optarg
, &certs
)) {
107 /* this can be specified multiple times */
108 if(addCertFile(optarg
, &roots
)) {
114 policy
= policyStringToOid(optarg
);
121 ortn
= SecKeychainOpen(optarg
, &kcRef
);
123 cssmPerror("SecKeychainOpen", ortn
);
127 /* this can be specified multiple times */
128 if(keychains
== NULL
) {
129 keychains
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
131 CFArrayAppendValue(keychains
, kcRef
);
135 actionFlags
&= ~CSSM_TP_ACTION_FETCH_CERT_FROM_NET
;
136 forceActionFlags
= true;
139 actionFlags
|= CSSM_TP_ACTION_LEAF_IS_CA
;
142 /* No keychains, signalled by empty keychain array */
143 if(keychains
!= NULL
) {
144 fprintf(stderr
, "-k and -n are mutually exclusive\n");
148 keychains
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
160 memset(&time
, 0, sizeof(struct tm
));
161 if (strptime(optarg
, "%Y-%m-%d-%H:%M:%S", &time
) == NULL
) {
162 if (strptime(optarg
, "%Y-%m-%d", &time
) == NULL
) {
163 fprintf(stderr
, "Date processing error\n");
169 gregorianDate
.second
= time
.tm_sec
;
170 gregorianDate
.minute
= time
.tm_min
;
171 gregorianDate
.hour
= time
.tm_hour
;
172 gregorianDate
.day
= time
.tm_mday
;
173 gregorianDate
.month
= time
.tm_mon
+ 1;
174 gregorianDate
.year
= time
.tm_year
+ 1900;
176 if (dateRef
== NULL
) {
177 dateRef
= CFDateCreate(NULL
, CFGregorianDateGetAbsoluteTime(gregorianDate
, NULL
));
192 fprintf(stderr
, "***No certs specified.\n");
196 if(CFArrayGetCount(roots
) != 1) {
197 fprintf(stderr
, "***Multiple roots and no certs not allowed.\n");
202 /* no certs and one root: verify the root */
203 certs
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
204 CFArrayAppendValue(certs
, CFArrayGetValueAtIndex(roots
, 0));
205 actionFlags
|= CSSM_TP_ACTION_LEAF_IS_CA
;
208 /* cook up a SecPolicyRef */
209 ortn
= SecPolicySearchCreate(CSSM_CERT_X_509v3
,
214 cssmPerror("SecPolicySearchCreate", ortn
);
218 ortn
= SecPolicySearchCopyNext(searchRef
, &policyRef
);
220 cssmPerror("SecPolicySearchCopyNext", ortn
);
225 /* per-policy options */
226 if(compareOids(policy
, &CSSMOID_APPLE_TP_SSL
) || compareOids(policy
, &CSSMOID_APPLE_TP_APPLEID_SHARING
)) {
227 if(sslHost
!= NULL
) {
228 memset(&sslOpts
, 0, sizeof(sslOpts
));
229 sslOpts
.Version
= CSSM_APPLE_TP_SSL_OPTS_VERSION
;
230 sslOpts
.ServerName
= sslHost
;
231 sslOpts
.ServerNameLen
= (uint32
) strlen(sslHost
);
232 optionData
.Data
= (uint8
*)&sslOpts
;
233 optionData
.Length
= sizeof(sslOpts
);
234 ortn
= SecPolicySetValue(policyRef
, &optionData
);
236 cssmPerror("SecPolicySetValue", ortn
);
242 if(compareOids(policy
, &CSSMOID_APPLE_TP_SMIME
)) {
243 if(emailAddrs
!= NULL
) {
244 memset(&smimeOpts
, 0, sizeof(smimeOpts
));
245 smimeOpts
.Version
= CSSM_APPLE_TP_SMIME_OPTS_VERSION
;
246 smimeOpts
.SenderEmail
= emailAddrs
;
247 smimeOpts
.SenderEmailLen
= (uint32
) strlen(emailAddrs
);
248 optionData
.Data
= (uint8
*)&smimeOpts
;
249 optionData
.Length
= sizeof(smimeOpts
);
250 ortn
= SecPolicySetValue(policyRef
, &optionData
);
252 cssmPerror("SecPolicySetValue", ortn
);
259 /* Now create a SecTrustRef and set its options */
260 ortn
= SecTrustCreateWithCertificates(certs
, policyRef
, &trustRef
);
262 cssmPerror("SecTrustCreateWithCertificates", ortn
);
267 /* roots (anchors) are optional */
269 ortn
= SecTrustSetAnchorCertificates(trustRef
, roots
);
271 cssmPerror("SecTrustSetAnchorCertificates", ortn
);
276 if(actionFlags
|| forceActionFlags
) {
277 memset(&actionData
, 0, sizeof(actionData
));
278 actionData
.Version
= CSSM_APPLE_TP_ACTION_VERSION
;
279 actionData
.ActionFlags
= actionFlags
;
280 cfActionData
= CFDataCreate(NULL
, (UInt8
*)&actionData
, sizeof(actionData
));
281 ortn
= SecTrustSetParameters(trustRef
, CSSM_TP_ACTION_DEFAULT
, cfActionData
);
283 cssmPerror("SecTrustSetParameters", ortn
);
289 ortn
= SecTrustSetKeychains(trustRef
, keychains
);
291 cssmPerror("SecTrustSetKeychains", ortn
);
296 if(dateRef
!= NULL
) {
297 ortn
= SecTrustSetVerifyDate(trustRef
, dateRef
);
299 cssmPerror("SecTrustSetVerifyDate", ortn
);
306 ortn
= SecTrustEvaluate(trustRef
, &resultType
);
308 /* should never fail - error on this doesn't mean the cert verified badly */
309 cssmPerror("SecTrustEvaluate", ortn
);
314 case kSecTrustResultUnspecified
:
315 /* cert chain valid, no special UserTrust assignments */
316 case kSecTrustResultProceed
:
317 /* cert chain valid AND user explicitly trusts this */
319 case kSecTrustResultDeny
:
321 fprintf(stderr
, "SecTrustEvaluate result: kSecTrustResultDeny\n");
325 case kSecTrustResultConfirm
:
327 * Cert chain may well have verified OK, but user has flagged
328 * one of these certs as untrustable.
331 fprintf(stderr
, "SecTrustEvaluate result: kSecTrustResultConfirm\n");
338 /* See what the TP had to say about this */
339 ortn
= SecTrustGetCssmResultCode(trustRef
, &ocrtn
);
341 cssmPerror("SecTrustGetCssmResultCode", ortn
);
344 cssmPerror("Cert Verify Result", ocrtn
);
350 if((ourRtn
== 0) & !quiet
) {
351 printf("...certificate verification successful.\n");
357 CFRELEASE(keychains
);
358 CFRELEASE(policyRef
);
360 CFRELEASE(searchRef
);
361 CFRELEASE(cfActionData
);