Improvements

This commit is contained in:
Thomas 2021-03-03 11:32:43 +01:00
parent 1e9bbf4d5c
commit 16ae642243
4 changed files with 23 additions and 45 deletions

View File

@ -35,7 +35,6 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.Menu;

View File

@ -19,7 +19,6 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.util.Base64;
import android.util.Log;
import androidx.preference.PreferenceManager;
@ -33,7 +32,9 @@ import org.spongycastle.crypto.params.ECPublicKeyParameters;
import org.spongycastle.jce.provider.BouncyCastleProvider;
import org.spongycastle.jce.spec.ECNamedCurveSpec;
import org.spongycastle.jce.spec.ECParameterSpec;
import org.spongycastle.jce.spec.ECPrivateKeySpec;
import org.spongycastle.jce.spec.ECPublicKeySpec;
import org.spongycastle.jce.spec.IEKeySpec;
import org.spongycastle.math.ec.ECCurve;
import org.spongycastle.math.ec.ECPoint;
@ -50,6 +51,7 @@ import java.security.PublicKey;
import java.security.Security;
import java.security.interfaces.ECPublicKey;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.ArrayList;
import javax.crypto.Cipher;
@ -114,7 +116,7 @@ public class ECDH {
}
static byte[] base64Decode(String str) {
return Base64.decode(str, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
return Base64.decode(str, Base64.DEFAULT);
}
static synchronized KeyPair generateKeyPair()
@ -207,27 +209,14 @@ public class ECDH {
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
String[] slugArray = slug.split("@");
Account account = new AccountDAO(context, db).getUniqAccountUsernameInstance(slugArray[0], slugArray[1]);
byte[] privateKey = getSharedSecret(context, account);
try {
Cipher outCipher = Cipher.getInstance("ECIES", "SC");
// outCipher.init(Cipher.DECRYPT_MODE, readPrivateKey(privateKey));
outCipher.init(Cipher.DECRYPT_MODE, readPrivateKey(privateKey));
CipherInputStream cipherInputStream = new CipherInputStream(
new ByteArrayInputStream(Base64.decode(cyphered, Base64.DEFAULT)), outCipher);
ArrayList<Byte> values = new ArrayList<>();
int nextByte;
while ((nextByte = cipherInputStream.read()) != -1) {
values.add((byte) nextByte);
}
byte[] bytes = new byte[values.size()];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = values.get(i).byteValue();
}
String finalText = new String(bytes, 0, bytes.length, StandardCharsets.UTF_8);
Log.v(Helper.TAG, "---> " + finalText);
byte[] plaintext = outCipher.doFinal(cyphered.getBytes(StandardCharsets.UTF_8));
String finalText = new String(plaintext);
return finalText;
} catch (Exception e) {
e.printStackTrace();
@ -248,8 +237,10 @@ public class ECDH {
public static PrivateKey readPrivateKey(byte[] key) throws Exception {
SecretKeySpec signingKey = new SecretKeySpec(key, "ECIES");
return kf.generatePrivate(signingKey);
KeyFactory kf = KeyFactory.getInstance("ECDH", new BouncyCastleProvider());
ECParameterSpec parameterSpec = org.spongycastle.jce.ECNamedCurveTable.getParameterSpec("prime256v1");
ECPrivateKeySpec pubSpec = new ECPrivateKeySpec(new BigInteger(1, key), parameterSpec);
return kf.generatePrivate(pubSpec);
}
static synchronized PrivateKey readMyPrivateKey(Context context) throws Exception {
@ -271,7 +262,7 @@ public class ECDH {
.getDefaultSharedPreferences(context);
String strPub = prefs.getString(kp_public, "");
String strPriv = prefs.getString(kp_private, "");
if (strPub.trim().isEmpty() || strPriv.trim().isEmpty()) {
if (strPub.trim().isEmpty() || strPriv.trim().isEmpty() || 1 == 1) {
return newPair(context);
}
try {

View File

@ -14,9 +14,7 @@ package app.fedilab.android.services;
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -24,10 +22,7 @@ import org.unifiedpush.android.connector.MessagingReceiver;
import org.unifiedpush.android.connector.MessagingReceiverHandler;
import app.fedilab.android.activities.BaseMainActivity;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.helper.ECDH;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.NotificationsHelper;
import app.fedilab.android.helper.PushNotifications;
import app.fedilab.android.sqlite.AccountDAO;
@ -40,29 +35,23 @@ class handler implements MessagingReceiverHandler {
@Override
public void onMessage(@Nullable Context context, @NotNull String s, @NotNull String slug) {
//TODO: remove after tests
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String crypted_message_test = sharedpreferences.getString("CRYPTED_MESSAGE_TEST", null);
Log.v(Helper.TAG, "crypted_message_test: " + crypted_message_test);
if (crypted_message_test == null) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("CRYPTED_MESSAGE_TEST", s);
editor.apply();
}
new Thread(() -> {
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
String[] slugArray = slug.split("@");
//ECDH.uncryptMessage(context, s, slug);
Account account = new AccountDAO(context, db).getUniqAccountUsernameInstance(slugArray[0], slugArray[1]);
NotificationsHelper.task(context, account);
if (context != null) {
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
String[] slugArray = slug.split("@");
//ECDH.uncryptMessage(context, s, slug);
Account account = new AccountDAO(context, db).getUniqAccountUsernameInstance(slugArray[0], slugArray[1]);
NotificationsHelper.task(context, account);
}
}).start();
}
@Override
public void onNewEndpoint(@Nullable Context context, @NotNull String endpoint, @NotNull String slug) {
new PushNotifications()
.registerPushNotifications(context, endpoint, slug);
if (context != null) {
new PushNotifications()
.registerPushNotifications(context, endpoint, slug);
}
}
@Override

View File

@ -6,7 +6,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import org.unifiedpush.android.connector_fcm_added.RegistrationFCM;