2 * Copyright (c) 2018 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 #import <Foundation/Foundation.h>
27 #include <Security/SecTrustSettingsPriv.h>
28 #include <Security/SecCertificatePriv.h>
29 #include <utilities/fileIo.h>
30 #include <utilities/SecCFWrappers.h>
32 #include "SecurityCommands.h"
34 NSString* toolAppID = @"com.apple.security";
36 static int addCertFile(const char *fileName, NSMutableArray *array) {
37 SecCertificateRef certRef = NULL;
39 unsigned char *buf = NULL;
43 if (readFileSizet(fileName, &buf, &numBytes)) {
48 data = [NSData dataWithBytes:buf length:numBytes];
49 certRef = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)data);
51 certRef = SecCertificateCreateWithPEM(NULL, (__bridge CFDataRef)data);
58 [array addObject:(__bridge id)certRef];
63 CFReleaseNull(certRef);
67 static int returnCFError(CFErrorRef CF_CONSUMED error) {
68 CFStringRef errorString = CFErrorCopyDescription(error);
69 CFStringPerformWithCString(errorString, ^(const char *utf8Str) {
70 fprintf(stderr, "Failed to copy CT exceptions: %s\n", utf8Str);
72 CFIndex errCode = CFErrorGetCode(error);
77 static int resetExceptions(bool resetCerts, bool resetDomains) {
79 CFErrorRef error = NULL;
81 NSDictionary *resetCertsDict = @{ (__bridge NSString*)kSecCTExceptionsCAsKey: @[] };
82 result = SecTrustStoreSetCTExceptions(NULL, (__bridge CFDictionaryRef)resetCertsDict, &error);
84 if (!error && resetDomains) {
85 NSDictionary *resetDomainsDict = @{ (__bridge NSString*)kSecCTExceptionsDomainsKey : @[] };
86 result = SecTrustStoreSetCTExceptions(NULL, (__bridge CFDictionaryRef)resetDomainsDict, &error);
89 return returnCFError(error);
94 static int addExceptions(CFStringRef key, NSArray *newExceptions) {
95 CFErrorRef error = NULL;
96 NSDictionary *currentExceptions = CFBridgingRelease(SecTrustStoreCopyCTExceptions((__bridge CFStringRef)toolAppID, &error));
97 if (!currentExceptions && error) {
98 return returnCFError(error);
101 NSMutableArray *exceptionsForKey = nil;
102 if (currentExceptions && currentExceptions[(__bridge NSString*)key]) {
103 exceptionsForKey = [currentExceptions[(__bridge NSString*)key] mutableCopy];
104 [exceptionsForKey addObjectsFromArray:newExceptions];
106 exceptionsForKey = [newExceptions copy];
109 NSDictionary *newExceptionsDict = @{ (__bridge NSString*)key: exceptionsForKey };
110 bool result = SecTrustStoreSetCTExceptions(NULL, (__bridge CFDictionaryRef)newExceptionsDict, &error);
112 return returnCFError(error);
118 int add_ct_exceptions(int argc, char * const *argv) {
121 bool resetDomains = false;
122 bool resetCerts = false;
124 NSMutableArray *domains = [NSMutableArray array];
125 NSMutableArray *certs = [NSMutableArray array];
126 NSDictionary *plist = nil;
130 return SHOW_USAGE_MESSAGE;
133 while ((arg = getopt(argc, argv, "d:c:r:p:")) != -1) {
136 NSString *domain = [NSString stringWithCString:optarg encoding:NSUTF8StringEncoding];
137 [domains addObject:domain];
141 if (addCertFile(optarg, certs)) {
142 fprintf(stderr, "Failed to read cert file\n");
147 if (!strcmp(optarg, "all")) {
150 } else if (!strcmp(optarg, "domain")) {
152 } else if (!strcmp(optarg, "cert")) {
155 return SHOW_USAGE_MESSAGE;
159 plist = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithCString:optarg encoding:NSUTF8StringEncoding]];
163 return SHOW_USAGE_MESSAGE;
167 /* handle reset operation */
168 if (resetCerts || resetDomains) {
169 return resetExceptions(resetCerts, resetDomains);
174 CFErrorRef error = NULL;
175 bool result = SecTrustStoreSetCTExceptions(NULL, (__bridge CFDictionaryRef)plist, &error);
177 return returnCFError(error);
185 if ([domains count]) {
186 status = addExceptions(kSecCTExceptionsDomainsKey, domains);
189 fprintf(stderr, "failed to add domain exceptions\n");
195 NSMutableArray<NSDictionary *>*valuesForCAsKey = [NSMutableArray arrayWithCapacity:[certs count]];
196 [certs enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
197 SecCertificateRef cert = (__bridge SecCertificateRef)obj;
198 NSData* hash = CFBridgingRelease(SecCertificateCopySubjectPublicKeyInfoSHA256Digest(cert));
199 NSDictionary *value = @{ (__bridge NSString*)kSecCTExceptionsHashAlgorithmKey:@"sha256",
200 (__bridge NSString*)kSecCTExceptionsSPKIHashKey:hash };
201 [valuesForCAsKey addObject:value];
203 status = addExceptions(kSecCTExceptionsCAsKey, valuesForCAsKey);
206 fprintf(stderr, "failed to add cert exceptions\n");
213 static int printExceptions(CFStringRef key, NSDictionary *allExceptions) {
214 if (!allExceptions || !allExceptions[(__bridge NSString*)key] ||
215 [allExceptions[(__bridge NSString*)key] count] == 0) {
216 CFStringPerformWithCString(key, ^(const char *utf8Str) {
217 fprintf(stdout, "No CT Exceptions for %s\n", utf8Str);
222 NSArray *exceptionsForKey = allExceptions[(__bridge NSString*)key];
223 NSMutableString *exceptionsString = [NSMutableString stringWithFormat:@"\t%@ : [",key];
224 [exceptionsForKey enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
225 if ([obj isKindOfClass:[NSString class]]) {
227 [exceptionsString appendFormat:@"\"%@\"",obj];
229 [exceptionsString appendFormat:@", \"%@\"", obj];
231 } else if ([obj isKindOfClass:[NSDictionary class]]) {
233 [exceptionsString appendString:@"\n\t "];
235 [exceptionsString appendString:@"\t "];
237 [exceptionsString appendFormat:@"\"%@:", obj[(__bridge NSString*)kSecCTExceptionsHashAlgorithmKey]];
238 NSString *hashHex = CFBridgingRelease(CFDataCopyHexString((__bridge CFDataRef)obj[(__bridge NSString*)kSecCTExceptionsSPKIHashKey]));
239 [exceptionsString appendString:hashHex];
240 if ([exceptionsForKey count] == idx + 1) { // last entry
241 [exceptionsString appendString:@"\"\n"];
243 [exceptionsString appendString:@"\",\n"];
247 [exceptionsString appendString:@"]\n"];
248 CFStringPerformWithCString((__bridge CFStringRef)exceptionsString, ^(const char *utf8Str) {
249 fprintf(stdout, "\n%s\n", utf8Str);
255 int show_ct_exceptions(int argc, char * const *argv) {
257 bool allExceptions = false;
258 NSString *identifier = nil;
259 bool domainExceptions = false;
260 bool certExceptions = false;
263 while ((arg = getopt(argc, argv, "ai:dc")) != -1) {
266 allExceptions = true;
269 identifier = [NSString stringWithCString:optarg encoding:NSUTF8StringEncoding];
272 domainExceptions = true;
275 certExceptions = true;
279 return SHOW_USAGE_MESSAGE;
283 if (!domainExceptions && !certExceptions) {
284 /* Nothing specified, show both */
285 domainExceptions = true;
286 certExceptions = true;
291 fprintf(stdout, "Showing exceptions for all apps\n");
292 } else if (!identifier) {
293 identifier = toolAppID;
297 CFStringPerformWithCString((__bridge CFStringRef)identifier, ^(const char *utf8Str) {
298 fprintf(stdout, "Showing exceptions for %s\n", utf8Str);
302 CFErrorRef error = NULL;
303 NSDictionary *results = CFBridgingRelease(SecTrustStoreCopyCTExceptions((__bridge CFStringRef)identifier, &error));
305 /* Copy failed, return error */
306 if (!results && error) {
307 return returnCFError(error);
310 /* print domain exceptions */
312 if (domainExceptions) {
313 status = printExceptions(kSecCTExceptionsDomainsKey, results);
316 fprintf(stderr, "failed to print domain exceptions\n");
320 /* print cert exceptions */
321 if (certExceptions) {
322 status = printExceptions(kSecCTExceptionsCAsKey, results);
325 fprintf(stderr, "failed to print cert exceptions\n");