5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2015 Xamarin Inc. (http://www.xamarin.com)
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
26 #if SECURITY_DEP && MONO_FEATURE_BTLS
30 using System.Runtime.InteropServices;
31 using System.Runtime.CompilerServices;
35 delegate int MonoBtlsVerifyCallback (MonoBtlsX509StoreCtx ctx);
36 delegate int MonoBtlsSelectCallback ();
38 class MonoBtlsSsl : MonoBtlsObject
40 internal class BoringSslHandle : MonoBtlsHandle
42 public BoringSslHandle (IntPtr handle)
47 protected override bool ReleaseHandle ()
49 mono_btls_ssl_destroy (handle);
54 [DllImport (BTLS_DYLIB)]
55 extern static void mono_btls_ssl_destroy (IntPtr handle);
57 [DllImport (BTLS_DYLIB)]
58 extern static IntPtr mono_btls_ssl_new (IntPtr handle);
60 [DllImport (BTLS_DYLIB)]
61 extern static int mono_btls_ssl_use_certificate (IntPtr handle, IntPtr x509);
63 [DllImport (BTLS_DYLIB)]
64 extern static int mono_btls_ssl_use_private_key (IntPtr handle, IntPtr key);
66 [DllImport (BTLS_DYLIB)]
67 extern static int mono_btls_ssl_add_chain_certificate (IntPtr handle, IntPtr x509);
69 [DllImport (BTLS_DYLIB)]
70 extern static int mono_btls_ssl_accept (IntPtr handle);
72 [DllImport (BTLS_DYLIB)]
73 extern static int mono_btls_ssl_connect (IntPtr handle);
75 [DllImport (BTLS_DYLIB)]
76 extern static int mono_btls_ssl_handshake (IntPtr handle);
78 [DllImport (BTLS_DYLIB)]
79 extern static void mono_btls_ssl_close (IntPtr handle);
81 [DllImport (BTLS_DYLIB)]
82 extern static void mono_btls_ssl_set_bio (IntPtr handle, IntPtr bio);
84 [DllImport (BTLS_DYLIB)]
85 extern static int mono_btls_ssl_read (IntPtr handle, IntPtr data, int len);
87 [DllImport (BTLS_DYLIB)]
88 extern static int mono_btls_ssl_write (IntPtr handle, IntPtr data, int len);
90 [DllImport (BTLS_DYLIB)]
91 extern static int mono_btls_ssl_get_error (IntPtr handle, int ret_code);
93 [DllImport (BTLS_DYLIB)]
94 extern static int mono_btls_ssl_get_version (IntPtr handle);
96 [DllImport (BTLS_DYLIB)]
97 extern static void mono_btls_ssl_set_min_version (IntPtr handle, int version);
99 [DllImport (BTLS_DYLIB)]
100 extern static void mono_btls_ssl_set_max_version (IntPtr handle, int version);
102 [DllImport (BTLS_DYLIB)]
103 extern static int mono_btls_ssl_get_cipher (IntPtr handle);
105 [DllImport (BTLS_DYLIB)]
106 extern static int mono_btls_ssl_get_ciphers (IntPtr handle, out IntPtr data);
108 [DllImport (BTLS_DYLIB)]
109 extern static IntPtr mono_btls_ssl_get_peer_certificate (IntPtr handle);
111 [DllImport (BTLS_DYLIB)]
112 extern static int mono_btls_ssl_set_cipher_list (IntPtr handle, IntPtr str);
114 [DllImport (BTLS_DYLIB)]
115 extern static void mono_btls_ssl_print_errors_cb (IntPtr func, IntPtr ctx);
117 [DllImport (BTLS_DYLIB)]
118 extern static int mono_btls_ssl_set_verify_param (IntPtr handle, IntPtr param);
120 [DllImport (BTLS_DYLIB)]
121 extern static int mono_btls_ssl_set_server_name (IntPtr handle, IntPtr name);
123 [DllImport (BTLS_DYLIB)]
124 extern static IntPtr mono_btls_ssl_get_server_name (IntPtr handle);
126 static BoringSslHandle Create_internal (MonoBtlsSslCtx ctx)
128 var handle = mono_btls_ssl_new (ctx.Handle.DangerousGetHandle ());
129 if (handle == IntPtr.Zero)
130 throw new MonoBtlsException ();
131 return new BoringSslHandle (handle);
134 PrintErrorsCallbackFunc printErrorsFunc;
135 IntPtr printErrorsFuncPtr;
137 public MonoBtlsSsl (MonoBtlsSslCtx ctx)
138 : base (Create_internal (ctx))
140 printErrorsFunc = PrintErrorsCallback;
141 printErrorsFuncPtr = Marshal.GetFunctionPointerForDelegate (printErrorsFunc);
144 new internal BoringSslHandle Handle {
145 get { return (BoringSslHandle)base.Handle; }
148 public void SetBio (MonoBtlsBio bio)
151 mono_btls_ssl_set_bio (
152 Handle.DangerousGetHandle (),
153 bio.Handle.DangerousGetHandle ());
156 Exception ThrowError ([CallerMemberName] string callerName = null)
160 if (callerName == null)
161 callerName = GetType ().Name;
162 errors = GetErrors ();
167 if (errors != null) {
168 Console.Error.WriteLine ("ERROR: {0} failed: {1}", callerName, errors);
169 throw new MonoBtlsException ("{0} failed: {1}.", callerName, errors);
171 Console.Error.WriteLine ("ERROR: {0} failed.", callerName);
172 throw new MonoBtlsException ("{0} failed.", callerName);
176 MonoBtlsSslError GetError (int ret_code)
179 var error = mono_btls_ssl_get_error (
180 Handle.DangerousGetHandle (), ret_code);
181 return (MonoBtlsSslError)error;
184 public void SetCertificate (MonoBtlsX509 x509)
188 var ret = mono_btls_ssl_use_certificate (
189 Handle.DangerousGetHandle (),
190 x509.Handle.DangerousGetHandle ());
195 public void SetPrivateKey (MonoBtlsKey key)
199 var ret = mono_btls_ssl_use_private_key (
200 Handle.DangerousGetHandle (),
201 key.Handle.DangerousGetHandle ());
206 public void AddIntermediateCertificate (MonoBtlsX509 x509)
210 var ret = mono_btls_ssl_add_chain_certificate (
211 Handle.DangerousGetHandle (),
212 x509.Handle.DangerousGetHandle ());
217 public MonoBtlsSslError Accept ()
221 var ret = mono_btls_ssl_accept (Handle.DangerousGetHandle ());
223 var error = GetError (ret);
227 public MonoBtlsSslError Connect ()
231 var ret = mono_btls_ssl_connect (Handle.DangerousGetHandle ());
233 var error = GetError (ret);
237 public MonoBtlsSslError Handshake ()
241 var ret = mono_btls_ssl_handshake (Handle.DangerousGetHandle ());
243 var error = GetError (ret);
247 delegate int PrintErrorsCallbackFunc (IntPtr str, IntPtr len, IntPtr ctx);
249 [Mono.Util.MonoPInvokeCallback (typeof (PrintErrorsCallbackFunc))]
250 static int PrintErrorsCallback (IntPtr str, IntPtr len, IntPtr ctx)
252 var sb = (StringBuilder)GCHandle.FromIntPtr (ctx).Target;
254 var text = Marshal.PtrToStringAnsi (str, (int)len);
262 public string GetErrors ()
264 var text = new StringBuilder ();
265 var handle = GCHandle.Alloc (text);
268 mono_btls_ssl_print_errors_cb (printErrorsFuncPtr, GCHandle.ToIntPtr (handle));
269 return text.ToString ();
271 if (handle.IsAllocated)
276 public void PrintErrors ()
278 var errors = GetErrors ();
279 if (string.IsNullOrEmpty (errors))
281 Console.Error.WriteLine (errors);
284 public MonoBtlsSslError Read (IntPtr data, ref int dataSize)
287 var ret = mono_btls_ssl_read (
288 Handle.DangerousGetHandle (), data, dataSize);
292 return MonoBtlsSslError.None;
295 var error = mono_btls_ssl_get_error (
296 Handle.DangerousGetHandle (), ret);
298 return (MonoBtlsSslError)error;
301 public MonoBtlsSslError Write (IntPtr data, ref int dataSize)
304 var ret = mono_btls_ssl_write (
305 Handle.DangerousGetHandle (), data, dataSize);
309 return MonoBtlsSslError.None;
312 var error = mono_btls_ssl_get_error (
313 Handle.DangerousGetHandle (), ret);
315 return (MonoBtlsSslError)error;
318 public int GetVersion ()
321 return mono_btls_ssl_get_version (Handle.DangerousGetHandle ());
324 public void SetMinVersion (int version)
327 mono_btls_ssl_set_min_version (Handle.DangerousGetHandle (), version);
330 public void SetMaxVersion (int version)
333 mono_btls_ssl_set_max_version (Handle.DangerousGetHandle (), version);
336 public int GetCipher ()
339 var cipher = mono_btls_ssl_get_cipher (Handle.DangerousGetHandle ());
340 CheckError (cipher > 0);
344 public short[] GetCiphers ()
348 var count = mono_btls_ssl_get_ciphers (
349 Handle.DangerousGetHandle (), out data);
350 CheckError (count > 0);
352 short[] ciphers = new short[count];
353 Marshal.Copy (data, ciphers, 0, count);
360 public void SetCipherList (string str)
363 IntPtr strPtr = IntPtr.Zero;
365 strPtr = Marshal.StringToHGlobalAnsi (str);
366 var ret = mono_btls_ssl_set_cipher_list (
367 Handle.DangerousGetHandle (), strPtr);
370 if (strPtr != IntPtr.Zero)
371 Marshal.FreeHGlobal (strPtr);
375 public MonoBtlsX509 GetPeerCertificate ()
378 var x509 = mono_btls_ssl_get_peer_certificate (
379 Handle.DangerousGetHandle ());
380 if (x509 == IntPtr.Zero)
382 return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (x509));
385 public void SetVerifyParam (MonoBtlsX509VerifyParam param)
388 var ret = mono_btls_ssl_set_verify_param (
389 Handle.DangerousGetHandle (),
390 param.Handle.DangerousGetHandle ());
394 public void SetServerName (string name)
397 IntPtr namePtr = IntPtr.Zero;
399 namePtr = Marshal.StringToHGlobalAnsi (name);
400 var ret = mono_btls_ssl_set_server_name (
401 Handle.DangerousGetHandle (), namePtr);
404 if (namePtr != IntPtr.Zero)
405 Marshal.FreeHGlobal (namePtr);
409 public string GetServerName ()
412 var namePtr = mono_btls_ssl_get_server_name (
413 Handle.DangerousGetHandle ());
414 if (namePtr == IntPtr.Zero)
416 return Marshal.PtrToStringAnsi (namePtr);
419 protected override void Close ()
421 mono_btls_ssl_close (Handle.DangerousGetHandle ());