Skip to content

Commit 7f86f42

Browse files
committed
password cipher fix
Fixes Issue #35
1 parent 5e8c201 commit 7f86f42

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type mysqlConn struct {
2020
cfg *config
2121
flags ClientFlag
2222
charset byte
23-
scrambleBuff []byte
23+
cipher []byte
2424
netConn net.Conn
2525
buf *buffer
2626
protocol uint8

packets.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ func (mc *mysqlConn) readPacket() (data []byte, err error) {
3333
return nil, driver.ErrBadConn
3434
}
3535

36-
// Packet Length
36+
// Packet Length [24 bit]
3737
pktLen := uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16
3838

39-
if pktLen == 0 {
39+
if pktLen < 1 {
4040
errLog.Print(errMalformPkt.Error())
4141
return nil, driver.ErrBadConn
4242
}
4343

44-
// Check Packet Sync
44+
// Check Packet Sync [8 bit]
4545
if data[3] != mc.sequence {
4646
if data[3] > mc.sequence {
4747
return nil, errPktSyncMul
@@ -51,7 +51,7 @@ func (mc *mysqlConn) readPacket() (data []byte, err error) {
5151
}
5252
mc.sequence++
5353

54-
// Read packet body
54+
// Read packet body [pktLen bytes]
5555
data = make([]byte, pktLen)
5656
err = mc.buf.read(data)
5757
if err == nil {
@@ -103,8 +103,8 @@ func (mc *mysqlConn) readInitPacket() (err error) {
103103
// connection id [4 bytes]
104104
pos := 1 + bytes.IndexByte(data[1:], 0x00) + 1 + 4
105105

106-
// first part of scramble buffer [8 bytes]
107-
mc.scrambleBuff = data[pos : pos+8]
106+
// first part of the password cipher [8 bytes]
107+
mc.cipher = append(mc.cipher, data[pos:pos+8]...)
108108

109109
// (filler) always 0x00 [1 byte]
110110
pos += 8 + 1
@@ -126,7 +126,11 @@ func (mc *mysqlConn) readInitPacket() (err error) {
126126
// reserved (all [00]) [10 byte]
127127
pos += 1 + 2 + 2 + 1 + 10
128128

129-
mc.scrambleBuff = append(mc.scrambleBuff, data[pos:len(data)-1]...)
129+
// second part of the password cipher [12? bytes]
130+
// The documentation is ambiguous about the length.
131+
// The official Python library uses the fixed length 12
132+
// which is not documented but seems to work.
133+
mc.cipher = append(mc.cipher, data[pos:pos+12]...)
130134

131135
if data[len(data)-1] == 0 {
132136
return
@@ -152,8 +156,8 @@ func (mc *mysqlConn) writeAuthPacket() error {
152156
}
153157

154158
// User Password
155-
scrambleBuff := scramblePassword(mc.scrambleBuff, []byte(mc.cfg.passwd))
156-
mc.scrambleBuff = nil
159+
scrambleBuff := scramblePassword(mc.cipher, []byte(mc.cfg.passwd))
160+
mc.cipher = nil
157161

158162
pktLen := 4 + 4 + 1 + 23 + len(mc.cfg.user) + 1 + 1 + len(scrambleBuff)
159163

utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func scramblePassword(scramble, password []byte) []byte {
108108
for i := range result {
109109
result[i] = scrambleHash[i] ^ stage1Hash[i]
110110
}
111-
return result[0:]
111+
return result
112112
}
113113

114114
/******************************************************************************

0 commit comments

Comments
 (0)