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;
39 delegate int MonoBtlsVerifyCallback (MonoBtlsX509StoreCtx ctx);
40 delegate int MonoBtlsSelectCallback ();
42 class MonoBtlsSsl : MonoBtlsObject
44 internal class BoringSslHandle : MonoBtlsHandle
46 public BoringSslHandle (IntPtr handle)
51 protected override bool ReleaseHandle ()
53 mono_btls_ssl_destroy (handle);
58 [DllImport (BTLS_DYLIB)]
59 extern static void mono_btls_ssl_destroy (IntPtr handle);
61 [DllImport (BTLS_DYLIB)]
62 extern static IntPtr mono_btls_ssl_new (IntPtr handle);
64 [DllImport (BTLS_DYLIB)]
65 extern static int mono_btls_ssl_use_certificate (IntPtr handle, IntPtr x509);
67 [DllImport (BTLS_DYLIB)]
68 extern static int mono_btls_ssl_use_private_key (IntPtr handle, IntPtr key);
70 [DllImport (BTLS_DYLIB)]
71 extern static int mono_btls_ssl_add_chain_certificate (IntPtr handle, IntPtr x509);
73 [DllImport (BTLS_DYLIB)]
74 extern static int mono_btls_ssl_accept (IntPtr handle);
76 [DllImport (BTLS_DYLIB)]
77 extern static int mono_btls_ssl_connect (IntPtr handle);
79 [DllImport (BTLS_DYLIB)]
80 extern static int mono_btls_ssl_handshake (IntPtr handle);
82 [DllImport (BTLS_DYLIB)]
83 extern static void mono_btls_ssl_close (IntPtr handle);
85 [DllImport (BTLS_DYLIB)]
86 extern static void mono_btls_ssl_set_bio (IntPtr handle, IntPtr bio);
88 [DllImport (BTLS_DYLIB)]
89 extern static int mono_btls_ssl_read (IntPtr handle, IntPtr data, int len);
91 [DllImport (BTLS_DYLIB)]
92 extern static int mono_btls_ssl_write (IntPtr handle, IntPtr data, int len);
94 [DllImport (BTLS_DYLIB)]
95 extern static int mono_btls_ssl_get_error (IntPtr handle, int ret_code);
97 [DllImport (BTLS_DYLIB)]
98 extern static int mono_btls_ssl_get_version (IntPtr handle);
100 [DllImport (BTLS_DYLIB)]
101 extern static void mono_btls_ssl_set_min_version (IntPtr handle, int version);
103 [DllImport (BTLS_DYLIB)]
104 extern static void mono_btls_ssl_set_max_version (IntPtr handle, int version);
106 [DllImport (BTLS_DYLIB)]
107 extern static int mono_btls_ssl_get_cipher (IntPtr handle);
109 [DllImport (BTLS_DYLIB)]
110 extern static int mono_btls_ssl_get_ciphers (IntPtr handle, out IntPtr data);
112 [DllImport (BTLS_DYLIB)]
113 extern static IntPtr mono_btls_ssl_get_peer_certificate (IntPtr handle);
115 [DllImport (BTLS_DYLIB)]
116 extern static int mono_btls_ssl_set_cipher_list (IntPtr handle, IntPtr str);
118 [DllImport (BTLS_DYLIB)]
119 extern static void mono_btls_ssl_print_errors_cb (IntPtr func, IntPtr ctx);
121 [DllImport (BTLS_DYLIB)]
122 extern static int mono_btls_ssl_set_verify_param (IntPtr handle, IntPtr param);
124 [DllImport (BTLS_DYLIB)]
125 extern static int mono_btls_ssl_set_server_name (IntPtr handle, IntPtr name);
127 static BoringSslHandle Create_internal (MonoBtlsSslCtx ctx)
129 var handle = mono_btls_ssl_new (ctx.Handle.DangerousGetHandle ());
130 if (handle == IntPtr.Zero)
131 throw new MonoBtlsException ();
132 return new BoringSslHandle (handle);
135 PrintErrorsCallbackFunc printErrorsFunc;
136 IntPtr printErrorsFuncPtr;
138 public MonoBtlsSsl (MonoBtlsSslCtx ctx)
139 : base (Create_internal (ctx))
141 printErrorsFunc = PrintErrorsCallback;
142 printErrorsFuncPtr = Marshal.GetFunctionPointerForDelegate (printErrorsFunc);
145 new internal BoringSslHandle Handle {
146 get { return (BoringSslHandle)base.Handle; }
149 public void SetBio (MonoBtlsBio bio)
152 mono_btls_ssl_set_bio (
153 Handle.DangerousGetHandle (),
154 bio.Handle.DangerousGetHandle ());
157 Exception ThrowError ([CallerMemberName] string callerName = null)
161 if (callerName == null)
162 callerName = GetType ().Name;
163 errors = GetErrors ();
168 if (errors != null) {
169 Console.Error.WriteLine ("ERROR: {0} failed: {1}", callerName, errors);
170 throw new MonoBtlsException ("{0} failed: {1}.", callerName, errors);
172 Console.Error.WriteLine ("ERROR: {0} failed.", callerName);
173 throw new MonoBtlsException ("{0} failed.", callerName);
177 MonoBtlsSslError GetError (int ret_code)
180 var error = mono_btls_ssl_get_error (
181 Handle.DangerousGetHandle (), ret_code);
182 return (MonoBtlsSslError)error;
185 public void SetCertificate (MonoBtlsX509 x509)
189 var ret = mono_btls_ssl_use_certificate (
190 Handle.DangerousGetHandle (),
191 x509.Handle.DangerousGetHandle ());
196 public void SetPrivateKey (MonoBtlsKey key)
200 var ret = mono_btls_ssl_use_private_key (
201 Handle.DangerousGetHandle (),
202 key.Handle.DangerousGetHandle ());
207 public void AddIntermediateCertificate (MonoBtlsX509 x509)
211 var ret = mono_btls_ssl_add_chain_certificate (
212 Handle.DangerousGetHandle (),
213 x509.Handle.DangerousGetHandle ());
218 public MonoBtlsSslError Accept ()
222 var ret = mono_btls_ssl_accept (Handle.DangerousGetHandle ());
224 var error = GetError (ret);
228 public MonoBtlsSslError Connect ()
232 var ret = mono_btls_ssl_connect (Handle.DangerousGetHandle ());
234 var error = GetError (ret);
238 public MonoBtlsSslError Handshake ()
242 var ret = mono_btls_ssl_handshake (Handle.DangerousGetHandle ());
244 var error = GetError (ret);
248 delegate int PrintErrorsCallbackFunc (IntPtr str, IntPtr len, IntPtr ctx);
251 [MonoPInvokeCallback (typeof (PrintErrorsCallbackFunc))]
253 static int PrintErrorsCallback (IntPtr str, IntPtr len, IntPtr ctx)
255 var sb = (StringBuilder)GCHandle.FromIntPtr (ctx).Target;
257 var text = Marshal.PtrToStringAnsi (str, (int)len);
265 public string GetErrors ()
267 var text = new StringBuilder ();
268 var handle = GCHandle.Alloc (text);
271 mono_btls_ssl_print_errors_cb (printErrorsFuncPtr, GCHandle.ToIntPtr (handle));
272 return text.ToString ();
274 if (handle.IsAllocated)
279 public void PrintErrors ()
281 var errors = GetErrors ();
282 if (string.IsNullOrEmpty (errors))
284 Console.Error.WriteLine (errors);
287 public MonoBtlsSslError Read (IntPtr data, ref int dataSize)
290 var ret = mono_btls_ssl_read (
291 Handle.DangerousGetHandle (), data, dataSize);
295 return MonoBtlsSslError.None;
298 var error = mono_btls_ssl_get_error (
299 Handle.DangerousGetHandle (), ret);
301 return (MonoBtlsSslError)error;
304 public MonoBtlsSslError Write (IntPtr data, ref int dataSize)
307 var ret = mono_btls_ssl_write (
308 Handle.DangerousGetHandle (), data, dataSize);
312 return MonoBtlsSslError.None;
315 var error = mono_btls_ssl_get_error (
316 Handle.DangerousGetHandle (), ret);
318 return (MonoBtlsSslError)error;
321 public int GetVersion ()
324 return mono_btls_ssl_get_version (Handle.DangerousGetHandle ());
327 public void SetMinVersion (int version)
330 mono_btls_ssl_set_min_version (Handle.DangerousGetHandle (), version);
333 public void SetMaxVersion (int version)
336 mono_btls_ssl_set_max_version (Handle.DangerousGetHandle (), version);
339 public int GetCipher ()
342 var cipher = mono_btls_ssl_get_cipher (Handle.DangerousGetHandle ());
343 CheckError (cipher > 0);
347 public short[] GetCiphers ()
351 var count = mono_btls_ssl_get_ciphers (
352 Handle.DangerousGetHandle (), out data);
353 CheckError (count > 0);
355 short[] ciphers = new short[count];
356 Marshal.Copy (data, ciphers, 0, count);
363 public void SetCipherList (string str)
366 IntPtr strPtr = IntPtr.Zero;
368 strPtr = Marshal.StringToHGlobalAnsi (str);
369 var ret = mono_btls_ssl_set_cipher_list (
370 Handle.DangerousGetHandle (), strPtr);
373 if (strPtr != IntPtr.Zero)
374 Marshal.FreeHGlobal (strPtr);
378 public MonoBtlsX509 GetPeerCertificate ()
381 var x509 = mono_btls_ssl_get_peer_certificate (
382 Handle.DangerousGetHandle ());
383 if (x509 == IntPtr.Zero)
385 return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (x509));
388 public void SetVerifyParam (MonoBtlsX509VerifyParam param)
391 var ret = mono_btls_ssl_set_verify_param (
392 Handle.DangerousGetHandle (),
393 param.Handle.DangerousGetHandle ());
397 public void SetServerName (string name)
400 IntPtr namePtr = IntPtr.Zero;
402 namePtr = Marshal.StringToHGlobalAnsi (name);
403 var ret = mono_btls_ssl_set_server_name (
404 Handle.DangerousGetHandle (), namePtr);
407 if (namePtr != IntPtr.Zero)
408 Marshal.FreeHGlobal (namePtr);
412 protected override void Close ()
414 mono_btls_ssl_close (Handle.DangerousGetHandle ());