open-keychain/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/remote/OpenPgpService.java

490 lines
21 KiB
Java
Raw Normal View History

2013-05-28 09:10:36 -04:00
/*
2014-02-14 11:01:17 -05:00
* Copyright (C) 2013-2014 Dominik Schürmann <dominik@dominikschuermann.de>
2013-05-28 09:10:36 -04:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
2013-05-28 09:10:36 -04:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
2013-05-28 09:10:36 -04:00
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2013-05-28 09:10:36 -04:00
*/
2013-09-15 07:52:05 -04:00
package org.sufficientlysecure.keychain.service.remote;
2013-05-28 09:10:36 -04:00
2014-02-14 11:01:17 -05:00
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
2013-05-29 09:58:50 -04:00
2013-09-10 17:19:34 -04:00
import org.openintents.openpgp.IOpenPgpService;
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.OpenPgpSignatureResult;
import org.openintents.openpgp.util.OpenPgpConstants;
2013-10-05 12:35:16 -04:00
import org.spongycastle.util.Arrays;
2013-05-28 09:10:36 -04:00
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.Id;
2013-09-08 12:21:04 -04:00
import org.sufficientlysecure.keychain.helper.Preferences;
import org.sufficientlysecure.keychain.pgp.PgpOperation;
2013-09-06 07:48:27 -04:00
import org.sufficientlysecure.keychain.provider.KeychainContract;
2013-05-28 09:10:36 -04:00
import org.sufficientlysecure.keychain.service.KeychainIntentService;
import org.sufficientlysecure.keychain.service.PassphraseCacheService;
2013-09-08 10:08:36 -04:00
import org.sufficientlysecure.keychain.util.InputData;
import org.sufficientlysecure.keychain.util.Log;
2013-05-28 09:10:36 -04:00
2014-02-14 11:01:17 -05:00
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
2013-05-28 09:10:36 -04:00
2013-09-15 09:20:15 -04:00
public class OpenPgpService extends RemoteService {
2013-09-06 12:54:55 -04:00
/**
* Search database for key ids based on emails.
*
2013-09-06 12:54:55 -04:00
* @param encryptionUserIds
* @return
*/
2014-02-14 11:01:17 -05:00
private Bundle getKeyIdsFromEmails(String[] encryptionUserIds) {
2013-09-06 12:54:55 -04:00
// find key ids to given emails in database
ArrayList<Long> keyIds = new ArrayList<Long>();
boolean missingUserIdsCheck = false;
boolean dublicateUserIdsCheck = false;
ArrayList<String> missingUserIds = new ArrayList<String>();
ArrayList<String> dublicateUserIds = new ArrayList<String>();
2013-09-06 12:54:55 -04:00
for (String email : encryptionUserIds) {
Uri uri = KeychainContract.KeyRings.buildPublicKeyRingsByEmailsUri(email);
Cursor cur = getContentResolver().query(uri, null, null, null, null);
if (cur.moveToFirst()) {
long id = cur.getLong(cur.getColumnIndex(KeychainContract.KeyRings.MASTER_KEY_ID));
keyIds.add(id);
} else {
missingUserIdsCheck = true;
missingUserIds.add(email);
2013-09-06 12:54:55 -04:00
Log.d(Constants.TAG, "user id missing");
}
if (cur.moveToNext()) {
dublicateUserIdsCheck = true;
dublicateUserIds.add(email);
2013-09-06 12:54:55 -04:00
Log.d(Constants.TAG, "more than one user id with the same email");
}
}
// convert to long[]
long[] keyIdsArray = new long[keyIds.size()];
for (int i = 0; i < keyIdsArray.length; i++) {
keyIdsArray[i] = keyIds.get(i);
}
2013-09-16 07:00:47 -04:00
// allow the user to verify pub key selection
2014-02-14 11:01:17 -05:00
if (missingUserIdsCheck || dublicateUserIdsCheck) {
// build PendingIntent for passphrase input
Intent intent = new Intent(getBaseContext(), RemoteServiceActivity.class);
intent.setAction(RemoteServiceActivity.ACTION_SELECT_PUB_KEYS);
intent.putExtra(RemoteServiceActivity.EXTRA_SELECTED_MASTER_KEY_IDS, keyIdsArray);
intent.putExtra(RemoteServiceActivity.EXTRA_MISSING_USER_IDS, missingUserIds);
intent.putExtra(RemoteServiceActivity.EXTRA_DUBLICATE_USER_IDS, dublicateUserIds);
2014-02-14 11:01:17 -05:00
PendingIntent pi = PendingIntent.getActivity(getBaseContext(), 42, intent, 0);
2014-02-14 11:01:17 -05:00
// return PendingIntent to be executed by client
Bundle result = new Bundle();
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED);
result.putParcelable(OpenPgpConstants.RESULT_INTENT, pi);
2014-02-14 11:01:17 -05:00
return result;
2013-09-16 07:00:47 -04:00
}
if (keyIdsArray.length == 0) {
return null;
}
2013-09-06 12:54:55 -04:00
2014-02-14 11:01:17 -05:00
Bundle result = new Bundle();
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_SUCCESS);
result.putLongArray(OpenPgpConstants.PARAMS_KEY_IDS, keyIdsArray);
return result;
}
2014-02-14 11:01:17 -05:00
private Bundle getPassphraseBundleIntent(long keyId) {
// build PendingIntent for passphrase input
Intent intent = new Intent(getBaseContext(), RemoteServiceActivity.class);
intent.setAction(RemoteServiceActivity.ACTION_CACHE_PASSPHRASE);
intent.putExtra(RemoteServiceActivity.EXTRA_SECRET_KEY_ID, keyId);
PendingIntent pi = PendingIntent.getActivity(getBaseContext(), 42, intent, 0);
2014-02-14 11:01:17 -05:00
// return PendingIntent to be executed by client
Bundle result = new Bundle();
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED);
result.putParcelable(OpenPgpConstants.RESULT_INTENT, pi);
2014-02-14 11:01:17 -05:00
return result;
}
2014-02-14 11:01:17 -05:00
private Bundle encryptAndSignImpl(Bundle params, ParcelFileDescriptor input,
ParcelFileDescriptor output, AppSettings appSettings,
boolean sign) {
2013-06-17 13:51:41 -04:00
try {
2014-02-14 11:01:17 -05:00
boolean asciiArmor = params.getBoolean(OpenPgpConstants.PARAMS_REQUEST_ASCII_ARMOR, false);
2013-10-05 12:35:16 -04:00
2014-02-14 11:01:17 -05:00
long[] keyIds;
if (params.containsKey(OpenPgpConstants.PARAMS_KEY_IDS)) {
keyIds = params.getLongArray(OpenPgpConstants.PARAMS_KEY_IDS);
} else {
// get key ids based on given user ids
String[] userIds = params.getStringArray(OpenPgpConstants.PARAMS_USER_IDS);
Bundle result = getKeyIdsFromEmails(userIds);
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED);
2013-10-05 12:35:16 -04:00
2014-02-14 11:01:17 -05:00
if (result.getInt(OpenPgpConstants.RESULT_CODE, 0) == OpenPgpConstants.RESULT_CODE_SUCCESS) {
keyIds = result.getLongArray(OpenPgpConstants.PARAMS_KEY_IDS);
} else {
// non-unique result, we need user interaction!
return result;
}
2013-10-05 12:35:16 -04:00
}
// add own key for encryption
keyIds = Arrays.copyOf(keyIds, keyIds.length + 1);
keyIds[keyIds.length - 1] = appSettings.getKeyId();
2013-06-17 13:51:41 -04:00
// build InputData and write into OutputStream
2014-02-14 11:01:17 -05:00
// Get Input- and OutputStream from ParcelFileDescriptor
InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(input);
OutputStream os = new ParcelFileDescriptor.AutoCloseOutputStream(output);
try {
long inputLength = is.available();
InputData inputData = new InputData(is, inputLength);
2013-06-17 13:51:41 -04:00
2014-02-14 11:01:17 -05:00
PgpOperation operation = new PgpOperation(getContext(), null, inputData, os);
if (sign) {
String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(),
appSettings.getKeyId());
if (passphrase == null) {
return getPassphraseBundleIntent(appSettings.getKeyId());
}
2013-06-17 13:51:41 -04:00
2014-02-14 11:01:17 -05:00
operation.signAndEncrypt(asciiArmor, appSettings.getCompression(), keyIds, null,
appSettings.getEncryptionAlgorithm(), appSettings.getKeyId(),
appSettings.getHashAlgorithm(), true, passphrase);
} else {
operation.signAndEncrypt(asciiArmor, appSettings.getCompression(), keyIds, null,
appSettings.getEncryptionAlgorithm(), Id.key.none,
appSettings.getHashAlgorithm(), true, null);
}
} finally {
is.close();
os.close();
2013-10-05 12:35:16 -04:00
}
2014-02-14 11:01:17 -05:00
Bundle result = new Bundle();
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_SUCCESS);
return result;
2013-06-17 13:51:41 -04:00
} catch (Exception e) {
2014-02-14 11:01:17 -05:00
Bundle result = new Bundle();
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR);
result.putParcelable(OpenPgpConstants.RESULT_ERRORS,
new OpenPgpError(OpenPgpError.GENERIC_ERROR, e.getMessage()));
return result;
2013-06-17 13:51:41 -04:00
}
}
// TODO: asciiArmor?!
private Bundle signImpl(ParcelFileDescriptor input, ParcelFileDescriptor output, AppSettings appSettings) {
2013-09-08 12:21:04 -04:00
try {
// get passphrase from cache, if key has "no" passphrase, this returns an empty String
String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), appSettings.getKeyId());
2013-09-08 17:08:30 -04:00
if (passphrase == null) {
2014-02-14 11:01:17 -05:00
return getPassphraseBundleIntent(appSettings.getKeyId());
}
2013-09-08 12:21:04 -04:00
2014-02-14 11:01:17 -05:00
// Get Input- and OutputStream from ParcelFileDescriptor
InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(input);
OutputStream os = new ParcelFileDescriptor.AutoCloseOutputStream(output);
try {
long inputLength = is.available();
InputData inputData = new InputData(is, inputLength);
PgpOperation operation = new PgpOperation(getContext(), null, inputData, os);
operation.signText(appSettings.getKeyId(), passphrase, appSettings.getHashAlgorithm(),
Preferences.getPreferences(this).getForceV3Signatures());
2014-02-14 11:01:17 -05:00
} finally {
is.close();
os.close();
}
2013-09-08 12:21:04 -04:00
Bundle result = new Bundle();
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_SUCCESS);
return result;
2013-09-08 12:21:04 -04:00
} catch (Exception e) {
Bundle result = new Bundle();
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR);
result.putParcelable(OpenPgpConstants.RESULT_ERRORS,
new OpenPgpError(OpenPgpError.GENERIC_ERROR, e.getMessage()));
return result;
2013-09-08 12:21:04 -04:00
}
2013-09-06 12:36:16 -04:00
}
2014-02-14 11:01:17 -05:00
private Bundle decryptAndVerifyImpl(Bundle params, ParcelFileDescriptor input,
ParcelFileDescriptor output, AppSettings appSettings) {
2013-05-28 09:10:36 -04:00
try {
2014-02-14 11:01:17 -05:00
// Get Input- and OutputStream from ParcelFileDescriptor
InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(input);
OutputStream os = new ParcelFileDescriptor.AutoCloseOutputStream(output);
OpenPgpSignatureResult sigResult = null;
try {
2013-09-09 16:38:09 -04:00
2014-02-14 11:01:17 -05:00
// TODOs API 2.0:
// implement verify-only!
// fix the mess: http://stackoverflow.com/questions/148130/how-do-i-peek-at-the-first-two-bytes-in-an-inputstream
// should we allow to decrypt everything under every key id or only the one set?
// TODO: instead of trying to get the passphrase before
// pause stream when passphrase is missing and then resume
// TODO: this is not really needed
// checked if it is text with BEGIN and END tags
// String message = new String(inputBytes);
// Log.d(Constants.TAG, "in: " + message);
boolean signedOnly = false;
// Matcher matcher = PgpHelper.PGP_MESSAGE.matcher(message);
// if (matcher.matches()) {
// Log.d(Constants.TAG, "PGP_MESSAGE matched");
// message = matcher.group(1);
// // replace non breakable spaces
// message = message.replaceAll("\\xa0", " ");
//
// // overwrite inputBytes
// inputBytes = message.getBytes();
// } else {
// matcher = PgpHelper.PGP_SIGNED_MESSAGE.matcher(message);
// if (matcher.matches()) {
// signedOnly = true;
// Log.d(Constants.TAG, "PGP_SIGNED_MESSAGE matched");
// message = matcher.group(1);
// // replace non breakable spaces
// message = message.replaceAll("\\xa0", " ");
//
// // overwrite inputBytes
// inputBytes = message.getBytes();
// } else {
// Log.d(Constants.TAG, "Nothing matched! Binary?");
// }
// }
// END TODO
2013-05-28 09:10:36 -04:00
2014-02-14 11:01:17 -05:00
// Log.d(Constants.TAG, "in: " + new String(inputBytes));
2013-05-28 09:10:36 -04:00
2014-02-14 11:01:17 -05:00
// TODO: This allows to decrypt messages with ALL secret keys, not only the one for the
// app, Fix this?
2013-05-28 09:10:36 -04:00
2014-02-14 11:01:17 -05:00
// String passphrase = null;
// if (!signedOnly) {
// // BEGIN Get key
// // TODO: this input stream is consumed after PgpMain.getDecryptionKeyId()... do it
// // better!
// InputStream inputStream2 = new ByteArrayInputStream(inputBytes);
//
// // TODO: duplicates functions from DecryptActivity!
// long secretKeyId;
// try {
// if (inputStream2.markSupported()) {
// // should probably set this to the max size of two
// // pgpF objects, if it even needs to be anything other
// // than 0.
// inputStream2.mark(200);
// }
// secretKeyId = PgpHelper.getDecryptionKeyId(this, inputStream2);
// if (secretKeyId == Id.key.none) {
// throw new PgpGeneralException(getString(R.string.error_no_secret_key_found));
// }
// } catch (NoAsymmetricEncryptionException e) {
// if (inputStream2.markSupported()) {
// inputStream2.reset();
// }
// secretKeyId = Id.key.symmetric;
// if (!PgpOperation.hasSymmetricEncryption(this, inputStream2)) {
// throw new PgpGeneralException(
// getString(R.string.error_no_known_encryption_found));
// }
// // we do not support symmetric decryption from the API!
// throw new Exception("Symmetric decryption is not supported!");
// }
//
// Log.d(Constants.TAG, "secretKeyId " + secretKeyId);
2013-05-28 09:10:36 -04:00
2014-02-14 11:01:17 -05:00
// NOTE: currently this only gets the passphrase for the saved key
String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), appSettings.getKeyId());
if (passphrase == null) {
return getPassphraseBundleIntent(appSettings.getKeyId());
}
// }
2013-05-28 09:10:36 -04:00
2014-02-14 11:01:17 -05:00
// build InputData and write into OutputStream
long inputLength = is.available();
InputData inputData = new InputData(is, inputLength);
2013-05-28 09:10:36 -04:00
2014-02-14 11:01:17 -05:00
Bundle outputBundle;
PgpOperation operation = new PgpOperation(getContext(), null, inputData, os);
if (signedOnly) {
outputBundle = operation.verifyText();
} else {
// BIG TODO: instead of trying to get the passphrase before
// pause stream when passphrase is missing and then resume
outputBundle = operation.decryptAndVerify(passphrase, false);
2013-09-13 20:08:06 -04:00
}
2014-02-14 11:01:17 -05:00
// outputStream.close();
2013-05-28 09:10:36 -04:00
2014-02-14 11:01:17 -05:00
// byte[] outputBytes = ((ByteArrayOutputStream) outputStream).toByteArray();
2013-05-28 09:10:36 -04:00
2014-02-14 11:01:17 -05:00
// get signature informations from bundle
boolean signature = outputBundle.getBoolean(KeychainIntentService.RESULT_SIGNATURE);
if (signature) {
long signatureKeyId = outputBundle
.getLong(KeychainIntentService.RESULT_SIGNATURE_KEY_ID);
String signatureUserId = outputBundle
.getString(KeychainIntentService.RESULT_SIGNATURE_USER_ID);
boolean signatureSuccess = outputBundle
.getBoolean(KeychainIntentService.RESULT_SIGNATURE_SUCCESS);
boolean signatureUnknown = outputBundle
.getBoolean(KeychainIntentService.RESULT_SIGNATURE_UNKNOWN);
int signatureStatus = OpenPgpSignatureResult.SIGNATURE_ERROR;
if (signatureSuccess) {
signatureStatus = OpenPgpSignatureResult.SIGNATURE_SUCCESS_CERTIFIED;
} else if (signatureUnknown) {
signatureStatus = OpenPgpSignatureResult.SIGNATURE_UNKNOWN_PUB_KEY;
}
sigResult = new OpenPgpSignatureResult(signatureStatus, signatureUserId,
signedOnly, signatureKeyId);
}
} finally {
is.close();
os.close();
}
Bundle result = new Bundle();
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_SUCCESS);
result.putParcelable(OpenPgpConstants.RESULT_SIGNATURE, sigResult);
return result;
} catch (Exception e) {
Bundle result = new Bundle();
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR);
result.putParcelable(OpenPgpConstants.RESULT_ERRORS,
new OpenPgpError(OpenPgpError.GENERIC_ERROR, e.getMessage()));
return result;
2013-10-05 12:35:16 -04:00
}
}
2014-02-14 07:40:24 -05:00
/**
* Checks that params != null and API version fits
*
* @param params
* @return
*/
2014-02-14 11:01:17 -05:00
2014-02-14 07:40:24 -05:00
private Bundle validateParamsAndVersion(Bundle params) {
if (params == null) {
Bundle result = new Bundle();
OpenPgpError error = new OpenPgpError(OpenPgpError.GENERIC_ERROR, "params Bundle required!");
result.putParcelable(OpenPgpConstants.RESULT_ERRORS, error);
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR);
return result;
}
if (params.getInt(OpenPgpConstants.PARAMS_API_VERSION) != OpenPgpConstants.API_VERSION) {
// not compatible!
Bundle result = new Bundle();
OpenPgpError error = new OpenPgpError(OpenPgpError.INCOMPATIBLE_API_VERSIONS, "Incompatible API versions!");
result.putParcelable(OpenPgpConstants.RESULT_ERRORS, error);
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR);
return result;
}
return null;
}
2014-02-14 11:01:17 -05:00
// TODO: enqueue in thread pool!!!
2013-09-10 17:19:34 -04:00
private final IOpenPgpService.Stub mBinder = new IOpenPgpService.Stub() {
2013-05-28 09:10:36 -04:00
@Override
public Bundle sign(Bundle params, final ParcelFileDescriptor input, final ParcelFileDescriptor output) {
final AppSettings appSettings = getAppSettings();
2014-02-14 07:40:24 -05:00
Bundle errorResult = validateParamsAndVersion(params);
if (errorResult != null) {
return errorResult;
}
2013-06-17 13:51:41 -04:00
return signImpl(input, output, appSettings);
2013-05-28 09:10:36 -04:00
}
@Override
public Bundle encrypt(Bundle params, ParcelFileDescriptor input, ParcelFileDescriptor output) {
2014-02-14 11:01:17 -05:00
final AppSettings appSettings = getAppSettings();
2013-09-06 12:54:55 -04:00
2014-02-14 11:01:17 -05:00
Bundle errorResult = validateParamsAndVersion(params);
if (errorResult != null) {
return errorResult;
}
return encryptAndSignImpl(params, input, output, appSettings, false);
2013-05-28 09:10:36 -04:00
}
@Override
public Bundle signAndEncrypt(Bundle params, ParcelFileDescriptor input, ParcelFileDescriptor output) {
2014-02-14 11:01:17 -05:00
final AppSettings appSettings = getAppSettings();
2014-02-14 11:01:17 -05:00
Bundle errorResult = validateParamsAndVersion(params);
if (errorResult != null) {
return errorResult;
}
return encryptAndSignImpl(params, input, output, appSettings, true);
2013-05-28 09:10:36 -04:00
}
2013-10-02 13:08:33 -04:00
@Override
public Bundle decryptAndVerify(Bundle params, ParcelFileDescriptor input, ParcelFileDescriptor output) {
2013-10-05 12:35:16 -04:00
return null;
2013-10-02 13:08:33 -04:00
}
// @Override
// public void getKeyIds(final String[] userIds, final boolean allowUserInteraction,
// final IOpenPgpKeyIdsCallback callback) throws RemoteException {
//
// final AppSettings settings = getAppSettings();
//
// Runnable r = new Runnable() {
// @Override
// public void run() {
// getKeyIdsSafe(userIds, allowUserInteraction, callback, settings);
// }
// };
//
// checkAndEnqueue(r);
// }
};
2013-09-16 07:08:02 -04:00
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
2013-05-28 09:10:36 -04:00
}