This repository has been archived on 2024-04-07. You can view files and clone it, but cannot push or open issues or pull requests.
go-monero/pkg/http/tls.go
Ciro S. Costa 096ad758c2 source formatting
no behavior changes

Signed-off-by: Ciro S. Costa <utxobr@protonmail.com>
2021-07-18 06:51:24 -04:00

46 lines
899 B
Go

package http
import (
"crypto/tls"
"crypto/x509"
"fmt"
"os"
)
func WithCACert(fpath string) func(*tls.Config) error {
return func(config *tls.Config) error {
certBytes, err := os.ReadFile(fpath)
if err != nil {
return fmt.Errorf("read file '%s': %w", fpath, err)
}
pool := x509.NewCertPool()
if ok := pool.AppendCertsFromPEM(certBytes); !ok {
return fmt.Errorf("ca cert '%s' not valid", fpath)
}
config.RootCAs = pool
return nil
}
}
func WithInsecureSkipVerify() func(*tls.Config) {
return func(config *tls.Config) {
config.InsecureSkipVerify = true
}
}
func WithClientCertificate(cert, key string) func(*tls.Config) error {
return func(config *tls.Config) error {
keypair, err := tls.LoadX509KeyPair(cert, key)
if err != nil {
return fmt.Errorf("load x509 key pair: %w", err)
}
config.Certificates = []tls.Certificate{keypair}
return nil
}
}