1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-30 13:12:25 -05:00

Eliminate the need to pass a context to LocalKeyStore.getInstance

Instead, have K9.onCreate initialize the location of the key
store file (similar to what is done with
BinaryTempFileBody.setTempDirectory).

Also, LocalKeyStore.getInstance has been changed so that it
no longer needs to be synchronized.
This commit is contained in:
Joe Steele 2013-12-03 19:20:20 -05:00
parent 9333f4f7f4
commit cedcd7e47c
9 changed files with 34 additions and 29 deletions

View File

@ -1873,7 +1873,7 @@ public class Account implements BaseAccount {
/** /**
* Add a new certificate for the incoming or outgoing server to the local key store. * Add a new certificate for the incoming or outgoing server to the local key store.
*/ */
public void addCertificate(Context context, CheckDirection direction, public void addCertificate(CheckDirection direction,
X509Certificate certificate) throws CertificateException { X509Certificate certificate) throws CertificateException {
Uri uri; Uri uri;
if (direction.equals(CheckDirection.INCOMING)) { if (direction.equals(CheckDirection.INCOMING)) {
@ -1881,7 +1881,7 @@ public class Account implements BaseAccount {
} else { } else {
uri = Uri.parse(getTransportUri()); uri = Uri.parse(getTransportUri());
} }
LocalKeyStore localKeyStore = LocalKeyStore.getInstance(context); LocalKeyStore localKeyStore = LocalKeyStore.getInstance();
localKeyStore.addCertificate(uri.getHost(), uri.getPort(), certificate); localKeyStore.addCertificate(uri.getHost(), uri.getPort(), certificate);
} }
@ -1890,7 +1890,7 @@ public class Account implements BaseAccount {
* new host/port, then try and delete any (possibly non-existent) certificate stored for the * new host/port, then try and delete any (possibly non-existent) certificate stored for the
* old host/port. * old host/port.
*/ */
public void deleteCertificate(Context context, String newHost, int newPort, public void deleteCertificate(String newHost, int newPort,
CheckDirection direction) { CheckDirection direction) {
Uri uri; Uri uri;
if (direction.equals(CheckDirection.INCOMING)) { if (direction.equals(CheckDirection.INCOMING)) {
@ -1905,7 +1905,7 @@ public class Account implements BaseAccount {
return; return;
} }
if (!newHost.equals(oldHost) || newPort != oldPort) { if (!newHost.equals(oldHost) || newPort != oldPort) {
LocalKeyStore localKeyStore = LocalKeyStore.getInstance(context); LocalKeyStore localKeyStore = LocalKeyStore.getInstance();
localKeyStore.deleteCertificate(oldHost, oldPort); localKeyStore.deleteCertificate(oldHost, oldPort);
} }
} }
@ -1914,8 +1914,8 @@ public class Account implements BaseAccount {
* Examine the settings for the account and attempt to delete (possibly non-existent) * Examine the settings for the account and attempt to delete (possibly non-existent)
* certificates for the incoming and outgoing servers. * certificates for the incoming and outgoing servers.
*/ */
public void deleteCertificates(Context context) { public void deleteCertificates() {
LocalKeyStore localKeyStore = LocalKeyStore.getInstance(context); LocalKeyStore localKeyStore = LocalKeyStore.getInstance();
Uri uri = Uri.parse(getStoreUri()); Uri uri = Uri.parse(getStoreUri());
localKeyStore.deleteCertificate(uri.getHost(), uri.getPort()); localKeyStore.deleteCertificate(uri.getHost(), uri.getPort());

View File

@ -39,6 +39,7 @@ import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.internet.BinaryTempFileBody; import com.fsck.k9.mail.internet.BinaryTempFileBody;
import com.fsck.k9.mail.store.LocalStore; import com.fsck.k9.mail.store.LocalStore;
import com.fsck.k9.provider.UnreadWidgetProvider; import com.fsck.k9.provider.UnreadWidgetProvider;
import com.fsck.k9.security.LocalKeyStore;
import com.fsck.k9.service.BootReceiver; import com.fsck.k9.service.BootReceiver;
import com.fsck.k9.service.MailService; import com.fsck.k9.service.MailService;
import com.fsck.k9.service.ShutdownReceiver; import com.fsck.k9.service.ShutdownReceiver;
@ -590,6 +591,8 @@ public class K9 extends Application {
*/ */
BinaryTempFileBody.setTempDirectory(getCacheDir()); BinaryTempFileBody.setTempDirectory(getCacheDir());
LocalKeyStore.setKeyStoreLocation(getDir("KeyStore", MODE_PRIVATE).toString());
/* /*
* Enable background sync of messages * Enable background sync of messages
*/ */

View File

@ -15,7 +15,6 @@ import android.util.Log;
import com.fsck.k9.mail.Store; import com.fsck.k9.mail.Store;
import com.fsck.k9.preferences.Editor; import com.fsck.k9.preferences.Editor;
import com.fsck.k9.preferences.Storage; import com.fsck.k9.preferences.Storage;
import com.fsck.k9.security.LocalKeyStore;
public class Preferences { public class Preferences {
@ -128,7 +127,7 @@ public class Preferences {
Store.removeAccount(account); Store.removeAccount(account);
account.deleteCertificates(mContext); account.deleteCertificates();
account.delete(this); account.delete(this);
if (newAccount == account) { if (newAccount == account) {

View File

@ -363,7 +363,7 @@ public class AccountSetupCheckSettings extends K9Activity implements OnClickList
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
try { try {
mAccount.addCertificate(getApplicationContext(), mDirection, chain[0]); mAccount.addCertificate(mDirection, chain[0]);
} catch (CertificateException e) { } catch (CertificateException e) {
showErrorDialog( showErrorDialog(
R.string.account_setup_failed_dlg_certificate_message_fmt, R.string.account_setup_failed_dlg_certificate_message_fmt,

View File

@ -428,7 +428,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
mWebdavMailboxPathView.getText().toString()); mWebdavMailboxPathView.getText().toString());
} }
mAccount.deleteCertificate(this, host, port, CheckDirection.INCOMING); mAccount.deleteCertificate(host, port, CheckDirection.INCOMING);
ServerSettings settings = new ServerSettings(mStoreType, host, port, ServerSettings settings = new ServerSettings(mStoreType, host, port,
connectionSecurity, authType, username, password, extra); connectionSecurity, authType, username, password, extra);

View File

@ -312,7 +312,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener,
String newHost = mServerView.getText().toString(); String newHost = mServerView.getText().toString();
int newPort = Integer.parseInt(mPortView.getText().toString()); int newPort = Integer.parseInt(mPortView.getText().toString());
uri = new URI(smtpSchemes[securityType], userInfo, newHost, newPort, null, null, null); uri = new URI(smtpSchemes[securityType], userInfo, newHost, newPort, null, null, null);
mAccount.deleteCertificate(this, newHost, newPort, CheckDirection.OUTGOING); mAccount.deleteCertificate(newHost, newPort, CheckDirection.OUTGOING);
mAccount.setTransportUri(uri.toString()); mAccount.setTransportUri(uri.toString());
AccountSetupCheckSettings.actionCheckSettings(this, mAccount, CheckDirection.OUTGOING); AccountSetupCheckSettings.actionCheckSettings(this, mAccount, CheckDirection.OUTGOING);
} catch (UnsupportedEncodingException enc) { } catch (UnsupportedEncodingException enc) {

View File

@ -3,7 +3,6 @@ package com.fsck.k9.net.ssl;
import android.util.Log; import android.util.Log;
import com.fsck.k9.K9;
import com.fsck.k9.helper.DomainNameChecker; import com.fsck.k9.helper.DomainNameChecker;
import com.fsck.k9.mail.CertificateChainException; import com.fsck.k9.mail.CertificateChainException;
import com.fsck.k9.security.LocalKeyStore; import com.fsck.k9.security.LocalKeyStore;
@ -102,7 +101,7 @@ public final class TrustManagerFactory {
static { static {
try { try {
keyStore = LocalKeyStore.getInstance(K9.app); keyStore = LocalKeyStore.getInstance();
javax.net.ssl.TrustManagerFactory tmf = javax.net.ssl.TrustManagerFactory.getInstance("X509"); javax.net.ssl.TrustManagerFactory tmf = javax.net.ssl.TrustManagerFactory.getInstance("X509");
tmf.init((KeyStore) null); tmf.init((KeyStore) null);

View File

@ -13,7 +13,6 @@ import java.security.cert.X509Certificate;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import android.content.Context;
import android.util.Log; import android.util.Log;
import com.fsck.k9.K9; import com.fsck.k9.K9;
@ -21,26 +20,32 @@ import com.fsck.k9.K9;
public class LocalKeyStore { public class LocalKeyStore {
private static final int KEY_STORE_FILE_VERSION = 1; private static final int KEY_STORE_FILE_VERSION = 1;
private static LocalKeyStore sInstance; private static String sKeyStoreLocation;
public static void setKeyStoreLocation(String directory) {
sKeyStoreLocation = directory;
}
public synchronized static LocalKeyStore getInstance(Context context) { private static class LocalKeyStoreHolder {
if (sInstance == null) { static final LocalKeyStore INSTANCE = new LocalKeyStore();
sInstance = new LocalKeyStore(context); }
}
return sInstance; public static LocalKeyStore getInstance() {
return LocalKeyStoreHolder.INSTANCE;
} }
private final Context mContext;
private File mKeyStoreFile; private File mKeyStoreFile;
private KeyStore mKeyStore; private KeyStore mKeyStore;
private LocalKeyStore(Context context) { private LocalKeyStore() {
mContext = context.getApplicationContext(); if (sKeyStoreLocation == null) {
upgradeKeyStoreFile(); Log.e(K9.LOG_TAG, "Local key store location has not been initialized");
setKeyStoreFile(null); } else {
upgradeKeyStoreFile();
setKeyStoreFile(null);
}
} }
/** /**
@ -167,11 +172,10 @@ public class LocalKeyStore {
} }
private String getKeyStoreFilePath(int version) { private String getKeyStoreFilePath(int version) {
File dir = mContext.getDir("KeyStore", Context.MODE_PRIVATE);
if (version < 1) { if (version < 1) {
return dir + File.separator + "KeyStore.bks"; return sKeyStoreLocation + File.separator + "KeyStore.bks";
} else { } else {
return dir + File.separator + "KeyStore_v" + version + ".bks"; return sKeyStoreLocation + File.separator + "KeyStore_v" + version + ".bks";
} }
} }
} }

View File

@ -208,7 +208,7 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
mKeyStoreFile = File.createTempFile("localKeyStore", null, getContext().getCacheDir()); mKeyStoreFile = File.createTempFile("localKeyStore", null, getContext().getCacheDir());
mKeyStore = LocalKeyStore.getInstance(getContext()); mKeyStore = LocalKeyStore.getInstance();
mKeyStore.setKeyStoreFile(mKeyStoreFile); mKeyStore.setKeyStoreFile(mKeyStoreFile);
} }