Overwrites dequeued ref/value with default. Fixes 18421.
[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
25 #if MONOTOUCH || MONODROID
26 using MSX = Mono.Security.X509;
27 #else
28 extern alias MonoSecurity;
29 using MSX = MonoSecurity::Mono.Security.X509;
30 #endif
31
32 using System;
33 using System.Runtime.InteropServices;
34
35 namespace System.Security.Cryptography.X509Certificates {
36
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";
40         
41                 [DllImport (SecurityLibrary)]
42                 extern static IntPtr SecCertificateCreateWithData (IntPtr allocator, IntPtr nsdataRef);
43                 
44                 [DllImport (SecurityLibrary)]
45                 extern static int SecTrustCreateWithCertificates (IntPtr certOrCertArray, IntPtr policies, out IntPtr sectrustref);
46                 
47                 [DllImport (SecurityLibrary)]
48                 extern static IntPtr SecPolicyCreateSSL (bool server, IntPtr cfStringHostname);
49                 
50                 [DllImport (SecurityLibrary)]
51                 extern static int SecTrustEvaluate (IntPtr secTrustRef, out SecTrustResult secTrustResultTime);
52
53                 [DllImport (CoreFoundationLibrary, CharSet=CharSet.Unicode)]
54                 extern static IntPtr CFStringCreateWithCharacters (IntPtr allocator, string str, int count);
55
56                 [DllImport (CoreFoundationLibrary)]
57                 unsafe extern static IntPtr CFDataCreate (IntPtr allocator, byte *bytes, IntPtr length);
58
59                 [DllImport (CoreFoundationLibrary)]
60                 unsafe extern static void CFRelease (IntPtr handle);
61
62                 [DllImport (CoreFoundationLibrary)]
63                 extern static IntPtr CFArrayCreate (IntPtr allocator, IntPtr values, IntPtr numValues, IntPtr callbacks);
64                 
65                 public enum SecTrustResult {
66                         Invalid,
67                         Proceed,
68                         Confirm,
69                         Deny,
70                         Unspecified,
71                         RecoverableTrustFailure,
72                         FatalTrustFailure,
73                         ResultOtherError,
74                 }
75
76                 static IntPtr MakeCFData (byte [] data)
77                 {
78                         unsafe {
79                                 fixed (byte *ptr = &data [0])
80                                         return CFDataCreate (IntPtr.Zero, ptr, (IntPtr) data.Length);
81                         }
82                 }
83
84                 static unsafe IntPtr FromIntPtrs (IntPtr [] values)
85                 {
86                         fixed (IntPtr* pv = values) {
87                                 return CFArrayCreate (
88                                         IntPtr.Zero, 
89                                         (IntPtr) pv,
90                                         (IntPtr) values.Length,
91                                         IntPtr.Zero);
92                         }
93                 }
94                 
95                 public static SecTrustResult TrustEvaluateSsl (MSX.X509CertificateCollection certificates, string host)
96                 {
97                         if (certificates == null)
98                                 return SecTrustResult.Deny;
99
100                         try {
101                                 return _TrustEvaluateSsl (certificates, host);
102                         } catch {
103                                 return SecTrustResult.Deny;
104                         }
105                 }
106                 
107                 static SecTrustResult _TrustEvaluateSsl (MSX.X509CertificateCollection certificates, string hostName)
108                 {
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;
117
118                         try {
119                                 for (int i = 0; i < certCount; i++)
120                                         cfDataPtrs [i] = MakeCFData (certificates [i].RawData);
121                                 
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;
126                                 }
127                                 certArray = FromIntPtrs (secCerts);
128                                 host = CFStringCreateWithCharacters (IntPtr.Zero, hostName, hostName.Length);
129                                 sslsecpolicy = SecPolicyCreateSSL (true, host);
130
131                                 int code = SecTrustCreateWithCertificates (certArray, sslsecpolicy, out sectrust);
132                                 if (code == 0)
133                                         code = SecTrustEvaluate (sectrust, out result);
134                                 return result;
135                         } finally {
136                                 for (int i = 0; i < certCount; i++)
137                                         if (cfDataPtrs [i] != IntPtr.Zero)
138                                                 CFRelease (cfDataPtrs [i]);
139
140                                 if (certArray != IntPtr.Zero)
141                                         CFRelease (certArray);
142                                 
143                                 for (int i = 0; i < certCount; i++)
144                                         if (secCerts [i] != IntPtr.Zero)
145                                                 CFRelease (secCerts [i]);
146
147                                 if (sslsecpolicy != IntPtr.Zero)
148                                         CFRelease (sslsecpolicy);
149                                 if (host != IntPtr.Zero)
150                                         CFRelease (host);
151                                 if (sectrust != IntPtr.Zero)
152                                         CFRelease (sectrust);
153                         }
154                 }
155         }
156 }
157 #endif