[btls] Convert BTLS icalls to pinvokes by invoking them using [DllImp… (#3799)
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsX509VerifyParam.cs
1 //
2 // MonoBtlsX509VerifyParam.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26 #if SECURITY_DEP
27 using System;
28 using System.IO;
29 using System.Runtime.InteropServices;
30 using System.Runtime.CompilerServices;
31
32 namespace Mono.Btls
33 {
34         class MonoBtlsX509VerifyParam : MonoBtlsObject
35         {
36                 internal class BoringX509VerifyParamHandle : MonoBtlsHandle
37                 {
38                         public BoringX509VerifyParamHandle (IntPtr handle)
39                                 : base (handle, true)
40                         {
41                         }
42
43                         protected override bool ReleaseHandle ()
44                         {
45                                 mono_btls_x509_verify_param_free (handle);
46                                 return true;
47                         }
48                 }
49
50                 new internal BoringX509VerifyParamHandle Handle {
51                         get { return (BoringX509VerifyParamHandle)base.Handle; }
52                 }
53
54                 [DllImport (BTLS_DYLIB)]
55                 extern static IntPtr mono_btls_x509_verify_param_new ();
56
57                 [DllImport (BTLS_DYLIB)]
58                 extern static IntPtr mono_btls_x509_verify_param_copy (IntPtr handle);
59
60                 [DllImport (BTLS_DYLIB)]
61                 extern static IntPtr mono_btls_x509_verify_param_lookup (IntPtr name);
62
63                 [DllImport (BTLS_DYLIB)]
64                 extern static int mono_btls_x509_verify_param_can_modify (IntPtr param);
65
66                 [DllImport (BTLS_DYLIB)]
67                 extern static int mono_btls_x509_verify_param_set_name (IntPtr handle, IntPtr name);
68
69                 [DllImport (BTLS_DYLIB)]
70                 extern static int mono_btls_x509_verify_param_set_host (IntPtr handle, IntPtr name, int namelen);
71
72                 [DllImport (BTLS_DYLIB)]
73                 extern static int mono_btls_x509_verify_param_add_host (IntPtr handle, IntPtr name, int namelen);
74
75                 [DllImport (BTLS_DYLIB)]
76                 extern static ulong mono_btls_x509_verify_param_get_flags (IntPtr handle);
77
78                 [DllImport (BTLS_DYLIB)]
79                 extern static int mono_btls_x509_verify_param_set_flags (IntPtr handle, ulong flags);
80
81                 [DllImport (BTLS_DYLIB)]
82                 extern static MonoBtlsX509VerifyFlags mono_btls_x509_verify_param_get_mono_flags (IntPtr handle);
83
84                 [DllImport (BTLS_DYLIB)]
85                 extern static int mono_btls_x509_verify_param_set_mono_flags (IntPtr handle, MonoBtlsX509VerifyFlags flags);
86
87                 [DllImport (BTLS_DYLIB)]
88                 extern static int mono_btls_x509_verify_param_set_purpose (IntPtr handle, MonoBtlsX509Purpose purpose);
89
90                 [DllImport (BTLS_DYLIB)]
91                 extern static int mono_btls_x509_verify_param_get_depth (IntPtr handle);
92
93                 [DllImport (BTLS_DYLIB)]
94                 extern static int mono_btls_x509_verify_param_set_depth (IntPtr handle, int depth);
95
96                 [DllImport (BTLS_DYLIB)]
97                 extern static int mono_btls_x509_verify_param_set_time (IntPtr handle, long time);
98
99                 [DllImport (BTLS_DYLIB)]
100                 extern static IntPtr mono_btls_x509_verify_param_get_peername (IntPtr handle);
101
102                 [DllImport (BTLS_DYLIB)]
103                 extern static void mono_btls_x509_verify_param_free (IntPtr handle);
104
105                 internal MonoBtlsX509VerifyParam ()
106                         : base (new BoringX509VerifyParamHandle (mono_btls_x509_verify_param_new ()))
107                 {
108                 }
109
110                 internal MonoBtlsX509VerifyParam (BoringX509VerifyParamHandle handle)
111                         : base (handle)
112                 {
113                 }
114
115                 public MonoBtlsX509VerifyParam Copy ()
116                 {
117                         var copy = mono_btls_x509_verify_param_copy (Handle.DangerousGetHandle ());
118                         CheckError (copy != IntPtr.Zero);
119                         return new MonoBtlsX509VerifyParam (new BoringX509VerifyParamHandle (copy));
120                 }
121
122                 public static MonoBtlsX509VerifyParam GetSslClient ()
123                 {
124                         return Lookup ("ssl_client", true);
125                 }
126
127                 public static MonoBtlsX509VerifyParam GetSslServer ()
128                 {
129                         return Lookup ("ssl_server", true);
130                 }
131
132                 public static MonoBtlsX509VerifyParam Lookup (string name, bool fail = false)
133                 {
134                         IntPtr namePtr = IntPtr.Zero;
135                         IntPtr handle = IntPtr.Zero;
136
137                         try {
138                                 namePtr = Marshal.StringToHGlobalAnsi (name);
139                                 handle = mono_btls_x509_verify_param_lookup (namePtr);
140                                 if (handle == IntPtr.Zero) {
141                                         if (!fail)
142                                                 return null;
143                                         throw new MonoBtlsException ("X509_VERIFY_PARAM_lookup() could not find '{0}'.", name);
144                                 }
145
146                                 return new MonoBtlsX509VerifyParam (new BoringX509VerifyParamHandle (handle));
147                         } finally {
148                                 if (namePtr != IntPtr.Zero)
149                                         Marshal.FreeHGlobal (namePtr);
150                         }
151                 }
152
153                 public bool CanModify {
154                         get {
155                                 return mono_btls_x509_verify_param_can_modify (Handle.DangerousGetHandle ()) != 0;
156                         }
157                 }
158
159                 void WantToModify ()
160                 {
161                         if (!CanModify)
162                                 throw new MonoBtlsException ("Attempting to modify read-only MonoBtlsX509VerifyParam instance.");
163                 }
164
165                 public void SetName (string name)
166                 {
167                         WantToModify ();
168                         IntPtr namePtr = IntPtr.Zero;
169                         try {
170                                 namePtr = Marshal.StringToHGlobalAnsi (name);
171                                 var ret = mono_btls_x509_verify_param_set_name (
172                                         Handle.DangerousGetHandle (), namePtr);
173                                 CheckError (ret);
174                         } finally {
175                                 if (namePtr != IntPtr.Zero)
176                                         Marshal.FreeHGlobal (namePtr);
177                         }
178                 }
179
180                 public void SetHost (string name)
181                 {
182                         WantToModify ();
183                         IntPtr namePtr = IntPtr.Zero;
184                         try {
185                                 namePtr = Marshal.StringToHGlobalAnsi (name);
186                                 var ret = mono_btls_x509_verify_param_set_host (
187                                         Handle.DangerousGetHandle (), namePtr, name.Length);
188                                 CheckError (ret);
189                         } finally {
190                                 if (namePtr != IntPtr.Zero)
191                                         Marshal.FreeHGlobal (namePtr);
192                         }
193                 }
194
195                 public void AddHost (string name)
196                 {
197                         WantToModify ();
198                         IntPtr namePtr = IntPtr.Zero;
199                         try {
200                                 namePtr = Marshal.StringToHGlobalAnsi (name);
201                                 var ret = mono_btls_x509_verify_param_add_host (
202                                         Handle.DangerousGetHandle (), namePtr, name.Length);
203                                 CheckError (ret);
204                         } finally {
205                                 if (namePtr != IntPtr.Zero)
206                                         Marshal.FreeHGlobal (namePtr);
207                         }
208                 }
209
210                 public ulong GetFlags ()
211                 {
212                         return mono_btls_x509_verify_param_get_flags (Handle.DangerousGetHandle ());
213                 }
214
215                 public void SetFlags (ulong flags)
216                 {
217                         WantToModify ();
218                         var ret = mono_btls_x509_verify_param_set_flags (
219                                 Handle.DangerousGetHandle (), flags);
220                         CheckError (ret);
221                 }
222
223                 public MonoBtlsX509VerifyFlags GetMonoFlags ()
224                 {
225                         return mono_btls_x509_verify_param_get_mono_flags (
226                                 Handle.DangerousGetHandle ());
227                 }
228
229                 public void SetMonoFlags (MonoBtlsX509VerifyFlags flags)
230                 {
231                         WantToModify ();
232                         var ret = mono_btls_x509_verify_param_set_mono_flags (
233                                 Handle.DangerousGetHandle (), flags);
234                         CheckError (ret);
235                 }
236
237                 public void SetPurpose (MonoBtlsX509Purpose purpose)
238                 {
239                         WantToModify ();
240                         var ret = mono_btls_x509_verify_param_set_purpose (
241                                 Handle.DangerousGetHandle (), purpose);
242                         CheckError (ret);
243                 }
244
245                 public int GetDepth ()
246                 {
247                         return mono_btls_x509_verify_param_get_depth (Handle.DangerousGetHandle ());
248                 }
249
250                 public void SetDepth (int depth)
251                 {
252                         WantToModify ();
253                         var ret = mono_btls_x509_verify_param_set_depth (
254                                 Handle.DangerousGetHandle (), depth);
255                         CheckError (ret);
256                 }
257
258                 public void SetTime (DateTime time)
259                 {
260                         WantToModify ();
261                         var epoch = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
262                         var ticks = (long)time.Subtract (epoch).TotalSeconds;
263                         var ret = mono_btls_x509_verify_param_set_time (
264                                 Handle.DangerousGetHandle (), ticks);
265                         CheckError (ret);
266                 }
267
268                 public string GetPeerName ()
269                 {
270                         var peer = mono_btls_x509_verify_param_get_peername (Handle.DangerousGetHandle ());
271                         if (peer == IntPtr.Zero)
272                                 return null;
273                         return Marshal.PtrToStringAnsi (peer);
274                 }
275         }
276 }
277 #endif