Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 495ca05

Browse files
author
Chris Board
committed
Interim support for MySQL 8
Interim support for MySQL 8 with some caveats described in the README.
1 parent 6a11144 commit 495ca05

File tree

15 files changed

+243
-52
lines changed

15 files changed

+243
-52
lines changed
0 Bytes
Binary file not shown.

.idea/caches/gradle_models.ser

-18.6 KB
Binary file not shown.

.idea/codeStyles/Project.xml

Lines changed: 109 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AndroidMySQLConnector/build.gradle

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ else
2020
}
2121

2222
archivesBaseName="AndroidMySQLConnector"
23-
version '0.25'
23+
version '0.28'
2424
group 'com.BoardiesITSolutions'
2525

2626

2727
android {
28-
compileSdkVersion 28
28+
compileSdkVersion 29
2929
defaultConfig {
3030
//applicationId "com.BoardiesITSolutions.AndroidMySQLConnector"
3131
minSdkVersion 19
32-
targetSdkVersion 28
33-
versionCode 14
34-
versionName "0.25"
35-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
32+
targetSdkVersion 29
33+
versionCode 20
34+
versionName "0.28"
35+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
3636
}
3737
buildTypes {
3838
release {
@@ -83,13 +83,11 @@ android {
8383
}
8484

8585
def getArtifactFullPath() {
86-
return ".//build/outputs/aar/${archivesBaseName}-${project.version}.aar"
86+
//return ".//build/outputs/aar/${archivesBaseName}-${project.version}.aar"
87+
return ".//build/outputs/aar/${archivesBaseName}-debug.aar"
8788
}
8889

8990
dependencies {
9091
implementation fileTree(dir: 'libs', include: ['*.jar'])
91-
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
92-
testImplementation 'junit:junit:4.12'
93-
androidTestImplementation 'com.android.support.test:runner:1.0.2'
94-
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
92+
implementation 'androidx.appcompat:appcompat:1.1.0'
9593
}

AndroidMySQLConnector/src/main/java/com/BoardiesITSolutions/AndroidMySQLConnector/Connection.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.BoardiesITSolutions.AndroidMySQLConnector;
22

33
import android.annotation.TargetApi;
4-
import android.content.Context;
54
import android.os.Build;
6-
import android.support.annotation.RequiresApi;
7-
import android.support.v4.app.Fragment;
8-
import android.support.v7.app.AppCompatActivity;
5+
import androidx.annotation.RequiresApi;
6+
import androidx.appcompat.app.AppCompatActivity;
97
import android.util.Log;
108

119

@@ -84,6 +82,8 @@ public class Connection
8482
public static final int CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS = 0x00400000;
8583
public static final int CLIENT_SESSION_TRACK = 0x00800000;
8684

85+
public static final int CLIENT_OPTIONAL_RESULTSET_METADATA = 1 << 25;
86+
8787
//Character Sets
8888
private final int LATIN1_SWEDISH_CI = 0x08;
8989
private final int UTF8_GENERAL_CI = 0x21;
@@ -324,6 +324,11 @@ private void processWelcomePacket() throws MySQLConnException, IOException {
324324
temp = mysqlIO.swapByteArray(temp);
325325
connectionID = mysqlIO.fromByteArray(temp);
326326
byte[] salt1 = (byte[]) mysqlIO.extractData(8);
327+
if (this.majorVersion == 8)
328+
{
329+
//Log.d("Connection", "Version 8 detected. Shifting 1 byte");
330+
//mysqlIO.shiftCurrentBytePosition(1);
331+
}
327332
authSalt = Connection.toString(salt1, 0, salt1.length);
328333
//There is a null terminator at the end of the salt, shift by one as we don't need it
329334
mysqlIO.shiftCurrentBytePosition(1);
@@ -364,9 +369,16 @@ private void processWelcomePacket() throws MySQLConnException, IOException {
364369
//Check if the server is supporting compression, if it does, we need to turn off as we don't
365370
if ((Connection.this.serverCapabilities & CLIENT_COMPRESS) == CLIENT_COMPRESS)
366371
{
372+
Log.d("Connection", "Disabling client compress");
367373
clientCapabilities &= ~CLIENT_COMPRESS;
368374
}
369375

376+
//If MySQL 8 turn off can accept expired password
377+
if (this.getMajorVersion() >= 8)
378+
{
379+
clientCapabilities &= ~CLIENT_OPTIONAL_RESULTSET_METADATA;
380+
}
381+
370382
//Check if the server is set to don't allow database.table, if so unset it so we can
371383
//Having this enabled means SHOW TABLES and SHOW DATABASES can't be executed as it will only
372384
//use the database that we are connected to
@@ -773,7 +785,11 @@ public Statement createStatement()
773785

774786
private void parseVersionNumber()
775787
{
776-
this.serverVersion = this.serverVersion.substring(0, this.serverVersion.indexOf("-")-1);
788+
if (this.serverVersion.contains("-"))
789+
{
790+
this.serverVersion = this.serverVersion.substring(0, this.serverVersion.indexOf("-") - 1);
791+
}
792+
777793
this.serverVersion = this.serverVersion.replaceAll("[^\\d.]", "");
778794
if ((this.serverVersion != null) && this.serverVersion.length() > 0)
779795
{

AndroidMySQLConnector/src/main/java/com/BoardiesITSolutions/AndroidMySQLConnector/MySQLIO.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.BoardiesITSolutions.AndroidMySQLConnector;
22

33
import android.os.Build;
4-
import android.support.annotation.RequiresApi;
4+
import androidx.annotation.RequiresApi;
55
import android.util.Log;
66

77
import java.io.BufferedInputStream;
88
import java.io.BufferedOutputStream;
99
import java.io.ByteArrayOutputStream;
1010
import java.io.IOException;
1111
import java.net.Socket;
12-
import java.nio.charset.Charset;
13-
import java.nio.charset.StandardCharsets;
1412
import java.util.ArrayList;
1513
import java.util.Arrays;
1614
import java.util.List;

AndroidMySQLConnector/src/main/java/com/BoardiesITSolutions/AndroidMySQLConnector/PacketManager/AuthResponse.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.BoardiesITSolutions.AndroidMySQLConnector.PacketManager;
22

3+
import android.os.Message;
34
import android.util.Log;
45

56
import com.BoardiesITSolutions.AndroidMySQLConnector.Connection;
@@ -10,6 +11,7 @@
1011
import java.io.UnsupportedEncodingException;
1112
import java.nio.ByteBuffer;
1213
import java.nio.charset.Charset;
14+
import java.nio.charset.StandardCharsets;
1315
import java.security.MessageDigest;
1416
import java.security.NoSuchAlgorithmException;
1517

@@ -130,7 +132,38 @@ private void createAuthenticationPacket() throws IOException
130132
else
131133
{
132134
Log.e("AuthResponse", "Only mysql_native_password is currently supported");
133-
throw new UnsupportedOperationException("mysql_native_password is currently only supported. Please use this authentication method");
135+
Log.d("AuthResponse", "Auth Plugin Name: " + this.mysqlConn.getAuthPluginName());
136+
if (this.mysqlConn.getAuthPluginName().equals("caching_sha2_password"))
137+
{
138+
Log.d("AuthResponse", "Using MySQL Caching Sha2 Password");
139+
140+
String password = this.mysqlConn.getPassword();
141+
142+
MessageDigest hash1MD = sha256();
143+
byte[] hash1 = hash1MD.digest(password.getBytes(StandardCharsets.UTF_8));
144+
MessageDigest hash2MD = sha256();
145+
hash2MD.update(hash1MD.digest(hash1));
146+
hash2MD.update(this.mysqlConn.getAuthSalt().getBytes(StandardCharsets.UTF_8));
147+
byte[] hash2 = hash1MD.digest();
148+
149+
byte[] hash3 = new byte[hash1.length];
150+
//Perform XOR
151+
int i = 0;
152+
for (byte b : hash1)
153+
{
154+
hash3[i] = (byte) (b ^ hash2[i++]);
155+
}
156+
157+
dataOutPacket.write(hash3);
158+
159+
//byte[] hash1 = sha256String(password);
160+
//byte[] hash2 = sha256Bytes(hash1);
161+
162+
}
163+
else
164+
{
165+
throw new UnsupportedOperationException("mysql_native_password is currently only supported. Please use this authentication method");
166+
}
134167
}
135168

136169
//Add the database if we have a database available
@@ -199,6 +232,22 @@ private void createAuthenticationPacket() throws IOException
199232
ex.printStackTrace();
200233
}
201234
}
235+
236+
private MessageDigest sha256() throws NoSuchAlgorithmException
237+
{
238+
return MessageDigest.getInstance("SHA-256");
239+
//byte[] encodedHash = digest.digest(data.getBytes(StandardCharsets.UTF_8));
240+
241+
}
242+
243+
/**
244+
* Uses for MySQL Native Password
245+
* @param password
246+
* @param seed
247+
* @return
248+
* @throws NoSuchAlgorithmException
249+
* @throws UnsupportedEncodingException
250+
*/
202251
private byte[] scramblePassword(String password, String seed) throws NoSuchAlgorithmException, UnsupportedEncodingException
203252
{
204253
MessageDigest md = MessageDigest.getInstance("SHA-1");

AndroidMySQLConnector/src/main/java/com/BoardiesITSolutions/AndroidMySQLConnector/PacketManager/MySQLErrorPacket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.BoardiesITSolutions.AndroidMySQLConnector.PacketManager;
22

33
import android.os.Build;
4-
import android.support.annotation.RequiresApi;
4+
import androidx.annotation.RequiresApi;
55

66
import com.BoardiesITSolutions.AndroidMySQLConnector.Connection;
77
import com.BoardiesITSolutions.AndroidMySQLConnector.Exceptions.InvalidSQLPacketException;

AndroidMySQLConnector/src/main/java/com/BoardiesITSolutions/AndroidMySQLConnector/PacketManager/MySQLOKPacket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.BoardiesITSolutions.AndroidMySQLConnector.PacketManager;
22

3-
import android.support.v7.widget.ThemedSpinnerAdapter;
3+
import androidx.appcompat.widget.ThemedSpinnerAdapter;
44
import android.util.Log;
55

66
import com.BoardiesITSolutions.AndroidMySQLConnector.Connection;

0 commit comments

Comments
 (0)