@@ -143,7 +143,7 @@ static Boolean isRootCertificate(SecCertificateRef cert, CFErrorRef *errRef) {
143
143
return equal;
144
144
}
145
145
146
- // FetchPEMRoots fetches the system's list of trusted X.509 root certificates
146
+ // CopyPEMRoots fetches the system's list of trusted X.509 root certificates
147
147
// for the kSecTrustSettingsPolicy SSL.
148
148
//
149
149
// On success it returns 0 and fills pemRoots with a CFDataRef that contains the extracted root
@@ -152,15 +152,15 @@ static Boolean isRootCertificate(SecCertificateRef cert, CFErrorRef *errRef) {
152
152
//
153
153
// Note: The CFDataRef returned in pemRoots and untrustedPemRoots must
154
154
// be released (using CFRelease) after we've consumed its content.
155
- int FetchPEMRoots (CFDataRef *pemRoots, CFDataRef *untrustedPemRoots, bool debugDarwinRoots) {
155
+ int CopyPEMRoots (CFDataRef *pemRoots, CFDataRef *untrustedPemRoots, bool debugDarwinRoots) {
156
156
int i;
157
157
158
158
if (debugDarwinRoots) {
159
- printf( "crypto/x509: kSecTrustSettingsResultInvalid = %d\n", kSecTrustSettingsResultInvalid);
160
- printf( "crypto/x509: kSecTrustSettingsResultTrustRoot = %d\n", kSecTrustSettingsResultTrustRoot);
161
- printf( "crypto/x509: kSecTrustSettingsResultTrustAsRoot = %d\n", kSecTrustSettingsResultTrustAsRoot);
162
- printf( "crypto/x509: kSecTrustSettingsResultDeny = %d\n", kSecTrustSettingsResultDeny);
163
- printf( "crypto/x509: kSecTrustSettingsResultUnspecified = %d\n", kSecTrustSettingsResultUnspecified);
159
+ fprintf(stderr, "crypto/x509: kSecTrustSettingsResultInvalid = %d\n", kSecTrustSettingsResultInvalid);
160
+ fprintf(stderr, "crypto/x509: kSecTrustSettingsResultTrustRoot = %d\n", kSecTrustSettingsResultTrustRoot);
161
+ fprintf(stderr, "crypto/x509: kSecTrustSettingsResultTrustAsRoot = %d\n", kSecTrustSettingsResultTrustAsRoot);
162
+ fprintf(stderr, "crypto/x509: kSecTrustSettingsResultDeny = %d\n", kSecTrustSettingsResultDeny);
163
+ fprintf(stderr, "crypto/x509: kSecTrustSettingsResultUnspecified = %d\n", kSecTrustSettingsResultUnspecified);
164
164
}
165
165
166
166
// Get certificates from all domains, not just System, this lets
@@ -170,7 +170,7 @@ int FetchPEMRoots(CFDataRef *pemRoots, CFDataRef *untrustedPemRoots, bool debugD
170
170
kSecTrustSettingsDomainAdmin, kSecTrustSettingsDomainUser };
171
171
172
172
int numDomains = sizeof(domains)/sizeof(SecTrustSettingsDomain);
173
- if (pemRoots == NULL) {
173
+ if (pemRoots == NULL || untrustedPemRoots == NULL ) {
174
174
return -1;
175
175
}
176
176
@@ -186,8 +186,6 @@ int FetchPEMRoots(CFDataRef *pemRoots, CFDataRef *untrustedPemRoots, bool debugD
186
186
187
187
CFIndex numCerts = CFArrayGetCount(certs);
188
188
for (j = 0; j < numCerts; j++) {
189
- CFDataRef data = NULL;
190
- CFArrayRef trustSettings = NULL;
191
189
SecCertificateRef cert = (SecCertificateRef)CFArrayGetValueAtIndex(certs, j);
192
190
if (cert == NULL) {
193
191
continue;
@@ -206,7 +204,7 @@ int FetchPEMRoots(CFDataRef *pemRoots, CFDataRef *untrustedPemRoots, bool debugD
206
204
CFErrorRef errRef = NULL;
207
205
CFStringRef summary = SecCertificateCopyShortDescription(NULL, cert, &errRef);
208
206
if (errRef != NULL) {
209
- printf( "crypto/x509: SecCertificateCopyShortDescription failed\n");
207
+ fprintf(stderr, "crypto/x509: SecCertificateCopyShortDescription failed\n");
210
208
CFRelease(errRef);
211
209
continue;
212
210
}
@@ -215,7 +213,7 @@ int FetchPEMRoots(CFDataRef *pemRoots, CFDataRef *untrustedPemRoots, bool debugD
215
213
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
216
214
char *buffer = malloc(maxSize);
217
215
if (CFStringGetCString(summary, buffer, maxSize, kCFStringEncodingUTF8)) {
218
- printf( "crypto/x509: %s returned %d\n", buffer, (int)result);
216
+ fprintf(stderr, "crypto/x509: %s returned %d\n", buffer, (int)result);
219
217
}
220
218
free(buffer);
221
219
CFRelease(summary);
@@ -251,6 +249,7 @@ int FetchPEMRoots(CFDataRef *pemRoots, CFDataRef *untrustedPemRoots, bool debugD
251
249
continue;
252
250
}
253
251
252
+ CFDataRef data = NULL;
254
253
err = SecItemExport(cert, kSecFormatX509Cert, kSecItemPemArmour, NULL, &data);
255
254
if (err != noErr) {
256
255
continue;
@@ -274,22 +273,22 @@ import (
274
273
)
275
274
276
275
func loadSystemRoots () (* CertPool , error ) {
277
- roots := NewCertPool ()
278
-
279
- var data C.CFDataRef = 0
280
- var untrustedData C.CFDataRef = 0
281
- err := C .FetchPEMRoots (& data , & untrustedData , C .bool (debugDarwinRoots ))
276
+ var data , untrustedData C.CFDataRef
277
+ err := C .CopyPEMRoots (& data , & untrustedData , C .bool (debugDarwinRoots ))
282
278
if err == - 1 {
283
279
return nil , errors .New ("crypto/x509: failed to load darwin system roots with cgo" )
284
280
}
285
-
286
281
defer C .CFRelease (C .CFTypeRef (data ))
282
+ defer C .CFRelease (C .CFTypeRef (untrustedData ))
283
+
287
284
buf := C .GoBytes (unsafe .Pointer (C .CFDataGetBytePtr (data )), C .int (C .CFDataGetLength (data )))
285
+ roots := NewCertPool ()
288
286
roots .AppendCertsFromPEM (buf )
289
- if untrustedData == 0 {
287
+
288
+ if C .CFDataGetLength (untrustedData ) == 0 {
290
289
return roots , nil
291
290
}
292
- defer C . CFRelease ( C . CFTypeRef ( untrustedData ))
291
+
293
292
buf = C .GoBytes (unsafe .Pointer (C .CFDataGetBytePtr (untrustedData )), C .int (C .CFDataGetLength (untrustedData )))
294
293
untrustedRoots := NewCertPool ()
295
294
untrustedRoots .AppendCertsFromPEM (buf )
0 commit comments