2012-03-12 09:28:35 -04:00
|
|
|
/*
|
2012-06-20 07:49:57 -04:00
|
|
|
* Copyright (C) 2011 Senecaso
|
|
|
|
*
|
2012-03-12 09:28:35 -04:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-01-16 08:31:16 -05:00
|
|
|
package org.sufficientlysecure.keychain.util;
|
2010-08-16 21:02:39 -04:00
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2010-08-17 10:51:39 -04:00
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.InetAddress;
|
2010-08-16 21:02:39 -04:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLEncoder;
|
2010-08-17 10:51:39 -04:00
|
|
|
import java.net.UnknownHostException;
|
2011-10-16 21:07:37 -04:00
|
|
|
import java.util.ArrayList;
|
2010-08-16 21:02:39 -04:00
|
|
|
import java.util.GregorianCalendar;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2011-10-16 21:07:37 -04:00
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
import org.apache.http.HttpStatus;
|
|
|
|
import org.apache.http.NameValuePair;
|
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
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;
|
2013-09-15 09:29:38 -04:00
|
|
|
import org.sufficientlysecure.keychain.pgp.PgpHelper;
|
2013-09-15 10:42:08 -04:00
|
|
|
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
|
2011-10-16 21:07:37 -04:00
|
|
|
|
2010-08-16 21:02:39 -04:00
|
|
|
import android.text.Html;
|
|
|
|
|
|
|
|
public class HkpKeyServer extends KeyServer {
|
2010-08-17 10:51:39 -04:00
|
|
|
private static class HttpError extends Exception {
|
|
|
|
private static final long serialVersionUID = 1718783705229428893L;
|
|
|
|
private int mCode;
|
|
|
|
private String mData;
|
|
|
|
|
|
|
|
public HttpError(int code, String data) {
|
|
|
|
super("" + code + ": " + data);
|
|
|
|
mCode = code;
|
|
|
|
mData = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getCode() {
|
|
|
|
return mCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getData() {
|
|
|
|
return mData;
|
|
|
|
}
|
|
|
|
}
|
2011-10-16 21:07:37 -04:00
|
|
|
|
2010-08-16 21:02:39 -04:00
|
|
|
private String mHost;
|
|
|
|
private short mPort = 11371;
|
|
|
|
|
|
|
|
// example:
|
2012-03-09 10:27:29 -05:00
|
|
|
// pub 2048R/<a href="/pks/lookup?op=get&search=0x887DF4BE9F5C9090">9F5C9090</a> 2009-08-17 <a
|
|
|
|
// href="/pks/lookup?op=vindex&search=0x887DF4BE9F5C9090">Jörg Runge
|
|
|
|
// <joerg@joergrunge.de></a>
|
|
|
|
public static Pattern PUB_KEY_LINE = Pattern
|
|
|
|
.compile(
|
|
|
|
"pub +([0-9]+)([a-z]+)/.*?0x([0-9a-z]+).*? +([0-9-]+) +(.+)[\n\r]+((?: +.+[\n\r]+)*)",
|
|
|
|
Pattern.CASE_INSENSITIVE);
|
|
|
|
public static Pattern USER_ID_LINE = Pattern.compile("^ +(.+)$", Pattern.MULTILINE
|
|
|
|
| Pattern.CASE_INSENSITIVE);
|
2010-08-16 21:02:39 -04:00
|
|
|
|
|
|
|
public HkpKeyServer(String host) {
|
|
|
|
mHost = host;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HkpKeyServer(String host, short port) {
|
|
|
|
mHost = host;
|
|
|
|
mPort = port;
|
|
|
|
}
|
|
|
|
|
2011-10-16 21:07:37 -04:00
|
|
|
static private String readAll(InputStream in, String encoding) throws IOException {
|
2010-08-16 21:02:39 -04:00
|
|
|
ByteArrayOutputStream raw = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
byte buffer[] = new byte[1 << 16];
|
|
|
|
int n = 0;
|
2010-08-17 10:51:39 -04:00
|
|
|
while ((n = in.read(buffer)) != -1) {
|
2010-08-16 21:02:39 -04:00
|
|
|
raw.write(buffer, 0, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoding == null) {
|
|
|
|
encoding = "utf8";
|
|
|
|
}
|
2010-08-17 10:51:39 -04:00
|
|
|
return raw.toString(encoding);
|
|
|
|
}
|
|
|
|
|
2011-10-16 21:07:37 -04:00
|
|
|
private String query(String request) throws QueryException, HttpError {
|
2010-08-17 10:51:39 -04:00
|
|
|
InetAddress ips[];
|
|
|
|
try {
|
|
|
|
ips = InetAddress.getAllByName(mHost);
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
throw new QueryException(e.toString());
|
|
|
|
}
|
2010-08-18 08:26:13 -04:00
|
|
|
for (int i = 0; i < ips.length; ++i) {
|
2010-08-17 10:51:39 -04:00
|
|
|
try {
|
|
|
|
String url = "http://" + ips[i].getHostAddress() + ":" + mPort + request;
|
|
|
|
URL realUrl = new URL(url);
|
|
|
|
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
|
2010-08-17 20:53:18 -04:00
|
|
|
conn.setConnectTimeout(5000);
|
|
|
|
conn.setReadTimeout(25000);
|
2010-08-17 10:51:39 -04:00
|
|
|
conn.connect();
|
|
|
|
int response = conn.getResponseCode();
|
|
|
|
if (response >= 200 && response < 300) {
|
|
|
|
return readAll(conn.getInputStream(), conn.getContentEncoding());
|
2011-10-16 21:07:37 -04:00
|
|
|
} else {
|
2010-08-17 10:51:39 -04:00
|
|
|
String data = readAll(conn.getErrorStream(), conn.getContentEncoding());
|
|
|
|
throw new HttpError(response, data);
|
|
|
|
}
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
// nothing to do, try next IP
|
|
|
|
} catch (IOException e) {
|
|
|
|
// nothing to do, try next IP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new QueryException("querying server(s) for '" + mHost + "' failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-09-10 11:55:54 -04:00
|
|
|
public ArrayList<KeyInfo> search(String query) throws QueryException, TooManyResponses,
|
2012-03-09 10:27:29 -05:00
|
|
|
InsufficientQuery {
|
2012-09-10 11:55:54 -04:00
|
|
|
ArrayList<KeyInfo> results = new ArrayList<KeyInfo>();
|
2010-08-17 10:51:39 -04:00
|
|
|
|
|
|
|
if (query.length() < 3) {
|
|
|
|
throw new InsufficientQuery();
|
|
|
|
}
|
|
|
|
|
|
|
|
String encodedQuery;
|
|
|
|
try {
|
|
|
|
encodedQuery = URLEncoder.encode(query, "utf8");
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String request = "/pks/lookup?op=index&search=" + encodedQuery;
|
|
|
|
|
|
|
|
String data = null;
|
|
|
|
try {
|
|
|
|
data = query(request);
|
|
|
|
} catch (HttpError e) {
|
|
|
|
if (e.getCode() == 404) {
|
|
|
|
return results;
|
|
|
|
} else {
|
|
|
|
if (e.getData().toLowerCase().contains("no keys found")) {
|
|
|
|
return results;
|
|
|
|
} else if (e.getData().toLowerCase().contains("too many")) {
|
|
|
|
throw new TooManyResponses();
|
|
|
|
} else if (e.getData().toLowerCase().contains("insufficient")) {
|
|
|
|
throw new InsufficientQuery();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new QueryException("querying server(s) for '" + mHost + "' failed");
|
|
|
|
}
|
|
|
|
|
2010-08-16 21:02:39 -04:00
|
|
|
Matcher matcher = PUB_KEY_LINE.matcher(data);
|
|
|
|
while (matcher.find()) {
|
|
|
|
KeyInfo info = new KeyInfo();
|
|
|
|
info.size = Integer.parseInt(matcher.group(1));
|
|
|
|
info.algorithm = matcher.group(2);
|
2013-09-22 13:58:33 -04:00
|
|
|
info.keyId = PgpKeyHelper.convertHexToKeyId(matcher.group(3));
|
|
|
|
info.fingerPrint = PgpKeyHelper.convertKeyIdToHex(info.keyId);
|
2010-08-16 21:02:39 -04:00
|
|
|
String chunks[] = matcher.group(4).split("-");
|
2012-03-09 10:27:29 -05:00
|
|
|
info.date = new GregorianCalendar(Integer.parseInt(chunks[0]),
|
|
|
|
Integer.parseInt(chunks[1]), Integer.parseInt(chunks[2])).getTime();
|
2012-09-10 11:55:54 -04:00
|
|
|
info.userIds = new ArrayList<String>();
|
2010-08-16 21:02:39 -04:00
|
|
|
if (matcher.group(5).startsWith("*** KEY")) {
|
|
|
|
info.revoked = matcher.group(5);
|
|
|
|
} else {
|
|
|
|
String tmp = matcher.group(5).replaceAll("<.*?>", "");
|
|
|
|
tmp = Html.fromHtml(tmp).toString();
|
|
|
|
info.userIds.add(tmp);
|
|
|
|
}
|
|
|
|
if (matcher.group(6).length() > 0) {
|
|
|
|
Matcher matcher2 = USER_ID_LINE.matcher(matcher.group(6));
|
|
|
|
while (matcher2.find()) {
|
|
|
|
String tmp = matcher2.group(1).replaceAll("<.*?>", "");
|
|
|
|
tmp = Html.fromHtml(tmp).toString();
|
|
|
|
info.userIds.add(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
results.add(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-03-09 10:27:29 -05:00
|
|
|
public String get(long keyId) throws QueryException {
|
2011-10-16 21:07:37 -04:00
|
|
|
HttpClient client = new DefaultHttpClient();
|
2010-08-17 10:51:39 -04:00
|
|
|
try {
|
2012-03-09 10:27:29 -05:00
|
|
|
HttpGet get = new HttpGet("http://" + mHost + ":" + mPort
|
2013-09-22 13:58:33 -04:00
|
|
|
+ "/pks/lookup?op=get&search=0x" + PgpKeyHelper.convertKeyToHex(keyId));
|
2012-03-09 10:27:29 -05:00
|
|
|
|
2011-10-16 21:07:37 -04:00
|
|
|
HttpResponse response = client.execute(get);
|
|
|
|
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
|
|
|
|
throw new QueryException("not found");
|
|
|
|
}
|
2012-03-09 10:27:29 -05:00
|
|
|
|
2011-10-16 21:07:37 -04:00
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
|
InputStream is = entity.getContent();
|
|
|
|
String data = readAll(is, EntityUtils.getContentCharSet(entity));
|
2013-09-15 10:42:08 -04:00
|
|
|
Matcher matcher = PgpHelper.PGP_PUBLIC_KEY.matcher(data);
|
2011-10-16 21:07:37 -04:00
|
|
|
if (matcher.find()) {
|
|
|
|
return matcher.group(1);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
// nothing to do, better luck on the next keyserver
|
|
|
|
} finally {
|
|
|
|
client.getConnectionManager().shutdown();
|
2010-08-16 21:02:39 -04:00
|
|
|
}
|
2012-03-09 10:27:29 -05:00
|
|
|
|
2010-08-16 21:02:39 -04:00
|
|
|
return null;
|
|
|
|
}
|
2011-10-16 21:07:37 -04:00
|
|
|
|
|
|
|
@Override
|
2013-09-22 13:58:33 -04:00
|
|
|
public void add(String armoredText) throws AddKeyException {
|
2011-10-16 21:07:37 -04:00
|
|
|
HttpClient client = new DefaultHttpClient();
|
|
|
|
try {
|
|
|
|
HttpPost post = new HttpPost("http://" + mHost + ":" + mPort + "/pks/add");
|
2012-03-09 10:27:29 -05:00
|
|
|
|
2011-10-16 21:07:37 -04:00
|
|
|
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
|
2013-09-22 13:58:33 -04:00
|
|
|
nameValuePairs.add(new BasicNameValuePair("keytext", armoredText));
|
2011-10-16 21:07:37 -04:00
|
|
|
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
|
2012-03-09 10:27:29 -05:00
|
|
|
|
2011-10-16 21:07:37 -04:00
|
|
|
HttpResponse response = client.execute(post);
|
|
|
|
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
|
|
|
|
throw new AddKeyException();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
// nothing to do, better luck on the next keyserver
|
|
|
|
} finally {
|
|
|
|
client.getConnectionManager().shutdown();
|
|
|
|
}
|
|
|
|
}
|
2010-08-16 21:02:39 -04:00
|
|
|
}
|