Skip to content

Fix buttons and improve handling of certificates when Safari is not the default browser #949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
May 13, 2024
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f43b0e4
Fix check on buttons returning the correct message
MatteoPologruto May 8, 2024
f69980a
Update certificates regardless of the default browser
MatteoPologruto May 8, 2024
f7bd6e3
Set installCerts when the certificate is installed from previous vers…
MatteoPologruto May 8, 2024
8006310
Do not set installCerts to false if the default browser is not Safari
MatteoPologruto May 8, 2024
aa02ed0
Do not ask again to update the certificate if the user refuses once
MatteoPologruto May 9, 2024
e46bfbf
Fix user script on macOS
MatteoPologruto May 9, 2024
7f4cdf6
Check for the presence of the certificate in the keychain to determin…
MatteoPologruto May 9, 2024
4285c04
Fix getExpirationDate breaking when the certificate is expired
MatteoPologruto May 9, 2024
4064541
Fix return value in case of error
MatteoPologruto May 9, 2024
cf31546
getExpirationDate rewritten to use the correct expiration field.
Xayton May 9, 2024
411d051
Separate osascript default button from the one to press
MatteoPologruto May 9, 2024
c11610b
Fix leftover buttons
MatteoPologruto May 9, 2024
9494f25
Small text fixes in the "manage certificate" dialog
Xayton May 10, 2024
d56f231
Simplify error management in getExpirationDate
Xayton May 10, 2024
8ec1efc
Fix compiler warnings and move obj-c code into a separate file.
Xayton May 10, 2024
34fae24
certInKeychain returns a bool
Xayton May 10, 2024
8854680
Fix building errors caused by objective-c files on Ubuntu and Windows
MatteoPologruto May 13, 2024
681a250
Build objective-c files only on Darwin
MatteoPologruto May 13, 2024
c461c40
Remove -ld_classic library because XCode is not up to date on the CI
MatteoPologruto May 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
getExpirationDate rewritten to use the correct expiration field.
  • Loading branch information
Xayton committed May 9, 2024
commit cf315464198033e2915b8212e77532734d5441c3
76 changes: 39 additions & 37 deletions certificates/install_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ package certificates
#cgo LDFLAGS: -framework Cocoa
#import <Cocoa/Cocoa.h>


// Used to return error strings (as NSString) as a C-string to the Go code.
const char *toErrorString(NSString *errString) {
NSLog(@"%@", errString);
return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];
}

const char *installCert(const char *path) {
NSURL *url = [NSURL fileURLWithPath:@(path) isDirectory:NO];
NSData *rootCertData = [NSData dataWithContentsOfURL:url];
Expand Down Expand Up @@ -90,7 +97,9 @@ const char *uninstallCert() {
return "";
}

const char *getExpirationDate(char *expirationDate){
// Returns the expiration date "kSecOIDX509V1ValidityNotAfter" of the Arduino certificate.
// The value is returned as a CFAbsoluteTime: a long number of seconds from the date of 1 Jan 2001 00:00:00 GMT.
const char *getExpirationDate(long *expirationDate) {
// Create a key-value dictionary used to query the Keychain and look for the "Arduino" root certificate.
NSDictionary *getquery = @{
(id)kSecClass: (id)kSecClassCertificate,
Expand All @@ -101,42 +110,32 @@ const char *getExpirationDate(char *expirationDate){
OSStatus err = noErr;
SecCertificateRef cert = NULL;

// Use this function to check for errors
// Search the keychain for certificates matching the query above.
err = SecItemCopyMatching((CFDictionaryRef)getquery, (CFTypeRef *)&cert);

if (err != noErr){
NSString *errString = [@"Error: " stringByAppendingFormat:@"%d", err];
NSLog(@"%@", errString);
return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];
}

// Get data from the certificate. We just need the "invalidity date" property.
CFDictionaryRef valuesDict = SecCertificateCopyValues(cert, (__bridge CFArrayRef)@[(__bridge id)kSecOIDX509V1ValidityNotAfter], NULL);

id expirationDateValue;
if(valuesDict){
CFDictionaryRef invalidityDateDictionaryRef = CFDictionaryGetValue(valuesDict, kSecOIDX509V1ValidityNotAfter);
if(invalidityDateDictionaryRef){
CFTypeRef invalidityRef = CFDictionaryGetValue(invalidityDateDictionaryRef, kSecPropertyKeyValue);
if(invalidityRef){
expirationDateValue = CFBridgingRelease(invalidityRef);
}
}
CFRelease(valuesDict);
}
// Get data from the certificate, as a dictionary of properties. We just need the "invalidity not after" property.
CFDictionaryRef certDict = SecCertificateCopyValues(cert,
(__bridge CFArrayRef)@[(__bridge id)kSecOIDX509V1ValidityNotAfter], NULL);
if (certDict == NULL) return toErrorString(@"SecCertificateCopyValues failed");

NSString *outputString = [@"" stringByAppendingFormat:@"%@", expirationDateValue];
if([outputString isEqualToString:@""]){
NSString *errString = @"Error: the expiration date of the certificate could not be found";
NSLog(@"%@", errString);
return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];
}

// This workaround allows to obtain the expiration date alongside the error message
strncpy(expirationDate, [outputString cStringUsingEncoding:[NSString defaultCStringEncoding]], 32);
expirationDate[32-1] = 0;
// Get the "validity not after" property as a dictionary, and get the "value" key (that is a number).
CFDictionaryRef validityNotAfterDict = CFDictionaryGetValue(certDict, kSecOIDX509V1ValidityNotAfter);
if (validityNotAfterDict == NULL) return toErrorString(@"CFDictionaryGetValue (validity) failed");

return "";
CFNumberRef number = (CFNumberRef)CFDictionaryGetValue(validityNotAfterDict, kSecPropertyKeyValue);
if (number == NULL) return toErrorString(@"CFDictionaryGetValue (keyValue) failed");

CFNumberGetValue(number, kCFNumberSInt64Type, expirationDate);
// NSLog(@"Certificate validity not after: %ld", *expirationDate);

CFRelease(certDict);
return ""; // No error.
}

const char *getDefaultBrowserName() {
Expand Down Expand Up @@ -173,7 +172,6 @@ const char *certInKeychain() {
import "C"
import (
"errors"
"strconv"
"time"
"unsafe"

Expand Down Expand Up @@ -215,16 +213,20 @@ func UninstallCertificates() error {
// GetExpirationDate returns the expiration date of a certificate stored in the keychain
func GetExpirationDate() (time.Time, error) {
log.Infof("Retrieving certificate's expiration date")
dateString := C.CString("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") // 32 characters string
defer C.free(unsafe.Pointer(dateString))
p := C.getExpirationDate(dateString)
s := C.GoString(p)
if len(s) != 0 {
utilities.UserPrompt(s, "\"OK\"", "OK", "Arduino Agent: Error retrieving expiration date")
return time.Time{}, errors.New(s)

expirationDateLong := C.long(0)

err := C.getExpirationDate(&expirationDateLong)
errString := C.GoString(err)
if len(errString) > 0 {
utilities.UserPrompt(errString, "\"OK\"", "OK", "Arduino Agent: Error retrieving expiration date")
return time.Time{}, errors.New(errString)
}
dateValue, _ := strconv.ParseInt(C.GoString(dateString), 10, 64)
return time.Unix(dateValue, 0).AddDate(31, 0, 0), nil

// The expirationDate is the number of seconds from the date of 1 Jan 2001 00:00:00 GMT.
// Add 31 years to convert it to Unix Epoch.
expirationDate := int64(expirationDateLong)
return time.Unix(expirationDate, 0).AddDate(31, 0, 0), nil
}

// GetDefaultBrowserName returns the name of the default browser
Expand Down
Loading