mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-30 13:12:25 -05:00
Remove LocalKeyStore's dependency on Account to reduce coupling
This commit is contained in:
parent
5bf27c1031
commit
2a9ac867b9
@ -1,6 +1,8 @@
|
||||
|
||||
package com.fsck.k9;
|
||||
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
@ -22,6 +24,7 @@ import android.net.ConnectivityManager;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection;
|
||||
import com.fsck.k9.crypto.Apg;
|
||||
import com.fsck.k9.crypto.CryptoProvider;
|
||||
import com.fsck.k9.helper.Utility;
|
||||
@ -40,6 +43,7 @@ import com.fsck.k9.search.SqlQueryBuilder;
|
||||
import com.fsck.k9.search.SearchSpecification.Attribute;
|
||||
import com.fsck.k9.search.SearchSpecification.SearchCondition;
|
||||
import com.fsck.k9.search.SearchSpecification.Searchfield;
|
||||
import com.fsck.k9.security.LocalKeyStore;
|
||||
import com.fsck.k9.view.ColorChip;
|
||||
import com.larswerkman.colorpicker.ColorPicker;
|
||||
|
||||
@ -1865,4 +1869,50 @@ public class Account implements BaseAccount {
|
||||
search.and(Searchfield.FOLDER, folderName, Attribute.NOT_EQUALS);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new certificate for the incoming or outgoing server to the local key store.
|
||||
*/
|
||||
public void addCertificate(CheckDirection direction, X509Certificate certificate)
|
||||
throws CertificateException {
|
||||
Uri uri;
|
||||
if (direction.equals(CheckDirection.INCOMING)) {
|
||||
uri = Uri.parse(getStoreUri());
|
||||
} else {
|
||||
uri = Uri.parse(getTransportUri());
|
||||
}
|
||||
LocalKeyStore.getInstance().addCertificate(uri.getHost(), uri.getPort(), certificate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Examine the existing settings for an account. If the old host/port is different from the
|
||||
* new host/port, then try and delete any (possibly non-existent) certificate stored for the
|
||||
* old host/port.
|
||||
*/
|
||||
public void deleteCertificate(String newHost, int newPort, CheckDirection direction) {
|
||||
Uri uri;
|
||||
if (direction.equals(CheckDirection.INCOMING)) {
|
||||
uri = Uri.parse(getStoreUri());
|
||||
} else {
|
||||
uri = Uri.parse(getTransportUri());
|
||||
}
|
||||
String oldHost = uri.getHost();
|
||||
int oldPort = uri.getPort();
|
||||
if (!newHost.equals(oldHost) || newPort != oldPort) {
|
||||
LocalKeyStore.getInstance().deleteCertificate(oldHost, oldPort);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Examine the settings for the account and attempt to delete (possibly non-existent)
|
||||
* certificates for the incoming and outgoing servers.
|
||||
*/
|
||||
public void deleteCertificates() {
|
||||
LocalKeyStore localKeyStore = LocalKeyStore.getInstance();
|
||||
|
||||
Uri uri = Uri.parse(getStoreUri());
|
||||
localKeyStore.deleteCertificate(uri.getHost(), uri.getPort());
|
||||
uri = Uri.parse(getTransportUri());
|
||||
localKeyStore.deleteCertificate(uri.getHost(), uri.getPort());
|
||||
}
|
||||
}
|
||||
|
@ -128,8 +128,8 @@ public class Preferences {
|
||||
|
||||
Store.removeAccount(account);
|
||||
|
||||
account.deleteCertificates();
|
||||
account.delete(this);
|
||||
LocalKeyStore.getInstance().deleteCertificates(account);
|
||||
|
||||
if (newAccount == account) {
|
||||
newAccount = null;
|
||||
|
@ -24,7 +24,6 @@ import com.fsck.k9.mail.Store;
|
||||
import com.fsck.k9.mail.Transport;
|
||||
import com.fsck.k9.mail.store.WebDavStore;
|
||||
import com.fsck.k9.mail.filter.Hex;
|
||||
import com.fsck.k9.security.LocalKeyStore;
|
||||
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.CertificateEncodingException;
|
||||
@ -364,7 +363,7 @@ public class AccountSetupCheckSettings extends K9Activity implements OnClickList
|
||||
new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
try {
|
||||
LocalKeyStore.getInstance().addCertificate(mAccount, mDirection, chain[0]);
|
||||
mAccount.addCertificate(mDirection, chain[0]);
|
||||
} catch (CertificateException e) {
|
||||
showErrorDialog(
|
||||
R.string.account_setup_failed_dlg_certificate_message_fmt,
|
||||
|
@ -26,7 +26,6 @@ import com.fsck.k9.mail.store.Pop3Store;
|
||||
import com.fsck.k9.mail.store.WebDavStore;
|
||||
import com.fsck.k9.mail.store.ImapStore.ImapStoreSettings;
|
||||
import com.fsck.k9.mail.store.WebDavStore.WebDavStoreSettings;
|
||||
import com.fsck.k9.security.LocalKeyStore;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
@ -429,7 +428,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
|
||||
mWebdavMailboxPathView.getText().toString());
|
||||
}
|
||||
|
||||
LocalKeyStore.getInstance().deleteCertificate(mAccount, host, port, CheckDirection.INCOMING);
|
||||
mAccount.deleteCertificate(host, port, CheckDirection.INCOMING);
|
||||
ServerSettings settings = new ServerSettings(mStoreType, host, port,
|
||||
connectionSecurity, authType, username, password, extra);
|
||||
|
||||
|
@ -18,7 +18,6 @@ import com.fsck.k9.activity.K9Activity;
|
||||
import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection;
|
||||
import com.fsck.k9.helper.Utility;
|
||||
import com.fsck.k9.mail.transport.SmtpTransport;
|
||||
import com.fsck.k9.security.LocalKeyStore;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
@ -313,7 +312,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener,
|
||||
String newHost = mServerView.getText().toString();
|
||||
int newPort = Integer.parseInt(mPortView.getText().toString());
|
||||
uri = new URI(smtpSchemes[securityType], userInfo, newHost, newPort, null, null, null);
|
||||
LocalKeyStore.getInstance().deleteCertificate(mAccount, newHost, newPort, CheckDirection.OUTGOING);
|
||||
mAccount.deleteCertificate(newHost, newPort, CheckDirection.OUTGOING);
|
||||
mAccount.setTransportUri(uri.toString());
|
||||
AccountSetupCheckSettings.actionCheckSettings(this, mAccount, CheckDirection.OUTGOING);
|
||||
} catch (UnsupportedEncodingException enc) {
|
||||
|
@ -14,12 +14,9 @@ import java.security.cert.X509Certificate;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
import com.fsck.k9.Account;
|
||||
import com.fsck.k9.K9;
|
||||
import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection;
|
||||
|
||||
public class LocalKeyStore {
|
||||
private static final int KEY_STORE_FILE_VERSION = 1;
|
||||
@ -120,17 +117,6 @@ public class LocalKeyStore {
|
||||
}
|
||||
}
|
||||
|
||||
public void addCertificate(Account account, CheckDirection direction,
|
||||
X509Certificate certificate) throws CertificateException {
|
||||
Uri uri = null;
|
||||
if (direction.equals(CheckDirection.INCOMING)) {
|
||||
uri = Uri.parse(account.getStoreUri());
|
||||
} else {
|
||||
uri = Uri.parse(account.getTransportUri());
|
||||
}
|
||||
addCertificate(uri.getHost(), uri.getPort(), certificate);
|
||||
}
|
||||
|
||||
public synchronized boolean isValidCertificate(Certificate certificate,
|
||||
String host, int port) {
|
||||
if (mKeyStore == null) {
|
||||
@ -163,42 +149,6 @@ public class LocalKeyStore {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Examine the existing settings for an account. If the old host/port is different from
|
||||
* the new host/port, then try and delete any (possibly non-existent) certificate stored
|
||||
* for the old host/port.
|
||||
* @param account
|
||||
* @param newHost
|
||||
* @param newPort
|
||||
* @param direction
|
||||
*/
|
||||
public void deleteCertificate(Account account, String newHost, int newPort, CheckDirection direction) {
|
||||
Uri uri = null;
|
||||
if (direction.equals(CheckDirection.INCOMING)) {
|
||||
uri = Uri.parse(account.getStoreUri());
|
||||
} else {
|
||||
uri = Uri.parse(account.getTransportUri());
|
||||
}
|
||||
String oldHost = uri.getHost();
|
||||
int oldPort = uri.getPort();
|
||||
if (!newHost.equals(oldHost) || newPort != oldPort) {
|
||||
deleteCertificate(oldHost, oldPort);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Examine the settings for the account and attempt to delete (possibly
|
||||
* non-existent) certificates for the incoming and outgoing servers.
|
||||
*
|
||||
* @param account
|
||||
*/
|
||||
public void deleteCertificates(Account account) {
|
||||
Uri uri = Uri.parse(account.getStoreUri());
|
||||
deleteCertificate(uri.getHost(), uri.getPort());
|
||||
uri = Uri.parse(account.getTransportUri());
|
||||
deleteCertificate(uri.getHost(), uri.getPort());
|
||||
}
|
||||
|
||||
private void upgradeKeyStoreFile() {
|
||||
if (KEY_STORE_FILE_VERSION > 0) {
|
||||
// Blow away version "0" because certificate aliases have changed.
|
||||
|
Loading…
Reference in New Issue
Block a user