From 754b2a6cb15a6af61150cd4b1ce472bebd4fec22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sun, 9 Feb 2014 19:22:14 +0100 Subject: Restructuring for new API library --- .../src/main/AndroidManifest.xml | 13 ++ .../org/openintents/openpgp/IOpenPgpCallback.aidl | 45 +++++ .../openpgp/IOpenPgpKeyIdsCallback.aidl | 39 ++++ .../org/openintents/openpgp/IOpenPgpService.aidl | 143 +++++++++++++++ .../aidl/org/openintents/openpgp/OpenPgpData.aidl | 20 ++ .../aidl/org/openintents/openpgp/OpenPgpError.aidl | 20 ++ .../openpgp/OpenPgpSignatureResult.aidl | 20 ++ .../service/remote/IExtendedApiCallback.aidl | 24 +++ .../service/remote/IExtendedApiService.aidl | 48 +++++ .../org/openintents/openpgp/OpenPgpConstants.java | 10 + .../java/org/openintents/openpgp/OpenPgpData.java | 127 +++++++++++++ .../java/org/openintents/openpgp/OpenPgpError.java | 81 +++++++++ .../org/openintents/openpgp/OpenPgpHelper.java | 52 ++++++ .../openintents/openpgp/OpenPgpListPreference.java | 201 +++++++++++++++++++++ .../openpgp/OpenPgpServiceConnection.java | 93 ++++++++++ .../openpgp/OpenPgpSignatureResult.java | 110 +++++++++++ 16 files changed, 1046 insertions(+) create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/AndroidManifest.xml create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpCallback.aidl create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpKeyIdsCallback.aidl create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpData.aidl create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiCallback.aidl create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiService.aidl create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpConstants.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpData.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpError.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpHelper.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpListPreference.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpServiceConnection.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/AndroidManifest.xml b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/AndroidManifest.xml new file mode 100644 index 000000000..32b4a82e5 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/AndroidManifest.xml @@ -0,0 +1,13 @@ + + + + + + + + \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpCallback.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpCallback.aidl new file mode 100644 index 000000000..ba41de1ba --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpCallback.aidl @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * 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; + +import org.openintents.openpgp.OpenPgpData; +import org.openintents.openpgp.OpenPgpSignatureResult; +import org.openintents.openpgp.OpenPgpError; + +interface IOpenPgpCallback { + + /** + * onSuccess returns on successful OpenPGP operations. + * + * @param output + * contains resulting output (decrypted content (when input was encrypted) + * or content without signature (when input was signed-only)) + * @param signatureResult + * signatureResult is only non-null if decryptAndVerify() was called and the content + * was encrypted or signed-and-encrypted. + */ + oneway void onSuccess(in OpenPgpData output, in OpenPgpSignatureResult signatureResult); + + /** + * onError returns on errors or when allowUserInteraction was set to false, but user interaction + * was required execute an OpenPGP operation. + * + * @param error + * See OpenPgpError class for more information. + */ + oneway void onError(in OpenPgpError error); +} \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpKeyIdsCallback.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpKeyIdsCallback.aidl new file mode 100644 index 000000000..4ca356fad --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpKeyIdsCallback.aidl @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * 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; + +import org.openintents.openpgp.OpenPgpError; + +interface IOpenPgpKeyIdsCallback { + + /** + * onSuccess returns on successful getKeyIds operations. + * + * @param keyIds + * returned key ids + */ + oneway void onSuccess(in long[] keyIds); + + /** + * onError returns on errors or when allowUserInteraction was set to false, but user interaction + * was required execute an OpenPGP operation. + * + * @param error + * See OpenPgpError class for more information. + */ + oneway void onError(in OpenPgpError error); +} \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl new file mode 100644 index 000000000..8f9e8a0fd --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * 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; + +import org.openintents.openpgp.OpenPgpData; +import org.openintents.openpgp.IOpenPgpCallback; +import org.openintents.openpgp.IOpenPgpKeyIdsCallback; + +/** + * All methods are oneway, which means they are asynchronous and non-blocking. + * Results are returned to the callback, which has to be implemented on client side. + */ +interface IOpenPgpService { + + /** + * Sign + * + * After successful signing, callback's onSuccess will contain the resulting output. + * + * @param input + * OpenPgpData object containing String, byte[], ParcelFileDescriptor, or Uri + * @param output + * Request output format by defining OpenPgpData object + * + * new OpenPgpData(OpenPgpData.TYPE_STRING) + * Returns as String + * (OpenPGP Radix-64, 33 percent overhead compared to binary, see http://tools.ietf.org/html/rfc4880#page-53) + * new OpenPgpData(OpenPgpData.TYPE_BYTE_ARRAY) + * Returns as byte[] + * new OpenPgpData(uri) + * Writes output to given Uri + * new OpenPgpData(fileDescriptor) + * Writes output to given ParcelFileDescriptor + * @param callback + * Callback where to return results + */ + oneway void sign(in OpenPgpData input, in OpenPgpData output, in IOpenPgpCallback callback); + + /** + * Encrypt + * + * After successful encryption, callback's onSuccess will contain the resulting output. + * + * @param input + * OpenPgpData object containing String, byte[], ParcelFileDescriptor, or Uri + * @param output + * Request output format by defining OpenPgpData object + * + * new OpenPgpData(OpenPgpData.TYPE_STRING) + * Returns as String + * (OpenPGP Radix-64, 33 percent overhead compared to binary, see http://tools.ietf.org/html/rfc4880#page-53) + * new OpenPgpData(OpenPgpData.TYPE_BYTE_ARRAY) + * Returns as byte[] + * new OpenPgpData(uri) + * Writes output to given Uri + * new OpenPgpData(fileDescriptor) + * Writes output to given ParcelFileDescriptor + * @param keyIds + * Key Ids of recipients. Can be retrieved with getKeyIds() + * @param callback + * Callback where to return results + */ + oneway void encrypt(in OpenPgpData input, in OpenPgpData output, in long[] keyIds, in IOpenPgpCallback callback); + + /** + * Sign then encrypt + * + * After successful signing and encryption, callback's onSuccess will contain the resulting output. + * + * @param input + * OpenPgpData object containing String, byte[], ParcelFileDescriptor, or Uri + * @param output + * Request output format by defining OpenPgpData object + * + * new OpenPgpData(OpenPgpData.TYPE_STRING) + * Returns as String + * (OpenPGP Radix-64, 33 percent overhead compared to binary, see http://tools.ietf.org/html/rfc4880#page-53) + * new OpenPgpData(OpenPgpData.TYPE_BYTE_ARRAY) + * Returns as byte[] + * new OpenPgpData(uri) + * Writes output to given Uri + * new OpenPgpData(fileDescriptor) + * Writes output to given ParcelFileDescriptor + * @param keyIds + * Key Ids of recipients. Can be retrieved with getKeyIds() + * @param callback + * Callback where to return results + */ + oneway void signAndEncrypt(in OpenPgpData input, in OpenPgpData output, in long[] keyIds, in IOpenPgpCallback callback); + + /** + * Decrypts and verifies given input bytes. This methods handles encrypted-only, signed-and-encrypted, + * and also signed-only input. + * + * After successful decryption/verification, callback's onSuccess will contain the resulting output. + * The signatureResult in onSuccess is only non-null if signed-and-encrypted or signed-only inputBytes were given. + * + * @param input + * OpenPgpData object containing String, byte[], ParcelFileDescriptor, or Uri + * @param output + * Request output format by defining OpenPgpData object + * + * new OpenPgpData(OpenPgpData.TYPE_STRING) + * Returns as String + * (OpenPGP Radix-64, 33 percent overhead compared to binary, see http://tools.ietf.org/html/rfc4880#page-53) + * new OpenPgpData(OpenPgpData.TYPE_BYTE_ARRAY) + * Returns as byte[] + * new OpenPgpData(uri) + * Writes output to given Uri + * new OpenPgpData(fileDescriptor) + * Writes output to given ParcelFileDescriptor + * @param callback + * Callback where to return results + */ + oneway void decryptAndVerify(in OpenPgpData input, in OpenPgpData output, in IOpenPgpCallback callback); + + /** + * Get available key ids based on given user ids + * + * @param ids + * User Ids (emails) of recipients OR key ids + * @param allowUserInteraction + * Enable user interaction to lookup and import unknown keys + * @param callback + * Callback where to return results (different type than callback in other functions!) + */ + oneway void getKeyIds(in String[] ids, in boolean allowUserInteraction, in IOpenPgpKeyIdsCallback callback); + +} \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpData.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpData.aidl new file mode 100644 index 000000000..3711e4fb4 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpData.aidl @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * 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; + +// Declare OpenPgpData so AIDL can find it and knows that it implements the parcelable protocol. +parcelable OpenPgpData; \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl new file mode 100644 index 000000000..7a6bed1e6 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * 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; + +// Declare OpenPgpError so AIDL can find it and knows that it implements the parcelable protocol. +parcelable OpenPgpError; \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl new file mode 100644 index 000000000..e246792d0 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * 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; + +// Declare OpenPgpSignatureResult so AIDL can find it and knows that it implements the parcelable protocol. +parcelable OpenPgpSignatureResult; \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiCallback.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiCallback.aidl new file mode 100644 index 000000000..f69f66fd7 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiCallback.aidl @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * 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.sufficientlysecure.keychain.service.remote; + +interface IExtendedApiCallback { + + oneway void onSuccess(in byte[] outputBytes); + + oneway void onError(in String error); +} \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiService.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiService.aidl new file mode 100644 index 000000000..669bd31b5 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiService.aidl @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * 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.sufficientlysecure.keychain.service.remote; + +import org.sufficientlysecure.keychain.service.remote.IExtendedApiCallback; + +/** + * All methods are oneway, which means they are asynchronous and non-blocking. + * Results are returned to the callback, which has to be implemented on client side. + */ +interface IExtendedApiService { + + /** + * Symmetric Encrypt + * + * @param inputBytes + * Byte array you want to encrypt + * @param passphrase + * symmetric passhprase + * @param callback + * Callback where to return results + */ + oneway void encrypt(in byte[] inputBytes, in String passphrase, in IExtendedApiCallback callback); + + /** + * Generates self signed X509 certificate signed by OpenPGP private key (from app settings) + * + * @param subjAltNameURI + * @param callback + * Callback where to return results + */ + oneway void selfSignedX509Cert(in String subjAltNameURI, in IExtendedApiCallback callback); + +} \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpConstants.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpConstants.java new file mode 100644 index 000000000..b1ca1bfe6 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpConstants.java @@ -0,0 +1,10 @@ +package org.openintents.openpgp; + +public class OpenPgpConstants { + + public static final String TAG = "OpenPgp API"; + + public static final int REQUIRED_API_VERSION = 1; + public static final String SERVICE_INTENT = "org.openintents.openpgp.IOpenPgpService"; + +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpData.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpData.java new file mode 100644 index 000000000..6615c2146 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpData.java @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * 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; + +import android.net.Uri; +import android.os.Parcel; +import android.os.ParcelFileDescriptor; +import android.os.Parcelable; + +public class OpenPgpData implements Parcelable { + public static final int TYPE_STRING = 0; + public static final int TYPE_BYTE_ARRAY = 1; + public static final int TYPE_FILE_DESCRIPTOR = 2; + public static final int TYPE_URI = 3; + + int type; + + String string; + byte[] bytes = new byte[0]; + ParcelFileDescriptor fileDescriptor; + Uri uri; + + public int getType() { + return type; + } + + public String getString() { + return string; + } + + public byte[] getBytes() { + return bytes; + } + + public ParcelFileDescriptor getFileDescriptor() { + return fileDescriptor; + } + + public Uri getUri() { + return uri; + } + + public OpenPgpData() { + + } + + /** + * Not a real constructor. This can be used to define requested output type. + * + * @param type + */ + public OpenPgpData(int type) { + this.type = type; + } + + public OpenPgpData(String string) { + this.string = string; + this.type = TYPE_STRING; + } + + public OpenPgpData(byte[] bytes) { + this.bytes = bytes; + this.type = TYPE_BYTE_ARRAY; + } + + public OpenPgpData(ParcelFileDescriptor fileDescriptor) { + this.fileDescriptor = fileDescriptor; + this.type = TYPE_FILE_DESCRIPTOR; + } + + public OpenPgpData(Uri uri) { + this.uri = uri; + this.type = TYPE_URI; + } + + public OpenPgpData(OpenPgpData b) { + this.string = b.string; + this.bytes = b.bytes; + this.fileDescriptor = b.fileDescriptor; + this.uri = b.uri; + } + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(type); + dest.writeString(string); + dest.writeInt(bytes.length); + dest.writeByteArray(bytes); + dest.writeParcelable(fileDescriptor, 0); + dest.writeParcelable(uri, 0); + } + + public static final Creator CREATOR = new Creator() { + public OpenPgpData createFromParcel(final Parcel source) { + OpenPgpData vr = new OpenPgpData(); + vr.type = source.readInt(); + vr.string = source.readString(); + vr.bytes = new byte[source.readInt()]; + source.readByteArray(vr.bytes); + vr.fileDescriptor = source.readParcelable(ParcelFileDescriptor.class.getClassLoader()); + vr.fileDescriptor = source.readParcelable(Uri.class.getClassLoader()); + return vr; + } + + public OpenPgpData[] newArray(final int size) { + return new OpenPgpData[size]; + } + }; + +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpError.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpError.java new file mode 100644 index 000000000..f108d3169 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpError.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * 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; + +import android.os.Parcel; +import android.os.Parcelable; + +public class OpenPgpError implements Parcelable { + public static final int GENERIC_ERROR = 0; + public static final int NO_OR_WRONG_PASSPHRASE = 1; + public static final int NO_USER_IDS = 2; + public static final int USER_INTERACTION_REQUIRED = 3; + + int errorId; + String message; + + public OpenPgpError() { + } + + public OpenPgpError(int errorId, String message) { + this.errorId = errorId; + this.message = message; + } + + public OpenPgpError(OpenPgpError b) { + this.errorId = b.errorId; + this.message = b.message; + } + + public int getErrorId() { + return errorId; + } + + public void setErrorId(int errorId) { + this.errorId = errorId; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(errorId); + dest.writeString(message); + } + + public static final Creator CREATOR = new Creator() { + public OpenPgpError createFromParcel(final Parcel source) { + OpenPgpError error = new OpenPgpError(); + error.errorId = source.readInt(); + error.message = source.readString(); + return error; + } + + public OpenPgpError[] newArray(final int size) { + return new OpenPgpError[size]; + } + }; +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpHelper.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpHelper.java new file mode 100644 index 000000000..7305c47ce --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpHelper.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * 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; + +import java.util.List; +import java.util.regex.Pattern; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ResolveInfo; + +public class OpenPgpHelper { + private Context context; + + public static Pattern PGP_MESSAGE = Pattern.compile( + ".*?(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*", Pattern.DOTALL); + + public static Pattern PGP_SIGNED_MESSAGE = Pattern + .compile( + ".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*", + Pattern.DOTALL); + + public OpenPgpHelper(Context context) { + super(); + this.context = context; + } + + public boolean isAvailable() { + Intent intent = new Intent(OpenPgpConstants.SERVICE_INTENT); + List resInfo = context.getPackageManager().queryIntentServices(intent, 0); + if (!resInfo.isEmpty()) { + return true; + } else { + return false; + } + } + +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpListPreference.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpListPreference.java new file mode 100644 index 000000000..4ddd97485 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpListPreference.java @@ -0,0 +1,201 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * 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; + +import java.util.ArrayList; +import java.util.List; + +import android.app.AlertDialog.Builder; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.content.pm.ServiceInfo; +import android.graphics.drawable.Drawable; +import android.preference.DialogPreference; +import android.util.AttributeSet; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ListAdapter; +import android.widget.TextView; + +public class OpenPgpListPreference extends DialogPreference { + ArrayList mProviderList = new ArrayList(); + private String mSelectedPackage; + + public OpenPgpListPreference(Context context, AttributeSet attrs) { + super(context, attrs); + + List resInfo = context.getPackageManager().queryIntentServices( + new Intent(OpenPgpConstants.SERVICE_INTENT), PackageManager.GET_META_DATA); + if (!resInfo.isEmpty()) { + for (ResolveInfo resolveInfo : resInfo) { + if (resolveInfo.serviceInfo == null) + continue; + + String packageName = resolveInfo.serviceInfo.packageName; + String simpleName = String.valueOf(resolveInfo.serviceInfo.loadLabel(context + .getPackageManager())); + Drawable icon = resolveInfo.serviceInfo.loadIcon(context.getPackageManager()); + + // get api version + ServiceInfo si = resolveInfo.serviceInfo; + int apiVersion = si.metaData.getInt("api_version"); + + mProviderList.add(new OpenPgpProviderEntry(packageName, simpleName, icon, + apiVersion)); + } + } + } + + public OpenPgpListPreference(Context context) { + this(context, null); + } + + /** + * Can be used to add "no selection" + * + * @param packageName + * @param simpleName + * @param icon + */ + public void addProvider(int position, String packageName, String simpleName, Drawable icon, + int apiVersion) { + mProviderList.add(position, new OpenPgpProviderEntry(packageName, simpleName, icon, + apiVersion)); + } + + @Override + protected void onPrepareDialogBuilder(Builder builder) { + // Init ArrayAdapter with OpenPGP Providers + ListAdapter adapter = new ArrayAdapter(getContext(), + android.R.layout.select_dialog_singlechoice, android.R.id.text1, mProviderList) { + public View getView(int position, View convertView, ViewGroup parent) { + // User super class to create the View + View v = super.getView(position, convertView, parent); + TextView tv = (TextView) v.findViewById(android.R.id.text1); + + // Put the image on the TextView + tv.setCompoundDrawablesWithIntrinsicBounds(mProviderList.get(position).icon, null, + null, null); + + // Add margin between image and text (support various screen densities) + int dp10 = (int) (10 * getContext().getResources().getDisplayMetrics().density + 0.5f); + tv.setCompoundDrawablePadding(dp10); + + // disable if it has the wrong api_version + if (mProviderList.get(position).apiVersion == OpenPgpConstants.REQUIRED_API_VERSION) { + tv.setEnabled(true); + } else { + tv.setEnabled(false); + tv.setText(tv.getText() + " (API v" + mProviderList.get(position).apiVersion + + ", needs v" + OpenPgpConstants.REQUIRED_API_VERSION + ")"); + } + + return v; + } + }; + + builder.setSingleChoiceItems(adapter, getIndexOfProviderList(getValue()), + new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + mSelectedPackage = mProviderList.get(which).packageName; + + /* + * Clicking on an item simulates the positive button click, and dismisses + * the dialog. + */ + OpenPgpListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE); + dialog.dismiss(); + } + }); + + /* + * The typical interaction for list-based dialogs is to have click-on-an-item dismiss the + * dialog instead of the user having to press 'Ok'. + */ + builder.setPositiveButton(null, null); + } + + @Override + protected void onDialogClosed(boolean positiveResult) { + super.onDialogClosed(positiveResult); + + if (positiveResult && (mSelectedPackage != null)) { + if (callChangeListener(mSelectedPackage)) { + setValue(mSelectedPackage); + } + } + } + + private int getIndexOfProviderList(String packageName) { + for (OpenPgpProviderEntry app : mProviderList) { + if (app.packageName.equals(packageName)) { + return mProviderList.indexOf(app); + } + } + + return -1; + } + + public void setValue(String packageName) { + mSelectedPackage = packageName; + persistString(packageName); + } + + public String getValue() { + return mSelectedPackage; + } + + public String getEntry() { + return getEntryByValue(mSelectedPackage); + } + + public String getEntryByValue(String packageName) { + for (OpenPgpProviderEntry app : mProviderList) { + if (app.packageName.equals(packageName)) { + return app.simpleName; + } + } + + return null; + } + + private static class OpenPgpProviderEntry { + private String packageName; + private String simpleName; + private Drawable icon; + private int apiVersion; + + public OpenPgpProviderEntry(String packageName, String simpleName, Drawable icon, + int apiVersion) { + this.packageName = packageName; + this.simpleName = simpleName; + this.icon = icon; + this.apiVersion = apiVersion; + } + + @Override + public String toString() { + return simpleName; + } + } +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpServiceConnection.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpServiceConnection.java new file mode 100644 index 000000000..f7ba06aaf --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpServiceConnection.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * 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; + +import org.openintents.openpgp.IOpenPgpService; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.IBinder; +import android.util.Log; + +public class OpenPgpServiceConnection { + private Context mApplicationContext; + + private IOpenPgpService mService; + private boolean mBound; + private String mCryptoProviderPackageName; + + public OpenPgpServiceConnection(Context context, String cryptoProviderPackageName) { + this.mApplicationContext = context.getApplicationContext(); + this.mCryptoProviderPackageName = cryptoProviderPackageName; + } + + public IOpenPgpService getService() { + return mService; + } + + public boolean isBound() { + return mBound; + } + + private ServiceConnection mCryptoServiceConnection = new ServiceConnection() { + public void onServiceConnected(ComponentName name, IBinder service) { + mService = IOpenPgpService.Stub.asInterface(service); + Log.d(OpenPgpConstants.TAG, "connected to service"); + mBound = true; + } + + public void onServiceDisconnected(ComponentName name) { + mService = null; + Log.d(OpenPgpConstants.TAG, "disconnected from service"); + mBound = false; + } + }; + + /** + * If not already bound, bind! + * + * @return + */ + public boolean bindToService() { + if (mService == null && !mBound) { // if not already connected + try { + Log.d(OpenPgpConstants.TAG, "not bound yet"); + + Intent serviceIntent = new Intent(); + serviceIntent.setAction(IOpenPgpService.class.getName()); + serviceIntent.setPackage(mCryptoProviderPackageName); + mApplicationContext.bindService(serviceIntent, mCryptoServiceConnection, + Context.BIND_AUTO_CREATE); + + return true; + } catch (Exception e) { + Log.d(OpenPgpConstants.TAG, "Exception on binding", e); + return false; + } + } else { + Log.d(OpenPgpConstants.TAG, "already bound"); + return true; + } + } + + public void unbindFromService() { + mApplicationContext.unbindService(mCryptoServiceConnection); + } + +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java new file mode 100644 index 000000000..829f8f8cf --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * 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; + +import android.os.Parcel; +import android.os.Parcelable; + +public class OpenPgpSignatureResult implements Parcelable { + // generic error on signature verification + public static final int SIGNATURE_ERROR = 0; + // successfully verified signature, with trusted public key + public static final int SIGNATURE_SUCCESS_TRUSTED = 1; + // no public key was found for this signature verification + // you can retrieve the key with + // getKeys(new String[] {String.valueOf(signatureResult.getKeyId)}, true, callback) + public static final int SIGNATURE_UNKNOWN_PUB_KEY = 2; + // successfully verified signature, but with untrusted public key + public static final int SIGNATURE_SUCCESS_UNTRUSTED = 3; + + int status; + boolean signatureOnly; + String userId; + long keyId; + + public int getStatus() { + return status; + } + + public boolean isSignatureOnly() { + return signatureOnly; + } + + public String getUserId() { + return userId; + } + + public long getKeyId() { + return keyId; + } + + public OpenPgpSignatureResult() { + + } + + public OpenPgpSignatureResult(int signatureStatus, String signatureUserId, + boolean signatureOnly, long keyId) { + this.status = signatureStatus; + this.signatureOnly = signatureOnly; + this.userId = signatureUserId; + this.keyId = keyId; + } + + public OpenPgpSignatureResult(OpenPgpSignatureResult b) { + this.status = b.status; + this.userId = b.userId; + this.signatureOnly = b.signatureOnly; + this.keyId = b.keyId; + } + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(status); + dest.writeByte((byte) (signatureOnly ? 1 : 0)); + dest.writeString(userId); + dest.writeLong(keyId); + } + + public static final Creator CREATOR = new Creator() { + public OpenPgpSignatureResult createFromParcel(final Parcel source) { + OpenPgpSignatureResult vr = new OpenPgpSignatureResult(); + vr.status = source.readInt(); + vr.signatureOnly = source.readByte() == 1; + vr.userId = source.readString(); + vr.keyId = source.readLong(); + return vr; + } + + public OpenPgpSignatureResult[] newArray(final int size) { + return new OpenPgpSignatureResult[size]; + } + }; + + @Override + public String toString() { + String out = new String(); + out += "\nstatus: " + status; + out += "\nuserId: " + userId; + out += "\nsignatureOnly: " + signatureOnly; + out += "\nkeyId: " + keyId; + return out; + } + +} -- cgit v1.2.3 From 7939aaaa440f84a0df5524f8a1a1c04dd569eedd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Fri, 14 Feb 2014 02:33:21 +0100 Subject: Introducing new ParcelFileDescriptor pipes --- .../src/main/AndroidManifest.xml | 2 +- .../openpgp/IOpenPgpKeyIdsCallback.aidl | 2 +- .../org/openintents/openpgp/IOpenPgpService.aidl | 46 +++-- .../aidl/org/openintents/openpgp/OpenPgpError.aidl | 2 +- .../openpgp/OpenPgpSignatureResult.aidl | 2 +- .../org/openintents/openpgp/OpenPgpConstants.java | 10 - .../java/org/openintents/openpgp/OpenPgpError.java | 11 +- .../org/openintents/openpgp/OpenPgpHelper.java | 52 ------ .../openintents/openpgp/OpenPgpListPreference.java | 201 --------------------- .../openpgp/OpenPgpServiceConnection.java | 93 ---------- .../openpgp/OpenPgpSignatureResult.java | 2 +- .../org/openintents/openpgp/util/OpenPgpApi.java | 83 +++++++++ .../openintents/openpgp/util/OpenPgpConstants.java | 48 +++++ .../openpgp/util/OpenPgpListPreference.java | 201 +++++++++++++++++++++ .../openpgp/util/OpenPgpServiceConnection.java | 93 ++++++++++ .../org/openintents/openpgp/util/OpenPgpUtils.java | 52 ++++++ .../openpgp/util/ParcelFileDescriptorUtil.java | 104 +++++++++++ 17 files changed, 628 insertions(+), 376 deletions(-) delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpConstants.java delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpHelper.java delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpListPreference.java delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpServiceConnection.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/AndroidManifest.xml b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/AndroidManifest.xml index 32b4a82e5..768922c22 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/AndroidManifest.xml +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/AndroidManifest.xml @@ -5,7 +5,7 @@ android:versionName="1.0" > diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpKeyIdsCallback.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpKeyIdsCallback.aidl index 4ca356fad..f9b5fc453 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpKeyIdsCallback.aidl +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpKeyIdsCallback.aidl @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Dominik Schürmann + * Copyright (C) 2014 Dominik Schürmann * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl index 8f9e8a0fd..ef390a7f6 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Dominik Schürmann + * Copyright (C) 2014 Dominik Schürmann * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,12 +20,35 @@ import org.openintents.openpgp.OpenPgpData; import org.openintents.openpgp.IOpenPgpCallback; import org.openintents.openpgp.IOpenPgpKeyIdsCallback; -/** - * All methods are oneway, which means they are asynchronous and non-blocking. - * Results are returned to the callback, which has to be implemented on client side. - */ interface IOpenPgpService { + /** + * Bundle params: + * api_version 1,2,3,... (current: 1) + * ascii_armor true/false (for output) + * key_ids long[] (for encrypt method) + * + * + * Bundle return: + * result_code RESULT_ERROR=0 (see error), RESULT_OK=1, RESULT_USER_INTERACTION_REQUIRED=2 (execute intent and do it again with params from intent) + * signature_result OpenPgpSignatureResult + * error OpenPgpError + * intent Intent + * + */ + + Bundle sign(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); + + Bundle encrypt(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); + + Bundle signAndEncrypt(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); + + Bundle decryptAndVerify(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); + + +/* + ------------------OLD-------------------------- +*/ /** * Sign * @@ -48,8 +71,9 @@ interface IOpenPgpService { * @param callback * Callback where to return results */ - oneway void sign(in OpenPgpData input, in OpenPgpData output, in IOpenPgpCallback callback); - + //oneway void sign(in OpenPgpData input, in OpenPgpData output, in IOpenPgpCallback callback); + + /** * Encrypt * @@ -74,7 +98,7 @@ interface IOpenPgpService { * @param callback * Callback where to return results */ - oneway void encrypt(in OpenPgpData input, in OpenPgpData output, in long[] keyIds, in IOpenPgpCallback callback); + //oneway void encrypt(in OpenPgpData input, in OpenPgpData output, in long[] keyIds, in IOpenPgpCallback callback); /** * Sign then encrypt @@ -100,7 +124,7 @@ interface IOpenPgpService { * @param callback * Callback where to return results */ - oneway void signAndEncrypt(in OpenPgpData input, in OpenPgpData output, in long[] keyIds, in IOpenPgpCallback callback); + //oneway void signAndEncrypt(in OpenPgpData input, in OpenPgpData output, in long[] keyIds, in IOpenPgpCallback callback); /** * Decrypts and verifies given input bytes. This methods handles encrypted-only, signed-and-encrypted, @@ -126,7 +150,7 @@ interface IOpenPgpService { * @param callback * Callback where to return results */ - oneway void decryptAndVerify(in OpenPgpData input, in OpenPgpData output, in IOpenPgpCallback callback); + //oneway void decryptAndVerify(in OpenPgpData input, in OpenPgpData output, in IOpenPgpCallback callback); /** * Get available key ids based on given user ids @@ -138,6 +162,6 @@ interface IOpenPgpService { * @param callback * Callback where to return results (different type than callback in other functions!) */ - oneway void getKeyIds(in String[] ids, in boolean allowUserInteraction, in IOpenPgpKeyIdsCallback callback); + //oneway void getKeyIds(in String[] ids, in boolean allowUserInteraction, in IOpenPgpKeyIdsCallback callback); } \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl index 7a6bed1e6..7f867653d 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Dominik Schürmann + * Copyright (C) 2014 Dominik Schürmann * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl index e246792d0..d1d138b30 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Dominik Schürmann + * Copyright (C) 2014 Dominik Schürmann * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpConstants.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpConstants.java deleted file mode 100644 index b1ca1bfe6..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpConstants.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.openintents.openpgp; - -public class OpenPgpConstants { - - public static final String TAG = "OpenPgp API"; - - public static final int REQUIRED_API_VERSION = 1; - public static final String SERVICE_INTENT = "org.openintents.openpgp.IOpenPgpService"; - -} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpError.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpError.java index f108d3169..4dd2cc641 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpError.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpError.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Dominik Schürmann + * Copyright (C) 2014 Dominik Schürmann * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,10 +20,13 @@ import android.os.Parcel; import android.os.Parcelable; public class OpenPgpError implements Parcelable { + public static final int CLIENT_SIDE_ERROR = -1; + public static final int GENERIC_ERROR = 0; - public static final int NO_OR_WRONG_PASSPHRASE = 1; - public static final int NO_USER_IDS = 2; - public static final int USER_INTERACTION_REQUIRED = 3; + public static final int INCOMPATIBLE_API_VERSIONS = 1; + + public static final int NO_OR_WRONG_PASSPHRASE = 2; + public static final int NO_USER_IDS = 3; int errorId; String message; diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpHelper.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpHelper.java deleted file mode 100644 index 7305c47ce..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpHelper.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2013 Dominik Schürmann - * - * 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; - -import java.util.List; -import java.util.regex.Pattern; - -import android.content.Context; -import android.content.Intent; -import android.content.pm.ResolveInfo; - -public class OpenPgpHelper { - private Context context; - - public static Pattern PGP_MESSAGE = Pattern.compile( - ".*?(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*", Pattern.DOTALL); - - public static Pattern PGP_SIGNED_MESSAGE = Pattern - .compile( - ".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*", - Pattern.DOTALL); - - public OpenPgpHelper(Context context) { - super(); - this.context = context; - } - - public boolean isAvailable() { - Intent intent = new Intent(OpenPgpConstants.SERVICE_INTENT); - List resInfo = context.getPackageManager().queryIntentServices(intent, 0); - if (!resInfo.isEmpty()) { - return true; - } else { - return false; - } - } - -} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpListPreference.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpListPreference.java deleted file mode 100644 index 4ddd97485..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpListPreference.java +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright (C) 2013 Dominik Schürmann - * - * 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; - -import java.util.ArrayList; -import java.util.List; - -import android.app.AlertDialog.Builder; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; -import android.content.pm.ServiceInfo; -import android.graphics.drawable.Drawable; -import android.preference.DialogPreference; -import android.util.AttributeSet; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.ListAdapter; -import android.widget.TextView; - -public class OpenPgpListPreference extends DialogPreference { - ArrayList mProviderList = new ArrayList(); - private String mSelectedPackage; - - public OpenPgpListPreference(Context context, AttributeSet attrs) { - super(context, attrs); - - List resInfo = context.getPackageManager().queryIntentServices( - new Intent(OpenPgpConstants.SERVICE_INTENT), PackageManager.GET_META_DATA); - if (!resInfo.isEmpty()) { - for (ResolveInfo resolveInfo : resInfo) { - if (resolveInfo.serviceInfo == null) - continue; - - String packageName = resolveInfo.serviceInfo.packageName; - String simpleName = String.valueOf(resolveInfo.serviceInfo.loadLabel(context - .getPackageManager())); - Drawable icon = resolveInfo.serviceInfo.loadIcon(context.getPackageManager()); - - // get api version - ServiceInfo si = resolveInfo.serviceInfo; - int apiVersion = si.metaData.getInt("api_version"); - - mProviderList.add(new OpenPgpProviderEntry(packageName, simpleName, icon, - apiVersion)); - } - } - } - - public OpenPgpListPreference(Context context) { - this(context, null); - } - - /** - * Can be used to add "no selection" - * - * @param packageName - * @param simpleName - * @param icon - */ - public void addProvider(int position, String packageName, String simpleName, Drawable icon, - int apiVersion) { - mProviderList.add(position, new OpenPgpProviderEntry(packageName, simpleName, icon, - apiVersion)); - } - - @Override - protected void onPrepareDialogBuilder(Builder builder) { - // Init ArrayAdapter with OpenPGP Providers - ListAdapter adapter = new ArrayAdapter(getContext(), - android.R.layout.select_dialog_singlechoice, android.R.id.text1, mProviderList) { - public View getView(int position, View convertView, ViewGroup parent) { - // User super class to create the View - View v = super.getView(position, convertView, parent); - TextView tv = (TextView) v.findViewById(android.R.id.text1); - - // Put the image on the TextView - tv.setCompoundDrawablesWithIntrinsicBounds(mProviderList.get(position).icon, null, - null, null); - - // Add margin between image and text (support various screen densities) - int dp10 = (int) (10 * getContext().getResources().getDisplayMetrics().density + 0.5f); - tv.setCompoundDrawablePadding(dp10); - - // disable if it has the wrong api_version - if (mProviderList.get(position).apiVersion == OpenPgpConstants.REQUIRED_API_VERSION) { - tv.setEnabled(true); - } else { - tv.setEnabled(false); - tv.setText(tv.getText() + " (API v" + mProviderList.get(position).apiVersion - + ", needs v" + OpenPgpConstants.REQUIRED_API_VERSION + ")"); - } - - return v; - } - }; - - builder.setSingleChoiceItems(adapter, getIndexOfProviderList(getValue()), - new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - mSelectedPackage = mProviderList.get(which).packageName; - - /* - * Clicking on an item simulates the positive button click, and dismisses - * the dialog. - */ - OpenPgpListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE); - dialog.dismiss(); - } - }); - - /* - * The typical interaction for list-based dialogs is to have click-on-an-item dismiss the - * dialog instead of the user having to press 'Ok'. - */ - builder.setPositiveButton(null, null); - } - - @Override - protected void onDialogClosed(boolean positiveResult) { - super.onDialogClosed(positiveResult); - - if (positiveResult && (mSelectedPackage != null)) { - if (callChangeListener(mSelectedPackage)) { - setValue(mSelectedPackage); - } - } - } - - private int getIndexOfProviderList(String packageName) { - for (OpenPgpProviderEntry app : mProviderList) { - if (app.packageName.equals(packageName)) { - return mProviderList.indexOf(app); - } - } - - return -1; - } - - public void setValue(String packageName) { - mSelectedPackage = packageName; - persistString(packageName); - } - - public String getValue() { - return mSelectedPackage; - } - - public String getEntry() { - return getEntryByValue(mSelectedPackage); - } - - public String getEntryByValue(String packageName) { - for (OpenPgpProviderEntry app : mProviderList) { - if (app.packageName.equals(packageName)) { - return app.simpleName; - } - } - - return null; - } - - private static class OpenPgpProviderEntry { - private String packageName; - private String simpleName; - private Drawable icon; - private int apiVersion; - - public OpenPgpProviderEntry(String packageName, String simpleName, Drawable icon, - int apiVersion) { - this.packageName = packageName; - this.simpleName = simpleName; - this.icon = icon; - this.apiVersion = apiVersion; - } - - @Override - public String toString() { - return simpleName; - } - } -} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpServiceConnection.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpServiceConnection.java deleted file mode 100644 index f7ba06aaf..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpServiceConnection.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2013 Dominik Schürmann - * - * 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; - -import org.openintents.openpgp.IOpenPgpService; - -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.ServiceConnection; -import android.os.IBinder; -import android.util.Log; - -public class OpenPgpServiceConnection { - private Context mApplicationContext; - - private IOpenPgpService mService; - private boolean mBound; - private String mCryptoProviderPackageName; - - public OpenPgpServiceConnection(Context context, String cryptoProviderPackageName) { - this.mApplicationContext = context.getApplicationContext(); - this.mCryptoProviderPackageName = cryptoProviderPackageName; - } - - public IOpenPgpService getService() { - return mService; - } - - public boolean isBound() { - return mBound; - } - - private ServiceConnection mCryptoServiceConnection = new ServiceConnection() { - public void onServiceConnected(ComponentName name, IBinder service) { - mService = IOpenPgpService.Stub.asInterface(service); - Log.d(OpenPgpConstants.TAG, "connected to service"); - mBound = true; - } - - public void onServiceDisconnected(ComponentName name) { - mService = null; - Log.d(OpenPgpConstants.TAG, "disconnected from service"); - mBound = false; - } - }; - - /** - * If not already bound, bind! - * - * @return - */ - public boolean bindToService() { - if (mService == null && !mBound) { // if not already connected - try { - Log.d(OpenPgpConstants.TAG, "not bound yet"); - - Intent serviceIntent = new Intent(); - serviceIntent.setAction(IOpenPgpService.class.getName()); - serviceIntent.setPackage(mCryptoProviderPackageName); - mApplicationContext.bindService(serviceIntent, mCryptoServiceConnection, - Context.BIND_AUTO_CREATE); - - return true; - } catch (Exception e) { - Log.d(OpenPgpConstants.TAG, "Exception on binding", e); - return false; - } - } else { - Log.d(OpenPgpConstants.TAG, "already bound"); - return true; - } - } - - public void unbindFromService() { - mApplicationContext.unbindService(mCryptoServiceConnection); - } - -} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java index 829f8f8cf..226eeacc2 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Dominik Schürmann + * Copyright (C) 2014 Dominik Schürmann * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java new file mode 100644 index 000000000..6efb507c4 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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.util; + +import android.os.Bundle; +import android.os.ParcelFileDescriptor; +import android.os.RemoteException; +import android.util.Log; + +import org.openintents.openpgp.IOpenPgpService; +import org.openintents.openpgp.OpenPgpError; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class OpenPgpApi { + + IOpenPgpService mService; + + public OpenPgpApi(IOpenPgpService service) { + this.mService = service; + } + + public Bundle sign(InputStream is, final OutputStream os) { + try { + // send the input and output pfds + ParcelFileDescriptor input = ParcelFileDescriptorUtil.pipeFrom(is, + new ParcelFileDescriptorUtil.IThreadListener() { + + @Override + public void onThreadFinished(Thread thread) { + Log.d(OpenPgpConstants.TAG, "Copy to service finished"); + } + }); + ParcelFileDescriptor output = ParcelFileDescriptorUtil.pipeTo(os, + new ParcelFileDescriptorUtil.IThreadListener() { + + @Override + public void onThreadFinished(Thread thread) { + Log.d(OpenPgpConstants.TAG, "Service finished writing!"); + } + }); + + // blocks until result is ready + Bundle result = mService.sign(null, input, output); + // close() is required to halt the TransferThread + output.close(); + + return result; + } catch (RemoteException e) { + Log.e(OpenPgpConstants.TAG, "RemoteException", e); + Bundle result = new Bundle(); + result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR); + result.putParcelable(OpenPgpConstants.RESULT_ERRORS, + new OpenPgpError(OpenPgpError.CLIENT_SIDE_ERROR, e.getMessage())); + return result; + } catch (IOException e) { + Log.e(OpenPgpConstants.TAG, "IOException", e); + Bundle result = new Bundle(); + result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR); + result.putParcelable(OpenPgpConstants.RESULT_ERRORS, + new OpenPgpError(OpenPgpError.CLIENT_SIDE_ERROR, e.getMessage())); + return result; + } + } + + +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java new file mode 100644 index 000000000..3dd9391d0 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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.util; + +public class OpenPgpConstants { + + public static final String TAG = "OpenPgp API"; + + public static final int API_VERSION = 1; + public static final String SERVICE_INTENT = "org.openintents.openpgp.IOpenPgpService"; + + + /* Bundle params */ + public static final String PARAMS_API_VERSION = "api_version"; + // request ASCII Armor for output + // OpenPGP Radix-64, 33 percent overhead compared to binary, see http://tools.ietf.org/html/rfc4880#page-53) + public static final String PARAMS_REQUEST_ASCII_ARMOR = "ascii_armor"; + // (for encrypt method) + public static final String PARAMS_KEY_IDS = "key_ids"; + + /* Bundle return */ + public static final String RESULT_CODE = "result_code"; + public static final String RESULT_SIGNATURE = "signature"; + public static final String RESULT_ERRORS = "error"; + public static final String RESULT_INTENT = "intent"; + + // get actual error object from RESULT_ERRORS + public static final int RESULT_CODE_ERROR = 0; + // success! + public static final int RESULT_CODE_SUCCESS = 1; + // execute intent and do it again with params from intent + public static final int RESULT_CODE_USER_INTERACTION_REQUIRED = 2; + +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java new file mode 100644 index 000000000..ea287a7a9 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java @@ -0,0 +1,201 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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.util; + +import java.util.ArrayList; +import java.util.List; + +import android.app.AlertDialog.Builder; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.content.pm.ServiceInfo; +import android.graphics.drawable.Drawable; +import android.preference.DialogPreference; +import android.util.AttributeSet; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ListAdapter; +import android.widget.TextView; + +public class OpenPgpListPreference extends DialogPreference { + ArrayList mProviderList = new ArrayList(); + private String mSelectedPackage; + + public OpenPgpListPreference(Context context, AttributeSet attrs) { + super(context, attrs); + + List resInfo = context.getPackageManager().queryIntentServices( + new Intent(OpenPgpConstants.SERVICE_INTENT), PackageManager.GET_META_DATA); + if (!resInfo.isEmpty()) { + for (ResolveInfo resolveInfo : resInfo) { + if (resolveInfo.serviceInfo == null) + continue; + + String packageName = resolveInfo.serviceInfo.packageName; + String simpleName = String.valueOf(resolveInfo.serviceInfo.loadLabel(context + .getPackageManager())); + Drawable icon = resolveInfo.serviceInfo.loadIcon(context.getPackageManager()); + + // get api version + ServiceInfo si = resolveInfo.serviceInfo; + int apiVersion = si.metaData.getInt("api_version"); + + mProviderList.add(new OpenPgpProviderEntry(packageName, simpleName, icon, + apiVersion)); + } + } + } + + public OpenPgpListPreference(Context context) { + this(context, null); + } + + /** + * Can be used to add "no selection" + * + * @param packageName + * @param simpleName + * @param icon + */ + public void addProvider(int position, String packageName, String simpleName, Drawable icon, + int apiVersion) { + mProviderList.add(position, new OpenPgpProviderEntry(packageName, simpleName, icon, + apiVersion)); + } + + @Override + protected void onPrepareDialogBuilder(Builder builder) { + // Init ArrayAdapter with OpenPGP Providers + ListAdapter adapter = new ArrayAdapter(getContext(), + android.R.layout.select_dialog_singlechoice, android.R.id.text1, mProviderList) { + public View getView(int position, View convertView, ViewGroup parent) { + // User super class to create the View + View v = super.getView(position, convertView, parent); + TextView tv = (TextView) v.findViewById(android.R.id.text1); + + // Put the image on the TextView + tv.setCompoundDrawablesWithIntrinsicBounds(mProviderList.get(position).icon, null, + null, null); + + // Add margin between image and text (support various screen densities) + int dp10 = (int) (10 * getContext().getResources().getDisplayMetrics().density + 0.5f); + tv.setCompoundDrawablePadding(dp10); + + // disable if it has the wrong api_version + if (mProviderList.get(position).apiVersion == OpenPgpConstants.API_VERSION) { + tv.setEnabled(true); + } else { + tv.setEnabled(false); + tv.setText(tv.getText() + " (API v" + mProviderList.get(position).apiVersion + + ", needs v" + OpenPgpConstants.API_VERSION + ")"); + } + + return v; + } + }; + + builder.setSingleChoiceItems(adapter, getIndexOfProviderList(getValue()), + new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + mSelectedPackage = mProviderList.get(which).packageName; + + /* + * Clicking on an item simulates the positive button click, and dismisses + * the dialog. + */ + OpenPgpListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE); + dialog.dismiss(); + } + }); + + /* + * The typical interaction for list-based dialogs is to have click-on-an-item dismiss the + * dialog instead of the user having to press 'Ok'. + */ + builder.setPositiveButton(null, null); + } + + @Override + protected void onDialogClosed(boolean positiveResult) { + super.onDialogClosed(positiveResult); + + if (positiveResult && (mSelectedPackage != null)) { + if (callChangeListener(mSelectedPackage)) { + setValue(mSelectedPackage); + } + } + } + + private int getIndexOfProviderList(String packageName) { + for (OpenPgpProviderEntry app : mProviderList) { + if (app.packageName.equals(packageName)) { + return mProviderList.indexOf(app); + } + } + + return -1; + } + + public void setValue(String packageName) { + mSelectedPackage = packageName; + persistString(packageName); + } + + public String getValue() { + return mSelectedPackage; + } + + public String getEntry() { + return getEntryByValue(mSelectedPackage); + } + + public String getEntryByValue(String packageName) { + for (OpenPgpProviderEntry app : mProviderList) { + if (app.packageName.equals(packageName)) { + return app.simpleName; + } + } + + return null; + } + + private static class OpenPgpProviderEntry { + private String packageName; + private String simpleName; + private Drawable icon; + private int apiVersion; + + public OpenPgpProviderEntry(String packageName, String simpleName, Drawable icon, + int apiVersion) { + this.packageName = packageName; + this.simpleName = simpleName; + this.icon = icon; + this.apiVersion = apiVersion; + } + + @Override + public String toString() { + return simpleName; + } + } +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java new file mode 100644 index 000000000..780b4606b --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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.util; + +import org.openintents.openpgp.IOpenPgpService; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.IBinder; +import android.util.Log; + +public class OpenPgpServiceConnection { + private Context mApplicationContext; + + private IOpenPgpService mService; + private boolean mBound; + private String mCryptoProviderPackageName; + + public OpenPgpServiceConnection(Context context, String cryptoProviderPackageName) { + this.mApplicationContext = context.getApplicationContext(); + this.mCryptoProviderPackageName = cryptoProviderPackageName; + } + + public IOpenPgpService getService() { + return mService; + } + + public boolean isBound() { + return mBound; + } + + private ServiceConnection mCryptoServiceConnection = new ServiceConnection() { + public void onServiceConnected(ComponentName name, IBinder service) { + mService = IOpenPgpService.Stub.asInterface(service); + Log.d(OpenPgpConstants.TAG, "connected to service"); + mBound = true; + } + + public void onServiceDisconnected(ComponentName name) { + mService = null; + Log.d(OpenPgpConstants.TAG, "disconnected from service"); + mBound = false; + } + }; + + /** + * If not already bound, bind! + * + * @return + */ + public boolean bindToService() { + if (mService == null && !mBound) { // if not already connected + try { + Log.d(OpenPgpConstants.TAG, "not bound yet"); + + Intent serviceIntent = new Intent(); + serviceIntent.setAction(IOpenPgpService.class.getName()); + serviceIntent.setPackage(mCryptoProviderPackageName); + mApplicationContext.bindService(serviceIntent, mCryptoServiceConnection, + Context.BIND_AUTO_CREATE); + + return true; + } catch (Exception e) { + Log.d(OpenPgpConstants.TAG, "Exception on binding", e); + return false; + } + } else { + Log.d(OpenPgpConstants.TAG, "already bound"); + return true; + } + } + + public void unbindFromService() { + mApplicationContext.unbindService(mCryptoServiceConnection); + } + +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java new file mode 100644 index 000000000..6dbf76897 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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.util; + +import java.util.List; +import java.util.regex.Pattern; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ResolveInfo; + +public class OpenPgpUtils { + private Context context; + + public static Pattern PGP_MESSAGE = Pattern.compile( + ".*?(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*", Pattern.DOTALL); + + public static Pattern PGP_SIGNED_MESSAGE = Pattern + .compile( + ".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*", + Pattern.DOTALL); + + public OpenPgpUtils(Context context) { + super(); + this.context = context; + } + + public boolean isAvailable() { + Intent intent = new Intent(OpenPgpConstants.SERVICE_INTENT); + List resInfo = context.getPackageManager().queryIntentServices(intent, 0); + if (!resInfo.isEmpty()) { + return true; + } else { + return false; + } + } + +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java new file mode 100644 index 000000000..20f8c36f3 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * 2013 Flow (http://stackoverflow.com/questions/18212152/transfer-inputstream-to-another-service-across-process-boundaries-with-parcelf) + * + * 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.util; + +import android.os.ParcelFileDescriptor; +import android.util.Log; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class ParcelFileDescriptorUtil { + + public interface IThreadListener { + void onThreadFinished(final Thread thread); + } + + public static ParcelFileDescriptor pipeFrom(InputStream inputStream, IThreadListener listener) + throws IOException { + ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe(); + ParcelFileDescriptor readSide = pipe[0]; + ParcelFileDescriptor writeSide = pipe[1]; + + // start the transfer thread + new TransferThread(inputStream, new ParcelFileDescriptor.AutoCloseOutputStream(writeSide), + listener) + .start(); + + return readSide; + } + + public static ParcelFileDescriptor pipeTo(OutputStream outputStream, IThreadListener listener) + throws IOException { + ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe(); + ParcelFileDescriptor readSide = pipe[0]; + ParcelFileDescriptor writeSide = pipe[1]; + + // start the transfer thread + new TransferThread(new ParcelFileDescriptor.AutoCloseInputStream(readSide), outputStream, + listener) + .start(); + + return writeSide; + } + + static class TransferThread extends Thread { + final InputStream mIn; + final OutputStream mOut; + final IThreadListener mListener; + + TransferThread(InputStream in, OutputStream out, IThreadListener listener) { + super("ParcelFileDescriptor Transfer Thread"); + mIn = in; + mOut = out; + mListener = listener; + setDaemon(true); + } + + @Override + public void run() { + byte[] buf = new byte[1024]; + int len; + + try { + while ((len = mIn.read(buf)) > 0) { + mOut.write(buf, 0, len); + } + mOut.flush(); // just to be safe + } catch (IOException e) { + Log.e(OpenPgpConstants.TAG, "TransferThread" + getId() + ": writing failed", e); + } finally { + try { + mIn.close(); + } catch (IOException e) { + e.printStackTrace(); + } + try { + mOut.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (mListener != null) { + Log.d(OpenPgpConstants.TAG, "TransferThread " + getId() + " finished!"); + mListener.onThreadFinished(this); + } + } + } +} \ No newline at end of file -- cgit v1.2.3 From ee2fec17597c35ad2329f44cc697e18e3e4169cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Fri, 14 Feb 2014 02:41:27 +0100 Subject: fix nullpointer --- .../src/main/java/org/openintents/openpgp/util/OpenPgpApi.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java index 6efb507c4..d490bca0c 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java @@ -56,8 +56,11 @@ public class OpenPgpApi { } }); + Bundle params = new Bundle(); + params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION); + // blocks until result is ready - Bundle result = mService.sign(null, input, output); + Bundle result = mService.sign(params, input, output); // close() is required to halt the TransferThread output.close(); -- cgit v1.2.3 From acad2ba95723a940467e89a07e91498188a88745 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Fri, 14 Feb 2014 13:40:24 +0100 Subject: PendingIntent to handle user input --- .../org/openintents/openpgp/IOpenPgpService.aidl | 151 ++++----------------- .../org/openintents/openpgp/util/OpenPgpApi.java | 123 +++++++++++++++-- .../openintents/openpgp/util/OpenPgpConstants.java | 2 +- 3 files changed, 140 insertions(+), 136 deletions(-) (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl index ef390a7f6..714ca040e 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl @@ -23,145 +23,52 @@ import org.openintents.openpgp.IOpenPgpKeyIdsCallback; interface IOpenPgpService { /** - * Bundle params: - * api_version 1,2,3,... (current: 1) - * ascii_armor true/false (for output) - * key_ids long[] (for encrypt method) + * General extras + * -------------- + * + * params: + * int api_version (current: 1) + * boolean ascii_armor true/false (for output) * * * Bundle return: - * result_code RESULT_ERROR=0 (see error), RESULT_OK=1, RESULT_USER_INTERACTION_REQUIRED=2 (execute intent and do it again with params from intent) - * signature_result OpenPgpSignatureResult - * error OpenPgpError - * intent Intent + * int result_code 0,1, or 2 (see OpenPgpConstants) + * OpenPgpSignatureResult signature_result + * OpenPgpError error + * Intent intent * */ - Bundle sign(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); - - Bundle encrypt(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); - - Bundle signAndEncrypt(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); - - Bundle decryptAndVerify(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); - - -/* - ------------------OLD-------------------------- -*/ /** - * Sign - * - * After successful signing, callback's onSuccess will contain the resulting output. + * sign only * - * @param input - * OpenPgpData object containing String, byte[], ParcelFileDescriptor, or Uri - * @param output - * Request output format by defining OpenPgpData object - * - * new OpenPgpData(OpenPgpData.TYPE_STRING) - * Returns as String - * (OpenPGP Radix-64, 33 percent overhead compared to binary, see http://tools.ietf.org/html/rfc4880#page-53) - * new OpenPgpData(OpenPgpData.TYPE_BYTE_ARRAY) - * Returns as byte[] - * new OpenPgpData(uri) - * Writes output to given Uri - * new OpenPgpData(fileDescriptor) - * Writes output to given ParcelFileDescriptor - * @param callback - * Callback where to return results + * params: + * String passphrase (optional) */ - //oneway void sign(in OpenPgpData input, in OpenPgpData output, in IOpenPgpCallback callback); - + Bundle sign(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); /** - * Encrypt - * - * After successful encryption, callback's onSuccess will contain the resulting output. - * - * @param input - * OpenPgpData object containing String, byte[], ParcelFileDescriptor, or Uri - * @param output - * Request output format by defining OpenPgpData object - * - * new OpenPgpData(OpenPgpData.TYPE_STRING) - * Returns as String - * (OpenPGP Radix-64, 33 percent overhead compared to binary, see http://tools.ietf.org/html/rfc4880#page-53) - * new OpenPgpData(OpenPgpData.TYPE_BYTE_ARRAY) - * Returns as byte[] - * new OpenPgpData(uri) - * Writes output to given Uri - * new OpenPgpData(fileDescriptor) - * Writes output to given ParcelFileDescriptor - * @param keyIds - * Key Ids of recipients. Can be retrieved with getKeyIds() - * @param callback - * Callback where to return results + * encrypt + * + * params: + * long[] key_ids + * or + * String[] user_ids (= emails of recipients) (if more than one key has this user_id, an Intent is returned) */ - //oneway void encrypt(in OpenPgpData input, in OpenPgpData output, in long[] keyIds, in IOpenPgpCallback callback); - + Bundle encrypt(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); + /** - * Sign then encrypt - * - * After successful signing and encryption, callback's onSuccess will contain the resulting output. + * sign and encrypt * - * @param input - * OpenPgpData object containing String, byte[], ParcelFileDescriptor, or Uri - * @param output - * Request output format by defining OpenPgpData object - * - * new OpenPgpData(OpenPgpData.TYPE_STRING) - * Returns as String - * (OpenPGP Radix-64, 33 percent overhead compared to binary, see http://tools.ietf.org/html/rfc4880#page-53) - * new OpenPgpData(OpenPgpData.TYPE_BYTE_ARRAY) - * Returns as byte[] - * new OpenPgpData(uri) - * Writes output to given Uri - * new OpenPgpData(fileDescriptor) - * Writes output to given ParcelFileDescriptor - * @param keyIds - * Key Ids of recipients. Can be retrieved with getKeyIds() - * @param callback - * Callback where to return results + * params: + * same as in encrypt() */ - //oneway void signAndEncrypt(in OpenPgpData input, in OpenPgpData output, in long[] keyIds, in IOpenPgpCallback callback); - + Bundle signAndEncrypt(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); + /** * Decrypts and verifies given input bytes. This methods handles encrypted-only, signed-and-encrypted, * and also signed-only input. - * - * After successful decryption/verification, callback's onSuccess will contain the resulting output. - * The signatureResult in onSuccess is only non-null if signed-and-encrypted or signed-only inputBytes were given. - * - * @param input - * OpenPgpData object containing String, byte[], ParcelFileDescriptor, or Uri - * @param output - * Request output format by defining OpenPgpData object - * - * new OpenPgpData(OpenPgpData.TYPE_STRING) - * Returns as String - * (OpenPGP Radix-64, 33 percent overhead compared to binary, see http://tools.ietf.org/html/rfc4880#page-53) - * new OpenPgpData(OpenPgpData.TYPE_BYTE_ARRAY) - * Returns as byte[] - * new OpenPgpData(uri) - * Writes output to given Uri - * new OpenPgpData(fileDescriptor) - * Writes output to given ParcelFileDescriptor - * @param callback - * Callback where to return results */ - //oneway void decryptAndVerify(in OpenPgpData input, in OpenPgpData output, in IOpenPgpCallback callback); - - /** - * Get available key ids based on given user ids - * - * @param ids - * User Ids (emails) of recipients OR key ids - * @param allowUserInteraction - * Enable user interaction to lookup and import unknown keys - * @param callback - * Callback where to return results (different type than callback in other functions!) - */ - //oneway void getKeyIds(in String[] ids, in boolean allowUserInteraction, in IOpenPgpKeyIdsCallback callback); - + Bundle decryptAndVerify(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); + } \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java index d490bca0c..adb187c04 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java @@ -16,15 +16,14 @@ package org.openintents.openpgp.util; +import android.os.AsyncTask; import android.os.Bundle; import android.os.ParcelFileDescriptor; -import android.os.RemoteException; import android.util.Log; import org.openintents.openpgp.IOpenPgpService; import org.openintents.openpgp.OpenPgpError; -import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -32,11 +31,98 @@ public class OpenPgpApi { IOpenPgpService mService; + private static final int OPERATION_SIGN = 0; + private static final int OPERATION_ENCRYPT = 1; + private static final int OPERATION_SIGN_ENCRYPT = 2; + private static final int OPERATION_DECRYPT_VERIFY = 3; + public OpenPgpApi(IOpenPgpService service) { this.mService = service; } public Bundle sign(InputStream is, final OutputStream os) { + return executeApi(OPERATION_SIGN, new Bundle(), is, os); + } + + public Bundle sign(Bundle params, InputStream is, final OutputStream os) { + return executeApi(OPERATION_SIGN, params, is, os); + } + + public void sign(Bundle params, InputStream is, final OutputStream os, IOpenPgpCallback callback) { + executeApiAsync(OPERATION_SIGN, params, is, os, callback); + } + + public Bundle encrypt(InputStream is, final OutputStream os) { + return executeApi(OPERATION_ENCRYPT, new Bundle(), is, os); + } + + public Bundle encrypt(Bundle params, InputStream is, final OutputStream os) { + return executeApi(OPERATION_ENCRYPT, params, is, os); + } + + public void encrypt(Bundle params, InputStream is, final OutputStream os, IOpenPgpCallback callback) { + executeApiAsync(OPERATION_ENCRYPT, params, is, os, callback); + } + + public Bundle signAndEncrypt(InputStream is, final OutputStream os) { + return executeApi(OPERATION_SIGN_ENCRYPT, new Bundle(), is, os); + } + + public Bundle signAndEncrypt(Bundle params, InputStream is, final OutputStream os) { + return executeApi(OPERATION_SIGN_ENCRYPT, params, is, os); + } + + public void signAndEncrypt(Bundle params, InputStream is, final OutputStream os, IOpenPgpCallback callback) { + executeApiAsync(OPERATION_SIGN_ENCRYPT, params, is, os, callback); + } + + public Bundle decryptAndVerify(InputStream is, final OutputStream os) { + return executeApi(OPERATION_DECRYPT_VERIFY, new Bundle(), is, os); + } + + public Bundle decryptAndVerify(Bundle params, InputStream is, final OutputStream os) { + return executeApi(OPERATION_DECRYPT_VERIFY, params, is, os); + } + + public void decryptAndVerify(Bundle params, InputStream is, final OutputStream os, IOpenPgpCallback callback) { + executeApiAsync(OPERATION_DECRYPT_VERIFY, params, is, os, callback); + } + + public interface IOpenPgpCallback { + void onReturn(final Bundle result); + } + + private class OpenPgpAsyncTask extends AsyncTask { + int operationId; + Bundle params; + InputStream is; + OutputStream os; + IOpenPgpCallback callback; + + private OpenPgpAsyncTask(int operationId, Bundle params, InputStream is, OutputStream os, IOpenPgpCallback callback) { + this.operationId = operationId; + this.params = params; + this.is = is; + this.os = os; + this.callback = callback; + } + + @Override + protected Bundle doInBackground(Void... unused) { + return executeApi(operationId, params, is, os); + } + + protected void onPostExecute(Bundle result) { + callback.onReturn(result); + } + + } + + private void executeApiAsync(int operationId, Bundle params, InputStream is, OutputStream os, IOpenPgpCallback callback) { + new OpenPgpAsyncTask(operationId, params, is, os, callback).execute((Void[]) null); + } + + private Bundle executeApi(int operationId, Bundle params, InputStream is, OutputStream os) { try { // send the input and output pfds ParcelFileDescriptor input = ParcelFileDescriptorUtil.pipeFrom(is, @@ -56,24 +142,35 @@ public class OpenPgpApi { } }); - Bundle params = new Bundle(); params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION); + // default result is error + Bundle result = new Bundle(); + result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR); + result.putParcelable(OpenPgpConstants.RESULT_ERRORS, + new OpenPgpError(OpenPgpError.GENERIC_ERROR, "This should never happen!")); + // blocks until result is ready - Bundle result = mService.sign(params, input, output); + switch (operationId) { + case OPERATION_SIGN: + result = mService.sign(params, input, output); + break; + case OPERATION_ENCRYPT: + result = mService.encrypt(params, input, output); + break; + case OPERATION_SIGN_ENCRYPT: + result = mService.signAndEncrypt(params, input, output); + break; + case OPERATION_DECRYPT_VERIFY: + result = mService.decryptAndVerify(params, input, output); + break; + } // close() is required to halt the TransferThread output.close(); return result; - } catch (RemoteException e) { - Log.e(OpenPgpConstants.TAG, "RemoteException", e); - Bundle result = new Bundle(); - result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR); - result.putParcelable(OpenPgpConstants.RESULT_ERRORS, - new OpenPgpError(OpenPgpError.CLIENT_SIDE_ERROR, e.getMessage())); - return result; - } catch (IOException e) { - Log.e(OpenPgpConstants.TAG, "IOException", e); + } catch (Exception e) { + Log.e(OpenPgpConstants.TAG, "Exception", e); Bundle result = new Bundle(); result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR); result.putParcelable(OpenPgpConstants.RESULT_ERRORS, diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java index 3dd9391d0..1bf3d76a3 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java @@ -42,7 +42,7 @@ public class OpenPgpConstants { public static final int RESULT_CODE_ERROR = 0; // success! public static final int RESULT_CODE_SUCCESS = 1; - // execute intent and do it again with params from intent + // executeServiceMethod intent and do it again with params from intent public static final int RESULT_CODE_USER_INTERACTION_REQUIRED = 2; } -- cgit v1.2.3 From d6953745810bd4c6dee3bfefb538236b2b7bdbb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Fri, 14 Feb 2014 17:01:17 +0100 Subject: conversion of other methods --- .../org/openintents/openpgp/IOpenPgpCallback.aidl | 45 -------- .../openpgp/IOpenPgpKeyIdsCallback.aidl | 39 ------- .../org/openintents/openpgp/IOpenPgpService.aidl | 9 +- .../aidl/org/openintents/openpgp/OpenPgpData.aidl | 20 ---- .../java/org/openintents/openpgp/OpenPgpData.java | 127 --------------------- .../openpgp/OpenPgpSignatureResult.java | 8 +- .../openintents/openpgp/util/OpenPgpConstants.java | 1 + 7 files changed, 6 insertions(+), 243 deletions(-) delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpCallback.aidl delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpKeyIdsCallback.aidl delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpData.aidl delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpData.java (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpCallback.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpCallback.aidl deleted file mode 100644 index ba41de1ba..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpCallback.aidl +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2013 Dominik Schürmann - * - * 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; - -import org.openintents.openpgp.OpenPgpData; -import org.openintents.openpgp.OpenPgpSignatureResult; -import org.openintents.openpgp.OpenPgpError; - -interface IOpenPgpCallback { - - /** - * onSuccess returns on successful OpenPGP operations. - * - * @param output - * contains resulting output (decrypted content (when input was encrypted) - * or content without signature (when input was signed-only)) - * @param signatureResult - * signatureResult is only non-null if decryptAndVerify() was called and the content - * was encrypted or signed-and-encrypted. - */ - oneway void onSuccess(in OpenPgpData output, in OpenPgpSignatureResult signatureResult); - - /** - * onError returns on errors or when allowUserInteraction was set to false, but user interaction - * was required execute an OpenPGP operation. - * - * @param error - * See OpenPgpError class for more information. - */ - oneway void onError(in OpenPgpError error); -} \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpKeyIdsCallback.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpKeyIdsCallback.aidl deleted file mode 100644 index f9b5fc453..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpKeyIdsCallback.aidl +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * - * 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; - -import org.openintents.openpgp.OpenPgpError; - -interface IOpenPgpKeyIdsCallback { - - /** - * onSuccess returns on successful getKeyIds operations. - * - * @param keyIds - * returned key ids - */ - oneway void onSuccess(in long[] keyIds); - - /** - * onError returns on errors or when allowUserInteraction was set to false, but user interaction - * was required execute an OpenPGP operation. - * - * @param error - * See OpenPgpError class for more information. - */ - oneway void onError(in OpenPgpError error); -} \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl index 714ca040e..ced1df026 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl @@ -16,10 +16,6 @@ package org.openintents.openpgp; -import org.openintents.openpgp.OpenPgpData; -import org.openintents.openpgp.IOpenPgpCallback; -import org.openintents.openpgp.IOpenPgpKeyIdsCallback; - interface IOpenPgpService { /** @@ -29,7 +25,7 @@ interface IOpenPgpService { * params: * int api_version (current: 1) * boolean ascii_armor true/false (for output) - * + * String passphrase (for key, optional) * * Bundle return: * int result_code 0,1, or 2 (see OpenPgpConstants) @@ -41,9 +37,6 @@ interface IOpenPgpService { /** * sign only - * - * params: - * String passphrase (optional) */ Bundle sign(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpData.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpData.aidl deleted file mode 100644 index 3711e4fb4..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpData.aidl +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (C) 2013 Dominik Schürmann - * - * 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; - -// Declare OpenPgpData so AIDL can find it and knows that it implements the parcelable protocol. -parcelable OpenPgpData; \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpData.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpData.java deleted file mode 100644 index 6615c2146..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpData.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (C) 2013 Dominik Schürmann - * - * 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; - -import android.net.Uri; -import android.os.Parcel; -import android.os.ParcelFileDescriptor; -import android.os.Parcelable; - -public class OpenPgpData implements Parcelable { - public static final int TYPE_STRING = 0; - public static final int TYPE_BYTE_ARRAY = 1; - public static final int TYPE_FILE_DESCRIPTOR = 2; - public static final int TYPE_URI = 3; - - int type; - - String string; - byte[] bytes = new byte[0]; - ParcelFileDescriptor fileDescriptor; - Uri uri; - - public int getType() { - return type; - } - - public String getString() { - return string; - } - - public byte[] getBytes() { - return bytes; - } - - public ParcelFileDescriptor getFileDescriptor() { - return fileDescriptor; - } - - public Uri getUri() { - return uri; - } - - public OpenPgpData() { - - } - - /** - * Not a real constructor. This can be used to define requested output type. - * - * @param type - */ - public OpenPgpData(int type) { - this.type = type; - } - - public OpenPgpData(String string) { - this.string = string; - this.type = TYPE_STRING; - } - - public OpenPgpData(byte[] bytes) { - this.bytes = bytes; - this.type = TYPE_BYTE_ARRAY; - } - - public OpenPgpData(ParcelFileDescriptor fileDescriptor) { - this.fileDescriptor = fileDescriptor; - this.type = TYPE_FILE_DESCRIPTOR; - } - - public OpenPgpData(Uri uri) { - this.uri = uri; - this.type = TYPE_URI; - } - - public OpenPgpData(OpenPgpData b) { - this.string = b.string; - this.bytes = b.bytes; - this.fileDescriptor = b.fileDescriptor; - this.uri = b.uri; - } - - public int describeContents() { - return 0; - } - - public void writeToParcel(Parcel dest, int flags) { - dest.writeInt(type); - dest.writeString(string); - dest.writeInt(bytes.length); - dest.writeByteArray(bytes); - dest.writeParcelable(fileDescriptor, 0); - dest.writeParcelable(uri, 0); - } - - public static final Creator CREATOR = new Creator() { - public OpenPgpData createFromParcel(final Parcel source) { - OpenPgpData vr = new OpenPgpData(); - vr.type = source.readInt(); - vr.string = source.readString(); - vr.bytes = new byte[source.readInt()]; - source.readByteArray(vr.bytes); - vr.fileDescriptor = source.readParcelable(ParcelFileDescriptor.class.getClassLoader()); - vr.fileDescriptor = source.readParcelable(Uri.class.getClassLoader()); - return vr; - } - - public OpenPgpData[] newArray(final int size) { - return new OpenPgpData[size]; - } - }; - -} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java index 226eeacc2..16c79ca27 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java @@ -22,14 +22,14 @@ import android.os.Parcelable; public class OpenPgpSignatureResult implements Parcelable { // generic error on signature verification public static final int SIGNATURE_ERROR = 0; - // successfully verified signature, with trusted public key - public static final int SIGNATURE_SUCCESS_TRUSTED = 1; + // successfully verified signature, with certified public key + public static final int SIGNATURE_SUCCESS_CERTIFIED = 1; // no public key was found for this signature verification // you can retrieve the key with // getKeys(new String[] {String.valueOf(signatureResult.getKeyId)}, true, callback) public static final int SIGNATURE_UNKNOWN_PUB_KEY = 2; - // successfully verified signature, but with untrusted public key - public static final int SIGNATURE_SUCCESS_UNTRUSTED = 3; + // successfully verified signature, but with certified public key + public static final int SIGNATURE_SUCCESS_UNCERTIFIED = 3; int status; boolean signatureOnly; diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java index 1bf3d76a3..64bc92fdd 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java @@ -30,6 +30,7 @@ public class OpenPgpConstants { // OpenPGP Radix-64, 33 percent overhead compared to binary, see http://tools.ietf.org/html/rfc4880#page-53) public static final String PARAMS_REQUEST_ASCII_ARMOR = "ascii_armor"; // (for encrypt method) + public static final String PARAMS_USER_IDS = "user_ids"; public static final String PARAMS_KEY_IDS = "key_ids"; /* Bundle return */ -- cgit v1.2.3 From 581e6edb4cdfb6274f21e532ba60a610a3ce1fc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Fri, 14 Feb 2014 17:19:54 +0100 Subject: cleanup --- .../service/remote/IExtendedApiCallback.aidl | 24 ----------- .../service/remote/IExtendedApiService.aidl | 48 ---------------------- 2 files changed, 72 deletions(-) delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiCallback.aidl delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiService.aidl (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiCallback.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiCallback.aidl deleted file mode 100644 index f69f66fd7..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiCallback.aidl +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) 2013 Dominik Schürmann - * - * 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.sufficientlysecure.keychain.service.remote; - -interface IExtendedApiCallback { - - oneway void onSuccess(in byte[] outputBytes); - - oneway void onError(in String error); -} \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiService.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiService.aidl deleted file mode 100644 index 669bd31b5..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/sufficientlysecure/keychain/service/remote/IExtendedApiService.aidl +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2013 Dominik Schürmann - * - * 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.sufficientlysecure.keychain.service.remote; - -import org.sufficientlysecure.keychain.service.remote.IExtendedApiCallback; - -/** - * All methods are oneway, which means they are asynchronous and non-blocking. - * Results are returned to the callback, which has to be implemented on client side. - */ -interface IExtendedApiService { - - /** - * Symmetric Encrypt - * - * @param inputBytes - * Byte array you want to encrypt - * @param passphrase - * symmetric passhprase - * @param callback - * Callback where to return results - */ - oneway void encrypt(in byte[] inputBytes, in String passphrase, in IExtendedApiCallback callback); - - /** - * Generates self signed X509 certificate signed by OpenPGP private key (from app settings) - * - * @param subjAltNameURI - * @param callback - * Callback where to return results - */ - oneway void selfSignedX509Cert(in String subjAltNameURI, in IExtendedApiCallback callback); - -} \ No newline at end of file -- cgit v1.2.3 From 494a5fa414fe5962bdee0d50e761da9dc0cc1cc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 15 Feb 2014 01:06:03 +0100 Subject: private request codes, pass params through methods and pending intents, getKeyIds method --- .../org/openintents/openpgp/IOpenPgpService.aidl | 11 +++ .../org/openintents/openpgp/util/OpenPgpApi.java | 83 ++++++++++++---------- .../openpgp/util/ParcelFileDescriptorUtil.java | 4 +- 3 files changed, 60 insertions(+), 38 deletions(-) (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl index ced1df026..2d22f8f2d 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl @@ -64,4 +64,15 @@ interface IOpenPgpService { */ Bundle decryptAndVerify(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); + /** + * Retrieves key ids based on given user ids (=emails) + * + * params: + * String[] user_ids + * + * result: + * long[] key_ids + */ + Bundle getKeyIds(in Bundle params); + } \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java index adb187c04..5f67ddf88 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java @@ -35,6 +35,7 @@ public class OpenPgpApi { private static final int OPERATION_ENCRYPT = 1; private static final int OPERATION_SIGN_ENCRYPT = 2; private static final int OPERATION_DECRYPT_VERIFY = 3; + private static final int OPERATION_GET_KEY_IDS = 4; public OpenPgpApi(IOpenPgpService service) { this.mService = service; @@ -88,6 +89,10 @@ public class OpenPgpApi { executeApiAsync(OPERATION_DECRYPT_VERIFY, params, is, os, callback); } + public Bundle getKeyIds(Bundle params) { + return executeApi(OPERATION_GET_KEY_IDS, params, null, null); + } + public interface IOpenPgpCallback { void onReturn(final Bundle result); } @@ -124,24 +129,6 @@ public class OpenPgpApi { private Bundle executeApi(int operationId, Bundle params, InputStream is, OutputStream os) { try { - // send the input and output pfds - ParcelFileDescriptor input = ParcelFileDescriptorUtil.pipeFrom(is, - new ParcelFileDescriptorUtil.IThreadListener() { - - @Override - public void onThreadFinished(Thread thread) { - Log.d(OpenPgpConstants.TAG, "Copy to service finished"); - } - }); - ParcelFileDescriptor output = ParcelFileDescriptorUtil.pipeTo(os, - new ParcelFileDescriptorUtil.IThreadListener() { - - @Override - public void onThreadFinished(Thread thread) { - Log.d(OpenPgpConstants.TAG, "Service finished writing!"); - } - }); - params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION); // default result is error @@ -150,25 +137,49 @@ public class OpenPgpApi { result.putParcelable(OpenPgpConstants.RESULT_ERRORS, new OpenPgpError(OpenPgpError.GENERIC_ERROR, "This should never happen!")); - // blocks until result is ready - switch (operationId) { - case OPERATION_SIGN: - result = mService.sign(params, input, output); - break; - case OPERATION_ENCRYPT: - result = mService.encrypt(params, input, output); - break; - case OPERATION_SIGN_ENCRYPT: - result = mService.signAndEncrypt(params, input, output); - break; - case OPERATION_DECRYPT_VERIFY: - result = mService.decryptAndVerify(params, input, output); - break; + if (operationId == OPERATION_GET_KEY_IDS) { + result = mService.getKeyIds(params); + return result; + } else { + // send the input and output pfds + ParcelFileDescriptor input = ParcelFileDescriptorUtil.pipeFrom(is, + new ParcelFileDescriptorUtil.IThreadListener() { + + @Override + public void onThreadFinished(Thread thread) { + Log.d(OpenPgpConstants.TAG, "Copy to service finished"); + } + }); + ParcelFileDescriptor output = ParcelFileDescriptorUtil.pipeTo(os, + new ParcelFileDescriptorUtil.IThreadListener() { + + @Override + public void onThreadFinished(Thread thread) { + Log.d(OpenPgpConstants.TAG, "Service finished writing!"); + } + }); + + + // blocks until result is ready + switch (operationId) { + case OPERATION_SIGN: + result = mService.sign(params, input, output); + break; + case OPERATION_ENCRYPT: + result = mService.encrypt(params, input, output); + break; + case OPERATION_SIGN_ENCRYPT: + result = mService.signAndEncrypt(params, input, output); + break; + case OPERATION_DECRYPT_VERIFY: + result = mService.decryptAndVerify(params, input, output); + break; + } + // close() is required to halt the TransferThread + output.close(); + + return result; } - // close() is required to halt the TransferThread - output.close(); - - return result; } catch (Exception e) { Log.e(OpenPgpConstants.TAG, "Exception", e); Bundle result = new Bundle(); diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java index 20f8c36f3..75d4b8c18 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java @@ -87,12 +87,12 @@ public class ParcelFileDescriptorUtil { try { mIn.close(); } catch (IOException e) { - e.printStackTrace(); + Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e); } try { mOut.close(); } catch (IOException e) { - e.printStackTrace(); + Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e); } } if (mListener != null) { -- cgit v1.2.3 From 5f39cb3ec0d1d3f77d48fdbb428a2147dfe11d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 15 Feb 2014 02:08:27 +0100 Subject: fix passtrough of params --- .../src/main/java/org/openintents/openpgp/util/OpenPgpApi.java | 4 ---- .../src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java | 5 ++++- 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java index 5f67ddf88..5c38d3304 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java @@ -131,11 +131,7 @@ public class OpenPgpApi { try { params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION); - // default result is error Bundle result = new Bundle(); - result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR); - result.putParcelable(OpenPgpConstants.RESULT_ERRORS, - new OpenPgpError(OpenPgpError.GENERIC_ERROR, "This should never happen!")); if (operationId == OPERATION_GET_KEY_IDS) { result = mService.getKeyIds(params); diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java index 64bc92fdd..229c8d42a 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java @@ -33,7 +33,7 @@ public class OpenPgpConstants { public static final String PARAMS_USER_IDS = "user_ids"; public static final String PARAMS_KEY_IDS = "key_ids"; - /* Bundle return */ + /* Service Bundle returns */ public static final String RESULT_CODE = "result_code"; public static final String RESULT_SIGNATURE = "signature"; public static final String RESULT_ERRORS = "error"; @@ -46,4 +46,7 @@ public class OpenPgpConstants { // executeServiceMethod intent and do it again with params from intent public static final int RESULT_CODE_USER_INTERACTION_REQUIRED = 2; + /* PendingIntent returns */ + public static final String PI_RESULT_PARAMS = "params"; + } -- cgit v1.2.3 From 52f1c930ebe98251fb9d3b34b2725087f2917f64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 15 Feb 2014 13:00:47 +0100 Subject: parse string util method, better help text for access screen --- .../aidl/org/openintents/openpgp/OpenPgpError.aidl | 2 ++ .../openpgp/OpenPgpSignatureResult.aidl | 2 ++ .../openintents/openpgp/util/OpenPgpConstants.java | 2 ++ .../openpgp/util/OpenPgpListPreference.java | 39 +++++----------------- .../openpgp/util/OpenPgpServiceConnection.java | 5 +-- .../org/openintents/openpgp/util/OpenPgpUtils.java | 34 +++++++++++++------ 6 files changed, 41 insertions(+), 43 deletions(-) (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl index 7f867653d..0fbea0a8c 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl @@ -16,5 +16,7 @@ package org.openintents.openpgp; +import org.openintents.openpgp.OpenPgpError; + // Declare OpenPgpError so AIDL can find it and knows that it implements the parcelable protocol. parcelable OpenPgpError; \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl index d1d138b30..dfc7e0992 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl @@ -16,5 +16,7 @@ package org.openintents.openpgp; +import org.openintents.openpgp.OpenPgpSignatureResult; + // Declare OpenPgpSignatureResult so AIDL can find it and knows that it implements the parcelable protocol. parcelable OpenPgpSignatureResult; \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java index 229c8d42a..263b42aaa 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java @@ -32,6 +32,8 @@ public class OpenPgpConstants { // (for encrypt method) public static final String PARAMS_USER_IDS = "user_ids"; public static final String PARAMS_KEY_IDS = "key_ids"; + // optional parameter: + public static final String PARAMS_PASSPHRASE = "passphrase"; /* Service Bundle returns */ public static final String RESULT_CODE = "result_code"; diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java index ea287a7a9..e29794e87 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java @@ -16,16 +16,11 @@ package org.openintents.openpgp.util; -import java.util.ArrayList; -import java.util.List; - import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; -import android.content.pm.ServiceInfo; import android.graphics.drawable.Drawable; import android.preference.DialogPreference; import android.util.AttributeSet; @@ -35,6 +30,9 @@ import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.TextView; +import java.util.ArrayList; +import java.util.List; + public class OpenPgpListPreference extends DialogPreference { ArrayList mProviderList = new ArrayList(); private String mSelectedPackage; @@ -42,8 +40,8 @@ public class OpenPgpListPreference extends DialogPreference { public OpenPgpListPreference(Context context, AttributeSet attrs) { super(context, attrs); - List resInfo = context.getPackageManager().queryIntentServices( - new Intent(OpenPgpConstants.SERVICE_INTENT), PackageManager.GET_META_DATA); + Intent intent = new Intent(OpenPgpConstants.SERVICE_INTENT); + List resInfo = context.getPackageManager().queryIntentServices(intent, 0); if (!resInfo.isEmpty()) { for (ResolveInfo resolveInfo : resInfo) { if (resolveInfo.serviceInfo == null) @@ -54,12 +52,7 @@ public class OpenPgpListPreference extends DialogPreference { .getPackageManager())); Drawable icon = resolveInfo.serviceInfo.loadIcon(context.getPackageManager()); - // get api version - ServiceInfo si = resolveInfo.serviceInfo; - int apiVersion = si.metaData.getInt("api_version"); - - mProviderList.add(new OpenPgpProviderEntry(packageName, simpleName, icon, - apiVersion)); + mProviderList.add(new OpenPgpProviderEntry(packageName, simpleName, icon)); } } } @@ -75,10 +68,8 @@ public class OpenPgpListPreference extends DialogPreference { * @param simpleName * @param icon */ - public void addProvider(int position, String packageName, String simpleName, Drawable icon, - int apiVersion) { - mProviderList.add(position, new OpenPgpProviderEntry(packageName, simpleName, icon, - apiVersion)); + public void addProvider(int position, String packageName, String simpleName, Drawable icon) { + mProviderList.add(position, new OpenPgpProviderEntry(packageName, simpleName, icon)); } @Override @@ -99,15 +90,6 @@ public class OpenPgpListPreference extends DialogPreference { int dp10 = (int) (10 * getContext().getResources().getDisplayMetrics().density + 0.5f); tv.setCompoundDrawablePadding(dp10); - // disable if it has the wrong api_version - if (mProviderList.get(position).apiVersion == OpenPgpConstants.API_VERSION) { - tv.setEnabled(true); - } else { - tv.setEnabled(false); - tv.setText(tv.getText() + " (API v" + mProviderList.get(position).apiVersion - + ", needs v" + OpenPgpConstants.API_VERSION + ")"); - } - return v; } }; @@ -183,14 +165,11 @@ public class OpenPgpListPreference extends DialogPreference { private String packageName; private String simpleName; private Drawable icon; - private int apiVersion; - public OpenPgpProviderEntry(String packageName, String simpleName, Drawable icon, - int apiVersion) { + public OpenPgpProviderEntry(String packageName, String simpleName, Drawable icon) { this.packageName = packageName; this.simpleName = simpleName; this.icon = icon; - this.apiVersion = apiVersion; } @Override diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java index 780b4606b..8e8812faa 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java @@ -61,11 +61,12 @@ public class OpenPgpServiceConnection { /** * If not already bound, bind! - * + * * @return */ public boolean bindToService() { - if (mService == null && !mBound) { // if not already connected + // if not already connected + if (mService == null && !mBound) { try { Log.d(OpenPgpConstants.TAG, "not bound yet"); diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java index 6dbf76897..ffecaceba 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java @@ -17,6 +17,7 @@ package org.openintents.openpgp.util; import java.util.List; +import java.util.regex.Matcher; import java.util.regex.Pattern; import android.content.Context; @@ -24,22 +25,33 @@ import android.content.Intent; import android.content.pm.ResolveInfo; public class OpenPgpUtils { - private Context context; - public static Pattern PGP_MESSAGE = Pattern.compile( - ".*?(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*", Pattern.DOTALL); + public static final Pattern PGP_MESSAGE = Pattern.compile( + ".*?(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*", + Pattern.DOTALL); - public static Pattern PGP_SIGNED_MESSAGE = Pattern - .compile( - ".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*", - Pattern.DOTALL); + public static final Pattern PGP_SIGNED_MESSAGE = Pattern.compile( + ".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*", + Pattern.DOTALL); - public OpenPgpUtils(Context context) { - super(); - this.context = context; + public static final int PARSE_RESULT_NO_PGP = -1; + public static final int PARSE_RESULT_MESSAGE = 0; + public static final int PARSE_RESULT_SIGNED_MESSAGE = 1; + + public static int parseMessage(String message) { + Matcher matcherSigned = PGP_SIGNED_MESSAGE.matcher(message); + Matcher matcherMessage = PGP_MESSAGE.matcher(message); + + if (matcherMessage.matches()) { + return PARSE_RESULT_MESSAGE; + } else if (matcherSigned.matches()) { + return PARSE_RESULT_SIGNED_MESSAGE; + } else { + return PARSE_RESULT_NO_PGP; + } } - public boolean isAvailable() { + public static boolean isAvailable(Context context) { Intent intent = new Intent(OpenPgpConstants.SERVICE_INTENT); List resInfo = context.getPackageManager().queryIntentServices(intent, 0); if (!resInfo.isEmpty()) { -- cgit v1.2.3 From f9578d24fad3d96b490fd5a8cf7ce84e8742e69b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 15 Feb 2014 15:19:50 +0100 Subject: documentation on service --- .../org/openintents/openpgp/IOpenPgpService.aidl | 41 +++++++++++++--------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl index 2d22f8f2d..578a7d4b5 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl @@ -22,38 +22,42 @@ interface IOpenPgpService { * General extras * -------------- * - * params: - * int api_version (current: 1) - * boolean ascii_armor true/false (for output) - * String passphrase (for key, optional) + * Bundle params: + * int api_version (required) + * boolean ascii_armor (request ascii armor for ouput) * - * Bundle return: - * int result_code 0,1, or 2 (see OpenPgpConstants) - * OpenPgpSignatureResult signature_result - * OpenPgpError error - * Intent intent + * returned Bundle: + * int result_code (0, 1, or 2 (see OpenPgpConstants)) + * OpenPgpError error (if result_code == 0) + * Intent intent (if result_code == 2) * */ /** - * sign only + * Sign only + * + * optional params: + * String passphrase (for key passphrase) */ Bundle sign(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); /** - * encrypt + * Encrypt * - * params: + * Bundle params: * long[] key_ids * or - * String[] user_ids (= emails of recipients) (if more than one key has this user_id, an Intent is returned) + * String[] user_ids (= emails of recipients) (if more than one key has this user_id, a PendingIntent is returned) + * + * optional params: + * String passphrase (for key passphrase) */ Bundle encrypt(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); /** - * sign and encrypt + * Sign and encrypt * - * params: + * Bundle params: * same as in encrypt() */ Bundle signAndEncrypt(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); @@ -61,16 +65,19 @@ interface IOpenPgpService { /** * Decrypts and verifies given input bytes. This methods handles encrypted-only, signed-and-encrypted, * and also signed-only input. + * + * returned Bundle: + * OpenPgpSignatureResult signature_result */ Bundle decryptAndVerify(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); /** * Retrieves key ids based on given user ids (=emails) * - * params: + * Bundle params: * String[] user_ids * - * result: + * returned Bundle: * long[] key_ids */ Bundle getKeyIds(in Bundle params); -- cgit v1.2.3 From 3d59796337e99c0153cf173eba617d156f158d3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 15 Feb 2014 15:46:30 +0100 Subject: Fix problem with unparceling by setting class loader in api lib, remove unneeded aidl classes --- .../aidl/org/openintents/openpgp/OpenPgpError.aidl | 22 ---------------------- .../openpgp/OpenPgpSignatureResult.aidl | 22 ---------------------- .../org/openintents/openpgp/util/OpenPgpApi.java | 12 ++++++++++-- 3 files changed, 10 insertions(+), 46 deletions(-) delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl deleted file mode 100644 index 0fbea0a8c..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpError.aidl +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * - * 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; - -import org.openintents.openpgp.OpenPgpError; - -// Declare OpenPgpError so AIDL can find it and knows that it implements the parcelable protocol. -parcelable OpenPgpError; \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl deleted file mode 100644 index dfc7e0992..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/OpenPgpSignatureResult.aidl +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * - * 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; - -import org.openintents.openpgp.OpenPgpSignatureResult; - -// Declare OpenPgpSignatureResult so AIDL can find it and knows that it implements the parcelable protocol. -parcelable OpenPgpSignatureResult; \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java index 5c38d3304..f121c345d 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java @@ -16,6 +16,7 @@ package org.openintents.openpgp.util; +import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.os.ParcelFileDescriptor; @@ -30,6 +31,7 @@ import java.io.OutputStream; public class OpenPgpApi { IOpenPgpService mService; + Context mContext; private static final int OPERATION_SIGN = 0; private static final int OPERATION_ENCRYPT = 1; @@ -37,7 +39,8 @@ public class OpenPgpApi { private static final int OPERATION_DECRYPT_VERIFY = 3; private static final int OPERATION_GET_KEY_IDS = 4; - public OpenPgpApi(IOpenPgpService service) { + public OpenPgpApi(Context context, IOpenPgpService service) { + this.mContext = context; this.mService = service; } @@ -131,7 +134,7 @@ public class OpenPgpApi { try { params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION); - Bundle result = new Bundle(); + Bundle result = null; if (operationId == OPERATION_GET_KEY_IDS) { result = mService.getKeyIds(params); @@ -174,6 +177,11 @@ public class OpenPgpApi { // close() is required to halt the TransferThread output.close(); + // set class loader to current context to allow unparcelling + // of OpenPgpError and OpenPgpSignatureResult + // http://stackoverflow.com/a/3806769 + result.setClassLoader(mContext.getClassLoader()); + return result; } } catch (Exception e) { -- cgit v1.2.3 From 2b98f2a0d7e37542a062886437c3f7b14599a4da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 15 Feb 2014 17:09:21 +0100 Subject: Restructure API lib folder to support Eclipse --- .../src/main/AndroidManifest.xml | 13 -- .../org/openintents/openpgp/IOpenPgpService.aidl | 85 --------- .../java/org/openintents/openpgp/OpenPgpError.java | 84 --------- .../openpgp/OpenPgpSignatureResult.java | 110 ------------ .../org/openintents/openpgp/util/OpenPgpApi.java | 198 --------------------- .../openintents/openpgp/util/OpenPgpConstants.java | 54 ------ .../openpgp/util/OpenPgpListPreference.java | 180 ------------------- .../openpgp/util/OpenPgpServiceConnection.java | 94 ---------- .../org/openintents/openpgp/util/OpenPgpUtils.java | 64 ------- .../openpgp/util/ParcelFileDescriptorUtil.java | 104 ----------- .../org/openintents/openpgp/IOpenPgpService.aidl | 85 +++++++++ .../src/org/openintents/openpgp/OpenPgpError.java | 84 +++++++++ .../openpgp/OpenPgpSignatureResult.java | 110 ++++++++++++ .../org/openintents/openpgp/util/OpenPgpApi.java | 198 +++++++++++++++++++++ .../openintents/openpgp/util/OpenPgpConstants.java | 54 ++++++ .../openpgp/util/OpenPgpListPreference.java | 180 +++++++++++++++++++ .../openpgp/util/OpenPgpServiceConnection.java | 94 ++++++++++ .../org/openintents/openpgp/util/OpenPgpUtils.java | 64 +++++++ .../openpgp/util/ParcelFileDescriptorUtil.java | 104 +++++++++++ 19 files changed, 973 insertions(+), 986 deletions(-) delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/AndroidManifest.xml delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpError.java delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/IOpenPgpService.aidl create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/OpenPgpError.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/OpenPgpSignatureResult.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpConstants.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpServiceConnection.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpUtils.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/AndroidManifest.xml b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/AndroidManifest.xml deleted file mode 100644 index 768922c22..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/AndroidManifest.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl deleted file mode 100644 index 578a7d4b5..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * - * 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 { - - /** - * General extras - * -------------- - * - * Bundle params: - * int api_version (required) - * boolean ascii_armor (request ascii armor for ouput) - * - * returned Bundle: - * int result_code (0, 1, or 2 (see OpenPgpConstants)) - * OpenPgpError error (if result_code == 0) - * Intent intent (if result_code == 2) - * - */ - - /** - * Sign only - * - * optional params: - * String passphrase (for key passphrase) - */ - Bundle sign(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); - - /** - * Encrypt - * - * Bundle params: - * long[] key_ids - * or - * String[] user_ids (= emails of recipients) (if more than one key has this user_id, a PendingIntent is returned) - * - * optional params: - * String passphrase (for key passphrase) - */ - Bundle encrypt(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); - - /** - * Sign and encrypt - * - * Bundle params: - * same as in encrypt() - */ - Bundle signAndEncrypt(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); - - /** - * Decrypts and verifies given input bytes. This methods handles encrypted-only, signed-and-encrypted, - * and also signed-only input. - * - * returned Bundle: - * OpenPgpSignatureResult signature_result - */ - Bundle decryptAndVerify(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); - - /** - * Retrieves key ids based on given user ids (=emails) - * - * Bundle params: - * String[] user_ids - * - * returned Bundle: - * long[] key_ids - */ - Bundle getKeyIds(in Bundle params); - -} \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpError.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpError.java deleted file mode 100644 index 4dd2cc641..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpError.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * - * 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; - -import android.os.Parcel; -import android.os.Parcelable; - -public class OpenPgpError implements Parcelable { - public static final int CLIENT_SIDE_ERROR = -1; - - public static final int GENERIC_ERROR = 0; - public static final int INCOMPATIBLE_API_VERSIONS = 1; - - public static final int NO_OR_WRONG_PASSPHRASE = 2; - public static final int NO_USER_IDS = 3; - - int errorId; - String message; - - public OpenPgpError() { - } - - public OpenPgpError(int errorId, String message) { - this.errorId = errorId; - this.message = message; - } - - public OpenPgpError(OpenPgpError b) { - this.errorId = b.errorId; - this.message = b.message; - } - - public int getErrorId() { - return errorId; - } - - public void setErrorId(int errorId) { - this.errorId = errorId; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public int describeContents() { - return 0; - } - - public void writeToParcel(Parcel dest, int flags) { - dest.writeInt(errorId); - dest.writeString(message); - } - - public static final Creator CREATOR = new Creator() { - public OpenPgpError createFromParcel(final Parcel source) { - OpenPgpError error = new OpenPgpError(); - error.errorId = source.readInt(); - error.message = source.readString(); - return error; - } - - public OpenPgpError[] newArray(final int size) { - return new OpenPgpError[size]; - } - }; -} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java deleted file mode 100644 index 16c79ca27..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * - * 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; - -import android.os.Parcel; -import android.os.Parcelable; - -public class OpenPgpSignatureResult implements Parcelable { - // generic error on signature verification - public static final int SIGNATURE_ERROR = 0; - // successfully verified signature, with certified public key - public static final int SIGNATURE_SUCCESS_CERTIFIED = 1; - // no public key was found for this signature verification - // you can retrieve the key with - // getKeys(new String[] {String.valueOf(signatureResult.getKeyId)}, true, callback) - public static final int SIGNATURE_UNKNOWN_PUB_KEY = 2; - // successfully verified signature, but with certified public key - public static final int SIGNATURE_SUCCESS_UNCERTIFIED = 3; - - int status; - boolean signatureOnly; - String userId; - long keyId; - - public int getStatus() { - return status; - } - - public boolean isSignatureOnly() { - return signatureOnly; - } - - public String getUserId() { - return userId; - } - - public long getKeyId() { - return keyId; - } - - public OpenPgpSignatureResult() { - - } - - public OpenPgpSignatureResult(int signatureStatus, String signatureUserId, - boolean signatureOnly, long keyId) { - this.status = signatureStatus; - this.signatureOnly = signatureOnly; - this.userId = signatureUserId; - this.keyId = keyId; - } - - public OpenPgpSignatureResult(OpenPgpSignatureResult b) { - this.status = b.status; - this.userId = b.userId; - this.signatureOnly = b.signatureOnly; - this.keyId = b.keyId; - } - - public int describeContents() { - return 0; - } - - public void writeToParcel(Parcel dest, int flags) { - dest.writeInt(status); - dest.writeByte((byte) (signatureOnly ? 1 : 0)); - dest.writeString(userId); - dest.writeLong(keyId); - } - - public static final Creator CREATOR = new Creator() { - public OpenPgpSignatureResult createFromParcel(final Parcel source) { - OpenPgpSignatureResult vr = new OpenPgpSignatureResult(); - vr.status = source.readInt(); - vr.signatureOnly = source.readByte() == 1; - vr.userId = source.readString(); - vr.keyId = source.readLong(); - return vr; - } - - public OpenPgpSignatureResult[] newArray(final int size) { - return new OpenPgpSignatureResult[size]; - } - }; - - @Override - public String toString() { - String out = new String(); - out += "\nstatus: " + status; - out += "\nuserId: " + userId; - out += "\nsignatureOnly: " + signatureOnly; - out += "\nkeyId: " + keyId; - return out; - } - -} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java deleted file mode 100644 index f121c345d..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * - * 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.util; - -import android.content.Context; -import android.os.AsyncTask; -import android.os.Bundle; -import android.os.ParcelFileDescriptor; -import android.util.Log; - -import org.openintents.openpgp.IOpenPgpService; -import org.openintents.openpgp.OpenPgpError; - -import java.io.InputStream; -import java.io.OutputStream; - -public class OpenPgpApi { - - IOpenPgpService mService; - Context mContext; - - private static final int OPERATION_SIGN = 0; - private static final int OPERATION_ENCRYPT = 1; - private static final int OPERATION_SIGN_ENCRYPT = 2; - private static final int OPERATION_DECRYPT_VERIFY = 3; - private static final int OPERATION_GET_KEY_IDS = 4; - - public OpenPgpApi(Context context, IOpenPgpService service) { - this.mContext = context; - this.mService = service; - } - - public Bundle sign(InputStream is, final OutputStream os) { - return executeApi(OPERATION_SIGN, new Bundle(), is, os); - } - - public Bundle sign(Bundle params, InputStream is, final OutputStream os) { - return executeApi(OPERATION_SIGN, params, is, os); - } - - public void sign(Bundle params, InputStream is, final OutputStream os, IOpenPgpCallback callback) { - executeApiAsync(OPERATION_SIGN, params, is, os, callback); - } - - public Bundle encrypt(InputStream is, final OutputStream os) { - return executeApi(OPERATION_ENCRYPT, new Bundle(), is, os); - } - - public Bundle encrypt(Bundle params, InputStream is, final OutputStream os) { - return executeApi(OPERATION_ENCRYPT, params, is, os); - } - - public void encrypt(Bundle params, InputStream is, final OutputStream os, IOpenPgpCallback callback) { - executeApiAsync(OPERATION_ENCRYPT, params, is, os, callback); - } - - public Bundle signAndEncrypt(InputStream is, final OutputStream os) { - return executeApi(OPERATION_SIGN_ENCRYPT, new Bundle(), is, os); - } - - public Bundle signAndEncrypt(Bundle params, InputStream is, final OutputStream os) { - return executeApi(OPERATION_SIGN_ENCRYPT, params, is, os); - } - - public void signAndEncrypt(Bundle params, InputStream is, final OutputStream os, IOpenPgpCallback callback) { - executeApiAsync(OPERATION_SIGN_ENCRYPT, params, is, os, callback); - } - - public Bundle decryptAndVerify(InputStream is, final OutputStream os) { - return executeApi(OPERATION_DECRYPT_VERIFY, new Bundle(), is, os); - } - - public Bundle decryptAndVerify(Bundle params, InputStream is, final OutputStream os) { - return executeApi(OPERATION_DECRYPT_VERIFY, params, is, os); - } - - public void decryptAndVerify(Bundle params, InputStream is, final OutputStream os, IOpenPgpCallback callback) { - executeApiAsync(OPERATION_DECRYPT_VERIFY, params, is, os, callback); - } - - public Bundle getKeyIds(Bundle params) { - return executeApi(OPERATION_GET_KEY_IDS, params, null, null); - } - - public interface IOpenPgpCallback { - void onReturn(final Bundle result); - } - - private class OpenPgpAsyncTask extends AsyncTask { - int operationId; - Bundle params; - InputStream is; - OutputStream os; - IOpenPgpCallback callback; - - private OpenPgpAsyncTask(int operationId, Bundle params, InputStream is, OutputStream os, IOpenPgpCallback callback) { - this.operationId = operationId; - this.params = params; - this.is = is; - this.os = os; - this.callback = callback; - } - - @Override - protected Bundle doInBackground(Void... unused) { - return executeApi(operationId, params, is, os); - } - - protected void onPostExecute(Bundle result) { - callback.onReturn(result); - } - - } - - private void executeApiAsync(int operationId, Bundle params, InputStream is, OutputStream os, IOpenPgpCallback callback) { - new OpenPgpAsyncTask(operationId, params, is, os, callback).execute((Void[]) null); - } - - private Bundle executeApi(int operationId, Bundle params, InputStream is, OutputStream os) { - try { - params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION); - - Bundle result = null; - - if (operationId == OPERATION_GET_KEY_IDS) { - result = mService.getKeyIds(params); - return result; - } else { - // send the input and output pfds - ParcelFileDescriptor input = ParcelFileDescriptorUtil.pipeFrom(is, - new ParcelFileDescriptorUtil.IThreadListener() { - - @Override - public void onThreadFinished(Thread thread) { - Log.d(OpenPgpConstants.TAG, "Copy to service finished"); - } - }); - ParcelFileDescriptor output = ParcelFileDescriptorUtil.pipeTo(os, - new ParcelFileDescriptorUtil.IThreadListener() { - - @Override - public void onThreadFinished(Thread thread) { - Log.d(OpenPgpConstants.TAG, "Service finished writing!"); - } - }); - - - // blocks until result is ready - switch (operationId) { - case OPERATION_SIGN: - result = mService.sign(params, input, output); - break; - case OPERATION_ENCRYPT: - result = mService.encrypt(params, input, output); - break; - case OPERATION_SIGN_ENCRYPT: - result = mService.signAndEncrypt(params, input, output); - break; - case OPERATION_DECRYPT_VERIFY: - result = mService.decryptAndVerify(params, input, output); - break; - } - // close() is required to halt the TransferThread - output.close(); - - // set class loader to current context to allow unparcelling - // of OpenPgpError and OpenPgpSignatureResult - // http://stackoverflow.com/a/3806769 - result.setClassLoader(mContext.getClassLoader()); - - return result; - } - } catch (Exception e) { - Log.e(OpenPgpConstants.TAG, "Exception", e); - Bundle result = new Bundle(); - result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR); - result.putParcelable(OpenPgpConstants.RESULT_ERRORS, - new OpenPgpError(OpenPgpError.CLIENT_SIDE_ERROR, e.getMessage())); - return result; - } - } - - -} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java deleted file mode 100644 index 263b42aaa..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * - * 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.util; - -public class OpenPgpConstants { - - public static final String TAG = "OpenPgp API"; - - public static final int API_VERSION = 1; - public static final String SERVICE_INTENT = "org.openintents.openpgp.IOpenPgpService"; - - - /* Bundle params */ - public static final String PARAMS_API_VERSION = "api_version"; - // request ASCII Armor for output - // OpenPGP Radix-64, 33 percent overhead compared to binary, see http://tools.ietf.org/html/rfc4880#page-53) - public static final String PARAMS_REQUEST_ASCII_ARMOR = "ascii_armor"; - // (for encrypt method) - public static final String PARAMS_USER_IDS = "user_ids"; - public static final String PARAMS_KEY_IDS = "key_ids"; - // optional parameter: - public static final String PARAMS_PASSPHRASE = "passphrase"; - - /* Service Bundle returns */ - public static final String RESULT_CODE = "result_code"; - public static final String RESULT_SIGNATURE = "signature"; - public static final String RESULT_ERRORS = "error"; - public static final String RESULT_INTENT = "intent"; - - // get actual error object from RESULT_ERRORS - public static final int RESULT_CODE_ERROR = 0; - // success! - public static final int RESULT_CODE_SUCCESS = 1; - // executeServiceMethod intent and do it again with params from intent - public static final int RESULT_CODE_USER_INTERACTION_REQUIRED = 2; - - /* PendingIntent returns */ - public static final String PI_RESULT_PARAMS = "params"; - -} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java deleted file mode 100644 index e29794e87..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java +++ /dev/null @@ -1,180 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * - * 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.util; - -import android.app.AlertDialog.Builder; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.content.pm.ResolveInfo; -import android.graphics.drawable.Drawable; -import android.preference.DialogPreference; -import android.util.AttributeSet; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.ListAdapter; -import android.widget.TextView; - -import java.util.ArrayList; -import java.util.List; - -public class OpenPgpListPreference extends DialogPreference { - ArrayList mProviderList = new ArrayList(); - private String mSelectedPackage; - - public OpenPgpListPreference(Context context, AttributeSet attrs) { - super(context, attrs); - - Intent intent = new Intent(OpenPgpConstants.SERVICE_INTENT); - List resInfo = context.getPackageManager().queryIntentServices(intent, 0); - if (!resInfo.isEmpty()) { - for (ResolveInfo resolveInfo : resInfo) { - if (resolveInfo.serviceInfo == null) - continue; - - String packageName = resolveInfo.serviceInfo.packageName; - String simpleName = String.valueOf(resolveInfo.serviceInfo.loadLabel(context - .getPackageManager())); - Drawable icon = resolveInfo.serviceInfo.loadIcon(context.getPackageManager()); - - mProviderList.add(new OpenPgpProviderEntry(packageName, simpleName, icon)); - } - } - } - - public OpenPgpListPreference(Context context) { - this(context, null); - } - - /** - * Can be used to add "no selection" - * - * @param packageName - * @param simpleName - * @param icon - */ - public void addProvider(int position, String packageName, String simpleName, Drawable icon) { - mProviderList.add(position, new OpenPgpProviderEntry(packageName, simpleName, icon)); - } - - @Override - protected void onPrepareDialogBuilder(Builder builder) { - // Init ArrayAdapter with OpenPGP Providers - ListAdapter adapter = new ArrayAdapter(getContext(), - android.R.layout.select_dialog_singlechoice, android.R.id.text1, mProviderList) { - public View getView(int position, View convertView, ViewGroup parent) { - // User super class to create the View - View v = super.getView(position, convertView, parent); - TextView tv = (TextView) v.findViewById(android.R.id.text1); - - // Put the image on the TextView - tv.setCompoundDrawablesWithIntrinsicBounds(mProviderList.get(position).icon, null, - null, null); - - // Add margin between image and text (support various screen densities) - int dp10 = (int) (10 * getContext().getResources().getDisplayMetrics().density + 0.5f); - tv.setCompoundDrawablePadding(dp10); - - return v; - } - }; - - builder.setSingleChoiceItems(adapter, getIndexOfProviderList(getValue()), - new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - mSelectedPackage = mProviderList.get(which).packageName; - - /* - * Clicking on an item simulates the positive button click, and dismisses - * the dialog. - */ - OpenPgpListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE); - dialog.dismiss(); - } - }); - - /* - * The typical interaction for list-based dialogs is to have click-on-an-item dismiss the - * dialog instead of the user having to press 'Ok'. - */ - builder.setPositiveButton(null, null); - } - - @Override - protected void onDialogClosed(boolean positiveResult) { - super.onDialogClosed(positiveResult); - - if (positiveResult && (mSelectedPackage != null)) { - if (callChangeListener(mSelectedPackage)) { - setValue(mSelectedPackage); - } - } - } - - private int getIndexOfProviderList(String packageName) { - for (OpenPgpProviderEntry app : mProviderList) { - if (app.packageName.equals(packageName)) { - return mProviderList.indexOf(app); - } - } - - return -1; - } - - public void setValue(String packageName) { - mSelectedPackage = packageName; - persistString(packageName); - } - - public String getValue() { - return mSelectedPackage; - } - - public String getEntry() { - return getEntryByValue(mSelectedPackage); - } - - public String getEntryByValue(String packageName) { - for (OpenPgpProviderEntry app : mProviderList) { - if (app.packageName.equals(packageName)) { - return app.simpleName; - } - } - - return null; - } - - private static class OpenPgpProviderEntry { - private String packageName; - private String simpleName; - private Drawable icon; - - public OpenPgpProviderEntry(String packageName, String simpleName, Drawable icon) { - this.packageName = packageName; - this.simpleName = simpleName; - this.icon = icon; - } - - @Override - public String toString() { - return simpleName; - } - } -} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java deleted file mode 100644 index 8e8812faa..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * - * 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.util; - -import org.openintents.openpgp.IOpenPgpService; - -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.ServiceConnection; -import android.os.IBinder; -import android.util.Log; - -public class OpenPgpServiceConnection { - private Context mApplicationContext; - - private IOpenPgpService mService; - private boolean mBound; - private String mCryptoProviderPackageName; - - public OpenPgpServiceConnection(Context context, String cryptoProviderPackageName) { - this.mApplicationContext = context.getApplicationContext(); - this.mCryptoProviderPackageName = cryptoProviderPackageName; - } - - public IOpenPgpService getService() { - return mService; - } - - public boolean isBound() { - return mBound; - } - - private ServiceConnection mCryptoServiceConnection = new ServiceConnection() { - public void onServiceConnected(ComponentName name, IBinder service) { - mService = IOpenPgpService.Stub.asInterface(service); - Log.d(OpenPgpConstants.TAG, "connected to service"); - mBound = true; - } - - public void onServiceDisconnected(ComponentName name) { - mService = null; - Log.d(OpenPgpConstants.TAG, "disconnected from service"); - mBound = false; - } - }; - - /** - * If not already bound, bind! - * - * @return - */ - public boolean bindToService() { - // if not already connected - if (mService == null && !mBound) { - try { - Log.d(OpenPgpConstants.TAG, "not bound yet"); - - Intent serviceIntent = new Intent(); - serviceIntent.setAction(IOpenPgpService.class.getName()); - serviceIntent.setPackage(mCryptoProviderPackageName); - mApplicationContext.bindService(serviceIntent, mCryptoServiceConnection, - Context.BIND_AUTO_CREATE); - - return true; - } catch (Exception e) { - Log.d(OpenPgpConstants.TAG, "Exception on binding", e); - return false; - } - } else { - Log.d(OpenPgpConstants.TAG, "already bound"); - return true; - } - } - - public void unbindFromService() { - mApplicationContext.unbindService(mCryptoServiceConnection); - } - -} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java deleted file mode 100644 index ffecaceba..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * - * 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.util; - -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import android.content.Context; -import android.content.Intent; -import android.content.pm.ResolveInfo; - -public class OpenPgpUtils { - - public static final Pattern PGP_MESSAGE = Pattern.compile( - ".*?(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*", - Pattern.DOTALL); - - public static final Pattern PGP_SIGNED_MESSAGE = Pattern.compile( - ".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*", - Pattern.DOTALL); - - public static final int PARSE_RESULT_NO_PGP = -1; - public static final int PARSE_RESULT_MESSAGE = 0; - public static final int PARSE_RESULT_SIGNED_MESSAGE = 1; - - public static int parseMessage(String message) { - Matcher matcherSigned = PGP_SIGNED_MESSAGE.matcher(message); - Matcher matcherMessage = PGP_MESSAGE.matcher(message); - - if (matcherMessage.matches()) { - return PARSE_RESULT_MESSAGE; - } else if (matcherSigned.matches()) { - return PARSE_RESULT_SIGNED_MESSAGE; - } else { - return PARSE_RESULT_NO_PGP; - } - } - - public static boolean isAvailable(Context context) { - Intent intent = new Intent(OpenPgpConstants.SERVICE_INTENT); - List resInfo = context.getPackageManager().queryIntentServices(intent, 0); - if (!resInfo.isEmpty()) { - return true; - } else { - return false; - } - } - -} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java deleted file mode 100644 index 75d4b8c18..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * 2013 Flow (http://stackoverflow.com/questions/18212152/transfer-inputstream-to-another-service-across-process-boundaries-with-parcelf) - * - * 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.util; - -import android.os.ParcelFileDescriptor; -import android.util.Log; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -public class ParcelFileDescriptorUtil { - - public interface IThreadListener { - void onThreadFinished(final Thread thread); - } - - public static ParcelFileDescriptor pipeFrom(InputStream inputStream, IThreadListener listener) - throws IOException { - ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe(); - ParcelFileDescriptor readSide = pipe[0]; - ParcelFileDescriptor writeSide = pipe[1]; - - // start the transfer thread - new TransferThread(inputStream, new ParcelFileDescriptor.AutoCloseOutputStream(writeSide), - listener) - .start(); - - return readSide; - } - - public static ParcelFileDescriptor pipeTo(OutputStream outputStream, IThreadListener listener) - throws IOException { - ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe(); - ParcelFileDescriptor readSide = pipe[0]; - ParcelFileDescriptor writeSide = pipe[1]; - - // start the transfer thread - new TransferThread(new ParcelFileDescriptor.AutoCloseInputStream(readSide), outputStream, - listener) - .start(); - - return writeSide; - } - - static class TransferThread extends Thread { - final InputStream mIn; - final OutputStream mOut; - final IThreadListener mListener; - - TransferThread(InputStream in, OutputStream out, IThreadListener listener) { - super("ParcelFileDescriptor Transfer Thread"); - mIn = in; - mOut = out; - mListener = listener; - setDaemon(true); - } - - @Override - public void run() { - byte[] buf = new byte[1024]; - int len; - - try { - while ((len = mIn.read(buf)) > 0) { - mOut.write(buf, 0, len); - } - mOut.flush(); // just to be safe - } catch (IOException e) { - Log.e(OpenPgpConstants.TAG, "TransferThread" + getId() + ": writing failed", e); - } finally { - try { - mIn.close(); - } catch (IOException e) { - Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e); - } - try { - mOut.close(); - } catch (IOException e) { - Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e); - } - } - if (mListener != null) { - Log.d(OpenPgpConstants.TAG, "TransferThread " + getId() + " finished!"); - mListener.onThreadFinished(this); - } - } - } -} \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/IOpenPgpService.aidl b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/IOpenPgpService.aidl new file mode 100644 index 000000000..578a7d4b5 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/IOpenPgpService.aidl @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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 { + + /** + * General extras + * -------------- + * + * Bundle params: + * int api_version (required) + * boolean ascii_armor (request ascii armor for ouput) + * + * returned Bundle: + * int result_code (0, 1, or 2 (see OpenPgpConstants)) + * OpenPgpError error (if result_code == 0) + * Intent intent (if result_code == 2) + * + */ + + /** + * Sign only + * + * optional params: + * String passphrase (for key passphrase) + */ + Bundle sign(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); + + /** + * Encrypt + * + * Bundle params: + * long[] key_ids + * or + * String[] user_ids (= emails of recipients) (if more than one key has this user_id, a PendingIntent is returned) + * + * optional params: + * String passphrase (for key passphrase) + */ + Bundle encrypt(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); + + /** + * Sign and encrypt + * + * Bundle params: + * same as in encrypt() + */ + Bundle signAndEncrypt(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); + + /** + * Decrypts and verifies given input bytes. This methods handles encrypted-only, signed-and-encrypted, + * and also signed-only input. + * + * returned Bundle: + * OpenPgpSignatureResult signature_result + */ + Bundle decryptAndVerify(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output); + + /** + * Retrieves key ids based on given user ids (=emails) + * + * Bundle params: + * String[] user_ids + * + * returned Bundle: + * long[] key_ids + */ + Bundle getKeyIds(in Bundle params); + +} \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/OpenPgpError.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/OpenPgpError.java new file mode 100644 index 000000000..4dd2cc641 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/OpenPgpError.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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; + +import android.os.Parcel; +import android.os.Parcelable; + +public class OpenPgpError implements Parcelable { + public static final int CLIENT_SIDE_ERROR = -1; + + public static final int GENERIC_ERROR = 0; + public static final int INCOMPATIBLE_API_VERSIONS = 1; + + public static final int NO_OR_WRONG_PASSPHRASE = 2; + public static final int NO_USER_IDS = 3; + + int errorId; + String message; + + public OpenPgpError() { + } + + public OpenPgpError(int errorId, String message) { + this.errorId = errorId; + this.message = message; + } + + public OpenPgpError(OpenPgpError b) { + this.errorId = b.errorId; + this.message = b.message; + } + + public int getErrorId() { + return errorId; + } + + public void setErrorId(int errorId) { + this.errorId = errorId; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(errorId); + dest.writeString(message); + } + + public static final Creator CREATOR = new Creator() { + public OpenPgpError createFromParcel(final Parcel source) { + OpenPgpError error = new OpenPgpError(); + error.errorId = source.readInt(); + error.message = source.readString(); + return error; + } + + public OpenPgpError[] newArray(final int size) { + return new OpenPgpError[size]; + } + }; +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/OpenPgpSignatureResult.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/OpenPgpSignatureResult.java new file mode 100644 index 000000000..16c79ca27 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/OpenPgpSignatureResult.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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; + +import android.os.Parcel; +import android.os.Parcelable; + +public class OpenPgpSignatureResult implements Parcelable { + // generic error on signature verification + public static final int SIGNATURE_ERROR = 0; + // successfully verified signature, with certified public key + public static final int SIGNATURE_SUCCESS_CERTIFIED = 1; + // no public key was found for this signature verification + // you can retrieve the key with + // getKeys(new String[] {String.valueOf(signatureResult.getKeyId)}, true, callback) + public static final int SIGNATURE_UNKNOWN_PUB_KEY = 2; + // successfully verified signature, but with certified public key + public static final int SIGNATURE_SUCCESS_UNCERTIFIED = 3; + + int status; + boolean signatureOnly; + String userId; + long keyId; + + public int getStatus() { + return status; + } + + public boolean isSignatureOnly() { + return signatureOnly; + } + + public String getUserId() { + return userId; + } + + public long getKeyId() { + return keyId; + } + + public OpenPgpSignatureResult() { + + } + + public OpenPgpSignatureResult(int signatureStatus, String signatureUserId, + boolean signatureOnly, long keyId) { + this.status = signatureStatus; + this.signatureOnly = signatureOnly; + this.userId = signatureUserId; + this.keyId = keyId; + } + + public OpenPgpSignatureResult(OpenPgpSignatureResult b) { + this.status = b.status; + this.userId = b.userId; + this.signatureOnly = b.signatureOnly; + this.keyId = b.keyId; + } + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(status); + dest.writeByte((byte) (signatureOnly ? 1 : 0)); + dest.writeString(userId); + dest.writeLong(keyId); + } + + public static final Creator CREATOR = new Creator() { + public OpenPgpSignatureResult createFromParcel(final Parcel source) { + OpenPgpSignatureResult vr = new OpenPgpSignatureResult(); + vr.status = source.readInt(); + vr.signatureOnly = source.readByte() == 1; + vr.userId = source.readString(); + vr.keyId = source.readLong(); + return vr; + } + + public OpenPgpSignatureResult[] newArray(final int size) { + return new OpenPgpSignatureResult[size]; + } + }; + + @Override + public String toString() { + String out = new String(); + out += "\nstatus: " + status; + out += "\nuserId: " + userId; + out += "\nsignatureOnly: " + signatureOnly; + out += "\nkeyId: " + keyId; + return out; + } + +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java new file mode 100644 index 000000000..f121c345d --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java @@ -0,0 +1,198 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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.util; + +import android.content.Context; +import android.os.AsyncTask; +import android.os.Bundle; +import android.os.ParcelFileDescriptor; +import android.util.Log; + +import org.openintents.openpgp.IOpenPgpService; +import org.openintents.openpgp.OpenPgpError; + +import java.io.InputStream; +import java.io.OutputStream; + +public class OpenPgpApi { + + IOpenPgpService mService; + Context mContext; + + private static final int OPERATION_SIGN = 0; + private static final int OPERATION_ENCRYPT = 1; + private static final int OPERATION_SIGN_ENCRYPT = 2; + private static final int OPERATION_DECRYPT_VERIFY = 3; + private static final int OPERATION_GET_KEY_IDS = 4; + + public OpenPgpApi(Context context, IOpenPgpService service) { + this.mContext = context; + this.mService = service; + } + + public Bundle sign(InputStream is, final OutputStream os) { + return executeApi(OPERATION_SIGN, new Bundle(), is, os); + } + + public Bundle sign(Bundle params, InputStream is, final OutputStream os) { + return executeApi(OPERATION_SIGN, params, is, os); + } + + public void sign(Bundle params, InputStream is, final OutputStream os, IOpenPgpCallback callback) { + executeApiAsync(OPERATION_SIGN, params, is, os, callback); + } + + public Bundle encrypt(InputStream is, final OutputStream os) { + return executeApi(OPERATION_ENCRYPT, new Bundle(), is, os); + } + + public Bundle encrypt(Bundle params, InputStream is, final OutputStream os) { + return executeApi(OPERATION_ENCRYPT, params, is, os); + } + + public void encrypt(Bundle params, InputStream is, final OutputStream os, IOpenPgpCallback callback) { + executeApiAsync(OPERATION_ENCRYPT, params, is, os, callback); + } + + public Bundle signAndEncrypt(InputStream is, final OutputStream os) { + return executeApi(OPERATION_SIGN_ENCRYPT, new Bundle(), is, os); + } + + public Bundle signAndEncrypt(Bundle params, InputStream is, final OutputStream os) { + return executeApi(OPERATION_SIGN_ENCRYPT, params, is, os); + } + + public void signAndEncrypt(Bundle params, InputStream is, final OutputStream os, IOpenPgpCallback callback) { + executeApiAsync(OPERATION_SIGN_ENCRYPT, params, is, os, callback); + } + + public Bundle decryptAndVerify(InputStream is, final OutputStream os) { + return executeApi(OPERATION_DECRYPT_VERIFY, new Bundle(), is, os); + } + + public Bundle decryptAndVerify(Bundle params, InputStream is, final OutputStream os) { + return executeApi(OPERATION_DECRYPT_VERIFY, params, is, os); + } + + public void decryptAndVerify(Bundle params, InputStream is, final OutputStream os, IOpenPgpCallback callback) { + executeApiAsync(OPERATION_DECRYPT_VERIFY, params, is, os, callback); + } + + public Bundle getKeyIds(Bundle params) { + return executeApi(OPERATION_GET_KEY_IDS, params, null, null); + } + + public interface IOpenPgpCallback { + void onReturn(final Bundle result); + } + + private class OpenPgpAsyncTask extends AsyncTask { + int operationId; + Bundle params; + InputStream is; + OutputStream os; + IOpenPgpCallback callback; + + private OpenPgpAsyncTask(int operationId, Bundle params, InputStream is, OutputStream os, IOpenPgpCallback callback) { + this.operationId = operationId; + this.params = params; + this.is = is; + this.os = os; + this.callback = callback; + } + + @Override + protected Bundle doInBackground(Void... unused) { + return executeApi(operationId, params, is, os); + } + + protected void onPostExecute(Bundle result) { + callback.onReturn(result); + } + + } + + private void executeApiAsync(int operationId, Bundle params, InputStream is, OutputStream os, IOpenPgpCallback callback) { + new OpenPgpAsyncTask(operationId, params, is, os, callback).execute((Void[]) null); + } + + private Bundle executeApi(int operationId, Bundle params, InputStream is, OutputStream os) { + try { + params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION); + + Bundle result = null; + + if (operationId == OPERATION_GET_KEY_IDS) { + result = mService.getKeyIds(params); + return result; + } else { + // send the input and output pfds + ParcelFileDescriptor input = ParcelFileDescriptorUtil.pipeFrom(is, + new ParcelFileDescriptorUtil.IThreadListener() { + + @Override + public void onThreadFinished(Thread thread) { + Log.d(OpenPgpConstants.TAG, "Copy to service finished"); + } + }); + ParcelFileDescriptor output = ParcelFileDescriptorUtil.pipeTo(os, + new ParcelFileDescriptorUtil.IThreadListener() { + + @Override + public void onThreadFinished(Thread thread) { + Log.d(OpenPgpConstants.TAG, "Service finished writing!"); + } + }); + + + // blocks until result is ready + switch (operationId) { + case OPERATION_SIGN: + result = mService.sign(params, input, output); + break; + case OPERATION_ENCRYPT: + result = mService.encrypt(params, input, output); + break; + case OPERATION_SIGN_ENCRYPT: + result = mService.signAndEncrypt(params, input, output); + break; + case OPERATION_DECRYPT_VERIFY: + result = mService.decryptAndVerify(params, input, output); + break; + } + // close() is required to halt the TransferThread + output.close(); + + // set class loader to current context to allow unparcelling + // of OpenPgpError and OpenPgpSignatureResult + // http://stackoverflow.com/a/3806769 + result.setClassLoader(mContext.getClassLoader()); + + return result; + } + } catch (Exception e) { + Log.e(OpenPgpConstants.TAG, "Exception", e); + Bundle result = new Bundle(); + result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR); + result.putParcelable(OpenPgpConstants.RESULT_ERRORS, + new OpenPgpError(OpenPgpError.CLIENT_SIDE_ERROR, e.getMessage())); + return result; + } + } + + +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpConstants.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpConstants.java new file mode 100644 index 000000000..263b42aaa --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpConstants.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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.util; + +public class OpenPgpConstants { + + public static final String TAG = "OpenPgp API"; + + public static final int API_VERSION = 1; + public static final String SERVICE_INTENT = "org.openintents.openpgp.IOpenPgpService"; + + + /* Bundle params */ + public static final String PARAMS_API_VERSION = "api_version"; + // request ASCII Armor for output + // OpenPGP Radix-64, 33 percent overhead compared to binary, see http://tools.ietf.org/html/rfc4880#page-53) + public static final String PARAMS_REQUEST_ASCII_ARMOR = "ascii_armor"; + // (for encrypt method) + public static final String PARAMS_USER_IDS = "user_ids"; + public static final String PARAMS_KEY_IDS = "key_ids"; + // optional parameter: + public static final String PARAMS_PASSPHRASE = "passphrase"; + + /* Service Bundle returns */ + public static final String RESULT_CODE = "result_code"; + public static final String RESULT_SIGNATURE = "signature"; + public static final String RESULT_ERRORS = "error"; + public static final String RESULT_INTENT = "intent"; + + // get actual error object from RESULT_ERRORS + public static final int RESULT_CODE_ERROR = 0; + // success! + public static final int RESULT_CODE_SUCCESS = 1; + // executeServiceMethod intent and do it again with params from intent + public static final int RESULT_CODE_USER_INTERACTION_REQUIRED = 2; + + /* PendingIntent returns */ + public static final String PI_RESULT_PARAMS = "params"; + +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java new file mode 100644 index 000000000..e29794e87 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java @@ -0,0 +1,180 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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.util; + +import android.app.AlertDialog.Builder; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.pm.ResolveInfo; +import android.graphics.drawable.Drawable; +import android.preference.DialogPreference; +import android.util.AttributeSet; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ListAdapter; +import android.widget.TextView; + +import java.util.ArrayList; +import java.util.List; + +public class OpenPgpListPreference extends DialogPreference { + ArrayList mProviderList = new ArrayList(); + private String mSelectedPackage; + + public OpenPgpListPreference(Context context, AttributeSet attrs) { + super(context, attrs); + + Intent intent = new Intent(OpenPgpConstants.SERVICE_INTENT); + List resInfo = context.getPackageManager().queryIntentServices(intent, 0); + if (!resInfo.isEmpty()) { + for (ResolveInfo resolveInfo : resInfo) { + if (resolveInfo.serviceInfo == null) + continue; + + String packageName = resolveInfo.serviceInfo.packageName; + String simpleName = String.valueOf(resolveInfo.serviceInfo.loadLabel(context + .getPackageManager())); + Drawable icon = resolveInfo.serviceInfo.loadIcon(context.getPackageManager()); + + mProviderList.add(new OpenPgpProviderEntry(packageName, simpleName, icon)); + } + } + } + + public OpenPgpListPreference(Context context) { + this(context, null); + } + + /** + * Can be used to add "no selection" + * + * @param packageName + * @param simpleName + * @param icon + */ + public void addProvider(int position, String packageName, String simpleName, Drawable icon) { + mProviderList.add(position, new OpenPgpProviderEntry(packageName, simpleName, icon)); + } + + @Override + protected void onPrepareDialogBuilder(Builder builder) { + // Init ArrayAdapter with OpenPGP Providers + ListAdapter adapter = new ArrayAdapter(getContext(), + android.R.layout.select_dialog_singlechoice, android.R.id.text1, mProviderList) { + public View getView(int position, View convertView, ViewGroup parent) { + // User super class to create the View + View v = super.getView(position, convertView, parent); + TextView tv = (TextView) v.findViewById(android.R.id.text1); + + // Put the image on the TextView + tv.setCompoundDrawablesWithIntrinsicBounds(mProviderList.get(position).icon, null, + null, null); + + // Add margin between image and text (support various screen densities) + int dp10 = (int) (10 * getContext().getResources().getDisplayMetrics().density + 0.5f); + tv.setCompoundDrawablePadding(dp10); + + return v; + } + }; + + builder.setSingleChoiceItems(adapter, getIndexOfProviderList(getValue()), + new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + mSelectedPackage = mProviderList.get(which).packageName; + + /* + * Clicking on an item simulates the positive button click, and dismisses + * the dialog. + */ + OpenPgpListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE); + dialog.dismiss(); + } + }); + + /* + * The typical interaction for list-based dialogs is to have click-on-an-item dismiss the + * dialog instead of the user having to press 'Ok'. + */ + builder.setPositiveButton(null, null); + } + + @Override + protected void onDialogClosed(boolean positiveResult) { + super.onDialogClosed(positiveResult); + + if (positiveResult && (mSelectedPackage != null)) { + if (callChangeListener(mSelectedPackage)) { + setValue(mSelectedPackage); + } + } + } + + private int getIndexOfProviderList(String packageName) { + for (OpenPgpProviderEntry app : mProviderList) { + if (app.packageName.equals(packageName)) { + return mProviderList.indexOf(app); + } + } + + return -1; + } + + public void setValue(String packageName) { + mSelectedPackage = packageName; + persistString(packageName); + } + + public String getValue() { + return mSelectedPackage; + } + + public String getEntry() { + return getEntryByValue(mSelectedPackage); + } + + public String getEntryByValue(String packageName) { + for (OpenPgpProviderEntry app : mProviderList) { + if (app.packageName.equals(packageName)) { + return app.simpleName; + } + } + + return null; + } + + private static class OpenPgpProviderEntry { + private String packageName; + private String simpleName; + private Drawable icon; + + public OpenPgpProviderEntry(String packageName, String simpleName, Drawable icon) { + this.packageName = packageName; + this.simpleName = simpleName; + this.icon = icon; + } + + @Override + public String toString() { + return simpleName; + } + } +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpServiceConnection.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpServiceConnection.java new file mode 100644 index 000000000..8e8812faa --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpServiceConnection.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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.util; + +import org.openintents.openpgp.IOpenPgpService; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.IBinder; +import android.util.Log; + +public class OpenPgpServiceConnection { + private Context mApplicationContext; + + private IOpenPgpService mService; + private boolean mBound; + private String mCryptoProviderPackageName; + + public OpenPgpServiceConnection(Context context, String cryptoProviderPackageName) { + this.mApplicationContext = context.getApplicationContext(); + this.mCryptoProviderPackageName = cryptoProviderPackageName; + } + + public IOpenPgpService getService() { + return mService; + } + + public boolean isBound() { + return mBound; + } + + private ServiceConnection mCryptoServiceConnection = new ServiceConnection() { + public void onServiceConnected(ComponentName name, IBinder service) { + mService = IOpenPgpService.Stub.asInterface(service); + Log.d(OpenPgpConstants.TAG, "connected to service"); + mBound = true; + } + + public void onServiceDisconnected(ComponentName name) { + mService = null; + Log.d(OpenPgpConstants.TAG, "disconnected from service"); + mBound = false; + } + }; + + /** + * If not already bound, bind! + * + * @return + */ + public boolean bindToService() { + // if not already connected + if (mService == null && !mBound) { + try { + Log.d(OpenPgpConstants.TAG, "not bound yet"); + + Intent serviceIntent = new Intent(); + serviceIntent.setAction(IOpenPgpService.class.getName()); + serviceIntent.setPackage(mCryptoProviderPackageName); + mApplicationContext.bindService(serviceIntent, mCryptoServiceConnection, + Context.BIND_AUTO_CREATE); + + return true; + } catch (Exception e) { + Log.d(OpenPgpConstants.TAG, "Exception on binding", e); + return false; + } + } else { + Log.d(OpenPgpConstants.TAG, "already bound"); + return true; + } + } + + public void unbindFromService() { + mApplicationContext.unbindService(mCryptoServiceConnection); + } + +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpUtils.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpUtils.java new file mode 100644 index 000000000..ffecaceba --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpUtils.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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.util; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ResolveInfo; + +public class OpenPgpUtils { + + public static final Pattern PGP_MESSAGE = Pattern.compile( + ".*?(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*", + Pattern.DOTALL); + + public static final Pattern PGP_SIGNED_MESSAGE = Pattern.compile( + ".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*", + Pattern.DOTALL); + + public static final int PARSE_RESULT_NO_PGP = -1; + public static final int PARSE_RESULT_MESSAGE = 0; + public static final int PARSE_RESULT_SIGNED_MESSAGE = 1; + + public static int parseMessage(String message) { + Matcher matcherSigned = PGP_SIGNED_MESSAGE.matcher(message); + Matcher matcherMessage = PGP_MESSAGE.matcher(message); + + if (matcherMessage.matches()) { + return PARSE_RESULT_MESSAGE; + } else if (matcherSigned.matches()) { + return PARSE_RESULT_SIGNED_MESSAGE; + } else { + return PARSE_RESULT_NO_PGP; + } + } + + public static boolean isAvailable(Context context) { + Intent intent = new Intent(OpenPgpConstants.SERVICE_INTENT); + List resInfo = context.getPackageManager().queryIntentServices(intent, 0); + if (!resInfo.isEmpty()) { + return true; + } else { + return false; + } + } + +} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java new file mode 100644 index 000000000..75d4b8c18 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * 2013 Flow (http://stackoverflow.com/questions/18212152/transfer-inputstream-to-another-service-across-process-boundaries-with-parcelf) + * + * 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.util; + +import android.os.ParcelFileDescriptor; +import android.util.Log; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class ParcelFileDescriptorUtil { + + public interface IThreadListener { + void onThreadFinished(final Thread thread); + } + + public static ParcelFileDescriptor pipeFrom(InputStream inputStream, IThreadListener listener) + throws IOException { + ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe(); + ParcelFileDescriptor readSide = pipe[0]; + ParcelFileDescriptor writeSide = pipe[1]; + + // start the transfer thread + new TransferThread(inputStream, new ParcelFileDescriptor.AutoCloseOutputStream(writeSide), + listener) + .start(); + + return readSide; + } + + public static ParcelFileDescriptor pipeTo(OutputStream outputStream, IThreadListener listener) + throws IOException { + ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe(); + ParcelFileDescriptor readSide = pipe[0]; + ParcelFileDescriptor writeSide = pipe[1]; + + // start the transfer thread + new TransferThread(new ParcelFileDescriptor.AutoCloseInputStream(readSide), outputStream, + listener) + .start(); + + return writeSide; + } + + static class TransferThread extends Thread { + final InputStream mIn; + final OutputStream mOut; + final IThreadListener mListener; + + TransferThread(InputStream in, OutputStream out, IThreadListener listener) { + super("ParcelFileDescriptor Transfer Thread"); + mIn = in; + mOut = out; + mListener = listener; + setDaemon(true); + } + + @Override + public void run() { + byte[] buf = new byte[1024]; + int len; + + try { + while ((len = mIn.read(buf)) > 0) { + mOut.write(buf, 0, len); + } + mOut.flush(); // just to be safe + } catch (IOException e) { + Log.e(OpenPgpConstants.TAG, "TransferThread" + getId() + ": writing failed", e); + } finally { + try { + mIn.close(); + } catch (IOException e) { + Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e); + } + try { + mOut.close(); + } catch (IOException e) { + Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e); + } + } + if (mListener != null) { + Log.d(OpenPgpConstants.TAG, "TransferThread " + getId() + " finished!"); + mListener.onThreadFinished(this); + } + } + } +} \ No newline at end of file -- cgit v1.2.3 From 9f7f4fd27a02c02b15df613710433dfc35ad8e17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 15 Feb 2014 18:04:30 +0100 Subject: Add None-entry to ListPreference --- .../src/org/openintents/openpgp/util/OpenPgpListPreference.java | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java index e29794e87..4917b6993 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java @@ -33,6 +33,8 @@ import android.widget.TextView; import java.util.ArrayList; import java.util.List; +import org.sufficientlysecure.keychain.api.R; + public class OpenPgpListPreference extends DialogPreference { ArrayList mProviderList = new ArrayList(); private String mSelectedPackage; @@ -55,6 +57,10 @@ public class OpenPgpListPreference extends DialogPreference { mProviderList.add(new OpenPgpProviderEntry(packageName, simpleName, icon)); } } + + // add "none" + mProviderList.add(0, new OpenPgpProviderEntry("", context.getString(R.string.openpgp_list_preference_none), + context.getResources().getDrawable(R.drawable.ic_action_cancel_launchersize))); } public OpenPgpListPreference(Context context) { -- cgit v1.2.3 From a659e97a645a7874b1712d9ad4e1a7493629ed9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 15 Feb 2014 18:11:14 +0100 Subject: list preference: load provider list on open of dialog not on create of preference to reload when keychain has been installed --- .../openpgp/util/OpenPgpListPreference.java | 48 ++++++++++++---------- 1 file changed, 26 insertions(+), 22 deletions(-) (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java index 4917b6993..987c8c8fc 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java @@ -36,31 +36,11 @@ import java.util.List; import org.sufficientlysecure.keychain.api.R; public class OpenPgpListPreference extends DialogPreference { - ArrayList mProviderList = new ArrayList(); + private ArrayList mProviderList = new ArrayList(); private String mSelectedPackage; public OpenPgpListPreference(Context context, AttributeSet attrs) { super(context, attrs); - - Intent intent = new Intent(OpenPgpConstants.SERVICE_INTENT); - List resInfo = context.getPackageManager().queryIntentServices(intent, 0); - if (!resInfo.isEmpty()) { - for (ResolveInfo resolveInfo : resInfo) { - if (resolveInfo.serviceInfo == null) - continue; - - String packageName = resolveInfo.serviceInfo.packageName; - String simpleName = String.valueOf(resolveInfo.serviceInfo.loadLabel(context - .getPackageManager())); - Drawable icon = resolveInfo.serviceInfo.loadIcon(context.getPackageManager()); - - mProviderList.add(new OpenPgpProviderEntry(packageName, simpleName, icon)); - } - } - - // add "none" - mProviderList.add(0, new OpenPgpProviderEntry("", context.getString(R.string.openpgp_list_preference_none), - context.getResources().getDrawable(R.drawable.ic_action_cancel_launchersize))); } public OpenPgpListPreference(Context context) { @@ -68,7 +48,7 @@ public class OpenPgpListPreference extends DialogPreference { } /** - * Can be used to add "no selection" + * Public method to add new entries for legacy applications * * @param packageName * @param simpleName @@ -80,6 +60,30 @@ public class OpenPgpListPreference extends DialogPreference { @Override protected void onPrepareDialogBuilder(Builder builder) { + + // get providers + mProviderList.clear(); + Intent intent = new Intent(OpenPgpConstants.SERVICE_INTENT); + List resInfo = getContext().getPackageManager().queryIntentServices(intent, 0); + if (!resInfo.isEmpty()) { + for (ResolveInfo resolveInfo : resInfo) { + if (resolveInfo.serviceInfo == null) + continue; + + String packageName = resolveInfo.serviceInfo.packageName; + String simpleName = String.valueOf(resolveInfo.serviceInfo.loadLabel(getContext() + .getPackageManager())); + Drawable icon = resolveInfo.serviceInfo.loadIcon(getContext().getPackageManager()); + + mProviderList.add(new OpenPgpProviderEntry(packageName, simpleName, icon)); + } + } + + // add "none"-entry + mProviderList.add(0, new OpenPgpProviderEntry("", + getContext().getString(R.string.openpgp_list_preference_none), + getContext().getResources().getDrawable(R.drawable.ic_action_cancel_launchersize))); + // Init ArrayAdapter with OpenPGP Providers ListAdapter adapter = new ArrayAdapter(getContext(), android.R.layout.select_dialog_singlechoice, android.R.id.text1, mProviderList) { -- cgit v1.2.3 From 884aaf7e7ac24a54f54c4e714121869346b7a623 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 15 Feb 2014 18:44:13 +0100 Subject: save selected provider in list preference --- .../org/openintents/openpgp/util/OpenPgpListPreference.java | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java index 987c8c8fc..f1f326fca 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java @@ -21,6 +21,7 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.ResolveInfo; +import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.preference.DialogPreference; import android.util.AttributeSet; @@ -161,6 +162,16 @@ public class OpenPgpListPreference extends DialogPreference { return getEntryByValue(mSelectedPackage); } + @Override + protected Object onGetDefaultValue(TypedArray a, int index) { + return a.getString(index); + } + + @Override + protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { + setValue(restoreValue ? getPersistedString(mSelectedPackage) : (String) defaultValue); + } + public String getEntryByValue(String packageName) { for (OpenPgpProviderEntry app : mProviderList) { if (app.packageName.equals(packageName)) { -- cgit v1.2.3 From ea361f4bf50fca2dea96e83a186fff7bb01bccd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 15 Feb 2014 18:51:58 +0100 Subject: select provider with preference --- .../src/org/openintents/openpgp/util/OpenPgpListPreference.java | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java index f1f326fca..034186a3a 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java @@ -36,6 +36,10 @@ import java.util.List; import org.sufficientlysecure.keychain.api.R; +/** + * Does not extend ListPreference, but is very similar to it! + * http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/preference/ListPreference.java/?v=source + */ public class OpenPgpListPreference extends DialogPreference { private ArrayList mProviderList = new ArrayList(); private String mSelectedPackage; -- cgit v1.2.3 From 81b810e9f6cee2ac549758563fcd8987806828a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 15 Feb 2014 20:14:23 +0100 Subject: intent demo, fix some intents --- .../openintents/openpgp/util/OpenPgpConstants.java | 1 - .../keychain/api/KeychainIntents.java | 37 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/sufficientlysecure/keychain/api/KeychainIntents.java (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpConstants.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpConstants.java index 263b42aaa..9a2d3c054 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpConstants.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpConstants.java @@ -23,7 +23,6 @@ public class OpenPgpConstants { public static final int API_VERSION = 1; public static final String SERVICE_INTENT = "org.openintents.openpgp.IOpenPgpService"; - /* Bundle params */ public static final String PARAMS_API_VERSION = "api_version"; // request ASCII Armor for output diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/sufficientlysecure/keychain/api/KeychainIntents.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/sufficientlysecure/keychain/api/KeychainIntents.java new file mode 100644 index 000000000..12e32933b --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/sufficientlysecure/keychain/api/KeychainIntents.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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.sufficientlysecure.keychain.api; + +public class KeychainIntents { + + public static final String ENCRYPT = "org.sufficientlysecure.keychain.action.ENCRYPT"; + public static final String ENCRYPT_EXTRA_TEXT = "text"; + public static final String ENCRYPT_ASCII_ARMOR = "ascii_armor"; + + public static final String DECRYPT = "org.sufficientlysecure.keychain.action.DECRYPT"; + public static final String DECRYPT_EXTRA_TEXT = "text"; + + public static final String IMPORT_KEY = "org.sufficientlysecure.keychain.action.IMPORT_KEY"; + public static final String IMPORT_KEY_EXTRA_KEY_BYTES = "key_bytes"; + + public static final String IMPORT_KEY_FROM_KEYSERVER = "org.sufficientlysecure.keychain.action.IMPORT_KEY_FROM_KEYSERVER"; + public static final String IMPORT_KEY_FROM_KEYSERVER_QUERY = "query"; + public static final String IMPORT_KEY_FROM_KEYSERVER_FINGERPRINT = "fingerprint"; + + public static final String IMPORT_KEY_FROM_QR_CODE = "org.sufficientlysecure.keychain.action.IMPORT_KEY_FROM_QR_CODE"; + +} -- cgit v1.2.3 From 154849b5911cc2b99aa0f5c42eea53aaf55ba930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sun, 16 Feb 2014 01:35:14 +0100 Subject: rename intent helper --- .../keychain/api/KeychainIntents.java | 37 ---------------------- .../keychain/api/OpenKeychainIntents.java | 37 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 37 deletions(-) delete mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/sufficientlysecure/keychain/api/KeychainIntents.java create mode 100644 OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/sufficientlysecure/keychain/api/OpenKeychainIntents.java (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/sufficientlysecure/keychain/api/KeychainIntents.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/sufficientlysecure/keychain/api/KeychainIntents.java deleted file mode 100644 index 12e32933b..000000000 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/sufficientlysecure/keychain/api/KeychainIntents.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * - * 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.sufficientlysecure.keychain.api; - -public class KeychainIntents { - - public static final String ENCRYPT = "org.sufficientlysecure.keychain.action.ENCRYPT"; - public static final String ENCRYPT_EXTRA_TEXT = "text"; - public static final String ENCRYPT_ASCII_ARMOR = "ascii_armor"; - - public static final String DECRYPT = "org.sufficientlysecure.keychain.action.DECRYPT"; - public static final String DECRYPT_EXTRA_TEXT = "text"; - - public static final String IMPORT_KEY = "org.sufficientlysecure.keychain.action.IMPORT_KEY"; - public static final String IMPORT_KEY_EXTRA_KEY_BYTES = "key_bytes"; - - public static final String IMPORT_KEY_FROM_KEYSERVER = "org.sufficientlysecure.keychain.action.IMPORT_KEY_FROM_KEYSERVER"; - public static final String IMPORT_KEY_FROM_KEYSERVER_QUERY = "query"; - public static final String IMPORT_KEY_FROM_KEYSERVER_FINGERPRINT = "fingerprint"; - - public static final String IMPORT_KEY_FROM_QR_CODE = "org.sufficientlysecure.keychain.action.IMPORT_KEY_FROM_QR_CODE"; - -} diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/sufficientlysecure/keychain/api/OpenKeychainIntents.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/sufficientlysecure/keychain/api/OpenKeychainIntents.java new file mode 100644 index 000000000..15aceb534 --- /dev/null +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/sufficientlysecure/keychain/api/OpenKeychainIntents.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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.sufficientlysecure.keychain.api; + +public class OpenKeychainIntents { + + public static final String ENCRYPT = "org.sufficientlysecure.keychain.action.ENCRYPT"; + public static final String ENCRYPT_EXTRA_TEXT = "text"; // String + public static final String ENCRYPT_ASCII_ARMOR = "ascii_armor"; // boolean + + public static final String DECRYPT = "org.sufficientlysecure.keychain.action.DECRYPT"; + public static final String DECRYPT_EXTRA_TEXT = "text"; // String + + public static final String IMPORT_KEY = "org.sufficientlysecure.keychain.action.IMPORT_KEY"; + public static final String IMPORT_KEY_EXTRA_KEY_BYTES = "key_bytes"; // byte[] + + public static final String IMPORT_KEY_FROM_KEYSERVER = "org.sufficientlysecure.keychain.action.IMPORT_KEY_FROM_KEYSERVER"; + public static final String IMPORT_KEY_FROM_KEYSERVER_QUERY = "query"; // String + public static final String IMPORT_KEY_FROM_KEYSERVER_FINGERPRINT = "fingerprint"; // String + + public static final String IMPORT_KEY_FROM_QR_CODE = "org.sufficientlysecure.keychain.action.IMPORT_KEY_FROM_QR_CODE"; + +} -- cgit v1.2.3 From 5d8867260946937498cf9e8d64fea3a9f53d9dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Fri, 21 Feb 2014 15:56:58 +0100 Subject: do not serialize async tasks --- .../src/org/openintents/openpgp/util/OpenPgpApi.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java index f121c345d..41cbfec59 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java @@ -18,6 +18,7 @@ package org.openintents.openpgp.util; import android.content.Context; import android.os.AsyncTask; +import android.os.Build; import android.os.Bundle; import android.os.ParcelFileDescriptor; import android.util.Log; @@ -127,7 +128,15 @@ public class OpenPgpApi { } private void executeApiAsync(int operationId, Bundle params, InputStream is, OutputStream os, IOpenPgpCallback callback) { - new OpenPgpAsyncTask(operationId, params, is, os, callback).execute((Void[]) null); + OpenPgpAsyncTask task = new OpenPgpAsyncTask(operationId, params, is, os, callback); + + // don't serialize async tasks! + // http://commonsware.com/blog/2012/04/20/asynctask-threading-regression-confirmed.html + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null); + } else { + task.execute((Void[]) null); + } } private Bundle executeApi(int operationId, Bundle params, InputStream is, OutputStream os) { -- cgit v1.2.3 From 1ed532b6e2cfe10725aa3ad7fdfb8a6da8c9a235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Fri, 21 Feb 2014 16:04:05 +0100 Subject: remove logging from library --- .../org/openintents/openpgp/util/OpenPgpApi.java | 11 +-------- .../openintents/openpgp/util/OpenPgpConstants.java | 1 + .../openpgp/util/OpenPgpServiceConnection.java | 28 +++++++++------------- .../openpgp/util/ParcelFileDescriptorUtil.java | 9 ++++--- 4 files changed, 17 insertions(+), 32 deletions(-) (limited to 'OpenPGP-Keychain-API/libraries/keychain-api-library/src') diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java index 41cbfec59..f121c345d 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java @@ -18,7 +18,6 @@ package org.openintents.openpgp.util; import android.content.Context; import android.os.AsyncTask; -import android.os.Build; import android.os.Bundle; import android.os.ParcelFileDescriptor; import android.util.Log; @@ -128,15 +127,7 @@ public class OpenPgpApi { } private void executeApiAsync(int operationId, Bundle params, InputStream is, OutputStream os, IOpenPgpCallback callback) { - OpenPgpAsyncTask task = new OpenPgpAsyncTask(operationId, params, is, os, callback); - - // don't serialize async tasks! - // http://commonsware.com/blog/2012/04/20/asynctask-threading-regression-confirmed.html - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { - task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null); - } else { - task.execute((Void[]) null); - } + new OpenPgpAsyncTask(operationId, params, is, os, callback).execute((Void[]) null); } private Bundle executeApi(int operationId, Bundle params, InputStream is, OutputStream os) { diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpConstants.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpConstants.java index 9a2d3c054..263b42aaa 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpConstants.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpConstants.java @@ -23,6 +23,7 @@ public class OpenPgpConstants { public static final int API_VERSION = 1; public static final String SERVICE_INTENT = "org.openintents.openpgp.IOpenPgpService"; + /* Bundle params */ public static final String PARAMS_API_VERSION = "api_version"; // request ASCII Armor for output diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpServiceConnection.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpServiceConnection.java index 8e8812faa..c80656c52 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpServiceConnection.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpServiceConnection.java @@ -23,18 +23,17 @@ import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; -import android.util.Log; public class OpenPgpServiceConnection { private Context mApplicationContext; - private IOpenPgpService mService; private boolean mBound; - private String mCryptoProviderPackageName; + private IOpenPgpService mService; + private String mProviderPackageName; - public OpenPgpServiceConnection(Context context, String cryptoProviderPackageName) { + public OpenPgpServiceConnection(Context context, String providerPackageName) { this.mApplicationContext = context.getApplicationContext(); - this.mCryptoProviderPackageName = cryptoProviderPackageName; + this.mProviderPackageName = providerPackageName; } public IOpenPgpService getService() { @@ -45,50 +44,45 @@ public class OpenPgpServiceConnection { return mBound; } - private ServiceConnection mCryptoServiceConnection = new ServiceConnection() { + private ServiceConnection mServiceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName name, IBinder service) { mService = IOpenPgpService.Stub.asInterface(service); - Log.d(OpenPgpConstants.TAG, "connected to service"); mBound = true; } public void onServiceDisconnected(ComponentName name) { mService = null; - Log.d(OpenPgpConstants.TAG, "disconnected from service"); mBound = false; } }; /** - * If not already bound, bind! + * If not already bound, bind to service! * * @return */ public boolean bindToService() { - // if not already connected + // if not already bound... if (mService == null && !mBound) { try { - Log.d(OpenPgpConstants.TAG, "not bound yet"); - Intent serviceIntent = new Intent(); serviceIntent.setAction(IOpenPgpService.class.getName()); - serviceIntent.setPackage(mCryptoProviderPackageName); - mApplicationContext.bindService(serviceIntent, mCryptoServiceConnection, + // NOTE: setPackage is very important to restrict the intent to this provider only! + serviceIntent.setPackage(mProviderPackageName); + mApplicationContext.bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE); return true; } catch (Exception e) { - Log.d(OpenPgpConstants.TAG, "Exception on binding", e); return false; } } else { - Log.d(OpenPgpConstants.TAG, "already bound"); return true; } } public void unbindFromService() { - mApplicationContext.unbindService(mCryptoServiceConnection); + mApplicationContext.unbindService(mServiceConnection); } } diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java index 75d4b8c18..3569caf5b 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java @@ -18,7 +18,6 @@ package org.openintents.openpgp.util; import android.os.ParcelFileDescriptor; -import android.util.Log; import java.io.IOException; import java.io.InputStream; @@ -82,21 +81,21 @@ public class ParcelFileDescriptorUtil { } mOut.flush(); // just to be safe } catch (IOException e) { - Log.e(OpenPgpConstants.TAG, "TransferThread" + getId() + ": writing failed", e); + //Log.e(OpenPgpConstants.TAG, "TransferThread" + getId() + ": writing failed", e); } finally { try { mIn.close(); } catch (IOException e) { - Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e); + //Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e); } try { mOut.close(); } catch (IOException e) { - Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e); + //Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e); } } if (mListener != null) { - Log.d(OpenPgpConstants.TAG, "TransferThread " + getId() + " finished!"); + //Log.d(OpenPgpConstants.TAG, "TransferThread " + getId() + " finished!"); mListener.onThreadFinished(this); } } -- cgit v1.2.3