android-selfsigned


Source link: https://github.com/onehilltech/android-selfsigned

android-selfsigned

A simple library for supporting self-signed certificates in Android

  • Integrate with services that use self-signed certificates.
  • Preserve existing security measures on the mobile device.
  • Ideal for prototyping and testing using secure protocols.

NOTE. We strongly recommend that you purchase a certificate from a trusted authority when you move to production.

Installation

Gradle

buildscript {

repositories {

  maven {
 url "https://jitpack.io" 
}

}
 
}
  dependencies {

# Only include if using HttpsURLConnection
compile com.github.onehilltech.android-selfsigned:android:x.y.z

# Otherwise, use appropriate module for framework in use
compile com.github.onehilltech.android-selfsigned:android-volley:x.y. 
}
 

Getting Started

Manually define the list of hostnames/IP addresses that are using self-signed certificates. It is best to define the list as a resource so you can have different list for different Gradle configurations:

<resources>
  <string-array name="hostnames">

<!-- localhost on the Android emulator -->

<item>10.0.2.2</item>
  </string-array> </resources>

Define an Application class to initialize the DefaultHostnameVerifier, which is used by HttpsURLConnection.

public class TheApplication extends Application  {

@Override
public void onCreate ()
{

  super.onCreate ();

String [] hostnames = this.getResources ().getStringArray (R.array.hostnames);

  SelfSigned.getDefaultHostnameVerifier ().addAll (Arrays.asList (hostnames));

}
 
}

Make sure you add the TheApplication class to AndroidManifest.xml.

<application
  android:name="[package].TheApplication"

 >

</application>

Add the public certificate to the application's assets. For example, if the certificate is in a file named server.crt, then it must be added to main/assets/server.crt (or the assets folder for the target configuration).

Now, determine the method for making secure requests:

android-volley

Volley uses HttpsURLConnection under the hood. If you do not set the default SSLSocketFactory, as explained above, then you can use the helper class to create a RequestQueue that supports self-signed certificates:

VolleySelfSigned.newRequestQueue (context, "server.crt")

Now, requests executed on the returned RequestQueue that interact with an hostname/IP address defined in the resources above will not throw the usual security exceptions.

HttpsURLConnection

First, create a SSLContext that uses the public certificate bundled as an asset:

SSLContext sslContext = AndroidSelfSigned.newSSLContext (context, "server.crt");

Attach the SSLContext to a HttpsURLConnection:

URL url = new URL ("https://10.0.2.2");
 HttpsURLConnection conn = (HttpsURLConnection)url.openConnection ();
 conn.setSSLSocketFactory (sslContext.getSocketFactory ());

You can even set the SSLContext as the default so you do not have to initialize the SSLSocketFactory for each HttpsURLConnection:

HttpsURLConnection.setDefaultSSLSocketFactory (sslContext.getSocketFactory ());

If you use this approach, it is best to do so in the Application class for your application.

Resources

QuiltView displays views of different sizes in a scrollable grid.

Robolectric is a unit test framework that de-fangs the Android SDK jar so you can test-drive the development of your Android app. Tests run inside the JVM on your workstation in seconds.

osmdroid is a (almost) full/free replacement for Android's MapView (v1 API) class. It also includes a modular tile provider system with support for numerous online and offline tile sources and overlay support with built-in overlays for plotting icons, tracking location, and drawing shapes.

A carefully crafted Android framework that includes: DI, ORM, EventBus, JSON (de)serialization feature, improved AsyncTask & IntentService, Logger, RESTClient, ImageFetcher, and a lot of utils.

Android common lib, include ImageCache, HttpCache, DropDownListView, DownloadManager, Utils and so on.

An HTTP & SPDY client for Android and Java applications.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes