65 * <b>WARNING:</b> This only works if a dedicated thread is used for |
65 * <b>WARNING:</b> This only works if a dedicated thread is used for |
66 * opening sockets! |
66 * opening sockets! |
67 */ |
67 */ |
68 public class MemorizingTrustManager implements X509TrustManager { |
68 public class MemorizingTrustManager implements X509TrustManager { |
69 final static String TAG = "MemorizingTrustManager"; |
69 final static String TAG = "MemorizingTrustManager"; |
|
70 public final static String INTERCEPT_DECISION_INTENT = "de.duenndns.ssl.INTERCEPT_DECISION"; |
|
71 public final static String INTERCEPT_DECISION_INTENT_LAUNCH = INTERCEPT_DECISION_INTENT + ".launch_intent"; |
70 final static String DECISION_INTENT = "de.duenndns.ssl.DECISION"; |
72 final static String DECISION_INTENT = "de.duenndns.ssl.DECISION"; |
71 final static String DECISION_INTENT_APP = DECISION_INTENT + ".app"; |
73 final static String DECISION_INTENT_APP = DECISION_INTENT + ".app"; |
72 final static String DECISION_INTENT_ID = DECISION_INTENT + ".decisionId"; |
74 final static String DECISION_INTENT_ID = DECISION_INTENT + ".decisionId"; |
73 final static String DECISION_INTENT_CERT = DECISION_INTENT + ".cert"; |
75 final static String DECISION_INTENT_CERT = DECISION_INTENT + ".cert"; |
74 final static String DECISION_INTENT_CHOICE = DECISION_INTENT + ".decisionChoice"; |
76 final static String DECISION_INTENT_CHOICE = DECISION_INTENT + ".decisionChoice"; |
289 si.append(")"); |
291 si.append(")"); |
290 } |
292 } |
291 return si.toString(); |
293 return si.toString(); |
292 } |
294 } |
293 |
295 |
294 void startActivityNotification(Intent intent, String certName) { |
296 void startActivityNotification(PendingIntent intent, String certName) { |
295 Notification n = new Notification(android.R.drawable.ic_lock_lock, |
297 Notification n = new Notification(android.R.drawable.ic_lock_lock, |
296 master.getString(R.string.mtm_notification), |
298 master.getString(R.string.mtm_notification), |
297 System.currentTimeMillis()); |
299 System.currentTimeMillis()); |
298 PendingIntent call = PendingIntent.getActivity(master, 0, intent, 0); |
|
299 n.setLatestEventInfo(master.getApplicationContext(), |
300 n.setLatestEventInfo(master.getApplicationContext(), |
300 master.getString(R.string.mtm_notification), |
301 master.getString(R.string.mtm_notification), |
301 certName, call); |
302 certName, intent); |
302 n.flags |= Notification.FLAG_AUTO_CANCEL; |
303 n.flags |= Notification.FLAG_AUTO_CANCEL; |
303 |
304 |
304 notificationManager.notify(NOTIFICATION_ID, n); |
305 notificationManager.notify(NOTIFICATION_ID, n); |
|
306 } |
|
307 |
|
308 void launchServiceMode(Intent activityIntent, final String certMessage) { |
|
309 BroadcastReceiver launchNotifReceiver= new BroadcastReceiver() { |
|
310 public void onReceive(Context ctx, Intent i) { |
|
311 Log.i(TAG, "Interception not done by the application. Send notification"); |
|
312 PendingIntent pi = i.getParcelableExtra(INTERCEPT_DECISION_INTENT_LAUNCH); |
|
313 startActivityNotification(pi, certMessage); |
|
314 } |
|
315 }; |
|
316 master.registerReceiver(launchNotifReceiver, new IntentFilter(INTERCEPT_DECISION_INTENT + "/" + master.getPackageName())); |
|
317 PendingIntent call = PendingIntent.getActivity(master, 0, activityIntent, 0); |
|
318 Intent ni = new Intent(INTERCEPT_DECISION_INTENT + "/" + master.getPackageName()); |
|
319 ni.putExtra(INTERCEPT_DECISION_INTENT_LAUNCH, call); |
|
320 master.sendOrderedBroadcast(ni, null); |
|
321 |
305 } |
322 } |
306 |
323 |
307 void interact(final X509Certificate[] chain, String authType, CertificateException cause) |
324 void interact(final X509Certificate[] chain, String authType, CertificateException cause) |
308 throws CertificateException |
325 throws CertificateException |
309 { |
326 { |
327 |
344 |
328 try { |
345 try { |
329 master.startActivity(ni); |
346 master.startActivity(ni); |
330 } catch (Exception e) { |
347 } catch (Exception e) { |
331 Log.e(TAG, "startActivity: " + e); |
348 Log.e(TAG, "startActivity: " + e); |
332 startActivityNotification(ni, certMessage); |
349 launchServiceMode(ni, certMessage); |
333 } |
350 } |
334 } |
351 } |
335 }); |
352 }); |
336 |
353 |
337 Log.d(TAG, "openDecisions: " + openDecisions); |
354 Log.d(TAG, "openDecisions: " + openDecisions); |