2 * Copyright (c) 2003-2010,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 "keychain_delete.h"
27 #include "keychain_find.h"
29 #include "keychain_utilities.h"
32 #include <Security/SecKeychain.h>
33 #include <Security/SecKeychainItem.h>
34 #include <Security/SecTrustSettings.h>
37 do_delete(CFTypeRef keychainOrArray
)
39 /* @@@ SecKeychainDelete should really take a CFTypeRef argument. */
40 OSStatus result
= SecKeychainDelete((SecKeychainRef
)keychainOrArray
);
43 /* @@@ Add printing of keychainOrArray. */
44 sec_perror("SecKeychainDelete", result
);
51 do_delete_certificate(CFTypeRef keychainOrArray
, const char *name
, const char *hash
, Boolean deleteTrust
)
53 OSStatus result
= noErr
;
54 SecKeychainItemRef itemToDelete
= NULL
;
59 itemToDelete
= find_unique_certificate(keychainOrArray
, name
, hash
);
62 result
= SecTrustSettingsRemoveTrustSettings((SecCertificateRef
)itemToDelete
,
63 kSecTrustSettingsDomainUser
);
64 if (result
&& result
!= errSecItemNotFound
) {
65 sec_perror("SecTrustSettingsRemoveTrustSettings (user)", result
);
68 result
= SecTrustSettingsRemoveTrustSettings((SecCertificateRef
)itemToDelete
,
69 kSecTrustSettingsDomainAdmin
);
70 if (result
&& result
!= errSecItemNotFound
) {
71 sec_perror("SecTrustSettingsRemoveTrustSettings (admin)", result
);
75 result
= SecKeychainItemDelete(itemToDelete
);
77 sec_perror("SecKeychainItemDelete", result
);
82 fprintf(stderr
, "Unable to delete certificate matching \"%s\"",
83 (name
) ? name
: (hash
) ? hash
: "");
87 safe_CFRelease(&itemToDelete
);
93 keychain_delete_certificate(int argc
, char * const *argv
)
95 CFTypeRef keychainOrArray
= NULL
;
98 Boolean delete_trust
= FALSE
;
101 while ((ch
= getopt(argc
, argv
, "hc:Z:t")) != -1)
116 result
= 2; /* @@@ Return 2 triggers usage message. */
124 keychainOrArray
= keychain_create_array(argc
, argv
);
126 result
= do_delete_certificate(keychainOrArray
, name
, hash
, delete_trust
);
129 safe_CFRelease(&keychainOrArray
);
135 keychain_delete(int argc
, char * const *argv
)
137 CFTypeRef keychainOrArray
= NULL
;
140 while ((ch
= getopt(argc
, argv
, "h")) != -1)
146 return 2; /* @@@ Return 2 triggers usage message. */
153 keychainOrArray
= keychain_create_array(argc
, argv
);
155 result
= do_delete(keychainOrArray
);
157 CFRelease(keychainOrArray
);