mirror of
https://github.com/fatedier/frp.git
synced 2026-10-18 21:13:28 +08:00
feat(client): add useSystemCa and combine it with trustedCaFile (#5572)
Move the frpc-only TLS option into the shared TLSConfig as transport.tls.useSystemCa, replacing the unpublished useSystemRoots. When set, frpc verifies the server with the system CAs; when trustedCaFile is also set, its CAs are appended to the system pool so both are trusted. Failures to load either source abort the connection instead of falling back. Enabling the option without effective TLS is a configuration error, and the field is rejected in frps transport.tls and in both webServer.tls sections. Fixes #5545
This commit is contained in:
+41
-9
@@ -80,17 +80,23 @@ func newRandomTLSKeyPair() (*tls.Certificate, error) {
|
||||
// Only support one ca file to add
|
||||
func newCertPool(caPath string) (*x509.CertPool, error) {
|
||||
pool := x509.NewCertPool()
|
||||
if err := appendCertsFromFile(pool, caPath); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
func appendCertsFromFile(pool *x509.CertPool, caPath string) error {
|
||||
caCrt, err := os.ReadFile(caPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
if !pool.AppendCertsFromPEM(caCrt) {
|
||||
return nil, fmt.Errorf("failed to parse CA certificate from file %q: no valid PEM certificates found", caPath)
|
||||
return fmt.Errorf("failed to parse CA certificate from file %q: no valid PEM certificates found", caPath)
|
||||
}
|
||||
|
||||
return pool, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewServerTLSConfig(certPath, keyPath, caPath string) (*tls.Config, error) {
|
||||
@@ -126,13 +132,21 @@ func NewServerTLSConfig(certPath, keyPath, caPath string) (*tls.Config, error) {
|
||||
}
|
||||
|
||||
func NewClientTLSConfig(certPath, keyPath, caPath, serverName string) (*tls.Config, error) {
|
||||
return NewClientTLSConfigWithSystemRoots(certPath, keyPath, caPath, serverName, false)
|
||||
return NewClientTLSConfigWithSystemCA(certPath, keyPath, caPath, serverName, false)
|
||||
}
|
||||
|
||||
// NewClientTLSConfigWithSystemRoots behaves like NewClientTLSConfig, except that when
|
||||
// caPath is empty and useSystemRoots is true, the server certificate is verified against
|
||||
// the host's root store.
|
||||
func NewClientTLSConfigWithSystemRoots(certPath, keyPath, caPath, serverName string, useSystemRoots bool) (*tls.Config, error) {
|
||||
// NewClientTLSConfigWithSystemCA enables verification against system CAs when
|
||||
// useSystemCa is true, adding certificates from caPath if it is also set.
|
||||
// Without either trust source, server certificate verification remains disabled.
|
||||
func NewClientTLSConfigWithSystemCA(certPath, keyPath, caPath, serverName string, useSystemCa bool) (*tls.Config, error) {
|
||||
return newClientTLSConfig(certPath, keyPath, caPath, serverName, useSystemCa, x509.SystemCertPool)
|
||||
}
|
||||
|
||||
func newClientTLSConfig(
|
||||
certPath, keyPath, caPath, serverName string,
|
||||
useSystemCa bool,
|
||||
systemCertPool func() (*x509.CertPool, error),
|
||||
) (*tls.Config, error) {
|
||||
base := &tls.Config{}
|
||||
|
||||
if certPath != "" && keyPath != "" {
|
||||
@@ -147,6 +161,24 @@ func NewClientTLSConfigWithSystemRoots(certPath, keyPath, caPath, serverName str
|
||||
base.ServerName = serverName
|
||||
|
||||
switch {
|
||||
case caPath != "" && useSystemCa:
|
||||
// SystemCertPool returns a copy, so appending does not change the system pool.
|
||||
// Design basis: Go 1.26.7 crypto/x509/verify.go:559-573 first uses the
|
||||
// Windows/macOS verifier, then falls back to Go verification using only the
|
||||
// added CAs if platform verification fails. This platform behavior has not been
|
||||
// tested here; preserve the system pool instead of copying its subjects.
|
||||
pool, err := systemCertPool()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load system CA pool: %w", err)
|
||||
}
|
||||
if pool == nil {
|
||||
return nil, fmt.Errorf("failed to load system CA pool: no pool returned")
|
||||
}
|
||||
if err := appendCertsFromFile(pool, caPath); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
base.RootCAs = pool
|
||||
base.InsecureSkipVerify = false
|
||||
case caPath != "":
|
||||
pool, err := newCertPool(caPath)
|
||||
if err != nil {
|
||||
@@ -155,7 +187,7 @@ func NewClientTLSConfigWithSystemRoots(certPath, keyPath, caPath, serverName str
|
||||
|
||||
base.RootCAs = pool
|
||||
base.InsecureSkipVerify = false
|
||||
case useSystemRoots:
|
||||
case useSystemCa:
|
||||
// A nil RootCAs makes crypto/tls use the host root store, through the
|
||||
// platform verifier on macOS and Windows.
|
||||
base.InsecureSkipVerify = false
|
||||
|
||||
Reference in New Issue
Block a user