@@ -89,6 +89,56 @@ const char *uninstallCert() {
89
89
}
90
90
return "";
91
91
}
92
+
93
+ const char *evaluateCert(){
94
+ // Each line is a key-value of the dictionary. Note: the the inverted order, value first then key.
95
+ NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
96
+ (id)kSecClassCertificate, kSecClass,
97
+ CFSTR("Arduino"), kSecAttrLabel,
98
+ kSecMatchLimitOne, kSecMatchLimit,
99
+ kCFBooleanFalse, kSecReturnAttributes,
100
+ kCFBooleanTrue, kSecReturnData,
101
+ nil];
102
+
103
+ OSStatus err = noErr;
104
+ CFTypeRef cert;
105
+ // Use this function to check for errors
106
+ err = SecItemCopyMatching((CFDictionaryRef)dict, &cert);
107
+ if (err == noErr) {
108
+ SecPolicyRef policy = SecPolicyCreateBasicX509();
109
+ SecTrustRef trust;
110
+ err = SecTrustCreateWithCertificates(cert, policy, &trust);
111
+ CFRelease(policy);
112
+ if (err != noErr) {
113
+ NSString *errString = [@"Could not create a trust item from the certificates. Error: " stringByAppendingFormat:@"%d", err];
114
+ NSLog(@"%@", errString);
115
+ return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];;
116
+ }
117
+ SecTrustEvaluateAsyncWithError(trust, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
118
+ ^(SecTrustRef evaluatedTrust, bool trustResult, CFErrorRef error) {
119
+ if (trustResult == YES) {
120
+ // Evaluation succeeded!
121
+ } else {
122
+ // Evaluation failed: Check the error
123
+ SecTrustResultType trustResult;
124
+ SecTrustGetTrustResult(trust, &trustResult);
125
+ if (trustResult == kSecTrustResultRecoverableTrustFailure) {
126
+ // Make changes and try again.
127
+ }
128
+ }
129
+
130
+ // Finally, release the trust and error.
131
+ if (evaluatedTrust) { CFRelease(evaluatedTrust); }
132
+ if (error) { CFRelease(error); }
133
+ }
134
+ );
135
+ } else if (err != errSecItemNotFound){
136
+ NSString *errString = [@"Error: " stringByAppendingFormat:@"%d", err];
137
+ NSLog(@"%@", errString);
138
+ return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];;
139
+ }
140
+ return "";
141
+ }
92
142
*/
93
143
import "C"
94
144
import (
@@ -131,3 +181,17 @@ func UninstallCertificates() error {
131
181
}
132
182
return nil
133
183
}
184
+
185
+ // EvaluateCertificates will evaluate the certificates validity and eventually uninstall and re-install them,
186
+ // if something goes wrong will show a dialog with the error and return an error
187
+ func EvaluateCertificates () error {
188
+ log .Infof ("Evaluating certificates" )
189
+ p := C .evaluateCert ()
190
+ s := C .GoString (p )
191
+ if len (s ) != 0 {
192
+ oscmd := exec .Command ("osascript" , "-e" , "display dialog \" " + s + "\" buttons \" OK\" with title \" Arduino Agent: Error evaluating certificates\" " )
193
+ _ = oscmd .Run ()
194
+ return errors .New (s )
195
+ }
196
+ return nil
197
+ }
0 commit comments