Use bytes to share via safeslinger, import on result

This commit is contained in:
Dominik Schürmann 2014-09-04 13:44:01 +02:00
parent d0f46f1efb
commit 4872fd5b2d

View File

@ -55,6 +55,7 @@ import org.sufficientlysecure.keychain.util.Notify;
import org.sufficientlysecure.keychain.util.QrCodeUtils; import org.sufficientlysecure.keychain.util.QrCodeUtils;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import edu.cmu.cylab.starslinger.exchange.ExchangeActivity; import edu.cmu.cylab.starslinger.exchange.ExchangeActivity;
import edu.cmu.cylab.starslinger.exchange.ExchangeConfig; import edu.cmu.cylab.starslinger.exchange.ExchangeConfig;
@ -82,6 +83,8 @@ public class ViewKeyShareFragment extends LoaderFragment implements
private static final int LOADER_ID_UNIFIED = 0; private static final int LOADER_ID_UNIFIED = 0;
private static final int REQUEST_CODE_SAFESLINGER = 1;
private Uri mDataUri; private Uri mDataUri;
@Override @Override
@ -170,7 +173,8 @@ public class ViewKeyShareFragment extends LoaderFragment implements
private void share(Uri dataUri, ProviderHelper providerHelper, boolean fingerprintOnly, private void share(Uri dataUri, ProviderHelper providerHelper, boolean fingerprintOnly,
boolean toClipboard, boolean toSafeSlinger) { boolean toClipboard, boolean toSafeSlinger) {
try { try {
String content; String content = null;
byte[] keyBlob = null;
if (fingerprintOnly) { if (fingerprintOnly) {
byte[] data = (byte[]) providerHelper.getGenericData( byte[] data = (byte[]) providerHelper.getGenericData(
KeyRings.buildUnifiedKeyRingUri(dataUri), KeyRings.buildUnifiedKeyRingUri(dataUri),
@ -182,10 +186,15 @@ public class ViewKeyShareFragment extends LoaderFragment implements
content = fingerprint; content = fingerprint;
} }
} else { } else {
// get public keyring as ascii armored string
Uri uri = KeychainContract.KeyRingData.buildPublicKeyRingUri(dataUri); Uri uri = KeychainContract.KeyRingData.buildPublicKeyRingUri(dataUri);
if (toSafeSlinger) {
keyBlob = (byte[]) providerHelper.getGenericData(
uri, KeychainContract.KeyRingData.KEY_RING_DATA, ProviderHelper.FIELD_TYPE_BLOB);
} else {
// get public keyring as ascii armored string
content = providerHelper.getKeyRingAsArmoredString(uri); content = providerHelper.getKeyRingAsArmoredString(uri);
} }
}
if (toClipboard) { if (toClipboard) {
ClipboardReflection.copyToClipboard(getActivity(), content); ClipboardReflection.copyToClipboard(getActivity(), content);
@ -198,9 +207,9 @@ public class ViewKeyShareFragment extends LoaderFragment implements
Notify.showNotify(getActivity(), message, Notify.Style.OK); Notify.showNotify(getActivity(), message, Notify.Style.OK);
} else if (toSafeSlinger) { } else if (toSafeSlinger) {
Intent slingerIntent = new Intent(getActivity(), ExchangeActivity.class); Intent slingerIntent = new Intent(getActivity(), ExchangeActivity.class);
slingerIntent.putExtra(ExchangeConfig.extra.USER_DATA, content.getBytes("UTF-8")); slingerIntent.putExtra(ExchangeConfig.extra.USER_DATA, keyBlob);
slingerIntent.putExtra(ExchangeConfig.extra.HOST_NAME, Constants.SAFESLINGER_SERVER); slingerIntent.putExtra(ExchangeConfig.extra.HOST_NAME, Constants.SAFESLINGER_SERVER);
startActivity(slingerIntent); startActivityForResult(slingerIntent, REQUEST_CODE_SAFESLINGER);
} else { } else {
// Android will fail with android.os.TransactionTooLargeException if key is too big // Android will fail with android.os.TransactionTooLargeException if key is too big
// see http://www.lonestarprod.com/?p=34 // see http://www.lonestarprod.com/?p=34
@ -234,6 +243,48 @@ public class ViewKeyShareFragment extends LoaderFragment implements
} }
} }
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE_SAFESLINGER:
switch (resultCode) {
case ExchangeActivity.RESULT_EXCHANGE_OK:
// use newly exchanged data from 'theirSecrets'
ArrayList<byte[]> theirSecrets = endExchange(data);
Intent importIntent = new Intent(getActivity(), ImportKeysActivity.class);
importIntent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY);
// TODO
importIntent.putExtra(ImportKeysActivity.EXTRA_KEY_BYTES, theirSecrets.get(0));
startActivity(importIntent);
break;
case ExchangeActivity.RESULT_EXCHANGE_CANCELED:
// handle canceled result
// ...
break;
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private static ArrayList<byte[]> endExchange(Intent data) {
ArrayList<byte[]> theirSecrets = new ArrayList<byte[]>();
Bundle extras = data.getExtras();
if (extras != null) {
byte[] d = null;
int i = 0;
do {
d = extras.getByteArray(ExchangeConfig.extra.MEMBER_DATA + i);
if (d != null) {
theirSecrets.add(d);
i++;
}
} while (d != null);
}
return theirSecrets;
}
private void showQrCodeDialog() { private void showQrCodeDialog() {
Intent qrCodeIntent = new Intent(getActivity(), QrCodeActivity.class); Intent qrCodeIntent = new Intent(getActivity(), QrCodeActivity.class);
qrCodeIntent.setData(mDataUri); qrCodeIntent.setData(mDataUri);