Skip to content

Commit

Permalink
Update xrpc tls
Browse files Browse the repository at this point in the history
  • Loading branch information
onanying committed Jun 30, 2023
1 parent 187bb18 commit 1fd038c
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/xrpc/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
)

// LoadTLSConfig This is self-signed TLS
// Normal TLS use credentials.NewServerTLSFromFile
func LoadTLSConfig(caFile, certFile, keyFile string) (*tls.Config, error) {
caPEMBlock, err := os.ReadFile(caFile)
if err != nil {
Expand All @@ -23,6 +25,7 @@ func LoadTLSConfig(caFile, certFile, keyFile string) (*tls.Config, error) {
return NewTLSConfig(caPEMBlock, certPEMBlock, keyPEMBlock)
}

// NewTLSConfig This is self-signed TLS
func NewTLSConfig(ca, cert, key []byte) (*tls.Config, error) {
certificate, err := tls.X509KeyPair(cert, key)
if err != nil {
Expand All @@ -39,6 +42,8 @@ func NewTLSConfig(ca, cert, key []byte) (*tls.Config, error) {
}, nil
}

// LoadTLSClientConfig This is self-signed TLS
// Normal TLS use credentials.NewClientTLSFromFile
func LoadTLSClientConfig(caFile, certFile, keyFile string) (*tls.Config, error) {
caPEMBlock, err := os.ReadFile(caFile)
if err != nil {
Expand All @@ -55,6 +60,7 @@ func LoadTLSClientConfig(caFile, certFile, keyFile string) (*tls.Config, error)
return NewTLSClientConfig(caPEMBlock, certPEMBlock, keyPEMBlock)
}

// NewTLSClientConfig This is self-signed TLS
func NewTLSClientConfig(ca, cert, key []byte) (*tls.Config, error) {
certificate, err := tls.X509KeyPair(cert, key)
if err != nil {
Expand All @@ -66,6 +72,7 @@ func NewTLSClientConfig(ca, cert, key []byte) (*tls.Config, error) {
}
return &tls.Config{
Certificates: []tls.Certificate{certificate},
ServerName: "",
RootCAs: caCertPool,
}, nil
}

0 comments on commit 1fd038c

Please sign in to comment.