Add MemorizingTrustManager library in source.
authorDa Risk <da_risk@beem-project.com>
Mon, 22 Aug 2011 22:35:57 +0200
changeset 919 84d62c76469e
parent 918 ed8eca7fad9a
child 920 ff346f5bc36f
Add MemorizingTrustManager library in source.
AndroidManifest.xml
doc/asmack-beem/beem_patches/10-custom-sslcontext.patch
libs/asmack-android-7-beem.jar
res/values-fr/strings.xml
res/values/strings.xml
src/de/duenndns/ssl/MTMDecision.java
src/de/duenndns/ssl/MemorizingActivity.java
src/de/duenndns/ssl/MemorizingTrustManager.java
src/de/duenndns/ssl/package-info.java
--- a/AndroidManifest.xml	Thu Jul 21 22:44:37 2011 +0200
+++ b/AndroidManifest.xml	Mon Aug 22 22:35:57 2011 +0200
@@ -32,6 +32,8 @@
 		<activity android:name=".ui.GroupList" android:label="GroupList" />
 		<activity android:name=".ui.PrivacyList" android:label="@string/privacy_list_name" />
 
+		<!-- MemorizingTrustManagerActivity -->
+		<activity android:name="de.duenndns.ssl.MemorizingActivity" />
 		<!--
 			Could be interesting if we would launch beem at startup <receiver
 			android:name=".tool.BeemBroadcastReceiver" android:enabled="true">
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/asmack-beem/beem_patches/10-custom-sslcontext.patch	Mon Aug 22 22:35:57 2011 +0200
@@ -0,0 +1,84 @@
+Index: org/jivesoftware/smack/XMPPConnection.java
+===================================================================
+--- org/jivesoftware/smack/XMPPConnection.java	(révision 11644)
++++ org/jivesoftware/smack/XMPPConnection.java	(copie de travail)
+@@ -758,14 +758,14 @@
+      * @throws Exception if an exception occurs.
+      */
+     void proceedTLSReceived() throws Exception {
+-        SSLContext context = SSLContext.getInstance("TLS");
++        SSLContext context = this.config.getCustomSSLContext();
+         KeyStore ks = null;
+         KeyManager[] kms = null;
+         PasswordCallback pcb = null;
+ 
+         if(config.getCallbackHandler() == null) {
+            ks = null;
+-        } else {
++        } else if (context == null) {
+             //System.out.println("Keystore type: "+configuration.getKeystoreType());
+             if(config.getKeystoreType().equals("NONE")) {
+                 ks = null;
+@@ -821,10 +821,12 @@
+         }
+ 
+         // Verify certificate presented by the server
+-        context.init(kms,
+-                new javax.net.ssl.TrustManager[]{new ServerTrustManager(getServiceName(), config)},
+-                //new javax.net.ssl.TrustManager[]{new OpenTrustManager()},
+-                new java.security.SecureRandom());
++	if (context == null) {
++		context = SSLContext.getInstance("TLS");
++		context.init(kms,
++			new javax.net.ssl.TrustManager[]{new ServerTrustManager(getServiceName(), config)},
++			new java.security.SecureRandom());
++	}
+         Socket plain = socket;
+         // Secure the plain connection
+         socket = context.getSocketFactory().createSocket(plain,
+Index: org/jivesoftware/smack/ConnectionConfiguration.java
+===================================================================
+--- org/jivesoftware/smack/ConnectionConfiguration.java	(révision 11644)
++++ org/jivesoftware/smack/ConnectionConfiguration.java	(copie de travail)
+@@ -20,6 +20,7 @@
+ 
+ package org.jivesoftware.smack;
+ 
++import javax.net.ssl.SSLContext;
+ import org.jivesoftware.smack.proxy.ProxyInfo;
+ import org.jivesoftware.smack.util.DNSUtil;
+ 
+@@ -59,6 +60,7 @@
+     private boolean selfSignedCertificateEnabled = false;
+     private boolean expiredCertificatesCheckEnabled = false;
+     private boolean notMatchingDomainCheckEnabled = false;
++    private SSLContext customSSLContext;
+ 
+     private boolean compressionEnabled = false;
+ 
+@@ -487,6 +489,25 @@
+     }
+ 
+     /**
++     * Gets the custom SSLContext for SSL sockets. This is null by default.
++     *
++     * @return the SSLContext previously set with setCustomSSLContext() or null.
++     */
++    public SSLContext getCustomSSLContext() {
++	    return this.customSSLContext;
++    }
++
++    /**
++     * Sets a custom SSLContext for creating SSL sockets. A custom Context causes all other
++     * SSL/TLS realted settings to be ignored.
++     *
++     * @param context the custom SSLContext for new sockets; null to reset default behaviour.
++     */
++    public void setCustomSSLContext(SSLContext context) {
++	    this.customSSLContext = context;
++    }
++
++    /**
+      * Returns true if the connection is going to use stream compression. Stream compression
+      * will be requested after TLS was established (if TLS was enabled) and only if the server
+      * offered stream compression. With stream compression network traffic can be reduced
Binary file libs/asmack-android-7-beem.jar has changed
--- a/res/values-fr/strings.xml	Thu Jul 21 22:44:37 2011 +0200
+++ b/res/values-fr/strings.xml	Mon Aug 22 22:35:57 2011 +0200
@@ -313,6 +313,14 @@
 	<string name="privacy_list_delete_dialog_no">Non</string>
 
 	<string name="UpdateButton">Mettre à jour</string>
+
+	<!-- MemorizingTrustManager library -->
+	<string name="mtm_accept_cert">Accepter un certificat de sécurité invalide ?</string>
+	<string name="mtm_decision_always">Toujours</string>
+	<string name="mtm_decision_once">Une fois</string>
+	<string name="mtm_decision_abort">Annuler</string>
+
+	<string name="mtm_notification">Certificate Verification</string>
 	
 	<!-- Error messages -->
 	<string name="error_login_authentication">Erreur lors de l\'authenfitication, mauvais login ou password</string>
--- a/res/values/strings.xml	Thu Jul 21 22:44:37 2011 +0200
+++ b/res/values/strings.xml	Mon Aug 22 22:35:57 2011 +0200
@@ -300,7 +300,15 @@
 	<string name="privacy_list_delete_dialog_no">No</string>
 
 	<string name="UpdateButton">Update</string>
-	
+
+	<!-- MemorizingTrustManager library -->
+	<string name="mtm_accept_cert">Accept Unknown Certificate?</string>
+	<string name="mtm_decision_always">Always</string>
+	<string name="mtm_decision_once">Once</string>
+	<string name="mtm_decision_abort">Abort</string>
+
+	<string name="mtm_notification">Certificate Verification</string>
+
 	<!-- Error messages -->
 	
 	<string name="error_login_authentication">Error during authentication, bad login or password.</string>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/de/duenndns/ssl/MTMDecision.java	Mon Aug 22 22:35:57 2011 +0200
@@ -0,0 +1,33 @@
+/* MemorizingTrustManager - a TrustManager which asks the user about invalid
+ *  certificates and memorizes their decision.
+ *
+ * Copyright (c) 2010 Georg Lukas <georg@op-co.de>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package de.duenndns.ssl;
+
+class MTMDecision {
+	public final static int DECISION_INVALID	= 0;
+	public final static int DECISION_ABORT		= 1;
+	public final static int DECISION_ONCE		= 2;
+	public final static int DECISION_ALWAYS	= 3;
+
+	int state = DECISION_INVALID;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/de/duenndns/ssl/MemorizingActivity.java	Mon Aug 22 22:35:57 2011 +0200
@@ -0,0 +1,97 @@
+/* MemorizingTrustManager - a TrustManager which asks the user about invalid
+ *  certificates and memorizes their decision.
+ *
+ * Copyright (c) 2010 Georg Lukas <georg@op-co.de>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package de.duenndns.ssl;
+
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
+import android.content.DialogInterface.*;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
+
+import com.beem.project.beem.R;
+
+public class MemorizingActivity extends Activity
+		implements OnClickListener,OnCancelListener {
+	final static String TAG = "MemorizingActivity";
+
+	int decisionId;
+	String app;
+	
+	@Override
+	public void onCreate(Bundle savedInstanceState) {
+		Log.d(TAG, "onCreate");
+		super.onCreate(savedInstanceState);
+	}
+
+	@Override
+	public void onResume() {
+		super.onResume();
+		Intent i = getIntent();
+		app = i.getStringExtra(MemorizingTrustManager.DECISION_INTENT_APP);
+		decisionId = i.getIntExtra(MemorizingTrustManager.DECISION_INTENT_ID, MTMDecision.DECISION_INVALID);
+		String cert = i.getStringExtra(MemorizingTrustManager.DECISION_INTENT_CERT);
+		Log.d(TAG, "onResume with " + i.getExtras() + " decId=" + decisionId);
+		Log.d(TAG, "data: " + i.getData());
+		new AlertDialog.Builder(this).setTitle(R.string.mtm_accept_cert)
+			.setMessage(cert)
+			.setPositiveButton(R.string.mtm_decision_always, this)
+			.setNeutralButton(R.string.mtm_decision_once, this)
+			.setNegativeButton(R.string.mtm_decision_abort, this)
+			.setOnCancelListener(this)
+			.create().show();
+	}
+
+	void sendDecision(int decision) {
+		Log.d(TAG, "Sending decision to " + app + ": " + decision);
+		Intent i = new Intent(MemorizingTrustManager.DECISION_INTENT + "/" + app);
+		i.putExtra(MemorizingTrustManager.DECISION_INTENT_ID, decisionId);
+		i.putExtra(MemorizingTrustManager.DECISION_INTENT_CHOICE, decision);
+		sendBroadcast(i);
+		finish();
+	}
+
+	// react on AlertDialog button press
+	public void onClick(DialogInterface dialog, int btnId) {
+		int decision;
+		dialog.dismiss();
+		switch (btnId) {
+		case DialogInterface.BUTTON_POSITIVE:
+			decision = MTMDecision.DECISION_ALWAYS;
+			break;
+		case DialogInterface.BUTTON_NEUTRAL:
+			decision = MTMDecision.DECISION_ONCE;
+			break;
+		default:
+			decision = MTMDecision.DECISION_ABORT;
+		}
+		sendDecision(decision);
+	}
+
+	public void onCancel(DialogInterface dialog) {
+		sendDecision(MTMDecision.DECISION_ABORT);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/de/duenndns/ssl/MemorizingTrustManager.java	Mon Aug 22 22:35:57 2011 +0200
@@ -0,0 +1,373 @@
+/* MemorizingTrustManager - a TrustManager which asks the user about invalid
+ *  certificates and memorizes their decision.
+ *
+ * Copyright (c) 2010 Georg Lukas <georg@op-co.de>
+ *
+ * MemorizingTrustManager.java contains the actual trust manager and interface
+ * code to create a MemorizingActivity and obtain the results.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package de.duenndns.ssl;
+
+import android.app.Activity;
+import android.app.Application;
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.Service;
+import android.app.AlertDialog;
+import android.app.PendingIntent;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.DialogInterface.OnClickListener;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.Uri;
+import android.util.Log;
+import android.os.Handler;
+
+import java.io.File;
+import java.security.cert.*;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.HashMap;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactory;
+import javax.net.ssl.X509TrustManager;
+
+import com.beem.project.beem.R;
+
+/**
+ * A X509 trust manager implementation which asks the user about invalid
+ * certificates and memorizes their decision.
+ * <p>
+ * The certificate validity is checked using the system default X509
+ * TrustManager, creating a query Dialog if the check fails.
+ * <p>
+ * <b>WARNING:</b> This only works if a dedicated thread is used for
+ * opening sockets!
+ */
+public class MemorizingTrustManager implements X509TrustManager {
+	final static String TAG = "MemorizingTrustManager";
+	final static String DECISION_INTENT = "de.duenndns.ssl.DECISION";
+	final static String DECISION_INTENT_APP    = DECISION_INTENT + ".app";
+	final static String DECISION_INTENT_ID     = DECISION_INTENT + ".decisionId";
+	final static String DECISION_INTENT_CERT   = DECISION_INTENT + ".cert";
+	final static String DECISION_INTENT_CHOICE = DECISION_INTENT + ".decisionChoice";
+	private final static int NOTIFICATION_ID = 100509;
+
+	static String KEYSTORE_DIR = "KeyStore";
+	static String KEYSTORE_FILE = "KeyStore.bks";
+
+	Context master;
+	NotificationManager notificationManager;
+	private static int decisionId = 0;
+	private static HashMap<Integer,MTMDecision> openDecisions = new HashMap();
+
+	Handler masterHandler;
+	private File keyStoreFile;
+	private KeyStore appKeyStore;
+	private X509TrustManager defaultTrustManager;
+	private X509TrustManager appTrustManager;
+
+	/** Creates an instance of the MemorizingTrustManager class.
+	 *
+	 * @param m Activity or Service to show the Dialog / Notification
+	 */
+	private MemorizingTrustManager(Context m) {
+		master = m;
+		masterHandler = new Handler();
+		notificationManager = (NotificationManager)master.getSystemService(Context.NOTIFICATION_SERVICE);
+
+		Application app;
+		if (m instanceof Service) {
+			app = ((Service)m).getApplication();
+		} else if (m instanceof Activity) {
+			app = ((Activity)m).getApplication();
+		} else throw new ClassCastException("MemorizingTrustManager context must be either Activity or Service!");
+
+		File dir = app.getDir(KEYSTORE_DIR, Context.MODE_PRIVATE);
+		keyStoreFile = new File(dir + File.separator + KEYSTORE_FILE);
+
+		appKeyStore = loadAppKeyStore();
+		defaultTrustManager = getTrustManager(null);
+		appTrustManager = getTrustManager(appKeyStore);
+	}
+
+	/**
+	 * Returns a X509TrustManager list containing a new instance of
+	 * TrustManagerFactory.
+	 *
+	 * This function is meant for convenience only. You can use it
+	 * as follows to integrate TrustManagerFactory for HTTPS sockets:
+	 *
+	 * <pre>
+	 *     SSLContext sc = SSLContext.getInstance("TLS");
+	 *     sc.init(null, MemorizingTrustManager.getInstanceList(this),
+	 *         new java.security.SecureRandom());
+	 *     HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
+	 * </pre>
+	 * @param c Activity or Service to show the Dialog / Notification
+	 */
+	public static X509TrustManager[] getInstanceList(Context c) {
+		return new X509TrustManager[] { new MemorizingTrustManager(c) };
+	}
+
+	/**
+	 * Changes the path for the KeyStore file.
+	 *
+	 * The actual filename relative to the app's directory will be
+	 * <code>app_<i>dirname</i>/<i>filename</i></code>.
+	 *
+	 * @param dirname directory to store the KeyStore.
+	 * @param filename file name for the KeyStore.
+	 */
+	public static void setKeyStoreFile(String dirname, String filename) {
+		KEYSTORE_DIR = dirname;
+		KEYSTORE_FILE = filename;
+	}
+
+	X509TrustManager getTrustManager(KeyStore ks) {
+		try {
+			TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
+			tmf.init(ks);
+			for (TrustManager t : tmf.getTrustManagers()) {
+				if (t instanceof X509TrustManager) {
+					return (X509TrustManager)t;
+				}
+			}
+		} catch (Exception e) {
+			// Here, we are covering up errors. It might be more useful
+			// however to throw them out of the constructor so the
+			// embedding app knows something went wrong.
+			Log.e(TAG, "getTrustManager(" + ks + ")", e);
+		}
+		return null;
+	}
+
+	KeyStore loadAppKeyStore() {
+		KeyStore ks;
+		try {
+			ks = KeyStore.getInstance(KeyStore.getDefaultType());
+		} catch (KeyStoreException e) {
+			Log.e(TAG, "getAppKeyStore()", e);
+			return null;
+		}
+		try {
+			ks.load(null, null);
+			ks.load(new java.io.FileInputStream(keyStoreFile), "MTM".toCharArray());
+		} catch (java.io.FileNotFoundException e) {
+			Log.i(TAG, "getAppKeyStore(" + keyStoreFile + ") - file does not exist");
+		} catch (Exception e) {
+			Log.e(TAG, "getAppKeyStore(" + keyStoreFile + ")", e);
+		}
+		return ks;
+	}
+
+	void storeCert(X509Certificate[] chain) {
+		// add all certs from chain to appKeyStore
+		try {
+			for (X509Certificate c : chain)
+				appKeyStore.setCertificateEntry(c.getSubjectDN().toString(), c);
+		} catch (KeyStoreException e) {
+			Log.e(TAG, "storeCert(" + chain + ")", e);
+			return;
+		}
+		
+		// reload appTrustManager
+		appTrustManager = getTrustManager(appKeyStore);
+
+		// store KeyStore to file
+		try {
+			java.io.FileOutputStream fos = new java.io.FileOutputStream(keyStoreFile);
+			appKeyStore.store(fos, "MTM".toCharArray());
+			fos.close();
+		} catch (Exception e) {
+			Log.e(TAG, "storeCert(" + keyStoreFile + ")", e);
+		}
+	}
+
+	private boolean isExpiredException(Throwable e) {
+		do {
+			if (e instanceof CertificateExpiredException)
+				return true;
+			e = e.getCause();
+		} while (e != null);
+		return false;
+	}
+
+	public void checkCertTrusted(X509Certificate[] chain, String authType, boolean isServer)
+		throws CertificateException
+	{
+		Log.d(TAG, "checkCertTrusted(" + chain + ", " + authType + ", " + isServer + ")");
+		try {
+			Log.d(TAG, "checkCertTrusted: trying appTrustManager");
+			if (isServer)
+				appTrustManager.checkServerTrusted(chain, authType);
+			else
+				appTrustManager.checkClientTrusted(chain, authType);
+		} catch (CertificateException ae) {
+			// if the cert is stored in our appTrustManager, we ignore expiredness
+			ae.printStackTrace();
+			if (isExpiredException(ae)) {
+				Log.i(TAG, "checkCertTrusted: accepting expired certificate from keystore");
+				return;
+			}
+			try {
+				Log.d(TAG, "checkCertTrusted: trying defaultTrustManager");
+				if (isServer)
+					defaultTrustManager.checkServerTrusted(chain, authType);
+				else
+					defaultTrustManager.checkClientTrusted(chain, authType);
+			} catch (CertificateException e) {
+				e.printStackTrace();
+				interact(chain, authType, e);
+			}
+		}
+	}
+
+	public void checkClientTrusted(X509Certificate[] chain, String authType)
+		throws CertificateException
+	{
+		checkCertTrusted(chain, authType, false);
+	}
+
+	public void checkServerTrusted(X509Certificate[] chain, String authType)
+		throws CertificateException
+	{
+		checkCertTrusted(chain, authType, true);
+	}
+
+	public X509Certificate[] getAcceptedIssuers()
+	{
+		Log.d(TAG, "getAcceptedIssuers()");
+		return defaultTrustManager.getAcceptedIssuers();
+	}
+
+	private int createDecisionId(MTMDecision d) {
+		int myId;
+		synchronized(openDecisions) {
+			myId = decisionId;
+			openDecisions.put(myId, d);
+			decisionId += 1;
+		}
+		return myId;
+	}
+
+	private String certChainMessage(final X509Certificate[] chain, CertificateException cause) {
+		Throwable e = cause;
+		Log.d(TAG, "certChainMessage for " + e);
+		StringBuffer si = new StringBuffer();
+		if (e.getCause() != null) {
+			e = e.getCause();
+			si.append(e.getLocalizedMessage());
+			si.append("\n");
+		}
+		for (X509Certificate c : chain) {
+			si.append("\n");
+			si.append(c.getSubjectDN().toString());
+			si.append(" (");
+			si.append(c.getIssuerDN().toString());
+			si.append(")");
+		}
+		return si.toString();
+	}
+
+	void startActivityNotification(Intent intent, String certName) {
+		Notification n = new Notification(android.R.drawable.ic_lock_lock,
+				master.getString(R.string.mtm_notification),
+				System.currentTimeMillis());
+		PendingIntent call = PendingIntent.getActivity(master, 0, intent, 0);
+		n.setLatestEventInfo(master.getApplicationContext(),
+				master.getString(R.string.mtm_notification),
+				certName, call);
+		n.flags |= Notification.FLAG_AUTO_CANCEL;
+
+		notificationManager.notify(NOTIFICATION_ID, n);
+	}
+
+	void interact(final X509Certificate[] chain, String authType, CertificateException cause)
+		throws CertificateException
+	{
+		/* prepare the MTMDecision blocker object */
+		MTMDecision choice = new MTMDecision();
+		final int myId = createDecisionId(choice);
+		final String certTitle = chain[0].getSubjectDN().toString();
+		final String certMessage = certChainMessage(chain, cause);
+
+		BroadcastReceiver decisionReceiver = new BroadcastReceiver() {
+			public void onReceive(Context ctx, Intent i) { interactResult(i); }
+		};
+		master.registerReceiver(decisionReceiver, new IntentFilter(DECISION_INTENT + "/" + master.getPackageName()));
+		masterHandler.post(new Runnable() {
+			public void run() {
+				Intent ni = new Intent(master, MemorizingActivity.class);
+				ni.setData(Uri.parse(MemorizingTrustManager.class.getName() + "/" + myId));
+				ni.putExtra(DECISION_INTENT_APP, master.getPackageName());
+				ni.putExtra(DECISION_INTENT_ID, myId);
+				ni.putExtra(DECISION_INTENT_CERT, certMessage);
+
+				try {
+					master.startActivity(ni);
+				} catch (Exception e) {
+					Log.e(TAG, "startActivity: " + e);
+					startActivityNotification(ni, certMessage);
+				}
+			}
+		});
+
+		Log.d(TAG, "openDecisions: " + openDecisions);
+		Log.d(TAG, "waiting on " + myId);
+		try {
+			synchronized(choice) { choice.wait(); }
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}
+		master.unregisterReceiver(decisionReceiver);
+		Log.d(TAG, "finished wait on " + myId + ": " + choice.state);
+		switch (choice.state) {
+		case MTMDecision.DECISION_ALWAYS:
+			storeCert(chain);
+		case MTMDecision.DECISION_ONCE:
+			break;
+		default:
+			throw (cause);
+		}
+	}
+
+	public static void interactResult(Intent i) {
+		int decisionId = i.getIntExtra(DECISION_INTENT_ID, MTMDecision.DECISION_INVALID);
+		int choice = i.getIntExtra(DECISION_INTENT_CHOICE, MTMDecision.DECISION_INVALID);
+		Log.d(TAG, "interactResult: " + decisionId + " chose " + choice);
+		Log.d(TAG, "openDecisions: " + openDecisions);
+
+		MTMDecision d;
+		synchronized(openDecisions) {
+			 d = openDecisions.get(decisionId);
+			 openDecisions.remove(decisionId);
+		}
+		synchronized(d) {
+			d.state = choice;
+			d.notify();
+		}
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/de/duenndns/ssl/package-info.java	Mon Aug 22 22:35:57 2011 +0200
@@ -0,0 +1,7 @@
+/**
+ * This package contains the MemorizingTrustManager library made by Georg.
+ * It is a "plugin" for Android Java to allow asking the user about SSL certificates
+ * https://github.com/ge0rg/MemorizingTrustManager
+ */
+package de.duenndns.ssl;
+