forked from chrislusf/glow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cert_files.go
67 lines (57 loc) · 1.54 KB
/
cert_files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package netchan
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"github.com/chrislusf/glow/util"
)
type CertFiles struct {
CertFile string // A PEM eoncoded certificate file.
KeyFile string // A PEM encoded private key file.
CaFile string // A PEM eoncoded CA's certificate file.
}
func (c *CertFiles) IsEnabled() bool {
return c.CaFile != ""
}
func (c *CertFiles) MakeTLSConfig() *tls.Config {
if !c.IsEnabled() {
return nil
}
certFile, err := filepath.Abs(util.CleanPath(c.CertFile))
if err != nil {
panic(fmt.Errorf("Failed to load cert file %s: %v", c.CertFile, err))
}
keyFile, err := filepath.Abs(util.CleanPath(c.KeyFile))
if err != nil {
panic(fmt.Errorf("Failed to load cert file %s: %v", c.KeyFile, err))
}
caFile, err := filepath.Abs(util.CleanPath(c.CaFile))
if err != nil {
panic(fmt.Errorf("Failed to load cert file %s: %v", c.CaFile, err))
}
// Load cert
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
log.Fatal(err)
}
// Load CA cert
caCert, err := ioutil.ReadFile(caFile)
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Create tls config
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert, // server side setting
InsecureSkipVerify: false, // client side setting
}
tlsConfig.BuildNameToCertificate()
return tlsConfig
}