Mono SSL Issues

When I try to use WebClient to download some data I'm receiving the following error:

{System.Net.WebException: Error: SendFailure (Error writing headers) ---> System.Net.WebException: Error writing headers ---> System.IO.IOException: The authentication or decryption has failed. ---> Mono.Security.Protocol.Tls.TlsException: Invalid certificate received from server. Error code: 0xffffffff800b010a
  at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.RemoteValidation (Mono.Security.Protocol.Tls.ClientContext context, AlertDescription description) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 () [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process () [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslClientStream.EndNegotiateHandshake (IAsyncResult result) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  --- End of inner exception stack trace ---
  at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 
  at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0 
  at System.Net.WebClient.GetWebResponse (System.Net.WebRequest request) [0x00000] in <filename unknown>:0 
  at System.Net.WebClient.ReadAll (System.Net.WebRequest request, System.Object userToken) [0x00000] in <filename unknown>:0 
  at System.Net.WebClient.DownloadDataCore (System.Uri address, System.Object userToken) [0x00000] in <filename unknown>:0 }

I believe my SSL certificate is fine. I don't receive any SSL errors/warnings when navigating the site in a browser. The code also executes fine if I run it in a standard C# console project (not using Mono). Any advice/suggestions?

Here is my Xamarin Android code that always fails:

[Activity(Label = "AndroidApplication14", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        TextView text = FindViewById<TextView>(Resource.Id.MyTextView);

        string data = "";
        try
        {
            data = new WebClient().DownloadString("https://tws.vectren.com/webservices/cxf");
        }
        catch (Exception ex)
        {
            //Exception is always caught here
            data = ex.Message;
        }

        text.Text = data;
    }
}

And here is the standard console code that always succeeds:

class Program
{
    static void Main(string[] args)
    {
        string data = "";
        try
        {
            data = new WebClient().DownloadString("https://tws.vectren.com/webservices/cxf");
        }
        catch (Exception ex)
        {
            data = ex.Message;
        }

        Console.WriteLine(data);
    }
}

Posts

  • CraigSchulteCraigSchulte USMember

    I wanted to add that calling this code fixes the problem:

    System.Net.ServicePointManager.ServerCertificateValidationCallback =
                    new System.Net.Security.RemoteCertificateValidationCallback(delegate { return true; });
    

    However this seems more like a hack than a fix and I don't want to do this in a production environment. So I don't think I can rely on this as a solution.

  • CraigSchulteCraigSchulte USMember
    edited May 2014

    Nevermind, I just realized I'm still seeing a certificate error/warning message when accessing the site through Chrome for Android, so there probably is an issue with the certificate.

    However I don't get the same certificate error/warning message when accessing the site through Desktop Chrome (on my laptop), which is what had me so confused.

    Any ideas why my phone won't trust the certificate but my laptop will?

  • Hi!

    Did you solve the problem?
    I have several tablets here with different Android versions.

    Works fine, except on the Medion LifeTab (Android 4.0.3).

  • MaratMarat USMember

    Hi!
    Did you solve the problem?
    Works fine, except on the Lenovo A5000 (Android 4.4.2).

  • Mohib.ShethMohib.Sheth CZMember ✭✭

    I am now facing this issue while using Xamarin.Insights. Does anybody have any idea how to fix this?

  • The current version of mono does not properly support the TLS protocol. Just a guess, but perhaps this is what you are dealing with.

    There is work underway to integrate Microsoft's libraries in to Mono but it is not ready yet.
    https://github.com/mono/mono-tls

  • Sorry for late response, I did resolve the problem. The problem was an error in our SSL certificate and had nothing to do with Android, Xamarin, or the code I wrote.

    I can't remember the exact details, another person corrected the SSL certificate, and the problem went away.

  • flowiesflowies AUMember

    Had this issue, after going around in circles I found this https://support.apple.com/en-au/HT204132

    and found that my cert issn't trusted by apple

Sign In or Register to comment.