Skip to content

mysql: require pinned server public key for caching_sha2 full auth - #3779

Open
wwbmmm wants to merge 5 commits into
apache:masterfrom
wwbmmm:oncall/req-20261121-025731
Open

wwbmmm wants to merge 5 commits into
apache:masterfrom
wwbmmm:oncall/req-20261121-025731

Conversation

@wwbmmm

@wwbmmm wwbmmm commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: resolve

Problem Summary:
On a plain-TCP connection using caching_sha2_password, when the server answers the fast scramble with AuthMoreData 0x04 (perform_full_authentication), the client automatically requests the server's RSA public key in-band and encrypts (password || NUL) XOR salt with it. The key is not authenticated and there was no way to pin it (no equivalent of libmysqlclient's --get-server-public-key / --server-public-key-path), so the password could be recovered by whoever supplied that key.

What is changed and the side effects?

Changed:

  • MysqlAuthenticator gains set_server_public_key(pem): the server public key can be pinned out of band (PEM, obtained from a trusted channel). The pinned key is carried to the connection phase through the existing per-connection auth context.
  • On a non-TLS connection, the 0x04 full-authentication branch now fails immediately, without asking for the server key, when no key is pinned. The RPC error text and the error log tell the operator to use TLS or pin the key.
  • When a key is pinned, the PEM received from the server is parsed and compared (as DER, so encoding variations do not matter) with the pinned key before the password is encrypted; a mismatch fails the authentication and no ciphertext is sent.
  • The TLS/secure-transport branch and the caching_sha2 fast-auth path are unchanged.
  • Docs (docs/cn/mysql_client.md) and test/README_mysql_auth.md updated.

Side effects:

  • Performance effects: none outside the authentication exchange (one PEM parse for the pinned-key comparison).

  • Breaking backward compatibility: caching_sha2_password full authentication over a non-TLS connection now requires a pinned key. Users hitting that path must switch to TLS or call MysqlAuthenticator::set_server_public_key().

Tests:

  • New test/brpc_mysql_full_auth_unittest.cpp drives a real brpc mysql Channel against a fake MySQL server: 0x04 without a pinned key fails before requesting the key; a mismatched key fails without sending the password; the matching pinned key succeeds and the server recovers the password from the RSA-OAEP ciphertext; the secure-transport branch still sends the password in the clear.
  • Added ServerPublicKeyMatches cases to test/brpc_mysql_auth_scramble_unittest.cpp (same key, PKCS#1 vs SubjectPublicKeyInfo encodings, invalid input).
  • Ran cmake -S . -B build -DBUILD_UNIT_TESTS=ON -DDOWNLOAD_GTEST=OFF -DBRPC_SYSTEM_GTEST_SOURCE_DIR=/usr/src/googletest, make -j6, and the mysql unit tests: brpc_mysql_full_auth_unittest (4 passed), brpc_mysql_auth_handshake_unittest (28), brpc_mysql_auth_packet_unittest (27), brpc_mysql_auth_scramble_unittest (38), brpc_mysql_reply_parse_unittest (36). Integration tests skip without a live mysqld.

Check List:


🤖 This PR was automatically created by brpc-oncall

On a plain-TCP connection the RSA public key a MySQL server sends during
caching_sha2_password full authentication arrives in-band, before the server
is authenticated.  Encrypting the password with it makes the password
recoverable by whoever supplied the key.

Refuse the exchange unless the caller pinned the server public key with
MysqlAuthenticator::set_server_public_key(); the received PEM must then match
the pinned key before the password is encrypted.  TLS connections and the
fast-auth path are unchanged.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A moderate active issue remains because valid PKCS#1 public keys are rejected, so the advertised equivalent encodings are not handled correctly.

1 open issue
What changed in this PR

This pull request hardens MySQL caching_sha2_password full authentication by requiring an out-of-band pinned RSA public key for plain-TCP connections, while preserving TLS behavior and fast authentication.

Changes:

  • Adds pinned server public-key configuration and propagates it through the MySQL authentication context.
  • Validates the server key before RSA encryption and rejects unpinned plain-TCP full authentication.
  • Adds end-to-end fake-server tests, key-format tests, and documentation updates.
File Description
test/​README_mysql_auth.md Documents the plain-TCP full-authentication test scenarios.
test/​brpc_mysql_full_auth_unittest.cpp Adds fake-server end-to-end tests for pinned-key authentication and secure-transport behavior.
test/​brpc_mysql_auth_scramble_unittest.cpp Tests public-key matching across PEM encodings and invalid inputs.
src/​brpc/​policy/​mysql/​mysql_protocol.cpp Enforces pinned-key validation during the caching_sha2_password exchange.
src/​brpc/​policy/​mysql/​mysql_authenticator.h Exposes server public-key configuration on MysqlAuthenticator.
src/​brpc/​policy/​mysql/​mysql_authenticator.cpp Serializes and parses the pinned key through the connection authentication context.
src/​brpc/​policy/​mysql/​mysql_auth_scramble.h Declares public-key comparison and documents the security contract.
src/​brpc/​policy/​mysql/​mysql_auth_scramble.cpp Implements PEM public-key parsing and DER comparison.
docs/​cn/​mysql_client.md Documents caching_sha2_password support and public-key pinning.

Review effort: Lite

PEM_read_bio_PUBKEY() only reads SubjectPublicKeyInfo before OpenSSL 3.0,
so a pinned key in the PKCS#1 "BEGIN RSA PUBLIC KEY" form was rejected even
though it encodes the same key.  Fall back to PEM_read_bio_RSAPublicKey()
and re-encode it as SubjectPublicKeyInfo so both PEM forms compare equal.
The key-format test now embeds a matching PKCS#1 PEM instead of generating
one at runtime.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

An active moderate issue remains because OpenSSL 3 builds still reject valid PKCS#1 RSA public-key inputs, causing the new compatibility test to fail and preventing pins in that encoding from being accepted.

1 open issue
1 resolved since last review

Review effort: Lite

PEM_read_bio_PUBKEY() does not accept the PKCS#1 "BEGIN RSA PUBLIC KEY"
form on every supported OpenSSL version.  Instead of relying on it (or on
deprecated RSA PEM helpers), decode a PKCS#1 block explicitly and wrap the
RSAPublicKey DER in a SubjectPublicKeyInfo DER, so the pinned key and the
key sent by the server are always compared in the same encoding.  Tests
cover the PKCS#1 form, CRLF line endings and malformed blocks.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The change still has a PKCS#1 RSA key compatibility gap in the authentication path and a non-portable test dependency on a transitive include.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

High severity Include <thread> for std::thread usage

test/​brpc_mysql_full_auth_unittest.cpp:40

This test uses std::thread at lines 294 and 364, but the new include list only includes <future>. <future> is not required to provide the std::thread declaration, so this relies on a non-portable transitive include and can fail to compile with a different standard library. Include <thread> directly.

1 resolved since last review

Review effort: Lite

CachingSha2PasswordRsaEncrypt() still loaded the server key only with
PEM_read_bio_PUBKEY(), so a server key in the PKCS#1 form would pass the
pinned-key comparison and then fail encryption.  Factor the PEM parsing into
ReadPublicKeyPem(), which normalizes the PKCS#1 form to SubjectPublicKeyInfo
and decodes it uniformly, and use it from both the comparison and the
encryptor.  Cover the PKCS#1 form in the RSA round-trip test, and include
<thread> directly in the fake-server test.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because the parser accepts malformed authentication context with too few fields, allowing invalid data to proceed instead of being rejected.

No open issues 🎉

1 resolved since last review

Review effort: Lite

MysqlParseAuthenticator() returned void and cleared the fields when the
serialized authentication string had too few fields, so the connection-phase
handler went on to build a handshake response from empty credentials.  The
string is produced by MysqlAuthenticator::SerializeToString() in the same
process, so a short (or undecodable) string is a malformed state: return
false and fail the authentication instead, and require the five fields the
serializer always writes.
@wwbmmm
wwbmmm requested a lite review from Copilot November 28, 2026 03:45
@wwbmmm

wwbmmm commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

[replied by brpc-oncall robot] On the malformed-authentication-context note: that string never comes from the network — AuthContext::user() is filled by MysqlAuthenticator::SerializeToString() in the same process, and HandleAuthentication re-parses it only to recover the credentials. Even so, the parse is no longer lenient: MysqlParseAuthenticator now returns false when the string has fewer fields than the serializer always writes (5 fields) or carries a non-base64 pinned key, and the connection-phase handler fails the authentication instead of proceeding with empty credentials.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Because this security-sensitive authentication change adds custom PEM/DER handling, maintainer review and validation across supported OpenSSL versions and real MySQL server configurations are still needed beyond the provided fake-server tests.

No open issues 🎉

Review effort: Lite

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