From dab8d3807f92dc3a63ff3b6ee09aafe6a7be8a57 Mon Sep 17 00:00:00 2001 From: Joe Steele Date: Wed, 4 Dec 2013 12:10:12 -0500 Subject: [PATCH] Change LocalKeyStore error reporting The error reporting assures an exception is thrown if setKeyStoreFile(null) is called without a prior call to setKeyStoreLocation(String directory). Also, fix TrustManagerFactoryTest indentation. --- src/com/fsck/k9/security/LocalKeyStore.java | 24 ++++++++++++++----- .../k9/net/ssl/TrustManagerFactoryTest.java | 6 ++--- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/com/fsck/k9/security/LocalKeyStore.java b/src/com/fsck/k9/security/LocalKeyStore.java index f7b34939c..d432ab294 100644 --- a/src/com/fsck/k9/security/LocalKeyStore.java +++ b/src/com/fsck/k9/security/LocalKeyStore.java @@ -40,11 +40,17 @@ public class LocalKeyStore { private LocalKeyStore() { - if (sKeyStoreLocation == null) { - Log.e(K9.LOG_TAG, "Local key store location has not been initialized"); - } else { + try { upgradeKeyStoreFile(); setKeyStoreFile(null); + } catch (CertificateException e) { + /* + * Can happen if setKeyStoreLocation(String directory) has not been + * called before the first call to getInstance(). Not necessarily an + * error, presuming setKeyStoreFile(File) is called next with a + * non-null File. + */ + Log.w(K9.LOG_TAG, "Local key store has not been initialized"); } } @@ -56,8 +62,11 @@ public class LocalKeyStore { * {@link File} containing locally saved certificates. May be 0 * length, in which case it is deleted and recreated. May be * {@code null}, in which case a default file location is used. + * @throws CertificateException + * Occurs if {@code file == null} and + * {@code setKeyStoreLocation(directory)} was not called previously. */ - public synchronized void setKeyStoreFile(File file) { + public synchronized void setKeyStoreFile(File file) throws CertificateException { if (file == null) { file = new File(getKeyStoreFilePath(KEY_STORE_FILE_VERSION)); } @@ -164,14 +173,17 @@ public class LocalKeyStore { } } - private void upgradeKeyStoreFile() { + private void upgradeKeyStoreFile() throws CertificateException { if (KEY_STORE_FILE_VERSION > 0) { // Blow away version "0" because certificate aliases have changed. new File(getKeyStoreFilePath(0)).delete(); } } - private String getKeyStoreFilePath(int version) { + private String getKeyStoreFilePath(int version) throws CertificateException { + if (sKeyStoreLocation == null) { + throw new CertificateException("Local key store location has not been initialized"); + } if (version < 1) { return sKeyStoreLocation + File.separator + "KeyStore.bks"; } else { diff --git a/tests/src/com/fsck/k9/net/ssl/TrustManagerFactoryTest.java b/tests/src/com/fsck/k9/net/ssl/TrustManagerFactoryTest.java index 01c91ef93..7fcb34611 100644 --- a/tests/src/com/fsck/k9/net/ssl/TrustManagerFactoryTest.java +++ b/tests/src/com/fsck/k9/net/ssl/TrustManagerFactoryTest.java @@ -73,7 +73,7 @@ public class TrustManagerFactoryTest extends AndroidTestCase { + "zOztD2HZfxhIz3uH2gXmqUo=\n" + "-----END CERTIFICATE-----\n"; - private static final String CERT3 = + private static final String CERT3 = "-----BEGIN CERTIFICATE-----\n" + "MIIDjDCCAnSgAwIBAgIBATANBgkqhkiG9w0BAQUFADBNMQswCQYDVQQGEwJDSDET\n" + "MBEGA1UECAwKU29tZS1TdGF0ZTEMMAoGA1UECgwDSy05MRswGQYDVQQDDBJ0ZXN0\n" @@ -96,7 +96,7 @@ public class TrustManagerFactoryTest extends AndroidTestCase { + "Dcf5/G8bZe2DnavBQfML1wI5d7NUWE8CWb95SsIvFXI0qZE0oIR+axBVl9u97uaO\n" + "-----END CERTIFICATE-----\n"; - private static final String DIGI_CERT = + private static final String DIGI_CERT = "-----BEGIN CERTIFICATE-----\n" + "MIIG5jCCBc6gAwIBAgIQAze5KDR8YKauxa2xIX84YDANBgkqhkiG9w0BAQUFADBs\n" + "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" @@ -137,7 +137,7 @@ public class TrustManagerFactoryTest extends AndroidTestCase { + "zVMSW4SOwg/H7ZMZ2cn6j1g0djIvruFQFGHUqFijyDATI+/GJYw2jxyA\n" + "-----END CERTIFICATE-----\n"; - private static final String GITHUB_CERT = + private static final String GITHUB_CERT = "-----BEGIN CERTIFICATE-----\n" + "MIIHOjCCBiKgAwIBAgIQBH++LkveAITSyvjj7P5wWDANBgkqhkiG9w0BAQUFADBp\n" + "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"