Skip to content

Commit 21d7e97

Browse files
elemountjulienschmidt
authored andcommitted
Fix mysql_clear_password plugin on auth switch panic. (go-sql-driver#646)
Fixes go-sql-driver#636
1 parent 37b91d8 commit 21d7e97

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

packets.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,9 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
352352
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
353353
func (mc *mysqlConn) writeOldAuthPacket(cipher []byte) error {
354354
// User password
355-
scrambleBuff := scrambleOldPassword(cipher, []byte(mc.cfg.Passwd))
355+
// https://dev.mysql.com/doc/internals/en/old-password-authentication.html
356+
// Old password authentication only need and will need 8-byte challenge.
357+
scrambleBuff := scrambleOldPassword(cipher[:8], []byte(mc.cfg.Passwd))
356358

357359
// Calculate the packet length and add a tailing 0
358360
pktLen := len(scrambleBuff) + 1
@@ -392,7 +394,9 @@ func (mc *mysqlConn) writeClearAuthPacket() error {
392394
// Native password authentication method
393395
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
394396
func (mc *mysqlConn) writeNativeAuthPacket(cipher []byte) error {
395-
scrambleBuff := scramblePassword(cipher, []byte(mc.cfg.Passwd))
397+
// https://dev.mysql.com/doc/internals/en/secure-password-authentication.html
398+
// Native password authentication only need and will need 20-byte challenge.
399+
scrambleBuff := scramblePassword(cipher[0:20], []byte(mc.cfg.Passwd))
396400

397401
// Calculate the packet length and add a tailing 0
398402
pktLen := len(scrambleBuff)
@@ -495,7 +499,7 @@ func (mc *mysqlConn) readResultOK() ([]byte, error) {
495499
if len(data) > 1 {
496500
pluginEndIndex := bytes.IndexByte(data, 0x00)
497501
plugin := string(data[1:pluginEndIndex])
498-
cipher := data[pluginEndIndex+1 : len(data)-1]
502+
cipher := data[pluginEndIndex+1:]
499503

500504
switch plugin {
501505
case "mysql_old_password":

0 commit comments

Comments
 (0)