@@ -77,26 +77,29 @@ func defaultConfig(ops ...func(*tls.Config)) *tls.Config {
7777}
7878
7979// certPool returns an X.509 certificate pool from `caFile`, the certificate file.
80- func certPool (caFile string , exclusivePool bool ) (* x509.CertPool , error ) {
80+ func certPool (opts Options ) (* x509.CertPool , error ) {
8181 // If we should verify the server, we need to load a trusted ca
8282 var (
8383 pool * x509.CertPool
8484 err error
8585 )
86- if exclusivePool {
86+ if opts . ExclusiveRootPools {
8787 pool = x509 .NewCertPool ()
8888 } else {
8989 pool , err = x509 .SystemCertPool ()
9090 if err != nil {
9191 return nil , fmt .Errorf ("failed to read system certificates: %v" , err )
9292 }
9393 }
94- pemData , err := os .ReadFile (caFile )
94+ if opts .CAFile == "" {
95+ return pool , nil
96+ }
97+ pemData , err := os .ReadFile (opts .CAFile )
9598 if err != nil {
96- return nil , fmt .Errorf ("could not read CA certificate %q: %v" , caFile , err )
99+ return nil , fmt .Errorf ("could not read CA certificate %q: %v" , opts . CAFile , err )
97100 }
98101 if ! pool .AppendCertsFromPEM (pemData ) {
99- return nil , fmt .Errorf ("failed to append certificates from PEM file: %q" , caFile )
102+ return nil , fmt .Errorf ("failed to append certificates from PEM file: %q" , opts . CAFile )
100103 }
101104 return pool , nil
102105}
@@ -199,7 +202,7 @@ func Client(options Options) (*tls.Config, error) {
199202 tlsConfig := defaultConfig ()
200203 tlsConfig .InsecureSkipVerify = options .InsecureSkipVerify
201204 if ! options .InsecureSkipVerify && options .CAFile != "" {
202- CAs , err := certPool (options . CAFile , options . ExclusiveRootPools )
205+ CAs , err := certPool (options )
203206 if err != nil {
204207 return nil , err
205208 }
@@ -232,7 +235,7 @@ func Server(options Options) (*tls.Config, error) {
232235 }
233236 tlsConfig .Certificates = []tls.Certificate {tlsCert }
234237 if options .ClientAuth >= tls .VerifyClientCertIfGiven && options .CAFile != "" {
235- CAs , err := certPool (options . CAFile , options . ExclusiveRootPools )
238+ CAs , err := certPool (options )
236239 if err != nil {
237240 return nil , err
238241 }
0 commit comments