This commit is contained in:
Wilco Baan Hofman 2018-08-25 14:37:48 +02:00
parent 761b704888
commit 40e8e39fb9
4 changed files with 79 additions and 120 deletions

View file

@ -15,55 +15,14 @@
package nl.eventinfra.wifisetup; package nl.eventinfra.wifisetup;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.provider.Settings.Secure;
import android.security.KeyChain;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.UnknownHostException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
/* import android.util.Base64; */
import org.json.JSONException;
import org.json.JSONObject;
import nl.eventinfra.wifisetup.R;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.AlertDialog.Builder; import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiEnterpriseConfig;
import android.net.wifi.WifiEnterpriseConfig.Eap;
import android.net.wifi.WifiEnterpriseConfig.Phase2; import android.net.wifi.WifiEnterpriseConfig.Phase2;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Build; import android.os.Build;
@ -74,18 +33,24 @@ import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.CheckBox; import android.widget.Toast;
import android.widget.ViewFlipper; import android.widget.ViewFlipper;
import java.io.InputStream;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.List;
/* import android.util.Base64; */
// API level 18 and up // API level 18 and up
import android.net.wifi.WifiEnterpriseConfig;
import android.net.wifi.WifiEnterpriseConfig.Eap;
public class WifiSetup extends Activity { public class WifiSetup extends Activity {
protected static final int SHOW_PREFERENCES = 0;
// FIXME This should be a configuration setting somehow // FIXME This should be a configuration setting somehow
private static final String INT_EAP = "eap"; private static final String INT_EAP = "eap";
private static final String INT_PHASE2 = "phase2"; private static final String INT_PHASE2 = "phase2";
@ -101,15 +66,12 @@ public class WifiSetup extends Activity {
private static final String INT_IDENTITY = "identity"; private static final String INT_IDENTITY = "identity";
private static final String INT_ANONYMOUS_IDENTITY = "anonymous_identity"; private static final String INT_ANONYMOUS_IDENTITY = "anonymous_identity";
private static final String INT_ENTERPRISEFIELD_NAME = "android.net.wifi.WifiConfiguration$EnterpriseField"; private static final String INT_ENTERPRISEFIELD_NAME = "android.net.wifi.WifiConfiguration$EnterpriseField";
// Because android.security.Credentials cannot be resolved... // Because android.security.Credentials cannot be resolved...
private static final String INT_KEYSTORE_URI = "keystore://"; private static final String INT_KEYSTORE_URI = "keystore://";
private static final String INT_CA_PREFIX = INT_KEYSTORE_URI + "CACERT_"; private static final String INT_CA_PREFIX = INT_KEYSTORE_URI + "CACERT_";
private static final String INT_PRIVATE_KEY_PREFIX = INT_KEYSTORE_URI + "USRPKEY_"; private static final String INT_PRIVATE_KEY_PREFIX = INT_KEYSTORE_URI + "USRPKEY_";
private static final String INT_PRIVATE_KEY_ID_PREFIX = "USRPKEY_"; private static final String INT_PRIVATE_KEY_ID_PREFIX = "USRPKEY_";
private static final String INT_CLIENT_CERT_PREFIX = INT_KEYSTORE_URI + "USRCERT_"; private static final String INT_CLIENT_CERT_PREFIX = INT_KEYSTORE_URI + "USRCERT_";
protected static final int SHOW_PREFERENCES = 0;
private Handler mHandler = new Handler(); private Handler mHandler = new Handler();
private EditText username; private EditText username;
private EditText password; private EditText password;
@ -127,11 +89,12 @@ public class WifiSetup extends Activity {
private String s_password; private String s_password;
private ViewFlipper flipper; private ViewFlipper flipper;
private void toastText(final String text) { static String removeQuotes(String str) {
if (toast != null) int len = str.length();
toast.cancel(); if ((len > 1) && (str.charAt(0) == '"') && (str.charAt(len - 1) == '"')) {
toast = Toast.makeText(getBaseContext(), text, Toast.LENGTH_SHORT); return str.substring(1, len - 1);
toast.show(); }
return str;
} }
/* /*
@ -144,6 +107,17 @@ public class WifiSetup extends Activity {
} }
*/ */
static String surroundWithQuotes(String string) {
return "\"" + string + "\"";
}
private void toastText(final String text) {
if (toast != null)
toast.cancel();
toast = Toast.makeText(getBaseContext(), text, Toast.LENGTH_SHORT);
toast.show();
}
// Called when the activity is first created. // Called when the activity is first created.
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -232,7 +206,7 @@ public class WifiSetup extends Activity {
} }
}; };
t.start(); t.start();
} }
}); });
@ -240,7 +214,7 @@ public class WifiSetup extends Activity {
private void saveWifiConfig() { private void saveWifiConfig() {
WifiManager wifiManager = (WifiManager) this.getApplicationContext().getSystemService(WIFI_SERVICE); WifiManager wifiManager = (WifiManager) this.getApplicationContext().getSystemService(WIFI_SERVICE);
if (!wifimanager) { if (wifiManager == null) {
return; return;
} }
wifiManager.setWifiEnabled(true); wifiManager.setWifiEnabled(true);
@ -259,19 +233,19 @@ public class WifiSetup extends Activity {
} }
if (check5g.isChecked()) { if (check5g.isChecked()) {
ssid = "34C3"; ssid = "emfcamp";
} else { } else {
ssid = "34C3-legacy"; ssid = "emfcamp-legacy18";
} }
subject_match = "/CN=radius.c3noc.net"; subject_match = "/CN=radius.emfcamp.org";
altsubject_match = "DNS:radius.c3noc.net"; altsubject_match = "DNS:radius.emfcamp.org";
s_username = username.getText().toString(); s_username = username.getText().toString();
s_password = password.getText().toString(); s_password = password.getText().toString();
realm = ""; realm = "";
if (s_username.equals("") && s_password.equals("")) { if (s_username.equals("") && s_password.equals("")) {
s_username = "34c3"; s_username = "emf";
s_password = "34c3"; s_password = "emf";
} else { } else {
if (s_username.contains("@")) { if (s_username.contains("@")) {
int idx = s_username.indexOf("@"); int idx = s_username.indexOf("@");
@ -280,7 +254,7 @@ public class WifiSetup extends Activity {
} }
// Use the existing eduroam profile if it exists. // Use the existing ssid profile if it exists.
boolean ssidExists = false; boolean ssidExists = false;
if (configs != null) { if (configs != null) {
for (WifiConfiguration config : configs) { for (WifiConfiguration config : configs) {
@ -291,12 +265,12 @@ public class WifiSetup extends Activity {
} }
} }
} }
currentConfig.SSID = surroundWithQuotes(ssid); currentConfig.SSID = surroundWithQuotes(ssid);
currentConfig.hiddenSSID = false; currentConfig.hiddenSSID = false;
currentConfig.priority = 40; currentConfig.priority = 40;
currentConfig.status = WifiConfiguration.Status.DISABLED; currentConfig.status = WifiConfiguration.Status.DISABLED;
currentConfig.allowedKeyManagement.clear(); currentConfig.allowedKeyManagement.clear();
currentConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); currentConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
@ -306,7 +280,7 @@ public class WifiSetup extends Activity {
currentConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); currentConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
currentConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); currentConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
// PairwiseCiphers (CCMP = WPA2 only) // PairwiseCiphers (CCMP = WPA2 only)
currentConfig.allowedPairwiseCiphers.clear(); currentConfig.allowedPairwiseCiphers.clear();
currentConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); currentConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
@ -329,8 +303,8 @@ public class WifiSetup extends Activity {
configMap.put(INT_EAP, "TTLS"); configMap.put(INT_EAP, "TTLS");
configMap.put(INT_PHASE2, "auth=PAP"); configMap.put(INT_PHASE2, "auth=PAP");
configMap.put(INT_ENGINE, "0"); configMap.put(INT_ENGINE, "0");
// configMap.put(INT_CA_CERT, INT_CA_PREFIX + ca_name);
// This sets the CA certificate.
applyAndroid43EnterpriseSettings(currentConfig, configMap); applyAndroid43EnterpriseSettings(currentConfig, configMap);
if (!ssidExists) { if (!ssidExists) {
@ -341,9 +315,8 @@ public class WifiSetup extends Activity {
wifiManager.enableNetwork(currentConfig.networkId, false); wifiManager.enableNetwork(currentConfig.networkId, false);
} }
wifiManager.saveConfiguration(); wifiManager.saveConfiguration();
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private void applyAndroid43EnterpriseSettings(WifiConfiguration currentConfig, HashMap<String,String> configMap) { private void applyAndroid43EnterpriseSettings(WifiConfiguration currentConfig, HashMap<String,String> configMap) {
@ -352,18 +325,18 @@ public class WifiSetup extends Activity {
InputStream in = getResources().openRawResource(R.raw.cacert); InputStream in = getResources().openRawResource(R.raw.cacert);
// InputStream in = new ByteArrayInputStream(Base64.decode(ca.replaceAll("-----(BEGIN|END) CERTIFICATE-----", ""), 0)); // InputStream in = new ByteArrayInputStream(Base64.decode(ca.replaceAll("-----(BEGIN|END) CERTIFICATE-----", ""), 0));
X509Certificate caCert = (X509Certificate) certFactory.generateCertificate(in); X509Certificate caCert = (X509Certificate) certFactory.generateCertificate(in);
WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig(); WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig();
enterpriseConfig.setPhase2Method(Phase2.PAP); enterpriseConfig.setPhase2Method(Phase2.PAP);
enterpriseConfig.setAnonymousIdentity(configMap.get(INT_ANONYMOUS_IDENTITY)); enterpriseConfig.setAnonymousIdentity(configMap.get(INT_ANONYMOUS_IDENTITY));
enterpriseConfig.setEapMethod(Eap.TTLS); enterpriseConfig.setEapMethod(Eap.TTLS);
enterpriseConfig.setCaCertificate(caCert); enterpriseConfig.setCaCertificate(caCert);
enterpriseConfig.setIdentity(s_username); enterpriseConfig.setIdentity(s_username);
enterpriseConfig.setPassword(s_password); enterpriseConfig.setPassword(s_password);
enterpriseConfig.setSubjectMatch(configMap.get(INT_SUBJECT_MATCH)); enterpriseConfig.setSubjectMatch(configMap.get(INT_SUBJECT_MATCH));
currentConfig.enterpriseConfig = enterpriseConfig; currentConfig.enterpriseConfig = enterpriseConfig;
} catch(Exception e) { } catch(Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -381,8 +354,8 @@ public class WifiSetup extends Activity {
Builder builder = new AlertDialog.Builder(this); Builder builder = new AlertDialog.Builder(this);
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.about: case R.id.about:
PackageInfo pi = null; PackageInfo pi;
try{ try{
pi = getPackageManager().getPackageInfo(getClass().getPackage().getName(), 0); pi = getPackageManager().getPackageInfo(getClass().getPackage().getName(), 0);
} catch(Exception e){ } catch(Exception e){
e.printStackTrace(); e.printStackTrace();
@ -395,16 +368,14 @@ public class WifiSetup extends Activity {
"C"+pi.versionCode+"-equi"); "C"+pi.versionCode+"-equi");
builder.setPositiveButton(getString(android.R.string.ok), null); builder.setPositiveButton(getString(android.R.string.ok), null);
builder.show(); builder.show();
return true; return true;
case R.id.exit: case R.id.exit:
System.exit(0); System.exit(0);
} }
return false; return false;
} }
/* Update the status in the main thread */ /* Update the status in the main thread */
protected void resultStatus(final boolean success, final String text) { protected void resultStatus(final boolean success, final String text) {
mHandler.post(new Runnable() { mHandler.post(new Runnable() {
@ -428,16 +399,4 @@ public class WifiSetup extends Activity {
} }
}); });
} }
static String removeQuotes(String str) {
int len = str.length();
if ((len > 1) && (str.charAt(0) == '"') && (str.charAt(len - 1) == '"')) {
return str.substring(1, len - 1);
}
return str;
}
static String surroundWithQuotes(String string) {
return "\"" + string + "\"";
}
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.3 MiB

View file

@ -2,38 +2,38 @@
<ViewFlipper <ViewFlipper
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
tools:context=".WifiSetup" android:id="@+id/viewflipper"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:id="@+id/viewflipper" tools:context=".WifiSetup"
> >
<ScrollView <ScrollView
android:id="@+id/FirstView"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:id="@+id/FirstView"
> >
<LinearLayout <LinearLayout
android:id="@+id/logonLayout"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/logonLayout" android:gravity="center_horizontal|top"
android:orientation="vertical" android:orientation="vertical">
android:gravity="center_horizontal|top">
<ImageView <ImageView
android:id="@+id/logo" android:id="@+id/logo"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="200dp" android:layout_height="200dp"
android:contentDescription="@string/LOGO"
android:padding="0dp"
android:scaleType="fitCenter" android:scaleType="fitCenter"
android:contentDescription="@string/LOGO" android:src="@drawable/emflogo" />
android:src="@drawable/ccc34c3logo"
android:padding="0dp" />
<TextView <TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:autoLink="web" android:layout_height="wrap_content"
android:layout_marginTop="15dp" android:layout_marginTop="15dp"
android:autoLink="web"
android:text="@string/twitter"/> android:text="@string/twitter"/>
<CheckBox android:id="@+id/check5g" <CheckBox android:id="@+id/check5g"
@ -47,35 +47,35 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingLeft="10dp" android:paddingLeft="10dp"
android:paddingRight="10dp" android:paddingRight="10dp"
android:textAlignment="center" android:text="@string/l5ghz"
android:text="@string/l5ghz"/> android:textAlignment="center" />
<TextView <TextView
android:layout_marginTop="5dp"
android:id="@+id/labelkeystore" android:id="@+id/labelkeystore"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="10dp" android:paddingLeft="10dp"
android:paddingRight="10dp" android:paddingRight="10dp"
android:textAlignment="center" android:text="@string/lkeystore"
android:text="@string/lkeystore"/> android:textAlignment="center" />
<LinearLayout <LinearLayout
android:id="@+id/logindata" android:id="@+id/logindata"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal|top" android:gravity="center_horizontal|top"
android:orientation="vertical"
android:visibility="gone"> android:visibility="gone">
<EditText <EditText
android:id="@+id/username" android:id="@+id/username"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:ems="10" android:ems="10"
android:hint="@string/Username"
android:inputType="textEmailAddress" android:inputType="textEmailAddress"
android:padding="5dp" android:padding="5dp"
android:layout_marginTop="15dp"
android:hint="@string/Username"
> >
<requestFocus /> <requestFocus />
</EditText> </EditText>
@ -84,11 +84,11 @@
android:id="@+id/password" android:id="@+id/password"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:ems="10" android:ems="10"
android:hint="@string/Password"
android:inputType="textPassword" android:inputType="textPassword"
android:padding="5dp" android:padding="5dp"
android:layout_marginTop="5dp"
android:hint="@string/Password"
/> />
</LinearLayout> </LinearLayout>
@ -96,8 +96,8 @@
android:id="@+id/button1" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/Logon" android:layout_margin="15dp"
android:layout_margin="15dp"/> android:text="@string/Logon" />
<Space <Space
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -107,11 +107,11 @@
</ScrollView> </ScrollView>
<LinearLayout <LinearLayout
android:id="@+id/logonLayout2"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:id="@+id/logonLayout2"
android:orientation="vertical"
android:gravity="center" android:gravity="center"
android:orientation="vertical"
> >
<TextView <TextView
android:id="@+id/resulttitle" android:id="@+id/resulttitle"

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="app_name">34C3 Wifi Setup</string> <string name="app_name">EMF Camp Wifi Setup</string>
<string name="TITLE_LOGON">Please logon</string> <string name="TITLE_LOGON">Please logon</string>
<string name="Username">username@realm</string> <string name="Username">username@realm</string>
@ -10,7 +10,7 @@
<string name="twitter">https://twitter.com/sha2017noc</string> <string name="twitter">https://twitter.com/sha2017noc</string>
<string name="c5ghz">This device supports the 5GHz band</string> <string name="c5ghz">This device supports the 5GHz band</string>
<string name="l5ghz">(Unfortunately, autodetecting 5GHz support is broken...)</string> <string name="l5ghz">(Unfortunately, autodetecting 5GHz support is broken&#8230;)</string>
<string name="lkeystore">Note: if you have no screen lock, Android refuses to store the security settings!</string> <string name="lkeystore">Note: if you have no screen lock, Android refuses to store the security settings!</string>
@ -18,5 +18,5 @@
<string name="EXIT_TITLE">Exit</string> <string name="EXIT_TITLE">Exit</string>
<string name="ABOUT_CONTENT">This small helper app creates a Wifi connection entry for the CCC/EMF/SHA2017 networks. It configures the correct SSL CA and subject name match, making it a little more secure than a hand-created entry.</string> <string name="ABOUT_CONTENT">This small helper app creates a Wifi connection entry for the CCC/EMF/SHA2017 networks. It configures the correct SSL CA and subject name match, making it a little more secure than a hand-created entry.</string>
<string name="LOGO">34C3 Logo</string> <string name="LOGO">34C3 Logo (or EMF, whatever)</string>
</resources> </resources>