FairEmail/app/src/main/java/eu/faircode/email/PgpHelper.java

183 lines
6.7 KiB
Java
Raw Normal View History

2021-11-26 14:33:19 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail 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.
FairEmail 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 FairEmail. If not, see <http://www.gnu.org/licenses/>.
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
2021-11-26 14:33:19 +00:00
*/
2021-11-27 07:07:35 +00:00
import static org.openintents.openpgp.util.OpenPgpApi.RESULT_CODE_ERROR;
import static org.openintents.openpgp.util.OpenPgpApi.RESULT_CODE_SUCCESS;
import static org.openintents.openpgp.util.OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED;
2021-11-26 14:33:19 +00:00
import android.content.Context;
2021-11-27 07:07:35 +00:00
import android.content.Intent;
2023-06-28 10:53:11 +00:00
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
2021-11-27 07:07:35 +00:00
import android.os.OperationCanceledException;
import android.text.TextUtils;
2021-11-26 14:33:19 +00:00
2023-06-28 10:53:11 +00:00
import androidx.preference.PreferenceManager;
2021-11-26 14:33:19 +00:00
import org.openintents.openpgp.IOpenPgpService2;
2021-11-27 07:07:35 +00:00
import org.openintents.openpgp.util.OpenPgpApi;
2021-11-26 14:33:19 +00:00
import org.openintents.openpgp.util.OpenPgpServiceConnection;
2021-11-27 07:07:35 +00:00
import java.io.InputStream;
import java.io.OutputStream;
2022-03-04 11:50:16 +00:00
import java.util.List;
2021-11-26 14:33:19 +00:00
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
2022-03-04 11:50:16 +00:00
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
2021-11-26 14:33:19 +00:00
public class PgpHelper {
private static final long CONNECT_TIMEOUT = 5000L;
2022-03-04 13:23:50 +00:00
private static final long KEY_TIMEOUT = 250L;
2021-11-26 14:33:19 +00:00
2021-11-27 07:07:35 +00:00
static Intent execute(Context context, Intent data, InputStream is, OutputStream os) {
return execute(context, data, is, os, CONNECT_TIMEOUT);
}
static Intent execute(Context context, Intent data, InputStream is, OutputStream os, long timeout) {
OpenPgpServiceConnection pgpService = null;
try {
pgpService = getConnection(context, timeout);
Log.i("Executing " + data.getAction() +
" " + TextUtils.join(", ", Log.getExtras(data.getExtras())));
OpenPgpApi api = new OpenPgpApi(context, pgpService.getService());
Intent result = api.executeApi(data, is, os);
int resultCode = result.getIntExtra(OpenPgpApi.RESULT_CODE, RESULT_CODE_ERROR);
Log.i("Result action=" + data.getAction() + " code=" + getResultName(resultCode) +
" " + TextUtils.join(", ", Log.getExtras(result.getExtras())));
return result;
} finally {
if (pgpService != null && pgpService.isBound())
pgpService.unbindFromService();
}
}
2022-10-30 06:39:56 +00:00
static boolean hasPgpKey(Context context, List<Address> recipients, boolean all) {
return hasPgpKey(context, recipients, all, KEY_TIMEOUT); // milliseconds
2022-03-04 12:41:39 +00:00
}
2022-10-30 06:39:56 +00:00
private static boolean hasPgpKey(Context context, List<Address> recipients, boolean all, long timeout) {
2022-03-04 11:50:16 +00:00
if (recipients == null || recipients.size() == 0)
return false;
String[] userIds = new String[recipients.size()];
for (int i = 0; i < recipients.size(); i++) {
InternetAddress recipient = (InternetAddress) recipients.get(i);
userIds[i] = recipient.getAddress();
}
Intent intent = new Intent(OpenPgpApi.ACTION_GET_KEY_IDS);
intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, userIds);
try {
Intent result = execute(context, intent, null, null, timeout);
int resultCode = result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR);
if (resultCode == OpenPgpApi.RESULT_CODE_SUCCESS) {
long[] keyIds = result.getLongArrayExtra(OpenPgpApi.EXTRA_KEY_IDS);
2022-10-30 06:39:56 +00:00
return (all ? keyIds.length == recipients.size() : keyIds.length > 0);
2022-03-04 11:50:16 +00:00
}
} catch (OperationCanceledException ignored) {
// Do nothing
} catch (Throwable ex) {
Log.w(ex);
}
return false;
}
2021-11-27 07:07:35 +00:00
private static String getResultName(int code) {
switch (code) {
case RESULT_CODE_ERROR:
return "error";
case RESULT_CODE_SUCCESS:
return "success";
case RESULT_CODE_USER_INTERACTION_REQUIRED:
return "interaction";
default:
return Integer.toString(code);
}
2021-11-26 14:33:19 +00:00
}
2021-11-27 07:07:35 +00:00
private static OpenPgpServiceConnection getConnection(Context context, long timeout) {
2023-06-28 10:53:11 +00:00
final String pkg = PgpHelper.getPackageName(context);
2021-11-26 14:33:19 +00:00
Log.i("PGP binding to " + pkg + " timeout=" + timeout);
CountDownLatch latch = new CountDownLatch(1);
2022-05-28 18:39:53 +00:00
OpenPgpServiceConnection pgpService = new OpenPgpServiceConnection(context, pkg,
2021-11-26 14:33:19 +00:00
new OpenPgpServiceConnection.OnBound() {
@Override
public void onBound(IOpenPgpService2 service) {
Log.i("Bound to " + pkg);
latch.countDown();
}
@Override
public void onError(Exception ex) {
Log.i(ex.getMessage());
latch.countDown();
}
});
pgpService.bindToService();
try {
latch.await(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException ex) {
Log.w(ex);
}
Log.i("PGP bound=" + pgpService.isBound());
2022-05-28 18:39:53 +00:00
if (!pgpService.isBound()) {
try {
pgpService.unbindFromService();
} catch (Throwable ex) {
Log.e(ex);
}
2021-11-27 07:07:35 +00:00
throw new OperationCanceledException();
2022-05-28 18:39:53 +00:00
}
2021-11-26 14:33:19 +00:00
return pgpService;
}
2023-06-28 10:53:11 +00:00
static String getPackageName(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getString("openpgp_provider", Helper.PGP_OPENKEYCHAIN_PACKAGE);
}
static boolean isOpenKeychainInstalled(Context context) {
String provider = getPackageName(context);
try {
PackageManager pm = context.getPackageManager();
Intent intent = new Intent(OpenPgpApi.SERVICE_INTENT_2);
intent.setPackage(provider);
List<ResolveInfo> ris = pm.queryIntentServices(intent, 0);
return (ris != null && ris.size() > 0);
} catch (Throwable ex) {
Log.e(ex);
return false;
}
}
2021-11-26 14:33:19 +00:00
}