Skip to content

Support caching_sha2_password full auth via RSA on non-TLS connections#81

Merged
jappeace merged 1 commit intowinterland1989:masterfrom
ikaro1192:rsa-full-auth
Apr 12, 2026
Merged

Support caching_sha2_password full auth via RSA on non-TLS connections#81
jappeace merged 1 commit intowinterland1989:masterfrom
ikaro1192:rsa-full-auth

Conversation

@ikaro1192
Copy link
Copy Markdown
Contributor

Summary

This PR implements the RSA public key encryption path for caching_sha2_password full authentication on non-TLS (plain TCP and Unix socket) connections.

Previously, when the server requested full authentication (status 0x04) on a non-TLS connection, plainFullAuth threw an AuthException — as noted in #72 ("Plain TCP connections throw an informative AuthException when full auth is
required (RSA not yet implemented)"). This meant caching_sha2_password only worked when:

  • The password verifier was already cached on the server (fast auth path), or
  • The connection used TLS

With this change, the client performs the full RSA handshake:

  1. Sends 0x02 to request the server's RSA public key
  2. Parses the PEM-encoded SubjectPublicKeyInfo (PKCS#8) response
  3. Encrypts (password ++ NUL) XOR cycled_nonce using RSA-OAEP with SHA1
  4. Sends the ciphertext and reads the final OK/ERR

The nonce is passed to plainFullAuth via partial application, keeping the completeAuth callback signature and the TLS module unchanged.

New dependencies

  • asn1-encoding — DER decoding for the RSA public key
  • asn1-types — ASN1 type definitions (SubjectPublicKeyInfo parsing)

plainFullAuth previously threw an AuthException when the MySQL server
requested full authentication (0x04) on a plain TCP or Unix socket
connection. This meant caching_sha2_password only worked when the
password verifier was already cached on the server (fast auth path)
or when using TLS.

Replace the error with an RSA-based full authentication flow:
request the server's RSA public key (0x02), parse the PEM-encoded
SubjectPublicKeyInfo, encrypt (password XOR cycled_nonce) using
RSA-OAEP with SHA1, and send the ciphertext. The nonce is captured
via partial application at the call site, keeping the completeAuth
callback signature unchanged.
@jappeace jappeace merged commit d482bce into winterland1989:master Apr 12, 2026
13 checks passed
@jappeace
Copy link
Copy Markdown
Collaborator

Best would be if we had integration tests as well for this code path.
but this is a lot better, thanks 🙏🏽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants