aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport
diff options
context:
space:
mode:
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java97
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java38
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java7
3 files changed, 132 insertions, 10 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java
new file mode 100644
index 000000000..08b8afae7
--- /dev/null
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java
@@ -0,0 +1,97 @@
+/*
+ * 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();
+ }
+ }
+}
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java
index 44679ba18..41f1e6997 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java
@@ -22,6 +22,7 @@ import de.measite.minidns.Client;
import de.measite.minidns.Question;
import de.measite.minidns.Record;
import de.measite.minidns.record.SRV;
+
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.helper.TlsHelper;
import org.sufficientlysecure.keychain.pgp.PgpHelper;
@@ -102,8 +103,9 @@ public class HkpKeyserver extends Keyserver {
*/
public static final Pattern PUB_KEY_LINE = Pattern
.compile("pub:([0-9a-fA-F]+):([0-9]+):([0-9]+):([0-9]+):([0-9]*):([rde]*)[ \n\r]*" // pub line
- + "((uid:([^:]*):([0-9]+):([0-9]*):([rde]*)[ \n\r]*)+)", // one or more uid lines
- Pattern.CASE_INSENSITIVE);
+ + "((uid:([^:]*):([0-9]+):([0-9]*):([rde]*)[ \n\r]*)+)", // one or more uid lines
+ Pattern.CASE_INSENSITIVE
+ );
/**
* uid:%escaped uid string%:%creationdate%:%expirationdate%:%flags%
@@ -215,10 +217,18 @@ public class HkpKeyserver extends Keyserver {
throw new HttpError(response, data);
}
} catch (IOException e) {
- throw new QueryFailedException("querying server(s) for '" + mHost + "' failed");
+ throw new QueryFailedException("Keyserver '" + mHost + "' is unavailable. Check your Internet connection!");
}
}
+ /**
+ * Results are sorted by creation date of key!
+ *
+ * @param query
+ * @return
+ * @throws QueryFailedException
+ * @throws QueryNeedsRepairException
+ */
@Override
public ArrayList<ImportKeysListEntry> search(String query) throws QueryFailedException,
QueryNeedsRepairException {
@@ -240,18 +250,26 @@ public class HkpKeyserver extends Keyserver {
try {
data = query(request);
} catch (HttpError e) {
- if (e.getCode() == 404) {
- return results;
- } else {
+ if (e.getData() != null) {
+ Log.d(Constants.TAG, "returned error data: " + e.getData().toLowerCase(Locale.US));
+
if (e.getData().toLowerCase(Locale.US).contains("no keys found")) {
+ // NOTE: This is also a 404 error for some keyservers!
return results;
} else if (e.getData().toLowerCase(Locale.US).contains("too many")) {
throw new TooManyResponsesException();
} else if (e.getData().toLowerCase(Locale.US).contains("insufficient")) {
throw new QueryTooShortException();
+ } else if (e.getCode() == 404) {
+ // NOTE: handle this 404 at last, maybe it was a "no keys found" error
+ throw new QueryFailedException("Keyserver '" + mHost + "' not found. Error 404");
+ } else {
+ // NOTE: some keyserver do not provide a more detailed error response
+ throw new QueryTooShortOrTooManyResponsesException();
}
}
- throw new QueryFailedException("querying server(s) for '" + mHost + "' failed");
+
+ throw new QueryFailedException("Querying server(s) for '" + mHost + "' failed.");
}
final Matcher matcher = PUB_KEY_LINE.matcher(data);
@@ -267,9 +285,9 @@ public class HkpKeyserver extends Keyserver {
// group 1 contains the full fingerprint (v4) or the long key id if available
// see http://bit.ly/1d4bxbk and http://bit.ly/1gD1wwr
- String fingerprintOrKeyId = matcher.group(1);
+ String fingerprintOrKeyId = matcher.group(1).toLowerCase(Locale.US);
if (fingerprintOrKeyId.length() > 16) {
- entry.setFingerprintHex(fingerprintOrKeyId.toLowerCase(Locale.US));
+ entry.setFingerprintHex(fingerprintOrKeyId);
entry.setKeyIdHex("0x" + fingerprintOrKeyId.substring(fingerprintOrKeyId.length()
- 16, fingerprintOrKeyId.length()));
} else {
@@ -291,7 +309,7 @@ public class HkpKeyserver extends Keyserver {
while (uidMatcher.find()) {
String tmp = uidMatcher.group(1).trim();
if (tmp.contains("%")) {
- if(tmp.contains("%%")) {
+ if (tmp.contains("%%")) {
// This is a fix for issue #683
// The server encodes a percent sign as %%, so it is swapped out with its
// urlencoded counterpart to prevent errors
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java
index 842e7d922..b726529f8 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java
@@ -44,6 +44,13 @@ public abstract class Keyserver {
private static final long serialVersionUID = 2703768928624654514L;
}
+ /**
+ * query too short _or_ too many responses
+ */
+ public static class QueryTooShortOrTooManyResponsesException extends QueryNeedsRepairException {
+ private static final long serialVersionUID = 2703768928624654514L;
+ }
+
public static class AddKeyException extends Exception {
private static final long serialVersionUID = -507574859137295530L;
}