-
-
Notifications
You must be signed in to change notification settings - Fork 138
Support caching_sha2_password
authentication mode
#358
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 5 commits into
jasync-sql:master
from
KarboniteKream:feat/caching-sha2-password
Jan 15, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6873171
Support `caching_sha2_password` authentication mode
KarboniteKream 4e47a79
Revert some changes to `AuthenticationSwitchRequestDecoder`
KarboniteKream 7f3c8d9
Fix old password authentication when receiving native authentication …
KarboniteKream 1cfae44
Add authentication tests
KarboniteKream f618ee3
Remove a trailing curly brace in logs
KarboniteKream 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
13 changes: 13 additions & 0 deletions
13
mysql-async/src/main/java/com/github/jasync/sql/db/mysql/decoder/AuthMoreDataDecoder.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.github.jasync.sql.db.mysql.decoder | ||
|
||
import com.github.jasync.sql.db.mysql.message.server.AuthMoreDataMessage | ||
import com.github.jasync.sql.db.mysql.message.server.ServerMessage | ||
import io.netty.buffer.ByteBuf | ||
|
||
class AuthMoreDataDecoder : MessageDecoder { | ||
override fun decode(buffer: ByteBuf): ServerMessage { | ||
return AuthMoreDataMessage( | ||
data = buffer.readByte(), | ||
) | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
...sync/src/main/java/com/github/jasync/sql/db/mysql/encoder/auth/AuthenticationScrambler.kt
10000
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,44 @@ | ||
package com.github.jasync.sql.db.mysql.encoder.auth | ||
|
||
import com.github.jasync.sql.db.util.length | ||
import java.nio.charset.Charset | ||
import java.security.MessageDigest | ||
import kotlin.experimental.xor | ||
|
||
object AuthenticationScrambler { | ||
|
||
fun scramble411( | ||
algorithm: String, | ||
password: String, | ||
charset: Charset, | ||
seed: ByteArray, | ||
seedFirst: Boolean, | ||
): ByteArray { | ||
val messageDigest = MessageDigest.getInstance(algorithm) | ||
val initialDigest = messageDigest.digest(password.toByteArray(charset)) | ||
|
||
messageDigest.reset() | ||
|
||
val finalDigest = messageDigest.digest(initialDigest) | ||
|
||
messageDigest.reset() | ||
|
||
if (seedFirst) { | ||
messageDigest.update(seed) | ||
messageDigest.update(finalDigest) | ||
} else { | ||
messageDigest.update(finalDigest) | ||
messageDigest.update(seed) | ||
} | ||
|
||
val result = messageDigest.digest() | ||
var counter = 0 | ||
|
||
while (counter < result.length) { | ||
result[counter] = (result[counter] xor initialDigest[counter]) | ||
counter += 1 | ||
} | ||
|
||
return result | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.github.jasync.sql.db.mysql.encoder.auth | ||
|
||
import java.nio.charset.Charset | ||
|
||
object CachingSha2PasswordAuthentication : AuthenticationMethod { | ||
|
||
private val EmptyArray = ByteArray(0) | ||
|
||
override fun generateAuthentication(charset: Charset, password: String?, seed: ByteArray?): ByteArray { | ||
return if (password != null) { | ||
if (seed != null) { | ||
// Fast authentication mode. Requires seed, but not SSL. | ||
AuthenticationScrambler.scramble411("SHA-256", password, charset, seed, false) | ||
} else { | ||
// Full authentication mode. | ||
// Since this sends the plaintext password, SSL is required. | ||
// Without SSL, the server always rejects the password. | ||
Sha256PasswordAuthentication.generateAuthentication(charset, password, null) | ||
} | ||
} else { | ||
EmptyArray | ||
} | ||
} | ||
} |
35 changes: 4 additions & 31 deletions
35
...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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,18 @@ | ||
package com.github.jasync.sql.db.mysql.encoder.auth | ||
|
||
import com.github.jasync.sql.db.util.length | ||
import java.nio.charset.Charset | ||
import java.security.MessageDigest | ||
import kotlin.experimental.xor | ||
|
||
object MySQLNativePasswordAuthentication : AuthenticationMethod { | ||
|
||
val EmptyArray = ByteArray(0) | ||
private val EmptyArray = ByteArray(0) | ||
|
||
override fun generateAuthentication(charset: Charset, password: String?, seed: ByteArray): ByteArray { | ||
override fun generateAuthentication(charset: Charset, password: String?, seed: ByteArray?): ByteArray { | ||
requireNotNull(seed) { "Seed should not be null" } | ||
|
||
return if (password != null) { | ||
scramble411(charset, password, seed) | ||
AuthenticationScrambler.scramble411("SHA-1", password, charset, seed, true) | ||
} else { | ||
EmptyArray | ||
} | ||
} | ||
|
||
private fun scramble411(charset: Charset, password: String, seed: ByteArray): ByteArray { | ||
|
||
val messageDigest = MessageDigest.getInstance("SHA-1") | ||
val initialDigest = messageDigest.digest(password.toByteArray(charset)) | ||
|
||
messageDigest.reset() | ||
|
||
val finalDigest = messageDigest.digest(initialDigest) | ||
|
||
messageDigest.reset() | ||
|
||
messageDigest.update(seed) | ||
messageDigest.update(finalDigest) | ||
|
||
val result = messageDigest.digest() | ||
var counter = 0 | ||
|
||
while (counter < result.length) { | ||
result[counter] = (result[counter] xor initialDigest[counter]) | ||
counter += 1 | ||
} | ||
|
||
return result | ||
} | ||
} |
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 |
---|---|---|
|
@@ -7,12 +7,22 @@ import kotlin.math.floor | |
@Suppress("RedundantExplicitType", "UNUSED_VALUE", "VARIABLE_WITH_REDUNDANT_INITIALIZER") | ||
object OldPasswordAuthentication : AuthenticationMethod { | ||
|
||
val EmptyArray = ByteArray(0) | ||
private val EmptyArray = ByteArray(0) | ||
|
||
override fun generateAuthentication(charset: Charset, password: String?, seed: ByteArray?): ByteArray { | ||
requireNotNull(seed) { "Seed should not be null" } | ||
|
||
override fun generateAuthentication(charset: Charset, password: String?, seed: ByteArray): ByteArray { | ||
return when { | ||
password != null && password.isNotEmpty() -> { | ||
newCrypt(charset, password, String(seed, charset)) | ||
!password.isNullOrEmpty() -> { | ||
// The native authentication handshake will provide a 20-byte challenge. | ||
// Use the first 8 bytes as the old password authentication challenge. | ||
val challenge = if (seed.length == 20) { | ||
seed.copyOf(8) | ||
} else { | ||
seed | ||
} | ||
Comment on lines
+17
to
+23
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. Without this, a "bad handshake" error is thrown. Reference
|
||
|
||
newCrypt(charset, password, String(challenge, charset)) | ||
} | ||
else -> EmptyArray | ||
} | ||
|
21 changes: 21 additions & 0 deletions
21
...src/main/java/com/github/jasync/sql/db/mysql/encoder/auth/Sha256PasswordAuthentication.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.github.jasync.sql.db.mysql.encoder.auth | ||
|
||
import com.github.jasync.sql.db.util.length | ||
import java.nio.charset.Charset | ||
|
||
// TODO: Implement public key encryption. | ||
object Sha256PasswordAuthentication : AuthenticationMethod { | ||
|
||
private val EmptyArray = ByteArray(0) | ||
|
||
override fun generateAuthentication(charset: Charset, password: String?, seed: ByteArray?): ByteArray { | ||
return if (password != null) { | ||
val bytes = password.toByteArray(charset) | ||
val result = ByteArray(bytes.length + 1) | ||
bytes.copyInto(result) | ||
result | ||
} else { | ||
EmptyArray | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...-async/src/main/java/com/github/jasync/sql/db/mysql/message/server/AuthMoreDataMessage.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.github.jasync.sql.db.mysql.message.server | ||
|
||
data class AuthMoreDataMessage( | ||
val data: Byte, | ||
) : ServerMessage(AuthMoreData) { | ||
fun isSuccess(): Boolean { | ||
return data == 3.toByte() | ||
} | ||
} |
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
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.