sign and encrypt basically working

This commit is contained in:
Dominik Schürmann 2013-09-06 18:54:55 +02:00
parent d6dd81d444
commit 227b277931
5 changed files with 89 additions and 62 deletions

View File

@ -41,12 +41,10 @@ interface ICryptoService {
* *
* @param inputBytes * @param inputBytes
* Byte array you want to encrypt * Byte array you want to encrypt
* @param signatureUserId
* User Ids (email) of sender
* @param callback * @param callback
* Callback where to return results * 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 * Encrypt and sign
@ -60,7 +58,7 @@ interface ICryptoService {
* @param callback * @param callback
* Callback where to return results * 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 * Decrypts and verifies given input bytes. If no signature is present this method

View File

@ -49,7 +49,6 @@ public class CryptoProviderDemoActivity extends Activity {
EditText mMessage; EditText mMessage;
EditText mCiphertext; EditText mCiphertext;
EditText mEncryptUserId; EditText mEncryptUserId;
EditText mSignUserId;
private CryptoServiceConnection mCryptoServiceConnection; private CryptoServiceConnection mCryptoServiceConnection;
@ -136,8 +135,7 @@ public class CryptoProviderDemoActivity extends Activity {
byte[] inputBytes = mMessage.getText().toString().getBytes(); byte[] inputBytes = mMessage.getText().toString().getBytes();
try { try {
mCryptoServiceConnection.getService().sign(inputBytes, mCryptoServiceConnection.getService().sign(inputBytes, encryptCallback);
mSignUserId.getText().toString(), encryptCallback);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(Constants.TAG, "CryptoProviderDemo", e); Log.e(Constants.TAG, "CryptoProviderDemo", e);
} }
@ -148,8 +146,7 @@ public class CryptoProviderDemoActivity extends Activity {
try { try {
mCryptoServiceConnection.getService().encryptAndSign(inputBytes, mCryptoServiceConnection.getService().encryptAndSign(inputBytes,
new String[] { mEncryptUserId.getText().toString() }, new String[] { mEncryptUserId.getText().toString() }, encryptCallback);
mSignUserId.getText().toString(), encryptCallback);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(Constants.TAG, "CryptoProviderDemo", e); Log.e(Constants.TAG, "CryptoProviderDemo", e);
} }

View File

@ -41,12 +41,10 @@ interface ICryptoService {
* *
* @param inputBytes * @param inputBytes
* Byte array you want to encrypt * Byte array you want to encrypt
* @param signatureUserId
* User Ids (email) of sender
* @param callback * @param callback
* Callback where to return results * 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 * Encrypt and sign
@ -60,7 +58,7 @@ interface ICryptoService {
* @param callback * @param callback
* Callback where to return results * 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 * Decrypts and verifies given input bytes. If no signature is present this method

View File

@ -23,9 +23,9 @@ public class AppSettings {
private String packageName; private String packageName;
private long keyId = Id.key.none; private long keyId = Id.key.none;
private boolean asciiArmor; private boolean asciiArmor;
private int encryptionAlgorithm = 7; // AES-128 private int encryptionAlgorithm;
private int hashAlgorithm = 10; // SHA-512 private int hashAlgorithm;
private int compression = 2; // zlib private int compression;
public AppSettings() { public AppSettings() {
@ -34,6 +34,11 @@ public class AppSettings {
public AppSettings(String packageName) { public AppSettings(String packageName) {
super(); super();
this.packageName = packageName; this.packageName = packageName;
// defaults:
this.asciiArmor = true;
this.encryptionAlgorithm = 7; // AES-128
this.hashAlgorithm = 10; // SHA-512
this.compression = 2; // zlib
} }
public String getPackageName() { public String getPackageName() {

View File

@ -109,16 +109,13 @@ public class CryptoService extends Service {
return passphrase; return passphrase;
} }
private synchronized void encryptSafe(byte[] inputBytes, String[] encryptionUserIds, /**
ICryptoCallback callback, AppSettings appSettings) throws RemoteException { * Search database for key ids based on emails.
try { *
// build InputData and write into OutputStream * @param encryptionUserIds
InputStream inputStream = new ByteArrayInputStream(inputBytes); * @return
long inputLength = inputBytes.length; */
InputData inputData = new InputData(inputStream, inputLength); private ArrayList<Long> getKeyIdsFromEmails(String[] encryptionUserIds) {
OutputStream outputStream = new ByteArrayOutputStream();
// find key ids to given emails in database // find key ids to given emails in database
boolean manySameUserIds = false; boolean manySameUserIds = false;
boolean missingUserIds = false; boolean missingUserIds = false;
@ -127,8 +124,7 @@ public class CryptoService extends Service {
Uri uri = KeychainContract.KeyRings.buildPublicKeyRingsByEmailsUri(email); Uri uri = KeychainContract.KeyRings.buildPublicKeyRingsByEmailsUri(email);
Cursor cur = getContentResolver().query(uri, null, null, null, null); Cursor cur = getContentResolver().query(uri, null, null, null, null);
if (cur.moveToFirst()) { if (cur.moveToFirst()) {
long id = cur.getLong(cur long id = cur.getLong(cur.getColumnIndex(KeychainContract.KeyRings.MASTER_KEY_ID));
.getColumnIndex(KeychainContract.KeyRings.MASTER_KEY_ID));
keyIds.add(id); keyIds.add(id);
} else { } else {
missingUserIds = true; missingUserIds = true;
@ -140,13 +136,43 @@ public class CryptoService extends Service {
} }
} }
// 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;
InputData inputData = new InputData(inputStream, inputLength);
OutputStream outputStream = new ByteArrayOutputStream();
ArrayList<Long> keyIds = getKeyIdsFromEmails(encryptionUserIds);
// also encrypt to our self (so that we can decrypt it later!) // also encrypt to our self (so that we can decrypt it later!)
keyIds.add(appSettings.getKeyId()); keyIds.add(appSettings.getKeyId());
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, PgpMain.encryptAndSign(mContext, null, inputData, outputStream,
appSettings.isAsciiArmor(), appSettings.getCompression(), keyIds, null, appSettings.isAsciiArmor(), appSettings.getCompression(), keyIds, null,
appSettings.getEncryptionAlgorithm(), Id.key.none, appSettings.getEncryptionAlgorithm(), Id.key.none,
appSettings.getHashAlgorithm(), true, null); appSettings.getHashAlgorithm(), true, null);
}
outputStream.close(); outputStream.close();
@ -163,20 +189,12 @@ public class CryptoService extends Service {
Log.e(Constants.TAG, "Error returning exception to client", t); Log.e(Constants.TAG, "Error returning exception to client", t);
} }
} }
} }
private synchronized void encryptAndSignSafe(byte[] inputBytes, String[] encryptionUserIds, private void signSafe(byte[] inputBytes, ICryptoCallback callback, AppSettings appSettings)
String signatureUserId, ICryptoCallback callback, AppSettings appSettings)
throws RemoteException { throws RemoteException {
// TODO!
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);
} }
private synchronized void decryptAndVerifySafe(byte[] inputBytes, ICryptoCallback callback, private synchronized void decryptAndVerifySafe(byte[] inputBytes, ICryptoCallback callback,
@ -258,7 +276,7 @@ public class CryptoService extends Service {
@Override @Override
public void run() { public void run() {
try { try {
encryptSafe(inputBytes, encryptionUserIds, callback, settings); encryptAndSignSafe(inputBytes, encryptionUserIds, callback, settings, false);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(Constants.TAG, "CryptoService", e); Log.e(Constants.TAG, "CryptoService", e);
} }
@ -270,8 +288,7 @@ public class CryptoService extends Service {
@Override @Override
public void encryptAndSign(final byte[] inputBytes, final String[] encryptionUserIds, public void encryptAndSign(final byte[] inputBytes, final String[] encryptionUserIds,
final String signatureUserId, final ICryptoCallback callback) final ICryptoCallback callback) throws RemoteException {
throws RemoteException {
final AppSettings settings = getAppSettings(); final AppSettings settings = getAppSettings();
@ -280,8 +297,7 @@ public class CryptoService extends Service {
@Override @Override
public void run() { public void run() {
try { try {
encryptAndSignSafe(inputBytes, encryptionUserIds, signatureUserId, encryptAndSignSafe(inputBytes, encryptionUserIds, callback, settings, true);
callback, settings);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(Constants.TAG, "CryptoService", e); Log.e(Constants.TAG, "CryptoService", e);
} }
@ -289,13 +305,26 @@ public class CryptoService extends Service {
}; };
checkAndEnqueue(r); checkAndEnqueue(r);
} }
@Override @Override
public void sign(byte[] inputBytes, String signatureUserId, ICryptoCallback callback) public void sign(final byte[] inputBytes, final ICryptoCallback callback)
throws RemoteException { 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);
} }