Skip to content

Commit 89dbaa4

Browse files
Add function to retrieve certificates as byte data from the keychain
1 parent d67e3c0 commit 89dbaa4

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

certificates/install_darwin.go

+34
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@ const char *evaluateCert(){
139139
}
140140
return "";
141141
}
142+
143+
const char *getCert(){
144+
// Each line is a key-value of the dictionary. Note: the the inverted order, value first then key.
145+
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
146+
(id)kSecClassCertificate, kSecClass,
147+
CFSTR("Arduino"), kSecAttrLabel,
148+
kSecMatchLimitOne, kSecMatchLimit,
149+
kCFBooleanFalse, kSecReturnAttributes,
150+
kCFBooleanTrue, kSecReturnData,
151+
nil];
152+
153+
OSStatus err = noErr;
154+
CFTypeRef cert;
155+
// Use this function to check for errors
156+
err = SecItemCopyMatching((CFDictionaryRef)dict, &cert);
157+
if (err != errSecItemNotFound){
158+
NSString *errString = [@"Error: " stringByAppendingFormat:@"%d", err];
159+
NSLog(@"%@", errString);
160+
return "";
161+
}
162+
NSString *certString = [@"" stringByAppendingFormat:@"%d", err];
163+
return [certString cStringUsingEncoding:[NSString defaultCStringEncoding]];;
164+
}
142165
*/
143166
import "C"
144167
import (
@@ -195,3 +218,14 @@ func EvaluateCertificates() error {
195218
}
196219
return nil
197220
}
221+
222+
// GetCertificate returns a certificate stored in the keychain
223+
func GetCertificate() ([]byte, error) {
224+
log.Infof("Evaluating certificates")
225+
p := C.evaluateCert()
226+
s := C.GoString(p)
227+
if len(s) != 0 {
228+
return []byte(s), nil
229+
}
230+
return nil, nil
231+
}

certificates/install_default.go

+6
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ func EvaluateCertificates() error {
4242
log.Warn("platform not supported for the certificates evaluation")
4343
return errors.New("platform not supported for the certificates evaluation")
4444
}
45+
46+
// GetCertificate won't do anything on unsupported Operative Systems
47+
func GetCertificate() ([]byte, error) {
48+
log.Warn("platform not supported for retrieving certificates")
49+
return nil, errors.New("platform not supported for retrieving certificates")
50+
}

0 commit comments

Comments
 (0)