-
Notifications
You must be signed in to change notification settings - Fork 42
/
authentication_handshake.go
374 lines (311 loc) · 11.3 KB
/
authentication_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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package mysql
import (
"net"
"github.com/cyberark/secretless-broker/internal/plugin/connectors/tcp/mysql/protocol"
"github.com/cyberark/secretless-broker/internal/plugin/connectors/tcp/ssl"
)
/*
AuthenticationHandshake represents the entire back and forth process
between a MySQL client and server during which authentication occurs.
Note this is distinct from the various specific handshake packets that are
sent back and forth during this process.
Note this is process is referred to as the "connection phase" in the MySQL
docs:
https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_connection_phase.html
Overview of the Handshake Process
+---------+ +-------------+ +---------+
| Client | | Secretless | | Backend |
+---------+ +-------------+ +---------+
| | |
| | InitialHandshakePacket |
| |<------------------------------------|
| | |
| InitialHandshakePacket | |
|<------------------------------| |
| | |
| HandshakeResponse | |
|------------------------------>| |
| | -------------------------------\ |
| |-| If client requested SSL, add | |
| | | it to HandshakeResponse | |
| | |------------------------------| |
| | HandshakeResponse |
| |------------------------------------>|
| | |
| | OkPacket |
| |<------------------------------------|
| | |
| OkPacket | |
|<------------------------------| |
| | |
Note: The above diagram was created using https://textart.io/sequence and the
following source:
object Client Secretless Backend
Backend->Secretless: InitialHandshakePacket
Secretless->Client: InitialHandshakePacket
Client->Secretless: HandshakeResponse
note right of Secretless: If client requested SSL, add\n it to HandshakeResponse
Secretless->Backend: HandshakeResponse
Backend->Secretless: OkPacket
Secretless->Client: OkPacket
*/
type AuthenticationHandshake struct {
connectionDetails *ConnectionDetails
clientConn *Connection
backendConn *Connection
sslMode *ssl.DbSSLMode
//TODO: after kumbi's work these 2 should be combined
rawServerHandshake Packet
serverHandshake *protocol.HandshakeV10
clientHandshakeResponse *protocol.HandshakeResponse41
err error
}
// NewAuthenticationHandshake creates a new AuthenticationHandshake command object,
// intended to be Run().
func NewAuthenticationHandshake(
clientConn *Connection,
backendConn *Connection,
connDetails *ConnectionDetails,
) AuthenticationHandshake {
return AuthenticationHandshake{
connectionDetails: connDetails,
clientConn: clientConn,
backendConn: backendConn,
}
}
// Run executes all the logic needed to complete authentication between a
// MySQL server and client. When it completes successfully,
// AuthenticatedBackendConn will return the raw, authenticated network conn.
func (h *AuthenticationHandshake) Run() error {
h.readServerHandshake()
h.writeHandshakeToClient()
h.validateServerSSL()
h.readClientHandshakeResponse()
h.overrideClientCapabilities()
h.injectCredentials()
h.handleClientSSLRequest()
h.writeClientHandshakeResponseToBackend()
h.verifyAndProxyOkResponse()
return h.err
}
// AuthenticatedBackendConn returns an already authenticated connection
// to the MySQL server. Intended to be called after Run() has completed.
func (h *AuthenticationHandshake) AuthenticatedBackendConn() net.Conn {
return h.backendConn.RawConnection()
}
func (h *AuthenticationHandshake) readServerHandshake() {
if h.err != nil {
return
}
h.rawServerHandshake = h.readBackendPacket()
if h.err != nil {
return
}
h.serverHandshake, h.err = protocol.UnpackHandshakeV10(h.rawServerHandshake)
}
func (h *AuthenticationHandshake) writeHandshakeToClient() {
if h.err != nil {
return
}
// Remove Client SSL Capability from Server Handshake Packet
// to force client to connect to Secretless without SSL
// TODO: update this after kumbi's work
packetWithNoSSL, err := protocol.RemoveSSLFromHandshakeV10(h.rawServerHandshake)
if err != nil {
h.err = err
return
}
// TODO: push all packing code into the `WriteXXX` methods
h.writeClientPacket(packetWithNoSSL)
}
func (h *AuthenticationHandshake) validateServerSSL() {
if h.err != nil {
return
}
if h.clientRequestedSSL() && !h.serverSupportsSSL() {
h.err = protocol.ErrNoTLS
}
}
func (h *AuthenticationHandshake) readClientHandshakeResponse() {
if h.err != nil {
return
}
rawResponse := h.readClientPacket()
if h.err != nil {
return
}
// TODO: client requesting SSL results ERROR 2026 (HY000): SSL connection error: protocol version mismatch
h.clientHandshakeResponse, h.err = protocol.UnpackHandshakeResponse41(rawResponse)
}
func (h *AuthenticationHandshake) overrideClientCapabilities() {
if h.err != nil {
return
}
// TODO: after kumbi's done, change below to method calls
// TODO: add tests cases for authentication plugins support
// Disable CapabilityFlag for authentication plugins support
h.clientHandshakeResponse.CapabilityFlags &^= protocol.ClientPluginAuth
// TODO: add tests cases for client secure connection
// Enable CapabilityFlag for client secure connection
h.clientHandshakeResponse.CapabilityFlags |= protocol.ClientSecureConnection
// Ensure CapabilityFlag is set when using TLS
if h.clientRequestedSSL() {
h.clientHandshakeResponse.CapabilityFlags |= protocol.ClientSSL
}
}
func (h *AuthenticationHandshake) injectCredentials() {
if h.err != nil {
return
}
// TODO: change this to method call on clientHandshakeResponse when Kumbi's work done
h.err = protocol.InjectCredentials(
h.clientHandshakeResponse,
h.serverHandshake.Salt,
h.connectionDetails.Username,
h.connectionDetails.Password,
)
}
func (h *AuthenticationHandshake) handleClientSSLRequest() {
if h.err != nil {
return
}
if !h.clientRequestedSSL() {
return
}
// The SSLRequest packet is created by copying the HandshakeResponse,
// but truncating the username and everything after the username in
// the payload, as described here:
//
// https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::SSLRequest
//
// The payload itself breaks down as follows:
//
// +------------+-----------------------------------------+
// | Num Bytes | Description |
// +------------+-----------------------------------------+
// | 4 | capability flags, CLIENT_SSL always set |
// | 4 | max-packet size |
// | 1 | character set |
// | string[23] | reserved (all [0]) |
// +------------+-----------------------------------------+
//
// Hence by taking the first (4+4+1+23) bytes we take everything in
// the payload up to, but not including, the username. The final
// +4 in (4+4+1+23)+4 accounts for the header section before the
// payload, ie, the payload_length and the sequence_id, as described
// in the comment above this one.
// TODO: Note currently just repeating this logic. Will change after kumbi integration
packedHandshakeRespPacket, err := protocol.PackHandshakeResponse41(h.clientHandshakeResponse)
if err != nil {
h.err = err
return
}
tmp := packedHandshakeRespPacket[:(4+4+1+23)+4]
sslPacket := make([]byte, len(tmp))
copy(sslPacket, tmp)
// This sets the payload_length bytes in the header portion of the packet
// to reflect the new length of the truncated packet.
pktLen := len(sslPacket) - 4
sslPacket[0] = byte(pktLen)
sslPacket[1] = byte(pktLen >> 8)
sslPacket[2] = byte(pktLen >> 16)
// Send TLS / SSL request packet
h.writeBackendPacket(sslPacket)
if h.err != nil {
return
}
// Switch to TLS
sslConn, err := ssl.HandleSSLUpgrade(h.backendConn.RawConnection(), *(h.dbSSLMode()))
if err != nil {
h.err = err
return
}
h.backendConn.SetConnection(sslConn)
}
func (h *AuthenticationHandshake) writeClientHandshakeResponseToBackend() {
if h.err != nil {
return
}
packedHandshakeRespPacket, err := protocol.PackHandshakeResponse41(h.clientHandshakeResponse)
if err != nil {
h.err = err
return
}
h.writeBackendPacket(packedHandshakeRespPacket)
}
func (h *AuthenticationHandshake) verifyAndProxyOkResponse() {
if h.err != nil {
return
}
rawPkt := h.readBackendPacket()
if h.err != nil {
return
}
switch protocol.GetPacketType(rawPkt) {
case protocol.ResponseErr:
// Return after adding the error response to AuthenticationHandshake
// as a protocol.Error type
//
// The protocol.Error type makes it possible
// to have Go errors that can contain rich protocol specific information
// and have the smarts to encode themselves into a MYSQL error packet
err := protocol.UnpackErrResponse(rawPkt)
h.err = err
return
default:
// Verify packet is valid; don't do anything with unpacked
if _, err := protocol.UnpackOkResponse(rawPkt); err != nil {
h.err = err
return
}
}
h.writeClientPacket(rawPkt)
}
func (h *AuthenticationHandshake) dbSSLMode() *ssl.DbSSLMode {
if h.err != nil {
return nil
}
// already memoized, just return it
if h.sslMode != nil {
return h.sslMode
}
var ret ssl.DbSSLMode
ret, h.err = ssl.NewDbSSLMode(
h.connectionDetails.SSLOptions, false,
)
h.sslMode = &ret
return h.sslMode
}
func (h *AuthenticationHandshake) clientRequestedSSL() bool {
if h.err != nil {
return false
}
return h.dbSSLMode().UseTLS
}
func (h *AuthenticationHandshake) serverSupportsSSL() bool {
return (h.serverHandshake.ServerCapabilities & protocol.ClientSSL) != 0
}
// NOTE: These lower level packet reading helper methods don't need the
// h.err guards, becuase they'll always be called _by_ a higher
// level method that has one.
func (h *AuthenticationHandshake) readClientPacket() Packet {
return h.readPacket(h.clientConn)
}
func (h *AuthenticationHandshake) writeClientPacket(pkt Packet) {
h.err = h.clientConn.write(pkt)
}
func (h *AuthenticationHandshake) readBackendPacket() Packet {
return h.readPacket(h.backendConn)
}
func (h *AuthenticationHandshake) writeBackendPacket(pkt Packet) {
h.err = h.backendConn.write(pkt)
}
// Just a helper method to DRY up the client/backend reads above
func (h *AuthenticationHandshake) readPacket(conn *Connection) Packet {
pkt, err := conn.read()
if err != nil {
h.err = err
return nil
}
return pkt
}