Skip to content

Commit

Permalink
- Fixed issue when encrypting message and decrypting on other app
Browse files Browse the repository at this point in the history
- Bug: save private key when using import
- Update gopenpgp internal lib
- Update flutter secure storage
- Update flutter shared prefs
- Bug: fixed internal gopenpgp if key is already unlocked
- Android SDK target update (30)
  • Loading branch information
MarinX committed Jan 20, 2021
1 parent d53ca6f commit 797f2d1
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 75 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 28
compileSdkVersion 30

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand All @@ -46,7 +46,7 @@ android {
defaultConfig {
applicationId "com.marinbasic.yapgp"
minSdkVersion 18
targetSdkVersion 28
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
33 changes: 17 additions & 16 deletions android/app/src/main/kotlin/com/marinbasic/yapgp/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.marinbasic.yapgp;

import androidx.annotation.NonNull;

import com.google.android.gms.tasks.Tasks;
import com.proton.gopenpgp.crypto.Crypto;
import com.proton.gopenpgp.crypto.Identity;
import com.proton.gopenpgp.crypto.Key;
import com.proton.gopenpgp.helper.Helper;

import org.json.JSONException;
import org.json.JSONObject;

import org.json.JSONObject;

import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import crypto.Crypto;
import crypto.Key;
import crypto.UserInfo;
import helper.Helper;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
Expand Down Expand Up @@ -55,9 +54,11 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {

return resultJSON;
case "Encrypt":
String pgpMessage = Helper.encryptMessageArmored(
call.argument("pubKey"),
call.argument("message")
String pgpMessage = Helper.encryptSignMessageArmored(
call.argument("pubKey").toString(),
call.argument("privKey").toString(),
call.argument("passphrase").toString().getBytes(),
call.argument("message").toString()
);

resultJSON.put("message", pgpMessage);
Expand All @@ -80,10 +81,10 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
return resultJSON;
case "Identity":
Key pubKey;
Identity id;
UserInfo id;
try{
pubKey = Crypto.newKeyFromArmored(call.argument("pubKey"));
id = pubKey.identity();
id = pubKey.primaryIdentity();
}catch (Exception e) {
resultJSON.put("error", "Invalid PGP public key");
return resultJSON;
Expand All @@ -97,13 +98,14 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
return resultJSON;
case "Import":
Key privateKey;
Identity identity;
UserInfo identity;
String password = call.argument("passphrase");
if(password == null) {
password = "";
}
try {
privateKey = Crypto.newKeyFromArmored(call.argument("privKey"));

if(privateKey.isLocked() && password.isEmpty()) {
resultJSON.put("error", "Private key locked with password");
return resultJSON;
Expand All @@ -113,7 +115,7 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
resultJSON.put("error", "Invalid password");
return resultJSON;
}
identity = privateKey.identity();
identity = privateKey.primaryIdentity();

resultJSON.put("publicKey", privateKey.getArmoredPublicKey());
resultJSON.put("privateKey", privateKey.armor());
Expand All @@ -133,9 +135,7 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
call.argument("message"),
Crypto.getUnixTime()
);
Long time = Helper.createdCleartextMessageArmored(call.argument("message"));
resultJSON.put("msg", msg);
resultJSON.put("time", time);
}catch (Exception e) {
resultJSON.put("error", "Contact key does not match PGP signed message");
return resultJSON;
Expand All @@ -154,6 +154,7 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
resultJSON.put("time", Crypto.getUnixTime());

}catch (Exception e) {
e.printStackTrace();
resultJSON.put("error", "Cannot create signature");
return resultJSON;
}
Expand Down
Binary file modified android/gopenpgp/gopenpgp.aar
Binary file not shown.
2 changes: 1 addition & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
android.bundle.enableUncompressedNativeLibs=false
android.enableR8=true
6 changes: 1 addition & 5 deletions lib/models/signature.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
class Signature {
String message;
DateTime datetime;

Signature({this.message, this.datetime});
Signature({this.message});

Signature.fromJson(Map<String, dynamic> json) {
message = json['msg'];
datetime = new DateTime.fromMillisecondsSinceEpoch(json['time'] * 1000);
print(json['time']);
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['msg'] = this.message;
data['time'] = this.datetime;
return data;
}
}
4 changes: 2 additions & 2 deletions lib/services/pgp_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class PGPService {
return PGP.fromJson(jsonDecode(result));
}

Future<String> encrypt(String msg, PGP key) async {
String result = await ch.invokeMethod("Encrypt", {"pubKey": key.publicKey, "message": msg});
Future<String> encrypt(String msg, PGP key, PGP contact) async {
String result = await ch.invokeMethod("Encrypt", {"pubKey": contact.publicKey,"privKey": key.privateKey,"passphrase": key.passphrase, "message": msg});
var json = jsonDecode(result);
return json["message"];
}
Expand Down
2 changes: 1 addition & 1 deletion lib/views/encrypt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class EncryptState extends State<Encrypt> {
.toString();
PGP mykey = _keys.where((element) => element.privateKey == privKey).first;

_service.encrypt(message, mykey).then((value) {
_service.encrypt(message, mykey, contact).then((value) {
setState(() {
isLoading = false;
_controller.text = value;
Expand Down
2 changes: 1 addition & 1 deletion lib/views/import_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ImportKeyState extends State<ImportKey> {
privKey,
_fbKey.currentState.value["password"].toString(),
)
.then((value) {
.then(Store.addKey).then((value) {
Navigator.pop(context, value);
}).catchError((e) {
_utils.showSnackbar(builderContext, e.message);
Expand Down
11 changes: 0 additions & 11 deletions lib/views/verify_sign.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,6 @@ class VerifySignatureState extends State<VerifySignature> {
],
),
),
Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
title: Text('Signed at'),
subtitle: Text(sig.datetime.toString()),
),
],
),
),
],
) :
FormBuilder(
Expand Down
Loading

0 comments on commit 797f2d1

Please sign in to comment.