Improvements

This commit is contained in:
Thomas 2021-03-02 16:58:13 +01:00
parent 7102ff3c79
commit c0b8c564df
4 changed files with 62 additions and 6 deletions

View File

@ -182,6 +182,9 @@ dependencies {
implementation 'com.github.UnifiedPush:android-connector:1.1.0'
implementation "com.madgag.spongycastle:bctls-jdk15on:1.58.0.0"
implementation 'commons-net:commons-net:3.6'
//Flavors
//Playstore

View File

@ -35,6 +35,7 @@ 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;
@ -133,6 +134,7 @@ import app.fedilab.android.fragments.TabLayoutNotificationsFragment;
import app.fedilab.android.fragments.TabLayoutScheduleFragment;
import app.fedilab.android.fragments.WhoToFollowFragment;
import app.fedilab.android.helper.CrossActions;
import app.fedilab.android.helper.ECDH;
import app.fedilab.android.helper.ExpandableHeightListView;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.MenuFloating;
@ -217,6 +219,8 @@ public abstract class BaseMainActivity extends BaseActivity
Account account = new AccountDAO(BaseMainActivity.this, db).getUniqAccount(userId, instance);
Intent intent = getIntent();
PackageManager pm = getPackageManager();
ECDH.uncryptMessage(BaseMainActivity.this, "", "Underground@toot.fedilab.app");
try {
if (intent != null && intent.getComponent() != null) {
ActivityInfo ai = pm.getActivityInfo(intent.getComponent(), PackageManager.GET_META_DATA);

View File

@ -17,7 +17,9 @@ package app.fedilab.android.helper;
import android.annotation.SuppressLint;
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;
@ -27,7 +29,10 @@ import org.spongycastle.asn1.x9.X9ECParameters;
import org.spongycastle.crypto.params.ECNamedDomainParameters;
import org.spongycastle.crypto.params.ECPrivateKeyParameters;
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.ECPublicKeySpec;
import org.spongycastle.math.ec.ECCurve;
import org.spongycastle.math.ec.ECPoint;
@ -44,9 +49,13 @@ import java.security.interfaces.ECPublicKey;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import javax.crypto.KeyAgreement;
import javax.crypto.NoSuchPaddingException;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.Sqlite;
// https://github.com/nelenkov/ecdh-kx/blob/master/src/org/nick/ecdhkx/Crypto.java
@ -112,8 +121,8 @@ public class ECDH {
}
private static byte[] generateSecret(PrivateKey myPrivKey, PublicKey otherPubKey) throws Exception {
ECPublicKey ecPubKey = (ECPublicKey) otherPubKey;
KeyAgreement keyAgreement = KeyAgreement.getInstance(KEGEN_ALG, PROVIDER);
KeyAgreement keyAgreement = KeyAgreement.getInstance(KEGEN_ALG);
keyAgreement.init(myPrivKey);
keyAgreement.doPhase(otherPubKey, true);
@ -188,10 +197,37 @@ public class ECDH {
}
static synchronized PublicKey readPublicKey(String keyStr) throws Exception {
X509EncodedKeySpec x509ks = new X509EncodedKeySpec(
base64Decode(keyStr));
return kf.generatePublic(x509ks);
public static String uncryptMessage(Context context, String cyphered, String slug) {
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);
getInstance();
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]);
String uncrypted = crypted_message_test;
try {
PublicKey dddd = getServerKey(context, account);
} catch (Exception e) {
e.printStackTrace();
}
byte[] ddd = getSharedSecret(context, account);
return "";
}
public static PublicKey readPublicKey(String keyStr) throws Exception {
KeyFactory kf = KeyFactory.getInstance("ECDH", new BouncyCastleProvider());
ECParameterSpec parameterSpec = org.spongycastle.jce.ECNamedCurveTable.getParameterSpec("prime256v1");
ECCurve curve = parameterSpec.getCurve();
ECPoint point = curve.decodePoint(base64Decode(keyStr));
ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, parameterSpec);
return kf.generatePublic(pubSpec);
}

View File

@ -14,7 +14,9 @@ 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;
@ -23,6 +25,7 @@ import org.unifiedpush.android.connector.MessagingReceiverHandler;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.NotificationsHelper;
import app.fedilab.android.helper.PushNotifications;
import app.fedilab.android.sqlite.AccountDAO;
@ -35,6 +38,16 @@ 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("@");