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
|
// Queries the supported TLS versions from a server.
package main
import (
"errors"
"flag"
"fmt"
"net"
"strings"
"golang.org/x/crypto/cryptobyte"
)
const (
recordTypeAlert uint8 = 21
recordTypeHandshake uint8 = 22
handshakeTypeClientHello uint8 = 1
handshakeTypeServerHello uint8 = 2
handshakeTypeHelloRetryRequest uint8 = 6 // up to draft -21
versionTLS10 uint16 = 0x301
versionTLS11 uint16 = 0x302
versionTLS12 uint16 = 0x303
versionTLS13DraftXX uint16 = 0x7f00
versionTLS13Draft01 uint16 = versionTLS13DraftXX | 1
versionTLS13Draft21 uint16 = versionTLS13DraftXX | 21
versionTLS13Draft28 uint16 = versionTLS13DraftXX | 28
versionTLS13 uint16 = 0x304
TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
TLS_AES_128_GCM_SHA256 uint16 = 0x1301
TLS_AES_256_GCM_SHA384 uint16 = 0x1302
TLS_CHACHA20_POLY1305_SHA256 uint16 = 0x1303
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
extServerName uint16 = 0
extSupportedGroups uint16 = 10
extSignatureAlgorithms uint16 = 13
extKeySharePre23 uint16 = 40
extSupportedVersions uint16 = 43
extKeyShare uint16 = 51
curveSECP256r1 uint16 = 0x0017
curveX25519 uint16 = 0x001d
PKCS1WithSHA256 uint16 = 0x0401
PSSWithSHA256 uint16 = 0x0804
ECDSAWithP256AndSHA256 uint16 = 0x0403
)
func addExtension(b *cryptobyte.Builder, extType uint16, f cryptobyte.BuilderContinuation) {
b.AddUint16(extType)
b.AddUint16LengthPrefixed(f)
}
func buildClientHelloRecord(host string, minVersion, maxVersion uint16) ([]byte, error) {
var b cryptobyte.Builder
useTLS13x304 := maxVersion == versionTLS13
if useTLS13x304 {
maxVersion = versionTLS13Draft28
}
if minVersion > maxVersion {
panic("failed: minVersion <= maxVersion")
}
// Record header
b.AddUint8(recordTypeHandshake)
b.AddUint16(versionTLS10)
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
// Handshake header
b.AddUint8(handshakeTypeClientHello)
b.AddUint24LengthPrefixed(func(b *cryptobyte.Builder) {
// ClientHello
b.AddUint16(versionTLS12)
clientRandom := make([]byte, 32)
b.AddBytes(clientRandom)
b.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
sid := make([]byte, 32)
b.AddBytes(sid)
})
// Cipher suites
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
cipherSuites := []uint16{
// MTI TLS 1.3 suites
TLS_AES_128_GCM_SHA256,
TLS_AES_256_GCM_SHA384,
TLS_CHACHA20_POLY1305_SHA256,
// modern TLS 1.2 suites
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
// MTI TLS 1.2 suite
TLS_RSA_WITH_AES_128_CBC_SHA,
}
for _, cipherSuite := range cipherSuites {
b.AddUint16(cipherSuite)
}
})
// Add NULL compression
b.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddUint8(0)
})
// Extensions
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
addExtension(b, extSupportedVersions, func(b *cryptobyte.Builder) {
// Advertise all draft versions
b.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
if useTLS13x304 {
b.AddUint16(versionTLS13)
}
for i := maxVersion; i >= minVersion; i -= 1 {
b.AddUint16(i)
}
// if this is not added, TLS 1.3
// implementations that do not
// want to negotiate 1.3 fail
// even if 1.2 is acceptable.
b.AddUint16(versionTLS12)
})
})
addExtension(b, extServerName, func(b *cryptobyte.Builder) {
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddUint8(0)
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
sniHostName := []byte(host)
b.AddBytes(sniHostName)
})
})
})
addExtension(b, extKeySharePre23, func(b *cryptobyte.Builder) {
// empty client_shares
b.AddUint16(0)
})
addExtension(b, extKeyShare, func(b *cryptobyte.Builder) {
// empty client_shares
b.AddUint16(0)
})
addExtension(b, extSupportedGroups, func(b *cryptobyte.Builder) {
// Advertise MTI groups
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddUint16(curveSECP256r1)
b.AddUint16(curveX25519)
})
})
addExtension(b, extSignatureAlgorithms, func(b *cryptobyte.Builder) {
// Advertise MTI signature algorithms
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddUint16(PKCS1WithSHA256)
b.AddUint16(PSSWithSHA256)
b.AddUint16(ECDSAWithP256AndSHA256)
})
})
})
})
})
return b.Bytes()
}
func parseServerHelloHandshake(recordFragment cryptobyte.String) (uint16, error) {
var hsType uint8
var hs cryptobyte.String
if !recordFragment.ReadUint8(&hsType) ||
!recordFragment.ReadUint24LengthPrefixed(&hs) {
return 0, errors.New("bad hs msg")
}
switch hsType {
case handshakeTypeServerHello:
var version uint16
if !hs.ReadUint16(&version) {
return 0, errors.New("SH too short")
}
// pre-TLS 1.3 or up to draft -21
if version != versionTLS12 {
return version, nil
}
// from draft -21 and up, look for supported_versions extension
var sid cryptobyte.String
if !hs.Skip(32) ||
!hs.ReadUint8LengthPrefixed(&sid) ||
// skip cipher, comp method
!hs.Skip(2+1) {
return 0, errors.New("invalid SH")
}
// parse (in TLS 1.2 optional) SH extensions
if !hs.Empty() {
hs.Skip(2)
for !hs.Empty() {
var extType uint16
var extData cryptobyte.String
if !hs.ReadUint16(&extType) ||
!hs.ReadUint16LengthPrefixed(&extData) {
return 0, errors.New("Invalid extension")
}
if extType == extSupportedVersions {
if !extData.ReadUint16(&version) {
return 0, errors.New("Invalid SV extension")
}
// accept version
break
}
}
}
return version, nil
case handshakeTypeHelloRetryRequest: // draft -21 and before
var version uint16
if !hs.ReadUint16(&version) {
return 0, errors.New("HRR too short")
}
return version, nil
default:
return 0, errors.New("unexpected hs msg")
}
}
func parseServerHelloRecord(buffer []byte) (uint16, error) {
s := cryptobyte.String(buffer)
var recordType uint8
var recordVersion uint16
var recordFragment cryptobyte.String
if !s.ReadUint8(&recordType) ||
!s.ReadUint16(&recordVersion) ||
!s.Skip(2) {
return 0, errors.New("bad record")
}
// do not read whole record as it may contain multiple HS messages
recordFragment = s
switch recordType {
case recordTypeHandshake:
return parseServerHelloHandshake(recordFragment)
case recordTypeAlert:
var alertLevel, alertDescription uint8
if recordFragment.ReadUint8(&alertLevel) && recordFragment.ReadUint8(&alertDescription) {
alertLevelStr := "unknown"
switch alertLevel {
case 1:
alertLevelStr = "warning"
case 2:
alertLevelStr = "fatal"
}
return 0, fmt.Errorf("%s alert message - %d", alertLevelStr, alertDescription)
}
return 0, errors.New("unexpected alert message")
default:
return 0, errors.New("unexpected record type")
}
}
func versionToString(version uint16) string {
switch version {
case versionTLS10:
return "TLS 1.0"
case versionTLS11:
return "TLS 1.1"
case versionTLS12:
return "TLS 1.2"
case versionTLS13:
return "TLS 1.3"
default:
if (version & versionTLS13DraftXX) == versionTLS13DraftXX {
return fmt.Sprintf("TLS 1.3 (draft %d)", version&0xff)
}
return fmt.Sprintf("unknown %#x", version)
}
}
func queryVersion(address, sniHost string, minVersion, maxVersion uint16) (uint16, error) {
clientHello, err := buildClientHelloRecord(sniHost, minVersion, maxVersion)
if err != nil {
return 0, err
}
conn, err := net.Dial("tcp", address)
if err != nil {
return 0, err
}
defer conn.Close()
conn.Write(clientHello)
// read record (not perfect, it assumes one message in a single packet)
buffer := make([]byte, 4096)
n, err := conn.Read(buffer)
if n == 0 && err != nil {
return 0, err
}
return parseServerHelloRecord(buffer)
}
func main() {
var address string
var sni string
flag.StringVar(&address, "connect", "localhost", "hostname[:port] to connect to")
flag.StringVar(&sni, "servername", "", "Set SNI instead of using the -connect host")
flag.Parse()
if !strings.Contains(address, ":") {
address += ":443"
}
sniHost, _, err := net.SplitHostPort(address)
if err != nil {
panic(err)
}
if sni != "" {
sniHost = sni
}
// prepare client hello
minVersion := versionTLS13Draft01
maxVersion := versionTLS13
for minVersion <= maxVersion || maxVersion == versionTLS13 {
version, err := queryVersion(address, sniHost, minVersion, maxVersion)
if err != nil {
fmt.Printf("%s query (max version: %s) failed: %s\n", address, versionToString(maxVersion), err)
break
}
fmt.Printf("%s version: %#x - %s\n", address, version, versionToString(version))
if version == 0 {
// version is supplied by the server, must check.
break
} else {
// Assume that the server selected its maximum supported
// (draft) version. Probe for the next (lower) version.
maxVersion = version - 1
}
}
}
|