Skip to content

Commit 499a841

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

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

certificates/install_darwin.go

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

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)