MySQL 8.0 身份验证插件(caching_sha2_password)

MySQL 5.6/5.7 默认身份验证插件是 mysql_native_password, 依赖于 SHA1 算法

MySQL 5.6 开始支持 sha256_password 身份验证插件。它使用加盐密码(salted password)进行多轮 SHA256 哈希,确保哈希值转换更安全。并且需要在安全连接(ssl)或密码使用 RSA 秘钥对加密传输

MySQL 8.0.3 开始,引入了一个新的身份验证插件 caching_sha2_password

MySQL 8.0.4 开始,caching_sha2_password 插件成为 MySQL 服务器的新默认身份验证插件。

caching_sha2_password 尝试一个两全其美的结合,既解决SHA1安全性问题又解决多轮哈希性能问题。

Fast authentication

  1. 客户端连接服务端
  2. 服务端给客户端发送 Nonce(20 字节长的随机数据)
  3. 客户端使用 XOR(SHA256(password), SHA256(SHA256(SHA256(password)), nonce)) 生成 Scramble 发送给服务端
  4. 服务端检查 username/SHA256(SHA256(user_password)) 是否在内存缓存条目中存在,存在则证明合法;发送 fast_auth_success 包到客户端
  5. 服务端发送 OK 包到客户端
  6. 进入命令阶段

Complete authentication

  1. 客户端连接服务端

  2. 服务端给客户端发送 Nonce(20 字节长的随机数据)

  3. 客户端使用 XOR(SHA256(password), SHA256(SHA256(SHA256(password)), nonce)) 生成 Scramble 发送给服务端

  4. 服务端检查 username/SHA256(SHA256(user_password)) 是否在内存缓存条目中存在,不存在则发送 perform_full_authentication 包到客户端继续认证

  5. 客户端收到 perform_full_authentication 包,可以进行如下处理

    5.1 如果连接已经建立基于 SSL 的安全通道,则可以直接发送明文密码到服务端

    5.2 向服务端发起获取公钥的请求(或者指定服务端公钥文件),使用公钥+Nonce加密密码,发送加密后的密码到服务端

  6. 服务器通过 SHA256 算法计算得到哈希值,判断是否用户认证通过,通过则发送 OK 包到客户端

  7. 进入命令阶段

How caching_sha2_password works?

Plugin caching_sha2_password works in two phases.

1. Fast authentication
2. Complete authentication

If server has cached hash entry for given user in memory, it uses scramble sent by client to perform fast authentication. If it is a success, authentication is done and connection will move to command phase. If there is an error, server will signal client to switch to full authentication that involves sending password over a secure connection server. Server then verifies password against authentication_string for given user account. If it is a success, server caches hash entry for the account and connection enters command phase. If there is an error, server sends error information to client and connection is terminated.

Following section describes state transitions and message exchanges between server and client.

Note that there are additional sanity checks performed by server and client at various steps. Such steps may result into end of communication by either party. However, such sanity checks are not covered in the diagram below.

Legends

参考