forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-sql-driver/mysql/auth.go

708 lines
10 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Copyright 2018 The Go-MySQL-Driver Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This Source Code Form is subject to the terms of the Mozilla Public
2024-02-18 10:42:21 +00:00
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
2024-02-18 10:42:21 +00:00
// You can obtain one at http://mozilla.org/MPL/2.0/.
package mysql
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"fmt"
"sync"
)
// server pub keys registry
2024-02-18 10:42:21 +00:00
var (
serverPubKeyLock sync.RWMutex
2024-02-18 10:42:21 +00:00
serverPubKeyRegistry map[string]*rsa.PublicKey
)
// RegisterServerPubKey registers a server RSA public key which can be used to
2024-02-18 10:42:21 +00:00
// send data in a secure manner to the server without receiving the public key
2024-02-18 10:42:21 +00:00
// in a potentially insecure way from the server first.
2024-02-18 10:42:21 +00:00
// Registered keys can afterwards be used adding serverPubKey=<name> to the DSN.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Note: The provided rsa.PublicKey instance is exclusively owned by the driver
2024-02-18 10:42:21 +00:00
// after registering it and may not be modified.
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// data, err := ioutil.ReadFile("mykey.pem")
2024-06-14 08:41:36 +00:00
// if err != nil {
2024-06-14 08:41:36 +00:00
// log.Fatal(err)
2024-06-14 08:41:36 +00:00
// }
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// block, _ := pem.Decode(data)
2024-06-14 08:41:36 +00:00
// if block == nil || block.Type != "PUBLIC KEY" {
2024-06-14 08:41:36 +00:00
// log.Fatal("failed to decode PEM block containing public key")
2024-06-14 08:41:36 +00:00
// }
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// pub, err := x509.ParsePKIXPublicKey(block.Bytes)
2024-06-14 08:41:36 +00:00
// if err != nil {
2024-06-14 08:41:36 +00:00
// log.Fatal(err)
2024-06-14 08:41:36 +00:00
// }
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// if rsaPubKey, ok := pub.(*rsa.PublicKey); ok {
2024-06-14 08:41:36 +00:00
// mysql.RegisterServerPubKey("mykey", rsaPubKey)
2024-06-14 08:41:36 +00:00
// } else {
2024-06-14 08:41:36 +00:00
// log.Fatal("not a RSA public key")
2024-06-14 08:41:36 +00:00
// }
2024-02-18 10:42:21 +00:00
func RegisterServerPubKey(name string, pubKey *rsa.PublicKey) {
2024-02-18 10:42:21 +00:00
serverPubKeyLock.Lock()
2024-02-18 10:42:21 +00:00
if serverPubKeyRegistry == nil {
2024-02-18 10:42:21 +00:00
serverPubKeyRegistry = make(map[string]*rsa.PublicKey)
2024-02-18 10:42:21 +00:00
}
serverPubKeyRegistry[name] = pubKey
2024-02-18 10:42:21 +00:00
serverPubKeyLock.Unlock()
2024-02-18 10:42:21 +00:00
}
// DeregisterServerPubKey removes the public key registered with the given name.
2024-02-18 10:42:21 +00:00
func DeregisterServerPubKey(name string) {
2024-02-18 10:42:21 +00:00
serverPubKeyLock.Lock()
2024-02-18 10:42:21 +00:00
if serverPubKeyRegistry != nil {
2024-02-18 10:42:21 +00:00
delete(serverPubKeyRegistry, name)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
serverPubKeyLock.Unlock()
2024-02-18 10:42:21 +00:00
}
func getServerPubKey(name string) (pubKey *rsa.PublicKey) {
2024-02-18 10:42:21 +00:00
serverPubKeyLock.RLock()
2024-02-18 10:42:21 +00:00
if v, ok := serverPubKeyRegistry[name]; ok {
2024-02-18 10:42:21 +00:00
pubKey = v
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
serverPubKeyLock.RUnlock()
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// Hash password using pre 4.1 (old password) method
2024-02-18 10:42:21 +00:00
// https://github.com/atcurtis/mariadb/blob/master/mysys/my_rnd.c
2024-02-18 10:42:21 +00:00
type myRnd struct {
seed1, seed2 uint32
}
const myRndMaxVal = 0x3FFFFFFF
// Pseudo random number generator
2024-02-18 10:42:21 +00:00
func newMyRnd(seed1, seed2 uint32) *myRnd {
2024-02-18 10:42:21 +00:00
return &myRnd{
2024-02-18 10:42:21 +00:00
seed1: seed1 % myRndMaxVal,
2024-02-18 10:42:21 +00:00
seed2: seed2 % myRndMaxVal,
}
2024-02-18 10:42:21 +00:00
}
// Tested to be equivalent to MariaDB's floating point variant
2024-02-18 10:42:21 +00:00
// http://play.golang.org/p/QHvhd4qved
2024-02-18 10:42:21 +00:00
// http://play.golang.org/p/RG0q4ElWDx
2024-02-18 10:42:21 +00:00
func (r *myRnd) NextByte() byte {
2024-02-18 10:42:21 +00:00
r.seed1 = (r.seed1*3 + r.seed2) % myRndMaxVal
2024-02-18 10:42:21 +00:00
r.seed2 = (r.seed1 + r.seed2 + 33) % myRndMaxVal
return byte(uint64(r.seed1) * 31 / myRndMaxVal)
2024-02-18 10:42:21 +00:00
}
// Generate binary hash from byte string using insecure pre 4.1 method
2024-02-18 10:42:21 +00:00
func pwHash(password []byte) (result [2]uint32) {
2024-02-18 10:42:21 +00:00
var add uint32 = 7
2024-02-18 10:42:21 +00:00
var tmp uint32
result[0] = 1345345333
2024-02-18 10:42:21 +00:00
result[1] = 0x12345671
for _, c := range password {
2024-02-18 10:42:21 +00:00
// skip spaces and tabs in password
2024-02-18 10:42:21 +00:00
if c == ' ' || c == '\t' {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
tmp = uint32(c)
2024-02-18 10:42:21 +00:00
result[0] ^= (((result[0] & 63) + add) * tmp) + (result[0] << 8)
2024-02-18 10:42:21 +00:00
result[1] += (result[1] << 8) ^ result[0]
2024-02-18 10:42:21 +00:00
add += tmp
2024-02-18 10:42:21 +00:00
}
// Remove sign bit (1<<31)-1)
2024-02-18 10:42:21 +00:00
result[0] &= 0x7FFFFFFF
2024-02-18 10:42:21 +00:00
result[1] &= 0x7FFFFFFF
return
2024-02-18 10:42:21 +00:00
}
// Hash password using insecure pre 4.1 method
2024-02-18 10:42:21 +00:00
func scrambleOldPassword(scramble []byte, password string) []byte {
2024-02-18 10:42:21 +00:00
scramble = scramble[:8]
hashPw := pwHash([]byte(password))
2024-02-18 10:42:21 +00:00
hashSc := pwHash(scramble)
r := newMyRnd(hashPw[0]^hashSc[0], hashPw[1]^hashSc[1])
var out [8]byte
2024-02-18 10:42:21 +00:00
for i := range out {
2024-02-18 10:42:21 +00:00
out[i] = r.NextByte() + 64
2024-02-18 10:42:21 +00:00
}
mask := r.NextByte()
2024-02-18 10:42:21 +00:00
for i := range out {
2024-02-18 10:42:21 +00:00
out[i] ^= mask
2024-02-18 10:42:21 +00:00
}
return out[:]
2024-02-18 10:42:21 +00:00
}
// Hash password using 4.1+ method (SHA1)
2024-02-18 10:42:21 +00:00
func scramblePassword(scramble []byte, password string) []byte {
2024-02-18 10:42:21 +00:00
if len(password) == 0 {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// stage1Hash = SHA1(password)
2024-02-18 10:42:21 +00:00
crypt := sha1.New()
2024-02-18 10:42:21 +00:00
crypt.Write([]byte(password))
2024-02-18 10:42:21 +00:00
stage1 := crypt.Sum(nil)
// scrambleHash = SHA1(scramble + SHA1(stage1Hash))
2024-02-18 10:42:21 +00:00
// inner Hash
2024-02-18 10:42:21 +00:00
crypt.Reset()
2024-02-18 10:42:21 +00:00
crypt.Write(stage1)
2024-02-18 10:42:21 +00:00
hash := crypt.Sum(nil)
// outer Hash
2024-02-18 10:42:21 +00:00
crypt.Reset()
2024-02-18 10:42:21 +00:00
crypt.Write(scramble)
2024-02-18 10:42:21 +00:00
crypt.Write(hash)
2024-02-18 10:42:21 +00:00
scramble = crypt.Sum(nil)
// token = scrambleHash XOR stage1Hash
2024-02-18 10:42:21 +00:00
for i := range scramble {
2024-02-18 10:42:21 +00:00
scramble[i] ^= stage1[i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return scramble
2024-02-18 10:42:21 +00:00
}
// Hash password using MySQL 8+ method (SHA256)
2024-02-18 10:42:21 +00:00
func scrambleSHA256Password(scramble []byte, password string) []byte {
2024-02-18 10:42:21 +00:00
if len(password) == 0 {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// XOR(SHA256(password), SHA256(SHA256(SHA256(password)), scramble))
crypt := sha256.New()
2024-02-18 10:42:21 +00:00
crypt.Write([]byte(password))
2024-02-18 10:42:21 +00:00
message1 := crypt.Sum(nil)
crypt.Reset()
2024-02-18 10:42:21 +00:00
crypt.Write(message1)
2024-02-18 10:42:21 +00:00
message1Hash := crypt.Sum(nil)
crypt.Reset()
2024-02-18 10:42:21 +00:00
crypt.Write(message1Hash)
2024-02-18 10:42:21 +00:00
crypt.Write(scramble)
2024-02-18 10:42:21 +00:00
message2 := crypt.Sum(nil)
for i := range message1 {
2024-02-18 10:42:21 +00:00
message1[i] ^= message2[i]
2024-02-18 10:42:21 +00:00
}
return message1
2024-02-18 10:42:21 +00:00
}
func encryptPassword(password string, seed []byte, pub *rsa.PublicKey) ([]byte, error) {
2024-02-18 10:42:21 +00:00
plain := make([]byte, len(password)+1)
2024-02-18 10:42:21 +00:00
copy(plain, password)
2024-02-18 10:42:21 +00:00
for i := range plain {
2024-02-18 10:42:21 +00:00
j := i % len(seed)
2024-02-18 10:42:21 +00:00
plain[i] ^= seed[j]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sha1 := sha1.New()
2024-02-18 10:42:21 +00:00
return rsa.EncryptOAEP(sha1, rand.Reader, pub, plain, nil)
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) sendEncryptedPassword(seed []byte, pub *rsa.PublicKey) error {
2024-02-18 10:42:21 +00:00
enc, err := encryptPassword(mc.cfg.Passwd, seed, pub)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return mc.writeAuthSwitchPacket(enc)
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) auth(authData []byte, plugin string) ([]byte, error) {
2024-02-18 10:42:21 +00:00
switch plugin {
2024-02-18 10:42:21 +00:00
case "caching_sha2_password":
2024-02-18 10:42:21 +00:00
authResp := scrambleSHA256Password(authData, mc.cfg.Passwd)
2024-02-18 10:42:21 +00:00
return authResp, nil
case "mysql_old_password":
2024-02-18 10:42:21 +00:00
if !mc.cfg.AllowOldPasswords {
2024-02-18 10:42:21 +00:00
return nil, ErrOldPassword
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(mc.cfg.Passwd) == 0 {
2024-02-18 10:42:21 +00:00
return nil, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Note: there are edge cases where this should work but doesn't;
2024-02-18 10:42:21 +00:00
// this is currently "wontfix":
2024-02-18 10:42:21 +00:00
// https://github.com/go-sql-driver/mysql/issues/184
2024-02-18 10:42:21 +00:00
authResp := append(scrambleOldPassword(authData[:8], mc.cfg.Passwd), 0)
2024-02-18 10:42:21 +00:00
return authResp, nil
case "mysql_clear_password":
2024-02-18 10:42:21 +00:00
if !mc.cfg.AllowCleartextPasswords {
2024-02-18 10:42:21 +00:00
return nil, ErrCleartextPassword
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// http://dev.mysql.com/doc/refman/5.7/en/cleartext-authentication-plugin.html
2024-02-18 10:42:21 +00:00
// http://dev.mysql.com/doc/refman/5.7/en/pam-authentication-plugin.html
2024-02-18 10:42:21 +00:00
return append([]byte(mc.cfg.Passwd), 0), nil
case "mysql_native_password":
2024-02-18 10:42:21 +00:00
if !mc.cfg.AllowNativePasswords {
2024-02-18 10:42:21 +00:00
return nil, ErrNativePassword
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// https://dev.mysql.com/doc/internals/en/secure-password-authentication.html
2024-02-18 10:42:21 +00:00
// Native password authentication only need and will need 20-byte challenge.
2024-02-18 10:42:21 +00:00
authResp := scramblePassword(authData[:20], mc.cfg.Passwd)
2024-02-18 10:42:21 +00:00
return authResp, nil
case "sha256_password":
2024-02-18 10:42:21 +00:00
if len(mc.cfg.Passwd) == 0 {
2024-02-18 10:42:21 +00:00
return []byte{0}, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if mc.cfg.tls != nil || mc.cfg.Net == "unix" {
2024-02-18 10:42:21 +00:00
// write cleartext auth packet
2024-02-18 10:42:21 +00:00
return append([]byte(mc.cfg.Passwd), 0), nil
2024-02-18 10:42:21 +00:00
}
pubKey := mc.cfg.pubKey
2024-02-18 10:42:21 +00:00
if pubKey == nil {
2024-02-18 10:42:21 +00:00
// request public key from server
2024-02-18 10:42:21 +00:00
return []byte{1}, nil
2024-02-18 10:42:21 +00:00
}
// encrypted password
2024-02-18 10:42:21 +00:00
enc, err := encryptPassword(mc.cfg.Passwd, authData, pubKey)
2024-02-18 10:42:21 +00:00
return enc, err
default:
2024-02-18 10:42:21 +00:00
errLog.Print("unknown auth plugin:", plugin)
2024-02-18 10:42:21 +00:00
return nil, ErrUnknownPlugin
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (mc *mysqlConn) handleAuthResult(oldAuthData []byte, plugin string) error {
2024-02-18 10:42:21 +00:00
// Read Result Packet
2024-02-18 10:42:21 +00:00
authData, newPlugin, err := mc.readAuthResult()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// handle auth plugin switch, if requested
2024-02-18 10:42:21 +00:00
if newPlugin != "" {
2024-02-18 10:42:21 +00:00
// If CLIENT_PLUGIN_AUTH capability is not supported, no new cipher is
2024-02-18 10:42:21 +00:00
// sent and we have to keep using the cipher sent in the init packet.
2024-02-18 10:42:21 +00:00
if authData == nil {
2024-02-18 10:42:21 +00:00
authData = oldAuthData
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
// copy data from read buffer to owned slice
2024-02-18 10:42:21 +00:00
copy(oldAuthData, authData)
2024-02-18 10:42:21 +00:00
}
plugin = newPlugin
authResp, err := mc.auth(authData, plugin)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err = mc.writeAuthSwitchPacket(authResp); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// Read Result Packet
2024-02-18 10:42:21 +00:00
authData, newPlugin, err = mc.readAuthResult()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// Do not allow to change the auth plugin more than once
2024-02-18 10:42:21 +00:00
if newPlugin != "" {
2024-02-18 10:42:21 +00:00
return ErrMalformPkt
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
switch plugin {
// https://insidemysql.com/preparing-your-community-connector-for-mysql-8-part-2-sha256/
2024-02-18 10:42:21 +00:00
case "caching_sha2_password":
2024-02-18 10:42:21 +00:00
switch len(authData) {
2024-02-18 10:42:21 +00:00
case 0:
2024-02-18 10:42:21 +00:00
return nil // auth successful
2024-02-18 10:42:21 +00:00
case 1:
2024-02-18 10:42:21 +00:00
switch authData[0] {
2024-02-18 10:42:21 +00:00
case cachingSha2PasswordFastAuthSuccess:
2024-02-18 10:42:21 +00:00
if err = mc.readResultOK(); err == nil {
2024-02-18 10:42:21 +00:00
return nil // auth successful
2024-02-18 10:42:21 +00:00
}
case cachingSha2PasswordPerformFullAuthentication:
2024-02-18 10:42:21 +00:00
if mc.cfg.tls != nil || mc.cfg.Net == "unix" {
2024-02-18 10:42:21 +00:00
// write cleartext auth packet
2024-02-18 10:42:21 +00:00
err = mc.writeAuthSwitchPacket(append([]byte(mc.cfg.Passwd), 0))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
pubKey := mc.cfg.pubKey
2024-02-18 10:42:21 +00:00
if pubKey == nil {
2024-02-18 10:42:21 +00:00
// request public key from server
2024-02-18 10:42:21 +00:00
data, err := mc.buf.takeSmallBuffer(4 + 1)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
data[4] = cachingSha2PasswordRequestPublicKey
2024-02-18 10:42:21 +00:00
mc.writePacket(data)
// parse public key
2024-02-18 10:42:21 +00:00
if data, err = mc.readPacket(); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
block, rest := pem.Decode(data[1:])
2024-02-18 10:42:21 +00:00
if block == nil {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("No Pem data found, data: %s", rest)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
pkix, err := x509.ParsePKIXPublicKey(block.Bytes)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
pubKey = pkix.(*rsa.PublicKey)
2024-02-18 10:42:21 +00:00
}
// send encrypted password
2024-02-18 10:42:21 +00:00
err = mc.sendEncryptedPassword(oldAuthData, pubKey)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return mc.readResultOK()
default:
2024-02-18 10:42:21 +00:00
return ErrMalformPkt
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return ErrMalformPkt
2024-02-18 10:42:21 +00:00
}
case "sha256_password":
2024-02-18 10:42:21 +00:00
switch len(authData) {
2024-02-18 10:42:21 +00:00
case 0:
2024-02-18 10:42:21 +00:00
return nil // auth successful
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
block, _ := pem.Decode(authData)
2024-02-18 10:42:21 +00:00
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// send encrypted password
2024-02-18 10:42:21 +00:00
err = mc.sendEncryptedPassword(oldAuthData, pub.(*rsa.PublicKey))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return mc.readResultOK()
2024-02-18 10:42:21 +00:00
}
default:
2024-02-18 10:42:21 +00:00
return nil // auth successful
2024-02-18 10:42:21 +00:00
}
return err
2024-02-18 10:42:21 +00:00
}