You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
mysql: require pinned server public key for caching_sha2 full auth - #3779
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:
Please make sure your changes are compilable.
When providing us with a new feature, it is best to add related tests.
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.
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.
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.
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.
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.
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.
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.
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.
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.
[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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
MysqlAuthenticatorgainsset_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.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:
test/brpc_mysql_full_auth_unittest.cppdrives 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.ServerPublicKeyMatchescases totest/brpc_mysql_auth_scramble_unittest.cpp(same key, PKCS#1 vs SubjectPublicKeyInfo encodings, invalid input).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