2 * Copyright (c) 2003-2017 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 "keychain_delete.h"
27 #include "keychain_find.h"
29 #include "keychain_utilities.h"
30 #include "security_tool.h"
32 #include <Security/SecIdentity.h>
33 #include <Security/SecKeychain.h>
34 #include <Security/SecKeychainItem.h>
35 #include <Security/SecTrustSettings.h>
38 do_delete(CFTypeRef keychainOrArray
)
40 /* @@@ SecKeychainDelete should really take a CFTypeRef argument. */
41 OSStatus result
= SecKeychainDelete((SecKeychainRef
)keychainOrArray
);
44 /* @@@ Add printing of keychainOrArray. */
45 sec_perror("SecKeychainDelete", result
);
52 do_delete_certificate(CFTypeRef keychainOrArray
, const char *name
, const char *hash
,
53 Boolean deleteTrust
, Boolean deleteIdentity
)
55 OSStatus result
= noErr
;
56 SecKeychainItemRef itemToDelete
= NULL
;
58 return SHOW_USAGE_MESSAGE
;
61 itemToDelete
= find_unique_certificate(keychainOrArray
, name
, hash
);
63 OSStatus status
= noErr
;
65 status
= SecTrustSettingsRemoveTrustSettings((SecCertificateRef
)itemToDelete
,
66 kSecTrustSettingsDomainUser
);
68 // if trust settings do not exist, it's not an error.
69 if (status
!= errSecItemNotFound
) {
71 sec_perror("SecTrustSettingsRemoveTrustSettings (user)", result
);
75 status
= SecTrustSettingsRemoveTrustSettings((SecCertificateRef
)itemToDelete
,
76 kSecTrustSettingsDomainAdmin
);
78 if (status
!= errSecItemNotFound
) {
80 sec_perror("SecTrustSettingsRemoveTrustSettings (admin)", result
);
85 if (!result
&& deleteIdentity
) {
86 SecIdentityRef identity
= NULL
;
87 status
= SecIdentityCreateWithCertificate(keychainOrArray
,
88 (SecCertificateRef
)itemToDelete
,
91 // if the private key doesn't exist, and we succeed in deleting
92 // the certificate, overall result will still be good.
93 if (status
== errSecItemNotFound
) {
99 SecKeyRef keyToDelete
= NULL
;
100 status
= SecIdentityCopyPrivateKey(identity
, &keyToDelete
);
104 result
= SecKeychainItemDelete((SecKeychainItemRef
)keyToDelete
);
106 sec_perror("SecKeychainItemDelete", result
);
109 safe_CFRelease(&keyToDelete
);
111 safe_CFRelease(&identity
);
114 fprintf(stderr
, "Unable to obtain private key reference for \"%s\" (error %d)",
115 (name
) ? name
: (hash
) ? hash
: "", (int) status
);
119 result
= SecKeychainItemDelete(itemToDelete
);
121 sec_perror("SecKeychainItemDelete", result
);
127 fprintf(stderr
, "Unable to delete certificate matching \"%s\"",
128 (name
) ? name
: (hash
) ? hash
: "");
132 safe_CFRelease(&itemToDelete
);
138 keychain_delete_cert_common(int argc
, char * const *argv
, Boolean delete_identity
)
140 CFTypeRef keychainOrArray
= NULL
;
143 Boolean delete_trust
= FALSE
;
146 while ((ch
= getopt(argc
, argv
, "hc:Z:t")) != -1)
161 result
= 2; /* @@@ Return 2 triggers usage message. */
169 keychainOrArray
= keychain_create_array(argc
, argv
);
171 result
= do_delete_certificate(keychainOrArray
, name
, hash
, delete_trust
, delete_identity
);
174 safe_CFRelease(&keychainOrArray
);
180 keychain_delete_certificate(int argc
, char * const *argv
)
182 return keychain_delete_cert_common(argc
, argv
, FALSE
);
186 keychain_delete_identity(int argc
, char * const *argv
)
188 return keychain_delete_cert_common(argc
, argv
, TRUE
);
192 keychain_delete(int argc
, char * const *argv
)
194 CFTypeRef keychainOrArray
= NULL
;
197 while ((ch
= getopt(argc
, argv
, "h")) != -1)
203 return SHOW_USAGE_MESSAGE
;
210 keychainOrArray
= keychain_create_array(argc
, argv
);
212 result
= do_delete(keychainOrArray
);
214 CFRelease(keychainOrArray
);