5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2016 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.CompilerServices;
31 using System.Runtime.InteropServices;
39 class MonoBtlsBio : MonoBtlsObject
41 internal MonoBtlsBio (BoringBioHandle handle)
46 new protected internal BoringBioHandle Handle {
47 get { return (BoringBioHandle)base.Handle; }
50 protected internal class BoringBioHandle : MonoBtlsHandle
52 public BoringBioHandle (IntPtr handle)
57 protected override bool ReleaseHandle ()
59 if (handle != IntPtr.Zero) {
60 mono_btls_bio_free (handle);
68 public static MonoBtlsBio CreateMonoStream (Stream stream)
70 return MonoBtlsBioMono.CreateStream (stream, false);
73 [DllImport (BTLS_DYLIB)]
74 extern static int mono_btls_bio_read (IntPtr bio, IntPtr data, int len);
76 [DllImport (BTLS_DYLIB)]
77 extern static int mono_btls_bio_write (IntPtr bio, IntPtr data, int len);
79 [DllImport (BTLS_DYLIB)]
80 extern static int mono_btls_bio_flush (IntPtr bio);
82 [DllImport (BTLS_DYLIB)]
83 extern static int mono_btls_bio_indent (IntPtr bio, uint indent, uint max_indent);
85 [DllImport (BTLS_DYLIB)]
86 extern static int mono_btls_bio_hexdump (IntPtr bio, IntPtr data, int len, uint indent);
88 [DllImport (BTLS_DYLIB)]
89 extern static void mono_btls_bio_print_errors (IntPtr bio);
91 [DllImport (BTLS_DYLIB)]
92 extern static void mono_btls_bio_free (IntPtr handle);
94 public int Read (byte[] buffer, int offset, int size)
97 var data = Marshal.AllocHGlobal (size);
98 if (data == IntPtr.Zero)
99 throw new OutOfMemoryException ();
101 bool release = false;
103 Handle.DangerousAddRef (ref release);
104 var ret = mono_btls_bio_read (Handle.DangerousGetHandle (), data, size);
106 Marshal.Copy (data, buffer,offset, ret);
110 Handle.DangerousRelease ();
111 Marshal.FreeHGlobal (data);
115 public int Write (byte[] buffer, int offset, int size)
118 var data = Marshal.AllocHGlobal (size);
119 if (data == IntPtr.Zero)
120 throw new OutOfMemoryException ();
122 bool release = false;
124 Handle.DangerousAddRef (ref release);
125 Marshal.Copy (buffer, offset, data, size);
126 return mono_btls_bio_write (Handle.DangerousGetHandle (), data, size);
129 Handle.DangerousRelease ();
130 Marshal.FreeHGlobal (data);
137 bool release = false;
139 Handle.DangerousAddRef (ref release);
140 return mono_btls_bio_flush (Handle.DangerousGetHandle ());
143 Handle.DangerousRelease ();
147 public int Indent (uint indent, uint max_indent)
150 bool release = false;
152 Handle.DangerousAddRef (ref release);
153 return mono_btls_bio_indent (Handle.DangerousGetHandle (), indent, max_indent);
156 Handle.DangerousRelease ();
160 public int HexDump (byte[] buffer, uint indent)
163 var data = Marshal.AllocHGlobal (buffer.Length);
164 if (data == IntPtr.Zero)
165 throw new OutOfMemoryException ();
167 bool release = false;
169 Handle.DangerousAddRef (ref release);
170 Marshal.Copy (buffer, 0, data, buffer.Length);
171 return mono_btls_bio_hexdump (Handle.DangerousGetHandle (), data, buffer.Length, indent);
174 Handle.DangerousRelease ();
175 Marshal.FreeHGlobal (data);
179 public void PrintErrors ()
182 bool release = false;
184 Handle.DangerousAddRef (ref release);
185 mono_btls_bio_print_errors (Handle.DangerousGetHandle ());
188 Handle.DangerousRelease ();
193 class MonoBtlsBioMemory : MonoBtlsBio
195 [DllImport (BTLS_DYLIB)]
196 extern static IntPtr mono_btls_bio_mem_new ();
198 [DllImport (BTLS_DYLIB)]
199 extern static int mono_btls_bio_mem_get_data (IntPtr handle, out IntPtr data);
201 public MonoBtlsBioMemory ()
202 : base (new BoringBioHandle (mono_btls_bio_mem_new ()))
206 public byte[] GetData ()
209 bool release = false;
211 Handle.DangerousAddRef (ref release);
212 var size = mono_btls_bio_mem_get_data (Handle.DangerousGetHandle (), out data);
213 CheckError (size > 0);
214 var buffer = new byte[size];
215 Marshal.Copy (data, buffer, 0, size);
219 Handle.DangerousRelease ();
224 interface IMonoBtlsBioMono
226 int Read (byte[] buffer, int offset, int size, out bool wantMore);
228 bool Write (byte[] buffer, int offset, int size);
235 class MonoBtlsBioMono : MonoBtlsBio
239 BioReadFunc readFunc;
240 BioWriteFunc writeFunc;
241 BioControlFunc controlFunc;
244 IntPtr controlFuncPtr;
245 IMonoBtlsBioMono backend;
247 public MonoBtlsBioMono (IMonoBtlsBioMono backend)
248 : base (new BoringBioHandle (mono_btls_bio_mono_new ()))
250 this.backend = backend;
251 handle = GCHandle.Alloc (this);
252 instance = GCHandle.ToIntPtr (handle);
255 controlFunc = Control;
256 readFuncPtr = Marshal.GetFunctionPointerForDelegate (readFunc);
257 writeFuncPtr = Marshal.GetFunctionPointerForDelegate (writeFunc);
258 controlFuncPtr = Marshal.GetFunctionPointerForDelegate (controlFunc);
259 mono_btls_bio_mono_initialize (Handle.DangerousGetHandle (), instance, readFuncPtr, writeFuncPtr, controlFuncPtr);
262 public static MonoBtlsBioMono CreateStream (Stream stream, bool ownsStream)
264 return new MonoBtlsBioMono (new StreamBackend (stream, ownsStream));
267 public static MonoBtlsBioMono CreateString (StringWriter writer)
269 return new MonoBtlsBioMono (new StringBackend (writer));
277 delegate int BioReadFunc (IntPtr bio, IntPtr data, int dataLength, out int wantMore);
278 delegate int BioWriteFunc (IntPtr bio, IntPtr data, int dataLength);
279 delegate long BioControlFunc (IntPtr bio, ControlCommand command, long arg);
281 [DllImport (BTLS_DYLIB)]
282 extern static IntPtr mono_btls_bio_mono_new ();
284 [DllImport (BTLS_DYLIB)]
285 extern static void mono_btls_bio_mono_initialize (IntPtr handle, IntPtr instance, IntPtr readFunc, IntPtr writeFunc, IntPtr controlFunc);
287 long Control (ControlCommand command, long arg)
290 case ControlCommand.Flush:
295 throw new NotImplementedException ();
299 int OnRead (IntPtr data, int dataLength, out int wantMore)
302 var buffer = new byte[dataLength];
303 var ret = backend.Read (buffer, 0, dataLength, out wantMoreBool);
304 wantMore = wantMoreBool ? 1 : 0;
307 Marshal.Copy (buffer, 0, data, ret);
312 [MonoPInvokeCallback (typeof (BioReadFunc))]
314 static int OnRead (IntPtr instance, IntPtr data, int dataLength, out int wantMore)
316 var c = (MonoBtlsBioMono)GCHandle.FromIntPtr (instance).Target;
318 return c.OnRead (data, dataLength, out wantMore);
319 } catch (Exception ex) {
326 int OnWrite (IntPtr data, int dataLength)
328 var buffer = new byte[dataLength];
329 Marshal.Copy (data, buffer, 0, dataLength);
330 var ok = backend.Write (buffer, 0, dataLength);
331 return ok ? dataLength : -1;
335 [MonoPInvokeCallback (typeof (BioWriteFunc))]
337 static int OnWrite (IntPtr instance, IntPtr data, int dataLength)
339 var c = (MonoBtlsBioMono)GCHandle.FromIntPtr (instance).Target;
341 return c.OnWrite (data, dataLength);
342 } catch (Exception ex) {
349 [MonoPInvokeCallback (typeof (BioControlFunc))]
351 static long Control (IntPtr instance, ControlCommand command, long arg)
353 var c = (MonoBtlsBioMono)GCHandle.FromIntPtr (instance).Target;
355 return c.Control (command, arg);
356 } catch (Exception ex) {
362 protected override void Close ()
365 if (backend != null) {
369 if (handle.IsAllocated)
376 class StreamBackend : IMonoBtlsBioMono
381 public Stream InnerStream {
382 get { return stream; }
385 public StreamBackend (Stream stream, bool ownsStream)
387 this.stream = stream;
388 this.ownsStream = ownsStream;
391 public int Read (byte[] buffer, int offset, int size, out bool wantMore)
394 return stream.Read (buffer, offset, size);
397 public bool Write (byte[] buffer, int offset, int size)
399 stream.Write (buffer, offset, size);
410 if (ownsStream && stream != null)
416 class StringBackend : IMonoBtlsBioMono
419 Encoding encoding = new UTF8Encoding ();
421 public StringBackend (StringWriter writer)
423 this.writer = writer;
426 public int Read (byte[] buffer, int offset, int size, out bool wantMore)
432 public bool Write (byte[] buffer, int offset, int size)
434 var text = encoding.GetString (buffer, offset, size);