mirror of
https://github.com/moparisthebest/k-9
synced 2025-02-11 12:40:22 -05:00
Remove LocalKeyStore's dependency on K9.app
This commit is contained in:
parent
16aa119d65
commit
8d85fe7e9e
@ -1873,15 +1873,16 @@ 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(CheckDirection direction, X509Certificate certificate)
|
public void addCertificate(Context context, CheckDirection direction,
|
||||||
throws CertificateException {
|
X509Certificate certificate) throws CertificateException {
|
||||||
Uri uri;
|
Uri uri;
|
||||||
if (direction.equals(CheckDirection.INCOMING)) {
|
if (direction.equals(CheckDirection.INCOMING)) {
|
||||||
uri = Uri.parse(getStoreUri());
|
uri = Uri.parse(getStoreUri());
|
||||||
} else {
|
} else {
|
||||||
uri = Uri.parse(getTransportUri());
|
uri = Uri.parse(getTransportUri());
|
||||||
}
|
}
|
||||||
LocalKeyStore.getInstance().addCertificate(uri.getHost(), uri.getPort(), certificate);
|
LocalKeyStore localKeyStore = LocalKeyStore.getInstance(context);
|
||||||
|
localKeyStore.addCertificate(uri.getHost(), uri.getPort(), certificate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1889,7 +1890,8 @@ 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(String newHost, int newPort, CheckDirection direction) {
|
public void deleteCertificate(Context context, String newHost, int newPort,
|
||||||
|
CheckDirection direction) {
|
||||||
Uri uri;
|
Uri uri;
|
||||||
if (direction.equals(CheckDirection.INCOMING)) {
|
if (direction.equals(CheckDirection.INCOMING)) {
|
||||||
uri = Uri.parse(getStoreUri());
|
uri = Uri.parse(getStoreUri());
|
||||||
@ -1899,7 +1901,8 @@ public class Account implements BaseAccount {
|
|||||||
String oldHost = uri.getHost();
|
String oldHost = uri.getHost();
|
||||||
int oldPort = uri.getPort();
|
int oldPort = uri.getPort();
|
||||||
if (!newHost.equals(oldHost) || newPort != oldPort) {
|
if (!newHost.equals(oldHost) || newPort != oldPort) {
|
||||||
LocalKeyStore.getInstance().deleteCertificate(oldHost, oldPort);
|
LocalKeyStore localKeyStore = LocalKeyStore.getInstance(context);
|
||||||
|
localKeyStore.deleteCertificate(oldHost, oldPort);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1907,8 +1910,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() {
|
public void deleteCertificates(Context context) {
|
||||||
LocalKeyStore localKeyStore = LocalKeyStore.getInstance();
|
LocalKeyStore localKeyStore = LocalKeyStore.getInstance(context);
|
||||||
|
|
||||||
Uri uri = Uri.parse(getStoreUri());
|
Uri uri = Uri.parse(getStoreUri());
|
||||||
localKeyStore.deleteCertificate(uri.getHost(), uri.getPort());
|
localKeyStore.deleteCertificate(uri.getHost(), uri.getPort());
|
||||||
|
@ -128,7 +128,7 @@ public class Preferences {
|
|||||||
|
|
||||||
Store.removeAccount(account);
|
Store.removeAccount(account);
|
||||||
|
|
||||||
account.deleteCertificates();
|
account.deleteCertificates(mContext);
|
||||||
account.delete(this);
|
account.delete(this);
|
||||||
|
|
||||||
if (newAccount == account) {
|
if (newAccount == account) {
|
||||||
|
@ -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(mDirection, chain[0]);
|
mAccount.addCertificate(getApplicationContext(), 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,
|
||||||
|
@ -428,7 +428,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
|
|||||||
mWebdavMailboxPathView.getText().toString());
|
mWebdavMailboxPathView.getText().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
mAccount.deleteCertificate(host, port, CheckDirection.INCOMING);
|
mAccount.deleteCertificate(this, 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);
|
||||||
|
|
||||||
|
@ -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(newHost, newPort, CheckDirection.OUTGOING);
|
mAccount.deleteCertificate(this, 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) {
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
package com.fsck.k9.net.ssl;
|
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;
|
||||||
@ -100,7 +102,7 @@ public final class TrustManagerFactory {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
keyStore = LocalKeyStore.getInstance();
|
keyStore = LocalKeyStore.getInstance(K9.app);
|
||||||
|
|
||||||
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);
|
||||||
|
@ -20,15 +20,25 @@ 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 final LocalKeyStore sInstance = new LocalKeyStore();
|
|
||||||
private File mKeyStoreFile;
|
|
||||||
private KeyStore mKeyStore;
|
|
||||||
|
|
||||||
public static LocalKeyStore getInstance() {
|
private static LocalKeyStore sInstance;
|
||||||
|
|
||||||
|
|
||||||
|
public static LocalKeyStore getInstance(Context context) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
sInstance = new LocalKeyStore(context);
|
||||||
|
}
|
||||||
return sInstance;
|
return sInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private LocalKeyStore() {
|
|
||||||
|
private final Context mContext;
|
||||||
|
private File mKeyStoreFile;
|
||||||
|
private KeyStore mKeyStore;
|
||||||
|
|
||||||
|
|
||||||
|
private LocalKeyStore(Context context) {
|
||||||
|
mContext = context.getApplicationContext();
|
||||||
upgradeKeyStoreFile();
|
upgradeKeyStoreFile();
|
||||||
setKeyStoreFile(null);
|
setKeyStoreFile(null);
|
||||||
}
|
}
|
||||||
@ -157,12 +167,11 @@ 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 K9.app.getDir("KeyStore", Context.MODE_PRIVATE)
|
return dir + File.separator + "KeyStore.bks";
|
||||||
+ File.separator + "KeyStore.bks";
|
|
||||||
} else {
|
} else {
|
||||||
return K9.app.getDir("KeyStore", Context.MODE_PRIVATE)
|
return dir + File.separator + "KeyStore_v" + version + ".bks";
|
||||||
+ File.separator + "KeyStore_v" + version + ".bks";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package com.fsck.k9.net.ssl;
|
|||||||
|
|
||||||
import javax.net.ssl.X509TrustManager;
|
import javax.net.ssl.X509TrustManager;
|
||||||
|
|
||||||
import com.fsck.k9.net.ssl.TrustManagerFactory;
|
|
||||||
import com.fsck.k9.security.LocalKeyStore;
|
import com.fsck.k9.security.LocalKeyStore;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
@ -208,9 +207,8 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
mKeyStoreFile = File.createTempFile("localKeyStore", null, getContext()
|
mKeyStoreFile = File.createTempFile("localKeyStore", null, getContext().getCacheDir());
|
||||||
.getCacheDir());
|
mKeyStore = LocalKeyStore.getInstance(getContext());
|
||||||
mKeyStore = LocalKeyStore.getInstance();
|
|
||||||
mKeyStore.setKeyStoreFile(mKeyStoreFile);
|
mKeyStore.setKeyStoreFile(mKeyStoreFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user