-
-
Notifications
You must be signed in to change notification settings - Fork 139
Implement password encryption using an RSA public key #373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
oshai
merged 4 commits into
jasync-sql:master
from
KarboniteKream:feat/rsa-public-key-encryption
Jan 26, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
10 changes: 9 additions & 1 deletion
10
...l-async/src/main/java/com/github/jasync/sql/db/mysql/encoder/auth/AuthenticationMethod.kt
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
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
20 changes: 10 additions & 10 deletions
20
...ain/java/com/github/jasync/sql/db/mysql/encoder/auth/CachingSha2PasswordAuthentication.kt
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
12 changes: 9 additions & 3 deletions
12
...ain/java/com/github/jasync/sql/db/mysql/encoder/auth/MySQLNativePasswordAuthentication.kt
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
12 changes: 9 additions & 3 deletions
12
...nc/src/main/java/com/github/jasync/sql/db/mysql/encoder/auth/OldPasswordAuthentication.kt
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
60 changes: 60 additions & 0 deletions
60
mysql-async/src/main/java/com/github/jasync/sql/db/mysql/encoder/auth/README.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Authentication methods | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome! |
||
|
||
This driver implements multiple authentication methods available in MySQL/MariaDB and PostgresSQL. | ||
The step-by-step authentication flow and implementation details are described below. | ||
|
||
## `caching_sha2_password` | ||
|
||
This is the default authentication method since MySQL 8.0. | ||
Official documentation can be found [here][caching-sha2-password]. | ||
|
||
The fast authentication flow (using password scrambling) is as follows: | ||
1. During the handshake, MySQL server sends the authentication seed (nonce). | ||
2. The driver scrambles the password using SHA-256 with `AuthenticationScrambler`, and sends `HandshakeResponse`. | ||
3. If the password entry is cached on the server, it performs fast authentication, and returns `AuthMoreData` | ||
message indicating success (`data=3`). This is followed by `OkMessage` and the authentication flow completes. | ||
4. In case the password is not cached, the server requires us to switch to full authentication, and returns | ||
`AuthMoreData` with `data=4`. | ||
|
||
If we need to perform the full authentication flow (using SHA-256 hashing), the process is as follows: | ||
1. If we're connected over SSL, we can send `AuthenticationSwitchResponse` with a plaintext password. | ||
Note that if we try to do the same over an unsafe connection, the server always rejects the password. | ||
2. If we are not connected over SSL, we can use the provided `rsaPublicKey` (used by the server) to encrypt the | ||
password, and send it as `AuthenticationSwitchResponse`. See `Sha256PasswordAuthentication` for | ||
implementation details. | ||
3. If `rsaPublicKey` is not specified, the public key used to encrypt the password can be fetched from the | ||
server. **This is currently not supported by the driver.** | ||
4. If the authentication was successful, the server caches the password entry, and returns `OkMessage`. | ||
The next authentication request for the specified user can therefore be done with fast authentication. | ||
|
||
## `sha256_password` | ||
|
||
This authentication method has been deprecated in favor of `caching_sha2_password` in MySQL 8.0, and works the | ||
same as its full authentication flow. | ||
|
||
## `mysql_native_password` | ||
|
||
This was the default authentication method until MySQL 8.0. | ||
Official documentation can be found [here][mysql-native-password]. | ||
|
||
The authentication flow is as follows: | ||
1. During the handshake, MySQL server sends the authentication seed (nonce). | ||
2. The driver scrambles the password using SHA-1 with `AuthenticationScrambler`, and sends `HandshakeResponse`. | ||
3. If the password is correct the server sends `OkMessage`, and `ErrorMessage` otherwise. | ||
|
||
## `mysql_old_password` | ||
|
||
This method was mainly used before MySQL 4.1. It was deprecated in MySQL 5.6 and removed in MySQL 5.7. | ||
Official documentation can be found [here][mysql-old-password]. | ||
|
||
The authentication flow is as follows: | ||
1. During the handshake, MySQL server sends the authentication seed (nonce). This can either be 8 bytes on older | ||
versions of MySQL, or 20 bytes if the server uses the `mysql_native_password` method as the default. In the | ||
latter case, the driver uses the first 8 bytes of the seed. | ||
2. The driver hashes the password using a proprietary algorithm, and sends `HandshakeResponse`. See | ||
`OldPasswordAuthentication` for implementation details. | ||
3. If the password is correct the server sends `OkMessage`, and `ErrorMessage` otherwise. | ||
|
||
[caching-sha2-password]: https://dev.mysql.com/doc/dev/mysql-server/8.0.32/page_caching_sha2_authentication_exchanges.html | ||
[mysql-native-password]: https://dev.mysql.com/doc/dev/mysql-server/8.0.32/page_protocol_connection_phase_authentication_methods_native_password_authentication.html | ||
[mysql-old-password]: https://dev.mysql.com/doc/dev/mysql-server/8.0.32/page_protocol_connection_phase_authentication_methods.html#page_protocol_connection_phase_authentication_methods_old_password_authentication |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.