diff options
Diffstat (limited to 'OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/util/HkpKeyServer.java')
-rw-r--r-- | OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/util/HkpKeyServer.java | 131 |
1 files changed, 74 insertions, 57 deletions
diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/util/HkpKeyServer.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/util/HkpKeyServer.java index 9d6850027..b987e1533 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/util/HkpKeyServer.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/util/HkpKeyServer.java @@ -18,7 +18,6 @@ package org.sufficientlysecure.keychain.util; -import android.text.Html; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; @@ -30,6 +29,7 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; +import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.pgp.PgpHelper; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListEntry; @@ -72,26 +72,26 @@ public class HkpKeyServer extends KeyServer { /** * pub:%keyid%:%algo%:%keylen%:%creationdate%:%expirationdate%:%flags% * <ul> - * <li>%<b>keyid</b>% = this is either the fingerprint or the key ID of the key. Either the 16-digit or 8-digit - * key IDs are acceptable, but obviously the fingerprint is best.</li> - * <li>%<b>algo</b>% = the algorithm number, (i.e. 1==RSA, 17==DSA, etc). - * See <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a></li> - * <li>%<b>keylen</b>% = the key length (i.e. 1024, 2048, 4096, etc.)</li> - * <li>%<b>creationdate</b>% = creation date of the key in standard - * <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a> form (i.e. number of seconds since - * 1/1/1970 UTC time)</li> - * <li>%<b>expirationdate</b>% = expiration date of the key in standard - * <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a> form (i.e. number of seconds since - * 1/1/1970 UTC time)</li> - * <li>%<b>flags</b>% = letter codes to indicate details of the key, if any. Flags may be in any order. The - * meaning of "disabled" is implementation-specific. Note that individual flags may be unimplemented, so - * the absence of a given flag does not necessarily mean the absence of the detail. - * <ul> - * <li>r == revoked</li> - * <li>d == disabled</li> - * <li>e == expired</li> - * </ul> - * </li> + * <li>%<b>keyid</b>% = this is either the fingerprint or the key ID of the key. Either the 16-digit or 8-digit + * key IDs are acceptable, but obviously the fingerprint is best.</li> + * <li>%<b>algo</b>% = the algorithm number, (i.e. 1==RSA, 17==DSA, etc). + * See <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a></li> + * <li>%<b>keylen</b>% = the key length (i.e. 1024, 2048, 4096, etc.)</li> + * <li>%<b>creationdate</b>% = creation date of the key in standard + * <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a> form (i.e. number of seconds since + * 1/1/1970 UTC time)</li> + * <li>%<b>expirationdate</b>% = expiration date of the key in standard + * <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a> form (i.e. number of seconds since + * 1/1/1970 UTC time)</li> + * <li>%<b>flags</b>% = letter codes to indicate details of the key, if any. Flags may be in any order. The + * meaning of "disabled" is implementation-specific. Note that individual flags may be unimplemented, so + * the absence of a given flag does not necessarily mean the absence of the detail. + * <ul> + * <li>r == revoked</li> + * <li>d == disabled</li> + * <li>e == expired</li> + * </ul> + * </li> * </ul> * * @see <a href="http://tools.ietf.org/html/draft-shaw-openpgp-hkp-00#section-5.2">5.2. Machine Readable Indexes</a> @@ -105,23 +105,23 @@ public class HkpKeyServer extends KeyServer { /** * uid:%escaped uid string%:%creationdate%:%expirationdate%:%flags% * <ul> - * <li>%<b>escaped uid string</b>% = the user ID string, with HTTP %-escaping for anything that isn't 7-bit - * safe as well as for the ":" character. Any other characters may be escaped, as desired.</li> - * <li>%<b>creationdate</b>% = creation date of the key in standard - * <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a> form (i.e. number of seconds since - * 1/1/1970 UTC time)</li> - * <li>%<b>expirationdate</b>% = expiration date of the key in standard - * <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a> form (i.e. number of seconds since - * 1/1/1970 UTC time)</li> - * <li>%<b>flags</b>% = letter codes to indicate details of the key, if any. Flags may be in any order. The - * meaning of "disabled" is implementation-specific. Note that individual flags may be unimplemented, so - * the absence of a given flag does not necessarily mean the absence of the detail. - * <ul> - * <li>r == revoked</li> - * <li>d == disabled</li> - * <li>e == expired</li> - * </ul> - * </li> + * <li>%<b>escaped uid string</b>% = the user ID string, with HTTP %-escaping for anything that isn't 7-bit + * safe as well as for the ":" character. Any other characters may be escaped, as desired.</li> + * <li>%<b>creationdate</b>% = creation date of the key in standard + * <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a> form (i.e. number of seconds since + * 1/1/1970 UTC time)</li> + * <li>%<b>expirationdate</b>% = expiration date of the key in standard + * <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a> form (i.e. number of seconds since + * 1/1/1970 UTC time)</li> + * <li>%<b>flags</b>% = letter codes to indicate details of the key, if any. Flags may be in any order. The + * meaning of "disabled" is implementation-specific. Note that individual flags may be unimplemented, so + * the absence of a given flag does not necessarily mean the absence of the detail. + * <ul> + * <li>r == revoked</li> + * <li>d == disabled</li> + * <li>e == expired</li> + * </ul> + * </li> * </ul> */ public static final Pattern UID_LINE = Pattern @@ -179,6 +179,7 @@ public class HkpKeyServer extends KeyServer { for (int i = 0; i < ips.length; ++i) { try { String url = "http://" + ips[i].getHostAddress() + ":" + mPort + request; + Log.d(Constants.TAG, "hkp keyserver query: " + url); URL realUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(); conn.setConnectTimeout(5000); @@ -238,22 +239,34 @@ public class HkpKeyServer extends KeyServer { final Matcher matcher = PUB_KEY_LINE.matcher(data); while (matcher.find()) { - final ImportKeysListEntry info = new ImportKeysListEntry(); - info.bitStrength = Integer.parseInt(matcher.group(3)); - final int algorithmId = Integer.decode(matcher.group(2)); - info.algorithm = getAlgorithmFromId(algorithmId); + final ImportKeysListEntry entry = new ImportKeysListEntry(); + + entry.setBitStrength(Integer.parseInt(matcher.group(3))); - info.hexKeyId = "0x" + matcher.group(1); - info.keyId = PgpKeyHelper.convertHexToKeyId(matcher.group(1)); + final int algorithmId = Integer.decode(matcher.group(2)); + entry.setAlgorithm(getAlgorithmFromId(algorithmId)); + + // group 1 contains the full fingerprint (v4) or the long key id if available + // see https://bitbucket.org/skskeyserver/sks-keyserver/pull-request/12/fixes-for-machine-readable-indexes/diff + // and https://github.com/openpgp-keychain/openpgp-keychain/issues/259#issuecomment-38168176 + String fingerprintOrKeyId = matcher.group(1); + if (fingerprintOrKeyId.length() > 16) { + entry.setFingerPrintHex(fingerprintOrKeyId.toLowerCase(Locale.US)); + entry.setKeyIdHex("0x" + fingerprintOrKeyId.substring(fingerprintOrKeyId.length() + - 16, fingerprintOrKeyId.length())); + } else { + // set key id only + entry.setKeyIdHex("0x" + fingerprintOrKeyId); + } final long creationDate = Long.parseLong(matcher.group(4)); final GregorianCalendar tmpGreg = new GregorianCalendar(TimeZone.getTimeZone("UTC")); tmpGreg.setTimeInMillis(creationDate * 1000); - info.date = tmpGreg.getTime(); + entry.setDate(tmpGreg.getTime()); - info.revoked = matcher.group(6).contains("r"); - info.userIds = new ArrayList<String>(); + entry.setRevoked(matcher.group(6).contains("r")); + ArrayList<String> userIds = new ArrayList<String>(); final String uidLines = matcher.group(7); final Matcher uidMatcher = UID_LINE.matcher(uidLines); while (uidMatcher.find()) { @@ -266,20 +279,23 @@ public class HkpKeyServer extends KeyServer { // will never happen, because "UTF8" is supported } } - info.userIds.add(tmp); + userIds.add(tmp); } - results.add(info); + entry.setUserIds(userIds); + + results.add(entry); } return results; } @Override - public String get(long keyId) throws QueryException { + public String get(String keyIdHex) throws QueryException { HttpClient client = new DefaultHttpClient(); try { - HttpGet get = new HttpGet("http://" + mHost + ":" + mPort - + "/pks/lookup?op=get&options=mr&search=" + PgpKeyHelper.convertKeyIdToHex(keyId)); - + String query = "http://" + mHost + ":" + mPort + + "/pks/lookup?op=get&options=mr&search=" + keyIdHex; + Log.d(Constants.TAG, "hkp keyserver get: " + query); + HttpGet get = new HttpGet(query); HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new QueryException("not found"); @@ -302,13 +318,14 @@ public class HkpKeyServer extends KeyServer { } @Override - public void add(String armoredText) throws AddKeyException { + public void add(String armoredKey) throws AddKeyException { HttpClient client = new DefaultHttpClient(); try { - HttpPost post = new HttpPost("http://" + mHost + ":" + mPort + "/pks/add"); - + String query = "http://" + mHost + ":" + mPort + "/pks/add"; + HttpPost post = new HttpPost(query); + Log.d(Constants.TAG, "hkp keyserver add: " + query); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); - nameValuePairs.add(new BasicNameValuePair("keytext", armoredText)); + nameValuePairs.add(new BasicNameValuePair("keytext", armoredKey)); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = client.execute(post); |