Skip to content

Commit 8fc4e4a

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

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

certificates/install_darwin.go

+42
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package certificates
1818
//inspired by https://stackoverflow.com/questions/12798950/ios-install-ssl-certificate-programmatically
1919

2020
/*
21+
// Explicitly tell the GCC compiler that the language is Objective-C.
2122
#cgo CFLAGS: -x objective-c
23+
// Pass the list of macOS frameworks needed by this piece of Objective-C code.
2224
#cgo LDFLAGS: -framework Cocoa
2325
#import <Cocoa/Cocoa.h>
2426
@@ -61,6 +63,32 @@ const char *installCert(const char *path) {
6163
return "";
6264
}
6365
66+
const char *uninstallCert() {
67+
// Each line is a key-value of the dictionary. Note: the the inverted order, value first then key.
68+
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
69+
(id)kSecClassCertificate, kSecClass,
70+
CFSTR("Arduino"), kSecAttrLabel,
71+
kSecMatchLimitOne, kSecMatchLimit,
72+
kCFBooleanTrue, kSecReturnAttributes,
73+
nil];
74+
75+
OSStatus err = noErr;
76+
// Use this function to check for errors
77+
err = SecItemCopyMatching((CFDictionaryRef)dict, nil);
78+
if (err == noErr) {
79+
err = SecItemDelete((CFDictionaryRef)dict);
80+
if (err != noErr) {
81+
NSString *errString = [@"Could not delete the certificates. Error: " stringByAppendingFormat:@"%d", err];
82+
NSLog(@"%@", errString);
83+
return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];;
84+
}
85+
} else if (err != errSecItemNotFound){
86+
NSString *errString = [@"Error: " stringByAppendingFormat:@"%d", err];
87+
NSLog(@"%@", errString);
88+
return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];;
89+
}
90+
return "";
91+
}
6492
*/
6593
import "C"
6694
import (
@@ -88,3 +116,17 @@ func InstallCertificate(cert *paths.Path) error {
88116
}
89117
return nil
90118
}
119+
120+
// UninstallCertificates will uninstall the certificates from the system keychain on macos,
121+
// if something goes wrong will show a dialog with the error and return an error
122+
func UninstallCertificates() error {
123+
log.Infof("Uninstalling certificates")
124+
p := C.uninstallCert()
125+
s := C.GoString(p)
126+
if len(s) != 0 {
127+
oscmd := exec.Command("osascript", "-e", "display dialog \""+s+"\" buttons \"OK\" with title \"Error uninstalling certificates\"")
128+
_ = oscmd.Run()
129+
return errors.New(s)
130+
}
131+
return nil
132+
}

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)