Bug 6931 - Regression in TlsServerCertificate since Mono 2.11.3
Summary: Regression in TlsServerCertificate since Mono 2.11.3
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: Mono.Security ()
Version: master
Hardware: All All
: --- major
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
URL:
Depends on:
Blocks: 6122
  Show dependency tree
 
Reported: 2012-09-05 13:19 UTC by Martin Baulig
Modified: 2012-09-13 20:53 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Martin Baulig 2012-09-05 13:19:55 UTC
We have a regression in Mono's TLS Server Certificate validation code which happend somewhere between Mono 2.11.3 and latest master (mine is at commit 651408b4).

I ran into this while trying to fix a client's bug, but realized that the problem is not specific to his server, but also happens with for instance https://www.github.com/.  Here is a simple test case:

=====
using System;
using System.Net;

namespace A
{
	class X
	{
		static void Main (string[] args)
		{
			var uri = "https://www.github.com/";
			var request = (HttpWebRequest)WebRequest.Create (uri);
			request.Method = "GET";
			var response = (HttpWebResponse)request.GetResponse ();
			
			Console.WriteLine ("GOT RESPONSE: {0}", response);
		}
	}
}
======

I'm getting the following exception:

======
[martin@nathan:Workspace]$ mono --debug A.exe 

Unhandled Exception:
System.Net.WebException: Error getting response stream (Write: The authentication or decryption has failed.): SendFailure ---> 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.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates) [0x0009b] in /Users/martin/Workspace/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls.Handshake.Client/TlsServerCertificate.cs:218 
  at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 () [0x00054] in /Users/martin/Workspace/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls.Handshake.Client/TlsServerCertificate.cs:105 
  at Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process () [0x00037] in /Users/martin/Workspace/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls.Handshake/HandshakeMessage.cs:105 
  at (wrapper remoting-invoke-with-check) Mono.Security.Protocol.Tls.Handshake.HandshakeMessage:Process ()
  at Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg) [0x00039] in /Users/martin/Workspace/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/ClientRecordProtocol.cs:81 
  at Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00123] in /Users/martin/Workspace/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs:397 
  --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x0002a] in /Users/martin/Workspace/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslStreamBase.cs:100 
  --- End of inner exception stack trace ---
  at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00065] in /Users/martin/Workspace/mono/mcs/class/System/System.Net/HttpWebRequest.cs:924 
  at System.Net.HttpWebRequest.GetResponse () [0x0000e] in /Users/martin/Workspace/mono/mcs/class/System/System.Net/HttpWebRequest.cs:930 
  at A.X.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.Net.WebException: Error getting response stream (Write: The authentication or decryption has failed.): SendFailure ---> 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.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates) [0x0009b] in /Users/martin/Workspace/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls.Handshake.Client/TlsServerCertificate.cs:218 
  at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 () [0x00054] in /Users/martin/Workspace/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls.Handshake.Client/TlsServerCertificate.cs:105 
  at Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process () [0x00037] in /Users/martin/Workspace/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls.Handshake/HandshakeMessage.cs:105 
  at (wrapper remoting-invoke-with-check) Mono.Security.Protocol.Tls.Handshake.HandshakeMessage:Process ()
  at Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg) [0x00039] in /Users/martin/Workspace/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/ClientRecordProtocol.cs:81 
  at Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00123] in /Users/martin/Workspace/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs:397 
  --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x0002a] in /Users/martin/Workspace/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslStreamBase.cs:100 
  --- End of inner exception stack trace ---
  at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00065] in /Users/martin/Workspace/mono/mcs/class/System/System.Net/HttpWebRequest.cs:924 
  at System.Net.HttpWebRequest.GetResponse () [0x0000e] in /Users/martin/Workspace/mono/mcs/class/System/System.Net/HttpWebRequest.cs:930 
  at A.X.Main (System.String[] args) [0x00000] in <filename unknown>:0 
======
Comment 1 Martin Baulig 2012-09-05 13:31:15 UTC
I just realized that the same problem also happens in Mono 2.10.10 (compiled from mono-2-10 commit f21ee490).

Mono 2.10.9 (the official Mac OS X package) works, as well as 2.11.2 and 2.11.3
Comment 2 Sebastien Pouliot 2012-09-06 21:41:56 UTC
I still had my mono (master) for that fix on my Lion (10.7.4) box.

[opt/mono]cat wc.cs 
using System;
using System.Net;

class Test {
	public static void Main (string[] args)
	{
		foreach (string s in args) {
			Read (s);
		}
	}
	
	static void Read (string url)
	{
		WebClient wc = new WebClient ();
		Console.WriteLine (wc.DownloadString (url)); 
	}
}

[opt/mono]mcs wc.cs


[opt/mono]mono --version
Mono JIT compiler version 2.11.4 (master/684cd28 Thu 23 Aug 2012 14:20:08 EDT)
Copyright (C) 2002-2012 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           normal
	SIGSEGV:       altstack
	Notification:  kqueue
	Architecture:  x86
	Disabled:      none
	Misc:          softdebug 
	LLVM:          supported, not enabled.
	GC:            Included Boehm (with typed GC)

[opt/mono]mono wc.exe https://www.github.com
<!DOCTYPE html>
<html>
  <head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# githubog: http://ogp.me/ns/fb/githubog#">
    <meta charset='utf-8'>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>GitHub · Social Coding</title>
...
Comment 3 Sebastien Pouliot 2012-09-06 21:44:13 UTC
[opt/mono]cat 6931.cs 
using System;
using System.Net;

namespace A
{
	class X
	{
		static void Main (string[] args)
		{
			var uri = "https://www.github.com/";
			var request = (HttpWebRequest)WebRequest.Create (uri);
			request.Method = "GET";
			var response = (HttpWebResponse)request.GetResponse ();

			Console.WriteLine ("GOT RESPONSE: {0}", response);
		}
	}
}

[opt/mono]mcs 6931.cs

[opt/mono]mono 6931.exe 
GOT RESPONSE: System.Net.HttpWebResponse


I'll rebuilt from the head of master and go backward...
Comment 4 Sebastien Pouliot 2012-09-06 22:11:37 UTC
both cases (comment #2 and comment #3) works correctly with a clean, unpatched master using revision 417ec7840f3f9195b03115f0c8c25aaa646a5b04

I'll try again tomorrow on my Snow Leopard machine w/mono-2-10 branch.
Comment 5 Sebastien Pouliot 2012-09-07 17:25:23 UTC
Mono/master works on Linux too - *if* you executed `mozroots` before (to populate your trusted roots store), i.e. the exception is normal if you do not have the root certificates installed.
Comment 6 Martin Baulig 2012-09-07 17:53:06 UTC
Oops, yeah that really fixed it for me :-)

However, IMHO this really needs to be documented in the build instructions and not just in the FAQ.

It also looks like this behavior is new - I already compiled and worked with so many different versions of Mono on both the Mac and the PC and SSL always worked out-of-the-box without using 'mozroots', I didn't even know it exists till today.
Comment 7 Sebastien Pouliot 2012-09-07 18:02:30 UTC
This situation has not changed since Mono exists (or at least supports SSL). IOW it never worked without using mozroots. You can easily forget you executed it as they (the roots) will exists as long as you don't reformat your drive (e.g. across Mono and Linux updates).

The only cases where this differs are MonoTouch and Mono for Android, where the decision about the certificates is delegated to the operating system.
Comment 8 Sebastien Pouliot 2012-09-13 20:53:35 UTC
We misunderstood each other :-(

> Mono/master works on Linux too - *if* you executed `mozroots`

mozroots is required on Linux, not on OSX where "almost" the same code than iOS is used (iOS cuts a bit, as stores do no even exists, so it did not exhibit the behaviour). 

I could not duplicate the issue because I had executed mozroot (to debug an older bug). However we do not want to go back to requiring mozroot (on OSX) like we had (a few time ago). Next's beer is on me ;-)

Fixed:
master: 6fc934ea50f7744c2310c1daf4e3038e087e8837
mono-2-10: 9b545aaabf67eb9d58313745d449c83909a06bfa