Skip to content

Commit

Permalink
Add function to uninstall certificate from the system keychain
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoPologruto committed Apr 10, 2024
1 parent b0a9091 commit f5dcb39
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
38 changes: 38 additions & 0 deletions certificates/install_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ const char *installCert(const char *path) {
return "";
}
const char *uninstallCert() {
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
(id)kSecClassCertificate, kSecClass,
kSecMatchLimitAll, kSecMatchLimit,
kCFBooleanTrue, kSecReturnAttributes,
nil];
OSStatus err = noErr;
CFTypeRef itemList;
err = SecItemCopyMatching((CFDictionaryRef)dict, &itemList);
if (err == noErr) {
err = SecItemDelete((CFDictionaryRef)dict);
if (err != noErr) {
NSString *errString = [@"Could not delete the certificates. Error: " stringByAppendingFormat:@"%d", err];
NSLog(@"%@", errString);
return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];;
}
} else if (err != errSecItemNotFound){
NSString *errString = [@"Error: " stringByAppendingFormat:@"%d", err];
NSLog(@"%@", errString);
return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];;
}
return "";
}
*/
import "C"
import (
Expand Down Expand Up @@ -88,3 +112,17 @@ func InstallCertificate(cert *paths.Path) error {
}
return nil
}

// UninstallCertificates will uninstall the certificates from the system keychain on macos,
// if something goes wrong will show a dialog with the error and return an error
func UninstallCertificates() error {
log.Infof("Uninstalling certificats")
p := C.uninstallCert()
s := C.GoString(p)
if len(s) != 0 {
oscmd := exec.Command("osascript", "-e", "display dialog \""+s+"\" buttons \"OK\" with title \"Error uninstalling certificates\"")
_ = oscmd.Run()
return errors.New(s)
}
return nil
}
6 changes: 6 additions & 0 deletions certificates/install_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ func InstallCertificate(cert *paths.Path) error {
log.Warn("platform not supported for the certificate install")
return errors.New("platform not supported for the certificate install")
}

// UninstallCertificates won't do anything on unsupported Operative Systems
func UninstallCertificates() error {
log.Warn("platform not supported for the certificates uninstall")
return errors.New("platform not supported for the certificates uninstall")
}

0 comments on commit f5dcb39

Please sign in to comment.