diff --git a/OpenPGP-Keychain-API-Demo/src/org/openintents/crypto/ICryptoService.aidl b/OpenPGP-Keychain-API-Demo/src/org/openintents/crypto/ICryptoService.aidl index c84ca28fb..b74ab642c 100644 --- a/OpenPGP-Keychain-API-Demo/src/org/openintents/crypto/ICryptoService.aidl +++ b/OpenPGP-Keychain-API-Demo/src/org/openintents/crypto/ICryptoService.aidl @@ -41,12 +41,10 @@ interface ICryptoService { * * @param inputBytes * Byte array you want to encrypt - * @param signatureUserId - * User Ids (email) of sender * @param callback * Callback where to return results */ - oneway void sign(in byte[] inputBytes, String signatureUserId, in ICryptoCallback callback); + oneway void sign(in byte[] inputBytes, in ICryptoCallback callback); /** * Encrypt and sign @@ -60,7 +58,7 @@ interface ICryptoService { * @param callback * Callback where to return results */ - oneway void encryptAndSign(in byte[] inputBytes, in String[] encryptionUserIds, String signatureUserId, in ICryptoCallback callback); + oneway void encryptAndSign(in byte[] inputBytes, in String[] encryptionUserIds, in ICryptoCallback callback); /** * Decrypts and verifies given input bytes. If no signature is present this method diff --git a/OpenPGP-Keychain-API-Demo/src/org/sufficientlysecure/keychain/demo/CryptoProviderDemoActivity.java b/OpenPGP-Keychain-API-Demo/src/org/sufficientlysecure/keychain/demo/CryptoProviderDemoActivity.java index 319820d7c..ca8824815 100644 --- a/OpenPGP-Keychain-API-Demo/src/org/sufficientlysecure/keychain/demo/CryptoProviderDemoActivity.java +++ b/OpenPGP-Keychain-API-Demo/src/org/sufficientlysecure/keychain/demo/CryptoProviderDemoActivity.java @@ -49,7 +49,6 @@ public class CryptoProviderDemoActivity extends Activity { EditText mMessage; EditText mCiphertext; EditText mEncryptUserId; - EditText mSignUserId; private CryptoServiceConnection mCryptoServiceConnection; @@ -136,8 +135,7 @@ public class CryptoProviderDemoActivity extends Activity { byte[] inputBytes = mMessage.getText().toString().getBytes(); try { - mCryptoServiceConnection.getService().sign(inputBytes, - mSignUserId.getText().toString(), encryptCallback); + mCryptoServiceConnection.getService().sign(inputBytes, encryptCallback); } catch (RemoteException e) { Log.e(Constants.TAG, "CryptoProviderDemo", e); } @@ -148,8 +146,7 @@ public class CryptoProviderDemoActivity extends Activity { try { mCryptoServiceConnection.getService().encryptAndSign(inputBytes, - new String[] { mEncryptUserId.getText().toString() }, - mSignUserId.getText().toString(), encryptCallback); + new String[] { mEncryptUserId.getText().toString() }, encryptCallback); } catch (RemoteException e) { Log.e(Constants.TAG, "CryptoProviderDemo", e); } diff --git a/OpenPGP-Keychain/src/org/openintents/crypto/ICryptoService.aidl b/OpenPGP-Keychain/src/org/openintents/crypto/ICryptoService.aidl index c84ca28fb..b74ab642c 100644 --- a/OpenPGP-Keychain/src/org/openintents/crypto/ICryptoService.aidl +++ b/OpenPGP-Keychain/src/org/openintents/crypto/ICryptoService.aidl @@ -41,12 +41,10 @@ interface ICryptoService { * * @param inputBytes * Byte array you want to encrypt - * @param signatureUserId - * User Ids (email) of sender * @param callback * Callback where to return results */ - oneway void sign(in byte[] inputBytes, String signatureUserId, in ICryptoCallback callback); + oneway void sign(in byte[] inputBytes, in ICryptoCallback callback); /** * Encrypt and sign @@ -60,7 +58,7 @@ interface ICryptoService { * @param callback * Callback where to return results */ - oneway void encryptAndSign(in byte[] inputBytes, in String[] encryptionUserIds, String signatureUserId, in ICryptoCallback callback); + oneway void encryptAndSign(in byte[] inputBytes, in String[] encryptionUserIds, in ICryptoCallback callback); /** * Decrypts and verifies given input bytes. If no signature is present this method diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/AppSettings.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/AppSettings.java index cd84e4ee2..e0d63d461 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/AppSettings.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/AppSettings.java @@ -23,9 +23,9 @@ public class AppSettings { private String packageName; private long keyId = Id.key.none; private boolean asciiArmor; - private int encryptionAlgorithm = 7; // AES-128 - private int hashAlgorithm = 10; // SHA-512 - private int compression = 2; // zlib + private int encryptionAlgorithm; + private int hashAlgorithm; + private int compression; public AppSettings() { @@ -34,6 +34,11 @@ public class AppSettings { public AppSettings(String packageName) { super(); this.packageName = packageName; + // defaults: + this.asciiArmor = true; + this.encryptionAlgorithm = 7; // AES-128 + this.hashAlgorithm = 10; // SHA-512 + this.compression = 2; // zlib } public String getPackageName() { diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoService.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoService.java index a86e6a1d6..74d4a7306 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoService.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoService.java @@ -109,9 +109,47 @@ public class CryptoService extends Service { return passphrase; } - private synchronized void encryptSafe(byte[] inputBytes, String[] encryptionUserIds, - ICryptoCallback callback, AppSettings appSettings) throws RemoteException { + /** + * Search database for key ids based on emails. + * + * @param encryptionUserIds + * @return + */ + private ArrayList getKeyIdsFromEmails(String[] encryptionUserIds) { + // find key ids to given emails in database + boolean manySameUserIds = false; + boolean missingUserIds = false; + ArrayList keyIds = new ArrayList(); + 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 { + missingUserIds = true; + Log.d(Constants.TAG, "user id missing"); + } + if (cur.moveToNext()) { + manySameUserIds = true; + Log.d(Constants.TAG, "more than one user id with the same email"); + } + } + + // TODO: show selection activity on missingUserIds or manySameUserIds + + return keyIds; + } + + private synchronized void encryptAndSignSafe(byte[] inputBytes, String[] encryptionUserIds, + ICryptoCallback callback, AppSettings appSettings, boolean sign) throws RemoteException { + try { + String passphrase = null; + if (sign) { + passphrase = getCachedPassphrase(appSettings.getKeyId()); + } + // build InputData and write into OutputStream InputStream inputStream = new ByteArrayInputStream(inputBytes); long inputLength = inputBytes.length; @@ -119,34 +157,22 @@ public class CryptoService extends Service { OutputStream outputStream = new ByteArrayOutputStream(); - // find key ids to given emails in database - boolean manySameUserIds = false; - boolean missingUserIds = false; - ArrayList keyIds = new ArrayList(); - 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 { - missingUserIds = true; - Log.d(Constants.TAG, "user id missing"); - } - if (cur.moveToNext()) { - manySameUserIds = true; - Log.d(Constants.TAG, "more than one user id with the same email"); - } - } - + ArrayList keyIds = getKeyIdsFromEmails(encryptionUserIds); + // also encrypt to our self (so that we can decrypt it later!) keyIds.add(appSettings.getKeyId()); - PgpMain.encryptAndSign(mContext, null, inputData, outputStream, - appSettings.isAsciiArmor(), appSettings.getCompression(), keyIds, null, - appSettings.getEncryptionAlgorithm(), Id.key.none, - appSettings.getHashAlgorithm(), true, null); + if (sign) { + PgpMain.encryptAndSign(mContext, null, inputData, outputStream, + appSettings.isAsciiArmor(), appSettings.getCompression(), keyIds, null, + appSettings.getEncryptionAlgorithm(), appSettings.getKeyId(), + appSettings.getHashAlgorithm(), true, passphrase); + } else { + PgpMain.encryptAndSign(mContext, null, inputData, outputStream, + appSettings.isAsciiArmor(), appSettings.getCompression(), keyIds, null, + appSettings.getEncryptionAlgorithm(), Id.key.none, + appSettings.getHashAlgorithm(), true, null); + } outputStream.close(); @@ -163,20 +189,12 @@ public class CryptoService extends Service { Log.e(Constants.TAG, "Error returning exception to client", t); } } + } - private synchronized void encryptAndSignSafe(byte[] inputBytes, String[] encryptionUserIds, - String signatureUserId, ICryptoCallback callback, AppSettings appSettings) + private void signSafe(byte[] inputBytes, ICryptoCallback callback, AppSettings appSettings) throws RemoteException { - - String passphrase = getCachedPassphrase(appSettings.getKeyId()); - - // PgpMain.encryptAndSign(this, this, inputData, outputStream, - // appSettings.isAsciiArmor(), - // appSettings.getCompression(), encryptionKeyIds, encryptionPassphrase, - // appSettings.getEncryptionAlgorithm(), appSettings.getKeyId(), - // appSettings.getHashAlgorithm(), true, passphrase); - + // TODO! } private synchronized void decryptAndVerifySafe(byte[] inputBytes, ICryptoCallback callback, @@ -258,7 +276,7 @@ public class CryptoService extends Service { @Override public void run() { try { - encryptSafe(inputBytes, encryptionUserIds, callback, settings); + encryptAndSignSafe(inputBytes, encryptionUserIds, callback, settings, false); } catch (RemoteException e) { Log.e(Constants.TAG, "CryptoService", e); } @@ -270,8 +288,7 @@ public class CryptoService extends Service { @Override public void encryptAndSign(final byte[] inputBytes, final String[] encryptionUserIds, - final String signatureUserId, final ICryptoCallback callback) - throws RemoteException { + final ICryptoCallback callback) throws RemoteException { final AppSettings settings = getAppSettings(); @@ -280,8 +297,7 @@ public class CryptoService extends Service { @Override public void run() { try { - encryptAndSignSafe(inputBytes, encryptionUserIds, signatureUserId, - callback, settings); + encryptAndSignSafe(inputBytes, encryptionUserIds, callback, settings, true); } catch (RemoteException e) { Log.e(Constants.TAG, "CryptoService", e); } @@ -289,13 +305,26 @@ public class CryptoService extends Service { }; checkAndEnqueue(r); - } @Override - public void sign(byte[] inputBytes, String signatureUserId, ICryptoCallback callback) + public void sign(final byte[] inputBytes, final ICryptoCallback callback) throws RemoteException { - // TODO Auto-generated method stub + final AppSettings settings = getAppSettings(); + + Runnable r = new Runnable() { + + @Override + public void run() { + try { + signSafe(inputBytes, callback, settings); + } catch (RemoteException e) { + Log.e(Constants.TAG, "CryptoService", e); + } + } + }; + + checkAndEnqueue(r); }