a4667c342be716f033b0a0c32ca7bf1715ad3ec7
[mono.git] / mcs / class / System / System.Security.Cryptography.X509Certificates / OSX509Certificates.cs
1 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
2 // Copyright 2012 Xamarin Inc.
3 //
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:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
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.
22 //
23 #if SECURITY_DEP
24 using System;
25 using System.Runtime.InteropServices;
26 using Mono.Security.X509;
27 using Mono.Security.X509.Extensions;
28
29 namespace Mono.Security.X509 {
30
31         internal class OSX509Certificates {
32                 public const string SecurityLibrary = "/System/Library/Frameworks/Security.framework/Security";
33                 public const string CoreFoundationLibrary = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";
34         
35                 [DllImport (SecurityLibrary)]
36                 extern static IntPtr SecCertificateCreateWithData (IntPtr allocator, IntPtr nsdataRef);
37                 
38                 [DllImport (SecurityLibrary)]
39                 extern static int SecTrustCreateWithCertificates (IntPtr certOrCertArray, IntPtr policies, out IntPtr sectrustref);
40                 
41                 [DllImport (SecurityLibrary)]
42                 extern static IntPtr SecPolicyCreateSSL (bool server, IntPtr cfStringHostname);
43                 
44                 [DllImport (SecurityLibrary)]
45                 extern static int SecTrustEvaluate (IntPtr secTrustRef, out SecTrustResult secTrustResultTime);
46
47                 [DllImport (CoreFoundationLibrary, CharSet=CharSet.Unicode)]
48                 extern static IntPtr CFStringCreateWithCharacters (IntPtr allocator, string str, int count);
49
50                 [DllImport (CoreFoundationLibrary)]
51                 unsafe extern static IntPtr CFDataCreate (IntPtr allocator, byte *bytes, IntPtr length);
52
53                 [DllImport (CoreFoundationLibrary)]
54                 unsafe extern static void CFRelease (IntPtr handle);
55
56                 [DllImport (CoreFoundationLibrary)]
57                 extern static IntPtr CFArrayCreate (IntPtr allocator, IntPtr values, IntPtr numValues, IntPtr callbacks);
58                 
59                 public enum SecTrustResult {
60                         Invalid,
61                         Proceed,
62                         Confirm,
63                         Deny,
64                         Unspecified,
65                         RecoverableTrustFailure,
66                         FatalTrustFailure,
67                         ResultOtherError,
68                 }
69
70                 static IntPtr MakeCFData (byte [] data)
71                 {
72                         unsafe {
73                                 fixed (byte *ptr = &data [0])
74                                         return CFDataCreate (IntPtr.Zero, ptr, (IntPtr) data.Length);
75                         }
76                 }
77
78                 static unsafe IntPtr FromIntPtrs (IntPtr [] values)
79                 {
80                         fixed (IntPtr* pv = values) {
81                                 return CFArrayCreate (
82                                         IntPtr.Zero, 
83                                         (IntPtr) pv,
84                                         (IntPtr) values.Length,
85                                         IntPtr.Zero);
86                         }
87                 }
88                 
89                 public static SecTrustResult TrustEvaluateSsl (X509CertificateCollection certificates, string host)
90                 {
91                         if (certificates == null)
92                                 return SecTrustResult.Deny;
93
94                         try {
95                                 return _TrustEvaluateSsl (certificates, host);
96                         } catch {
97                                 return SecTrustResult.Deny;
98                         }
99                 }
100                 
101                 static SecTrustResult _TrustEvaluateSsl (X509CertificateCollection certificates, string hostName)
102                 {
103                         int certCount = certificates.Count;
104                         IntPtr [] cfDataPtrs = new IntPtr [certCount];
105                         IntPtr [] secCerts = new IntPtr [certCount];
106                         IntPtr certArray = IntPtr.Zero;
107                         IntPtr sslsecpolicy = IntPtr.Zero;
108                         IntPtr host = IntPtr.Zero;
109                         IntPtr sectrust = IntPtr.Zero;
110                         SecTrustResult result = SecTrustResult.Deny;
111
112                         try {
113                                 for (int i = 0; i < certCount; i++)
114                                         cfDataPtrs [i] = MakeCFData (certificates [i].RawData);
115                                 
116                                 for (int i = 0; i < certCount; i++){
117                                         secCerts [i] = SecCertificateCreateWithData (IntPtr.Zero, cfDataPtrs [i]);
118                                         if (secCerts [i] == IntPtr.Zero)
119                                                 return SecTrustResult.Deny;
120                                 }
121                                 certArray = FromIntPtrs (secCerts);
122                                 host = CFStringCreateWithCharacters (IntPtr.Zero, hostName, hostName.Length);
123                                 sslsecpolicy = SecPolicyCreateSSL (true, host);
124
125                                 int code = SecTrustCreateWithCertificates (certArray, sslsecpolicy, out sectrust);
126                                 if (code == 0)
127                                         code = SecTrustEvaluate (sectrust, out result);
128                                 return result;
129                         } finally {
130                                 for (int i = 0; i < certCount; i++)
131                                         if (cfDataPtrs [i] != IntPtr.Zero)
132                                                 CFRelease (cfDataPtrs [i]);
133
134                                 if (certArray != IntPtr.Zero)
135                                         CFRelease (certArray);
136                                 
137                                 for (int i = 0; i < certCount; i++)
138                                         if (secCerts [i] != IntPtr.Zero)
139                                                 CFRelease (secCerts [i]);
140
141                                 if (sslsecpolicy != IntPtr.Zero)
142                                         CFRelease (sslsecpolicy);
143                                 if (host != IntPtr.Zero)
144                                         CFRelease (host);
145                                 if (sectrust != IntPtr.Zero)
146                                         CFRelease (sectrust);
147                         }
148                 }
149         }
150 }
151 #endif