-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Crypto] Initial support for OpenPGP clients
The default OpenPGP API doesn't work in AndroidX, therefore needed a workaround. There's an alternative library written in Kotlin (https://github.com/android-password-store/openpgp-ktx) but it inherits the same problem: it relies on Android preference library (an API is supposed to be independent of such a library). This workaround uses a single Dialog fragment to merge the two preferences and therefore, we have at least some control on how we are going to work with it. But I have to admit, the original OpenPGP library is not what a library should look like (neither is mine as a consequence). Related issue: open-keychain/open-keychain#2432
- Loading branch information
1 parent
24f2655
commit db0deb2
Showing
22 changed files
with
1,941 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
...n/java/io/github/muntashirakon/AppManager/settings/OpenPgpKeySelectionDialogFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* Copyright (C) 2020 Muntashir Al-Islam | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package io.github.muntashirakon.AppManager.settings; | ||
|
||
import android.app.Dialog; | ||
import android.app.PendingIntent; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.content.pm.ServiceInfo; | ||
import android.os.Bundle; | ||
|
||
import com.google.android.material.dialog.MaterialAlertDialogBuilder; | ||
|
||
import org.openintents.openpgp.IOpenPgpService2; | ||
import org.openintents.openpgp.OpenPgpError; | ||
import org.openintents.openpgp.util.OpenPgpApi; | ||
import org.openintents.openpgp.util.OpenPgpServiceConnection; | ||
import org.openintents.openpgp.util.OpenPgpUtils; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import androidx.activity.result.ActivityResultLauncher; | ||
import androidx.activity.result.IntentSenderRequest; | ||
import androidx.activity.result.contract.ActivityResultContracts; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.fragment.app.DialogFragment; | ||
import androidx.fragment.app.FragmentActivity; | ||
import io.github.muntashirakon.AppManager.AppManager; | ||
import io.github.muntashirakon.AppManager.R; | ||
import io.github.muntashirakon.AppManager.logs.Log; | ||
import io.github.muntashirakon.AppManager.utils.AppPref; | ||
import io.github.muntashirakon.AppManager.utils.ArrayUtils; | ||
|
||
public class OpenPgpKeySelectionDialogFragment extends DialogFragment { | ||
public static final String TAG = "OpenPgpKeySelectionDialogFragment"; | ||
|
||
private String mOpenPgpProvider; | ||
private OpenPgpServiceConnection mServiceConnection; | ||
private FragmentActivity activity; | ||
|
||
private static final int NO_KEY = 0; | ||
|
||
private ActivityResultLauncher<IntentSenderRequest> keyIdResultLauncher; | ||
|
||
@NonNull | ||
@Override | ||
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { | ||
activity = requireActivity(); | ||
// This must be registered using an activity context since the dialog won't exist | ||
keyIdResultLauncher = activity.registerForActivityResult( | ||
new ActivityResultContracts.StartIntentSenderForResult(), | ||
result -> { | ||
if (result.getData() != null) | ||
getUserId(result.getData()); | ||
}); | ||
mOpenPgpProvider = (String) AppPref.get(AppPref.PrefKey.PREF_OPEN_PGP_PACKAGE_STR); | ||
List<ServiceInfo> serviceInfoList = OpenPgpUtils.getPgpClientServices(activity); | ||
CharSequence[] packageLabels = new String[serviceInfoList.size()]; | ||
String[] packageNames = new String[serviceInfoList.size()]; | ||
ServiceInfo serviceInfo; | ||
PackageManager pm = activity.getPackageManager(); | ||
for (int i = 0; i < packageLabels.length; ++i) { | ||
serviceInfo = serviceInfoList.get(i); | ||
packageLabels[i] = serviceInfo.loadLabel(pm); | ||
packageNames[i] = serviceInfo.packageName; | ||
} | ||
int choice = ArrayUtils.indexOf(packageNames, mOpenPgpProvider); | ||
return new MaterialAlertDialogBuilder(activity) | ||
.setTitle(R.string.open_pgp_provider) | ||
.setSingleChoiceItems(packageLabels, choice, (dialog, which) -> { | ||
mOpenPgpProvider = packageNames[which]; | ||
AppPref.set(AppPref.PrefKey.PREF_OPEN_PGP_PACKAGE_STR, mOpenPgpProvider); | ||
}) | ||
.setNegativeButton(R.string.cancel, null) | ||
.setPositiveButton(R.string.ok, (dialog, which) -> chooseKey()) | ||
.create(); | ||
} | ||
|
||
private void chooseKey() { | ||
// bind to service | ||
mServiceConnection = new OpenPgpServiceConnection(AppManager.getContext(), mOpenPgpProvider, | ||
new OpenPgpServiceConnection.OnBound() { | ||
@Override | ||
public void onBound(IOpenPgpService2 service) { | ||
getUserId(new Intent()); | ||
} | ||
|
||
@Override | ||
public void onError(Exception e) { | ||
Log.e(OpenPgpApi.TAG, "exception on binding!", e); | ||
} | ||
} | ||
); | ||
mServiceConnection.bindToService(); | ||
} | ||
|
||
private void getUserId(@NonNull Intent data) { | ||
data.setAction(OpenPgpApi.ACTION_GET_SIGN_KEY_ID); | ||
data.putExtra(OpenPgpApi.EXTRA_USER_ID, ""); | ||
OpenPgpApi api = new OpenPgpApi(activity, mServiceConnection.getService()); | ||
api.executeApiAsync(data, null, null, new OpenPgpCallback()); | ||
} | ||
|
||
private class OpenPgpCallback implements OpenPgpApi.IOpenPgpCallback { | ||
|
||
private OpenPgpCallback() { | ||
} | ||
|
||
@Override | ||
public void onReturn(@NonNull Intent result) { | ||
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { | ||
case OpenPgpApi.RESULT_CODE_SUCCESS: { | ||
long keyId = result.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, NO_KEY); | ||
AppPref.set(AppPref.PrefKey.PREF_OPEN_PGP_USER_ID_LONG, keyId); | ||
break; | ||
} | ||
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: { | ||
PendingIntent pi = Objects.requireNonNull(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT)); | ||
keyIdResultLauncher.launch(new IntentSenderRequest.Builder(pi).build()); | ||
break; | ||
} | ||
case OpenPgpApi.RESULT_CODE_ERROR: { | ||
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR); | ||
if (error != null) | ||
Log.e(OpenPgpApi.TAG, "RESULT_CODE_ERROR: " + error.getMessage()); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (C) 2020 Muntashir Al-Islam | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
apply plugin: 'com.android.library' | ||
|
||
android { | ||
compileSdkVersion 29 | ||
|
||
defaultConfig { | ||
minSdkVersion 21 | ||
targetSdkVersion 29 | ||
versionCode 11 | ||
versionName "11" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
preRelease { | ||
minifyEnabled false | ||
versionNameSuffix '-PRE' | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation 'androidx.annotation:annotation:1.1.0' | ||
} | ||
|
||
sourceCompatibility = "1.8" | ||
targetCompatibility = "1.8" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!-- | ||
~ Copyright (C) 2020 Muntashir Al-Islam | ||
~ | ||
~ 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 <https://www.gnu.org/licenses/>. | ||
--> | ||
|
||
<manifest package="org.openintents.openpgp" /> |
27 changes: 27 additions & 0 deletions
27
libOpenPGP/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (C) 2014-2015 Dominik Schürmann <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.openintents.openpgp; | ||
|
||
interface IOpenPgpService { | ||
|
||
/** | ||
* do NOT use this, data returned from the service through "output" may be truncated | ||
* @deprecated | ||
*/ | ||
Intent execute(in Intent data, in ParcelFileDescriptor input, in ParcelFileDescriptor output); | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
libOpenPGP/src/main/aidl/org/openintents/openpgp/IOpenPgpService2.aidl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (C) 2015 Dominik Schürmann <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.openintents.openpgp; | ||
|
||
interface IOpenPgpService2 { | ||
|
||
/** | ||
* see org.openintents.openpgp.util.OpenPgpApi for documentation | ||
*/ | ||
ParcelFileDescriptor createOutputPipe(in int pipeId); | ||
|
||
/** | ||
* see org.openintents.openpgp.util.OpenPgpApi for documentation | ||
*/ | ||
Intent execute(in Intent data, in ParcelFileDescriptor input, int pipeId); | ||
} |
Oops, something went wrong.