diff options
author | Sam Whited <sam@samwhited.com> | 2014-10-30 15:20:20 -0400 |
---|---|---|
committer | Sam Whited <sam@samwhited.com> | 2014-10-30 15:33:13 -0400 |
commit | 46f147a82c6b161e071f717c7686f8b78a20aaf1 (patch) | |
tree | 38ee04006e38197fd8009697b8975007ec0f45c8 /libs/MemorizingTrustManager/example | |
parent | 142384e5805d93887d185c9a5b74d4850e4ec719 (diff) | |
parent | a362bd10214b16f8939c12a1dd4376667fe0d49f (diff) |
Merge branch 'gradle' into development
Conflicts:
.gitignore
CHANGELOG.md
README.md
libs/MemorizingTrustManager
libs/minidns
libs/openpgp-api-lib
Diffstat (limited to '')
-rw-r--r-- | libs/MemorizingTrustManager/example/AndroidManifest.xml | 29 | ||||
-rw-r--r-- | libs/MemorizingTrustManager/example/ant.properties | 18 | ||||
-rw-r--r-- | libs/MemorizingTrustManager/example/build.gradle | 23 | ||||
-rw-r--r-- | libs/MemorizingTrustManager/example/build.xml (renamed from build.xml) | 2 | ||||
-rw-r--r-- | libs/MemorizingTrustManager/example/proguard-project.txt | 20 | ||||
-rw-r--r-- | libs/MemorizingTrustManager/example/project.properties | 12 | ||||
-rw-r--r-- | libs/MemorizingTrustManager/example/res/layout/mtmexample.xml | 36 | ||||
-rw-r--r-- | libs/MemorizingTrustManager/example/res/values/strings.xml | 4 | ||||
-rw-r--r-- | libs/MemorizingTrustManager/example/src/de/duenndns/mtmexample/JULHandler.java | 169 | ||||
-rw-r--r-- | libs/MemorizingTrustManager/example/src/de/duenndns/mtmexample/MTMExample.java | 143 |
10 files changed, 455 insertions, 1 deletions
diff --git a/libs/MemorizingTrustManager/example/AndroidManifest.xml b/libs/MemorizingTrustManager/example/AndroidManifest.xml new file mode 100644 index 00000000..cdc0450b --- /dev/null +++ b/libs/MemorizingTrustManager/example/AndroidManifest.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="de.duenndns.mtmexample" + android:versionCode="1" + android:versionName="1.0"> + + <uses-sdk + android:minSdkVersion="3" + android:targetSdkVersion="19" /> + + <uses-permission android:name="android.permission.INTERNET" /> + + <application android:label="@string/app_name" android:icon="@android:drawable/ic_lock_lock"> + <activity + android:name=".MTMExample" + android:configChanges="keyboardHidden|orientation|screenSize|screenLayout" + android:label="@string/app_name" > + + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + + <!-- ADD THE FOLLOWING TO YOUR MANIFEST: --> + <activity android:name="de.duenndns.ssl.MemorizingActivity" + android:theme="@android:style/Theme.Translucent.NoTitleBar" /> + </application> +</manifest> diff --git a/libs/MemorizingTrustManager/example/ant.properties b/libs/MemorizingTrustManager/example/ant.properties new file mode 100644 index 00000000..27fcaadd --- /dev/null +++ b/libs/MemorizingTrustManager/example/ant.properties @@ -0,0 +1,18 @@ +# This file is used to override default values used by the Ant build system. +# +# This file must be checked in Version Control Systems, as it is +# integral to the build system of your project. + +# This file is only used by the Ant script. + +# You can use this to override default values such as +# 'source.dir' for the location of your java source folder and +# 'out.dir' for the location of your output folder. + +# You can also use it define how the release builds are signed by declaring +# the following properties: +# 'key.store' for the location of your keystore and +# 'key.alias' for the name of the key to use. +# The password will be asked during the build when you use the 'release' target. + +application.package=de.duenndns.mtmexample diff --git a/libs/MemorizingTrustManager/example/build.gradle b/libs/MemorizingTrustManager/example/build.gradle new file mode 100644 index 00000000..00bfe99e --- /dev/null +++ b/libs/MemorizingTrustManager/example/build.gradle @@ -0,0 +1,23 @@ +apply plugin: 'android' + +dependencies { + compile rootProject +} + +android { + compileSdkVersion 19 + buildToolsVersion "19.1" + defaultConfig { + minSdkVersion 7 + targetSdkVersion 19 + } + + sourceSets { + main { + manifest.srcFile 'AndroidManifest.xml' + java.srcDirs = ['src'] + res.srcDirs = ['res'] + } + } + +} diff --git a/build.xml b/libs/MemorizingTrustManager/example/build.xml index 2571750a..cdc74917 100644 --- a/build.xml +++ b/libs/MemorizingTrustManager/example/build.xml @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="UTF-8"?> -<project name="Conversations" default="help"> +<project name="MTMExample" default="help"> <!-- The local.properties file is created and updated by the 'android' tool. It contains the path to the SDK. It should *NOT* be checked into diff --git a/libs/MemorizingTrustManager/example/proguard-project.txt b/libs/MemorizingTrustManager/example/proguard-project.txt new file mode 100644 index 00000000..f2fe1559 --- /dev/null +++ b/libs/MemorizingTrustManager/example/proguard-project.txt @@ -0,0 +1,20 @@ +# To enable ProGuard in your project, edit project.properties +# to define the proguard.config property as described in that file. +# +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in ${sdk.dir}/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the ProGuard +# include property in project.properties. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/libs/MemorizingTrustManager/example/project.properties b/libs/MemorizingTrustManager/example/project.properties new file mode 100644 index 00000000..3692949f --- /dev/null +++ b/libs/MemorizingTrustManager/example/project.properties @@ -0,0 +1,12 @@ +# This file is automatically generated by Android Tools. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must be checked in Version Control Systems. +# +# To customize properties used by the Ant build system use, +# "ant.properties", and override values to adapt the script to your +# project structure. + +android.library.reference.1=../ +# Project target. +target=android-19 diff --git a/libs/MemorizingTrustManager/example/res/layout/mtmexample.xml b/libs/MemorizingTrustManager/example/res/layout/mtmexample.xml new file mode 100644 index 00000000..dfef58b6 --- /dev/null +++ b/libs/MemorizingTrustManager/example/res/layout/mtmexample.xml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent" > + <EditText + android:id="@+id/url" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:hint="HTTPS address" + android:text="https://op-co.de/mtm/" + android:singleLine="true" + /> + <Button + android:id="@+id/connect" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="Connect" + /> + <TextView + android:id="@+id/content" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:layout_weight="1" + android:text="Please enter a HTTPS URL and press 'Connect'!" + android:textSize="11pt" + /> + <Button + android:id="@+id/manage" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="Clean up Certificates" + android:onClick="onManage" + /> +</LinearLayout> + diff --git a/libs/MemorizingTrustManager/example/res/values/strings.xml b/libs/MemorizingTrustManager/example/res/values/strings.xml new file mode 100644 index 00000000..e4f505bc --- /dev/null +++ b/libs/MemorizingTrustManager/example/res/values/strings.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="app_name">MemorizingTrustManager Example</string> +</resources> diff --git a/libs/MemorizingTrustManager/example/src/de/duenndns/mtmexample/JULHandler.java b/libs/MemorizingTrustManager/example/src/de/duenndns/mtmexample/JULHandler.java new file mode 100644 index 00000000..40f71f58 --- /dev/null +++ b/libs/MemorizingTrustManager/example/src/de/duenndns/mtmexample/JULHandler.java @@ -0,0 +1,169 @@ +package de.duenndns.mtmexample; + +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintWriter; +import java.io.StringBufferInputStream; +import java.io.StringWriter; +import java.util.logging.Formatter; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogManager; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +import android.util.Log; + +/** + * A <code>java.util.logging</code> (JUL) Handler for Android. + * <p> + * If you want fine-grained control over MTM's logging, you can copy this + * class to your code base and call the static {@link #initialize()} method. + * </p> + * <p> + * This JUL Handler passes log messages sent to JUL to the Android log, while + * keeping the format and stack traces of optionally supplied Exceptions. It + * further allows to install a {@link DebugLogSettings} class via + * {@link #setDebugLogSettings(DebugLogSettings)} that determines whether JUL log messages of + * level {@link java.util.logging.Level#FINE} or lower are logged. This gives + * the application developer more control over the logged messages, while + * allowing a library developer to place debug log messages without risking to + * spam the Android log. + * </p> + * <p> + * If there are no {@code DebugLogSettings} configured, then all messages sent + * to JUL will be logged. + * </p> + * + * @author Florian Schmaus + * + */ +@SuppressWarnings("deprecation") +public class JULHandler extends Handler { + + /** Implement this interface to toggle debug logging. + */ + public interface DebugLogSettings { + public boolean isDebugLogEnabled(); + } + + private static final String CLASS_NAME = JULHandler.class.getName(); + + /** + * The global LogManager configuration. + * <p> + * This configures: + * <ul> + * <li> JULHandler as the default handler for all log messages + * <li> A default log level FINEST (300). Meaning that log messages of a level 300 or higher a + * logged + * </ul> + * </p> + */ + private static final InputStream LOG_MANAGER_CONFIG = new StringBufferInputStream( +// @formatter:off +"handlers = " + CLASS_NAME + '\n' + +".level = FINEST" +); +// @formatter:on + + // Constants for Android vs. JUL debug level comparisons + private static final int FINE_INT = Level.FINE.intValue(); + private static final int INFO_INT = Level.INFO.intValue(); + private static final int WARN_INT = Level.WARNING.intValue(); + private static final int SEVE_INT = Level.SEVERE.intValue(); + + private static final Logger LOGGER = Logger.getLogger(CLASS_NAME); + + /** A formatter that creates output similar to Android's Log.x. */ + private static final Formatter FORMATTER = new Formatter() { + @Override + public String format(LogRecord logRecord) { + Throwable thrown = logRecord.getThrown(); + if (thrown != null) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw, false); + pw.write(logRecord.getMessage() + ' '); + thrown.printStackTrace(pw); + pw.flush(); + return sw.toString(); + } else { + return logRecord.getMessage(); + } + } + }; + + private static DebugLogSettings sDebugLogSettings; + private static boolean initialized = false; + + public static void initialize() { + try { + LogManager.getLogManager().readConfiguration(LOG_MANAGER_CONFIG); + initialized = true; + } catch (IOException e) { + Log.e("JULHandler", "Can not initialize configuration", e); + } + if (initialized) LOGGER.info("Initialzied java.util.logging logger"); + } + + public static void setDebugLogSettings(DebugLogSettings debugLogSettings) { + if (!isInitialized()) initialize(); + sDebugLogSettings = debugLogSettings; + } + + public static boolean isInitialized() { + return initialized; + } + + public JULHandler() { + setFormatter(FORMATTER); + } + + @Override + public void close() {} + + @Override + public void flush() {} + + @Override + public boolean isLoggable(LogRecord record) { + final boolean debugLog = sDebugLogSettings == null ? true : sDebugLogSettings + .isDebugLogEnabled(); + + if (record.getLevel().intValue() <= FINE_INT) { + return debugLog; + } + return true; + } + + /** JUL method that forwards log records to Android's LogCat. */ + @Override + public void publish(LogRecord record) { + if (!isLoggable(record)) return; + + final int priority = getAndroidPriority(record.getLevel()); + final String tag = substringAfterLastDot(record.getSourceClassName()); + final String msg = getFormatter().format(record); + + Log.println(priority, tag, msg); + } + + /** Helper to convert JUL verbosity levels to Android's Log. */ + private static int getAndroidPriority(Level level) { + int value = level.intValue(); + if (value >= SEVE_INT) { + return Log.ERROR; + } else if (value >= WARN_INT) { + return Log.WARN; + } else if (value >= INFO_INT) { + return Log.INFO; + } else { + return Log.DEBUG; + } + } + + /** Helper to extract short class names. */ + private static String substringAfterLastDot(String s) { + return s.substring(s.lastIndexOf('.') + 1).trim(); + } +} diff --git a/libs/MemorizingTrustManager/example/src/de/duenndns/mtmexample/MTMExample.java b/libs/MemorizingTrustManager/example/src/de/duenndns/mtmexample/MTMExample.java new file mode 100644 index 00000000..0d16ae82 --- /dev/null +++ b/libs/MemorizingTrustManager/example/src/de/duenndns/mtmexample/MTMExample.java @@ -0,0 +1,143 @@ +package de.duenndns.mtmexample; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.os.Bundle; +import android.os.Handler; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.Window; +import android.widget.ArrayAdapter; +import android.widget.EditText; +import android.widget.TextView; + +import java.net.URL; +import java.security.KeyStoreException; +import java.util.ArrayList; +import java.util.Collections; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.X509TrustManager; + +import de.duenndns.ssl.MemorizingTrustManager; + +/** + * Example to demonstrate the use of MemorizingTrustManager on HTTPS + * sockets. + */ +public class MTMExample extends Activity implements OnClickListener +{ + MemorizingTrustManager mtm; + + TextView content; + HostnameVerifier defaultverifier; + EditText urlinput; + String text; + Handler hdlr; + + /** Creates the Activity and registers a MemorizingTrustManager. */ + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + JULHandler.initialize(); + requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); + setContentView(R.layout.mtmexample); + + + // set up gui elements + findViewById(R.id.connect).setOnClickListener(this); + content = (TextView)findViewById(R.id.content); + urlinput = (EditText)findViewById(R.id.url); + + // register handler for background thread + hdlr = new Handler(); + + // Here, the MemorizingTrustManager is activated for HTTPS + try { + // set location of the keystore + MemorizingTrustManager.setKeyStoreFile("private", "sslkeys.bks"); + + // register MemorizingTrustManager for HTTPS + SSLContext sc = SSLContext.getInstance("TLS"); + mtm = new MemorizingTrustManager(this); + sc.init(null, new X509TrustManager[] { mtm }, + new java.security.SecureRandom()); + HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); + HttpsURLConnection.setDefaultHostnameVerifier( + mtm.wrapHostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier())); + + // disable redirects to reduce possible confusion + HttpsURLConnection.setFollowRedirects(false); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** Updates the screen content from a background thread. */ + void setText(final String s, final boolean progress) { + text = s; + hdlr.post(new Runnable() { + public void run() { + content.setText(s); + setProgressBarIndeterminateVisibility(progress); + } + }); + } + + /** Spawns a new thread connecting to the specified URL. + * The result of the request is displayed on the screen. + * @param urlString a HTTPS URL to connect to. + */ + void connect(final String urlString) { + new Thread() { + public void run() { + try { + URL u = new URL(urlString); + HttpsURLConnection c = (HttpsURLConnection)u.openConnection(); + c.connect(); + setText("" + c.getResponseCode() + " " + + c.getResponseMessage(), false); + c.disconnect(); + } catch (Exception e) { + setText(e.toString(), false); + e.printStackTrace(); + } + } + }.start(); + } + + /** Reacts on the connect Button press. */ + @Override + public void onClick(View view) { + String url = urlinput.getText().toString(); + setText("Loading " + url, true); + setProgressBarIndeterminateVisibility(true); + connect(url); + } + + /** React on the "Manage Certificates" button press. */ + public void onManage(View view) { + final ArrayList<String> aliases = Collections.list(mtm.getCertificates()); + ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, aliases); + new AlertDialog.Builder(this).setTitle("Tap Certificate to Delete") + .setNegativeButton(android.R.string.cancel, null) + .setAdapter(adapter, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + try { + String alias = aliases.get(which); + mtm.deleteCertificate(alias); + setText("Deleted " + alias, false); + } catch (KeyStoreException e) { + e.printStackTrace(); + setText("Error: " + e.getLocalizedMessage(), false); + } + } + }) + .create().show(); + } +} |