-
Notifications
You must be signed in to change notification settings - Fork 5
/
getsni.go
86 lines (74 loc) · 2.33 KB
/
getsni.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
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package getsni
import (
"errors"
"golang.org/x/crypto/cryptobyte"
)
// GetSNI accepts the beginning of a TLS connection and returns the
// indicated server name, or an error if the server name was not found.
// Derived from unmarshal() in crypto/tls.
func GetSNI(clienthello []byte) (string, error) {
plaintext := cryptobyte.String(clienthello)
var s cryptobyte.String
// Skip uint8 ContentType and uint16 ProtocolVersion
if !plaintext.Skip(1+2) || !plaintext.ReadUint16LengthPrefixed(&s) {
return "", errors.New("Bad TLSPlaintext")
}
// Skip uint8 message type, uint24 length, uint16 version, and 32 byte random.
var sessionID cryptobyte.String
if !s.Skip(1+3+2+32) ||
!s.ReadUint8LengthPrefixed(&sessionID) {
return "", errors.New("Bad Handshake message")
}
var cipherSuites cryptobyte.String
if !s.ReadUint16LengthPrefixed(&cipherSuites) {
return "", errors.New("Bad ciphersuites")
}
var compressionMethods cryptobyte.String
if !s.ReadUint8LengthPrefixed(&compressionMethods) {
return "", errors.New("Bad compression methods")
}
if s.Empty() {
// ClientHello is optionally followed by extension data
return "", errors.New("Short hello")
}
var extensions cryptobyte.String
if !s.ReadUint16LengthPrefixed(&extensions) || !s.Empty() {
return "", errors.New("Bad extensions")
}
for !extensions.Empty() {
var extension uint16
var extData cryptobyte.String
if !extensions.ReadUint16(&extension) ||
!extensions.ReadUint16LengthPrefixed(&extData) {
return "", errors.New("Bad extension")
}
switch extension {
case 0: // Extension ID 0 is ServerName
// RFC 6066, Section 3
var nameList cryptobyte.String
if !extData.ReadUint16LengthPrefixed(&nameList) || nameList.Empty() {
return "", errors.New("Bad namelist")
}
for !nameList.Empty() {
var nameType uint8
var serverName cryptobyte.String
if !nameList.ReadUint8(&nameType) ||
!nameList.ReadUint16LengthPrefixed(&serverName) ||
serverName.Empty() {
return "", errors.New("Bad SNI")
}
if nameType != 0 {
continue
}
return string(serverName), nil
}
default:
// Ignore all other extensions.
continue
}
}
return "", errors.New("No SNI")
}