Merge branch 'master' of github.com:open-keychain/open-keychain

This commit is contained in:
Dominik Schürmann 2014-08-01 17:54:10 +02:00
commit f72a07690c
14 changed files with 259 additions and 147 deletions

View File

@ -1,4 +1,4 @@
package org.sufficientlysecure.keychain.tests;
package org.sufficientlysecure.keychain.pgp;
import junit.framework.AssertionFailedError;
@ -19,12 +19,6 @@ import org.spongycastle.bcpg.sig.KeyFlags;
import org.spongycastle.openpgp.PGPSignature;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.Constants.choice.algorithm;
import org.sufficientlysecure.keychain.pgp.CanonicalizedKeyRing;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing;
import org.sufficientlysecure.keychain.pgp.PgpKeyOperation;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
import org.sufficientlysecure.keychain.pgp.UncachedPublicKey;
import org.sufficientlysecure.keychain.pgp.WrappedSignature;
import org.sufficientlysecure.keychain.service.OperationResultParcel.OperationLog;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyAdd;
@ -40,6 +34,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
@RunWith(RobolectricTestRunner.class)
@ -208,8 +203,8 @@ public class PgpKeyOperationTest {
Assert.assertEquals("number of user ids must be two",
2, ring.getPublicKey().getUnorderedUserIds().size());
Assert.assertEquals("number of subkeys must be three",
3, KeyringTestingHelper.itToList(ring.getPublicKeys()).size());
List<UncachedPublicKey> subkeys = KeyringTestingHelper.itToList(ring.getPublicKeys());
Assert.assertEquals("number of subkeys must be three", 3, subkeys.size());
Assert.assertTrue("key ring should have been created in the last 120 seconds",
ring.getPublicKey().getCreationTime().after(new Date(new Date().getTime()-1000*120)));
@ -217,24 +212,21 @@ public class PgpKeyOperationTest {
Assert.assertNull("key ring should not expire",
ring.getPublicKey().getExpiryTime());
Iterator<UncachedPublicKey> it = ring.getPublicKeys();
Assert.assertEquals("first (master) key can certify",
KeyFlags.CERTIFY_OTHER, it.next().getKeyUsage());
KeyFlags.CERTIFY_OTHER, subkeys.get(0).getKeyUsage());
UncachedPublicKey signingKey = it.next();
Assert.assertEquals("second key can sign",
KeyFlags.SIGN_DATA, signingKey.getKeyUsage());
ArrayList<WrappedSignature> sigs = signingKey.getSignatures().next().getEmbeddedSignatures();
KeyFlags.SIGN_DATA, subkeys.get(1).getKeyUsage());
ArrayList<WrappedSignature> sigs = subkeys.get(1).getSignatures().next().getEmbeddedSignatures();
Assert.assertEquals("signing key signature should have one embedded signature",
1, sigs.size());
Assert.assertEquals("embedded signature should be of primary key binding type",
PGPSignature.PRIMARYKEY_BINDING, sigs.get(0).getSignatureType());
Assert.assertEquals("primary key binding signature issuer should be signing subkey",
signingKey.getKeyId(), sigs.get(0).getKeyId());
subkeys.get(1).getKeyId(), sigs.get(0).getKeyId());
Assert.assertEquals("third key can encrypt",
KeyFlags.ENCRYPT_COMMS, it.next().getKeyUsage());
KeyFlags.ENCRYPT_COMMS, subkeys.get(2).getKeyUsage());
}

View File

@ -1,4 +1,4 @@
package org.sufficientlysecure.keychain.tests;
package org.sufficientlysecure.keychain.pgp;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
@ -26,11 +26,6 @@ import org.spongycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
import org.spongycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder;
import org.spongycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.pgp.CanonicalizedKeyRing;
import org.sufficientlysecure.keychain.pgp.PgpKeyOperation;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
import org.sufficientlysecure.keychain.pgp.UncachedPublicKey;
import org.sufficientlysecure.keychain.pgp.WrappedSignature;
import org.sufficientlysecure.keychain.service.OperationResultParcel;
import org.sufficientlysecure.keychain.service.OperationResults.EditKeyResult;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel;

View File

@ -1,4 +1,4 @@
package org.sufficientlysecure.keychain.tests;
package org.sufficientlysecure.keychain.pgp;
import org.junit.Assert;
import org.junit.Before;
@ -10,13 +10,6 @@ import org.robolectric.shadows.ShadowLog;
import org.spongycastle.bcpg.PacketTags;
import org.spongycastle.bcpg.sig.KeyFlags;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing;
import org.sufficientlysecure.keychain.pgp.PgpKeyOperation;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
import org.sufficientlysecure.keychain.pgp.UncachedPublicKey;
import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing;
import org.sufficientlysecure.keychain.service.OperationResultParcel;
import org.sufficientlysecure.keychain.service.OperationResults.EditKeyResult;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel;

View File

@ -1,4 +1,4 @@
package org.sufficientlysecure.keychain.tests;
package org.sufficientlysecure.keychain.pgp;
import org.junit.Assert;
import org.junit.Before;
@ -9,21 +9,15 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadows.ShadowLog;
import org.spongycastle.bcpg.sig.KeyFlags;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.pgp.PgpKeyOperation;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
import org.sufficientlysecure.keychain.pgp.UncachedPublicKey;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.service.OperationResultParcel;
import org.sufficientlysecure.keychain.service.OperationResults.EditKeyResult;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel;
import org.sufficientlysecure.keychain.support.KeyringTestingHelper.RawPacket;
import org.sufficientlysecure.keychain.util.ProgressScaler;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@org.robolectric.annotation.Config(emulateSdk = 18) // Robolectric doesn't yet support 19

View File

@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package tests;
package org.sufficientlysecure.keychain.provider;
import java.util.Collections;
import java.util.Arrays;

View File

@ -0,0 +1,54 @@
package org.sufficientlysecure.keychain.util;
import android.os.Bundle;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadows.ShadowLog;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@org.robolectric.annotation.Config(emulateSdk = 18) // Robolectric doesn't yet support 19
public class FileImportCacheTest {
@Before
public void setUp() throws Exception {
ShadowLog.stream = System.out;
}
@Test
public void testInputOutput() throws Exception {
FileImportCache<Bundle> cache = new FileImportCache<Bundle>(Robolectric.application);
ArrayList<Bundle> list = new ArrayList<Bundle>();
for (int i = 0; i < 50; i++) {
Bundle b = new Bundle();
b.putInt("key1", i);
b.putString("key2", Integer.toString(i));
list.add(b);
}
// write to cache file
cache.writeCache(list);
// read back
List<Bundle> last = cache.readCacheIntoList();
for (int i = 0; i < list.size(); i++) {
Assert.assertEquals("input values should be equal to output values",
list.get(i).getInt("key1"), last.get(i).getInt("key1"));
Assert.assertEquals("input values should be equal to output values",
list.get(i).getString("key2"), last.get(i).getString("key2"));
}
}
}

View File

@ -61,6 +61,7 @@ public class KeychainApplication extends Application {
PRNGFixes.apply();
Log.d(Constants.TAG, "Bouncy Castle set and PRNG Fixes applied!");
/*
if (Constants.DEBUG) {
Provider[] providers = Security.getProviders();
Log.d(Constants.TAG, "Installed Security Providers:");
@ -68,6 +69,7 @@ public class KeychainApplication extends Application {
Log.d(Constants.TAG, "provider class: " + p.getClass().getName());
}
}
*/
// Create APG directory on sdcard if not existing
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

View File

@ -1,97 +0,0 @@
/*
* Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.keyimport;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcel;
import org.sufficientlysecure.keychain.KeychainApplication;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* When sending large data (over 1MB) through Androids Binder IPC you get
* JavaBinder E !!! FAILED BINDER TRANSACTION !!!
* <p/>
* To overcome this problem, we cache large Parcelables into a file in our private cache directory
* instead of sending them through IPC.
*/
public class FileImportCache {
private Context mContext;
private static final String FILENAME = "key_import.pcl";
private static final String BUNDLE_DATA = "data";
public FileImportCache(Context context) {
this.mContext = context;
}
public void writeCache(ArrayList<ParcelableKeyRing> selectedEntries) throws IOException {
Bundle in = new Bundle();
in.putParcelableArrayList(BUNDLE_DATA, selectedEntries);
File cacheDir = mContext.getCacheDir();
if (cacheDir == null) {
// https://groups.google.com/forum/#!topic/android-developers/-694j87eXVU
throw new IOException("cache dir is null!");
}
File tempFile = new File(mContext.getCacheDir(), FILENAME);
FileOutputStream fos = new FileOutputStream(tempFile);
Parcel p = Parcel.obtain(); // creating empty parcel object
in.writeToParcel(p, 0); // saving bundle as parcel
fos.write(p.marshall()); // writing parcel to file
fos.flush();
fos.close();
}
public List<ParcelableKeyRing> readCache() throws IOException {
Parcel parcel = Parcel.obtain(); // creating empty parcel object
Bundle out;
File cacheDir = mContext.getCacheDir();
if (cacheDir == null) {
// https://groups.google.com/forum/#!topic/android-developers/-694j87eXVU
throw new IOException("cache dir is null!");
}
File tempFile = new File(cacheDir, FILENAME);
try {
FileInputStream fis = new FileInputStream(tempFile);
byte[] array = new byte[(int) fis.getChannel().size()];
fis.read(array, 0, array.length);
fis.close();
parcel.unmarshall(array, 0, array.length);
parcel.setDataPosition(0);
out = parcel.readBundle(KeychainApplication.class.getClassLoader());
out.putAll(out);
return out.getParcelableArrayList(BUNDLE_DATA);
} finally {
parcel.recycle();
// delete temp file
tempFile.delete();
}
}
}

View File

@ -609,7 +609,7 @@ public class PgpKeyOperation {
for (int i = 0; i < saveParcel.mAddSubKeys.size(); i++) {
progress(R.string.progress_modify_subkeyadd, (i-1) * (100 / saveParcel.mAddSubKeys.size()));
SaveKeyringParcel.SubkeyAdd add = saveParcel.mAddSubKeys.get(0);
SaveKeyringParcel.SubkeyAdd add = saveParcel.mAddSubKeys.get(i);
log.add(LogLevel.INFO, LogType.MSG_MF_SUBKEY_NEW, indent);
if (add.mExpiry != null && new Date(add.mExpiry*1000).before(new Date())) {

View File

@ -169,6 +169,7 @@ public class UncachedPublicKey {
}
@SuppressWarnings("unchecked")
// TODO make this safe
public int getKeyUsage() {
if(mCacheUsage == null) {
mCacheUsage = 0;
@ -182,11 +183,6 @@ public class UncachedPublicKey {
if (hashed != null) {
mCacheUsage |= hashed.getKeyFlags();
}
PGPSignatureSubpacketVector unhashed = sig.getUnhashedSubPackets();
if (unhashed != null) {
mCacheUsage |= unhashed.getKeyFlags();
}
}
}
}

View File

@ -24,6 +24,7 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.Parcel;
import android.os.RemoteException;
import org.sufficientlysecure.keychain.Constants;
@ -31,7 +32,7 @@ import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.helper.FileHelper;
import org.sufficientlysecure.keychain.helper.OtherHelper;
import org.sufficientlysecure.keychain.helper.Preferences;
import org.sufficientlysecure.keychain.keyimport.FileImportCache;
import org.sufficientlysecure.keychain.util.FileImportCache;
import org.sufficientlysecure.keychain.keyimport.HkpKeyserver;
import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;
import org.sufficientlysecure.keychain.keyimport.KeybaseKeyserver;
@ -387,14 +388,16 @@ public class KeychainIntentService extends IntentService
}
} else if (ACTION_IMPORT_KEYRING.equals(action)) {
try {
List<ParcelableKeyRing> entries;
if (data.containsKey(IMPORT_KEY_LIST)) {
// get entries from intent
entries = data.getParcelableArrayList(IMPORT_KEY_LIST);
} else {
// get entries from cached file
FileImportCache cache = new FileImportCache(this);
entries = cache.readCache();
FileImportCache<ParcelableKeyRing> cache =
new FileImportCache<ParcelableKeyRing>(this);
entries = cache.readCacheIntoList();
}
PgpImportExport pgpImportExport = new PgpImportExport(this, this);
@ -523,6 +526,7 @@ public class KeychainIntentService extends IntentService
Intent importIntent = new Intent(this, KeychainIntentService.class);
importIntent.setAction(ACTION_IMPORT_KEYRING);
Bundle importData = new Bundle();
// This is not going through binder, nothing to fear of
importData.putParcelableArrayList(IMPORT_KEY_LIST, keyRings);

View File

@ -40,7 +40,7 @@ import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.helper.OtherHelper;
import org.sufficientlysecure.keychain.helper.Preferences;
import org.sufficientlysecure.keychain.keyimport.FileImportCache;
import org.sufficientlysecure.keychain.util.FileImportCache;
import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
@ -503,7 +503,7 @@ public class ImportKeysActivity extends ActionBarActivity {
// to prevent Java Binder problems on heavy imports
// read FileImportCache for more info.
try {
FileImportCache cache = new FileImportCache(this);
FileImportCache<ParcelableKeyRing> cache = new FileImportCache<ParcelableKeyRing>(this);
cache.writeCache(selectedEntries);
intent.putExtra(KeychainIntentService.EXTRA_DATA, data);

View File

@ -0,0 +1,179 @@
/*
* Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.util;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.KeychainApplication;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* When sending large data (over 1MB) through Androids Binder IPC you get
* JavaBinder E !!! FAILED BINDER TRANSACTION !!!
* <p/>
* To overcome this problem, we cache large Parcelables into a file in our private cache directory
* instead of sending them through IPC.
*/
public class FileImportCache<E extends Parcelable> {
private Context mContext;
private static final String FILENAME = "key_import.pcl";
public FileImportCache(Context context) {
this.mContext = context;
}
public void writeCache(ArrayList<E> selectedEntries) throws IOException {
writeCache(selectedEntries.iterator());
}
public void writeCache(Iterator<E> it) throws IOException {
File cacheDir = mContext.getCacheDir();
if (cacheDir == null) {
// https://groups.google.com/forum/#!topic/android-developers/-694j87eXVU
throw new IOException("cache dir is null!");
}
File tempFile = new File(mContext.getCacheDir(), FILENAME);
DataOutputStream oos = new DataOutputStream(new FileOutputStream(tempFile));
while (it.hasNext()) {
Parcel p = Parcel.obtain(); // creating empty parcel object
p.writeParcelable(it.next(), 0); // saving bundle as parcel
byte[] buf = p.marshall();
oos.writeInt(buf.length);
oos.write(buf);
p.recycle();
}
oos.close();
}
public List<E> readCacheIntoList() throws IOException {
ArrayList<E> result = new ArrayList<E>();
Iterator<E> it = readCache();
while (it.hasNext()) {
result.add(it.next());
}
return result;
}
public Iterator<E> readCache() throws IOException {
File cacheDir = mContext.getCacheDir();
if (cacheDir == null) {
// https://groups.google.com/forum/#!topic/android-developers/-694j87eXVU
throw new IOException("cache dir is null!");
}
final File tempFile = new File(cacheDir, FILENAME);
final DataInputStream ois = new DataInputStream(new FileInputStream(tempFile));
return new Iterator<E>() {
E mRing = null;
boolean closed = false;
byte[] buf = new byte[512];
private void readNext() {
if (mRing != null || closed) {
return;
}
try {
int length = ois.readInt();
while (buf.length < length) {
buf = new byte[buf.length * 2];
}
ois.readFully(buf, 0, length);
Parcel parcel = Parcel.obtain(); // creating empty parcel object
parcel.unmarshall(buf, 0, length);
parcel.setDataPosition(0);
mRing = parcel.readParcelable(KeychainApplication.class.getClassLoader());
parcel.recycle();
} catch (EOFException e) {
// aight
close();
} catch (IOException e) {
Log.e(Constants.TAG, "Encountered IOException during cache read!", e);
}
}
@Override
public boolean hasNext() {
readNext();
return mRing != null;
}
@Override
public E next() {
readNext();
try {
return mRing;
} finally {
mRing = null;
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public void finalize() throws Throwable {
close();
super.finalize();
}
private void close() {
if (!closed) {
try {
ois.close();
tempFile.delete();
} catch (IOException e) {
// nvm
}
}
closed = true;
}
};
}
}