Skip to content

Commit c3870b2

Browse files
committed
Remove addNUL
1 parent 1dc3cd1 commit c3870b2

File tree

4 files changed

+51
-68
lines changed

4 files changed

+51
-68
lines changed

auth.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -234,64 +234,64 @@ func (mc *mysqlConn) sendEncryptedPassword(seed []byte, pub *rsa.PublicKey) erro
234234
if err != nil {
235235
return err
236236
}
237-
return mc.writeAuthSwitchPacket(enc, false)
237+
return mc.writeAuthSwitchPacket(enc)
238238
}
239239

240-
func (mc *mysqlConn) auth(authData []byte, plugin string) ([]byte, bool, error) {
240+
func (mc *mysqlConn) auth(authData []byte, plugin string) ([]byte, error) {
241241
switch plugin {
242242
case "caching_sha2_password":
243243
authResp := scrambleSHA256Password(authData, mc.cfg.Passwd)
244-
return authResp, false, nil
244+
return authResp, nil
245245

246246
case "mysql_old_password":
247247
if !mc.cfg.AllowOldPasswords {
248-
return nil, false, ErrOldPassword
248+
return nil, ErrOldPassword
249249
}
250250
// Note: there are edge cases where this should work but doesn't;
251251
// this is currently "wontfix":
252252
// https://github.com/go-sql-driver/mysql/issues/184
253-
authResp := scrambleOldPassword(authData[:8], mc.cfg.Passwd)
254-
return authResp, true, nil
253+
authResp := append(scrambleOldPassword(authData[:8], mc.cfg.Passwd), 0)
254+
return authResp, nil
255255

256256
case "mysql_clear_password":
257257
if !mc.cfg.AllowCleartextPasswords {
258-
return nil, false, ErrCleartextPassword
258+
return nil, ErrCleartextPassword
259259
}
260260
// http://dev.mysql.com/doc/refman/5.7/en/cleartext-authentication-plugin.html
261261
// http://dev.mysql.com/doc/refman/5.7/en/pam-authentication-plugin.html
262-
return []byte(mc.cfg.Passwd), true, nil
262+
return append([]byte(mc.cfg.Passwd), 0), nil
263263

264264
case "mysql_native_password":
265265
if !mc.cfg.AllowNativePasswords {
266-
return nil, false, ErrNativePassword
266+
return nil, ErrNativePassword
267267
}
268268
// https://dev.mysql.com/doc/internals/en/secure-password-authentication.html
269269
// Native password authentication only need and will need 20-byte challenge.
270270
authResp := scramblePassword(authData[:20], mc.cfg.Passwd)
271-
return authResp, false, nil
271+
return authResp, nil
272272

273273
case "sha256_password":
274274
if len(mc.cfg.Passwd) == 0 {
275-
return nil, true, nil
275+
return []byte{0}, nil
276276
}
277277
if mc.cfg.tls != nil || mc.cfg.Net == "unix" {
278278
// write cleartext auth packet
279-
return []byte(mc.cfg.Passwd), true, nil
279+
return append([]byte(mc.cfg.Passwd), 0), nil
280280
}
281281

282282
pubKey := mc.cfg.pubKey
283283
if pubKey == nil {
284284
// request public key from server
285-
return []byte{1}, false, nil
285+
return []byte{1}, nil
286286
}
287287

288288
// encrypted password
289289
enc, err := encryptPassword(mc.cfg.Passwd, authData, pubKey)
290-
return enc, false, err
290+
return enc, err
291291

292292
default:
293293
errLog.Print("unknown auth plugin:", plugin)
294-
return nil, false, ErrUnknownPlugin
294+
return nil, ErrUnknownPlugin
295295
}
296296
}
297297

@@ -315,11 +315,11 @@ func (mc *mysqlConn) handleAuthResult(oldAuthData []byte, plugin string) error {
315315

316316
plugin = newPlugin
317317

318-
authResp, addNUL, err := mc.auth(authData, plugin)
318+
authResp, err := mc.auth(authData, plugin)
319319
if err != nil {
320320
return err
321321
}
322-
if err = mc.writeAuthSwitchPacket(authResp, addNUL); err != nil {
322+
if err = mc.writeAuthSwitchPacket(authResp); err != nil {
323323
return err
324324
}
325325

@@ -352,7 +352,7 @@ func (mc *mysqlConn) handleAuthResult(oldAuthData []byte, plugin string) error {
352352
case cachingSha2PasswordPerformFullAuthentication:
353353
if mc.cfg.tls != nil || mc.cfg.Net == "unix" {
354354
// write cleartext auth packet
355-
err = mc.writeAuthSwitchPacket([]byte(mc.cfg.Passwd), true)
355+
err = mc.writeAuthSwitchPacket(append([]byte(mc.cfg.Passwd), 0))
356356
if err != nil {
357357
return err
358358
}

auth_test.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ func TestAuthFastCachingSHA256PasswordCached(t *testing.T) {
8585
plugin := "caching_sha2_password"
8686

8787
// Send Client Authentication Packet
88-
authResp, addNUL, err := mc.auth(authData, plugin)
88+
authResp, err := mc.auth(authData, plugin)
8989
if err != nil {
9090
t.Fatal(err)
9191
}
92-
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
92+
err = mc.writeHandshakeResponsePacket(authResp, plugin)
9393
if err != nil {
9494
t.Fatal(err)
9595
}
@@ -130,11 +130,11 @@ func TestAuthFastCachingSHA256PasswordEmpty(t *testing.T) {
130130
plugin := "caching_sha2_password"
131131

132132
// Send Client Authentication Packet
133-
authResp, addNUL, err := mc.auth(authData, plugin)
133+
authResp, err := mc.auth(authData, plugin)
134134
if err != nil {
135135
t.Fatal(err)
136136
}
137-
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
137+
err = mc.writeHandshakeResponsePacket(authResp, plugin)
138138
if err != nil {
139139
t.Fatal(err)
140140
}
@@ -172,11 +172,11 @@ func TestAuthFastCachingSHA256PasswordFullRSA(t *testing.T) {
172172
plugin := "caching_sha2_password"
173173

174174
// Send Client Authentication Packet
175-
authResp, addNUL, err := mc.auth(authData, plugin)
175+
authResp, err := mc.auth(authData, plugin)
176176
if err != nil {
177177
t.Fatal(err)
178178
}
179-
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
179+
err = mc.writeHandshakeResponsePacket(authResp, plugin)
180180
if err != nil {
181181
t.Fatal(err)
182182
}
@@ -228,11 +228,11 @@ func TestAuthFastCachingSHA256PasswordFullRSAWithKey(t *testing.T) {
228228
plugin := "caching_sha2_password"
229229

230230
// Send Client Authentication Packet
231-
authResp, addNUL, err := mc.auth(authData, plugin)
231+
authResp, err := mc.auth(authData, plugin)
232232
if err != nil {
233233
t.Fatal(err)
234234
}
235-
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
235+
err = mc.writeHandshakeResponsePacket(authResp, plugin)
236236
if err != nil {
237237
t.Fatal(err)
238238
}
@@ -280,11 +280,11 @@ func TestAuthFastCachingSHA256PasswordFullSecure(t *testing.T) {
280280
plugin := "caching_sha2_password"
281281

282282
// Send Client Authentication Packet
283-
authResp, addNUL, err := mc.auth(authData, plugin)
283+
authResp, err := mc.auth(authData, plugin)
284284
if err != nil {
285285
t.Fatal(err)
286286
}
287-
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
287+
err = mc.writeHandshakeResponsePacket(authResp, plugin)
288288
if err != nil {
289289
t.Fatal(err)
290290
}
@@ -336,7 +336,7 @@ func TestAuthFastCleartextPasswordNotAllowed(t *testing.T) {
336336
plugin := "mysql_clear_password"
337337

338338
// Send Client Authentication Packet
339-
_, _, err := mc.auth(authData, plugin)
339+
_, err := mc.auth(authData, plugin)
340340
if err != ErrCleartextPassword {
341341
t.Errorf("expected ErrCleartextPassword, got %v", err)
342342
}
@@ -353,11 +353,11 @@ func TestAuthFastCleartextPassword(t *testing.T) {
353353
plugin := "mysql_clear_password"
354354

355355
// Send Client Authentication Packet
356-
authResp, addNUL, err := mc.auth(authData, plugin)
356+
authResp, err := mc.auth(authData, plugin)
357357
if err != nil {
358358
t.Fatal(err)
359359
}
360-
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
360+
err = mc.writeHandshakeResponsePacket(authResp, plugin)
361361
if err != nil {
362362
t.Fatal(err)
363363
}
@@ -396,11 +396,11 @@ func TestAuthFastCleartextPasswordEmpty(t *testing.T) {
396396
plugin := "mysql_clear_password"
397397

398398
// Send Client Authentication Packet
399-
authResp, addNUL, err := mc.auth(authData, plugin)
399+
authResp, err := mc.auth(authData, plugin)
400400
if err != nil {
401401
t.Fatal(err)
402402
}
403-
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
403+
err = mc.writeHandshakeResponsePacket(authResp, plugin)
404404
if err != nil {
405405
t.Fatal(err)
406406
}
@@ -439,7 +439,7 @@ func TestAuthFastNativePasswordNotAllowed(t *testing.T) {
439439
plugin := "mysql_native_password"
440440

441441
// Send Client Authentication Packet
442-
_, _, err := mc.auth(authData, plugin)
442+
_, err := mc.auth(authData, plugin)
443443
if err != ErrNativePassword {
444444
t.Errorf("expected ErrNativePassword, got %v", err)
445445
}
@@ -455,11 +455,11 @@ func TestAuthFastNativePassword(t *testing.T) {
455455
plugin := "mysql_native_password"
456456

457457
// Send Client Authentication Packet
458-
authResp, addNUL, err := mc.auth(authData, plugin)
458+
authResp, err := mc.auth(authData, plugin)
459459
if err != nil {
460460
t.Fatal(err)
461461
}
462-
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
462+
err = mc.writeHandshakeResponsePacket(authResp, plugin)
463463
if err != nil {
464464
t.Fatal(err)
465465
}
@@ -498,11 +498,11 @@ func TestAuthFastNativePasswordEmpty(t *testing.T) {
498498
plugin := "mysql_native_password"
499499

500500
// Send Client Authentication Packet
501-
authResp, addNUL, err := mc.auth(authData, plugin)
501+
authResp, err := mc.auth(authData, plugin)
502502
if err != nil {
503503
t.Fatal(err)
504504
}
505-
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
505+
err = mc.writeHandshakeResponsePacket(authResp, plugin)
506506
if err != nil {
507507
t.Fatal(err)
508508
}
@@ -540,11 +540,11 @@ func TestAuthFastSHA256PasswordEmpty(t *testing.T) {
540540
plugin := "sha256_password"
541541

542542
// Send Client Authentication Packet
543-
authResp, addNUL, err := mc.auth(authData, plugin)
543+
authResp, err := mc.auth(authData, plugin)
544544
if err != nil {
545545
t.Fatal(err)
546546
}
547-
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
547+
err = mc.writeHandshakeResponsePacket(authResp, plugin)
548548
if err != nil {
549549
t.Fatal(err)
550550
}
@@ -587,11 +587,11 @@ func TestAuthFastSHA256PasswordRSA(t *testing.T) {
587587
plugin := "sha256_password"
588588

589589
// Send Client Authentication Packet
590-
authResp, addNUL, err := mc.auth(authData, plugin)
590+
authResp, err := mc.auth(authData, plugin)
591591
if err != nil {
592592
t.Fatal(err)
593593
}
594-
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
594+
err = mc.writeHandshakeResponsePacket(authResp, plugin)
595595
if err != nil {
596596
t.Fatal(err)
597597
}
@@ -636,11 +636,11 @@ func TestAuthFastSHA256PasswordRSAWithKey(t *testing.T) {
636636
plugin := "sha256_password"
637637

638638
// Send Client Authentication Packet
639-
authResp, addNUL, err := mc.auth(authData, plugin)
639+
authResp, err := mc.auth(authData, plugin)
640640
if err != nil {
641641
t.Fatal(err)
642642
}
643-
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
643+
err = mc.writeHandshakeResponsePacket(authResp, plugin)
644644
if err != nil {
645645
t.Fatal(err)
646646
}
@@ -669,15 +669,15 @@ func TestAuthFastSHA256PasswordSecure(t *testing.T) {
669669
plugin := "sha256_password"
670670

671671
// send Client Authentication Packet
672-
authResp, addNUL, err := mc.auth(authData, plugin)
672+
authResp, err := mc.auth(authData, plugin)
673673
if err != nil {
674674
t.Fatal(err)
675675
}
676676

677677
// unset TLS config to prevent the actual establishment of a TLS wrapper
678678
mc.cfg.tls = nil
679679

680-
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
680+
err = mc.writeHandshakeResponsePacket(authResp, plugin)
681681
if err != nil {
682682
t.Fatal(err)
683683
}

driver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,18 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
110110
}
111111

112112
// Send Client Authentication Packet
113-
authResp, addNUL, err := mc.auth(authData, plugin)
113+
authResp, err := mc.auth(authData, plugin)
114114
if err != nil {
115115
// try the default auth plugin, if using the requested plugin failed
116116
errLog.Print("could not use requested auth plugin '"+plugin+"': ", err.Error())
117117
plugin = defaultAuthPlugin
118-
authResp, addNUL, err = mc.auth(authData, plugin)
118+
authResp, err = mc.auth(authData, plugin)
119119
if err != nil {
120120
mc.cleanup()
121121
return nil, err
122122
}
123123
}
124-
if err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin); err != nil {
124+
if err = mc.writeHandshakeResponsePacket(authResp, plugin); err != nil {
125125
mc.cleanup()
126126
return nil, err
127127
}

packets.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func (mc *mysqlConn) readHandshakePacket() (data []byte, plugin string, err erro
243243

244244
// Client Authentication Packet
245245
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse
246-
func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, addNUL bool, plugin string) error {
246+
func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string) error {
247247
// Adjust client flags based on server support
248248
clientFlags := clientProtocol41 |
249249
clientSecureConn |
@@ -270,9 +270,6 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, addNUL bool,
270270
// encode length of the auth plugin data
271271
var authRespLEIBuf [9]byte
272272
authRespLen := len(authResp)
273-
if addNUL {
274-
authRespLen++
275-
}
276273
authRespLEI := appendLengthEncodedInteger(authRespLEIBuf[:0], uint64(authRespLen))
277274
if len(authRespLEI) > 1 {
278275
// if the length can not be written in 1 byte, it must be written as a
@@ -281,9 +278,6 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, addNUL bool,
281278
}
282279

283280
pktLen := 4 + 4 + 1 + 23 + len(mc.cfg.User) + 1 + len(authRespLEI) + len(authResp) + 21 + 1
284-
if addNUL {
285-
pktLen++
286-
}
287281

288282
// To specify a db name
289283
if n := len(mc.cfg.DBName); n > 0 {
@@ -354,10 +348,6 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, addNUL bool,
354348
// Auth Data [length encoded integer]
355349
pos += copy(data[pos:], authRespLEI)
356350
pos += copy(data[pos:], authResp)
357-
if addNUL {
358-
data[pos] = 0x00
359-
pos++
360-
}
361351

362352
// Databasename [null terminated string]
363353
if len(mc.cfg.DBName) > 0 {
@@ -375,11 +365,8 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, addNUL bool,
375365
}
376366

377367
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
378-
func (mc *mysqlConn) writeAuthSwitchPacket(authData []byte, addNUL bool) error {
368+
func (mc *mysqlConn) writeAuthSwitchPacket(authData []byte) error {
379369
pktLen := 4 + len(authData)
380-
if addNUL {
381-
pktLen++
382-
}
383370
data := mc.buf.takeSmallBuffer(pktLen)
384371
if data == nil {
385372
// cannot take the buffer. Something must be wrong with the connection
@@ -389,10 +376,6 @@ func (mc *mysqlConn) writeAuthSwitchPacket(authData []byte, addNUL bool) error {
389376

390377
// Add the auth data [EOF]
391378
copy(data[4:], authData)
392-
if addNUL {
393-
data[pktLen-1] = 0x00
394-
}
395-
396379
return mc.writePacket(data)
397380
}
398381

0 commit comments

Comments
 (0)