forked from cloudflare/cfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls_handshake.go
344 lines (307 loc) · 9.87 KB
/
tls_handshake.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package scan
import (
"bytes"
"errors"
"fmt"
"net"
"strings"
"github.com/cloudflare/cf-tls/tls"
"github.com/cloudflare/cfssl/helpers"
)
// Sentinel for failures in sayHello. Should always be caught.
var errHelloFailed = errors.New("Handshake failed in sayHello")
// TLSHandshake contains scanners testing host cipher suite negotiation
var TLSHandshake = &Family{
Description: "Scans for host's SSL/TLS version and cipher suite negotiation",
Scanners: map[string]*Scanner{
"CipherSuite": {
"Determines host's cipher suites accepted and prefered order",
cipherSuiteScan,
},
"SigAlgs": {
"Determines host's accepted signature and hash algorithms",
sigAlgsScan,
},
"CertsBySigAlgs": {
"Determines host's certificate signature algorithm matching client's accepted signature and hash algorithms",
certSigAlgsScan,
},
"CertsByCiphers": {
"Determines host's certificate signature algorithm matching client's accepted ciphers",
certSigAlgsScanByCipher,
},
},
}
func getCipherIndex(ciphers []uint16, serverCipher uint16) (cipherIndex int, err error) {
//func getCipherIndex(ciphers []uint16, serverCipher uint16) (cipherIndex int, err error) {
// fmt.Println(serverCipher, ciphers)
var cipherID uint16
for cipherIndex, cipherID = range ciphers {
if serverCipher == cipherID {
return
}
}
err = fmt.Errorf("server negotiated ciphersuite we didn't send: %s", tls.CipherSuites[serverCipher])
return
}
func getCurveIndex(curves []tls.CurveID, serverCurve tls.CurveID) (curveIndex int, err error) {
var curveID tls.CurveID
for curveIndex, curveID = range curves {
if serverCurve == curveID {
return
}
}
err = fmt.Errorf("server negotiated elliptic curve we didn't send: %s", tls.Curves[serverCurve])
return
}
func sayHello(host string, ciphers []uint16, curves []tls.CurveID, vers uint16, sigAlgs []tls.SignatureAndHash) (cipherIndex, curveIndex int, certs [][]byte, err error) {
tcpConn, err := net.Dial(Network, host)
if err != nil {
return
}
config := defaultTLSConfig(host)
config.MinVersion = vers
config.MaxVersion = vers
if ciphers == nil {
ciphers = allCiphersIDs()
}
config.CipherSuites = ciphers
if curves == nil {
curves = allCurvesIDs()
}
config.CurvePreferences = curves
if sigAlgs == nil {
sigAlgs = tls.AllSignatureAndHashAlgorithms
}
tls.SetSupportedSKXSignatureAlgorithms(sigAlgs)
defer tls.ResetSupportedSKXSignatureAlgorithms()
conn := tls.Client(tcpConn, config)
serverCipher, serverCurveType, serverCurve, serverVersion, certificates, err := conn.SayHello()
certs = certificates
conn.Close()
if err != nil {
err = errHelloFailed
return
}
if serverVersion != vers {
err = fmt.Errorf("server negotiated protocol version we didn't send: %s", tls.Versions[serverVersion])
return
}
cipherIndex, err = getCipherIndex(ciphers, serverCipher)
if tls.CipherSuites[serverCipher].EllipticCurve {
if curves == nil {
curves = allCurvesIDs()
}
if serverCurveType != 3 {
err = fmt.Errorf("server negotiated non-named ECDH parameters; we didn't analyze them. Server curve type: %d", serverCurveType)
}
curveIndex, err = getCurveIndex(curves, serverCurve)
}
return
}
func allCiphersIDs() []uint16 {
ciphers := make([]uint16, 0, len(tls.CipherSuites))
for cipherID := range tls.CipherSuites {
ciphers = append(ciphers, cipherID)
}
return ciphers
}
func allCurvesIDs() []tls.CurveID {
curves := make([]tls.CurveID, 0, len(tls.Curves))
for curveID := range tls.Curves {
// No unassigned or explicit curves in the scan, per http://tools.ietf.org/html/rfc4492#section-5.4
if curveID == 0 || curveID == 65281 || curveID == 65282 {
continue
} else {
curves = append(curves, curveID)
}
}
return curves
}
type cipherDatum struct {
versionID uint16
curves []tls.CurveID
}
// cipherVersions contains lists of host's supported cipher suites based on SSL/TLS Version.
// If a cipher suite uses ECC, also contains a list of supported curves by SSL/TLS Version.
type cipherVersions struct {
cipherID uint16
data []cipherDatum
}
type cipherVersionList []cipherVersions
func (cvList cipherVersionList) String() string {
cvStrings := make([]string, len(cvList))
for i, c := range cvList {
versStrings := make([]string, len(c.data))
for j, d := range c.data {
curveStrings := make([]string, len(d.curves))
for k, c := range d.curves {
curveStrings[k] = tls.Curves[c]
}
versStrings[j] = fmt.Sprintf("%s: [ %s ]", tls.Versions[d.versionID], strings.Join(curveStrings, ","))
}
cvStrings[i] = fmt.Sprintf("%s\t%s", tls.CipherSuites[c.cipherID], strings.Join(versStrings, ","))
}
return strings.Join(cvStrings, "\n")
}
func (cvList cipherVersionList) MarshalJSON() ([]byte, error) {
b := new(bytes.Buffer)
cvStrs := make([]string, len(cvList))
for i, cv := range cvList {
versStrings := make([]string, len(cv.data))
for j, d := range cv.data {
curveStrings := make([]string, len(d.curves))
if len(d.curves) > 0 {
for k, c := range d.curves {
curveStrings[k] = fmt.Sprintf("\"%s\"", tls.Curves[c])
}
versStrings[j] = fmt.Sprintf("{\"%s\":[%s]}", tls.Versions[d.versionID], strings.Join(curveStrings, ","))
} else {
versStrings[j] = fmt.Sprintf("\"%s\"", tls.Versions[d.versionID])
}
}
cvStrs[i] = fmt.Sprintf("{\"%s\":[%s]}", tls.CipherSuites[cv.cipherID].String(), strings.Join(versStrings, ","))
}
fmt.Fprintf(b, "[%s]", strings.Join(cvStrs, ","))
return b.Bytes(), nil
}
func doCurveScan(host string, vers, cipherID uint16, ciphers []uint16) (supportedCurves []tls.CurveID, err error) {
allCurves := allCurvesIDs()
curves := make([]tls.CurveID, len(allCurves))
copy(curves, allCurves)
for len(curves) > 0 {
var curveIndex int
_, curveIndex, _, err = sayHello(host, []uint16{cipherID}, curves, vers, nil)
if err != nil {
// This case is expected, because eventually we ask only for curves the server doesn't support
if err == errHelloFailed {
err = nil
break
}
return
}
curveID := curves[curveIndex]
supportedCurves = append(supportedCurves, curveID)
curves = append(curves[:curveIndex], curves[curveIndex+1:]...)
}
return
}
// cipherSuiteScan returns, by TLS Version, the sort list of cipher suites
// supported by the host
func cipherSuiteScan(host string) (grade Grade, output Output, err error) {
var cvList cipherVersionList
allCiphers := allCiphersIDs()
var vers uint16
for vers = tls.VersionTLS12; vers >= tls.VersionSSL30; vers-- {
ciphers := make([]uint16, len(allCiphers))
copy(ciphers, allCiphers)
for len(ciphers) > 0 {
var cipherIndex int
cipherIndex, _, _, err = sayHello(host, ciphers, nil, vers, nil)
if err != nil {
if err == errHelloFailed {
err = nil
break
}
return
}
if vers == tls.VersionSSL30 {
grade = Warning
}
cipherID := ciphers[cipherIndex]
// If this is an EC cipher suite, do a second scan for curve support
var supportedCurves []tls.CurveID
if tls.CipherSuites[cipherID].EllipticCurve {
supportedCurves, err = doCurveScan(host, vers, cipherID, ciphers)
if len(supportedCurves) == 0 {
err = errors.New("couldn't negotiate any curves")
}
}
for i, c := range cvList {
if cipherID == c.cipherID {
cvList[i].data = append(c.data, cipherDatum{vers, supportedCurves})
goto exists
}
}
cvList = append(cvList, cipherVersions{cipherID, []cipherDatum{cipherDatum{vers, supportedCurves}}})
exists:
ciphers = append(ciphers[:cipherIndex], ciphers[cipherIndex+1:]...)
}
}
if len(cvList) == 0 {
err = errors.New("couldn't negotiate any cipher suites")
return
}
if grade != Warning {
grade = Good
}
output = cvList
return
}
// sigAlgsScan returns the accepted signature and hash algorithms of the host
func sigAlgsScan(host string) (grade Grade, output Output, err error) {
var supportedSigAlgs []tls.SignatureAndHash
for _, sigAlg := range tls.AllSignatureAndHashAlgorithms {
_, _, _, e := sayHello(host, nil, nil, tls.VersionTLS12, []tls.SignatureAndHash{sigAlg})
if e == nil {
supportedSigAlgs = append(supportedSigAlgs, sigAlg)
}
}
if len(supportedSigAlgs) > 0 {
grade = Good
output = supportedSigAlgs
} else {
err = errors.New("no SigAlgs supported")
}
return
}
// certSigAlgScan returns the server certificate with various sigature and hash algorithms in the ClientHello
func certSigAlgsScan(host string) (grade Grade, output Output, err error) {
var certSigAlgs = make(map[string]string)
for _, sigAlg := range tls.AllSignatureAndHashAlgorithms {
_, _, derCerts, e := sayHello(host, nil, nil, tls.VersionTLS12, []tls.SignatureAndHash{sigAlg})
if e == nil {
if len(derCerts) == 0 {
return Bad, nil, errors.New("no certs returned")
}
certs, _, err := helpers.ParseCertificatesDER(derCerts[0], "")
if err != nil {
return Bad, nil, err
}
certSigAlgs[sigAlg.String()] = helpers.SignatureString(certs[0].SignatureAlgorithm)
//certSigAlgs = append(certSigAlgs, certs[0].SignatureAlgorithm)
}
}
if len(certSigAlgs) > 0 {
grade = Good
output = certSigAlgs
} else {
err = errors.New("no SigAlgs supported")
}
return
}
// certSigAlgScan returns the server certificate with various ciphers in the ClientHello
func certSigAlgsScanByCipher(host string) (grade Grade, output Output, err error) {
var certSigAlgs = make(map[string]string)
for cipherID := range tls.CipherSuites {
_, _, derCerts, e := sayHello(host, []uint16{cipherID}, nil, tls.VersionTLS12, []tls.SignatureAndHash{})
if e == nil {
if len(derCerts) == 0 {
return Bad, nil, errors.New("no certs returned")
}
certs, _, err := helpers.ParseCertificatesDER(derCerts[0], "")
if err != nil {
return Bad, nil, err
}
certSigAlgs[tls.CipherSuites[cipherID].Name] = helpers.SignatureString(certs[0].SignatureAlgorithm)
//certSigAlgs = append(certSigAlgs, certs[0].SignatureAlgorithm)
}
}
if len(certSigAlgs) > 0 {
grade = Good
output = certSigAlgs
} else {
err = errors.New("no cipher supported")
}
return
}