@@ -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+ CFTypeRef itemList;
77+ err = SecItemCopyMatching((CFDictionaryRef)dict, &itemList);
78+ if (err == noErr) {
79+ err = SecItemDelete(itemList);
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*/
6593import "C"
6694import (
@@ -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+ }
0 commit comments