1 #if SECURITY_DEP && MONO_FEATURE_APPLETLS
3 // Trust.cs: Implements the managed SecTrust wrapper.
7 // Sebastien Pouliot <sebastien@xamarin.com>
9 // Copyright 2010 Novell, Inc
10 // Copyright 2012-2014 Xamarin Inc.
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.InteropServices;
33 using System.Security;
34 using System.Security.Cryptography.X509Certificates;
38 namespace Mono.AppleTls {
39 partial class SecTrust : INativeObject, IDisposable {
42 internal SecTrust (IntPtr handle, bool owns = false)
44 if (handle == IntPtr.Zero)
45 throw new Exception ("Invalid handle");
49 CFObject.CFRetain (handle);
52 [DllImport (AppleTlsContext.SecurityLibrary)]
53 extern static SecStatusCode SecTrustCreateWithCertificates (
54 /* CFTypeRef */ IntPtr certOrCertArray,
55 /* CFTypeRef __nullable */ IntPtr policies,
56 /* SecTrustRef *__nonull */ out IntPtr sectrustref);
59 public SecTrust (X509CertificateCollection certificates, SecPolicy policy)
61 if (certificates == null)
62 throw new ArgumentNullException ("certificates");
64 SecCertificate[] array = new SecCertificate [certificates.Count];
66 foreach (var certificate in certificates)
67 array [i++] = new SecCertificate (certificate);
68 Initialize (array, policy);
71 void Initialize (SecCertificate[] array, SecPolicy policy)
73 using (var certs = CFArray.CreateArray (array)) {
74 Initialize (certs.Handle, policy);
78 void Initialize (IntPtr certHandle, SecPolicy policy)
80 SecStatusCode result = SecTrustCreateWithCertificates (certHandle, policy == null ? IntPtr.Zero : policy.Handle, out handle);
81 if (result != SecStatusCode.Success)
82 throw new ArgumentException (result.ToString ());
85 [DllImport (AppleTlsContext.SecurityLibrary)]
86 extern static SecStatusCode /* OSStatus */ SecTrustEvaluate (IntPtr /* SecTrustRef */ trust, out /* SecTrustResultType */ SecTrustResult result);
88 public SecTrustResult Evaluate ()
90 if (handle == IntPtr.Zero)
91 throw new ObjectDisposedException ("SecTrust");
94 SecStatusCode result = SecTrustEvaluate (handle, out trust);
95 if (result != SecStatusCode.Success)
96 throw new InvalidOperationException (result.ToString ());
100 [DllImport (AppleTlsContext.SecurityLibrary)]
101 extern static IntPtr /* CFIndex */ SecTrustGetCertificateCount (IntPtr /* SecTrustRef */ trust);
105 if (handle == IntPtr.Zero)
107 return (int) SecTrustGetCertificateCount (handle);
111 [DllImport (AppleTlsContext.SecurityLibrary)]
112 extern static IntPtr /* SecCertificateRef */ SecTrustGetCertificateAtIndex (IntPtr /* SecTrustRef */ trust, IntPtr /* CFIndex */ ix);
114 public SecCertificate this [IntPtr index] {
116 if (handle == IntPtr.Zero)
117 throw new ObjectDisposedException ("SecTrust");
118 if (((long)index < 0) || ((long)index >= Count))
119 throw new ArgumentOutOfRangeException ("index");
121 return new SecCertificate (SecTrustGetCertificateAtIndex (handle, index));
125 [DllImport (AppleTlsContext.SecurityLibrary)]
126 extern static SecStatusCode /* OSStatus */ SecTrustSetAnchorCertificates (IntPtr /* SecTrustRef */ trust, IntPtr /* CFArrayRef */ anchorCertificates);
128 public SecStatusCode SetAnchorCertificates (X509CertificateCollection certificates)
130 if (handle == IntPtr.Zero)
131 throw new ObjectDisposedException ("SecTrust");
132 if (certificates == null)
133 return SecTrustSetAnchorCertificates (handle, IntPtr.Zero);
135 SecCertificate[] array = new SecCertificate [certificates.Count];
137 foreach (var certificate in certificates)
138 array [i++] = new SecCertificate (certificate);
139 return SetAnchorCertificates (array);
142 public SecStatusCode SetAnchorCertificates (SecCertificate[] array)
145 return SecTrustSetAnchorCertificates (handle, IntPtr.Zero);
146 using (var certs = CFArray.FromNativeObjects (array)) {
147 return SecTrustSetAnchorCertificates (handle, certs.Handle);
151 [DllImport (AppleTlsContext.SecurityLibrary)]
152 extern static SecStatusCode /* OSStatus */ SecTrustSetAnchorCertificatesOnly (IntPtr /* SecTrustRef */ trust, bool anchorCertificatesOnly);
154 public SecStatusCode SetAnchorCertificatesOnly (bool anchorCertificatesOnly)
156 if (handle == IntPtr.Zero)
157 throw new ObjectDisposedException ("SecTrust");
159 return SecTrustSetAnchorCertificatesOnly (handle, anchorCertificatesOnly);
162 [DllImport (AppleTlsContext.SecurityLibrary)]
163 extern static SecStatusCode /* OSStatus */ SecTrustSetVerifyDate (IntPtr /* SecTrustRef */ trust, IntPtr /* CFDateRef */ date);
165 public SecStatusCode SetVerifyDate (DateTime date)
167 using (var nativeDate = CFDate.Create (date))
168 return SecTrustSetVerifyDate (handle, nativeDate.Handle);
176 protected virtual void Dispose (bool disposing)
178 if (handle != IntPtr.Zero) {
179 CFObject.CFRelease (handle);
180 handle = IntPtr.Zero;
184 public void Dispose ()
187 GC.SuppressFinalize (this);
190 public IntPtr Handle {
191 get { return handle; }