Skip to content

Commit 99c830f

Browse files
committed
auth: fix sha256_password implementation
1 parent 20a5764 commit 99c830f

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

auth.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func (mc *mysqlConn) auth(authData []byte, plugin string) ([]byte, bool, error)
209209
}
210210
if mc.cfg.tls != nil || mc.cfg.Net == "unix" {
211211
// write cleartext auth packet
212-
return []byte(mc.cfg.Passwd), true, nil // TODO: nul-terminate
212+
return []byte(mc.cfg.Passwd), true, nil
213213
}
214214
// request public key
215215
// TODO: allow to specify a local file with the pub key via the DSN
@@ -234,6 +234,9 @@ func (mc *mysqlConn) handleAuthResult(oldAuthData []byte, plugin string) error {
234234
// sent and we have to keep using the cipher sent in the init packet.
235235
if authData == nil {
236236
authData = oldAuthData
237+
} else {
238+
// copy data from read buffer to owned slice
239+
copy(oldAuthData, authData)
237240
}
238241

239242
plugin = newPlugin
@@ -316,18 +319,23 @@ func (mc *mysqlConn) handleAuthResult(oldAuthData []byte, plugin string) error {
316319
}
317320

318321
case "sha256_password":
319-
block, _ := pem.Decode(authData)
320-
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
321-
if err != nil {
322-
return err
323-
}
322+
switch len(authData) {
323+
case 0:
324+
return nil // auth successful
325+
default:
326+
block, _ := pem.Decode(authData)
327+
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
328+
if err != nil {
329+
return err
330+
}
324331

325-
// send encrypted password
326-
err = mc.sendEncryptedPassword(oldAuthData, pub.(*rsa.PublicKey))
327-
if err != nil {
328-
return err
332+
// send encrypted password
333+
err = mc.sendEncryptedPassword(oldAuthData, pub.(*rsa.PublicKey))
334+
if err != nil {
335+
return err
336+
}
337+
return mc.readResultOK()
329338
}
330-
return mc.readResultOK()
331339

332340
default:
333341
return nil // auth successful

0 commit comments

Comments
 (0)