2 * Copyright (c) 2006,2012,2014 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 "trust_settings_impexp.h"
27 #include "security_tool.h"
28 #include <Security/Security.h>
29 #include <Security/SecTrustSettings.h>
32 #include <security_cdsa_utils/cuFileIo.h>
33 #include <CoreFoundation/CoreFoundation.h>
34 #include <utilities/fileIo.h>
36 extern int trust_settings_export(int argc
, char * const *argv
)
42 CFDataRef settings
= NULL
;
43 SecTrustSettingsDomain domain
= kSecTrustSettingsDomainUser
;
45 char *settingsFile
= NULL
;
49 return SHOW_USAGE_MESSAGE
;
53 while ((arg
= getopt(argc
, argv
, "dsh")) != -1) {
56 domain
= kSecTrustSettingsDomainAdmin
;
59 domain
= kSecTrustSettingsDomainSystem
;
62 return SHOW_USAGE_MESSAGE
;
65 if(optind
!= (argc
- 1)) {
66 /* no args left for settings file */
67 return SHOW_USAGE_MESSAGE
;
69 settingsFile
= argv
[optind
];
71 ortn
= SecTrustSettingsCreateExternalRepresentation(domain
, &settings
);
73 cssmPerror("SecTrustSettingsCreateExternalRepresentation", ortn
);
76 len
= (unsigned) CFDataGetLength(settings
);
77 rtn
= writeFile(settingsFile
, CFDataGetBytePtr(settings
), len
);
79 fprintf(stderr
, "Error (%d) writing %s.\n", rtn
, settingsFile
);
82 fprintf(stdout
, "...Trust Settings exported successfully.\n");
88 extern int trust_settings_import(int argc
, char * const *argv
)
94 char *settingsFile
= NULL
;
95 unsigned char *settingsData
= NULL
;
96 size_t settingsLen
= 0;
97 CFDataRef settings
= NULL
;
98 SecTrustSettingsDomain domain
= kSecTrustSettingsDomainUser
;
102 return SHOW_USAGE_MESSAGE
;
106 while ((arg
= getopt(argc
, argv
, "dh")) != -1) {
109 domain
= kSecTrustSettingsDomainAdmin
;
112 return SHOW_USAGE_MESSAGE
;
115 if(optind
!= (argc
- 1)) {
116 /* no args left for settings file */
117 return SHOW_USAGE_MESSAGE
;
119 settingsFile
= argv
[optind
];
120 rtn
= readFileSizet(settingsFile
, &settingsData
, &settingsLen
);
122 fprintf(stderr
, "Error (%d) reading %s.\n", rtn
, settingsFile
);
125 settings
= CFDataCreate(NULL
, (const UInt8
*)settingsData
, settingsLen
);
127 ortn
= SecTrustSettingsImportExternalRepresentation(domain
, settings
);
130 cssmPerror("SecTrustSettingsImportExternalRepresentation", ortn
);
134 fprintf(stdout
, "...Trust Settings imported successfully.\n");