Bug 54469

Summary: [appletls]: Keychain popups on Desktop Mono
Product: [Mono] Class Libraries Reporter: Martin Baulig <martin.baulig>
Component: SystemAssignee: Martin Baulig <martin.baulig>
Status: NEW ---    
Severity: normal CC: masafa, mono-bugs+mono, sebastien
Priority: ---    
Version: unspecified   
Target Milestone: Untriaged   
Hardware: PC   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Popup dialog

Description Martin Baulig 2017-04-04 20:38:32 UTC
Created attachment 21193 [details]
Popup dialog

After AppleTls has been moved into the Mono class libraries, we are now getting keychain popup messages whenever
using server mode (via HttpListener, for instance) or accessing a website that's using client certificates for authentication.  The only way of making them go away is to grant the 'mono' binary - and thus every application that can possibly run with Mono - full access to the private key.

To reproduce (you don't need the web-tests module for this, I'm just using it as an easy way of referencing a certificate/private key pair; you can also create your own self-signed certificate).

One-time setup:

* open "keychain Access" and verify that your current keychain (which is usually the "login" keychain)
  does not contain a key called "Hamiller-Tube.local".

* checkout https://github.com/xamarin/web-tests

* httpcfg -add -port 8888 -p12 /Workspace/web-tests/CA/server-cert.pfx -pwd monkey

* this will not touch the OS keychain, which you can verify in the Keychain Access tool.

* Run this:

	var listener = new HttpListener ();
	listener.Prefixes.Add ("https://*:8888/");
	listener.Start ();

	var context = listener.GetContext ();
	Console.WriteLine (context);

* open "https://localhost:8888/" in any web browser

* you will get the attached popup dialog

* click "decline" and go back to the Keychain Access tool.  You'll see a new certificate 
  and private key called "Hamiller-Tube.local".

* click "accept" and the same popup dialog may come back several times.

* when using Safari, it will also ask you to provide a client certificate.  That's a different issue,
  we have a bug about that.
Comment 1 Martin Baulig 2017-04-04 20:45:25 UTC
I tried to disable this in https://github.com/mono/mono/pull/4642, but unfortunately removing the implicit keychain lookup does not fix the problem.

The problem is that SecPKCS12Import (https://developer.apple.com/reference/security/1396915-secpkcs12import?language=objc) always imports the SecIdentityRef into the keychain.

In a Xamarin.Mac app, this is no big deal because you can use code-sign to
grant the app access and AFAIK you can also do this on a per-key basis, so
you could add your certificate and private key to the keychain and your app
can access it without popups.

With Desktop Mono, the same approach would require granting the "mono" binary
this trust - which would then extend to every single app that's ever going to run on it.
Comment 2 Martin Baulig 2017-04-04 20:48:44 UTC
After using the above HttpListener code and selecting "Always allow", you can do something like this:

	static byte [] ReadFile (string name)
		var fullName = "/Workspace/web-tests/CA/" + name + ".pem";
		var data = File.ReadAllBytes (fullName);
		return data;

	static X509Certificate ReadCertificate (string name)
		var data = ReadFile (name);
		return new X509Certificate (data);

	public static void TestServer ()
		var cert = ReadCertificate ("server-cert");
		var listener = MonoTlsProviderFactory.CreateHttpListener (cert);
		var listener = new HttpListener ();
		listener.Prefixes.Add ("https://*:9999/");
		listener.Start ();

		var context = listener.GetContext ();
		Console.WriteLine (context);

in a different app, this will use the previously password-protected private key without any questions asked.
Comment 3 Sebastien Pouliot 2017-04-04 21:04:36 UTC
The existing, managed PKCS12 code, should be used for Mono desktop. It was a good idea to try but it went a step too far