Exception handling with keyserver queries

This commit is contained in:
Dominik Schürmann 2014-07-31 17:56:47 +02:00
parent 33172d598d
commit cc034a0913
2 changed files with 14 additions and 6 deletions

View File

@ -22,6 +22,7 @@ import de.measite.minidns.Client;
import de.measite.minidns.Question; import de.measite.minidns.Question;
import de.measite.minidns.Record; import de.measite.minidns.Record;
import de.measite.minidns.record.SRV; import de.measite.minidns.record.SRV;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.helper.TlsHelper; import org.sufficientlysecure.keychain.helper.TlsHelper;
import org.sufficientlysecure.keychain.pgp.PgpHelper; import org.sufficientlysecure.keychain.pgp.PgpHelper;
@ -102,8 +103,9 @@ public class HkpKeyserver extends Keyserver {
*/ */
public static final Pattern PUB_KEY_LINE = Pattern 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 .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 + "((uid:([^:]*):([0-9]+):([0-9]*):([rde]*)[ \n\r]*)+)", // one or more uid lines
Pattern.CASE_INSENSITIVE); Pattern.CASE_INSENSITIVE
);
/** /**
* uid:%escaped uid string%:%creationdate%:%expirationdate%:%flags% * uid:%escaped uid string%:%creationdate%:%expirationdate%:%flags%
@ -241,8 +243,10 @@ public class HkpKeyserver extends Keyserver {
data = query(request); data = query(request);
} catch (HttpError e) { } catch (HttpError e) {
if (e.getCode() == 404) { if (e.getCode() == 404) {
return results; throw new QueryFailedException("keyserver '" + mHost + "' not found. Error 404");
} else { } 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")) { if (e.getData().toLowerCase(Locale.US).contains("no keys found")) {
return results; return results;
} else if (e.getData().toLowerCase(Locale.US).contains("too many")) { } else if (e.getData().toLowerCase(Locale.US).contains("too many")) {
@ -250,7 +254,11 @@ public class HkpKeyserver extends Keyserver {
} else if (e.getData().toLowerCase(Locale.US).contains("insufficient")) { } else if (e.getData().toLowerCase(Locale.US).contains("insufficient")) {
throw new QueryTooShortException(); throw new QueryTooShortException();
} }
// NOTE: some keyserver do not provide a more detailed error response
throw new QueryFailedException("Either no keys or too many have been found. Please improve your query!");
} }
throw new QueryFailedException("querying server(s) for '" + mHost + "' failed"); throw new QueryFailedException("querying server(s) for '" + mHost + "' failed");
} }
@ -291,7 +299,7 @@ public class HkpKeyserver extends Keyserver {
while (uidMatcher.find()) { while (uidMatcher.find()) {
String tmp = uidMatcher.group(1).trim(); String tmp = uidMatcher.group(1).trim();
if (tmp.contains("%")) { if (tmp.contains("%")) {
if(tmp.contains("%%")) { if (tmp.contains("%%")) {
// This is a fix for issue #683 // This is a fix for issue #683
// The server encodes a percent sign as %%, so it is swapped out with its // The server encodes a percent sign as %%, so it is swapped out with its
// urlencoded counterpart to prevent errors // urlencoded counterpart to prevent errors

View File

@ -271,7 +271,7 @@
<string name="error_nfc_needed">NFC is not available on your device!</string> <string name="error_nfc_needed">NFC is not available on your device!</string>
<string name="error_nothing_import">No keys found!</string> <string name="error_nothing_import">No keys found!</string>
<string name="error_keyserver_insufficient_query">Key search query too short</string> <string name="error_keyserver_insufficient_query">Key search query too short</string>
<string name="error_searching_keys">Unrecoverable error searching for keys at server</string> <string name="error_searching_keys">An error occurred when searching for keys</string>
<string name="error_keyserver_too_many_responses">Key search query returned too many candidates; Please refine query</string> <string name="error_keyserver_too_many_responses">Key search query returned too many candidates; Please refine query</string>
<string name="error_import_no_valid_keys">No valid keys found in File/Clipboard!</string> <string name="error_import_no_valid_keys">No valid keys found in File/Clipboard!</string>
<string name="error_generic_report_bug">A generic error occurred, please create a new bug report for OpenKeychain.</string> <string name="error_generic_report_bug">A generic error occurred, please create a new bug report for OpenKeychain.</string>