Change Autocrypt logic to more closely match the spec
This commit is contained in:
@@ -5,15 +5,17 @@ import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.openintents.openpgp.AutocryptPeerUpdate;
|
||||
import org.openintents.openpgp.AutocryptPeerUpdate.PreferEncrypt;
|
||||
import org.sufficientlysecure.keychain.Constants;
|
||||
import org.sufficientlysecure.keychain.operations.results.SaveKeyringResult;
|
||||
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
|
||||
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
|
||||
import org.sufficientlysecure.keychain.provider.AutocryptPeerDataAccessObject;
|
||||
import org.sufficientlysecure.keychain.provider.KeyWritableRepository;
|
||||
import org.sufficientlysecure.keychain.util.Log;
|
||||
import timber.log.Timber;
|
||||
|
||||
|
||||
class AutocryptInteractor {
|
||||
@@ -33,74 +35,100 @@ class AutocryptInteractor {
|
||||
this.keyWritableRepository = keyWritableRepository;
|
||||
}
|
||||
|
||||
void updateAutocryptPeerState(String autocryptPeerId, AutocryptPeerUpdate autocryptPeerUpdate)
|
||||
throws PgpGeneralException, IOException {
|
||||
if (autocryptPeerUpdate == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Long newMasterKeyId;
|
||||
if (autocryptPeerUpdate.hasKeyData()) {
|
||||
UncachedKeyRing uncachedKeyRing = UncachedKeyRing.decodeFromData(autocryptPeerUpdate.getKeyData());
|
||||
if (uncachedKeyRing.isSecret()) {
|
||||
Log.e(Constants.TAG, "Found secret key in autocrypt id! - Ignoring");
|
||||
return;
|
||||
}
|
||||
// this will merge if the key already exists - no worries!
|
||||
keyWritableRepository.savePublicKeyRing(uncachedKeyRing);
|
||||
newMasterKeyId = uncachedKeyRing.getMasterKeyId();
|
||||
} else {
|
||||
newMasterKeyId = null;
|
||||
}
|
||||
|
||||
void updateAutocryptPeerState(String autocryptPeerId, AutocryptPeerUpdate autocryptPeerUpdate) {
|
||||
Date effectiveDate = autocryptPeerUpdate.getEffectiveDate();
|
||||
autocryptPeerDao.updateLastSeen(autocryptPeerId, effectiveDate);
|
||||
|
||||
if (newMasterKeyId == null) {
|
||||
// 1. If the message’s effective date is older than the peers[from-addr].autocrypt_timestamp value, then no changes are required, and the update process terminates.
|
||||
Date lastSeenAutocrypt = autocryptPeerDao.getLastSeenKey(autocryptPeerId);
|
||||
if (lastSeenAutocrypt != null && lastSeenAutocrypt.after(effectiveDate)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Date lastSeenKey = autocryptPeerDao.getLastSeenKey(autocryptPeerId);
|
||||
if (lastSeenKey == null || effectiveDate.after(lastSeenKey)) {
|
||||
boolean isMutual = autocryptPeerUpdate.getPreferEncrypt() == PreferEncrypt.MUTUAL;
|
||||
autocryptPeerDao.updateKey(autocryptPeerId, effectiveDate, newMasterKeyId, isMutual);
|
||||
// 2. If the message’s effective date is more recent than peers[from-addr].last_seen then set peers[from-addr].last_seen to the message’s effective date.
|
||||
Date lastSeen = autocryptPeerDao.getLastSeen(autocryptPeerId);
|
||||
if (lastSeen == null || lastSeen.after(effectiveDate)) {
|
||||
autocryptPeerDao.updateLastSeen(autocryptPeerId, effectiveDate);
|
||||
}
|
||||
|
||||
// 3. If the Autocrypt header is unavailable, no further changes are required and the update process terminates.
|
||||
if (!autocryptPeerUpdate.hasKeyData()) {
|
||||
return;
|
||||
}
|
||||
|
||||
SaveKeyringResult saveKeyringResult = parseAndImportAutocryptKeyData(autocryptPeerUpdate);
|
||||
if (saveKeyringResult == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. Set peers[from-addr].autocrypt_timestamp to the message’s effective date.
|
||||
// 5. Set peers[from-addr].public_key to the corresponding keydata value of the Autocrypt header.
|
||||
Long newMasterKeyId = saveKeyringResult.savedMasterKeyId;
|
||||
// 6. Set peers[from-addr].prefer_encrypt to the corresponding prefer-encrypt value of the Autocrypt header.
|
||||
boolean isMutual = autocryptPeerUpdate.getPreferEncrypt() == PreferEncrypt.MUTUAL;
|
||||
|
||||
autocryptPeerDao.updateKey(autocryptPeerId, effectiveDate, newMasterKeyId, isMutual);
|
||||
}
|
||||
|
||||
|
||||
void updateAutocryptPeerGossipState(String autocryptPeerId, AutocryptPeerUpdate autocryptPeerUpdate)
|
||||
throws PgpGeneralException, IOException {
|
||||
if (autocryptPeerUpdate == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Long newMasterKeyId;
|
||||
if (autocryptPeerUpdate.hasKeyData()) {
|
||||
UncachedKeyRing uncachedKeyRing = UncachedKeyRing.decodeFromData(autocryptPeerUpdate.getKeyData());
|
||||
if (uncachedKeyRing.isSecret()) {
|
||||
Log.e(Constants.TAG, "Found secret key in autocrypt id! - Ignoring");
|
||||
return;
|
||||
}
|
||||
// this will merge if the key already exists - no worries!
|
||||
keyWritableRepository.savePublicKeyRing(uncachedKeyRing);
|
||||
newMasterKeyId = uncachedKeyRing.getMasterKeyId();
|
||||
} else {
|
||||
newMasterKeyId = null;
|
||||
}
|
||||
|
||||
Date lastSeen = autocryptPeerDao.getLastSeen(autocryptPeerId);
|
||||
void updateAutocryptPeerGossipState(String autocryptPeerId, AutocryptPeerUpdate autocryptPeerUpdate) {
|
||||
Date effectiveDate = autocryptPeerUpdate.getEffectiveDate();
|
||||
if (newMasterKeyId == null) {
|
||||
|
||||
// 1. If gossip-addr does not match any recipient in the mail’s To or Cc header, the update process terminates (i.e., header is ignored).
|
||||
// -> This should be taken care of in the mail client that sends us this data!
|
||||
|
||||
// 2. If peers[gossip-addr].gossip_timestamp is more recent than the message’s effective date, then the update process terminates.
|
||||
Date lastSeenGossip = autocryptPeerDao.getLastSeenGossip(autocryptPeerId);
|
||||
if (lastSeenGossip != null && lastSeenGossip.after(effectiveDate)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Date lastSeenKey = autocryptPeerDao.getLastSeenKey(autocryptPeerId);
|
||||
if (lastSeenKey != null && effectiveDate.before(lastSeenKey)) {
|
||||
if (!autocryptPeerUpdate.hasKeyData()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (lastSeen == null || effectiveDate.after(lastSeen)) {
|
||||
autocryptPeerDao.updateKeyGossip(autocryptPeerId, effectiveDate, newMasterKeyId);
|
||||
SaveKeyringResult saveKeyringResult = parseAndImportAutocryptKeyData(autocryptPeerUpdate);
|
||||
if (saveKeyringResult == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. Set peers[gossip-addr].gossip_timestamp to the message’s effective date.
|
||||
// 4. Set peers[gossip-addr].gossip_key to the value of the keydata attribute.
|
||||
Long newMasterKeyId = saveKeyringResult.savedMasterKeyId;
|
||||
|
||||
autocryptPeerDao.updateKeyGossip(autocryptPeerId, effectiveDate, newMasterKeyId);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private SaveKeyringResult parseAndImportAutocryptKeyData(AutocryptPeerUpdate autocryptPeerUpdate) {
|
||||
UncachedKeyRing uncachedKeyRing = parseAutocryptKeyData(autocryptPeerUpdate);
|
||||
if (uncachedKeyRing != null) {
|
||||
return importAutocryptKeyData(uncachedKeyRing);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private SaveKeyringResult importAutocryptKeyData(UncachedKeyRing uncachedKeyRing) {
|
||||
SaveKeyringResult saveKeyringResult = keyWritableRepository.savePublicKeyRing(uncachedKeyRing);
|
||||
if (!saveKeyringResult.success()) {
|
||||
Timber.e(Constants.TAG, "Error inserting key - ignoring!");
|
||||
return null;
|
||||
}
|
||||
return saveKeyringResult;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private UncachedKeyRing parseAutocryptKeyData(AutocryptPeerUpdate autocryptPeerUpdate) {
|
||||
UncachedKeyRing uncachedKeyRing;
|
||||
try {
|
||||
uncachedKeyRing = UncachedKeyRing.decodeFromData(autocryptPeerUpdate.getKeyData());
|
||||
} catch (IOException | PgpGeneralException e) {
|
||||
Timber.e(Constants.TAG, "Error parsing public key! - Ignoring");
|
||||
return null;
|
||||
}
|
||||
if (uncachedKeyRing.isSecret()) {
|
||||
Timber.e(Constants.TAG, "Found secret key in autocrypt id! - Ignoring");
|
||||
return null;
|
||||
}
|
||||
return uncachedKeyRing;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -762,7 +762,9 @@ public class OpenPgpService extends Service {
|
||||
String autocryptPeerId = data.getStringExtra(OpenPgpApi.EXTRA_AUTOCRYPT_PEER_ID);
|
||||
AutocryptPeerUpdate autocryptPeerUpdate = data.getParcelableExtra(OpenPgpApi.EXTRA_AUTOCRYPT_PEER_UPDATE);
|
||||
|
||||
autocryptInteractor.updateAutocryptPeerState(autocryptPeerId, autocryptPeerUpdate);
|
||||
if (autocryptPeerUpdate != null) {
|
||||
autocryptInteractor.updateAutocryptPeerState(autocryptPeerId, autocryptPeerUpdate);
|
||||
}
|
||||
}
|
||||
|
||||
if (data.hasExtra(OpenPgpApi.EXTRA_AUTOCRYPT_PEER_GOSSIP_UPDATES)) {
|
||||
@@ -770,7 +772,9 @@ public class OpenPgpService extends Service {
|
||||
for (String address : updates.keySet()) {
|
||||
Timber.d(Constants.TAG, "Updating gossip state: " + address);
|
||||
AutocryptPeerUpdate update = updates.getParcelable(address);
|
||||
autocryptInteractor.updateAutocryptPeerGossipState(address, update);
|
||||
if (update != null) {
|
||||
autocryptInteractor.updateAutocryptPeerGossipState(address, update);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user