Search in sources :

Example 1 with MemorizingTrustManager

use of eu.siacs.conversations.services.MemorizingTrustManager in project Conversations by siacs.

the class XmppConnection method getSSLSocketFactory.

private SSLSocketFactory getSSLSocketFactory() throws NoSuchAlgorithmException, KeyManagementException {
    final SSLContext sc = SSLSocketHelper.getSSLContext();
    final MemorizingTrustManager trustManager = this.mXmppConnectionService.getMemorizingTrustManager();
    final KeyManager[] keyManager;
    if (account.getPrivateKeyAlias() != null) {
        keyManager = new KeyManager[] { new MyKeyManager() };
    } else {
        keyManager = null;
    }
    final String domain = account.getServer();
    sc.init(keyManager, new X509TrustManager[] { mInteractive ? trustManager.getInteractive(domain) : trustManager.getNonInteractive(domain) }, mXmppConnectionService.getRNG());
    return sc.getSocketFactory();
}
Also used : MemorizingTrustManager(eu.siacs.conversations.services.MemorizingTrustManager) SSLContext(javax.net.ssl.SSLContext) X509KeyManager(javax.net.ssl.X509KeyManager) KeyManager(javax.net.ssl.KeyManager)

Example 2 with MemorizingTrustManager

use of eu.siacs.conversations.services.MemorizingTrustManager in project Conversations by siacs.

the class SettingsActivity method onStart.

@Override
public void onStart() {
    super.onStart();
    PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
    changeOmemoSettingSummary();
    if (QuickConversationsService.isQuicksy()) {
        final PreferenceCategory connectionOptions = (PreferenceCategory) mSettingsFragment.findPreference("connection_options");
        final PreferenceCategory groupChats = (PreferenceCategory) mSettingsFragment.findPreference("group_chats");
        final Preference channelDiscoveryMethod = mSettingsFragment.findPreference("channel_discovery_method");
        PreferenceScreen expert = (PreferenceScreen) mSettingsFragment.findPreference("expert");
        if (connectionOptions != null) {
            expert.removePreference(connectionOptions);
        }
        if (groupChats != null && channelDiscoveryMethod != null) {
            groupChats.removePreference(channelDiscoveryMethod);
        }
    }
    PreferenceScreen mainPreferenceScreen = (PreferenceScreen) mSettingsFragment.findPreference("main_screen");
    PreferenceCategory attachmentsCategory = (PreferenceCategory) mSettingsFragment.findPreference("attachments");
    CheckBoxPreference locationPlugin = (CheckBoxPreference) mSettingsFragment.findPreference("use_share_location_plugin");
    if (attachmentsCategory != null && locationPlugin != null) {
        if (!GeoHelper.isLocationPluginInstalled(this)) {
            attachmentsCategory.removePreference(locationPlugin);
        }
    }
    // this feature is only available on Huawei Android 6.
    PreferenceScreen huaweiPreferenceScreen = (PreferenceScreen) mSettingsFragment.findPreference("huawei");
    if (huaweiPreferenceScreen != null) {
        Intent intent = huaweiPreferenceScreen.getIntent();
        // remove when Api version is above M (Version 6.0) or if the intent is not callable
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M || !isCallable(intent)) {
            PreferenceCategory generalCategory = (PreferenceCategory) mSettingsFragment.findPreference("general");
            generalCategory.removePreference(huaweiPreferenceScreen);
            if (generalCategory.getPreferenceCount() == 0) {
                if (mainPreferenceScreen != null) {
                    mainPreferenceScreen.removePreference(generalCategory);
                }
            }
        }
    }
    ListPreference automaticMessageDeletionList = (ListPreference) mSettingsFragment.findPreference(AUTOMATIC_MESSAGE_DELETION);
    if (automaticMessageDeletionList != null) {
        final int[] choices = getResources().getIntArray(R.array.automatic_message_deletion_values);
        CharSequence[] entries = new CharSequence[choices.length];
        CharSequence[] entryValues = new CharSequence[choices.length];
        for (int i = 0; i < choices.length; ++i) {
            entryValues[i] = String.valueOf(choices[i]);
            if (choices[i] == 0) {
                entries[i] = getString(R.string.never);
            } else {
                entries[i] = TimeFrameUtils.resolve(this, 1000L * choices[i]);
            }
        }
        automaticMessageDeletionList.setEntries(entries);
        automaticMessageDeletionList.setEntryValues(entryValues);
    }
    boolean removeLocation = new Intent("eu.siacs.conversations.location.request").resolveActivity(getPackageManager()) == null;
    boolean removeVoice = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION).resolveActivity(getPackageManager()) == null;
    ListPreference quickAction = (ListPreference) mSettingsFragment.findPreference("quick_action");
    if (quickAction != null && (removeLocation || removeVoice)) {
        ArrayList<CharSequence> entries = new ArrayList<>(Arrays.asList(quickAction.getEntries()));
        ArrayList<CharSequence> entryValues = new ArrayList<>(Arrays.asList(quickAction.getEntryValues()));
        int index = entryValues.indexOf("location");
        if (index > 0 && removeLocation) {
            entries.remove(index);
            entryValues.remove(index);
        }
        index = entryValues.indexOf("voice");
        if (index > 0 && removeVoice) {
            entries.remove(index);
            entryValues.remove(index);
        }
        quickAction.setEntries(entries.toArray(new CharSequence[entries.size()]));
        quickAction.setEntryValues(entryValues.toArray(new CharSequence[entryValues.size()]));
    }
    final Preference removeCertsPreference = mSettingsFragment.findPreference("remove_trusted_certificates");
    if (removeCertsPreference != null) {
        removeCertsPreference.setOnPreferenceClickListener(preference -> {
            final MemorizingTrustManager mtm = xmppConnectionService.getMemorizingTrustManager();
            final ArrayList<String> aliases = Collections.list(mtm.getCertificates());
            if (aliases.size() == 0) {
                displayToast(getString(R.string.toast_no_trusted_certs));
                return true;
            }
            final ArrayList<Integer> selectedItems = new ArrayList<>();
            final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(SettingsActivity.this);
            dialogBuilder.setTitle(getResources().getString(R.string.dialog_manage_certs_title));
            dialogBuilder.setMultiChoiceItems(aliases.toArray(new CharSequence[aliases.size()]), null, (dialog, indexSelected, isChecked) -> {
                if (isChecked) {
                    selectedItems.add(indexSelected);
                } else if (selectedItems.contains(indexSelected)) {
                    selectedItems.remove(Integer.valueOf(indexSelected));
                }
                ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(selectedItems.size() > 0);
            });
            dialogBuilder.setPositiveButton(getResources().getString(R.string.dialog_manage_certs_positivebutton), (dialog, which) -> {
                int count = selectedItems.size();
                if (count > 0) {
                    for (int i = 0; i < count; i++) {
                        try {
                            Integer item = Integer.valueOf(selectedItems.get(i).toString());
                            String alias = aliases.get(item);
                            mtm.deleteCertificate(alias);
                        } catch (KeyStoreException e) {
                            e.printStackTrace();
                            displayToast("Error: " + e.getLocalizedMessage());
                        }
                    }
                    if (xmppConnectionServiceBound) {
                        reconnectAccounts();
                    }
                    displayToast(getResources().getQuantityString(R.plurals.toast_delete_certificates, count, count));
                }
            });
            dialogBuilder.setNegativeButton(getResources().getString(R.string.dialog_manage_certs_negativebutton), null);
            AlertDialog removeCertsDialog = dialogBuilder.create();
            removeCertsDialog.show();
            removeCertsDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
            return true;
        });
    }
    final Preference createBackupPreference = mSettingsFragment.findPreference("create_backup");
    if (createBackupPreference != null) {
        createBackupPreference.setSummary(getString(R.string.pref_create_backup_summary, FileBackend.getBackupDirectory(this)));
        createBackupPreference.setOnPreferenceClickListener(preference -> {
            if (hasStoragePermission(REQUEST_CREATE_BACKUP)) {
                createBackup();
            }
            return true;
        });
    }
    if (Config.ONLY_INTERNAL_STORAGE) {
        final Preference cleanCachePreference = mSettingsFragment.findPreference("clean_cache");
        if (cleanCachePreference != null) {
            cleanCachePreference.setOnPreferenceClickListener(preference -> cleanCache());
        }
        final Preference cleanPrivateStoragePreference = mSettingsFragment.findPreference("clean_private_storage");
        if (cleanPrivateStoragePreference != null) {
            cleanPrivateStoragePreference.setOnPreferenceClickListener(preference -> cleanPrivateStorage());
        }
    }
    final Preference deleteOmemoPreference = mSettingsFragment.findPreference("delete_omemo_identities");
    if (deleteOmemoPreference != null) {
        deleteOmemoPreference.setOnPreferenceClickListener(preference -> deleteOmemoIdentities());
    }
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) PreferenceScreen(android.preference.PreferenceScreen) CheckBoxPreference(android.preference.CheckBoxPreference) ArrayList(java.util.ArrayList) Intent(android.content.Intent) ListPreference(android.preference.ListPreference) KeyStoreException(java.security.KeyStoreException) MemorizingTrustManager(eu.siacs.conversations.services.MemorizingTrustManager) PreferenceCategory(android.preference.PreferenceCategory) CheckBoxPreference(android.preference.CheckBoxPreference) ListPreference(android.preference.ListPreference) Preference(android.preference.Preference)

Aggregations

MemorizingTrustManager (eu.siacs.conversations.services.MemorizingTrustManager)2 Intent (android.content.Intent)1 CheckBoxPreference (android.preference.CheckBoxPreference)1 ListPreference (android.preference.ListPreference)1 Preference (android.preference.Preference)1 PreferenceCategory (android.preference.PreferenceCategory)1 PreferenceScreen (android.preference.PreferenceScreen)1 AlertDialog (androidx.appcompat.app.AlertDialog)1 KeyStoreException (java.security.KeyStoreException)1 ArrayList (java.util.ArrayList)1 KeyManager (javax.net.ssl.KeyManager)1 SSLContext (javax.net.ssl.SSLContext)1 X509KeyManager (javax.net.ssl.X509KeyManager)1