Skip to content

Commit 34fae24

Browse files
committed
certInKeychain returns a bool
1 parent 8ec1efc commit 34fae24

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

certificates/certificates.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ const char *getDefaultBrowserName();
22

33
const char *installCert(const char *path);
44
const char *uninstallCert();
5-
const char *certInKeychain();
5+
const bool certInKeychain();
66

77
const char *getExpirationDate(long *expirationDate);

certificates/certificates.m

+10-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];
99
}
1010

11+
// Returns a string describing the name of the default browser set for the user, nil in case of error.
1112
const char *getDefaultBrowserName() {
1213
NSURL *defaultBrowserURL = [[NSWorkspace sharedWorkspace] URLForApplicationToOpenURL:[NSURL URLWithString:@"http://"]];
1314
if (defaultBrowserURL) {
@@ -87,23 +88,16 @@
8788
return "";
8889
}
8990

90-
const char *certInKeychain() {
91-
// Each line is a key-value of the dictionary. Note: the the inverted order, value first then key.
92-
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
93-
(id)kSecClassCertificate, kSecClass,
94-
CFSTR("Arduino"), kSecAttrLabel,
95-
kSecMatchLimitOne, kSecMatchLimit,
96-
kCFBooleanTrue, kSecReturnAttributes,
97-
nil];
91+
const bool certInKeychain() {
92+
// Create a key-value dictionary used to query the Keychain and look for the "Arduino" root certificate.
93+
NSDictionary *getquery = @{
94+
(id)kSecClass: (id)kSecClassCertificate,
95+
(id)kSecAttrLabel: @"Arduino",
96+
(id)kSecReturnRef: @YES,
97+
};
9898

99-
OSStatus err = noErr;
100-
// Use this function to check for errors
101-
err = SecItemCopyMatching((CFDictionaryRef)dict, nil);
102-
NSString *exists = @"false";
103-
if (err == noErr) {
104-
exists = @"true";
105-
}
106-
return [exists cStringUsingEncoding:[NSString defaultCStringEncoding]];
99+
OSStatus err = SecItemCopyMatching((CFDictionaryRef)getquery, nil);
100+
return (err == noErr); // No error means the certificate was found, otherwise err will be "errSecItemNotFound".
107101
}
108102

109103
// Returns the expiration date "kSecOIDX509V1ValidityNotAfter" of the Arduino certificate.

certificates/install_darwin.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ func GetDefaultBrowserName() string {
9696
// CertInKeychain checks if the certificate is stored inside the keychain
9797
func CertInKeychain() bool {
9898
log.Infof("Checking if the Arduino certificate is in the keychain")
99-
p := C.certInKeychain()
100-
s := C.GoString(p)
10199

102-
return s == "true"
100+
certInKeychain := C.certInKeychain()
101+
return bool(certInKeychain)
103102
}

0 commit comments

Comments
 (0)