2013-05-28 09:10:36 -04:00
|
|
|
/*
|
2013-05-28 16:48:42 -04:00
|
|
|
* Copyright (C) 2013 Dominik Schürmann <dominik@dominikschuermann.de>
|
2013-05-28 09:10:36 -04:00
|
|
|
*
|
2013-09-06 10:17:01 -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
|
|
|
*
|
2013-09-06 10:17:01 -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
|
|
|
*
|
2013-09-06 10:17:01 -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
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2013-06-17 09:52:09 -04:00
|
|
|
import java.util.ArrayList;
|
2013-09-09 06:59:53 -04:00
|
|
|
import java.util.regex.Matcher;
|
2013-05-29 09:58:50 -04:00
|
|
|
|
2013-09-10 17:19:34 -04:00
|
|
|
import org.openintents.openpgp.IOpenPgpCallback;
|
|
|
|
import org.openintents.openpgp.IOpenPgpService;
|
|
|
|
import org.openintents.openpgp.OpenPgpError;
|
|
|
|
import org.openintents.openpgp.OpenPgpSignatureResult;
|
2013-05-28 09:10:36 -04:00
|
|
|
import org.sufficientlysecure.keychain.Constants;
|
|
|
|
import org.sufficientlysecure.keychain.Id;
|
|
|
|
import org.sufficientlysecure.keychain.R;
|
2013-09-08 12:21:04 -04:00
|
|
|
import org.sufficientlysecure.keychain.helper.Preferences;
|
2013-09-15 10:42:08 -04:00
|
|
|
import org.sufficientlysecure.keychain.pgp.PgpHelper;
|
|
|
|
import org.sufficientlysecure.keychain.pgp.PgpOperation;
|
|
|
|
import org.sufficientlysecure.keychain.pgp.exception.NoAsymmetricEncryptionException;
|
|
|
|
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
|
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
|
|
|
|
|
|
|
import android.content.Intent;
|
2013-09-06 12:36:16 -04:00
|
|
|
import android.database.Cursor;
|
2013-09-06 07:48:27 -04:00
|
|
|
import android.net.Uri;
|
2013-05-28 09:10:36 -04:00
|
|
|
import android.os.Bundle;
|
2013-09-08 16:52:38 -04:00
|
|
|
import android.os.Handler;
|
2013-05-28 09:10:36 -04:00
|
|
|
import android.os.IBinder;
|
2013-09-08 16:52:38 -04:00
|
|
|
import android.os.Message;
|
|
|
|
import android.os.Messenger;
|
2013-05-28 09:10:36 -04:00
|
|
|
import android.os.RemoteException;
|
|
|
|
|
2013-09-15 09:20:15 -04:00
|
|
|
public class OpenPgpService extends RemoteService {
|
2013-09-09 13:38:13 -04:00
|
|
|
|
2013-05-28 09:10:36 -04:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
2013-09-15 09:20:15 -04:00
|
|
|
Log.d(Constants.TAG, "OpenPgpService, onCreate()");
|
2013-05-28 09:10:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2013-09-15 09:20:15 -04:00
|
|
|
Log.d(Constants.TAG, "OpenPgpService, onDestroy()");
|
2013-05-28 09:10:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
2013-09-08 16:52:38 -04:00
|
|
|
return mBinder;
|
2013-05-28 09:10:36 -04:00
|
|
|
}
|
|
|
|
|
2013-09-06 07:48:27 -04:00
|
|
|
private String getCachedPassphrase(long keyId) {
|
2013-09-15 09:20:15 -04:00
|
|
|
String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), keyId);
|
2013-09-06 07:48:27 -04:00
|
|
|
|
|
|
|
if (passphrase == null) {
|
|
|
|
Log.d(Constants.TAG, "No passphrase! Activity required!");
|
|
|
|
|
|
|
|
// start passphrase dialog
|
|
|
|
Bundle extras = new Bundle();
|
2013-09-15 09:20:15 -04:00
|
|
|
extras.putLong(RemoteServiceActivity.EXTRA_SECRET_KEY_ID, keyId);
|
2013-09-08 12:21:04 -04:00
|
|
|
|
2013-09-08 16:52:38 -04:00
|
|
|
PassphraseActivityCallback callback = new PassphraseActivityCallback();
|
|
|
|
Messenger messenger = new Messenger(new Handler(getMainLooper(), callback));
|
|
|
|
|
2013-09-15 09:20:15 -04:00
|
|
|
pauseQueueAndStartServiceActivity(RemoteServiceActivity.ACTION_CACHE_PASSPHRASE,
|
2013-09-08 16:52:38 -04:00
|
|
|
messenger, extras);
|
|
|
|
|
|
|
|
if (callback.isSuccess()) {
|
|
|
|
Log.d(Constants.TAG, "New passphrase entered!");
|
|
|
|
|
|
|
|
// get again after it was entered
|
2013-09-15 09:20:15 -04:00
|
|
|
passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), keyId);
|
2013-09-08 16:52:38 -04:00
|
|
|
} else {
|
|
|
|
Log.d(Constants.TAG, "Passphrase dialog canceled!");
|
|
|
|
|
2013-09-08 17:08:30 -04:00
|
|
|
return null;
|
2013-09-08 16:52:38 -04:00
|
|
|
}
|
|
|
|
|
2013-09-06 07:48:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return passphrase;
|
|
|
|
}
|
|
|
|
|
2013-09-15 08:16:29 -04:00
|
|
|
public class PassphraseActivityCallback extends UserInputCallback {
|
2013-09-08 16:52:38 -04:00
|
|
|
|
2013-09-08 17:08:30 -04:00
|
|
|
private boolean success = false;
|
2013-09-08 16:52:38 -04:00
|
|
|
|
|
|
|
public boolean isSuccess() {
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-09-15 08:16:29 -04:00
|
|
|
public void handleUserInput(Message msg) {
|
2013-09-09 13:38:13 -04:00
|
|
|
if (msg.arg1 == OKAY) {
|
2013-09-08 16:52:38 -04:00
|
|
|
success = true;
|
|
|
|
} else {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-06 12:54:55 -04:00
|
|
|
/**
|
|
|
|
* Search database for key ids based on emails.
|
|
|
|
*
|
|
|
|
* @param encryptionUserIds
|
|
|
|
* @return
|
|
|
|
*/
|
2013-09-08 11:04:33 -04:00
|
|
|
private long[] getKeyIdsFromEmails(String[] encryptionUserIds, long ownKeyId) {
|
2013-09-06 12:54:55 -04:00
|
|
|
// find key ids to given emails in database
|
|
|
|
ArrayList<Long> keyIds = new ArrayList<Long>();
|
2013-09-09 13:38:13 -04:00
|
|
|
|
|
|
|
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 {
|
2013-09-09 13:38:13 -04:00
|
|
|
missingUserIdsCheck = true;
|
|
|
|
missingUserIds.add(email);
|
2013-09-06 12:54:55 -04:00
|
|
|
Log.d(Constants.TAG, "user id missing");
|
|
|
|
}
|
|
|
|
if (cur.moveToNext()) {
|
2013-09-09 13:38:13 -04:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 11:04:33 -04:00
|
|
|
// also encrypt to our self (so that we can decrypt it later!)
|
|
|
|
keyIds.add(ownKeyId);
|
2013-09-06 12:54:55 -04:00
|
|
|
|
2013-09-08 16:52:38 -04:00
|
|
|
// convert to long[]
|
2013-09-08 11:04:33 -04:00
|
|
|
long[] keyIdsArray = new long[keyIds.size()];
|
|
|
|
for (int i = 0; i < keyIdsArray.length; i++) {
|
|
|
|
keyIdsArray[i] = keyIds.get(i);
|
|
|
|
}
|
|
|
|
|
2013-09-09 13:38:13 -04:00
|
|
|
if (missingUserIdsCheck || dublicateUserIdsCheck) {
|
2013-09-08 16:52:38 -04:00
|
|
|
SelectPubKeysActivityCallback callback = new SelectPubKeysActivityCallback();
|
|
|
|
Messenger messenger = new Messenger(new Handler(getMainLooper(), callback));
|
|
|
|
|
2013-09-08 11:04:33 -04:00
|
|
|
Bundle extras = new Bundle();
|
2013-09-15 09:20:15 -04:00
|
|
|
extras.putLongArray(RemoteServiceActivity.EXTRA_SELECTED_MASTER_KEY_IDS, keyIdsArray);
|
|
|
|
extras.putStringArrayList(RemoteServiceActivity.EXTRA_MISSING_USER_IDS, missingUserIds);
|
|
|
|
extras.putStringArrayList(RemoteServiceActivity.EXTRA_DUBLICATE_USER_IDS,
|
2013-09-09 13:38:13 -04:00
|
|
|
dublicateUserIds);
|
|
|
|
|
2013-09-15 09:20:15 -04:00
|
|
|
pauseQueueAndStartServiceActivity(RemoteServiceActivity.ACTION_SELECT_PUB_KEYS,
|
2013-09-08 16:52:38 -04:00
|
|
|
messenger, extras);
|
|
|
|
|
2013-09-09 13:46:18 -04:00
|
|
|
if (callback.isSuccess()) {
|
2013-09-08 16:52:38 -04:00
|
|
|
Log.d(Constants.TAG, "New selection of pub keys!");
|
|
|
|
keyIdsArray = callback.getPubKeyIds();
|
|
|
|
} else {
|
|
|
|
Log.d(Constants.TAG, "Pub key selection canceled!");
|
2013-09-09 13:46:18 -04:00
|
|
|
return null;
|
2013-09-08 16:52:38 -04:00
|
|
|
}
|
2013-09-08 11:04:33 -04:00
|
|
|
}
|
|
|
|
|
2013-09-09 13:46:18 -04:00
|
|
|
if (keyIdsArray.length == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-09-08 11:04:33 -04:00
|
|
|
return keyIdsArray;
|
2013-09-06 12:54:55 -04:00
|
|
|
}
|
|
|
|
|
2013-09-15 08:16:29 -04:00
|
|
|
public class SelectPubKeysActivityCallback extends UserInputCallback {
|
2013-09-08 16:52:38 -04:00
|
|
|
public static final String PUB_KEY_IDS = "pub_key_ids";
|
|
|
|
|
2013-09-09 13:46:18 -04:00
|
|
|
private boolean success = false;
|
2013-09-08 16:52:38 -04:00
|
|
|
private long[] pubKeyIds;
|
|
|
|
|
2013-09-09 13:46:18 -04:00
|
|
|
public boolean isSuccess() {
|
|
|
|
return success;
|
2013-09-08 16:52:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public long[] getPubKeyIds() {
|
|
|
|
return pubKeyIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-09-15 08:16:29 -04:00
|
|
|
public void handleUserInput(Message msg) {
|
2013-09-08 16:52:38 -04:00
|
|
|
if (msg.arg1 == OKAY) {
|
2013-09-09 13:46:18 -04:00
|
|
|
success = true;
|
2013-09-08 16:52:38 -04:00
|
|
|
pubKeyIds = msg.getData().getLongArray(PUB_KEY_IDS);
|
|
|
|
} else {
|
2013-09-09 13:46:18 -04:00
|
|
|
success = false;
|
2013-09-08 16:52:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-06 12:54:55 -04:00
|
|
|
private synchronized void encryptAndSignSafe(byte[] inputBytes, String[] encryptionUserIds,
|
2013-09-16 06:16:54 -04:00
|
|
|
boolean asciiArmor, boolean allowUserInteraction, IOpenPgpCallback callback,
|
|
|
|
AppSettings appSettings, boolean sign) throws RemoteException {
|
2013-06-17 13:51:41 -04:00
|
|
|
try {
|
|
|
|
// build InputData and write into OutputStream
|
|
|
|
InputStream inputStream = new ByteArrayInputStream(inputBytes);
|
|
|
|
long inputLength = inputBytes.length;
|
|
|
|
InputData inputData = new InputData(inputStream, inputLength);
|
|
|
|
|
2013-09-06 07:48:27 -04:00
|
|
|
OutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
|
2013-09-08 11:04:33 -04:00
|
|
|
long[] keyIds = getKeyIdsFromEmails(encryptionUserIds, appSettings.getKeyId());
|
2013-09-09 13:46:18 -04:00
|
|
|
if (keyIds == null) {
|
2013-09-10 17:19:34 -04:00
|
|
|
callback.onError(new OpenPgpError(OpenPgpError.ID_NO_USER_IDS, "No user ids!"));
|
2013-09-09 13:46:18 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-09-06 07:48:27 -04:00
|
|
|
|
2013-09-15 10:42:08 -04:00
|
|
|
PgpOperation operation = new PgpOperation(getContext(), null, inputData, outputStream);
|
2013-09-06 12:54:55 -04:00
|
|
|
if (sign) {
|
2013-09-08 12:21:04 -04:00
|
|
|
String passphrase = getCachedPassphrase(appSettings.getKeyId());
|
2013-09-08 17:08:30 -04:00
|
|
|
if (passphrase == null) {
|
2013-09-10 17:19:34 -04:00
|
|
|
callback.onError(new OpenPgpError(OpenPgpError.ID_NO_OR_WRONG_PASSPHRASE,
|
2013-09-08 17:08:30 -04:00
|
|
|
"No or wrong passphrase!"));
|
|
|
|
return;
|
|
|
|
}
|
2013-09-08 12:21:04 -04:00
|
|
|
|
2013-09-15 10:45:07 -04:00
|
|
|
operation.signAndEncrypt(asciiArmor, appSettings.getCompression(), keyIds, null,
|
2013-09-06 12:54:55 -04:00
|
|
|
appSettings.getEncryptionAlgorithm(), appSettings.getKeyId(),
|
|
|
|
appSettings.getHashAlgorithm(), true, passphrase);
|
|
|
|
} else {
|
2013-09-15 10:45:07 -04:00
|
|
|
operation.signAndEncrypt(asciiArmor, appSettings.getCompression(), keyIds, null,
|
2013-09-06 12:54:55 -04:00
|
|
|
appSettings.getEncryptionAlgorithm(), Id.key.none,
|
|
|
|
appSettings.getHashAlgorithm(), true, null);
|
|
|
|
}
|
2013-06-17 13:51:41 -04:00
|
|
|
|
2013-09-06 07:48:27 -04:00
|
|
|
outputStream.close();
|
2013-06-17 13:51:41 -04:00
|
|
|
|
2013-09-06 07:48:27 -04:00
|
|
|
byte[] outputBytes = ((ByteArrayOutputStream) outputStream).toByteArray();
|
2013-06-17 13:51:41 -04:00
|
|
|
|
2013-09-06 07:48:27 -04:00
|
|
|
// return over handler on client side
|
|
|
|
callback.onSuccess(outputBytes, null);
|
2013-06-17 13:51:41 -04:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(Constants.TAG, "KeychainService, Exception!", e);
|
|
|
|
|
|
|
|
try {
|
2013-09-10 17:19:34 -04:00
|
|
|
callback.onError(new OpenPgpError(0, e.getMessage()));
|
2013-06-17 13:51:41 -04:00
|
|
|
} catch (Exception t) {
|
|
|
|
Log.e(Constants.TAG, "Error returning exception to client", t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 06:59:53 -04:00
|
|
|
// TODO: asciiArmor?!
|
2013-09-16 06:16:54 -04:00
|
|
|
private void signSafe(byte[] inputBytes, boolean allowUserInteraction,
|
|
|
|
IOpenPgpCallback callback, AppSettings appSettings) throws RemoteException {
|
2013-09-08 12:21:04 -04:00
|
|
|
try {
|
|
|
|
Log.d(Constants.TAG, "current therad id: " + Thread.currentThread().getId());
|
|
|
|
|
|
|
|
// build InputData and write into OutputStream
|
|
|
|
InputStream inputStream = new ByteArrayInputStream(inputBytes);
|
|
|
|
long inputLength = inputBytes.length;
|
|
|
|
InputData inputData = new InputData(inputStream, inputLength);
|
|
|
|
|
|
|
|
OutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
String passphrase = getCachedPassphrase(appSettings.getKeyId());
|
2013-09-08 17:08:30 -04:00
|
|
|
if (passphrase == null) {
|
2013-09-10 17:19:34 -04:00
|
|
|
callback.onError(new OpenPgpError(OpenPgpError.ID_NO_OR_WRONG_PASSPHRASE,
|
2013-09-08 17:08:30 -04:00
|
|
|
"No or wrong passphrase!"));
|
|
|
|
return;
|
|
|
|
}
|
2013-09-08 12:21:04 -04:00
|
|
|
|
2013-09-15 10:42:08 -04:00
|
|
|
PgpOperation operation = new PgpOperation(getContext(), null, inputData, outputStream);
|
|
|
|
operation.signText(appSettings.getKeyId(), passphrase, appSettings.getHashAlgorithm(),
|
|
|
|
Preferences.getPreferences(this).getForceV3Signatures());
|
2013-09-08 12:21:04 -04:00
|
|
|
|
|
|
|
outputStream.close();
|
|
|
|
|
|
|
|
byte[] outputBytes = ((ByteArrayOutputStream) outputStream).toByteArray();
|
|
|
|
|
|
|
|
// return over handler on client side
|
|
|
|
callback.onSuccess(outputBytes, null);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(Constants.TAG, "KeychainService, Exception!", e);
|
|
|
|
|
|
|
|
try {
|
2013-09-10 17:19:34 -04:00
|
|
|
callback.onError(new OpenPgpError(0, e.getMessage()));
|
2013-09-08 12:21:04 -04:00
|
|
|
} catch (Exception t) {
|
|
|
|
Log.e(Constants.TAG, "Error returning exception to client", t);
|
|
|
|
}
|
|
|
|
}
|
2013-09-06 12:36:16 -04:00
|
|
|
}
|
|
|
|
|
2013-09-16 06:16:54 -04:00
|
|
|
private synchronized void decryptAndVerifySafe(byte[] inputBytes, boolean allowUserInteraction,
|
|
|
|
IOpenPgpCallback callback, AppSettings appSettings) throws RemoteException {
|
2013-05-28 09:10:36 -04:00
|
|
|
try {
|
2013-09-09 16:38:09 -04:00
|
|
|
// TODO: this is not really needed
|
|
|
|
// checked if it is text with BEGIN and END tags
|
2013-09-09 06:59:53 -04:00
|
|
|
String message = new String(inputBytes);
|
|
|
|
Log.d(Constants.TAG, "in: " + message);
|
|
|
|
boolean signedOnly = false;
|
2013-09-15 10:42:08 -04:00
|
|
|
Matcher matcher = PgpHelper.PGP_MESSAGE.matcher(message);
|
2013-09-09 06:59:53 -04:00
|
|
|
if (matcher.matches()) {
|
2013-09-09 16:38:09 -04:00
|
|
|
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 {
|
2013-09-15 10:42:08 -04:00
|
|
|
matcher = PgpHelper.PGP_SIGNED_MESSAGE.matcher(message);
|
2013-09-09 16:38:09 -04:00
|
|
|
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?");
|
|
|
|
}
|
2013-09-09 06:59:53 -04:00
|
|
|
}
|
2013-09-09 16:38:09 -04:00
|
|
|
// END TODO
|
|
|
|
|
|
|
|
Log.d(Constants.TAG, "in: " + new String(inputBytes));
|
2013-05-28 09:10:36 -04:00
|
|
|
|
2013-09-06 07:48:27 -04:00
|
|
|
// TODO: This allows to decrypt messages with ALL secret keys, not only the one for the
|
|
|
|
// app, Fix this?
|
2013-09-15 09:20:15 -04:00
|
|
|
// long secretKeyId = PgpMain.getDecryptionKeyId(getContext(), inputStream);
|
2013-09-09 06:59:53 -04:00
|
|
|
// if (secretKeyId == Id.key.none) {
|
|
|
|
// throw new PgpMain.PgpGeneralException(getString(R.string.error_noSecretKeyFound));
|
|
|
|
// }
|
2013-06-17 09:52:09 -04:00
|
|
|
|
2013-09-09 16:38:09 -04:00
|
|
|
String passphrase = null;
|
2013-09-09 06:59:53 -04:00
|
|
|
boolean assumeSymmetricEncryption = false;
|
2013-09-09 16:38:09 -04:00
|
|
|
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!
|
|
|
|
// TODO: we need activity to input symmetric passphrase
|
|
|
|
long secretKeyId;
|
|
|
|
try {
|
|
|
|
if (inputStream2.markSupported()) {
|
2013-09-15 09:20:15 -04:00
|
|
|
// 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);
|
2013-09-09 16:38:09 -04:00
|
|
|
}
|
2013-09-15 10:42:08 -04:00
|
|
|
secretKeyId = PgpHelper.getDecryptionKeyId(this, inputStream2);
|
2013-09-09 16:38:09 -04:00
|
|
|
if (secretKeyId == Id.key.none) {
|
2013-09-15 10:42:08 -04:00
|
|
|
throw new PgpGeneralException(getString(R.string.error_noSecretKeyFound));
|
2013-09-09 16:38:09 -04:00
|
|
|
}
|
|
|
|
assumeSymmetricEncryption = false;
|
2013-09-15 10:42:08 -04:00
|
|
|
} catch (NoAsymmetricEncryptionException e) {
|
2013-09-09 16:38:09 -04:00
|
|
|
if (inputStream2.markSupported()) {
|
|
|
|
inputStream2.reset();
|
|
|
|
}
|
|
|
|
secretKeyId = Id.key.symmetric;
|
2013-09-15 10:42:08 -04:00
|
|
|
if (!PgpOperation.hasSymmetricEncryption(this, inputStream2)) {
|
|
|
|
throw new PgpGeneralException(
|
2013-09-09 16:38:09 -04:00
|
|
|
getString(R.string.error_noKnownEncryptionFound));
|
|
|
|
}
|
|
|
|
assumeSymmetricEncryption = true;
|
2013-09-09 06:59:53 -04:00
|
|
|
}
|
2013-09-09 16:38:09 -04:00
|
|
|
|
|
|
|
Log.d(Constants.TAG, "secretKeyId " + secretKeyId);
|
|
|
|
|
|
|
|
passphrase = getCachedPassphrase(secretKeyId);
|
|
|
|
if (passphrase == null) {
|
2013-09-10 17:19:34 -04:00
|
|
|
callback.onError(new OpenPgpError(OpenPgpError.ID_NO_OR_WRONG_PASSPHRASE,
|
2013-09-09 16:38:09 -04:00
|
|
|
"No or wrong passphrase!"));
|
|
|
|
return;
|
2013-09-09 06:59:53 -04:00
|
|
|
}
|
|
|
|
}
|
2013-05-28 09:10:36 -04:00
|
|
|
|
2013-09-09 16:38:09 -04:00
|
|
|
// build InputData and write into OutputStream
|
|
|
|
InputStream inputStream = new ByteArrayInputStream(inputBytes);
|
|
|
|
long inputLength = inputBytes.length;
|
|
|
|
InputData inputData = new InputData(inputStream, inputLength);
|
2013-05-28 09:10:36 -04:00
|
|
|
|
2013-09-09 16:38:09 -04:00
|
|
|
OutputStream outputStream = new ByteArrayOutputStream();
|
2013-05-28 09:10:36 -04:00
|
|
|
|
2013-09-09 06:59:53 -04:00
|
|
|
Bundle outputBundle;
|
2013-09-15 10:42:08 -04:00
|
|
|
PgpOperation operation = new PgpOperation(getContext(), null, inputData, outputStream);
|
2013-09-09 06:59:53 -04:00
|
|
|
if (signedOnly) {
|
|
|
|
// TODO: download missing keys from keyserver?
|
2013-09-15 10:42:08 -04:00
|
|
|
outputBundle = operation.verifyText(false);
|
2013-09-09 06:59:53 -04:00
|
|
|
} else {
|
|
|
|
// TODO: assume symmetric: callback to enter symmetric pass
|
2013-09-15 10:42:08 -04:00
|
|
|
outputBundle = operation.decryptAndVerify(passphrase, assumeSymmetricEncryption);
|
2013-09-09 06:59:53 -04:00
|
|
|
}
|
2013-05-28 09:10:36 -04:00
|
|
|
|
|
|
|
outputStream.close();
|
|
|
|
|
|
|
|
byte[] outputBytes = ((ByteArrayOutputStream) outputStream).toByteArray();
|
|
|
|
|
|
|
|
// get signature informations from bundle
|
|
|
|
boolean signature = outputBundle.getBoolean(KeychainIntentService.RESULT_SIGNATURE);
|
|
|
|
|
2013-09-10 17:19:34 -04:00
|
|
|
OpenPgpSignatureResult sigResult = null;
|
2013-09-09 06:59:53 -04:00
|
|
|
if (signature) {
|
2013-09-15 09:23:13 -04:00
|
|
|
// long signatureKeyId = outputBundle
|
|
|
|
// .getLong(KeychainIntentService.RESULT_SIGNATURE_KEY_ID);
|
2013-09-13 20:08:06 -04:00
|
|
|
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);
|
2013-09-15 08:16:29 -04:00
|
|
|
|
2013-09-13 20:08:06 -04:00
|
|
|
int signatureStatus = OpenPgpSignatureResult.SIGNATURE_ERROR;
|
|
|
|
if (signatureSuccess) {
|
2013-09-15 09:26:41 -04:00
|
|
|
signatureStatus = OpenPgpSignatureResult.SIGNATURE_SUCCESS_TRUSTED;
|
2013-09-13 20:08:06 -04:00
|
|
|
} else if (signatureUnknown) {
|
|
|
|
signatureStatus = OpenPgpSignatureResult.SIGNATURE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
sigResult = new OpenPgpSignatureResult(signatureStatus, signatureUserId, signedOnly);
|
2013-09-09 06:59:53 -04:00
|
|
|
}
|
2013-05-28 09:10:36 -04:00
|
|
|
|
2013-05-28 16:48:42 -04:00
|
|
|
// return over handler on client side
|
2013-07-01 17:19:53 -04:00
|
|
|
callback.onSuccess(outputBytes, sigResult);
|
2013-05-28 09:10:36 -04:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(Constants.TAG, "KeychainService, Exception!", e);
|
|
|
|
|
|
|
|
try {
|
2013-09-10 17:19:34 -04:00
|
|
|
callback.onError(new OpenPgpError(0, e.getMessage()));
|
2013-05-28 09:10:36 -04:00
|
|
|
} catch (Exception t) {
|
|
|
|
Log.e(Constants.TAG, "Error returning exception to client", t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2013-06-17 13:51:41 -04:00
|
|
|
public void encrypt(final byte[] inputBytes, final String[] encryptionUserIds,
|
2013-09-16 06:16:54 -04:00
|
|
|
final boolean asciiArmor, final boolean allowUserInteraction,
|
|
|
|
final IOpenPgpCallback callback) throws RemoteException {
|
2013-05-28 09:10:36 -04:00
|
|
|
|
2013-09-06 07:48:27 -04:00
|
|
|
final AppSettings settings = getAppSettings();
|
|
|
|
|
2013-06-17 13:51:41 -04:00
|
|
|
Runnable r = new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2013-09-16 06:16:54 -04:00
|
|
|
encryptAndSignSafe(inputBytes, encryptionUserIds, asciiArmor,
|
|
|
|
allowUserInteraction, callback, settings, false);
|
2013-06-17 13:51:41 -04:00
|
|
|
} catch (RemoteException e) {
|
2013-09-15 09:20:15 -04:00
|
|
|
Log.e(Constants.TAG, "OpenPgpService", e);
|
2013-06-17 13:51:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
checkAndEnqueue(r);
|
2013-05-28 09:10:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-09-13 20:08:06 -04:00
|
|
|
public void signAndEncrypt(final byte[] inputBytes, final String[] encryptionUserIds,
|
2013-09-16 06:16:54 -04:00
|
|
|
final boolean asciiArmor, final boolean allowUserInteraction,
|
|
|
|
final IOpenPgpCallback callback) throws RemoteException {
|
2013-09-06 12:36:16 -04:00
|
|
|
|
|
|
|
final AppSettings settings = getAppSettings();
|
|
|
|
|
|
|
|
Runnable r = new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2013-09-16 06:16:54 -04:00
|
|
|
encryptAndSignSafe(inputBytes, encryptionUserIds, asciiArmor,
|
|
|
|
allowUserInteraction, callback, settings, true);
|
2013-09-06 12:36:16 -04:00
|
|
|
} catch (RemoteException e) {
|
2013-09-15 09:20:15 -04:00
|
|
|
Log.e(Constants.TAG, "OpenPgpService", e);
|
2013-09-06 12:36:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
checkAndEnqueue(r);
|
2013-05-28 09:10:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-09-10 17:19:34 -04:00
|
|
|
public void sign(final byte[] inputBytes, boolean asciiArmor,
|
2013-09-16 06:16:54 -04:00
|
|
|
final boolean allowUserInteraction, final IOpenPgpCallback callback)
|
|
|
|
throws RemoteException {
|
2013-09-06 12:54:55 -04:00
|
|
|
final AppSettings settings = getAppSettings();
|
|
|
|
|
|
|
|
Runnable r = new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2013-09-16 06:16:54 -04:00
|
|
|
signSafe(inputBytes, allowUserInteraction, callback, settings);
|
2013-09-06 12:54:55 -04:00
|
|
|
} catch (RemoteException e) {
|
2013-09-15 09:20:15 -04:00
|
|
|
Log.e(Constants.TAG, "OpenPgpService", e);
|
2013-09-06 12:54:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
checkAndEnqueue(r);
|
2013-05-28 09:10:36 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-09-16 06:16:54 -04:00
|
|
|
public void decryptAndVerify(final byte[] inputBytes, final boolean allowUserInteraction,
|
|
|
|
final IOpenPgpCallback callback) throws RemoteException {
|
2013-06-17 09:52:09 -04:00
|
|
|
|
2013-09-06 12:36:16 -04:00
|
|
|
final AppSettings settings = getAppSettings();
|
|
|
|
|
2013-06-17 09:52:09 -04:00
|
|
|
Runnable r = new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2013-09-16 06:16:54 -04:00
|
|
|
decryptAndVerifySafe(inputBytes, allowUserInteraction, callback, settings);
|
2013-06-17 09:52:09 -04:00
|
|
|
} catch (RemoteException e) {
|
2013-09-15 09:20:15 -04:00
|
|
|
Log.e(Constants.TAG, "OpenPgpService", e);
|
2013-06-17 09:52:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
checkAndEnqueue(r);
|
2013-05-28 09:10:36 -04:00
|
|
|
}
|
|
|
|
|
2013-06-17 09:52:09 -04:00
|
|
|
};
|
|
|
|
|
2013-05-28 09:10:36 -04:00
|
|
|
}
|