MTMExample.java

  1package de.duenndns.mtmexample;
  2
  3import android.app.Activity;
  4import android.app.AlertDialog;
  5import android.content.DialogInterface;
  6import android.os.Bundle;
  7import android.os.Handler;
  8import android.view.View;
  9import android.view.View.OnClickListener;
 10import android.view.Window;
 11import android.widget.ArrayAdapter;
 12import android.widget.EditText;
 13import android.widget.TextView;
 14
 15import java.net.URL;
 16import java.security.KeyStoreException;
 17import java.util.ArrayList;
 18import java.util.Collections;
 19
 20import javax.net.ssl.HostnameVerifier;
 21import javax.net.ssl.SSLContext;
 22import javax.net.ssl.HttpsURLConnection;
 23import javax.net.ssl.X509TrustManager;
 24
 25import de.duenndns.ssl.MemorizingTrustManager;
 26
 27/**
 28 * Example to demonstrate the use of MemorizingTrustManager on HTTPS
 29 * sockets.
 30 */
 31public class MTMExample extends Activity implements OnClickListener
 32{
 33	MemorizingTrustManager mtm;
 34	
 35	TextView content;
 36	HostnameVerifier defaultverifier;
 37	EditText urlinput;
 38	String text;
 39	Handler hdlr;
 40
 41	/** Creates the Activity and registers a MemorizingTrustManager. */
 42	@Override
 43	public void onCreate(Bundle savedInstanceState)
 44	{
 45		super.onCreate(savedInstanceState);
 46		JULHandler.initialize();
 47		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
 48		setContentView(R.layout.mtmexample);
 49
 50
 51		// set up gui elements
 52		findViewById(R.id.connect).setOnClickListener(this);
 53		content = (TextView)findViewById(R.id.content);
 54		urlinput = (EditText)findViewById(R.id.url);
 55
 56		// register handler for background thread
 57		hdlr = new Handler();
 58
 59		// Here, the MemorizingTrustManager is activated for HTTPS
 60		try {
 61			// set location of the keystore
 62			MemorizingTrustManager.setKeyStoreFile("private", "sslkeys.bks");
 63
 64			// register MemorizingTrustManager for HTTPS
 65			SSLContext sc = SSLContext.getInstance("TLS");
 66			mtm = new MemorizingTrustManager(this);
 67			sc.init(null, new X509TrustManager[] { mtm },
 68					new java.security.SecureRandom());
 69			HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
 70			HttpsURLConnection.setDefaultHostnameVerifier(
 71					mtm.wrapHostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier()));
 72
 73			// disable redirects to reduce possible confusion
 74			HttpsURLConnection.setFollowRedirects(false);
 75		} catch (Exception e) {
 76			e.printStackTrace();
 77		}
 78	}
 79
 80	/** Updates the screen content from a background thread. */
 81	void setText(final String s, final boolean progress) {
 82		text = s;
 83		hdlr.post(new Runnable() {
 84			public void run() {
 85				content.setText(s);
 86				setProgressBarIndeterminateVisibility(progress);
 87			}
 88		});
 89	}
 90
 91	/** Spawns a new thread connecting to the specified URL.
 92	 * The result of the request is displayed on the screen.
 93	 * @param urlString a HTTPS URL to connect to.
 94	 */
 95	void connect(final String urlString) {
 96		new Thread() {
 97			public void run() {
 98				try {
 99					URL u = new URL(urlString);
100					HttpsURLConnection c = (HttpsURLConnection)u.openConnection();
101					c.connect();
102					setText("" + c.getResponseCode() + " "
103							+ c.getResponseMessage(), false);
104					c.disconnect();
105				} catch (Exception e) {
106					setText(e.toString(), false);
107					e.printStackTrace();
108				}
109			}
110		}.start();
111	}
112
113	/** Reacts on the connect Button press. */
114	@Override
115	public void onClick(View view) {
116		String url = urlinput.getText().toString();
117		setText("Loading " + url, true);
118		setProgressBarIndeterminateVisibility(true);
119		connect(url);
120	}
121	
122	/** React on the "Manage Certificates" button press. */
123	public void onManage(View view) {
124		final ArrayList<String> aliases = Collections.list(mtm.getCertificates());
125		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, aliases);
126		new AlertDialog.Builder(this).setTitle("Tap Certificate to Delete")
127				.setNegativeButton(android.R.string.cancel, null)
128				.setAdapter(adapter, new DialogInterface.OnClickListener() {
129						@Override
130						public void onClick(DialogInterface dialog, int which) {
131							try {
132								String alias = aliases.get(which);
133								mtm.deleteCertificate(alias);
134								setText("Deleted " + alias, false);
135							} catch (KeyStoreException e) {
136								e.printStackTrace();
137								setText("Error: " + e.getLocalizedMessage(), false);
138							}
139						}
140					})
141				.create().show();
142	}
143}