2012-07-17 17:23:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Tobias Brunner
|
|
|
|
* Copyright (C) 2012 Giuliano Grassi
|
|
|
|
* Copyright (C) 2012 Ralf Sager
|
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.strongswan.android.ui;
|
|
|
|
|
2012-08-14 09:47:32 +00:00
|
|
|
import java.security.cert.X509Certificate;
|
|
|
|
|
2012-07-17 17:23:21 +00:00
|
|
|
import org.strongswan.android.R;
|
|
|
|
import org.strongswan.android.data.VpnProfile;
|
|
|
|
import org.strongswan.android.data.VpnProfileDataSource;
|
2012-08-27 14:51:41 +00:00
|
|
|
import org.strongswan.android.data.VpnType;
|
2012-08-07 16:03:51 +00:00
|
|
|
import org.strongswan.android.logic.TrustedCertificateManager;
|
2014-05-30 10:40:53 +00:00
|
|
|
import org.strongswan.android.security.TrustedCertificateEntry;
|
2012-07-17 17:23:21 +00:00
|
|
|
|
|
|
|
import android.app.Activity;
|
2012-08-07 16:03:51 +00:00
|
|
|
import android.app.AlertDialog;
|
2013-07-03 14:27:36 +00:00
|
|
|
import android.app.Dialog;
|
|
|
|
import android.app.DialogFragment;
|
2012-08-28 12:09:18 +00:00
|
|
|
import android.content.Context;
|
2012-08-07 16:03:51 +00:00
|
|
|
import android.content.DialogInterface;
|
2012-07-17 17:45:23 +00:00
|
|
|
import android.content.Intent;
|
2012-08-28 12:09:18 +00:00
|
|
|
import android.os.AsyncTask;
|
2012-07-17 17:23:21 +00:00
|
|
|
import android.os.Bundle;
|
2012-08-28 12:09:18 +00:00
|
|
|
import android.security.KeyChain;
|
|
|
|
import android.security.KeyChainAliasCallback;
|
|
|
|
import android.security.KeyChainException;
|
2013-07-03 14:27:36 +00:00
|
|
|
import android.text.Html;
|
2012-07-17 17:23:21 +00:00
|
|
|
import android.util.Log;
|
2012-07-17 17:45:23 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2012-08-07 16:03:51 +00:00
|
|
|
import android.view.View;
|
2012-08-14 09:47:32 +00:00
|
|
|
import android.view.View.OnClickListener;
|
2012-08-27 14:51:41 +00:00
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.AdapterView.OnItemSelectedListener;
|
2012-08-07 16:03:51 +00:00
|
|
|
import android.widget.CheckBox;
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
import android.widget.CompoundButton.OnCheckedChangeListener;
|
2012-07-17 17:23:21 +00:00
|
|
|
import android.widget.EditText;
|
2012-08-27 14:51:41 +00:00
|
|
|
import android.widget.Spinner;
|
2012-08-27 15:58:09 +00:00
|
|
|
import android.widget.TwoLineListItem;
|
2012-07-17 17:23:21 +00:00
|
|
|
|
|
|
|
public class VpnProfileDetailActivity extends Activity
|
|
|
|
{
|
2012-08-14 09:47:32 +00:00
|
|
|
private static final int SELECT_TRUSTED_CERTIFICATE = 0;
|
|
|
|
|
2012-07-17 17:23:21 +00:00
|
|
|
private VpnProfileDataSource mDataSource;
|
|
|
|
private Long mId;
|
2012-08-14 09:47:32 +00:00
|
|
|
private TrustedCertificateEntry mCertEntry;
|
2012-08-28 12:09:18 +00:00
|
|
|
private String mUserCertLoading;
|
|
|
|
private TrustedCertificateEntry mUserCertEntry;
|
2012-08-27 14:51:41 +00:00
|
|
|
private VpnType mVpnType = VpnType.IKEV2_EAP;
|
2012-07-17 17:23:21 +00:00
|
|
|
private VpnProfile mProfile;
|
|
|
|
private EditText mName;
|
|
|
|
private EditText mGateway;
|
2012-08-27 14:51:41 +00:00
|
|
|
private Spinner mSelectVpnType;
|
|
|
|
private ViewGroup mUsernamePassword;
|
2012-07-17 17:23:21 +00:00
|
|
|
private EditText mUsername;
|
|
|
|
private EditText mPassword;
|
2012-08-28 12:09:18 +00:00
|
|
|
private ViewGroup mUserCertificate;
|
|
|
|
private TwoLineListItem mSelectUserCert;
|
2012-08-07 16:03:51 +00:00
|
|
|
private CheckBox mCheckAuto;
|
2012-08-27 15:58:09 +00:00
|
|
|
private TwoLineListItem mSelectCert;
|
2013-07-03 14:27:36 +00:00
|
|
|
private TwoLineListItem mTncNotice;
|
2012-08-14 09:47:32 +00:00
|
|
|
|
2012-07-17 17:23:21 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
/* the title is set when we load the profile, if any */
|
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
|
|
|
mDataSource = new VpnProfileDataSource(this);
|
|
|
|
mDataSource.open();
|
|
|
|
|
|
|
|
setContentView(R.layout.profile_detail_view);
|
|
|
|
|
|
|
|
mName = (EditText)findViewById(R.id.name);
|
|
|
|
mGateway = (EditText)findViewById(R.id.gateway);
|
2012-08-27 14:51:41 +00:00
|
|
|
mSelectVpnType = (Spinner)findViewById(R.id.vpn_type);
|
2013-07-03 14:27:36 +00:00
|
|
|
mTncNotice = (TwoLineListItem)findViewById(R.id.tnc_notice);
|
2012-08-27 14:51:41 +00:00
|
|
|
|
|
|
|
mUsernamePassword = (ViewGroup)findViewById(R.id.username_password_group);
|
2012-07-17 17:23:21 +00:00
|
|
|
mUsername = (EditText)findViewById(R.id.username);
|
2012-08-27 14:51:41 +00:00
|
|
|
mPassword = (EditText)findViewById(R.id.password);
|
2012-07-17 17:23:21 +00:00
|
|
|
|
2012-08-28 12:09:18 +00:00
|
|
|
mUserCertificate = (ViewGroup)findViewById(R.id.user_certificate_group);
|
|
|
|
mSelectUserCert = (TwoLineListItem)findViewById(R.id.select_user_certificate);
|
|
|
|
|
2012-08-07 16:03:51 +00:00
|
|
|
mCheckAuto = (CheckBox)findViewById(R.id.ca_auto);
|
2012-08-27 15:58:09 +00:00
|
|
|
mSelectCert = (TwoLineListItem)findViewById(R.id.select_certificate);
|
2012-08-27 14:51:41 +00:00
|
|
|
|
|
|
|
mSelectVpnType.setOnItemSelectedListener(new OnItemSelectedListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
|
|
|
|
{
|
|
|
|
mVpnType = VpnType.values()[position];
|
2012-08-28 12:09:18 +00:00
|
|
|
updateCredentialView();
|
2012-08-27 14:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNothingSelected(AdapterView<?> parent)
|
|
|
|
{ /* should not happen */
|
|
|
|
mVpnType = VpnType.IKEV2_EAP;
|
2012-08-28 12:09:18 +00:00
|
|
|
updateCredentialView();
|
2012-08-27 14:51:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-07-03 14:27:36 +00:00
|
|
|
mTncNotice.getText1().setText(R.string.tnc_notice_title);
|
|
|
|
mTncNotice.getText2().setText(R.string.tnc_notice_subtitle);
|
|
|
|
mTncNotice.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v)
|
|
|
|
{
|
|
|
|
new TncNoticeDialog().show(VpnProfileDetailActivity.this.getFragmentManager(), "TncNotice");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-08-28 12:09:18 +00:00
|
|
|
mSelectUserCert.setOnClickListener(new SelectUserCertOnClickListener());
|
|
|
|
|
2012-08-07 16:03:51 +00:00
|
|
|
mCheckAuto.setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
|
|
|
|
{
|
2012-08-14 09:47:32 +00:00
|
|
|
updateCertificateSelector();
|
|
|
|
}
|
|
|
|
});
|
2012-08-07 16:03:51 +00:00
|
|
|
|
2012-08-14 09:47:32 +00:00
|
|
|
mSelectCert.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v)
|
|
|
|
{
|
|
|
|
Intent intent = new Intent(VpnProfileDetailActivity.this, TrustedCertificatesActivity.class);
|
|
|
|
startActivityForResult(intent, SELECT_TRUSTED_CERTIFICATE);
|
2012-08-07 16:03:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-07-17 17:23:21 +00:00
|
|
|
mId = savedInstanceState == null ? null : savedInstanceState.getLong(VpnProfileDataSource.KEY_ID);
|
|
|
|
if (mId == null)
|
|
|
|
{
|
|
|
|
Bundle extras = getIntent().getExtras();
|
|
|
|
mId = extras == null ? null : extras.getLong(VpnProfileDataSource.KEY_ID);
|
|
|
|
}
|
|
|
|
|
2012-08-14 09:47:32 +00:00
|
|
|
loadProfileData(savedInstanceState);
|
2012-08-07 16:03:51 +00:00
|
|
|
|
2012-08-28 12:09:18 +00:00
|
|
|
updateCredentialView();
|
2012-08-14 09:47:32 +00:00
|
|
|
updateCertificateSelector();
|
2012-07-17 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy()
|
|
|
|
{
|
|
|
|
super.onDestroy();
|
|
|
|
mDataSource.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState)
|
|
|
|
{
|
|
|
|
super.onSaveInstanceState(outState);
|
2012-08-14 09:31:37 +00:00
|
|
|
if (mId != null)
|
|
|
|
{
|
|
|
|
outState.putLong(VpnProfileDataSource.KEY_ID, mId);
|
|
|
|
}
|
2012-08-28 12:09:18 +00:00
|
|
|
if (mUserCertEntry != null)
|
|
|
|
{
|
|
|
|
outState.putString(VpnProfileDataSource.KEY_USER_CERTIFICATE, mUserCertEntry.getAlias());
|
|
|
|
}
|
2012-08-14 09:47:32 +00:00
|
|
|
if (mCertEntry != null)
|
|
|
|
{
|
|
|
|
outState.putString(VpnProfileDataSource.KEY_CERTIFICATE, mCertEntry.getAlias());
|
|
|
|
}
|
2012-07-17 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
2012-07-17 17:45:23 +00:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu)
|
|
|
|
{
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.profile_edit, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item)
|
|
|
|
{
|
|
|
|
switch (item.getItemId())
|
|
|
|
{
|
|
|
|
case android.R.id.home:
|
|
|
|
case R.id.menu_cancel:
|
|
|
|
finish();
|
|
|
|
return true;
|
|
|
|
case R.id.menu_accept:
|
|
|
|
saveProfile();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 09:47:32 +00:00
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data)
|
|
|
|
{
|
|
|
|
switch (requestCode)
|
|
|
|
{
|
|
|
|
case SELECT_TRUSTED_CERTIFICATE:
|
|
|
|
if (resultCode == RESULT_OK)
|
|
|
|
{
|
|
|
|
String alias = data.getStringExtra(VpnProfileDataSource.KEY_CERTIFICATE);
|
|
|
|
X509Certificate certificate = TrustedCertificateManager.getInstance().getCACertificateFromAlias(alias);
|
|
|
|
mCertEntry = certificate == null ? null : new TrustedCertificateEntry(alias, certificate);
|
|
|
|
updateCertificateSelector();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-27 14:51:41 +00:00
|
|
|
/**
|
2012-08-28 12:09:18 +00:00
|
|
|
* Update the UI to enter credentials depending on the type of VPN currently selected
|
2012-08-27 14:51:41 +00:00
|
|
|
*/
|
2012-08-28 12:09:18 +00:00
|
|
|
private void updateCredentialView()
|
2012-08-27 14:51:41 +00:00
|
|
|
{
|
|
|
|
mUsernamePassword.setVisibility(mVpnType.getRequiresUsernamePassword() ? View.VISIBLE : View.GONE);
|
2012-08-28 12:09:18 +00:00
|
|
|
mUserCertificate.setVisibility(mVpnType.getRequiresCertificate() ? View.VISIBLE : View.GONE);
|
2013-07-03 14:27:36 +00:00
|
|
|
mTncNotice.setVisibility(mVpnType.getEnableBYOD() ? View.VISIBLE : View.GONE);
|
2012-08-28 12:09:18 +00:00
|
|
|
|
|
|
|
if (mVpnType.getRequiresCertificate())
|
|
|
|
{
|
|
|
|
if (mUserCertLoading != null)
|
|
|
|
{
|
|
|
|
mSelectUserCert.getText1().setText(mUserCertLoading);
|
|
|
|
mSelectUserCert.getText2().setText(R.string.loading);
|
|
|
|
}
|
|
|
|
else if (mUserCertEntry != null)
|
|
|
|
{ /* clear any errors and set the new data */
|
|
|
|
mSelectUserCert.getText1().setError(null);
|
|
|
|
mSelectUserCert.getText1().setText(mUserCertEntry.getAlias());
|
|
|
|
mSelectUserCert.getText2().setText(mUserCertEntry.getCertificate().getSubjectDN().toString());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mSelectUserCert.getText1().setText(R.string.profile_user_select_certificate_label);
|
|
|
|
mSelectUserCert.getText2().setText(R.string.profile_user_select_certificate);
|
|
|
|
}
|
|
|
|
}
|
2012-08-27 14:51:41 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 16:03:51 +00:00
|
|
|
/**
|
|
|
|
* Show an alert in case the previously selected certificate is not found anymore
|
|
|
|
* or the user did not select a certificate in the spinner.
|
|
|
|
*/
|
|
|
|
private void showCertificateAlert()
|
|
|
|
{
|
|
|
|
AlertDialog.Builder adb = new AlertDialog.Builder(VpnProfileDetailActivity.this);
|
|
|
|
adb.setTitle(R.string.alert_text_nocertfound_title);
|
|
|
|
adb.setMessage(R.string.alert_text_nocertfound);
|
|
|
|
adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int id)
|
|
|
|
{
|
|
|
|
dialog.cancel();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
adb.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-14 09:47:32 +00:00
|
|
|
* Update the CA certificate selection UI depending on whether the
|
|
|
|
* certificate should be automatically selected or not.
|
2012-08-07 16:03:51 +00:00
|
|
|
*/
|
2012-08-14 09:47:32 +00:00
|
|
|
private void updateCertificateSelector()
|
2012-08-07 16:03:51 +00:00
|
|
|
{
|
2012-08-14 09:47:32 +00:00
|
|
|
if (!mCheckAuto.isChecked())
|
2012-08-07 16:03:51 +00:00
|
|
|
{
|
2012-08-14 09:47:32 +00:00
|
|
|
mSelectCert.setEnabled(true);
|
|
|
|
mSelectCert.setVisibility(View.VISIBLE);
|
2012-08-07 16:03:51 +00:00
|
|
|
|
2012-08-14 09:47:32 +00:00
|
|
|
if (mCertEntry != null)
|
|
|
|
{
|
2012-08-27 15:58:09 +00:00
|
|
|
mSelectCert.getText1().setText(mCertEntry.getSubjectPrimary());
|
|
|
|
mSelectCert.getText2().setText(mCertEntry.getSubjectSecondary());
|
2012-08-14 09:47:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-27 15:58:09 +00:00
|
|
|
mSelectCert.getText1().setText(R.string.profile_ca_select_certificate_label);
|
|
|
|
mSelectCert.getText2().setText(R.string.profile_ca_select_certificate);
|
2012-08-14 09:47:32 +00:00
|
|
|
}
|
2012-08-07 16:03:51 +00:00
|
|
|
}
|
2012-08-14 09:47:32 +00:00
|
|
|
else
|
2012-08-07 16:03:51 +00:00
|
|
|
{
|
2012-08-14 09:47:32 +00:00
|
|
|
mSelectCert.setEnabled(false);
|
|
|
|
mSelectCert.setVisibility(View.GONE);
|
2012-08-07 16:03:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-17 17:45:23 +00:00
|
|
|
/**
|
|
|
|
* Save or update the profile depending on whether we actually have a
|
|
|
|
* profile object or not (this was created in updateProfileData)
|
|
|
|
*/
|
|
|
|
private void saveProfile()
|
|
|
|
{
|
|
|
|
if (verifyInput())
|
|
|
|
{
|
|
|
|
if (mProfile != null)
|
|
|
|
{
|
|
|
|
updateProfileData();
|
|
|
|
mDataSource.updateVpnProfile(mProfile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mProfile = new VpnProfile();
|
|
|
|
updateProfileData();
|
|
|
|
mDataSource.insertProfile(mProfile);
|
|
|
|
}
|
|
|
|
setResult(RESULT_OK, new Intent().putExtra(VpnProfileDataSource.KEY_ID, mProfile.getId()));
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify the user input and display error messages.
|
|
|
|
* @return true if the input is valid
|
|
|
|
*/
|
|
|
|
private boolean verifyInput()
|
|
|
|
{
|
|
|
|
boolean valid = true;
|
|
|
|
if (mGateway.getText().toString().trim().isEmpty())
|
|
|
|
{
|
|
|
|
mGateway.setError(getString(R.string.alert_text_no_input_gateway));
|
|
|
|
valid = false;
|
|
|
|
}
|
2012-08-27 14:51:41 +00:00
|
|
|
if (mVpnType.getRequiresUsernamePassword())
|
2012-07-17 17:45:23 +00:00
|
|
|
{
|
2012-08-27 14:51:41 +00:00
|
|
|
if (mUsername.getText().toString().trim().isEmpty())
|
|
|
|
{
|
|
|
|
mUsername.setError(getString(R.string.alert_text_no_input_username));
|
|
|
|
valid = false;
|
|
|
|
}
|
2012-07-17 17:45:23 +00:00
|
|
|
}
|
2012-08-28 12:09:18 +00:00
|
|
|
if (mVpnType.getRequiresCertificate() && mUserCertEntry == null)
|
|
|
|
{ /* let's show an error icon */
|
|
|
|
mSelectUserCert.getText1().setError("");
|
|
|
|
valid = false;
|
|
|
|
}
|
2012-08-14 09:47:32 +00:00
|
|
|
if (!mCheckAuto.isChecked() && mCertEntry == null)
|
2012-08-07 16:03:51 +00:00
|
|
|
{
|
|
|
|
showCertificateAlert();
|
|
|
|
valid = false;
|
|
|
|
}
|
2012-07-17 17:45:23 +00:00
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the profile object with the data entered by the user
|
|
|
|
*/
|
|
|
|
private void updateProfileData()
|
|
|
|
{
|
|
|
|
/* the name is optional, we default to the gateway if none is given */
|
|
|
|
String name = mName.getText().toString().trim();
|
|
|
|
String gateway = mGateway.getText().toString().trim();
|
|
|
|
mProfile.setName(name.isEmpty() ? gateway : name);
|
|
|
|
mProfile.setGateway(gateway);
|
2012-08-27 14:51:41 +00:00
|
|
|
mProfile.setVpnType(mVpnType);
|
|
|
|
if (mVpnType.getRequiresUsernamePassword())
|
|
|
|
{
|
|
|
|
mProfile.setUsername(mUsername.getText().toString().trim());
|
|
|
|
String password = mPassword.getText().toString().trim();
|
|
|
|
password = password.isEmpty() ? null : password;
|
|
|
|
mProfile.setPassword(password);
|
|
|
|
}
|
2012-08-28 12:09:18 +00:00
|
|
|
if (mVpnType.getRequiresCertificate())
|
|
|
|
{
|
|
|
|
mProfile.setUserCertificateAlias(mUserCertEntry.getAlias());
|
|
|
|
}
|
2012-08-14 09:47:32 +00:00
|
|
|
String certAlias = mCheckAuto.isChecked() ? null : mCertEntry.getAlias();
|
|
|
|
mProfile.setCertificateAlias(certAlias);
|
2012-07-17 17:45:23 +00:00
|
|
|
}
|
|
|
|
|
2012-07-17 17:23:21 +00:00
|
|
|
/**
|
|
|
|
* Load an existing profile if we got an ID
|
2012-08-14 09:47:32 +00:00
|
|
|
*
|
|
|
|
* @param savedInstanceState previously saved state
|
2012-07-17 17:23:21 +00:00
|
|
|
*/
|
2012-08-14 09:47:32 +00:00
|
|
|
private void loadProfileData(Bundle savedInstanceState)
|
2012-07-17 17:23:21 +00:00
|
|
|
{
|
2012-08-28 12:09:18 +00:00
|
|
|
String useralias = null, alias = null;
|
2012-08-14 09:47:32 +00:00
|
|
|
|
2012-07-17 17:23:21 +00:00
|
|
|
getActionBar().setTitle(R.string.add_profile);
|
2012-08-27 14:53:07 +00:00
|
|
|
if (mId != null && mId != 0)
|
2012-07-17 17:23:21 +00:00
|
|
|
{
|
|
|
|
mProfile = mDataSource.getVpnProfile(mId);
|
|
|
|
if (mProfile != null)
|
|
|
|
{
|
|
|
|
mName.setText(mProfile.getName());
|
|
|
|
mGateway.setText(mProfile.getGateway());
|
2012-08-27 14:51:41 +00:00
|
|
|
mVpnType = mProfile.getVpnType();
|
2012-07-17 17:23:21 +00:00
|
|
|
mUsername.setText(mProfile.getUsername());
|
|
|
|
mPassword.setText(mProfile.getPassword());
|
2012-08-28 12:09:18 +00:00
|
|
|
useralias = mProfile.getUserCertificateAlias();
|
2012-08-14 09:47:32 +00:00
|
|
|
alias = mProfile.getCertificateAlias();
|
2012-07-17 17:23:21 +00:00
|
|
|
getActionBar().setTitle(mProfile.getName());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log.e(VpnProfileDetailActivity.class.getSimpleName(),
|
|
|
|
"VPN profile with id " + mId + " not found");
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
2012-08-14 09:47:32 +00:00
|
|
|
|
2012-08-27 14:51:41 +00:00
|
|
|
mSelectVpnType.setSelection(mVpnType.ordinal());
|
|
|
|
|
2012-08-28 12:09:18 +00:00
|
|
|
/* check if the user selected a user certificate previously */
|
|
|
|
useralias = savedInstanceState == null ? useralias: savedInstanceState.getString(VpnProfileDataSource.KEY_USER_CERTIFICATE);
|
|
|
|
if (useralias != null)
|
|
|
|
{
|
|
|
|
UserCertificateLoader loader = new UserCertificateLoader(this, useralias);
|
|
|
|
mUserCertLoading = useralias;
|
|
|
|
loader.execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if the user selected a CA certificate previously */
|
2012-08-14 09:47:32 +00:00
|
|
|
alias = savedInstanceState == null ? alias : savedInstanceState.getString(VpnProfileDataSource.KEY_CERTIFICATE);
|
|
|
|
mCheckAuto.setChecked(alias == null);
|
|
|
|
if (alias != null)
|
|
|
|
{
|
|
|
|
X509Certificate certificate = TrustedCertificateManager.getInstance().getCACertificateFromAlias(alias);
|
|
|
|
if (certificate != null)
|
|
|
|
{
|
|
|
|
mCertEntry = new TrustedCertificateEntry(alias, certificate);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* previously selected certificate is not here anymore */
|
|
|
|
showCertificateAlert();
|
|
|
|
mCertEntry = null;
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 17:23:21 +00:00
|
|
|
}
|
2012-08-28 12:09:18 +00:00
|
|
|
|
|
|
|
private class SelectUserCertOnClickListener implements OnClickListener, KeyChainAliasCallback
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onClick(View v)
|
|
|
|
{
|
|
|
|
String useralias = mUserCertEntry != null ? mUserCertEntry.getAlias() : null;
|
|
|
|
KeyChain.choosePrivateKeyAlias(VpnProfileDetailActivity.this, this, new String[] { "RSA" }, null, null, -1, useralias);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void alias(final String alias)
|
|
|
|
{
|
|
|
|
if (alias != null)
|
|
|
|
{ /* otherwise the dialog was canceled, the request denied */
|
|
|
|
try
|
|
|
|
{
|
|
|
|
final X509Certificate[] chain = KeyChain.getCertificateChain(VpnProfileDetailActivity.this, alias);
|
|
|
|
/* alias() is not called from our main thread */
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
if (chain != null && chain.length > 0)
|
|
|
|
{
|
|
|
|
mUserCertEntry = new TrustedCertificateEntry(alias, chain[0]);
|
|
|
|
}
|
|
|
|
updateCredentialView();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (KeyChainException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the selected user certificate asynchronously. This cannot be done
|
|
|
|
* from the main thread as getCertificateChain() calls back to our main
|
|
|
|
* thread to bind to the KeyChain service resulting in a deadlock.
|
|
|
|
*/
|
|
|
|
private class UserCertificateLoader extends AsyncTask<Void, Void, X509Certificate>
|
|
|
|
{
|
|
|
|
private final Context mContext;
|
|
|
|
private final String mAlias;
|
|
|
|
|
|
|
|
public UserCertificateLoader(Context context, String alias)
|
|
|
|
{
|
|
|
|
mContext = context;
|
|
|
|
mAlias = alias;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected X509Certificate doInBackground(Void... params)
|
|
|
|
{
|
|
|
|
X509Certificate[] chain = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
chain = KeyChain.getCertificateChain(mContext, mAlias);
|
|
|
|
}
|
|
|
|
catch (KeyChainException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
if (chain != null && chain.length > 0)
|
|
|
|
{
|
|
|
|
return chain[0];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(X509Certificate result)
|
|
|
|
{
|
|
|
|
if (result != null)
|
|
|
|
{
|
|
|
|
mUserCertEntry = new TrustedCertificateEntry(mAlias, result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* previously selected certificate is not here anymore */
|
|
|
|
mSelectUserCert.getText1().setError("");
|
|
|
|
mUserCertEntry = null;
|
|
|
|
}
|
|
|
|
mUserCertLoading = null;
|
|
|
|
updateCredentialView();
|
|
|
|
}
|
|
|
|
}
|
2013-07-03 14:27:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dialog with notification message if EAP-TNC is used.
|
|
|
|
*/
|
|
|
|
public static class TncNoticeDialog extends DialogFragment
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
return new AlertDialog.Builder(getActivity())
|
|
|
|
.setTitle(R.string.tnc_notice_title)
|
|
|
|
.setMessage(Html.fromHtml(getString(R.string.tnc_notice_details)))
|
|
|
|
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int id)
|
|
|
|
{
|
|
|
|
dialog.dismiss();
|
|
|
|
}
|
|
|
|
}).create();
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 17:23:21 +00:00
|
|
|
}
|