1 #if SECURITY_DEP && MONO_FEATURE_APPLETLS
3 // AppleCertificateHelper.cs
6 // Martin Baulig <martin.baulig@xamarin.com>
8 // Copyright (c) 2015 Xamarin, Inc.
11 #if MONO_SECURITY_ALIAS
12 extern alias MonoSecurity;
16 using System.Collections;
17 using System.Reflection;
18 using System.Runtime.InteropServices;
19 using System.Security.Cryptography.X509Certificates;
21 #if MONO_SECURITY_ALIAS
22 using MonoSecurity::Mono.Security.Interface;
24 using Mono.Security.Interface;
27 namespace Mono.AppleTls
29 static class AppleCertificateHelper
31 public static SecIdentity GetIdentity (X509Certificate certificate)
34 * If we got an 'X509Certificate2', then we require it to have a private key
37 var certificate2 = certificate as X509Certificate2;
38 if (certificate2 != null)
39 return SecIdentity.Import (certificate2);
42 * Reading Certificates from the Mac Keychain
43 * ==========================================
45 * Reading the private key from the keychain is a new feature introduced with
46 * AppleTls on XamMac and iOS. On Desktop Mono, this new feature has several
47 * known issues and it also did not received any testing yet. We go back to the old
48 * way of doing things, which is to explicitly provide an X509Certificate2 with a
51 * Keychain Dialog Popups
52 * ======================
54 * When using Xamarin.Mac or Xamarin.iOS, we try to search the keychain
55 * for the certificate and private key.
57 * On Xamarin.iOS, this is easy because each app has its own keychain.
59 * On Xamarin.Mac, the .app package needs to be trusted via code-sign
60 * to get permission to access the user's keychain. [FIXME: I still have to
61 * research how to actually do that.] Without this, you will get a popup
62 * message each time, asking you whether you want to allow the app to access
63 * the keychain, but you can make these go away by selecting "Trust always".
65 * On Desktop Mono, this is problematic because selecting "Trust always"
66 * give the 'mono' binary (and thus everything you'll ever run with Mono)
67 * permission to retrieve the private key from the keychain.
69 * This code would also trigger constant keychain popup messages,
70 * which could only be suppressed by granting full trust. It also makes it
71 * impossible to run Mono in headless mode.
76 * To avoid these problems, we are currently using an undocumented API
77 * called SecIdentityRef() to avoid using the Mac keychain whenever a
78 * X509Certificate2 with a private key is used.
80 * On iOS and XamMac, you can still provide the X509Certificate without
81 * a private key - in this case, a keychain search will be performed (and you
82 * may get a popup message on XamMac).
86 using (var secCert = new SecCertificate (certificate)) {
87 return SecKeyChain.FindIdentity (secCert, true);
94 public static SecIdentity GetIdentity (X509Certificate certificate, out SecCertificate[] intermediateCerts)
96 var identity = GetIdentity (certificate);
98 var impl2 = certificate.Impl as X509Certificate2Impl;
99 if (impl2 == null || impl2.IntermediateCertificates == null) {
100 intermediateCerts = new SecCertificate [0];
105 intermediateCerts = new SecCertificate [impl2.IntermediateCertificates.Count];
106 for (int i = 0; i < intermediateCerts.Length; i++)
107 intermediateCerts [i] = new SecCertificate (impl2.IntermediateCertificates [i]);
116 public static bool InvokeSystemCertificateValidator (
117 ICertificateValidator2 validator, string targetHost, bool serverMode,
118 X509CertificateCollection certificates,
119 ref MonoSslPolicyErrors errors, ref int status11)
121 if (certificates == null) {
122 errors |= MonoSslPolicyErrors.RemoteCertificateNotAvailable;
126 if (!string.IsNullOrEmpty (targetHost)) {
127 var pos = targetHost.IndexOf (':');
129 targetHost = targetHost.Substring (0, pos);
132 var policy = SecPolicy.CreateSslPolicy (!serverMode, targetHost);
133 var trust = new SecTrust (certificates, policy);
135 if (validator.Settings.TrustAnchors != null) {
136 var status = trust.SetAnchorCertificates (validator.Settings.TrustAnchors);
137 if (status != SecStatusCode.Success)
138 throw new InvalidOperationException (status.ToString ());
139 trust.SetAnchorCertificatesOnly (false);
142 var result = trust.Evaluate ();
143 if (result == SecTrustResult.Unspecified)
146 errors |= MonoSslPolicyErrors.RemoteCertificateChainErrors;