Dont try to parse key/fingerprint result from keyserver query

This commit is contained in:
Dominik Schürmann 2014-03-20 15:54:49 +01:00
parent 747a41a0be
commit 2b77489932
7 changed files with 91 additions and 88 deletions

View File

@ -460,12 +460,16 @@ public class PgpKeyHelper {
public static String convertFingerprintToHex(byte[] fingerprint, boolean split) { public static String convertFingerprintToHex(byte[] fingerprint, boolean split) {
String hexString = Hex.toHexString(fingerprint); String hexString = Hex.toHexString(fingerprint);
if (split) { if (split) {
hexString = hexString.replaceAll("(.{4})(?!$)", "$1 "); hexString = splitFingerprintHex(hexString);
} }
return hexString; return hexString;
} }
public static String splitFingerprintHex(String hexString) {
return hexString.replaceAll("(.{4})(?!$)", "$1 ");
}
/** /**
* Convert key id from long to 64 bit hex string * Convert key id from long to 64 bit hex string
* <p/> * <p/>
@ -497,19 +501,6 @@ public class PgpKeyHelper {
return hexString; return hexString;
} }
/**
* Used in HkpKeyServer to convert hex encoded key ids back to long.
*
* @param hexString
* @return
*/
public static long convertHexToKeyId(String hexString) {
int len = hexString.length();
String s2 = hexString.substring(len - 8);
String s1 = hexString.substring(0, len - 8);
return ((!s1.isEmpty() ? Long.parseLong(s1, 16) << 32 : 0) | Long.parseLong(s2, 16));
}
/** /**
* Splits userId string into naming part, email part, and comment part * Splits userId string into naming part, email part, and comment part
* *

View File

@ -749,7 +749,7 @@ public class KeychainIntentService extends IntentService
HkpKeyServer server = new HkpKeyServer(keyServer); HkpKeyServer server = new HkpKeyServer(keyServer);
for (ImportKeysListEntry entry : entries) { for (ImportKeysListEntry entry : entries) {
byte[] downloadedKey = server.get(entry.getKeyId()).getBytes(); byte[] downloadedKey = server.get(entry.getKeyIdHex()).getBytes();
/** /**
* TODO: copied from ImportKeysListLoader * TODO: copied from ImportKeysListLoader

View File

@ -134,10 +134,10 @@ public class ImportKeysAdapter extends ArrayAdapter<ImportKeysListEntry> {
holder.mMainUserIdRest.setVisibility(View.GONE); holder.mMainUserIdRest.setVisibility(View.GONE);
} }
holder.mKeyId.setText(entry.hexKeyId); holder.mKeyId.setText(entry.keyIdHex);
if (entry.fingerPrint != null) { if (entry.fingerPrintHex != null) {
holder.mFingerprint.setText(mActivity.getString(R.string.fingerprint) + " " + entry.fingerPrint); holder.mFingerprint.setText(mActivity.getString(R.string.fingerprint) + " " + entry.fingerPrintHex);
holder.mFingerprint.setVisibility(View.VISIBLE); holder.mFingerprint.setVisibility(View.VISIBLE);
} else { } else {
holder.mFingerprint.setVisibility(View.GONE); holder.mFingerprint.setVisibility(View.GONE);

View File

@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.ui.adapter;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.util.SparseArray; import android.util.SparseArray;
import org.spongycastle.openpgp.PGPKeyRing; import org.spongycastle.openpgp.PGPKeyRing;
import org.spongycastle.openpgp.PGPPublicKey; import org.spongycastle.openpgp.PGPPublicKey;
import org.spongycastle.openpgp.PGPSecretKeyRing; import org.spongycastle.openpgp.PGPSecretKeyRing;
@ -38,10 +39,11 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
public ArrayList<String> userIds; public ArrayList<String> userIds;
public long keyId; public long keyId;
public String keyIdHex;
public boolean revoked; public boolean revoked;
public Date date; // TODO: not displayed public Date date; // TODO: not displayed
public String fingerPrint; public String fingerPrintHex;
public String hexKeyId;
public int bitStrength; public int bitStrength;
public String algorithm; public String algorithm;
public boolean secretKey; public boolean secretKey;
@ -55,8 +57,8 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
this.keyId = b.keyId; this.keyId = b.keyId;
this.revoked = b.revoked; this.revoked = b.revoked;
this.date = b.date; this.date = b.date;
this.fingerPrint = b.fingerPrint; this.fingerPrintHex = b.fingerPrintHex;
this.hexKeyId = b.hexKeyId; this.keyIdHex = b.keyIdHex;
this.bitStrength = b.bitStrength; this.bitStrength = b.bitStrength;
this.algorithm = b.algorithm; this.algorithm = b.algorithm;
this.secretKey = b.secretKey; this.secretKey = b.secretKey;
@ -74,8 +76,8 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
dest.writeLong(keyId); dest.writeLong(keyId);
dest.writeByte((byte) (revoked ? 1 : 0)); dest.writeByte((byte) (revoked ? 1 : 0));
dest.writeSerializable(date); dest.writeSerializable(date);
dest.writeString(fingerPrint); dest.writeString(fingerPrintHex);
dest.writeString(hexKeyId); dest.writeString(keyIdHex);
dest.writeInt(bitStrength); dest.writeInt(bitStrength);
dest.writeString(algorithm); dest.writeString(algorithm);
dest.writeByte((byte) (secretKey ? 1 : 0)); dest.writeByte((byte) (secretKey ? 1 : 0));
@ -92,8 +94,8 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
vr.keyId = source.readLong(); vr.keyId = source.readLong();
vr.revoked = source.readByte() == 1; vr.revoked = source.readByte() == 1;
vr.date = (Date) source.readSerializable(); vr.date = (Date) source.readSerializable();
vr.fingerPrint = source.readString(); vr.fingerPrintHex = source.readString();
vr.hexKeyId = source.readString(); vr.keyIdHex = source.readString();
vr.bitStrength = source.readInt(); vr.bitStrength = source.readInt();
vr.algorithm = source.readString(); vr.algorithm = source.readString();
vr.secretKey = source.readByte() == 1; vr.secretKey = source.readByte() == 1;
@ -109,8 +111,8 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
} }
}; };
public long getKeyId() { public String getKeyIdHex() {
return keyId; return keyIdHex;
} }
public byte[] getBytes() { public byte[] getBytes() {
@ -121,6 +123,14 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
this.mBytes = bytes; this.mBytes = bytes;
} }
public boolean isSelected() {
return mSelected;
}
public void setSelected(boolean selected) {
this.mSelected = selected;
}
/** /**
* Constructor for later querying from keyserver * Constructor for later querying from keyserver
*/ */
@ -132,14 +142,6 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
userIds = new ArrayList<String>(); userIds = new ArrayList<String>();
} }
public boolean isSelected() {
return mSelected;
}
public void setSelected(boolean selected) {
this.mSelected = selected;
}
/** /**
* Constructor based on key object, used for import from NFC, QR Codes, files * Constructor based on key object, used for import from NFC, QR Codes, files
*/ */
@ -165,12 +167,13 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
for (String userId : new IterableIterator<String>(pgpKeyRing.getPublicKey().getUserIDs())) { for (String userId : new IterableIterator<String>(pgpKeyRing.getPublicKey().getUserIDs())) {
userIds.add(userId); userIds.add(userId);
} }
this.keyId = pgpKeyRing.getPublicKey().getKeyID(); this.keyId = pgpKeyRing.getPublicKey().getKeyID();
this.keyIdHex = PgpKeyHelper.convertKeyIdToHex(keyId);
this.revoked = pgpKeyRing.getPublicKey().isRevoked(); this.revoked = pgpKeyRing.getPublicKey().isRevoked();
this.fingerPrint = PgpKeyHelper.convertFingerprintToHex(pgpKeyRing.getPublicKey() this.fingerPrintHex = PgpKeyHelper.convertFingerprintToHex(pgpKeyRing.getPublicKey()
.getFingerprint(), true); .getFingerprint(), true);
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);

View File

@ -18,7 +18,6 @@
package org.sufficientlysecure.keychain.util; package org.sufficientlysecure.keychain.util;
import android.text.Html;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
@ -34,7 +33,6 @@ import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.pgp.PgpHelper; import org.sufficientlysecure.keychain.pgp.PgpHelper;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListEntry; import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListEntry;
import org.sufficientlysecure.keychain.util.Log;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
@ -246,8 +244,18 @@ public class HkpKeyServer extends KeyServer {
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); // group 1 contains the full fingerprint (v4) or the long key id if available
info.keyId = PgpKeyHelper.convertHexToKeyId(matcher.group(1)); // 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) {
info.fingerPrintHex = "0x" + PgpKeyHelper.splitFingerprintHex(fingerprintOrKeyId);
info.keyIdHex = "0x" + fingerprintOrKeyId.substring(fingerprintOrKeyId.length()
- 16, fingerprintOrKeyId.length());
} else {
// set key id only
info.keyIdHex = "0x" + fingerprintOrKeyId;
}
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"));
@ -277,11 +285,11 @@ public class HkpKeyServer extends KeyServer {
} }
@Override @Override
public String get(long keyId) throws QueryException { public String get(String keyIdHex) throws QueryException {
HttpClient client = new DefaultHttpClient(); HttpClient client = new DefaultHttpClient();
try { try {
String query = "http://" + mHost + ":" + mPort + String query = "http://" + mHost + ":" + mPort +
"/pks/lookup?op=get&options=mr&search=" + PgpKeyHelper.convertKeyIdToHex(keyId); "/pks/lookup?op=get&options=mr&search=" + keyIdHex;
Log.d(Constants.TAG, "hkp keyserver get: " + query); Log.d(Constants.TAG, "hkp keyserver get: " + query);
HttpGet get = new HttpGet(query); HttpGet get = new HttpGet(query);
HttpResponse response = client.execute(get); HttpResponse response = client.execute(get);
@ -306,14 +314,14 @@ public class HkpKeyServer extends KeyServer {
} }
@Override @Override
public void add(String armoredText) throws AddKeyException { public void add(String armoredKey) throws AddKeyException {
HttpClient client = new DefaultHttpClient(); HttpClient client = new DefaultHttpClient();
try { try {
String query = "http://" + mHost + ":" + mPort + "/pks/add"; String query = "http://" + mHost + ":" + mPort + "/pks/add";
HttpPost post = new HttpPost(query); HttpPost post = new HttpPost(query);
Log.d(Constants.TAG, "hkp keyserver add: " + query); Log.d(Constants.TAG, "hkp keyserver add: " + query);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("keytext", armoredText)); nameValuePairs.add(new BasicNameValuePair("keytext", armoredKey));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post); HttpResponse response = client.execute(post);

View File

@ -46,7 +46,7 @@ public abstract class KeyServer {
abstract List<ImportKeysListEntry> search(String query) throws QueryException, TooManyResponses, abstract List<ImportKeysListEntry> search(String query) throws QueryException, TooManyResponses,
InsufficientQuery; InsufficientQuery;
abstract String get(long keyId) throws QueryException; abstract String get(String keyIdHex) throws QueryException;
abstract void add(String armoredText) throws AddKeyException; abstract void add(String armoredKey) throws AddKeyException;
} }

View File

@ -77,10 +77,11 @@
<TextView <TextView
android:id="@+id/keyId" android:id="@+id/keyId"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_height="wrap_content"
android:text="BBBBBBBB" android:text="0xBBBBBBBBBBBBBBBB"
android:textAppearance="?android:attr/textAppearanceSmall" android:textAppearance="?android:attr/textAppearanceSmall"
android:typeface="monospace" /> android:typeface="monospace"
android:layout_weight="1" />
<TextView <TextView
android:id="@+id/algorithm" android:id="@+id/algorithm"