Skip to content

Commit 83e9a97

Browse files
committed
crypto/x509/internal/macos: return errors when CFRef might be NULL
Updates #51759 Change-Id: Ib73fa5ec62d90c7e595150217b048158789f1afd Reviewed-on: https://go-review.googlesource.com/c/go/+/394674 Run-TryBot: Filippo Valsorda <[email protected]> Trust: Josh Bleecher Snyder <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]>
1 parent a7e76b8 commit 83e9a97

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

Diff for: src/crypto/x509/internal/macos/corefoundation.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ func CFDataToSlice(data CFRef) []byte {
3737
}
3838

3939
// CFStringToString returns a Go string representation of the passed
40-
// in CFString.
40+
// in CFString, or an empty string if it's invalid.
4141
func CFStringToString(ref CFRef) string {
42-
data := CFStringCreateExternalRepresentation(ref)
42+
data, err := CFStringCreateExternalRepresentation(ref)
43+
if err != nil {
44+
return ""
45+
}
4346
b := CFDataToSlice(data)
4447
CFRelease(data)
4548
return string(b)
@@ -186,9 +189,12 @@ func x509_CFErrorCopyDescription_trampoline()
186189

187190
//go:cgo_import_dynamic x509_CFStringCreateExternalRepresentation CFStringCreateExternalRepresentation "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation"
188191

189-
func CFStringCreateExternalRepresentation(strRef CFRef) CFRef {
192+
func CFStringCreateExternalRepresentation(strRef CFRef) (CFRef, error) {
190193
ret := syscall(abi.FuncPCABI0(x509_CFStringCreateExternalRepresentation_trampoline), kCFAllocatorDefault, uintptr(strRef), kCFStringEncodingUTF8, 0, 0, 0)
191-
return CFRef(ret)
194+
if ret == 0 {
195+
return 0, errors.New("string can't be represented as UTF-8")
196+
}
197+
return CFRef(ret), nil
192198
}
193199
func x509_CFStringCreateExternalRepresentation_trampoline()
194200

Diff for: src/crypto/x509/internal/macos/security.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,16 @@ func x509_SecTrustCreateWithCertificates_trampoline()
131131

132132
//go:cgo_import_dynamic x509_SecCertificateCreateWithData SecCertificateCreateWithData "/System/Library/Frameworks/Security.framework/Versions/A/Security"
133133

134-
func SecCertificateCreateWithData(b []byte) CFRef {
134+
func SecCertificateCreateWithData(b []byte) (CFRef, error) {
135135
data := BytesToCFData(b)
136+
defer CFRelease(data)
136137
ret := syscall(abi.FuncPCABI0(x509_SecCertificateCreateWithData_trampoline), kCFAllocatorDefault, uintptr(data), 0, 0, 0, 0)
137-
CFRelease(data)
138-
return CFRef(ret)
138+
// Returns NULL if the data passed in the data parameter is not a valid
139+
// DER-encoded X.509 certificate.
140+
if ret == 0 {
141+
return 0, errors.New("SecCertificateCreateWithData: invalid certificate")
142+
}
143+
return CFRef(ret), nil
139144
}
140145
func x509_SecCertificateCreateWithData_trampoline()
141146

Diff for: src/crypto/x509/root_darwin.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate, err error) {
1313
certs := macOS.CFArrayCreateMutable()
1414
defer macOS.ReleaseCFArray(certs)
15-
leaf := macOS.SecCertificateCreateWithData(c.Raw)
16-
if leaf == 0 {
15+
leaf, err := macOS.SecCertificateCreateWithData(c.Raw)
16+
if err != nil {
1717
return nil, errors.New("invalid leaf certificate")
1818
}
1919
macOS.CFArrayAppendValue(certs, leaf)
@@ -23,8 +23,8 @@ func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate
2323
if err != nil {
2424
return nil, err
2525
}
26-
sc := macOS.SecCertificateCreateWithData(c.Raw)
27-
if sc != 0 {
26+
sc, err := macOS.SecCertificateCreateWithData(c.Raw)
27+
if err == nil {
2828
macOS.CFArrayAppendValue(certs, sc)
2929
}
3030
}

0 commit comments

Comments
 (0)