@@ -89,6 +89,59 @@ 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
+ kCFBooleanTrue, kSecReturnAttributes,
100
+ nil];
101
+
102
+ OSStatus err = noErr;
103
+ CFTypeRef cert;
104
+ // Use this function to check for errors
105
+ err = SecItemCopyMatching((CFDictionaryRef)dict, &cert);
106
+ if (err == noErr) {
107
+ SecPolicyRef policy = SecPolicyCreateBasicX509();
108
+ SecTrustRef trust;
109
+ err = SecTrustCreateWithCertificates(cert, policy, &trust);
110
+ CFRelease(policy);
111
+ if (err != noErr) {
112
+ NSString *errString = [@"Could not delete the certificates. Error: " stringByAppendingFormat:@"%d", err];
113
+ NSLog(@"%@", errString);
114
+ return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];;
115
+ }
116
+ SecTrustEvaluateAsyncWithError(trust, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
117
+ ^(SecTrustRef evaluatedTrust, bool trustResult, CFErrorRef error) {
118
+ if (trustResult == YES) {
119
+ // Evaluation succeeded!
120
+ SecKeyRef publicKey = SecTrustCopyPublicKey(evaluatedTrust);
121
+
122
+ // Use and release key . . .
123
+
124
+ } else {
125
+ // Evaluation failed: Check the error
126
+ SecTrustResultType trustResult;
127
+ SecTrustGetTrustResult(trust, &trustResult);
128
+ if (trustResult == kSecTrustResultRecoverableTrustFailure) {
129
+ // Make changes and try again.
130
+ }
131
+ }
132
+
133
+ // Finally, release the trust and error.
134
+ if (evaluatedTrust) { CFRelease(evaluatedTrust); }
135
+ if (error) { CFRelease(error); }
136
+ }
137
+ );
138
+ } else if (err != errSecItemNotFound){
139
+ NSString *errString = [@"Error: " stringByAppendingFormat:@"%d", err];
140
+ NSLog(@"%@", errString);
141
+ return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];;
142
+ }
143
+ return "";
144
+ }
92
145
*/
93
146
import "C"
94
147
import (
@@ -131,3 +184,17 @@ func UninstallCertificates() error {
131
184
}
132
185
return nil
133
186
}
187
+
188
+ // EvaluateCertificates will evaluate the certificates validity and eventually uninstall and re-install them,
189
+ // if something goes wrong will show a dialog with the error and return an error
190
+ func EvaluateCertificates () error {
191
+ log .Infof ("Evaluating certificates" )
192
+ p := C .evaluateCert ()
193
+ s := C .GoString (p )
194
+ if len (s ) != 0 {
195
+ oscmd := exec .Command ("osascript" , "-e" , "display dialog \" " + s + "\" buttons \" OK\" with title \" Arduino Agent: Error evaluating certificates\" " )
196
+ _ = oscmd .Run ()
197
+ return errors .New (s )
198
+ }
199
+ return nil
200
+ }
0 commit comments