Bug 30264 - X509Certificate verification should use system's installed root certificates on Android 4.0 and later
Summary: X509Certificate verification should use system's installed root certificates ...
Status: RESOLVED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 5.1
Hardware: Macintosh Mac OS
: High normal
Target Milestone: ---
Assignee: Marek Habersack
URL: https://github.com/mono/mono/blob/88b...
Depends on:
Blocks:
 
Reported: 2015-05-20 11:55 UTC by James Athey
Modified: 2015-09-01 15:55 UTC (History)
1 user (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 Developer Community or GitHub 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 ANSWERED

Description James Athey 2015-05-20 11:55:17 UTC
In Mono 4.0/Xamarin.Android 5.1, X.509 Certificate verification explicitly ignores SslPolicyErrors.RemoteCertificateChainErrors. The source of System.Net.ServicePointManager claims,

	// chain.Build() + GetErrorsFromChain() (above) will ALWAYS fail on
	// Android (there are no mozroots or preinstalled root certificates),
	// thus `errors` will ALWAYS have RemoteCertificateChainErrors.
	// Android just verified the chain; clear RemoteCertificateChainErrors.

https://github.com/mono/mono/blob/88bc5cee97dfd8ad9dd2f89bddca3336ade8ca97/mcs/class/System/System.Net/ServicePointManager.cs#L505

This comment is incorrect on Ice Cream Sandwich and later. Starting with that version, there are many dozens of root certificates present in /system/etc/security/cacerts.

Presumably, if Mono.Security.X509.X509StoreManager.LocalMachinePath returned "/system/etc/security/cacerts" on Android 4.0 or later, then X509 Chain Validation would succeed for valid certificates. ServicePointManager could then stop ignoring RemoteCertificateChainErrors on ICS+, and also stop calling the Android API to validate certificates.

See https://github.com/mono/mono/blob/effa4c07ba850bedbe1ff54b2a5df281c058ebcb/mcs/class/Mono.Security/Mono.Security.X509/X509StoreManager.cs#L67

For evidence that this would work, you need no special permissions to install these certificates to the CurrentUser's X509Store, which are then checked during X509 certificate verification. (This code only needs to run once per installation, because X509Store.Import() writes the certificate to disk. To remove the certificates, you need to uninstall all of the Mono APKs.)

	Mono.Security.X509.X509Store store = Mono.Security.X509.X509StoreManager.CurrentUser.TrustedRoot;

	string certDir = "/system/etc/security/cacerts";
	string[] certFiles = Directory.GetFiles(certDir);
	foreach (string certFile in certFiles)
	{
		byte[] certBytes = File.ReadAllBytes(Path.Combine(certDir, certFile));
		Mono.Security.X509.X509Certificate rootCert = new Mono.Security.X509.X509Certificate(certBytes);
		store.Import(rootCert);
	}

If you run the above code first, then connect an SslStream to a server with a valid cert:

		var tcpClient = new TcpClient("www.google.com", 443);
		var ssl = new SslStream(tcpClient.GetStream(), false, MyRemoteCertificateValidator);
		ssl.AuthenticateAsClient("www.google.com");
		ssl.Close();


	private bool MyRemoteCertificateValidator(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslpolicyerrors)
	{
		Console.Out.WriteLine(certificate.Subject);
		// With the system's root certs installed, chain.ChainStatus will be empty
		// Without the certs installed, chain.ChainStatus will contain "PartialChain"
		foreach (var s in chain.ChainStatus)
			Console.Out.WriteLine(s.StatusInformation);
		Console.Out.WriteLine(sslpolicyerrors.ToString());


		var x5092cert = new X509Certificate2(certificate);
		// With the system's root certs installed, Verify() will return true
		bool valid = x5092cert.Verify();

		return sslpolicyerrors == SslPolicyErrors.None;
	}
Comment 1 Marek Habersack 2015-09-01 15:55:02 UTC
So, while the suggestion is good it will not work, alas. The problem is that the import process would need write rights in one of the locations supported by Mono.Security for cert locations. Even if we added the Android system location, it would have to be supported by Mono.Security. There isn't even a write permission an app can ask to be granted write access to any system location on Android.
Furthermore, the verification is performed by https://github.com/mono/mono/blob/88bc5cee97dfd8ad9dd2f89bddca3336ade8ca97/mcs/class/System/System.Net/ServicePointManager.cs#L505 this call using Java APIs which, implicitly, read and verify against the certificates you mention.