2 * si-88-sectrust-valid.m
5 * Copyright (c) 2017-2018 Apple Inc. All Rights Reserved.
9 #include <CoreFoundation/CoreFoundation.h>
10 #include <Security/Security.h>
11 #include <Security/SecTrust.h>
12 #include <Security/SecPolicy.h>
15 #include <utilities/SecCFWrappers.h>
17 #include "shared_regressions.h"
19 static void test_valid_trust(SecCertificateRef leaf, SecCertificateRef ca, CFArrayRef anchors,
20 CFDateRef date, CFIndex policyID, SecTrustResultType expected, const char *test_name)
22 CFArrayRef policies=NULL;
23 SecPolicyRef policy=NULL;
24 SecTrustRef trust=NULL;
25 SecTrustResultType trustResult;
26 CFMutableArrayRef certs=NULL;
28 printf("Starting %s\n", test_name);
29 isnt(certs = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks), NULL, "create cert array");
32 CFArrayAppendValue(certs, leaf);
35 CFArrayAppendValue(certs, ca);
40 isnt(policy = SecPolicyCreateSSL(true, NULL), NULL, "create ssl policy");
42 isnt(policy = SecPolicyCreateBasicX509(), NULL, "create basic policy");
44 isnt(policies = CFArrayCreate(kCFAllocatorDefault, (const void **)&policy, 1, &kCFTypeArrayCallBacks), NULL, "create policies");
45 ok_status(SecTrustCreateWithCertificates(certs, policies, &trust), "create trust");
47 assert(trust); // silence analyzer
48 ok_status(SecTrustSetAnchorCertificates(trust, anchors), "set anchors");
49 ok_status(SecTrustSetVerifyDate(trust, date), "set date");
50 ok_status(SecTrustEvaluate(trust, &trustResult), "evaluate trust");
51 ok(trustResult == expected, "trustResult %d expected (got %d)",
52 (int)expected, (int)trustResult);
55 CFReleaseSafe(policy);
56 CFReleaseSafe(policies);
60 #import <Foundation/Foundation.h>
61 SecCertificateRef SecCertificateCreateWithPEM(CFAllocatorRef allocator, CFDataRef pem_certificate);
63 static SecCertificateRef SecCertificateCreateFromResource(NSString *name)
65 NSString *resources = @"si-88-sectrust-valid-data";
66 NSString *extension = @"pem";
68 NSURL *url = [[NSBundle mainBundle] URLForResource:name withExtension:extension subdirectory:resources];
70 printf("No URL for resource \"%s.pem\"\n", [name UTF8String]);
74 NSData *certData = [NSData dataWithContentsOfURL:url];
76 printf("No cert data for resource \"%s.pem\"\n", [name UTF8String]);
80 return SecCertificateCreateWithPEM(kCFAllocatorDefault, (__bridge CFDataRef)certData);
85 SecCertificateRef ca_na=NULL, ca_nb=NULL, root=NULL;
86 SecCertificateRef leaf_na_ok1=NULL, leaf_na_ok2=NULL;
87 SecCertificateRef leaf_nb_ok1=NULL, leaf_nb_ok2=NULL, leaf_nb_revoked1=NULL;
89 isnt(ca_na = SecCertificateCreateFromResource(@"ca-na"), NULL, "create ca-na cert");
90 isnt(ca_nb = SecCertificateCreateFromResource(@"ca-nb"), NULL, "create ca-nb cert");
91 isnt(root = SecCertificateCreateFromResource(@"root"), NULL, "create root cert");
92 isnt(leaf_na_ok1 = SecCertificateCreateFromResource(@"leaf-na-ok1"), NULL, "create leaf-na-ok1 cert");
93 isnt(leaf_na_ok2 = SecCertificateCreateFromResource(@"leaf-na-ok2"), NULL, "create leaf-na-ok2 cert");
94 isnt(leaf_nb_ok1 = SecCertificateCreateFromResource(@"leaf-nb-ok1"), NULL, "create leaf-nb-ok1 cert");
95 isnt(leaf_nb_ok2 = SecCertificateCreateFromResource(@"leaf-nb-ok2"), NULL, "create leaf-nb-ok2 cert");
96 isnt(leaf_nb_revoked1 = SecCertificateCreateFromResource(@"leaf-nb-revoked1"), NULL, "create leaf-nb-revoked1 cert");
98 CFMutableArrayRef anchors=NULL;
99 isnt(anchors = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks), NULL, "create anchors array");
100 if (anchors && root) {
101 CFArrayAppendValue(anchors, root);
103 CFCalendarRef cal = NULL;
105 CFDateRef date_20180102 = NULL; // a date when our test certs would all be valid, in the absence of Valid db info
107 isnt(cal = CFCalendarCreateWithIdentifier(kCFAllocatorDefault, kCFGregorianCalendar), NULL, "create calendar");
108 ok(CFCalendarComposeAbsoluteTime(cal, &at, "yMd", 2018, 1, 2), "create verify absolute time 20180102");
109 isnt(date_20180102 = CFDateCreate(kCFAllocatorDefault, at), NULL, "create verify date 20180102");
111 /* Case 0: leaf_na_ok1 (not revoked) */
112 /* -- OK: cert issued 2017-10-20, before the CA not-after date of 2017-10-21 */
113 /* test cert has no SCT, but is expected to be OK since we now only apply the CT restriction for SSL. */
114 test_valid_trust(leaf_na_ok1, ca_na, anchors, date_20180102, 0, kSecTrustResultUnspecified, "leaf_na_ok1 basic test");
116 /* Case 1: leaf_na_ok1 (not revoked) */
117 /* -- BAD: since a not-after date now requires CT (for SSL) and the test cert has no SCT, this is fatal. */
118 test_valid_trust(leaf_na_ok1, ca_na, anchors, date_20180102, 1, kSecTrustResultFatalTrustFailure, "leaf_na_ok1 ssl test");
120 /* Case 2: leaf_na_ok2 (revoked) */
121 /* -- BAD: cert issued 2017-10-26, after the CA not-after date of 2017-10-21 */
122 test_valid_trust(leaf_na_ok2, ca_na, anchors, date_20180102, 0, kSecTrustResultFatalTrustFailure, "leaf_na_ok2 basic test");
124 /* Case 3: leaf_nb_ok1 (revoked) */
125 /* -- BAD: cert issued 2017-10-20, before the CA not-before date of 2017-10-22 */
126 test_valid_trust(leaf_nb_ok1, ca_nb, anchors, date_20180102, 0, kSecTrustResultFatalTrustFailure, "leaf_nb_ok1 basic test");
128 /* Case 4: leaf_nb_ok2 (not revoked) */
129 /* -- OK: cert issued 2017-10-26, after the CA not-before date of 2017-10-22 */
130 test_valid_trust(leaf_nb_ok2, ca_nb, anchors, date_20180102, 0, kSecTrustResultUnspecified, "leaf_nb_ok2 basic test");
132 /* Case 5: leaf_nb_revoked1 (revoked) */
133 /* -- BAD: cert issued 2017-10-20, before the CA not-before date of 2017-10-22 */
134 test_valid_trust(leaf_nb_revoked1, ca_nb, anchors, date_20180102, 0, kSecTrustResultFatalTrustFailure, "leaf_nb_revoked1 basic test");
136 CFReleaseSafe(ca_na);
137 CFReleaseSafe(ca_nb);
139 CFReleaseSafe(leaf_na_ok1);
140 CFReleaseSafe(leaf_na_ok2);
141 CFReleaseSafe(leaf_nb_ok1);
142 CFReleaseSafe(leaf_nb_ok2);
143 CFReleaseSafe(leaf_nb_revoked1);
144 CFReleaseSafe(anchors);
146 CFReleaseSafe(date_20180102);
150 int si_88_sectrust_valid(int argc, char *const *argv)
152 plan_tests(12+(6*8));