Accessing Azure Mobile Services from Android 2.2 (Froyo) devices
If you try to access a Windows Azure Mobile Service using the Android SDK from a device running Android version 2.2 (also known as Froyo), you’ll get an error in all requests. The issue is that that version of Android doesn’t accept the SSL certificate used in Azure Mobile Services, and you end up getting the following exception:
Error: com.microsoft.windowsazure.mobileservices.MobileServiceException: Error while processing request.
Cause: javax.net.ssl.SSLException: Not trusted server certificate
Cause: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found.
Cause: java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found.
The number of Froyo Android devices is rapidly declining (2.2% in the date I wrote this post, according to the Android dashboard), but there are scenarios where you need to target up to that platform. This post will show one way to work around that limitation, created by Pablo Zaidenvoren and committed to the test project for the Android platform in the Android Mobile Services GitHub repository. The solution is to register a new SSL socket factory to be used by the mobile service client with a key store which accepts the certificate used by the Azure Mobile Service.
Scenario
In a TDD-like fashion, I’ll first write a program which will fail, then walk through the steps required to make it pass. Here’s the onCreate method in the main activity, whose layout has nothing but a button (btnClickMe) and a text view (txtResult). If you’re going to copy this code, remember to replace the application URL / key with yours, since I’ll delete this mobile service which I created right before I publish this post.
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- try {
- mClient = new MobileServiceClient(
- "https://blog20131019.azure-mobile.net/", // Replace with your
- "umtzEajPoOBsnvufFUVbLFTtjQrPPd37", // own URL / key
- this
- );
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- Button btn = (Button)findViewById(R.id.btnClickMe);
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- final StringBuilder sb = new StringBuilder();
- final TextView txtResult = (TextView)findViewById(R.id.txtResult);
- MobileServiceJsonTable table = mClient.getTable("TodoItem");
- sb.append("Created the table object, inserting item");
- txtResult.setText(sb.toString());
- JsonObject item = new JsonObject();
- item.addProperty("text", "Buy bread");
- item.addProperty("complete", false);
- table.insert(item, new TableJsonOperationCallback() {
- @Override
- public void onCompleted(JsonObject inserted, Exception error,
- ServiceFilterResponse response) {
- if (error != null) {
- sb.append("\nError: " + error.toString());
- Throwable cause = error.getCause();
- while (cause != null) {
- sb.append("\n Cause: " + cause.toString());
- cause = cause.getCause();
- }
- } else {
- sb.append("\nInserted: id=" + inserted.get("id").getAsString());
- }
- String txt = sb.toString();
- txtResult.setText(txt);
- }
- });
- }
- });
- }
If you run this code in a Froyo emulator (or a device), you’ll see printed out the error shown before.
On to the workaround
First, add a new folder under the res (resources) folder for the solution, by right-clicking it, and selecting New –> Folder:
Let’s call it ‘raw’, and click Finish:
Now, download the mobile service store file from https://github.com/WindowsAzure/azure-mobile-services/blob/dev/test/Android/ZumoE2ETestApp/res/raw/mobileservicestore.bks, and save it into the <project>/res/raw folder which you just created (in the GitHub page, clicking “View Raw” should prompt you to save the file). Back in Eclipse, select the ‘raw’ folder and hit F5 (Refresh), and you should see the file under the folder.
That file will need to be read in the new SSL socket factory, and to read that resource we’ll need a reference to the main activity. So we’ll add a static method (along with a new static field) which returns the instance of the main activity. And in the onCreate method, we set the (global) instance.
- private static Activity mInstance;
- public static Activity getInstance() {
- return mInstance;
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mInstance = this;
- try {
- mClient = new MobileServiceClient(
Now we can start by adding the Froyo support to the application. The snippet below shows only the beginning of the class – it doesn’t show the entire SSL factory with the additional certificate store (for Azure Mobile Services). You can find the whole code (may need to update the imports for the main activity and the resources) in the GitHub project.
- public class FroyoSupport {
- /**
- * Fixes an AndroidHttpClient instance to accept MobileServices SSL certificate
- * @param client AndroidHttpClient to fix
- */
- public static void fixAndroidHttpClientForCertificateValidation(AndroidHttpClient client) {
- final SchemeRegistry schemeRegistry = new SchemeRegistry();
- schemeRegistry.register(new Scheme("https",
- createAdditionalCertsSSLSocketFactory(), 443));
- client.getConnectionManager().getSchemeRegistry().unregister("https");
- client.getConnectionManager().getSchemeRegistry().register(new Scheme("https",
- createAdditionalCertsSSLSocketFactory(), 443));
- }
- private static SSLSocketFactory createAdditionalCertsSSLSocketFactory() {
- try {
- final KeyStore ks = KeyStore.getInstance("BKS");
- Activity mainActivity = MainActivity.getInstance();
- final InputStream in = mainActivity.getResources().openRawResource(R.raw.mobileservicestore);
- try {
- ks.load(in, "mobileservices".toCharArray());
- } finally {
- in.close();
- }
- return new AdditionalKeyStoresSSLSocketFactory(ks);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- private static class AdditionalKeyStoresSSLSocketFactory extends SSLSocketFactory {
Now that we have that support, we need to create a new instance of the factory used to create the AndroidHttpClient instances – which is defined in the Azure Mobile Services Android SDK (version 1.0.1 and later). The SDK already includes a default implementation of the class, so extending it to use the fix support is fairly straightforward. Add a new class (I’ll call it FroyoAndroidHttpClientFactory) which extends that default implementation:
- package com.example.froyoandwams;
- import android.net.http.AndroidHttpClient;
- import com.microsoft.windowsazure.mobileservices.AndroidHttpClientFactoryImpl;
- public class FroyoAndroidHttpClientFactory
- extends AndroidHttpClientFactoryImpl {
- @Override
- public AndroidHttpClient createAndroidHttpClient() {
- AndroidHttpClient client = super.createAndroidHttpClient();
- FroyoSupport.fixAndroidHttpClientForCertificateValidation(client);
- return client;
- }
- }
Now all we need to do is to hook up that AndroidHttpClientFactory with the MobileServiceClient, which can be done with the setAndroidHttpClientFactory method, as shown below.
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mInstance = this;
- try {
- mClient = new MobileServiceClient(
- "https://blog20131019.azure-mobile.net/", // Replace with your
- "umtzEajPoOBsnvufFUVbLFTtjQrPPd37", // own URL / key
- this
- );
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- mClient.setAndroidHttpClientFactory(new FroyoAndroidHttpClientFactory());
And that’s it. If you execute the application again and click the button, the item should be correctly inserted (and other operations should work as well).
If you want to download the code for this example, you can find it in my GitHub repository at https://github.com/carlosfigueira/blogsamples/tree/master/AzureMobileServices/FroyoAndWAMS.
Comments
- Anonymous
December 15, 2014
After that i now get "IOEXception Wrong version of key store." - Anonymous
November 07, 2015
Don't right-click and "save as" from GIT, download the raw file otherwise you get all the GIT HTML code too which obviously isn't the file you need.