forked from go-mysql-org/go-mysql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandshake_resp.go
200 lines (170 loc) · 5.29 KB
/
handshake_resp.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
package server
import (
"bytes"
"crypto/tls"
"encoding/binary"
. "github.com/go-mysql-org/go-mysql/mysql"
"github.com/pingcap/errors"
)
func (c *Conn) readHandshakeResponse() error {
data, pos, err := c.readFirstPart()
if err != nil {
return err
}
if pos, err = c.readUserName(data, pos); err != nil {
return err
}
authData, authLen, pos, err := c.readAuthData(data, pos)
if err != nil {
return err
}
pos += authLen
if pos, err = c.readDb(data, pos); err != nil {
return err
}
pos = c.readPluginName(data, pos)
cont, err := c.handleAuthMatch(authData, pos)
if err != nil {
return err
}
if !cont {
return nil
}
// ignore connect attrs for now, the proxy does not support passing attrs to actual MySQL server
// try to authenticate the client
return c.compareAuthData(c.authPluginName, authData)
}
func (c *Conn) readFirstPart() ([]byte, int, error) {
data, err := c.ReadPacket()
if err != nil {
return nil, 0, err
}
return c.decodeFirstPart(data)
}
func (c *Conn) decodeFirstPart(data []byte) ([]byte, int, error) {
pos := 0
// check CLIENT_PROTOCOL_41
if uint32(binary.LittleEndian.Uint16(data[:2]))&CLIENT_PROTOCOL_41 == 0 {
return nil, 0, errors.New("CLIENT_PROTOCOL_41 compatible client is required")
}
//capability
c.capability = binary.LittleEndian.Uint32(data[:4])
if c.capability&CLIENT_SECURE_CONNECTION == 0 {
return nil, 0, errors.New("CLIENT_SECURE_CONNECTION compatible client is required")
}
pos += 4
//skip max packet size
pos += 4
// connection's default character set as defined
c.charset = data[pos]
pos++
//skip reserved 23[00]
pos += 23
// is this a SSLRequest packet?
if len(data) == (4 + 4 + 1 + 23) {
if c.serverConf.capability&CLIENT_SSL == 0 {
return nil, 0, errors.Errorf("The host '%s' does not support SSL connections", c.RemoteAddr().String())
}
// switch to TLS
tlsConn := tls.Server(c.Conn.Conn, c.serverConf.tlsConfig)
if err := tlsConn.Handshake(); err != nil {
return nil, 0, err
}
c.Conn.Conn = tlsConn
// mysql handshake again
return c.readFirstPart()
}
return data, pos, nil
}
func (c *Conn) readUserName(data []byte, pos int) (int, error) {
//user name
user := string(data[pos : pos+bytes.IndexByte(data[pos:], 0x00)])
pos += len(user) + 1
c.user = user
return pos, nil
}
func (c *Conn) readDb(data []byte, pos int) (int, error) {
if c.capability&CLIENT_CONNECT_WITH_DB != 0 {
if len(data[pos:]) == 0 {
return pos, nil
}
db := string(data[pos : pos+bytes.IndexByte(data[pos:], 0x00)])
pos += len(db) + 1
if err := c.h.UseDB(db); err != nil {
return 0, err
}
}
return pos, nil
}
func (c *Conn) readPluginName(data []byte, pos int) int {
if c.capability&CLIENT_PLUGIN_AUTH != 0 {
c.authPluginName = string(data[pos : pos+bytes.IndexByte(data[pos:], 0x00)])
pos += len(c.authPluginName)
} else {
// The method used is Native Authentication if both CLIENT_PROTOCOL_41 and CLIENT_SECURE_CONNECTION are set,
// but CLIENT_PLUGIN_AUTH is not set, so we fallback to 'mysql_native_password'
c.authPluginName = AUTH_NATIVE_PASSWORD
}
return pos
}
func (c *Conn) readAuthData(data []byte, pos int) (auth []byte, authLen int, newPos int, err error) {
// prevent 'panic: runtime error: index out of range' error
defer func() {
if recover() != nil {
err = NewDefaultError(ER_HANDSHAKE_ERROR)
}
}()
// length encoded data
if c.capability&CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA != 0 {
authData, isNULL, readBytes, err := LengthEncodedString(data[pos:])
if err != nil {
return nil, 0, 0, err
}
if isNULL {
// no auth length and no auth data, just \NUL, considered invalid auth data, and reject connection as MySQL does
return nil, 0, 0, NewDefaultError(ER_ACCESS_DENIED_ERROR, c.RemoteAddr().String(), c.user, ER_NO)
}
auth = authData
authLen = readBytes
} else if c.capability&CLIENT_SECURE_CONNECTION != 0 {
// auth length and auth
authLen = int(data[pos])
pos++
auth = data[pos : pos+authLen]
} else {
authLen = bytes.IndexByte(data[pos:], 0x00)
auth = data[pos : pos+authLen]
// account for last NUL
authLen++
}
return auth, authLen, pos, nil
}
// Public Key Retrieval
// See: https://dev.mysql.com/doc/internals/en/public-key-retrieval.html
func (c *Conn) handlePublicKeyRetrieval(authData []byte) (bool, error) {
// if the client use 'sha256_password' auth method, and request for a public key
// we send back a keyfile with Protocol::AuthMoreData
if c.authPluginName == AUTH_SHA256_PASSWORD && len(authData) == 1 && authData[0] == 0x01 {
if c.serverConf.capability&CLIENT_SSL == 0 {
return false, errors.New("server does not support SSL: CLIENT_SSL not enabled")
}
if err := c.writeAuthMoreDataPubkey(); err != nil {
return false, err
}
return false, c.handleAuthSwitchResponse()
}
return true, nil
}
func (c *Conn) handleAuthMatch(authData []byte, pos int) (bool, error) {
// if the client responds the handshake with a different auth method, the server will send the AuthSwitchRequest packet
// to the client to ask the client to switch.
if c.authPluginName != c.serverConf.defaultAuthMethod {
if err := c.writeAuthSwitchRequest(c.serverConf.defaultAuthMethod); err != nil {
return false, err
}
c.authPluginName = c.serverConf.defaultAuthMethod
// handle AuthSwitchResponse
return false, c.handleAuthSwitchResponse()
}
return true, nil
}