token-import: look up keys locally by all fingerprints

This commit is contained in:
Vincent Breitmoser
2017-09-11 03:20:00 +02:00
parent bd2e6aa698
commit a1b049993f
3 changed files with 46 additions and 32 deletions

View File

@@ -922,7 +922,8 @@ public abstract class OperationResult implements Parcelable {
MSG_RET_LOCAL_SEARCH(LogLevel.DEBUG, R.string.msg_ret_local_search),
MSG_RET_LOCAL_FP_MATCH (LogLevel.DEBUG, R.string.msg_ret_local_fp_match),
MSG_RET_LOCAL_FP_MISMATCH (LogLevel.ERROR, R.string.msg_ret_local_fp_mismatch),
MSG_RET_LOCAL_NOT_FOUND (LogLevel.ERROR, R.string.msg_ret_local_not_found),
MSG_RET_LOCAL_NOT_FOUND (LogLevel.DEBUG, R.string.msg_ret_local_not_found),
MSG_RET_LOCAL_NONE_FOUND (LogLevel.ERROR, R.string.msg_ret_local_none_found),
MSG_RET_LOCAL_OK (LogLevel.OK, R.string.msg_ret_local_ok),
MSG_RET_LOCAL_SECRET (LogLevel.INFO, R.string.msg_ret_local_secret),
MSG_RET_LOCAL_START (LogLevel.START, R.string.msg_ret_local_start),

View File

@@ -53,6 +53,7 @@ import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing;
import org.sufficientlysecure.keychain.provider.KeyRepository;
import org.sufficientlysecure.keychain.provider.KeyRepository.NotFoundException;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.ui.token.PublicKeyRetrievalLoader.KeyRetrievalResult;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.util.ParcelableProxy;
@@ -102,44 +103,55 @@ public abstract class PublicKeyRetrievalLoader extends AsyncTaskLoader<KeyRetrie
@Override
public KeyRetrievalResult loadInBackground() {
OperationLog log = new OperationLog();
try {
log.add(LogType.MSG_RET_LOCAL_START, 0);
log.add(LogType.MSG_RET_LOCAL_START, 0);
// TODO check other fingerprints
long masterKeyId = KeyFormattingUtils.getKeyIdFromFingerprint(fingerprints[0]);
log.add(LogType.MSG_RET_LOCAL_SEARCH, 1, KeyFormattingUtils.convertKeyIdToHex(masterKeyId));
CachedPublicKeyRing cachedPublicKeyRing = keyRepository.getCachedPublicKeyRing(masterKeyId);
if (!Arrays.equals(fingerprints[0], cachedPublicKeyRing.getFingerprint())) {
log.add(LogType.MSG_RET_LOCAL_FP_MISMATCH, 1);
return KeyRetrievalResult.createWithError(log);
} else {
log.add(LogType.MSG_RET_LOCAL_FP_MATCH, 1);
for (byte[] fingerprint : fingerprints) {
long keyId = KeyFormattingUtils.getKeyIdFromFingerprint(fingerprint);
if (keyId == 0L) {
continue;
}
switch (cachedPublicKeyRing.getSecretKeyType(masterKeyId)) {
case PASSPHRASE:
case PASSPHRASE_EMPTY: {
log.add(LogType.MSG_RET_LOCAL_SECRET, 1);
log.add(LogType.MSG_RET_LOCAL_OK, 1);
return KeyRetrievalResult.createWithMasterKeyIdAndSecretAvailable(log, masterKeyId);
}
log.add(LogType.MSG_RET_LOCAL_SEARCH, 1, KeyFormattingUtils.convertKeyIdToHex(keyId));
try {
CachedPublicKeyRing cachedPublicKeyRing = keyRepository.getCachedPublicKeyRing(
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(keyId)
);
case GNU_DUMMY:
case DIVERT_TO_CARD:
case UNAVAILABLE: {
log.add(LogType.MSG_RET_LOCAL_OK, 1);
return KeyRetrievalResult.createWithMasterKeyId(log, masterKeyId);
}
long masterKeyId = cachedPublicKeyRing.getMasterKeyId();
// TODO check fingerprint
// if (!Arrays.equals(fingerprints, cachedPublicKeyRing.getFingerprint())) {
// log.add(LogType.MSG_RET_LOCAL_FP_MISMATCH, 1);
// return KeyRetrievalResult.createWithError(log);
// } else {
// log.add(LogType.MSG_RET_LOCAL_FP_MATCH, 1);
// }
default: {
throw new IllegalStateException("Unhandled SecretKeyType!");
switch (cachedPublicKeyRing.getSecretKeyType(keyId)) {
case PASSPHRASE:
case PASSPHRASE_EMPTY: {
log.add(LogType.MSG_RET_LOCAL_SECRET, 1);
log.add(LogType.MSG_RET_LOCAL_OK, 1);
return KeyRetrievalResult.createWithMasterKeyIdAndSecretAvailable(log, masterKeyId);
}
case GNU_DUMMY:
case DIVERT_TO_CARD:
case UNAVAILABLE: {
log.add(LogType.MSG_RET_LOCAL_OK, 1);
return KeyRetrievalResult.createWithMasterKeyId(log, masterKeyId);
}
default: {
throw new IllegalStateException("Unhandled SecretKeyType!");
}
}
} catch (PgpKeyNotFoundException | NotFoundException e) {
log.add(LogType.MSG_RET_LOCAL_NOT_FOUND, 2);
}
} catch (PgpKeyNotFoundException | NotFoundException e) {
log.add(LogType.MSG_RET_LOCAL_NOT_FOUND, 1);
return KeyRetrievalResult.createWithError(log);
}
log.add(LogType.MSG_RET_LOCAL_NONE_FOUND, 1);
return KeyRetrievalResult.createWithError(log);
}
}

View File

@@ -1977,7 +1977,8 @@
<string name="msg_ret_local_search">"Searching for key: %s"</string>
<string name="msg_ret_local_fp_match">"Local key's fingerprint matches"</string>
<string name="msg_ret_local_fp_mismatch">"Local key's fingerprint doesn't match!"</string>
<string name="msg_ret_local_not_found">"No key found"</string>
<string name="msg_ret_local_not_found">"Key not found"</string>
<string name="msg_ret_local_none_found">"No matching key found"</string>
<string name="msg_ret_local_ok">"Key found"</string>
<string name="msg_ret_local_secret">"Local key contains secret key material"</string>
<string name="msg_ret_local_start">"Looking for key in local key list…"</string>