> For the complete documentation index, see [llms.txt](https://mariadb.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mariadb.com/docs/server/reference/clientserver-protocol/1-connecting/caching_sha2_password-authentication-plugin.md). Agents can also search and read this documentation through its [MCP server](https://mariadb.com/docs/~gitbook/mcp).

# Connecting via caching\_sha2\_password

This plugin implements the caching\_sha2\_password authentication method, using an in-memory cache for fast authentication or RSA encryption for full verification.

## Overview

Caching `SHA256` first sends an `SHA256`-encrypted password. MySQL server has an in-memory cache of `SHA256` key for successful authentication. When a cache hit occurs, the connection is validated. Otherwise, authentication continues with additional steps similar to the [sha256\_password](https://mariadb.com/docs/server/reference/clientserver-protocol/1-connecting/sha256_password-plugin.md) process. The cache is MySQL server behavior. MariaDB Server does not cache and always requests full authentication.

Caching SHA256 authentication possible exchanges:

* Client sends an [SHA-2 encrypted password](#sha-2-encrypted-password).
* Server result is either [OK\_Packet](https://mariadb.com/docs/server/reference/clientserver-protocol/4-server-response-packets/ok_packet.md), [ERR\_Packet](https://mariadb.com/docs/server/reference/clientserver-protocol/4-server-response-packets/err_packet.md), or ["fast" authentication result](#fast-authentication-result).
* If the fast authentication result is `0x04` (full authentication required):
  * If the connection is secure (TLS is in use, or the transport is not TCP, such as a Unix socket):
    * Client sends a [clear password answer](#client-clear-password-answer).
  * Else:
    * If client doesn't know server RSA public key:
      * Client sends a [public key request](#public-key-request).
      * Server sends a [public key response](#public-key-response).
    * Client sends an [RSA encrypted password](#rsa-encrypted-password).
    * Ends with the server sending either an [OK\_Packet](https://mariadb.com/docs/server/reference/clientserver-protocol/4-server-response-packets/ok_packet.md) or an [ERR\_Packet](https://mariadb.com/docs/server/reference/clientserver-protocol/4-server-response-packets/err_packet.md).

## Authentication

### SHA-2 Encrypted Password

Encryption is `XOR`(`SHA256`(password), `SHA256`(`SHA256`(`SHA256`(password)), seed)).

* [byte<32>](https://mariadb.com/docs/server/reference/clientserver-protocol/protocol-data-types.md#fixed-length-bytes) encrypted password.

### "Fast" Authentication Result

Result of fast authentication.

* [byte<1>](https://mariadb.com/docs/server/reference/clientserver-protocol/protocol-data-types.md#fixed-length-bytes) fixed 0x01 value.
* [byte\<lenenc>](https://mariadb.com/docs/server/reference/clientserver-protocol/protocol-data-types.md#length-encoded-bytes) authentication result.

`0x03` value means successful authentication.\
`0x04` value means continue.

### Client Clear Password Answer

* [string\<NUL>](https://mariadb.com/docs/server/reference/clientserver-protocol/protocol-data-types.md#null-terminated-strings) password without encryption.

### Public Key Request

{% hint style="warning" %}
Value sent is not `0x01` like sha256\_password uses, but `0x02`.
{% endhint %}

* [byte<1>](https://mariadb.com/docs/server/reference/clientserver-protocol/protocol-data-types.md#fixed-length-bytes) fixed 0x02 value.

### Public Key Response

* [byte<1>](https://mariadb.com/docs/server/reference/clientserver-protocol/protocol-data-types.md#fixed-length-bytes) fixed 0x01 value.
* [byte\<EOF>](https://mariadb.com/docs/server/reference/clientserver-protocol/protocol-data-types.md#end-of-file-length-bytes) public key data.

### RSA Encrypted Password

* [byte\<n>](https://mariadb.com/docs/server/reference/clientserver-protocol/protocol-data-types.md#fixed-length-bytes) RSA encrypted password, where *n* is the RSA key size in bytes (256 for a 2048-bit key).

RSA encrypted value of the password XORed with the seed, using the server public key (`RSA_PKCS1_OAEP_PADDING`). The password includes its terminating `NUL` byte. The seed is the raw 20-byte scramble sent by the server, repeated as needed: byte *i* of the password is XORed with byte *i* mod 20 of the seed. The seed is not hashed.

<sub>*This page is licensed: CC BY-SA / Gnu FDL*</sub>

{% @marketo/form formId="4316" %}
