Fixed coding style.

This commit is contained in:
gogowitczak
2014-03-18 15:44:20 +01:00
parent 5277e4c1e7
commit fee18bd33e
2 changed files with 144 additions and 137 deletions

View File

@@ -173,33 +173,30 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
this.hexKeyId = PgpKeyHelper.convertKeyIdToHex(keyId); this.hexKeyId = PgpKeyHelper.convertKeyIdToHex(keyId);
this.bitStrength = pgpKeyRing.getPublicKey().getBitStrength(); this.bitStrength = pgpKeyRing.getPublicKey().getBitStrength();
final int algorithm = pgpKeyRing.getPublicKey().getAlgorithm(); final int algorithm = pgpKeyRing.getPublicKey().getAlgorithm();
this.algorithm = getAlgorithmFromId(algorithm); this.algorithm = getAlgorithmFromId(algorithm);
} }
/** /**
* Based on <a href="http://tools.ietf.org/html/rfc2440#section-9.1">OpenPGP Message Format</a> * Based on <a href="http://tools.ietf.org/html/rfc2440#section-9.1">OpenPGP Message Format</a>
*/ */
private final static SparseArray<String> ALGORITHM_IDS = new SparseArray<String>() {{ private final static SparseArray<String> ALGORITHM_IDS = new SparseArray<String>() {{
put(-1, "unknown"); // TODO: with resources put(-1, "unknown"); // TODO: with resources
put(0, "unencrypted"); put(0, "unencrypted");
put(PGPPublicKey.RSA_GENERAL, "RSA"); put(PGPPublicKey.RSA_GENERAL, "RSA");
put(PGPPublicKey.RSA_ENCRYPT, "RSA"); put(PGPPublicKey.RSA_ENCRYPT, "RSA");
put(PGPPublicKey.RSA_SIGN, "RSA"); put(PGPPublicKey.RSA_SIGN, "RSA");
put(PGPPublicKey.ELGAMAL_ENCRYPT, "ElGamal"); put(PGPPublicKey.ELGAMAL_ENCRYPT, "ElGamal");
put(PGPPublicKey.ELGAMAL_GENERAL, "ElGamal"); put(PGPPublicKey.ELGAMAL_GENERAL, "ElGamal");
put(PGPPublicKey.DSA, "DSA"); put(PGPPublicKey.DSA, "DSA");
put(PGPPublicKey.EC, "ECC"); put(PGPPublicKey.EC, "ECC");
put(PGPPublicKey.ECDSA, "ECC"); put(PGPPublicKey.ECDSA, "ECC");
put(PGPPublicKey.ECDH, "ECC"); put(PGPPublicKey.ECDH, "ECC");
}}; }};
/**
* Based on <a href="http://tools.ietf.org/html/rfc2440#section-9.1">OpenPGP Message Format</a>
*/
public static String getAlgorithmFromId(int algorithmId) {
return (ALGORITHM_IDS.get(algorithmId) != null ? ALGORITHM_IDS.get(algorithmId) : ALGORITHM_IDS.get(-1));
}
/**
* Based on <a href="http://tools.ietf.org/html/rfc2440#section-9.1">OpenPGP Message Format</a>
*/
public static String getAlgorithmFromId(int algorithmId) {
return (ALGORITHM_IDS.get(algorithmId) != null ? ALGORITHM_IDS.get(algorithmId) : ALGORITHM_IDS.get(-1));
}
} }

View File

@@ -69,55 +69,64 @@ public class HkpKeyServer extends KeyServer {
private String mHost; private String mHost;
private short mPort; private short mPort;
/** /**
* pub:%keyid%:%algo%:%keylen%:%creationdate%:%expirationdate%:%flags% * pub:%keyid%:%algo%:%keylen%:%creationdate%:%expirationdate%:%flags%
* <ul> * <ul>
* <li>%<b>keyid</b>% = this is either the fingerprint or the key ID of the key. * <li>%<b>keyid</b>% = this is either the fingerprint or the key ID of the key. Either the 16-digit or 8-digit
* Either the 16-digit or 8-digit key IDs are acceptable, but obviously the fingerprint is best.</li> * 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>algo</b>% = the algorithm number, (i.e. 1==RSA, 17==DSA, etc).
* <li>%<b>keylen</b>% = the key length (i.e. 1024, 2048, 4096, etc.)</li> * See <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a></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>keylen</b>% = the key length (i.e. 1024, 2048, 4096, etc.)</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>creationdate</b>% = creation date of the key in standard
* <li>%<b>flags</b>% = letter codes to indicate details of the key, if any. Flags may be in any order. The * <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a> form (i.e. number of seconds since
* meaning of "disabled" is implementation-specific. Note that individual flags may be unimplemented, so * 1/1/1970 UTC time)</li>
* the absence of a given flag does not necessarily mean the absence of the detail. * <li>%<b>expirationdate</b>% = expiration date of the key in standard
* <ul> * <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a> form (i.e. number of seconds since
* <li>r == revoked</li> * 1/1/1970 UTC time)</li>
* <li>d == disabled</li> * <li>%<b>flags</b>% = letter codes to indicate details of the key, if any. Flags may be in any order. The
* <li>e == expired</li> * meaning of "disabled" is implementation-specific. Note that individual flags may be unimplemented, so
* </ul> * the absence of a given flag does not necessarily mean the absence of the detail.
* </li> * <ul>
* </ul> * <li>r == revoked</li>
* * <li>d == disabled</li>
* * <li>e == expired</li>
* @see <a href="http://tools.ietf.org/html/draft-shaw-openpgp-hkp-00#section-5.2">5.2. Machine Readable Indexes</a> in Internet-Draft OpenPGP HTTP Keyserver Protocol Document * </ul>
*/ * </li>
public static final Pattern PUB_KEY_LINE = Pattern * </ul>
.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]*))+", // one or more uid lines * @see <a href="http://tools.ietf.org/html/draft-shaw-openpgp-hkp-00#section-5.2">5.2. Machine Readable Indexes</a>
Pattern.CASE_INSENSITIVE); * in Internet-Draft OpenPGP HTTP Keyserver Protocol Document
*/
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]*))+", // one or more uid lines
Pattern.CASE_INSENSITIVE);
/** /**
* uid:%escaped uid string%:%creationdate%:%expirationdate%:%flags% * uid:%escaped uid string%:%creationdate%:%expirationdate%:%flags%
* <ul> * <ul>
* <li>%<b>escaped uid string</b>% = the user ID string, with HTTP %-escaping for anything that isn't 7-bit * <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> * 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>creationdate</b>% = creation date of the key in standard
* <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> * <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a> form (i.e. number of seconds since
* <li>%<b>flags</b>% = letter codes to indicate details of the key, if any. Flags may be in any order. The * 1/1/1970 UTC time)</li>
* meaning of "disabled" is implementation-specific. Note that individual flags may be unimplemented, so * <li>%<b>expirationdate</b>% = expiration date of the key in standard
* the absence of a given flag does not necessarily mean the absence of the detail. * <a href="http://tools.ietf.org/html/rfc2440#section-9.1">RFC-2440</a> form (i.e. number of seconds since
* <ul> * 1/1/1970 UTC time)</li>
* <li>r == revoked</li> * <li>%<b>flags</b>% = letter codes to indicate details of the key, if any. Flags may be in any order. The
* <li>d == disabled</li> * meaning of "disabled" is implementation-specific. Note that individual flags may be unimplemented, so
* <li>e == expired</li> * the absence of a given flag does not necessarily mean the absence of the detail.
* </ul> * <ul>
* </li> * <li>r == revoked</li>
* </ul> * <li>d == disabled</li>
*/ * <li>e == expired</li>
public static final Pattern UID_LINE = Pattern * </ul>
.compile("uid:(.*):([0-9]+):([0-9]*):([rde]*)", * </li>
Pattern.CASE_INSENSITIVE); * </ul>
*/
public static final Pattern UID_LINE = Pattern
.compile("uid:(.*):([0-9]+):([0-9]*):([rde]*)",
Pattern.CASE_INSENSITIVE);
private static final short PORT_DEFAULT = 11371; private static final short PORT_DEFAULT = 11371;
@@ -192,77 +201,78 @@ public class HkpKeyServer extends KeyServer {
throw new QueryException("querying server(s) for '" + mHost + "' failed"); throw new QueryException("querying server(s) for '" + mHost + "' failed");
} }
@Override @Override
public ArrayList<ImportKeysListEntry> search(String query) throws QueryException, TooManyResponses, public ArrayList<ImportKeysListEntry> search(String query) throws QueryException, TooManyResponses,
InsufficientQuery { InsufficientQuery {
ArrayList<ImportKeysListEntry> results = new ArrayList<ImportKeysListEntry>(); ArrayList<ImportKeysListEntry> results = new ArrayList<ImportKeysListEntry>();
if (query.length() < 3) { if (query.length() < 3) {
throw new InsufficientQuery(); throw new InsufficientQuery();
} }
String encodedQuery; String encodedQuery;
try { try {
encodedQuery = URLEncoder.encode(query, "utf8"); encodedQuery = URLEncoder.encode(query, "utf8");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
return null; return null;
} }
String request = "/pks/lookup?op=index&search=" + encodedQuery + "&options=mr"; String request = "/pks/lookup?op=index&search=" + encodedQuery + "&options=mr";
String data = null; String data = null;
try { try {
data = query(request); data = query(request);
} catch (HttpError e) { } catch (HttpError e) {
if (e.getCode() == 404) { if (e.getCode() == 404) {
return results; return results;
} else { } else {
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")) {
throw new TooManyResponses(); throw new TooManyResponses();
} else if (e.getData().toLowerCase(Locale.US).contains("insufficient")) { } else if (e.getData().toLowerCase(Locale.US).contains("insufficient")) {
throw new InsufficientQuery(); throw new InsufficientQuery();
} }
} }
throw new QueryException("querying server(s) for '" + mHost + "' failed"); throw new QueryException("querying server(s) for '" + mHost + "' failed");
} }
final Matcher matcher = PUB_KEY_LINE.matcher(data); final Matcher matcher = PUB_KEY_LINE.matcher(data);
while (matcher.find()) { while (matcher.find()) {
final ImportKeysListEntry info = new ImportKeysListEntry(); final ImportKeysListEntry info = new ImportKeysListEntry();
info.bitStrength = Integer.parseInt(matcher.group(3)); info.bitStrength = Integer.parseInt(matcher.group(3));
final int algorithmId = Integer.decode(matcher.group(2)); final int algorithmId = Integer.decode(matcher.group(2));
info.algorithm = getAlgorithmFromId(algorithmId); info.algorithm = getAlgorithmFromId(algorithmId);
info.hexKeyId = "0x" + matcher.group(1); info.hexKeyId = "0x" + matcher.group(1);
info.keyId = PgpKeyHelper.convertHexToKeyId(matcher.group(1)); info.keyId = PgpKeyHelper.convertHexToKeyId(matcher.group(1));
final long creationDate = Long.parseLong(matcher.group(4)); final long creationDate = Long.parseLong(matcher.group(4));
final GregorianCalendar tmpGreg = new GregorianCalendar(TimeZone.getTimeZone("UTC")); final GregorianCalendar tmpGreg = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
tmpGreg.setTimeInMillis(creationDate*1000); tmpGreg.setTimeInMillis(creationDate * 1000);
info.date = tmpGreg.getTime(); info.date = tmpGreg.getTime();
info.revoked = matcher.group(6).contains("r"); info.revoked = matcher.group(6).contains("r");
info.userIds = new ArrayList<String>(); info.userIds = new ArrayList<String>();
final String uidLines = matcher.group(7); final String uidLines = matcher.group(7);
final Matcher uidMatcher = UID_LINE.matcher(uidLines); final Matcher uidMatcher = UID_LINE.matcher(uidLines);
while (uidMatcher.find()) { while (uidMatcher.find()) {
String tmp = uidMatcher.group(1).replaceAll("<.*?>", ""); String tmp = uidMatcher.group(1).replaceAll("<.*?>", "");
tmp = Html.fromHtml(tmp).toString().trim(); tmp = Html.fromHtml(tmp).toString().trim();
if (tmp.contains("%")) if (tmp.contains("%")) {
{ try {
try { // converts Strings like "Universit%C3%A4t" to a proper encoding form "Universität".
tmp = (URLDecoder.decode(tmp, "UTF8")); // converts String like "Universit%C3%A4t" to a proper form "Universität". tmp = (URLDecoder.decode(tmp, "UTF8"));
} catch (UnsupportedEncodingException ignored) { } catch (UnsupportedEncodingException ignored) {
} // will never happen, because "UTF8" is supported
} }
info.userIds.add(tmp); }
} info.userIds.add(tmp);
results.add(info); }
} results.add(info);
return results; }
} return results;
}
@Override @Override
public String get(long keyId) throws QueryException { public String get(long keyId) throws QueryException {