From 1c68a21e3a4a539c7bc06129d117ff0e4afab465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 20 Jul 2020 11:09:38 +0800 Subject: [PATCH] Add key import with openkeychain --- .../keychain/pgp/PgpHelper.java | 40 +++++++++++++++ .../java/org/telegram/ui/ChatActivity.java | 50 +++++++++++++++---- .../src/main/res/values/strings_nekox.xml | 7 ++- 3 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 TMessagesProj/src/main/java/org/sufficientlysecure/keychain/pgp/PgpHelper.java diff --git a/TMessagesProj/src/main/java/org/sufficientlysecure/keychain/pgp/PgpHelper.java b/TMessagesProj/src/main/java/org/sufficientlysecure/keychain/pgp/PgpHelper.java new file mode 100644 index 000000000..2ea7cdfbc --- /dev/null +++ b/TMessagesProj/src/main/java/org/sufficientlysecure/keychain/pgp/PgpHelper.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2017 Schürmann & Breitmoser GbR + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.pgp; + +import java.util.regex.Pattern; + +public class PgpHelper { + + public static final Pattern PGP_MESSAGE = Pattern.compile( + ".*?(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*", Pattern.DOTALL); + + public static final Pattern PGP_CLEARTEXT_SIGNATURE = Pattern + .compile(".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----" + + "BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*", + Pattern.DOTALL); + + public static final Pattern PGP_PUBLIC_KEY = Pattern.compile( + ".*?(-----BEGIN PGP PUBLIC KEY BLOCK-----.*?-----END PGP PUBLIC KEY BLOCK-----).*", + Pattern.DOTALL); + + public static final Pattern PGP_PRIVATE_KEY = Pattern.compile( + ".*?(-----BEGIN PGP PRIVATE KEY BLOCK-----.*?-----END PGP PRIVATE KEY BLOCK-----).*", + Pattern.DOTALL); + +} diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java index c88982ad3..33517e8d7 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java @@ -102,6 +102,7 @@ import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; import org.openintents.openpgp.OpenPgpDecryptionResult; import org.openintents.openpgp.OpenPgpSignatureResult; import org.openintents.openpgp.util.OpenPgpApi; +import org.sufficientlysecure.keychain.pgp.PgpHelper; import org.telegram.PhoneFormat.PhoneFormat; import org.telegram.messenger.AndroidUtilities; import org.telegram.messenger.ApplicationLoader; @@ -13267,9 +13268,9 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not menu.clear(); - menu.add(android.R.id.cut, android.R.id.cut,0,android.R.string.cut); - menu.add(android.R.id.copy, android.R.id.copy,1,android.R.string.copy); - menu.add(android.R.id.paste, android.R.id.paste, 2,android.R.string.paste); + menu.add(android.R.id.cut, android.R.id.cut, 0, android.R.string.cut); + menu.add(android.R.id.copy, android.R.id.copy, 1, android.R.string.copy); + menu.add(android.R.id.paste, android.R.id.paste, 2, android.R.string.paste); menu.add(R.id.menu_translate, R.id.menu_translate, 5, LocaleController.getString("Translate", R.string.Translate)); @@ -15032,10 +15033,24 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not options.add(88); icons.add(R.drawable.ic_translate); } - if (StrUtil.isNotBlank(selectedObject.messageOwner.message) && StrUtil.isNotBlank(NekoConfig.openPGPApp) && selectedObject.messageOwner.message.contains("-----BEGIN PGP")) { - items.add(LocaleController.getString("DecryptVerify", R.string.DecryptVerify)); - options.add(200); - icons.add(R.drawable.baseline_vpn_key_24); + if (StrUtil.isNotBlank(selectedObject.messageOwner.message) && StrUtil.isNotBlank(NekoConfig.openPGPApp)) { + if (PgpHelper.PGP_CLEARTEXT_SIGNATURE.matcher(selectedObject.messageOwner.message).matches()) { + items.add(LocaleController.getString("PGPVerify", R.string.PGPVerify)); + options.add(200); + icons.add(R.drawable.baseline_vpn_key_24); + } else if (PgpHelper.PGP_MESSAGE.matcher(selectedObject.messageOwner.message).matches()) { + items.add(LocaleController.getString("PGPDecrypt", R.string.PGPDecrypt)); + options.add(201); + icons.add(R.drawable.baseline_vpn_key_24); + } else if (PgpHelper.PGP_PRIVATE_KEY.matcher(selectedObject.messageOwner.message).matches()) { + items.add(LocaleController.getString("PGPImportPrivate", R.string.PGPImportPrivate)); + options.add(202); + icons.add(R.drawable.baseline_vpn_key_24); + } else if (PgpHelper.PGP_PUBLIC_KEY.matcher(selectedObject.messageOwner.message).matches()) { + items.add(LocaleController.getString("PGPImport", R.string.PGPImport)); + options.add(203); + icons.add(R.drawable.baseline_vpn_key_24); + } } } if (NekoConfig.showMessageDetails) { @@ -16231,12 +16246,12 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not }, null); break; } - case 200: { + case 200: case 201: { Intent open = new Intent(Intent.ACTION_SEND); open.setType("application/pgp-message"); open.putExtra(Intent.EXTRA_TEXT, selectedObject.messageOwner.message); - open.setClassName(NekoConfig.openPGPApp,NekoConfig.openPGPApp + ".ui.DecryptActivity"); + open.setClassName(NekoConfig.openPGPApp, NekoConfig.openPGPApp + ".ui.DecryptActivity"); try { @@ -16255,7 +16270,6 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not // @Override // public void onReturn(Intent result) { // -// // OpenPgpSignatureResult s = result.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE); // // } @@ -16264,6 +16278,22 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not break; } + case 202: case 203: { + + Intent open = new Intent(NekoConfig.openPGPApp + ".action.IMPORT_KEY"); + open.putExtra(NekoConfig.openPGPApp + ".EXTRA_KEY_BYTES", StrUtil.utf8Bytes(selectedObject.messageOwner.message)); + + try { + + getParentActivity().startActivity(open); + + } catch (Exception e) { + + AlertUtil.showToast(e); + + } + + } } selectedObject = null; selectedObjectGroup = null; diff --git a/TMessagesProj/src/main/res/values/strings_nekox.xml b/TMessagesProj/src/main/res/values/strings_nekox.xml index ff6a2e899..19013d084 100644 --- a/TMessagesProj/src/main/res/values/strings_nekox.xml +++ b/TMessagesProj/src/main/res/values/strings_nekox.xml @@ -222,11 +222,14 @@ Use Persian Calender OpenPGP Client - OpenPGP App + OpenPGP Provider OpenPGP Key None Sign - Decrypt / Verify + Decrypt Message + Verify Message + Import Public Key + Import Private Key \ No newline at end of file