]> git.saurik.com Git - apple/security.git/blob - OSX/shared_regressions/si-88-sectrust-valid.m
Security-58286.51.6.tar.gz
[apple/security.git] / OSX / shared_regressions / si-88-sectrust-valid.m
1 /*
2 * si-88-sectrust-valid.m
3 * Security
4 *
5 * Copyright (c) 2017-2018 Apple Inc. All Rights Reserved.
6 *
7 */
8
9 #include <CoreFoundation/CoreFoundation.h>
10 #include <Security/Security.h>
11 #include <Security/SecTrust.h>
12 #include <Security/SecPolicy.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <utilities/SecCFWrappers.h>
16
17 #include "shared_regressions.h"
18
19 static void test_valid_trust(SecCertificateRef leaf, SecCertificateRef ca, CFArrayRef anchors,
20 CFDateRef date, SecTrustResultType expected, const char *test_name)
21 {
22 CFArrayRef policies=NULL;
23 SecPolicyRef policy=NULL;
24 SecTrustRef trust=NULL;
25 SecTrustResultType trustResult;
26 CFMutableArrayRef certs=NULL;
27
28 printf("Starting %s\n", test_name);
29 isnt(certs = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks), NULL, "create cert array");
30 if (certs) {
31 if (leaf) {
32 CFArrayAppendValue(certs, leaf);
33 }
34 if (ca) {
35 CFArrayAppendValue(certs, ca);
36 }
37 }
38
39 isnt(policy = SecPolicyCreateBasicX509(), NULL, "create policy");
40 isnt(policies = CFArrayCreate(kCFAllocatorDefault, (const void **)&policy, 1, &kCFTypeArrayCallBacks), NULL, "create policies");
41 ok_status(SecTrustCreateWithCertificates(certs, policies, &trust), "create trust");
42
43 assert(trust); // silence analyzer
44 ok_status(SecTrustSetAnchorCertificates(trust, anchors), "set anchors");
45 ok_status(SecTrustSetVerifyDate(trust, date), "set date");
46 ok_status(SecTrustEvaluate(trust, &trustResult), "evaluate trust");
47 ok(trustResult == expected, "trustResult %d expected (got %d)",
48 (int)expected, (int)trustResult);
49
50 CFReleaseSafe(certs);
51 CFReleaseSafe(policy);
52 CFReleaseSafe(policies);
53 CFReleaseSafe(trust);
54 }
55
56 #import <Foundation/Foundation.h>
57 SecCertificateRef SecCertificateCreateWithPEM(CFAllocatorRef allocator, CFDataRef pem_certificate);
58
59 static SecCertificateRef SecCertificateCreateFromResource(NSString *name)
60 {
61 NSString *resources = @"si-88-sectrust-valid-data";
62 NSString *extension = @"pem";
63
64 NSURL *url = [[NSBundle mainBundle] URLForResource:name withExtension:extension subdirectory:resources];
65 if (!url) {
66 printf("No URL for resource \"%s.pem\"\n", [name UTF8String]);
67 return NULL;
68 }
69
70 NSData *certData = [NSData dataWithContentsOfURL:url];
71 if (!certData) {
72 printf("No cert data for resource \"%s.pem\"\n", [name UTF8String]);
73 return NULL;
74 }
75
76 return SecCertificateCreateWithPEM(kCFAllocatorDefault, (__bridge CFDataRef)certData);
77 }
78
79 static void tests()
80 {
81 SecCertificateRef ca_na=NULL, ca_nb=NULL, root=NULL;
82 SecCertificateRef leaf_na_ok1=NULL, leaf_na_ok2=NULL;
83 SecCertificateRef leaf_nb_ok1=NULL, leaf_nb_ok2=NULL, leaf_nb_revoked1=NULL;
84
85 isnt(ca_na = SecCertificateCreateFromResource(@"ca-na"), NULL, "create ca-na cert");
86 isnt(ca_nb = SecCertificateCreateFromResource(@"ca-nb"), NULL, "create ca-nb cert");
87 isnt(root = SecCertificateCreateFromResource(@"root"), NULL, "create root cert");
88 isnt(leaf_na_ok1 = SecCertificateCreateFromResource(@"leaf-na-ok1"), NULL, "create leaf-na-ok1 cert");
89 isnt(leaf_na_ok2 = SecCertificateCreateFromResource(@"leaf-na-ok2"), NULL, "create leaf-na-ok2 cert");
90 isnt(leaf_nb_ok1 = SecCertificateCreateFromResource(@"leaf-nb-ok1"), NULL, "create leaf-nb-ok1 cert");
91 isnt(leaf_nb_ok2 = SecCertificateCreateFromResource(@"leaf-nb-ok2"), NULL, "create leaf-nb-ok2 cert");
92 isnt(leaf_nb_revoked1 = SecCertificateCreateFromResource(@"leaf-nb-revoked1"), NULL, "create leaf-nb-revoked1 cert");
93
94 CFMutableArrayRef anchors=NULL;
95 isnt(anchors = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks), NULL, "create anchors array");
96 if (anchors && root) {
97 CFArrayAppendValue(anchors, root);
98 }
99 CFCalendarRef cal = NULL;
100 CFAbsoluteTime at;
101 CFDateRef date_20180102 = NULL; // a date when our test certs would all be valid, in the absence of Valid db info
102
103 isnt(cal = CFCalendarCreateWithIdentifier(kCFAllocatorDefault, kCFGregorianCalendar), NULL, "create calendar");
104 ok(CFCalendarComposeAbsoluteTime(cal, &at, "yMd", 2018, 1, 2), "create verify absolute time 20180102");
105 isnt(date_20180102 = CFDateCreate(kCFAllocatorDefault, at), NULL, "create verify date 20180102");
106
107 /* Case 1: leaf_na_ok1 (not revoked) */
108 /* -- was OK: cert issued 2017-10-20, before the CA not-after date of 2017-10-21 */
109 /* -- now BAD: since a not-after date now requires CT and the test cert has no SCT, this is fatal. */
110 test_valid_trust(leaf_na_ok1, ca_na, anchors, date_20180102, kSecTrustResultFatalTrustFailure, "leaf_na_ok1 test");
111
112 /* Case 2: leaf_na_ok2 (revoked) */
113 /* -- BAD: cert issued 2017-10-26, after the CA not-after date of 2017-10-21 */
114 test_valid_trust(leaf_na_ok2, ca_na, anchors, date_20180102, kSecTrustResultFatalTrustFailure, "leaf_na_ok2 test");
115
116 /* Case 3: leaf_nb_ok1 (revoked) */
117 /* -- BAD: cert issued 2017-10-20, before the CA not-before date of 2017-10-22 */
118 test_valid_trust(leaf_nb_ok1, ca_nb, anchors, date_20180102, kSecTrustResultFatalTrustFailure, "leaf_nb_ok1 test");
119
120 /* Case 4: leaf_nb_ok2 (not revoked) */
121 /* -- OK: cert issued 2017-10-26, after the CA not-before date of 2017-10-22 */
122 test_valid_trust(leaf_nb_ok2, ca_nb, anchors, date_20180102, kSecTrustResultUnspecified, "leaf_nb_ok2 test");
123
124 /* Case 5: leaf_nb_revoked1 (revoked) */
125 /* -- BAD: cert issued 2017-10-20, before the CA not-before date of 2017-10-22 */
126 test_valid_trust(leaf_nb_revoked1, ca_nb, anchors, date_20180102, kSecTrustResultFatalTrustFailure, "leaf_nb_revoked1 test");
127
128 CFReleaseSafe(ca_na);
129 CFReleaseSafe(ca_nb);
130 CFReleaseSafe(root);
131 CFReleaseSafe(leaf_na_ok1);
132 CFReleaseSafe(leaf_na_ok2);
133 CFReleaseSafe(leaf_nb_ok1);
134 CFReleaseSafe(leaf_nb_ok2);
135 CFReleaseSafe(leaf_nb_revoked1);
136 CFReleaseSafe(anchors);
137 CFReleaseSafe(cal);
138 CFReleaseSafe(date_20180102);
139 }
140
141
142 int si_88_sectrust_valid(int argc, char *const *argv)
143 {
144 plan_tests(52);
145
146 tests();
147
148 return 0;
149 }