Skip to content

Commit 7f4cdf6

Browse files
Check for the presence of the certificate in the keychain to determine if it is installed
1 parent e46bfbf commit 7f4cdf6

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

certificates/install_darwin.go

+30
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ const char *getDefaultBrowserName() {
150150
151151
return "";
152152
}
153+
154+
const char *certInKeychain() {
155+
// Each line is a key-value of the dictionary. Note: the the inverted order, value first then key.
156+
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
157+
(id)kSecClassCertificate, kSecClass,
158+
CFSTR("Arduino"), kSecAttrLabel,
159+
kSecMatchLimitOne, kSecMatchLimit,
160+
kCFBooleanTrue, kSecReturnAttributes,
161+
nil];
162+
163+
OSStatus err = noErr;
164+
// Use this function to check for errors
165+
err = SecItemCopyMatching((CFDictionaryRef)dict, nil);
166+
NSString *exists = @"false";
167+
if (err == noErr) {
168+
exists = @"true";
169+
}
170+
return [exists cStringUsingEncoding:[NSString defaultCStringEncoding]];;
171+
}
153172
*/
154173
import "C"
155174
import (
@@ -213,3 +232,14 @@ func GetDefaultBrowserName() string {
213232
p := C.getDefaultBrowserName()
214233
return C.GoString(p)
215234
}
235+
236+
// CertInKeychain checks if the certificate is stored inside the keychain
237+
func CertInKeychain() bool {
238+
log.Infof("Checking if the Arduino certificate is in the keychain")
239+
p := C.certInKeychain()
240+
s := C.GoString(p)
241+
if s == "true" {
242+
return true
243+
}
244+
return false
245+
}

certificates/install_default.go

+6
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,9 @@ func GetDefaultBrowserName() string {
4848
log.Warn("platform not supported for retrieving default browser name")
4949
return ""
5050
}
51+
52+
// CertInKeychain won't do anything on unsupported Operative Systems
53+
func CertInKeychain() bool {
54+
log.Warn("platform not supported for verifying the certificate existence")
55+
return false
56+
}

main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func loop() {
227227
if exist, err := installCertsKeyExists(configPath.String()); err != nil {
228228
log.Panicf("config.ini cannot be parsed: %s", err)
229229
} else if !exist {
230-
if config.CertsExist() {
230+
if cert.CertInKeychain() || config.CertsExist() {
231231
err = config.SetInstallCertsIni(configPath.String(), "true")
232232
if err != nil {
233233
log.Panicf("config.ini cannot be parsed: %s", err)
@@ -373,7 +373,7 @@ func loop() {
373373

374374
// check if the HTTPS certificates are expired or expiring and prompt the user to update them on macOS
375375
if runtime.GOOS == "darwin" && *installCerts {
376-
if config.CertsExist() {
376+
if cert.CertInKeychain() || config.CertsExist() {
377377
certDir := config.GetCertificatesDir()
378378
if expired, err := cert.IsExpired(); err != nil {
379379
log.Errorf("cannot check if certificates are expired something went wrong: %s", err)

systray/systray_real.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (s *Systray) start() {
9797
buttons := "{\"OK\", \"Install the certificate for Safari\"}"
9898
defaultButton := "Install the certificate for Safari"
9999
certDir := config.GetCertificatesDir()
100-
if config.CertsExist() {
100+
if cert.CertInKeychain() || config.CertsExist() {
101101
expDate, err := cert.GetExpirationDate()
102102
if err != nil {
103103
log.Errorf("cannot get certificates expiration date, something went wrong: %s", err)

0 commit comments

Comments
 (0)