1 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
2 // Copyright 2012 Xamarin Inc.
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #if MONOTOUCH || MONODROID
26 using MSX = Mono.Security.X509;
28 extern alias MonoSecurity;
29 using MSX = MonoSecurity::Mono.Security.X509;
33 using System.Runtime.InteropServices;
35 namespace System.Security.Cryptography.X509Certificates {
37 static class OSX509Certificates {
38 public const string SecurityLibrary = "/System/Library/Frameworks/Security.framework/Security";
39 public const string CoreFoundationLibrary = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";
41 [DllImport (SecurityLibrary)]
42 extern static IntPtr SecCertificateCreateWithData (IntPtr allocator, IntPtr nsdataRef);
44 [DllImport (SecurityLibrary)]
45 extern static int SecTrustCreateWithCertificates (IntPtr certOrCertArray, IntPtr policies, out IntPtr sectrustref);
47 [DllImport (SecurityLibrary)]
48 extern static IntPtr SecPolicyCreateSSL (bool server, IntPtr cfStringHostname);
50 [DllImport (SecurityLibrary)]
51 extern static int SecTrustEvaluate (IntPtr secTrustRef, out SecTrustResult secTrustResultTime);
53 [DllImport (CoreFoundationLibrary, CharSet=CharSet.Unicode)]
54 extern static IntPtr CFStringCreateWithCharacters (IntPtr allocator, string str, int count);
56 [DllImport (CoreFoundationLibrary)]
57 unsafe extern static IntPtr CFDataCreate (IntPtr allocator, byte *bytes, IntPtr length);
59 [DllImport (CoreFoundationLibrary)]
60 unsafe extern static void CFRelease (IntPtr handle);
62 [DllImport (CoreFoundationLibrary)]
63 extern static IntPtr CFArrayCreate (IntPtr allocator, IntPtr values, IntPtr numValues, IntPtr callbacks);
65 public enum SecTrustResult {
71 RecoverableTrustFailure,
76 static IntPtr MakeCFData (byte [] data)
79 fixed (byte *ptr = &data [0])
80 return CFDataCreate (IntPtr.Zero, ptr, (IntPtr) data.Length);
84 static unsafe IntPtr FromIntPtrs (IntPtr [] values)
86 fixed (IntPtr* pv = values) {
87 return CFArrayCreate (
90 (IntPtr) values.Length,
95 public static SecTrustResult TrustEvaluateSsl (MSX.X509CertificateCollection certificates, string host)
97 if (certificates == null)
98 return SecTrustResult.Deny;
101 return _TrustEvaluateSsl (certificates, host);
103 return SecTrustResult.Deny;
107 static SecTrustResult _TrustEvaluateSsl (MSX.X509CertificateCollection certificates, string hostName)
109 int certCount = certificates.Count;
110 IntPtr [] cfDataPtrs = new IntPtr [certCount];
111 IntPtr [] secCerts = new IntPtr [certCount];
112 IntPtr certArray = IntPtr.Zero;
113 IntPtr sslsecpolicy = IntPtr.Zero;
114 IntPtr host = IntPtr.Zero;
115 IntPtr sectrust = IntPtr.Zero;
116 SecTrustResult result = SecTrustResult.Deny;
119 for (int i = 0; i < certCount; i++)
120 cfDataPtrs [i] = MakeCFData (certificates [i].RawData);
122 for (int i = 0; i < certCount; i++){
123 secCerts [i] = SecCertificateCreateWithData (IntPtr.Zero, cfDataPtrs [i]);
124 if (secCerts [i] == IntPtr.Zero)
125 return SecTrustResult.Deny;
127 certArray = FromIntPtrs (secCerts);
128 host = CFStringCreateWithCharacters (IntPtr.Zero, hostName, hostName.Length);
129 sslsecpolicy = SecPolicyCreateSSL (true, host);
131 int code = SecTrustCreateWithCertificates (certArray, sslsecpolicy, out sectrust);
133 code = SecTrustEvaluate (sectrust, out result);
136 for (int i = 0; i < certCount; i++)
137 if (cfDataPtrs [i] != IntPtr.Zero)
138 CFRelease (cfDataPtrs [i]);
140 if (certArray != IntPtr.Zero)
141 CFRelease (certArray);
143 for (int i = 0; i < certCount; i++)
144 if (secCerts [i] != IntPtr.Zero)
145 CFRelease (secCerts [i]);
147 if (sslsecpolicy != IntPtr.Zero)
148 CFRelease (sslsecpolicy);
149 if (host != IntPtr.Zero)
151 if (sectrust != IntPtr.Zero)
152 CFRelease (sectrust);