Skip to content

Commit 2326a66

Browse files
committed
crypto/x509: fix and cleanup loadSystemRoots on macOS
Note how untrustedData is never NULL, so loadSystemRoots was checking the wrong thing. Also, renamed the C function to CopyPEMRoots to follow the CoreFoundation naming convention on ownership. Finally, redirect all debug output to standard error. Change-Id: Ie80abefadf8974a75c0646aa02fcfcebcbe3bde8 Reviewed-on: https://go-review.googlesource.com/c/go/+/178538 Reviewed-by: Adam Langley <[email protected]>
1 parent a3d4655 commit 2326a66

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

src/crypto/x509/root_cgo_darwin.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ static Boolean isRootCertificate(SecCertificateRef cert, CFErrorRef *errRef) {
143143
return equal;
144144
}
145145
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
147147
// for the kSecTrustSettingsPolicy SSL.
148148
//
149149
// 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) {
152152
//
153153
// Note: The CFDataRef returned in pemRoots and untrustedPemRoots must
154154
// 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) {
156156
int i;
157157
158158
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);
164164
}
165165
166166
// Get certificates from all domains, not just System, this lets
@@ -170,7 +170,7 @@ int FetchPEMRoots(CFDataRef *pemRoots, CFDataRef *untrustedPemRoots, bool debugD
170170
kSecTrustSettingsDomainAdmin, kSecTrustSettingsDomainUser };
171171
172172
int numDomains = sizeof(domains)/sizeof(SecTrustSettingsDomain);
173-
if (pemRoots == NULL) {
173+
if (pemRoots == NULL || untrustedPemRoots == NULL) {
174174
return -1;
175175
}
176176
@@ -186,8 +186,6 @@ int FetchPEMRoots(CFDataRef *pemRoots, CFDataRef *untrustedPemRoots, bool debugD
186186
187187
CFIndex numCerts = CFArrayGetCount(certs);
188188
for (j = 0; j < numCerts; j++) {
189-
CFDataRef data = NULL;
190-
CFArrayRef trustSettings = NULL;
191189
SecCertificateRef cert = (SecCertificateRef)CFArrayGetValueAtIndex(certs, j);
192190
if (cert == NULL) {
193191
continue;
@@ -206,7 +204,7 @@ int FetchPEMRoots(CFDataRef *pemRoots, CFDataRef *untrustedPemRoots, bool debugD
206204
CFErrorRef errRef = NULL;
207205
CFStringRef summary = SecCertificateCopyShortDescription(NULL, cert, &errRef);
208206
if (errRef != NULL) {
209-
printf("crypto/x509: SecCertificateCopyShortDescription failed\n");
207+
fprintf(stderr, "crypto/x509: SecCertificateCopyShortDescription failed\n");
210208
CFRelease(errRef);
211209
continue;
212210
}
@@ -215,7 +213,7 @@ int FetchPEMRoots(CFDataRef *pemRoots, CFDataRef *untrustedPemRoots, bool debugD
215213
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
216214
char *buffer = malloc(maxSize);
217215
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);
219217
}
220218
free(buffer);
221219
CFRelease(summary);
@@ -251,6 +249,7 @@ int FetchPEMRoots(CFDataRef *pemRoots, CFDataRef *untrustedPemRoots, bool debugD
251249
continue;
252250
}
253251
252+
CFDataRef data = NULL;
254253
err = SecItemExport(cert, kSecFormatX509Cert, kSecItemPemArmour, NULL, &data);
255254
if (err != noErr) {
256255
continue;
@@ -274,22 +273,22 @@ import (
274273
)
275274

276275
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))
282278
if err == -1 {
283279
return nil, errors.New("crypto/x509: failed to load darwin system roots with cgo")
284280
}
285-
286281
defer C.CFRelease(C.CFTypeRef(data))
282+
defer C.CFRelease(C.CFTypeRef(untrustedData))
283+
287284
buf := C.GoBytes(unsafe.Pointer(C.CFDataGetBytePtr(data)), C.int(C.CFDataGetLength(data)))
285+
roots := NewCertPool()
288286
roots.AppendCertsFromPEM(buf)
289-
if untrustedData == 0 {
287+
288+
if C.CFDataGetLength(untrustedData) == 0 {
290289
return roots, nil
291290
}
292-
defer C.CFRelease(C.CFTypeRef(untrustedData))
291+
293292
buf = C.GoBytes(unsafe.Pointer(C.CFDataGetBytePtr(untrustedData)), C.int(C.CFDataGetLength(untrustedData)))
294293
untrustedRoots := NewCertPool()
295294
untrustedRoots.AppendCertsFromPEM(buf)

src/crypto/x509/root_darwin_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,10 @@ func TestSystemRoots(t *testing.T) {
120120

121121
if t.Failed() && debugDarwinRoots {
122122
cmd := exec.Command("security", "dump-trust-settings")
123-
cmd.Stdout = os.Stdout
124-
cmd.Stderr = os.Stderr
123+
cmd.Stdout, cmd.Stderr = os.Stderr, os.Stderr
125124
cmd.Run()
126125
cmd = exec.Command("security", "dump-trust-settings", "-d")
127-
cmd.Stdout = os.Stdout
128-
cmd.Stderr = os.Stderr
126+
cmd.Stdout, cmd.Stderr = os.Stderr, os.Stderr
129127
cmd.Run()
130128
}
131129
}

0 commit comments

Comments
 (0)