]>
git.saurik.com Git - apple/security.git/blob - SecurityTool/sharedTool/keychain_backup.c
2 * Copyright (c) 2013-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 <TargetConditionals.h>
27 #if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
29 #include "SecurityCommands.h"
31 #include <AssertMacros.h>
32 #include <Security/SecItemPriv.h>
34 #include <utilities/SecCFWrappers.h>
36 #include "SecurityTool/sharedTool/readline.h"
37 #include "SecurityTool/sharedTool/tool_errors.h"
41 do_keychain_import(const char *backupPath
, const char *keybagPath
, const char *passwordString
)
43 CFDataRef backup
=NULL
;
44 CFDataRef keybag
=NULL
;
45 CFDataRef password
=NULL
;
49 require(password
= CFDataCreate(NULL
, (UInt8
*)passwordString
, strlen(passwordString
)), out
);
51 require(keybag
=copyFileContents(keybagPath
), out
);
52 require(backup
=copyFileContents(backupPath
), out
);
54 ok
=_SecKeychainRestoreBackup(backup
, keybag
, password
);
57 CFReleaseSafe(backup
);
58 CFReleaseSafe(keybag
);
59 CFReleaseSafe(password
);
65 do_keychain_export(const char *backupPath
, const char *keybagPath
, const char *passwordString
)
67 CFDataRef backup
=NULL
;
68 CFDataRef keybag
=NULL
;
69 CFDataRef password
=NULL
;
73 require(password
= CFDataCreate(NULL
, (UInt8
*)passwordString
, strlen(passwordString
)), out
);
75 require(keybag
=copyFileContents(keybagPath
), out
);
76 require(backup
=_SecKeychainCopyBackup(keybag
, password
), out
);
78 ok
=writeFileContents(backupPath
, backup
);
81 CFReleaseSafe(backup
);
82 CFReleaseSafe(keybag
);
83 CFReleaseSafe(password
);
90 keychain_import(int argc
, char * const *argv
)
94 const char *keybag
=NULL
;
95 const char *password
=NULL
;
97 while ((ch
= getopt(argc
, argv
, "vk:p:")) != -1)
111 return SHOW_USAGE_MESSAGE
;
119 sec_error("-k is required\n");
120 return SHOW_USAGE_MESSAGE
;
124 sec_error("<backup> is required\n");
125 return SHOW_USAGE_MESSAGE
;
128 return do_keychain_import(argv
[0], keybag
, password
);
132 keychain_export(int argc
, char * const *argv
)
136 const char *keybag
=NULL
;
137 const char *password
=NULL
;
139 while ((ch
= getopt(argc
, argv
, "vk:p:")) != -1)
153 return SHOW_USAGE_MESSAGE
;
161 sec_error("-k is required\n");
162 return SHOW_USAGE_MESSAGE
;
166 sec_error("<backup> is required\n");
167 return SHOW_USAGE_MESSAGE
;
170 return do_keychain_export(argv
[0], keybag
, password
);
173 #endif /* TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR */