1 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
2 // Copyright 2012-2014 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 extern alias PrebuiltSystem;
29 using XX509CertificateCollection = PrebuiltSystem::System.Security.Cryptography.X509Certificates.X509CertificateCollection;
31 using XX509CertificateCollection = System.Security.Cryptography.X509Certificates.X509CertificateCollection;
35 using System.Runtime.InteropServices;
37 namespace System.Security.Cryptography.X509Certificates {
39 static class OSX509Certificates {
40 public const string SecurityLibrary = "/System/Library/Frameworks/Security.framework/Security";
41 public const string CoreFoundationLibrary = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";
43 [DllImport (SecurityLibrary)]
44 extern static IntPtr SecCertificateCreateWithData (IntPtr allocator, IntPtr nsdataRef);
46 [DllImport (SecurityLibrary)]
47 extern static /* OSStatus */ int SecTrustCreateWithCertificates (IntPtr certOrCertArray, IntPtr policies, out IntPtr sectrustref);
49 [DllImport (SecurityLibrary)]
50 extern static /* OSStatus */ int SecTrustSetAnchorCertificates (IntPtr /* SecTrustRef */ trust, IntPtr /* CFArrayRef */ anchorCertificates);
52 [DllImport (SecurityLibrary)]
53 extern static IntPtr SecPolicyCreateSSL ([MarshalAs (UnmanagedType.I1)] bool server, IntPtr cfStringHostname);
55 [DllImport (SecurityLibrary)]
56 extern static /* OSStatus */ int SecTrustEvaluate (IntPtr secTrustRef, out SecTrustResult secTrustResultTime);
58 [DllImport (CoreFoundationLibrary, CharSet=CharSet.Unicode)]
59 extern static IntPtr CFStringCreateWithCharacters (IntPtr allocator, string str, /* CFIndex */ IntPtr count);
61 [DllImport (CoreFoundationLibrary)]
62 unsafe extern static IntPtr CFDataCreate (IntPtr allocator, byte *bytes, /* CFIndex */ IntPtr length);
64 [DllImport (CoreFoundationLibrary)]
65 extern static void CFRelease (IntPtr handle);
67 [DllImport (CoreFoundationLibrary)]
68 extern static IntPtr CFArrayCreate (IntPtr allocator, IntPtr values, /* CFIndex */ IntPtr numValues, IntPtr callbacks);
71 public enum SecTrustResult {
77 RecoverableTrustFailure,
82 static IntPtr MakeCFData (byte [] data)
85 fixed (byte *ptr = &data [0])
86 return CFDataCreate (IntPtr.Zero, ptr, (IntPtr) data.Length);
90 static unsafe IntPtr FromIntPtrs (IntPtr [] values)
92 fixed (IntPtr* pv = values) {
93 return CFArrayCreate (
96 (IntPtr) values.Length,
101 public static SecTrustResult TrustEvaluateSsl (XX509CertificateCollection certificates, XX509CertificateCollection anchors, string host)
103 if (certificates == null)
104 return SecTrustResult.Deny;
107 return _TrustEvaluateSsl (certificates, anchors, host);
109 return SecTrustResult.Deny;
113 static SecTrustResult _TrustEvaluateSsl (XX509CertificateCollection certificates, XX509CertificateCollection anchors, string hostName)
115 int certCount = certificates.Count;
116 int anchorCount = anchors != null ? anchors.Count : 0;
117 IntPtr [] cfDataPtrs = new IntPtr [certCount];
118 IntPtr [] secCerts = new IntPtr [certCount];
119 IntPtr [] cfDataAnchorPtrs = new IntPtr [anchorCount];
120 IntPtr [] secCertAnchors = new IntPtr [anchorCount];
121 IntPtr certArray = IntPtr.Zero;
122 IntPtr anchorArray = IntPtr.Zero;
123 IntPtr sslsecpolicy = IntPtr.Zero;
124 IntPtr host = IntPtr.Zero;
125 IntPtr sectrust = IntPtr.Zero;
126 SecTrustResult result = SecTrustResult.Deny;
129 for (int i = 0; i < certCount; i++)
130 cfDataPtrs [i] = MakeCFData (certificates [i].GetRawCertData ());
131 for (int i = 0; i < anchorCount; i++)
132 cfDataAnchorPtrs [i] = MakeCFData (anchors [i].GetRawCertData ());
134 for (int i = 0; i < certCount; i++){
135 secCerts [i] = SecCertificateCreateWithData (IntPtr.Zero, cfDataPtrs [i]);
136 if (secCerts [i] == IntPtr.Zero)
137 return SecTrustResult.Deny;
140 for (int i = 0; i < anchorCount; i++) {
141 secCertAnchors [i] = SecCertificateCreateWithData (IntPtr.Zero, cfDataAnchorPtrs [i]);
142 if (secCertAnchors [i] == IntPtr.Zero)
143 return SecTrustResult.Deny;
146 certArray = FromIntPtrs (secCerts);
148 host = CFStringCreateWithCharacters (IntPtr.Zero, hostName, (IntPtr) hostName.Length);
149 sslsecpolicy = SecPolicyCreateSSL (true, host);
151 int code = SecTrustCreateWithCertificates (certArray, sslsecpolicy, out sectrust);
153 return SecTrustResult.Deny;
155 if (anchorCount > 0) {
156 anchorArray = FromIntPtrs (secCertAnchors);
157 SecTrustSetAnchorCertificates (sectrust, anchorArray);
160 code = SecTrustEvaluate (sectrust, out result);
163 for (int i = 0; i < certCount; i++)
164 if (cfDataPtrs [i] != IntPtr.Zero)
165 CFRelease (cfDataPtrs [i]);
167 for (int i = 0; i < anchorCount; i++)
168 if (cfDataAnchorPtrs [i] != IntPtr.Zero)
169 CFRelease (cfDataAnchorPtrs [i]);
171 if (certArray != IntPtr.Zero)
172 CFRelease (certArray);
174 if (anchorArray != IntPtr.Zero)
175 CFRelease (anchorArray);
177 for (int i = 0; i < certCount; i++)
178 if (secCerts [i] != IntPtr.Zero)
179 CFRelease (secCerts [i]);
181 for (int i = 0; i < anchorCount; i++)
182 if (secCertAnchors [i] != IntPtr.Zero)
183 CFRelease (secCertAnchors [i]);
185 if (sslsecpolicy != IntPtr.Zero)
186 CFRelease (sslsecpolicy);
187 if (host != IntPtr.Zero)
189 if (sectrust != IntPtr.Zero)
190 CFRelease (sectrust);