forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.go
74 lines (60 loc) · 1.67 KB
/
tls.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
68
69
70
71
72
73
74
package quickfix
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"github.com/quickfixgo/quickfix/config"
)
func loadTLSConfig(settings *SessionSettings) (tlsConfig *tls.Config, err error) {
if !settings.HasSetting(config.SocketPrivateKeyFile) && !settings.HasSetting(config.SocketCertificateFile) {
return
}
privateKeyFile, err := settings.Setting(config.SocketPrivateKeyFile)
if err != nil {
return
}
certificateFile, err := settings.Setting(config.SocketCertificateFile)
if err != nil {
return
}
tlsConfig = defaultTLSConfig()
tlsConfig.Certificates = make([]tls.Certificate, 1)
if tlsConfig.Certificates[0], err = tls.LoadX509KeyPair(certificateFile, privateKeyFile); err != nil {
return
}
if !settings.HasSetting(config.SocketCAFile) {
return
}
caFile, err := settings.Setting(config.SocketCAFile)
if err != nil {
return
}
pem, err := ioutil.ReadFile(caFile)
if err != nil {
return
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(pem) {
err = fmt.Errorf("Failed to parse %v", caFile)
return
}
tlsConfig.RootCAs = certPool
tlsConfig.ClientCAs = certPool
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
return
}
//defaultTLSConfig brought to you by https://github.com/gtank/cryptopasta/
func defaultTLSConfig() *tls.Config {
return &tls.Config{
// Avoids most of the memorably-named TLS attacks
MinVersion: tls.VersionTLS12,
// Causes servers to use Go's default ciphersuite preferences,
// which are tuned to avoid attacks. Does nothing on clients.
PreferServerCipherSuites: true,
// Only use curves which have constant-time implementations
CurvePreferences: []tls.CurveID{
tls.CurveP256,
},
}
}