2 * Copyright (c) 2006,2010,2012,2014-2017 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"
38 #include <utilities/SecCFRelease.h>
41 * Read file as a cert, add to a CFArray, creating the array if necessary
43 static int addCertFile(
45 CFMutableArrayRef
*array
)
47 SecCertificateRef certRef
;
49 if(readCertFile(fileName
, &certRef
)) {
53 *array
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
55 CFArrayAppendValue(*array
, certRef
);
61 verify_cert(int argc
, char * const *argv
)
67 CFMutableArrayRef certs
= NULL
;
68 CFMutableArrayRef roots
= NULL
;
69 CFMutableArrayRef keychains
= NULL
;
70 CFMutableArrayRef policies
= NULL
;
71 const CSSM_OID
*policy
= &CSSMOID_APPLE_X509_BASIC
;
72 SecKeychainRef kcRef
= NULL
;
76 SecPolicyRef policyRef
= NULL
;
77 SecPolicyRef revPolicyRef
= NULL
;
78 SecTrustRef trustRef
= NULL
;
79 SecPolicySearchRef searchRef
= NULL
;
80 const char *emailAddrs
= NULL
;
81 const char *sslHost
= NULL
;
82 const char *name
= NULL
;
83 CSSM_APPLE_TP_SSL_OPTIONS sslOpts
;
84 CSSM_APPLE_TP_SMIME_OPTIONS smimeOpts
;
85 CSSM_APPLE_TP_ACTION_FLAGS actionFlags
= 0;
86 bool forceActionFlags
= false;
87 CSSM_APPLE_TP_ACTION_DATA actionData
;
89 CFDataRef cfActionData
= NULL
;
90 SecTrustResultType resultType
;
93 CFGregorianDate gregorianDate
;
94 CFDateRef dateRef
= NULL
;
95 CFOptionFlags revOptions
= 0;
98 return 2; /* @@@ Return 2 triggers usage message. */
100 /* permit network cert fetch unless explicitly turned off with '-L' */
101 actionFlags
|= CSSM_TP_ACTION_FETCH_CERT_FROM_NET
;
103 while ((arg
= getopt(argc
, argv
, "Cc:r:p:k:e:s:d:LlNnqR:")) != -1) {
109 /* this can be specified multiple times */
110 if(addCertFile(optarg
, &certs
)) {
116 /* this can be specified multiple times */
117 if(addCertFile(optarg
, &roots
)) {
123 policy
= policyStringToOid(optarg
);
130 ortn
= SecKeychainOpen(optarg
, &kcRef
);
132 cssmPerror("SecKeychainOpen", ortn
);
136 /* this can be specified multiple times */
137 if(keychains
== NULL
) {
138 keychains
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
140 CFArrayAppendValue(keychains
, kcRef
);
144 actionFlags
&= ~CSSM_TP_ACTION_FETCH_CERT_FROM_NET
;
145 forceActionFlags
= true;
148 actionFlags
|= CSSM_TP_ACTION_LEAF_IS_CA
;
151 /* Legacy macOS used 'n' as the "no keychain search list" flag.
152 iOS interprets it as the name option, with one argument.
154 char *o
= argv
[optind
];
155 if (o
&& o
[0] != '-') {
160 } /* intentional fall-through to "no keychains" case, if no arg */
162 /* No keychains, signalled by empty keychain array */
163 if(keychains
!= NULL
) {
164 fprintf(stderr
, "-k and -%c are mutually exclusive\n", arg
);
168 keychains
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
180 memset(&time
, 0, sizeof(struct tm
));
181 if (strptime(optarg
, "%Y-%m-%d-%H:%M:%S", &time
) == NULL
) {
182 if (strptime(optarg
, "%Y-%m-%d", &time
) == NULL
) {
183 fprintf(stderr
, "Date processing error\n");
188 gregorianDate
.second
= time
.tm_sec
;
189 gregorianDate
.minute
= time
.tm_min
;
190 gregorianDate
.hour
= time
.tm_hour
;
191 gregorianDate
.day
= time
.tm_mday
;
192 gregorianDate
.month
= time
.tm_mon
+ 1;
193 gregorianDate
.year
= time
.tm_year
+ 1900;
195 if (dateRef
== NULL
) {
196 dateRef
= CFDateCreate(NULL
, CFGregorianDateGetAbsoluteTime(gregorianDate
, NULL
));
200 revOptions
|= revCheckOptionStringToFlags(optarg
);
214 fprintf(stderr
, "***No certs specified.\n");
218 if(CFArrayGetCount(roots
) != 1) {
219 fprintf(stderr
, "***Multiple roots and no certs not allowed.\n");
224 /* no certs and one root: verify the root */
225 certs
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
226 CFArrayAppendValue(certs
, CFArrayGetValueAtIndex(roots
, 0));
227 actionFlags
|= CSSM_TP_ACTION_LEAF_IS_CA
;
230 /* cook up a SecPolicyRef */
231 ortn
= SecPolicySearchCreate(CSSM_CERT_X_509v3
,
236 cssmPerror("SecPolicySearchCreate", ortn
);
240 ortn
= SecPolicySearchCopyNext(searchRef
, &policyRef
);
242 cssmPerror("SecPolicySearchCopyNext", ortn
);
247 /* per-policy options */
248 if(compareOids(policy
, &CSSMOID_APPLE_TP_SSL
) || compareOids(policy
, &CSSMOID_APPLE_TP_APPLEID_SHARING
)) {
249 const char *nameStr
= (name
) ? name
: ((sslHost
) ? sslHost
: NULL
);
251 memset(&sslOpts
, 0, sizeof(sslOpts
));
252 sslOpts
.Version
= CSSM_APPLE_TP_SSL_OPTS_VERSION
;
253 sslOpts
.ServerName
= nameStr
;
254 sslOpts
.ServerNameLen
= (uint32
) strlen(nameStr
);
255 sslOpts
.Flags
= (client
) ? CSSM_APPLE_TP_SSL_CLIENT
: 0;
256 optionData
.Data
= (uint8
*)&sslOpts
;
257 optionData
.Length
= sizeof(sslOpts
);
258 ortn
= SecPolicySetValue(policyRef
, &optionData
);
260 cssmPerror("SecPolicySetValue", ortn
);
266 if(compareOids(policy
, &CSSMOID_APPLE_TP_SMIME
)) {
267 const char *nameStr
= (name
) ? name
: ((emailAddrs
) ? emailAddrs
: NULL
);
269 memset(&smimeOpts
, 0, sizeof(smimeOpts
));
270 smimeOpts
.Version
= CSSM_APPLE_TP_SMIME_OPTS_VERSION
;
271 smimeOpts
.SenderEmail
= nameStr
;
272 smimeOpts
.SenderEmailLen
= (uint32
) strlen(nameStr
);
273 optionData
.Data
= (uint8
*)&smimeOpts
;
274 optionData
.Length
= sizeof(smimeOpts
);
275 ortn
= SecPolicySetValue(policyRef
, &optionData
);
277 cssmPerror("SecPolicySetValue", ortn
);
284 /* create policies array */
285 policies
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
286 CFArrayAppendValue(policies
, policyRef
);
287 /* add optional SecPolicyRef for revocation, if specified */
288 if(revOptions
!= 0) {
289 revPolicyRef
= SecPolicyCreateRevocation(revOptions
);
290 CFArrayAppendValue(policies
, revPolicyRef
);
293 /* create trust reference from certs and policies */
294 ortn
= SecTrustCreateWithCertificates(certs
, policies
, &trustRef
);
296 cssmPerror("SecTrustCreateWithCertificates", ortn
);
301 /* roots (anchors) are optional */
303 ortn
= SecTrustSetAnchorCertificates(trustRef
, roots
);
305 cssmPerror("SecTrustSetAnchorCertificates", ortn
);
310 if(actionFlags
|| forceActionFlags
) {
311 memset(&actionData
, 0, sizeof(actionData
));
312 actionData
.Version
= CSSM_APPLE_TP_ACTION_VERSION
;
313 actionData
.ActionFlags
= actionFlags
;
314 cfActionData
= CFDataCreate(NULL
, (UInt8
*)&actionData
, sizeof(actionData
));
315 ortn
= SecTrustSetParameters(trustRef
, CSSM_TP_ACTION_DEFAULT
, cfActionData
);
317 cssmPerror("SecTrustSetParameters", ortn
);
323 ortn
= SecTrustSetKeychains(trustRef
, keychains
);
325 cssmPerror("SecTrustSetKeychains", ortn
);
330 if(dateRef
!= NULL
) {
331 ortn
= SecTrustSetVerifyDate(trustRef
, dateRef
);
333 cssmPerror("SecTrustSetVerifyDate", ortn
);
340 ortn
= SecTrustEvaluate(trustRef
, &resultType
);
342 /* should never fail - error on this doesn't mean the cert verified badly */
343 cssmPerror("SecTrustEvaluate", ortn
);
348 case kSecTrustResultUnspecified
:
349 /* cert chain valid, no special UserTrust assignments */
350 case kSecTrustResultProceed
:
351 /* cert chain valid AND user explicitly trusts this */
353 case kSecTrustResultDeny
:
355 fprintf(stderr
, "SecTrustEvaluate result: kSecTrustResultDeny\n");
362 /* See what the TP had to say about this */
363 ortn
= SecTrustGetCssmResultCode(trustRef
, &ocrtn
);
365 cssmPerror("SecTrustGetCssmResultCode", ortn
);
368 cssmPerror("Cert Verify Result", ocrtn
);
374 if((ourRtn
== 0) & !quiet
) {
375 printf("...certificate verification successful.\n");
378 CFReleaseNull(dateRef
);
382 CFRELEASE(keychains
);
384 CFRELEASE(revPolicyRef
);
385 CFRELEASE(policyRef
);
387 CFRELEASE(searchRef
);
388 CFRELEASE(cfActionData
);