On Feb 8, 2013, at 7:41 AM, Nikola <[email protected]> wrote:
> I've tried to use HttpsUrlConnection 
> (http://androidapi.xamarin.com/?link=T%3aJavax.Net.Ssl.HttpsURLConnection) 
> instead, but unfortunately, I am getting very similar results.

Unfortunately, I cannot use your code as-is, as it's missing crucial contextual 
information, e.g. what is the value of `alias`, and what (if any) device 
configuration do I need to do to make it work? In short, a complete sample to 
demonstrate the bug would be handy....for this aspect.

That said...

> Is there *any* way at all to connect to client certificate authenticated 
> HTTPS site with Mono for Android?

Is there any particular reason you're using the Java/Android stack for this? 
You could instead stick to the .NET stack. For example, I created a sample 
project, embedded my certificate as an EmbeddedResource, then subscribed to the 
ServicePointManager.ServerCertificateValidationCallback callback:

    using System;
    using System.IO;
    using System.Net;
    using System.Net.Security;
    using System.Runtime.InteropServices;
    using System.Security.Cryptography.X509Certificates;

    using Android.App;
    using Android.Content;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;

    namespace Scratch.CustomCert
    {
        static class MyCerts {
            static MyCerts ()
            {
                // TODO: update resource name as appropriate
                using (var s = 
typeof(MyCerts).Assembly.GetManifestResourceStream ("cert.dat")) {
                    var d = new byte [s.Length];
                    s.Read (d, 0, d.Length);
                    MyCert = new X509Certificate2 (d);
                }
            }

            public static readonly X509Certificate2 MyCert;
        }

        [Activity (Label = "Scratch.CustomCert", MainLauncher = true)]
        public class Activity1 : Activity
        {
            int count = 1;

            protected override void OnCreate (Bundle bundle)
            {
                base.OnCreate (bundle);

                // Set our view from the "main" layout resource
                SetContentView (Resource.Layout.Main);

                // Get our button from the layout resource,
                // and attach an event to it
                Button button = FindViewById<Button> (Resource.Id.myButton);

                ServicePointManager.ServerCertificateValidationCallback = 
RemoteCertificateValidationCallback;
                
                button.Click += delegate {
                    button.Text = string.Format ("{0} clicks!", count++);

                    try {
                        // TODO: update URL as appropriate.
                        var w = HttpWebRequest.Create ("https://example.com/";);
                        using (var response = w.GetResponse ())
                        using (var r = new StreamReader 
(response.GetResponseStream ())) {
                            button.Text = r.ReadToEnd ();
                        }
                    } catch (Exception e) {
                        Console.WriteLine ("error: {0}", e);
                    }
                };
            }

            bool RemoteCertificateValidationCallback(
                    object sender,
                    X509Certificate certificate,
                    X509Chain chain,
                    SslPolicyErrors sslPolicyErrors)
            {
                if (MyCerts.MyCert.Equals (certificate))
                    return true;

                return false;
            }
        }
    }


Using the .NET networking stack and using the 
ServicePointManager.ServerCertificateValidationCallback callback allows me to 
connect to a site which uses a self-signed certificate (and thus would normally 
cause Android all sorts of conniptions).

 - Jon

_______________________________________________
Monodroid mailing list
[email protected]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to