@@ -22,172 +22,7 @@ package certificates
22
22
#cgo CFLAGS: -x objective-c
23
23
// Pass the list of macOS frameworks needed by this piece of Objective-C code.
24
24
#cgo LDFLAGS: -framework Cocoa
25
- #import <Cocoa/Cocoa.h>
26
-
27
- const char *installCert(const char *path) {
28
- NSURL *url = [NSURL fileURLWithPath:@(path) isDirectory:NO];
29
- NSData *rootCertData = [NSData dataWithContentsOfURL:url];
30
-
31
- OSStatus err = noErr;
32
- SecCertificateRef rootCert = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef) rootCertData);
33
-
34
- CFTypeRef result;
35
-
36
- NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
37
- (id)kSecClassCertificate, kSecClass,
38
- rootCert, kSecValueRef,
39
- nil];
40
-
41
- err = SecItemAdd((CFDictionaryRef)dict, &result);
42
-
43
- if (err == noErr) {
44
- NSLog(@"Install root certificate success");
45
- } else if (err == errSecDuplicateItem) {
46
- NSString *errString = [@"duplicate root certificate entry. Error: " stringByAppendingFormat:@"%d", err];
47
- NSLog(@"%@", errString);
48
- return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];;
49
- } else {
50
- NSString *errString = [@"install root certificate failure. Error: " stringByAppendingFormat:@"%d", err];
51
- NSLog(@"%@", errString);
52
- return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];
53
- }
54
-
55
- NSDictionary *newTrustSettings = @{(id)kSecTrustSettingsResult: [NSNumber numberWithInt:kSecTrustSettingsResultTrustRoot]};
56
- err = SecTrustSettingsSetTrustSettings(rootCert, kSecTrustSettingsDomainUser, (__bridge CFTypeRef)(newTrustSettings));
57
- if (err != errSecSuccess) {
58
- NSString *errString = [@"Could not change the trust setting for a certificate. Error: " stringByAppendingFormat:@"%d", err];
59
- NSLog(@"%@", errString);
60
- return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];
61
- }
62
-
63
- return "";
64
- }
65
-
66
- const char *uninstallCert() {
67
- // Each line is a key-value of the dictionary. Note: the the inverted order, value first then key.
68
- NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
69
- (id)kSecClassCertificate, kSecClass,
70
- CFSTR("Arduino"), kSecAttrLabel,
71
- kSecMatchLimitOne, kSecMatchLimit,
72
- kCFBooleanTrue, kSecReturnAttributes,
73
- nil];
74
-
75
- OSStatus err = noErr;
76
- // Use this function to check for errors
77
- err = SecItemCopyMatching((CFDictionaryRef)dict, nil);
78
- if (err == noErr) {
79
- err = SecItemDelete((CFDictionaryRef)dict);
80
- if (err != noErr) {
81
- NSString *errString = [@"Could not delete the certificates. Error: " stringByAppendingFormat:@"%d", err];
82
- NSLog(@"%@", errString);
83
- return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];;
84
- }
85
- } else if (err != errSecItemNotFound){
86
- NSString *errString = [@"Error: " stringByAppendingFormat:@"%d", err];
87
- NSLog(@"%@", errString);
88
- return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];;
89
- }
90
- return "";
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
- }
142
-
143
- const char *getExpirationDate(char *expirationDate){
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 != noErr){
158
- NSString *errString = [@"Error: " stringByAppendingFormat:@"%d", err];
159
- NSLog(@"%@", errString);
160
- return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];
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
- id expirationDateValue;
167
- if(valuesDict){
168
- CFDictionaryRef invalidityDateDictionaryRef = CFDictionaryGetValue(valuesDict, kSecOIDInvalidityDate);
169
- if(invalidityDateDictionaryRef){
170
- CFTypeRef invalidityRef = CFDictionaryGetValue(invalidityDateDictionaryRef, kSecPropertyKeyValue);
171
- if(invalidityRef){
172
- expirationDateValue = CFBridgingRelease(invalidityRef);
173
- }
174
- }
175
- CFRelease(valuesDict);
176
- }
177
-
178
- NSString *outputString = [@"" stringByAppendingFormat:@"%@", expirationDateValue];
179
- if([outputString isEqualToString:@""]){
180
- NSString *errString = @"Error: the expiration date of the certificate could not be found";
181
- NSLog(@"%@", errString);
182
- return [errString cStringUsingEncoding:[NSString defaultCStringEncoding]];
183
- }
184
-
185
- // This workaround allows to obtain the expiration date alongside the error message
186
- strncpy(expirationDate, [outputString cStringUsingEncoding:[NSString defaultCStringEncoding]], 32);
187
- expirationDate[32-1] = 0;
188
-
189
- return "";
190
- }
25
+ #include "certs.h"
191
26
*/
192
27
import "C"
193
28
import (
0 commit comments