Skip to content

Commit fde44b6

Browse files
committed
Ensure proper error handling for invalid public key types in authentication flows
1 parent f612307 commit fde44b6

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

auth_caching_sha2.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,12 @@ func (p *CachingSha2PasswordPlugin) continuationAuth(packet []byte, authData []b
112112
if err != nil {
113113
return nil, fmt.Errorf("failed to parse public key: %w", err)
114114
}
115-
pubKey = pkix.(*rsa.PublicKey)
115+
116+
var ok bool
117+
pubKey, ok = pkix.(*rsa.PublicKey)
118+
if !ok {
119+
return nil, fmt.Errorf("server sent an invalid public key type: %T", pkix)
120+
}
116121
}
117122

118123
// Encrypt and send password
@@ -142,7 +147,7 @@ func (p *CachingSha2PasswordPlugin) continuationAuth(packet []byte, authData []b
142147
//
143148
// The algorithm is:
144149
// 1. SHA256(password)
145-
// 2. SHA256(SHA256(SHA256(password)))
150+
// 2. SHA256(SHA256(password))
146151
// 3. XOR(SHA256(password), SHA256(SHA256(SHA256(password)), scramble))
147152
//
148153
// This provides a way to verify the password without storing it in cleartext.

auth_sha256.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ func (p *Sha256PasswordPlugin) InitAuth(authData []byte, cfg *Config) ([]byte, e
6868
// 2. Error packet - Authentication failed
6969
// 3. More data packet - Contains the server's public key for password encryption
7070
func (p *Sha256PasswordPlugin) continuationAuth(packet []byte, authData []byte, mc *mysqlConn) ([]byte, error) {
71+
if len(packet) == 0 {
72+
return nil, fmt.Errorf("%w: empty auth response packet", ErrMalformPkt)
73+
}
7174

7275
switch packet[0] {
7376
case iOK, iERR, iEOF:
@@ -86,8 +89,13 @@ func (p *Sha256PasswordPlugin) continuationAuth(packet []byte, authData []byte,
8689
return nil, fmt.Errorf("failed to parse public key: %w", err)
8790
}
8891

92+
pubKey, ok := pub.(*rsa.PublicKey)
93+
if !ok {
94+
return nil, fmt.Errorf("server sent an invalid public key type: %T", pub)
95+
}
96+
8997
// Send encrypted password
90-
enc, err := encryptPassword(mc.cfg.Passwd, authData, pub.(*rsa.PublicKey))
98+
enc, err := encryptPassword(mc.cfg.Passwd, authData, pubKey)
9199
if err != nil {
92100
return nil, fmt.Errorf("failed to encrypt password with server key: %w", err)
93101
}

0 commit comments

Comments
 (0)