2008-11-01 17:32:06 -04:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
package com.fsck.k9.activity.setup;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.AlertDialog;
|
2014-07-04 19:23:43 -04:00
|
|
|
import android.app.DialogFragment;
|
|
|
|
import android.app.FragmentTransaction;
|
2008-11-01 17:32:06 -04:00
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
2011-04-19 04:46:17 -04:00
|
|
|
import android.net.Uri;
|
2008-11-01 17:32:06 -04:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Process;
|
2009-04-15 09:05:29 -04:00
|
|
|
import android.util.Log;
|
2008-11-01 17:32:06 -04:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.TextView;
|
2014-05-25 16:45:14 -04:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.*;
|
2010-05-19 14:17:06 -04:00
|
|
|
import com.fsck.k9.activity.K9Activity;
|
|
|
|
import com.fsck.k9.controller.MessagingController;
|
2014-05-25 16:45:14 -04:00
|
|
|
import com.fsck.k9.fragment.ConfirmationDialogFragment;
|
|
|
|
import com.fsck.k9.fragment.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.mail.AuthenticationFailedException;
|
|
|
|
import com.fsck.k9.mail.CertificateValidationException;
|
2014-05-25 16:45:14 -04:00
|
|
|
import com.fsck.k9.mail.ClientCertificateRequiredException;
|
|
|
|
import com.fsck.k9.mail.ServerSettings;
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.mail.Store;
|
|
|
|
import com.fsck.k9.mail.Transport;
|
2010-10-23 21:03:29 -04:00
|
|
|
import com.fsck.k9.mail.store.WebDavStore;
|
2011-04-04 10:47:56 -04:00
|
|
|
import com.fsck.k9.mail.filter.Hex;
|
2014-05-25 16:45:14 -04:00
|
|
|
import com.fsck.k9.net.ssl.SslHelper;
|
|
|
|
import com.fsck.k9.security.KeyChainKeyManager;
|
2010-10-23 21:03:29 -04:00
|
|
|
|
2009-12-09 22:16:42 -05:00
|
|
|
import java.security.cert.CertificateException;
|
2011-04-04 10:47:56 -04:00
|
|
|
import java.security.cert.CertificateEncodingException;
|
2009-12-09 22:16:42 -05:00
|
|
|
import java.security.cert.X509Certificate;
|
2011-04-04 10:47:56 -04:00
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.MessageDigest;
|
2011-04-19 04:46:17 -04:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
2014-05-25 16:45:14 -04:00
|
|
|
import java.util.Locale;
|
2009-12-09 22:16:42 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
/**
|
|
|
|
* Checks the given settings to make sure that they can be used to send and
|
|
|
|
* receive mail.
|
2014-05-25 16:45:14 -04:00
|
|
|
*
|
2008-11-01 17:32:06 -04:00
|
|
|
* XXX NOTE: The manifest for this app has it ignore config changes, because
|
|
|
|
* it doesn't correctly deal with restarting while its thread is running.
|
|
|
|
*/
|
2014-05-25 16:45:14 -04:00
|
|
|
public class AccountSetupCheckSettings extends K9Activity implements OnClickListener,
|
|
|
|
ConfirmationDialogFragmentListener{
|
2009-11-21 17:45:39 -05:00
|
|
|
|
|
|
|
public static final int ACTIVITY_REQUEST_CODE = 1;
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
private static final String EXTRA_ACCOUNT = "account";
|
|
|
|
|
2013-11-23 13:26:57 -05:00
|
|
|
private static final String EXTRA_CHECK_DIRECTION ="checkDirection";
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2013-11-23 13:26:57 -05:00
|
|
|
public enum CheckDirection {
|
|
|
|
INCOMING,
|
|
|
|
OUTGOING
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
private Handler mHandler = new Handler();
|
|
|
|
|
|
|
|
private ProgressBar mProgressBar;
|
|
|
|
|
|
|
|
private TextView mMessageView;
|
|
|
|
|
|
|
|
private Account mAccount;
|
|
|
|
|
2014-05-25 16:45:14 -04:00
|
|
|
private boolean mIsClientCertSet;
|
|
|
|
|
2013-11-23 13:26:57 -05:00
|
|
|
private CheckDirection mDirection;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
private boolean mCanceled;
|
|
|
|
|
|
|
|
private boolean mDestroyed;
|
|
|
|
|
2014-05-25 16:45:14 -04:00
|
|
|
public static void actionCheckSettings(Activity context, Account account,
|
|
|
|
CheckDirection direction) {
|
2008-11-01 17:32:06 -04:00
|
|
|
Intent i = new Intent(context, AccountSetupCheckSettings.class);
|
2010-03-03 23:00:30 -05:00
|
|
|
i.putExtra(EXTRA_ACCOUNT, account.getUuid());
|
2013-11-23 13:26:57 -05:00
|
|
|
i.putExtra(EXTRA_CHECK_DIRECTION, direction);
|
2009-03-24 14:20:16 -04:00
|
|
|
context.startActivityForResult(i, ACTIVITY_REQUEST_CODE);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
2008-11-01 17:32:06 -04:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.account_setup_check_settings);
|
|
|
|
mMessageView = (TextView)findViewById(R.id.message);
|
|
|
|
mProgressBar = (ProgressBar)findViewById(R.id.progress);
|
|
|
|
((Button)findViewById(R.id.cancel)).setOnClickListener(this);
|
|
|
|
|
|
|
|
setMessage(R.string.account_setup_check_settings_retr_info_msg);
|
|
|
|
mProgressBar.setIndeterminate(true);
|
|
|
|
|
2010-03-03 23:00:30 -05:00
|
|
|
String accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT);
|
|
|
|
mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
|
2013-11-23 13:26:57 -05:00
|
|
|
mDirection = (CheckDirection) getIntent().getSerializableExtra(EXTRA_CHECK_DIRECTION);
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
new Thread() {
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void run() {
|
2009-11-21 17:45:39 -05:00
|
|
|
Store store = null;
|
2008-11-01 17:32:06 -04:00
|
|
|
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
|
|
|
if (mDestroyed) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mCanceled) {
|
2008-11-01 17:32:06 -04:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2013-01-17 09:24:22 -05:00
|
|
|
|
|
|
|
final MessagingController ctrl = MessagingController.getInstance(getApplication());
|
|
|
|
ctrl.clearCertificateErrorNotifications(AccountSetupCheckSettings.this,
|
2013-11-23 13:26:57 -05:00
|
|
|
mAccount, mDirection);
|
2013-01-17 09:24:22 -05:00
|
|
|
|
2013-11-23 13:26:57 -05:00
|
|
|
if (mDirection.equals(CheckDirection.INCOMING)) {
|
2014-05-25 16:45:14 -04:00
|
|
|
// refresh URI that stores settings in order to include
|
|
|
|
// client certificate set by user
|
|
|
|
store = mAccount.getRemoteStore(mIsClientCertSet);
|
2010-10-23 21:03:29 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (store instanceof WebDavStore) {
|
2010-10-23 21:03:29 -04:00
|
|
|
setMessage(R.string.account_setup_check_settings_authenticate);
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-10-23 21:03:29 -04:00
|
|
|
setMessage(R.string.account_setup_check_settings_check_incoming_msg);
|
|
|
|
}
|
2009-11-21 17:45:39 -05:00
|
|
|
store.checkSettings();
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (store instanceof WebDavStore) {
|
2010-10-23 21:03:29 -04:00
|
|
|
setMessage(R.string.account_setup_check_settings_fetch);
|
|
|
|
}
|
|
|
|
MessagingController.getInstance(getApplication()).listFoldersSynchronous(mAccount, true, null);
|
2011-04-05 05:19:27 -04:00
|
|
|
MessagingController.getInstance(getApplication()).synchronizeMailbox(mAccount, mAccount.getInboxFolderName(), null, null);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mDestroyed) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mCanceled) {
|
2008-11-01 17:32:06 -04:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2013-11-23 13:26:57 -05:00
|
|
|
if (mDirection.equals(CheckDirection.OUTGOING)) {
|
2011-02-06 17:09:48 -05:00
|
|
|
if (!(mAccount.getRemoteStore() instanceof WebDavStore)) {
|
2010-10-23 21:03:29 -04:00
|
|
|
setMessage(R.string.account_setup_check_settings_check_outgoing_msg);
|
|
|
|
}
|
2010-03-03 23:00:30 -05:00
|
|
|
Transport transport = Transport.getInstance(mAccount);
|
2008-11-01 17:32:06 -04:00
|
|
|
transport.close();
|
|
|
|
transport.open();
|
|
|
|
transport.close();
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mDestroyed) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mCanceled) {
|
2008-11-01 17:32:06 -04:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setResult(RESULT_OK);
|
|
|
|
finish();
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (final AuthenticationFailedException afe) {
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Error while testing settings", afe);
|
2008-11-01 17:32:06 -04:00
|
|
|
showErrorDialog(
|
2009-11-21 17:45:39 -05:00
|
|
|
R.string.account_setup_failed_dlg_auth_message_fmt,
|
|
|
|
afe.getMessage() == null ? "" : afe.getMessage());
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (final CertificateValidationException cve) {
|
2014-05-25 16:45:14 -04:00
|
|
|
handleCertificateValidationException(cve);
|
|
|
|
} catch (final ClientCertificateRequiredException ccr) {
|
|
|
|
handleClientCertificateRequiredException(ccr);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (final Throwable t) {
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Error while testing settings", t);
|
2008-11-01 17:32:06 -04:00
|
|
|
showErrorDialog(
|
2009-11-21 17:45:39 -05:00
|
|
|
R.string.account_setup_failed_dlg_server_message_fmt,
|
|
|
|
(t.getMessage() == null ? "" : t.getMessage()));
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
2008-12-02 19:04:24 -05:00
|
|
|
|
2009-11-21 17:45:39 -05:00
|
|
|
}
|
|
|
|
.start();
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2014-05-25 16:45:14 -04:00
|
|
|
private void handleCertificateValidationException(CertificateValidationException cve) {
|
|
|
|
Log.e(K9.LOG_TAG, "Error while testing settings", cve);
|
|
|
|
|
|
|
|
X509Certificate[] chain = cve.getCertChain();
|
|
|
|
// Avoid NullPointerException in acceptKeyDialog()
|
|
|
|
if (chain != null) {
|
|
|
|
acceptKeyDialog(
|
|
|
|
R.string.account_setup_failed_dlg_certificate_message_fmt,
|
|
|
|
cve);
|
|
|
|
} else {
|
|
|
|
showErrorDialog(
|
|
|
|
R.string.account_setup_failed_dlg_server_message_fmt,
|
|
|
|
(cve.getMessage() == null ? "" : cve.getMessage()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleClientCertificateRequiredException(ClientCertificateRequiredException ccr) {
|
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Client certificate alias required: " + ccr.getMessage());
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the KeyChain API is not available on this Android
|
|
|
|
* version, inform user and abort
|
|
|
|
*/
|
|
|
|
if (!SslHelper.isClientCertificateSupportAvailable()) {
|
|
|
|
mHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
showDialogFragment(R.id.dialog_client_certificate_not_supported);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// abort
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String alias = null;
|
|
|
|
if (CheckDirection.INCOMING.equals(mDirection)) {
|
|
|
|
ServerSettings storeSettings = Store.decodeStoreUri(mAccount.getStoreUri());
|
|
|
|
alias = storeSettings.clientCertificateAlias;
|
|
|
|
} else if (CheckDirection.OUTGOING.equals(mDirection)) {
|
|
|
|
ServerSettings transportSettings = Transport.decodeTransportUri(mAccount.getTransportUri());
|
|
|
|
alias = transportSettings.clientCertificateAlias;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Client certificate alias is: " + alias);
|
|
|
|
|
|
|
|
alias = KeyChainKeyManager.interactivelyChooseClientCertificateAlias(
|
|
|
|
AccountSetupCheckSettings.this,
|
|
|
|
ccr.getKeyTypes(),
|
|
|
|
ccr.getIssuers(),
|
|
|
|
ccr.getHostName(),
|
|
|
|
ccr.getPort(),
|
|
|
|
alias);
|
|
|
|
|
|
|
|
// Note: KeyChainKeyManager gives back "" on cancel
|
|
|
|
if (alias != null && alias.equals("")) {
|
|
|
|
alias = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// save client certificate alias
|
|
|
|
if (alias != null) {
|
|
|
|
if (CheckDirection.INCOMING.equals(mDirection)) {
|
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Setting store client certificate alias to: " + alias);
|
|
|
|
|
|
|
|
// Set incoming server client certificate alias
|
|
|
|
String storeUri = mAccount.getStoreUri();
|
|
|
|
ServerSettings incoming = Store.decodeStoreUri(storeUri);
|
|
|
|
ServerSettings newIncoming = incoming.newClientCertificateAlias(alias);
|
|
|
|
String newStoreUri = Store.createStoreUri(newIncoming);
|
|
|
|
mAccount.setStoreUri(newStoreUri);
|
|
|
|
} else if (CheckDirection.OUTGOING.equals(mDirection)) {
|
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Setting transport client certificate alias to: " + alias);
|
|
|
|
|
|
|
|
// Set outgoing server client certificate alias
|
|
|
|
String transportUri = mAccount.getTransportUri();
|
|
|
|
ServerSettings outgoing = Transport.decodeTransportUri(transportUri);
|
|
|
|
ServerSettings newOutgoing = outgoing.newClientCertificateAlias(alias);
|
|
|
|
String newTransportUri = Transport.createTransportUri(newOutgoing);
|
|
|
|
mAccount.setTransportUri(newTransportUri);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the account settings
|
|
|
|
mAccount.save(Preferences.getPreferences(AccountSetupCheckSettings.this));
|
|
|
|
|
|
|
|
// try again
|
|
|
|
AccountSetupCheckSettings.actionCheckSettings(AccountSetupCheckSettings.this, mAccount,
|
|
|
|
mDirection);
|
|
|
|
} else {
|
|
|
|
showErrorDialog(R.string.dialog_client_certificate_required);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onDestroy() {
|
2008-11-01 17:32:06 -04:00
|
|
|
super.onDestroy();
|
|
|
|
mDestroyed = true;
|
|
|
|
mCanceled = true;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private void setMessage(final int resId) {
|
|
|
|
mHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
if (mDestroyed) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mMessageView.setText(getString(resId));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-05-25 16:45:14 -04:00
|
|
|
private void acceptKeyDialog(final int msgResId, final CertificateValidationException ex) {
|
2011-02-06 17:09:48 -05:00
|
|
|
mHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
if (mDestroyed) {
|
2008-12-02 19:04:24 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
String exMessage = "Unknown Error";
|
2009-11-21 17:45:39 -05:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (ex != null) {
|
|
|
|
if (ex.getCause() != null) {
|
|
|
|
if (ex.getCause().getCause() != null) {
|
2009-11-21 17:45:39 -05:00
|
|
|
exMessage = ex.getCause().getCause().getMessage();
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2009-11-21 17:45:39 -05:00
|
|
|
exMessage = ex.getCause().getMessage();
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2009-11-21 17:45:39 -05:00
|
|
|
exMessage = ex.getMessage();
|
|
|
|
}
|
2008-12-02 19:04:24 -05:00
|
|
|
}
|
2009-11-21 17:45:39 -05:00
|
|
|
|
2008-12-02 19:04:24 -05:00
|
|
|
mProgressBar.setIndeterminate(false);
|
2011-09-30 02:18:00 -04:00
|
|
|
StringBuilder chainInfo = new StringBuilder(100);
|
2011-04-04 10:47:56 -04:00
|
|
|
MessageDigest sha1 = null;
|
|
|
|
try {
|
|
|
|
sha1 = MessageDigest.getInstance("SHA-1");
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
Log.e(K9.LOG_TAG, "Error while initializing MessageDigest", e);
|
|
|
|
}
|
2013-08-27 18:48:07 -04:00
|
|
|
|
|
|
|
final X509Certificate[] chain = ex.getCertChain();
|
|
|
|
// We already know chain != null (tested before calling this method)
|
2011-02-06 17:09:48 -05:00
|
|
|
for (int i = 0; i < chain.length; i++) {
|
2009-11-21 17:45:39 -05:00
|
|
|
// display certificate chain information
|
2011-06-01 16:03:56 -04:00
|
|
|
//TODO: localize this strings
|
2011-10-06 12:20:11 -04:00
|
|
|
chainInfo.append("Certificate chain[").append(i).append("]:\n");
|
|
|
|
chainInfo.append("Subject: ").append(chain[i].getSubjectDN().toString()).append("\n");
|
2011-04-19 04:46:17 -04:00
|
|
|
|
|
|
|
// display SubjectAltNames too
|
|
|
|
// (the user may be mislead into mistrusting a certificate
|
|
|
|
// by a subjectDN not matching the server even though a
|
|
|
|
// SubjectAltName matches)
|
|
|
|
try {
|
2011-06-01 16:03:56 -04:00
|
|
|
final Collection < List<? >> subjectAlternativeNames = chain[i].getSubjectAlternativeNames();
|
|
|
|
if (subjectAlternativeNames != null) {
|
|
|
|
// The list of SubjectAltNames may be very long
|
|
|
|
//TODO: localize this string
|
2011-10-06 12:20:11 -04:00
|
|
|
StringBuilder altNamesText = new StringBuilder();
|
|
|
|
altNamesText.append("Subject has ").append(subjectAlternativeNames.size()).append(" alternative names\n");
|
2011-06-01 16:03:56 -04:00
|
|
|
|
|
|
|
// we need these for matching
|
|
|
|
String storeURIHost = (Uri.parse(mAccount.getStoreUri())).getHost();
|
|
|
|
String transportURIHost = (Uri.parse(mAccount.getTransportUri())).getHost();
|
|
|
|
|
|
|
|
for (List<?> subjectAlternativeName : subjectAlternativeNames) {
|
|
|
|
Integer type = (Integer)subjectAlternativeName.get(0);
|
|
|
|
Object value = subjectAlternativeName.get(1);
|
|
|
|
String name = "";
|
|
|
|
switch (type.intValue()) {
|
|
|
|
case 0:
|
|
|
|
Log.w(K9.LOG_TAG, "SubjectAltName of type OtherName not supported.");
|
|
|
|
continue;
|
|
|
|
case 1: // RFC822Name
|
|
|
|
name = (String)value;
|
|
|
|
break;
|
|
|
|
case 2: // DNSName
|
|
|
|
name = (String)value;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
Log.w(K9.LOG_TAG, "unsupported SubjectAltName of type x400Address");
|
|
|
|
continue;
|
|
|
|
case 4:
|
|
|
|
Log.w(K9.LOG_TAG, "unsupported SubjectAltName of type directoryName");
|
|
|
|
continue;
|
|
|
|
case 5:
|
|
|
|
Log.w(K9.LOG_TAG, "unsupported SubjectAltName of type ediPartyName");
|
|
|
|
continue;
|
|
|
|
case 6: // Uri
|
|
|
|
name = (String)value;
|
|
|
|
break;
|
|
|
|
case 7: // ip-address
|
|
|
|
name = (String)value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Log.w(K9.LOG_TAG, "unsupported SubjectAltName of unknown type");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if some of the SubjectAltNames match the store or transport -host,
|
|
|
|
// display them
|
|
|
|
if (name.equalsIgnoreCase(storeURIHost) || name.equalsIgnoreCase(transportURIHost)) {
|
|
|
|
//TODO: localize this string
|
2011-10-06 12:20:11 -04:00
|
|
|
altNamesText.append("Subject(alt): ").append(name).append(",...\n");
|
2012-07-06 08:21:47 -04:00
|
|
|
} else if (name.startsWith("*.") && (
|
|
|
|
storeURIHost.endsWith(name.substring(2)) ||
|
|
|
|
transportURIHost.endsWith(name.substring(2)))) {
|
|
|
|
//TODO: localize this string
|
|
|
|
altNamesText.append("Subject(alt): ").append(name).append(",...\n");
|
2011-06-01 16:03:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
chainInfo.append(altNamesText);
|
|
|
|
}
|
|
|
|
} catch (Exception e1) {
|
|
|
|
// don't fail just because of subjectAltNames
|
|
|
|
Log.w(K9.LOG_TAG, "cannot display SubjectAltNames in dialog", e1);
|
|
|
|
}
|
2011-04-19 04:46:17 -04:00
|
|
|
|
2011-10-06 12:20:11 -04:00
|
|
|
chainInfo.append("Issuer: ").append(chain[i].getIssuerDN().toString()).append("\n");
|
2011-04-04 10:47:56 -04:00
|
|
|
if (sha1 != null) {
|
|
|
|
sha1.reset();
|
|
|
|
try {
|
|
|
|
char[] sha1sum = Hex.encodeHex(sha1.digest(chain[i].getEncoded()));
|
2011-10-06 12:20:11 -04:00
|
|
|
chainInfo.append("Fingerprint (SHA-1): ").append(new String(sha1sum)).append("\n");
|
2011-04-04 10:47:56 -04:00
|
|
|
} catch (CertificateEncodingException e) {
|
|
|
|
Log.e(K9.LOG_TAG, "Error while encoding certificate", e);
|
|
|
|
}
|
|
|
|
}
|
2008-12-02 19:04:24 -05:00
|
|
|
}
|
|
|
|
|
2014-05-25 16:45:14 -04:00
|
|
|
// TODO: refactor with DialogFragment.
|
|
|
|
// This is difficult because we need to pass through chain[0] for onClick()
|
2008-12-02 19:04:24 -05:00
|
|
|
new AlertDialog.Builder(AccountSetupCheckSettings.this)
|
2009-11-21 17:45:39 -05:00
|
|
|
.setTitle(getString(R.string.account_setup_failed_dlg_invalid_certificate_title))
|
|
|
|
//.setMessage(getString(R.string.account_setup_failed_dlg_invalid_certificate)
|
2011-02-06 17:09:48 -05:00
|
|
|
.setMessage(getString(msgResId, exMessage)
|
2009-11-21 17:45:39 -05:00
|
|
|
+ " " + chainInfo.toString()
|
|
|
|
)
|
|
|
|
.setCancelable(true)
|
|
|
|
.setPositiveButton(
|
|
|
|
getString(R.string.account_setup_failed_dlg_invalid_certificate_accept),
|
2011-02-06 17:09:48 -05:00
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2014-05-25 16:45:14 -04:00
|
|
|
acceptCertificate(chain[0]);
|
2009-11-21 17:45:39 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(
|
|
|
|
getString(R.string.account_setup_failed_dlg_invalid_certificate_reject),
|
2011-02-06 17:09:48 -05:00
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2009-11-21 17:45:39 -05:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
2008-12-02 19:04:24 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-05-25 16:45:14 -04:00
|
|
|
/**
|
|
|
|
* Permanently accepts a certificate for the INCOMING or OUTGOING direction
|
|
|
|
* by adding it to the local key store.
|
|
|
|
*
|
|
|
|
* @param certificate
|
|
|
|
*/
|
|
|
|
private void acceptCertificate(X509Certificate certificate) {
|
|
|
|
try {
|
|
|
|
mAccount.addCertificate(mDirection, certificate);
|
|
|
|
} catch (CertificateException e) {
|
|
|
|
showErrorDialog(
|
|
|
|
R.string.account_setup_failed_dlg_certificate_message_fmt,
|
|
|
|
e.getMessage() == null ? "" : e.getMessage());
|
|
|
|
}
|
|
|
|
AccountSetupCheckSettings.actionCheckSettings(AccountSetupCheckSettings.this, mAccount,
|
|
|
|
mDirection);
|
|
|
|
}
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onActivityResult(int reqCode, int resCode, Intent data) {
|
2009-11-21 17:45:39 -05:00
|
|
|
setResult(resCode);
|
|
|
|
finish();
|
2008-12-02 19:04:24 -05:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private void onCancel() {
|
2008-11-01 17:32:06 -04:00
|
|
|
mCanceled = true;
|
|
|
|
setMessage(R.string.account_setup_check_settings_canceling_msg);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onClick(View v) {
|
|
|
|
switch (v.getId()) {
|
|
|
|
case R.id.cancel:
|
|
|
|
onCancel();
|
|
|
|
break;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
2014-05-25 16:45:14 -04:00
|
|
|
|
|
|
|
private void showErrorDialog(final int msgResId, final Object... args) {
|
|
|
|
mHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
showDialogFragment(R.id.dialog_account_setup_error, getString(msgResId, args));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showDialogFragment(int dialogId) {
|
|
|
|
showDialogFragment(dialogId, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showDialogFragment(int dialogId, String customMessage) {
|
|
|
|
if (mDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mProgressBar.setIndeterminate(false);
|
|
|
|
|
|
|
|
DialogFragment fragment;
|
|
|
|
switch (dialogId) {
|
|
|
|
case R.id.dialog_account_setup_error: {
|
|
|
|
fragment = ConfirmationDialogFragment.newInstance(dialogId,
|
|
|
|
getString(R.string.account_setup_failed_dlg_title),
|
|
|
|
customMessage,
|
|
|
|
getString(R.string.account_setup_failed_dlg_edit_details_action),
|
|
|
|
getString(R.string.account_setup_failed_dlg_continue_action)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R.id.dialog_client_certificate_not_supported: {
|
|
|
|
fragment = ConfirmationDialogFragment.newInstance(dialogId,
|
|
|
|
getString(R.string.dialog_client_certificate_title),
|
|
|
|
getString(R.string.dialog_client_certificate_not_supported),
|
|
|
|
getString(android.R.string.ok)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new RuntimeException("Called showDialog(int) with unknown dialog id.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 19:23:43 -04:00
|
|
|
FragmentTransaction ta = getFragmentManager().beginTransaction();
|
2014-05-25 16:45:14 -04:00
|
|
|
ta.add(fragment, getDialogTag(dialogId));
|
|
|
|
ta.commitAllowingStateLoss();
|
|
|
|
|
|
|
|
// TODO: commitAllowingStateLoss() is used to prevent https://code.google.com/p/android/issues/detail?id=23761
|
|
|
|
// but is a bad...
|
|
|
|
//fragment.show(ta, getDialogTag(dialogId));
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getDialogTag(int dialogId) {
|
|
|
|
return String.format(Locale.US, "dialog-%d", dialogId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doPositiveClick(int dialogId) {
|
|
|
|
switch (dialogId) {
|
|
|
|
case R.id.dialog_account_setup_error: {
|
|
|
|
finish();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doNegativeClick(int dialogId) {
|
|
|
|
switch (dialogId) {
|
|
|
|
case R.id.dialog_account_setup_error:
|
|
|
|
case R.id.dialog_client_certificate_not_supported: {
|
|
|
|
mCanceled = false;
|
|
|
|
setResult(RESULT_OK);
|
|
|
|
finish();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void dialogCancelled(int dialogId) {
|
|
|
|
// nothing to do here...
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|