]> git.saurik.com Git - apple/security.git/blob - SecurityTool/trust_settings_impexp.c
Security-58286.260.20.tar.gz
[apple/security.git] / SecurityTool / trust_settings_impexp.c
1 /*
2 * Copyright (c) 2006,2012,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 *
23 * trust_cert_add.c
24 */
25
26 #include "trust_settings_impexp.h"
27 #include "security_tool.h"
28 #include <Security/Security.h>
29 #include <Security/SecTrustSettings.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <security_cdsa_utils/cuFileIo.h>
33 #include <CoreFoundation/CoreFoundation.h>
34 #include <utilities/fileIo.h>
35
36 extern int trust_settings_export(int argc, char * const *argv)
37 {
38 extern char *optarg;
39 extern int optind;
40 OSStatus ortn;
41 int arg;
42 CFDataRef settings = NULL;
43 SecTrustSettingsDomain domain = kSecTrustSettingsDomainUser;
44 int rtn;
45 char *settingsFile = NULL;
46 unsigned len;
47
48 if(argc < 2) {
49 return SHOW_USAGE_MESSAGE;
50 }
51
52 optind = 1;
53 while ((arg = getopt(argc, argv, "dsh")) != -1) {
54 switch (arg) {
55 case 'd':
56 domain = kSecTrustSettingsDomainAdmin;
57 break;
58 case 's':
59 domain = kSecTrustSettingsDomainSystem;
60 break;
61 default:
62 return SHOW_USAGE_MESSAGE;
63 }
64 }
65 if(optind != (argc - 1)) {
66 /* no args left for settings file */
67 return SHOW_USAGE_MESSAGE;
68 }
69 settingsFile = argv[optind];
70
71 ortn = SecTrustSettingsCreateExternalRepresentation(domain, &settings);
72 if(ortn) {
73 cssmPerror("SecTrustSettingsCreateExternalRepresentation", ortn);
74 return 1;
75 }
76 len = (unsigned) CFDataGetLength(settings);
77 rtn = writeFile(settingsFile, CFDataGetBytePtr(settings), len);
78 if(rtn) {
79 fprintf(stderr, "Error (%d) writing %s.\n", rtn, settingsFile);
80 }
81 else if(!do_quiet) {
82 fprintf(stdout, "...Trust Settings exported successfully.\n");
83 }
84 CFRelease(settings);
85 return rtn;
86 }
87
88 extern int trust_settings_import(int argc, char * const *argv)
89 {
90 extern char *optarg;
91 extern int optind;
92 OSStatus ortn;
93 int arg;
94 char *settingsFile = NULL;
95 unsigned char *settingsData = NULL;
96 size_t settingsLen = 0;
97 CFDataRef settings = NULL;
98 SecTrustSettingsDomain domain = kSecTrustSettingsDomainUser;
99 int rtn;
100
101 if(argc < 2) {
102 return SHOW_USAGE_MESSAGE;
103 }
104
105 optind = 1;
106 while ((arg = getopt(argc, argv, "dh")) != -1) {
107 switch (arg) {
108 case 'd':
109 domain = kSecTrustSettingsDomainAdmin;
110 break;
111 default:
112 return SHOW_USAGE_MESSAGE;
113 }
114 }
115 if(optind != (argc - 1)) {
116 /* no args left for settings file */
117 return SHOW_USAGE_MESSAGE;
118 }
119 settingsFile = argv[optind];
120 rtn = readFileSizet(settingsFile, &settingsData, &settingsLen);
121 if(rtn) {
122 fprintf(stderr, "Error (%d) reading %s.\n", rtn, settingsFile);
123 return 1;
124 }
125 settings = CFDataCreate(NULL, (const UInt8 *)settingsData, settingsLen);
126 free(settingsData);
127 ortn = SecTrustSettingsImportExternalRepresentation(domain, settings);
128 CFRelease(settings);
129 if(ortn) {
130 cssmPerror("SecTrustSettingsImportExternalRepresentation", ortn);
131 rtn = 1;
132 }
133 else if(!do_quiet) {
134 fprintf(stdout, "...Trust Settings imported successfully.\n");
135 rtn = 0;
136 }
137 return rtn;
138 }
139