Skip to content

Commit f981ed2

Browse files
Add function to uninstall certificate from the system keychain
1 parent b0a9091 commit f981ed2

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

certificates/install_darwin.go

+38
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,30 @@ const char *installCert(const char *path) {
6161
return "";
6262
}
6363
64+
const char *uninstallCert() {
65+
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
66+
kSecClassCertificate, kSecClass,
67+
kSecMatchLimitAll, kSecMatchLimit,
68+
kCFBooleanTrue, kSecReturnAttributes,
69+
nil];
70+
71+
OSStatus err = noErr;
72+
CFTypeRef itemList;
73+
err = SecItemCopyMatching((CFDictionaryRef)dict, &itemList);
74+
if (err == noErr) {
75+
err = SecItemDelete((CFDictionaryRef)dict);
76+
if (err != noErr) {
77+
NSString *errString = [@"Could not delete the certificates. Error: " stringByAppendingFormat:@"%d", err];
78+
NSLog(@"%@", errString);
79+
return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];;
80+
}
81+
} else if (err != errSecItemNotFound){
82+
NSString *errString = [@"Error: " stringByAppendingFormat:@"%d", err];
83+
NSLog(@"%@", errString);
84+
return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];;
85+
}
86+
return "";
87+
}
6488
*/
6589
import "C"
6690
import (
@@ -88,3 +112,17 @@ func InstallCertificate(cert *paths.Path) error {
88112
}
89113
return nil
90114
}
115+
116+
// UninstallCertificates will uninstall the certificates from the system keychain on macos,
117+
// if something goes wrong will show a dialog with the error and return an error
118+
func UninstallCertificates() error {
119+
log.Infof("Uninstalling certificats")
120+
p := C.uninstallCert()
121+
s := C.GoString(p)
122+
if len(s) != 0 {
123+
oscmd := exec.Command("osascript", "-e", "display dialog \""+s+"\" buttons \"OK\" with title \"Error uninstalling certificates\"")
124+
_ = oscmd.Run()
125+
return errors.New(s)
126+
}
127+
return nil
128+
}

certificates/install_default.go

+6
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,9 @@ func InstallCertificate(cert *paths.Path) error {
3030
log.Warn("platform not supported for the certificate install")
3131
return errors.New("platform not supported for the certificate install")
3232
}
33+
34+
// UninstallCertificates won't do anything on unsupported Operative Systems
35+
func UninstallCertificates() error {
36+
log.Warn("platform not supported for the certificates uninstall")
37+
return errors.New("platform not supported for the certificates uninstall")
38+
}

0 commit comments

Comments
 (0)