Skip to content

Commit 2f3282d

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

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

certificates/install_darwin.go

+45
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,40 @@ const char *evaluateCert(){
139139
}
140140
return "";
141141
}
142+
143+
const char *getExpirationDate(){
144+
// Create a key-value dictionary used to query the Keychain and look for the "Arduino" root certificate.
145+
NSDictionary *getquery = @{
146+
(id)kSecClass: (id)kSecClassCertificate,
147+
(id)kSecAttrLabel: @"Arduino",
148+
(id)kSecReturnRef: @YES,
149+
};
150+
151+
OSStatus err = noErr;
152+
SecCertificateRef cert = NULL;
153+
154+
// Use this function to check for errors
155+
err = SecItemCopyMatching((CFDictionaryRef)getquery, (CFTypeRef *)&cert);
156+
157+
if (err != errSecItemNotFound && err != noErr){
158+
NSString *errString = [@"Error: " stringByAppendingFormat:@"%d", err];
159+
NSLog(@"%@", errString);
160+
return "";
161+
}
162+
163+
// Get data from the certificate. We just need the "invalidity date" property.
164+
CFDictionaryRef valuesDict = SecCertificateCopyValues(cert, (__bridge CFArrayRef)@[(__bridge id)kSecOIDInvalidityDate], NULL);
165+
166+
// TODO: Error checking.
167+
CFDictionaryRef invalidityDateDictionaryRef = CFDictionaryGetValue(valuesDict, kSecOIDInvalidityDate);
168+
CFTypeRef invalidityRef = CFDictionaryGetValue(invalidityDateDictionaryRef, kSecPropertyKeyValue);
169+
id expirationDateValue = CFBridgingRelease(invalidityRef);
170+
171+
CFRelease(valuesDict);
172+
173+
NSString *outputString = [@"" stringByAppendingFormat:@"%@", expirationDateValue];
174+
return [outputString cStringUsingEncoding:[NSString defaultCStringEncoding]];
175+
}
142176
*/
143177
import "C"
144178
import (
@@ -195,3 +229,14 @@ func EvaluateCertificates() error {
195229
}
196230
return nil
197231
}
232+
233+
// GetCertificate returns the expiration date of a certificate stored in the keychain
234+
func GetExpirationDate() (string, error) {
235+
log.Infof("Retrieving certificate's expiration date")
236+
p := C.getExpirationDate()
237+
s := C.GoString(p)
238+
if len(s) != 0 {
239+
return s, nil
240+
}
241+
return "", nil
242+
}

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 GetExpirationDate() (string, error) {
48+
log.Warn("platform not supported for retrieving certificates expiration date")
49+
return "", errors.New("platform not supported for retrieving certificates expiration date")
50+
}

0 commit comments

Comments
 (0)