mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-09-02 15:15:09 +00:00
(un)serialize will now throw instead of failing silently
This commit is contained in:
@@ -23,6 +23,7 @@ package org.kde.kdeconnect.Helpers.SecurityHelpers;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
import android.util.Base64;
|
import android.util.Base64;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -107,7 +108,7 @@ public class RsaHelper {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NetworkPackage encrypt(NetworkPackage np, PublicKey publicKey) throws GeneralSecurityException {
|
public static NetworkPackage encrypt(NetworkPackage np, PublicKey publicKey) throws GeneralSecurityException, JSONException {
|
||||||
|
|
||||||
String serialized = np.serialize();
|
String serialized = np.serialize();
|
||||||
|
|
||||||
@@ -152,7 +153,7 @@ public class RsaHelper {
|
|||||||
decryptedJson += decryptedChunk;
|
decryptedJson += decryptedChunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
NetworkPackage decrypted = np.unserialize(decryptedJson);
|
NetworkPackage decrypted = NetworkPackage.unserialize(decryptedJson);
|
||||||
decrypted.setPayload(np.getPayload(), np.getPayloadSize());
|
decrypted.setPayload(np.getPayload(), np.getPayloadSize());
|
||||||
return decrypted;
|
return decrypted;
|
||||||
}
|
}
|
||||||
|
@@ -26,6 +26,7 @@ import android.provider.Settings;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.kde.kdeconnect.Helpers.DeviceHelper;
|
import org.kde.kdeconnect.Helpers.DeviceHelper;
|
||||||
|
|
||||||
@@ -134,9 +135,8 @@ public class NetworkPackage {
|
|||||||
|
|
||||||
public boolean isEncrypted() { return mType.equals(PACKAGE_TYPE_ENCRYPTED); }
|
public boolean isEncrypted() { return mType.equals(PACKAGE_TYPE_ENCRYPTED); }
|
||||||
|
|
||||||
public String serialize() {
|
public String serialize() throws JSONException {
|
||||||
JSONObject jo = new JSONObject();
|
JSONObject jo = new JSONObject();
|
||||||
try {
|
|
||||||
jo.put("id", mId);
|
jo.put("id", mId);
|
||||||
jo.put("type", mType);
|
jo.put("type", mType);
|
||||||
jo.put("body", mBody);
|
jo.put("body", mBody);
|
||||||
@@ -144,21 +144,14 @@ public class NetworkPackage {
|
|||||||
jo.put("payloadSize", mPayloadSize);
|
jo.put("payloadSize", mPayloadSize);
|
||||||
jo.put("payloadTransferInfo", mPayloadTransferInfo);
|
jo.put("payloadTransferInfo", mPayloadTransferInfo);
|
||||||
}
|
}
|
||||||
} catch(Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Log.e("NetworkPackage", "Serialization exception");
|
|
||||||
}
|
|
||||||
|
|
||||||
//QJSon does not escape slashes, but Java JSONObject does. Converting to QJson format.
|
//QJSon does not escape slashes, but Java JSONObject does. Converting to QJson format.
|
||||||
String json = jo.toString().replace("\\/","/")+"\n";
|
String json = jo.toString().replace("\\/","/")+"\n";
|
||||||
|
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
static public NetworkPackage unserialize(String s) {
|
static public NetworkPackage unserialize(String s) throws JSONException {
|
||||||
|
|
||||||
NetworkPackage np = new NetworkPackage();
|
NetworkPackage np = new NetworkPackage();
|
||||||
try {
|
|
||||||
JSONObject jo = new JSONObject(s);
|
JSONObject jo = new JSONObject(s);
|
||||||
np.mId = jo.getLong("id");
|
np.mId = jo.getLong("id");
|
||||||
np.mType = jo.getString("type");
|
np.mType = jo.getString("type");
|
||||||
@@ -170,12 +163,6 @@ public class NetworkPackage {
|
|||||||
np.mPayloadTransferInfo = new JSONObject();
|
np.mPayloadTransferInfo = new JSONObject();
|
||||||
np.mPayloadSize = 0;
|
np.mPayloadSize = 0;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Log.e("NetworkPackage", "Unserialization exception unserializing "+s);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return np;
|
return np;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -23,6 +23,7 @@ package org.kde.kdeconnect;
|
|||||||
import android.test.AndroidTestCase;
|
import android.test.AndroidTestCase;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
import org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper;
|
import org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper;
|
||||||
|
|
||||||
import java.security.KeyPair;
|
import java.security.KeyPair;
|
||||||
@@ -44,7 +45,7 @@ public class NetworkPackageTest extends AndroidTestCase{
|
|||||||
// Called after each test
|
// Called after each test
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNetworkPackage(){
|
public void testNetworkPackage() throws JSONException {
|
||||||
NetworkPackage np = new NetworkPackage("com.test");
|
NetworkPackage np = new NetworkPackage("com.test");
|
||||||
|
|
||||||
np.set("hello", "hola");
|
np.set("hello", "hola");
|
||||||
@@ -80,7 +81,7 @@ public class NetworkPackageTest extends AndroidTestCase{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEncryption(){
|
public void testEncryption() throws JSONException {
|
||||||
NetworkPackage original = new NetworkPackage("com.test");
|
NetworkPackage original = new NetworkPackage("com.test");
|
||||||
original.set("hello", "hola");
|
original.set("hello", "hola");
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user