Skip to content

Commit fa463cc

Browse files
rolandshoemakergopherbot
authored andcommitted
crypto/x509/internal/macos: handle unexpected null returns
SecCreatePolicySSL returns null when called from a binary that has a strange path. This seems to be a weirdo macos bug, but we should be properly handling those null returns anyway. Also add handling for SecTrustGetCertificateAtIndex. Fixes #54590 Change-Id: I251e74f3b0bf65890a80b094b3e88718e13fd3db Reviewed-on: https://go-review.googlesource.com/c/go/+/438135 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Roland Shoemaker <[email protected]> Reviewed-by: Damien Neil <[email protected]> Auto-Submit: Roland Shoemaker <[email protected]>
1 parent 6a9aaf1 commit fa463cc

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

src/crypto/x509/internal/macos/security.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,6 @@ func SecTrustSettingsCopyTrustSettings(cert CFRef, domain SecTrustSettingsDomain
109109
}
110110
func x509_SecTrustSettingsCopyTrustSettings_trampoline()
111111

112-
//go:cgo_import_dynamic x509_SecPolicyCopyProperties SecPolicyCopyProperties "/System/Library/Frameworks/Security.framework/Versions/A/Security"
113-
114-
func SecPolicyCopyProperties(policy CFRef) CFRef {
115-
ret := syscall(abi.FuncPCABI0(x509_SecPolicyCopyProperties_trampoline), uintptr(policy), 0, 0, 0, 0, 0)
116-
return CFRef(ret)
117-
}
118-
func x509_SecPolicyCopyProperties_trampoline()
119-
120112
//go:cgo_import_dynamic x509_SecTrustCreateWithCertificates SecTrustCreateWithCertificates "/System/Library/Frameworks/Security.framework/Versions/A/Security"
121113

122114
func SecTrustCreateWithCertificates(certs CFRef, policies CFRef) (CFRef, error) {
@@ -147,14 +139,17 @@ func x509_SecCertificateCreateWithData_trampoline()
147139

148140
//go:cgo_import_dynamic x509_SecPolicyCreateSSL SecPolicyCreateSSL "/System/Library/Frameworks/Security.framework/Versions/A/Security"
149141

150-
func SecPolicyCreateSSL(name string) CFRef {
142+
func SecPolicyCreateSSL(name string) (CFRef, error) {
151143
var hostname CFString
152144
if name != "" {
153145
hostname = StringToCFString(name)
154146
defer CFRelease(CFRef(hostname))
155147
}
156148
ret := syscall(abi.FuncPCABI0(x509_SecPolicyCreateSSL_trampoline), 1 /* true */, uintptr(hostname), 0, 0, 0, 0)
157-
return CFRef(ret)
149+
if ret == 0 {
150+
return 0, OSStatus{"SecPolicyCreateSSL", int32(ret)}
151+
}
152+
return CFRef(ret), nil
158153
}
159154
func x509_SecPolicyCreateSSL_trampoline()
160155

@@ -220,9 +215,12 @@ func x509_SecTrustGetCertificateCount_trampoline()
220215

221216
//go:cgo_import_dynamic x509_SecTrustGetCertificateAtIndex SecTrustGetCertificateAtIndex "/System/Library/Frameworks/Security.framework/Versions/A/Security"
222217

223-
func SecTrustGetCertificateAtIndex(trustObj CFRef, i int) CFRef {
218+
func SecTrustGetCertificateAtIndex(trustObj CFRef, i int) (CFRef, error) {
224219
ret := syscall(abi.FuncPCABI0(x509_SecTrustGetCertificateAtIndex_trampoline), uintptr(trustObj), uintptr(i), 0, 0, 0, 0)
225-
return CFRef(ret)
220+
if ret == 0 {
221+
return 0, OSStatus{"SecTrustGetCertificateAtIndex", int32(ret)}
222+
}
223+
return CFRef(ret), nil
226224
}
227225
func x509_SecTrustGetCertificateAtIndex_trampoline()
228226

src/crypto/x509/internal/macos/security.s

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ TEXT ·x509_SecTrustSettingsCopyCertificates_trampoline(SB),NOSPLIT,$0-0
1313
JMP x509_SecTrustSettingsCopyCertificates(SB)
1414
TEXT ·x509_SecTrustSettingsCopyTrustSettings_trampoline(SB),NOSPLIT,$0-0
1515
JMP x509_SecTrustSettingsCopyTrustSettings(SB)
16-
TEXT ·x509_SecPolicyCopyProperties_trampoline(SB),NOSPLIT,$0-0
17-
JMP x509_SecPolicyCopyProperties(SB)
1816
TEXT ·x509_SecTrustCreateWithCertificates_trampoline(SB),NOSPLIT,$0-0
1917
JMP x509_SecTrustCreateWithCertificates(SB)
2018
TEXT ·x509_SecCertificateCreateWithData_trampoline(SB),NOSPLIT,$0-0

src/crypto/x509/root_darwin.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate
3232

3333
policies := macOS.CFArrayCreateMutable()
3434
defer macOS.ReleaseCFArray(policies)
35-
sslPolicy := macOS.SecPolicyCreateSSL(opts.DNSName)
35+
sslPolicy, err := macOS.SecPolicyCreateSSL(opts.DNSName)
36+
if err != nil {
37+
return nil, err
38+
}
3639
macOS.CFArrayAppendValue(policies, sslPolicy)
3740

3841
trustObj, err := macOS.SecTrustCreateWithCertificates(certs, policies)
@@ -61,7 +64,10 @@ func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate
6164
chain := [][]*Certificate{{}}
6265
numCerts := macOS.SecTrustGetCertificateCount(trustObj)
6366
for i := 0; i < numCerts; i++ {
64-
certRef := macOS.SecTrustGetCertificateAtIndex(trustObj, i)
67+
certRef, err := macOS.SecTrustGetCertificateAtIndex(trustObj, i)
68+
if err != nil {
69+
return nil, err
70+
}
6571
cert, err := exportCertificate(certRef)
6672
if err != nil {
6773
return nil, err

0 commit comments

Comments
 (0)