forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls.go
149 lines (125 loc) · 3.7 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) quickfixengine.org All rights reserved.
//
// This file may be distributed under the terms of the quickfixengine.org
// license as defined by quickfixengine.org and appearing in the file
// LICENSE included in the packaging of this file.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE.
//
// See http://www.quickfixengine.org/LICENSE for licensing information.
//
// Contact ask@quickfixengine.org if any conditions of this licensing
// are not clear to you.
package quickfix
import (
"crypto/tls"
"crypto/x509"
"fmt"
"os"
"github.com/quickfixgo/quickfix/config"
)
func loadTLSConfig(settings *SessionSettings) (tlsConfig *tls.Config, err error) {
allowSkipClientCerts := false
if settings.HasSetting(config.SocketUseSSL) {
allowSkipClientCerts, err = settings.BoolSetting(config.SocketUseSSL)
if err != nil {
return
}
}
var serverName string
if settings.HasSetting(config.SocketServerName) {
serverName, err = settings.Setting(config.SocketServerName)
if err != nil {
return
}
}
insecureSkipVerify := false
if settings.HasSetting(config.SocketInsecureSkipVerify) {
insecureSkipVerify, err = settings.BoolSetting(config.SocketInsecureSkipVerify)
if err != nil {
return
}
}
if !settings.HasSetting(config.SocketPrivateKeyFile) && !settings.HasSetting(config.SocketCertificateFile) {
if !allowSkipClientCerts {
return
}
}
tlsConfig = defaultTLSConfig()
tlsConfig.ServerName = serverName
tlsConfig.InsecureSkipVerify = insecureSkipVerify
setMinVersionExplicit(settings, tlsConfig)
if settings.HasSetting(config.SocketPrivateKeyFile) || settings.HasSetting(config.SocketCertificateFile) {
var privateKeyFile string
var certificateFile string
privateKeyFile, err = settings.Setting(config.SocketPrivateKeyFile)
if err != nil {
return
}
certificateFile, err = settings.Setting(config.SocketCertificateFile)
if err != nil {
return
}
tlsConfig.Certificates = make([]tls.Certificate, 1)
if tlsConfig.Certificates[0], err = tls.LoadX509KeyPair(certificateFile, privateKeyFile); err != nil {
return
}
}
if !allowSkipClientCerts {
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
}
if !settings.HasSetting(config.SocketCAFile) {
return
}
caFile, err := settings.Setting(config.SocketCAFile)
if err != nil {
return
}
pem, err := os.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
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,
},
}
}
func setMinVersionExplicit(settings *SessionSettings, tlsConfig *tls.Config) {
if settings.HasSetting(config.SocketMinimumTLSVersion) {
minVersion, err := settings.Setting(config.SocketMinimumTLSVersion)
if err != nil {
return
}
switch minVersion {
case "SSL30":
//nolint:staticcheck // SA1019 min version ok
tlsConfig.MinVersion = tls.VersionSSL30
case "TLS10":
tlsConfig.MinVersion = tls.VersionTLS10
case "TLS11":
tlsConfig.MinVersion = tls.VersionTLS11
case "TLS12":
tlsConfig.MinVersion = tls.VersionTLS12
}
}
}