Skip to content

Commit c9a10d4

Browse files
crypto/x509: return typed verification errors on macOS
On macOS return the error code from SecTrustEvaluateWithError, and use it to create typed errors that can be returned from Verify. Fixes #56891 Change-Id: Ib597ce202abb60702f730e75da583894422e4c14 Reviewed-on: https://go-review.googlesource.com/c/go/+/452620 Run-TryBot: Roland Shoemaker <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 2321abc commit c9a10d4

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ func CFErrorCopyDescription(errRef CFRef) CFRef {
186186
}
187187
func x509_CFErrorCopyDescription_trampoline()
188188

189+
//go:cgo_import_dynamic x509_CFErrorGetCode CFErrorGetCode "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation"
190+
191+
func CFErrorGetCode(errRef CFRef) int {
192+
return int(syscall(abi.FuncPCABI0(x509_CFErrorGetCode_trampoline), uintptr(errRef), 0, 0, 0, 0, 0))
193+
}
194+
func x509_CFErrorGetCode_trampoline()
195+
189196
//go:cgo_import_dynamic x509_CFStringCreateExternalRepresentation CFStringCreateExternalRepresentation "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation"
190197

191198
func CFStringCreateExternalRepresentation(strRef CFRef) (CFRef, error) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ TEXT ·x509_CFDataCreate_trampoline(SB),NOSPLIT,$0-0
3737
JMP x509_CFDataCreate(SB)
3838
TEXT ·x509_CFErrorCopyDescription_trampoline(SB),NOSPLIT,$0-0
3939
JMP x509_CFErrorCopyDescription(SB)
40+
TEXT ·x509_CFErrorGetCode_trampoline(SB),NOSPLIT,$0-0
41+
JMP x509_CFErrorGetCode(SB)
4042
TEXT ·x509_CFStringCreateExternalRepresentation_trampoline(SB),NOSPLIT,$0-0
4143
JMP x509_CFStringCreateExternalRepresentation(SB)

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package macOS
88

99
import (
1010
"errors"
11-
"fmt"
1211
"internal/abi"
1312
"strconv"
1413
"unsafe"
@@ -52,6 +51,15 @@ const (
5251
SecTrustSettingsDomainSystem
5352
)
5453

54+
const (
55+
// various macOS error codes that can be returned from
56+
// SecTrustEvaluateWithError that we can map to Go cert
57+
// verification error types.
58+
ErrSecCertificateExpired = -67818
59+
ErrSecHostNameMismatch = -67602
60+
ErrSecNotTrusted = -67843
61+
)
62+
5563
type OSStatus struct {
5664
call string
5765
status int32
@@ -191,17 +199,18 @@ func x509_SecTrustGetResult_trampoline()
191199

192200
//go:cgo_import_dynamic x509_SecTrustEvaluateWithError SecTrustEvaluateWithError "/System/Library/Frameworks/Security.framework/Versions/A/Security"
193201

194-
func SecTrustEvaluateWithError(trustObj CFRef) error {
202+
func SecTrustEvaluateWithError(trustObj CFRef) (int, error) {
195203
var errRef CFRef
196204
ret := syscall(abi.FuncPCABI0(x509_SecTrustEvaluateWithError_trampoline), uintptr(trustObj), uintptr(unsafe.Pointer(&errRef)), 0, 0, 0, 0)
197205
if int32(ret) != 1 {
198206
errStr := CFErrorCopyDescription(errRef)
199-
err := fmt.Errorf("x509: %s", CFStringToString(errStr))
207+
err := errors.New(CFStringToString(errStr))
208+
errCode := CFErrorGetCode(errRef)
200209
CFRelease(errRef)
201210
CFRelease(errStr)
202-
return err
211+
return errCode, err
203212
}
204-
return nil
213+
return 0, nil
205214
}
206215
func x509_SecTrustEvaluateWithError_trampoline()
207216

src/crypto/x509/root_darwin.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package x509
77
import (
88
macOS "crypto/x509/internal/macos"
99
"errors"
10+
"fmt"
1011
)
1112

1213
func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate, err error) {
@@ -57,8 +58,17 @@ func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate
5758
// always enforce its SCT requirements, and there are still _some_ people
5859
// using TLS or OCSP for that.
5960

60-
if err := macOS.SecTrustEvaluateWithError(trustObj); err != nil {
61-
return nil, err
61+
if ret, err := macOS.SecTrustEvaluateWithError(trustObj); err != nil {
62+
switch ret {
63+
case macOS.ErrSecCertificateExpired:
64+
return nil, CertificateInvalidError{c, Expired, err.Error()}
65+
case macOS.ErrSecHostNameMismatch:
66+
return nil, HostnameError{c, opts.DNSName}
67+
case macOS.ErrSecNotTrusted:
68+
return nil, UnknownAuthorityError{Cert: c}
69+
default:
70+
return nil, fmt.Errorf("x509: %s", err)
71+
}
6272
}
6373

6474
chain := [][]*Certificate{{}}

src/crypto/x509/root_darwin_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,23 @@ func TestPlatformVerifier(t *testing.T) {
4242
{
4343
name: "expired leaf",
4444
host: "expired.badssl.com",
45-
expectedErr: "x509: “*.badssl.com” certificate is expired",
45+
expectedErr: "x509: certificate has expired or is not yet valid: “*.badssl.com” certificate is expired",
4646
},
4747
{
4848
name: "wrong host for leaf",
4949
host: "wrong.host.badssl.com",
5050
verifyName: "wrong.host.badssl.com",
51-
expectedErr: "x509: *.badssl.com” certificate name does not match input",
51+
expectedErr: "x509: certificate is valid for *.badssl.com, badssl.com, not wrong.host.badssl.com",
5252
},
5353
{
5454
name: "self-signed leaf",
5555
host: "self-signed.badssl.com",
56-
expectedErr: "x509: “*.badssl.com” certificate is not trusted",
56+
expectedErr: "x509: certificate signed by unknown authority",
5757
},
5858
{
5959
name: "untrusted root",
6060
host: "untrusted-root.badssl.com",
61-
expectedErr: "x509: “BadSSL Untrusted Root Certificate Authority” certificate is not trusted",
61+
expectedErr: "x509: certificate signed by unknown authority",
6262
},
6363
{
6464
name: "revoked leaf",
@@ -74,7 +74,7 @@ func TestPlatformVerifier(t *testing.T) {
7474
name: "expired leaf (custom time)",
7575
host: "google.com",
7676
verifyTime: time.Time{}.Add(time.Hour),
77-
expectedErr: "x509: “*.google.com” certificate is expired",
77+
expectedErr: "x509: certificate has expired or is not yet valid: “*.google.com” certificate is expired",
7878
},
7979
{
8080
name: "valid chain (custom time)",

0 commit comments

Comments
 (0)