Client Authentication Packet https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_connection_phase_packets_protocol_handshake_response.html
(authResp []byte, plugin string)
| 319 | // Client Authentication Packet |
| 320 | // https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_connection_phase_packets_protocol_handshake_response.html |
| 321 | func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string) error { |
| 322 | // packet header 4 |
| 323 | // capabilities 4 |
| 324 | // maxPacketSize 4 |
| 325 | // collation id 1 |
| 326 | // filler 23 |
| 327 | data, err := mc.buf.takeSmallBuffer(4*3 + 24) |
| 328 | if err != nil { |
| 329 | mc.cleanup() |
| 330 | return err |
| 331 | } |
| 332 | _ = data[4*3+23] // boundery check |
| 333 | |
| 334 | // clientCapabilities [32 bit] |
| 335 | binary.LittleEndian.PutUint32(data[4:], uint32(mc.capabilities)) |
| 336 | |
| 337 | // MaxPacketSize [32 bit] (none) |
| 338 | binary.LittleEndian.PutUint32(data[8:], 0) |
| 339 | |
| 340 | // Collation ID [1 byte] |
| 341 | data[12] = defaultCollationID |
| 342 | if cname := mc.cfg.Collation; cname != "" { |
| 343 | colID, ok := collations[cname] |
| 344 | if ok { |
| 345 | data[12] = colID |
| 346 | } else if len(mc.cfg.charsets) > 0 { |
| 347 | // When cfg.charset is set, the collation is set by `SET NAMES <charset> COLLATE <collation>`. |
| 348 | return fmt.Errorf("unknown collation: %q", cname) |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | // Filler [23 bytes] (all 0x00) |
| 353 | // or filler 19bytes + mariadb extCapabilities |
| 354 | pos := 13 |
| 355 | if mc.capabilities&clientMySQL == 0 { |
| 356 | for ; pos < 13+19; pos++ { |
| 357 | data[pos] = 0 |
| 358 | } |
| 359 | // MariaDB Extended Capabilities |
| 360 | binary.LittleEndian.PutUint32(data[13+19:], uint32(mc.extCapabilities)) |
| 361 | } else { |
| 362 | for ; pos < 13+23; pos++ { |
| 363 | data[pos] = 0 |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // SSL Connection Request Packet |
| 368 | // https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_connection_phase_packets_protocol_ssl_request.html |
| 369 | // https://mariadb.com/kb/en/connection/#sslrequest-packet |
| 370 | if mc.cfg.TLS != nil { |
| 371 | // Send TLS / SSL request packet |
| 372 | if err := mc.writePacket(data); err != nil { |
| 373 | return err |
| 374 | } |
| 375 | |
| 376 | // Switch to TLS |
| 377 | tlsConn := tls.Client(mc.netConn, mc.cfg.TLS) |
| 378 | if err := tlsConn.Handshake(); err != nil { |