Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MonoBtlsSsl.cs « Mono.Btls « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d04a15a1a668c8ce2291423a9511c6e7b4c62877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//
// MonoBtlsSsl.cs
//
// Author:
//       Martin Baulig <martin.baulig@xamarin.com>
//
// Copyright (c) 2015 Xamarin Inc. (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#if SECURITY_DEP
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

#if MONOTOUCH
using MonoTouch;
#endif

namespace Mono.Btls
{
	delegate int MonoBtlsVerifyCallback (MonoBtlsX509StoreCtx ctx);
	delegate int MonoBtlsSelectCallback ();

	class MonoBtlsSsl : MonoBtlsObject
	{
		internal class BoringSslHandle : MonoBtlsHandle
		{
			public BoringSslHandle (IntPtr handle)
				: base (handle, true)
			{
			}

			protected override bool ReleaseHandle ()
			{
				mono_btls_ssl_destroy (handle);
				return true;
			}
		}

		[DllImport (BTLS_DYLIB)]
		extern static void mono_btls_ssl_destroy (IntPtr handle);

		[DllImport (BTLS_DYLIB)]
		extern static IntPtr mono_btls_ssl_new (IntPtr handle);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_use_certificate (IntPtr handle, IntPtr x509);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_use_private_key (IntPtr handle, IntPtr key);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_add_chain_certificate (IntPtr handle, IntPtr x509);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_accept (IntPtr handle);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_connect (IntPtr handle);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_handshake (IntPtr handle);

		[DllImport (BTLS_DYLIB)]
		extern static void mono_btls_ssl_close (IntPtr handle);

		[DllImport (BTLS_DYLIB)]
		extern static void mono_btls_ssl_set_bio (IntPtr handle, IntPtr bio);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_read (IntPtr handle, IntPtr data, int len);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_write (IntPtr handle, IntPtr data, int len);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_get_error (IntPtr handle, int ret_code);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_get_version (IntPtr handle);

		[DllImport (BTLS_DYLIB)]
		extern static void mono_btls_ssl_set_min_version (IntPtr handle, int version);

		[DllImport (BTLS_DYLIB)]
		extern static void mono_btls_ssl_set_max_version (IntPtr handle, int version);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_get_cipher (IntPtr handle);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_get_ciphers (IntPtr handle, out IntPtr data);

		[DllImport (BTLS_DYLIB)]
		extern static IntPtr mono_btls_ssl_get_peer_certificate (IntPtr handle);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_set_cipher_list (IntPtr handle, IntPtr str);

		[DllImport (BTLS_DYLIB)]
		extern static void mono_btls_ssl_print_errors_cb (IntPtr func, IntPtr ctx);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_set_verify_param (IntPtr handle, IntPtr param);

		[DllImport (BTLS_DYLIB)]
		extern static int mono_btls_ssl_set_server_name (IntPtr handle, IntPtr name);

		static BoringSslHandle Create_internal (MonoBtlsSslCtx ctx)
		{
			var handle = mono_btls_ssl_new (ctx.Handle.DangerousGetHandle ());
			if (handle == IntPtr.Zero)
				throw new MonoBtlsException ();
			return new BoringSslHandle (handle);
		}

		PrintErrorsCallbackFunc printErrorsFunc;
		IntPtr printErrorsFuncPtr;

		public MonoBtlsSsl (MonoBtlsSslCtx ctx)
			: base (Create_internal (ctx))
		{
			printErrorsFunc = PrintErrorsCallback;
			printErrorsFuncPtr = Marshal.GetFunctionPointerForDelegate (printErrorsFunc);
		}

		new internal BoringSslHandle Handle {
			get { return (BoringSslHandle)base.Handle; }
		}

		public void SetBio (MonoBtlsBio bio)
		{
			CheckThrow ();
			mono_btls_ssl_set_bio (
				Handle.DangerousGetHandle (),
				bio.Handle.DangerousGetHandle ());
		}

		Exception ThrowError ([CallerMemberName] string callerName = null)
		{
			string errors;
			try {
				if (callerName == null)
					callerName = GetType ().Name;
				errors = GetErrors ();
			} catch {
				errors = null;
			}

			if (errors != null) {
				Console.Error.WriteLine ("ERROR: {0} failed: {1}", callerName, errors);
				throw new MonoBtlsException ("{0} failed: {1}.", callerName, errors);
			} else {
				Console.Error.WriteLine ("ERROR: {0} failed.", callerName);
				throw new MonoBtlsException ("{0} failed.", callerName);
			}
		}

		MonoBtlsSslError GetError (int ret_code)
		{
			CheckThrow ();
			var error = mono_btls_ssl_get_error (
				Handle.DangerousGetHandle (), ret_code);
			return (MonoBtlsSslError)error;
		}

		public void SetCertificate (MonoBtlsX509 x509)
		{
			CheckThrow ();

			var ret = mono_btls_ssl_use_certificate (
				Handle.DangerousGetHandle (),
				x509.Handle.DangerousGetHandle ());
			if (ret <= 0)
				throw ThrowError ();
		}

		public void SetPrivateKey (MonoBtlsKey key)
		{
			CheckThrow ();

			var ret = mono_btls_ssl_use_private_key (
				Handle.DangerousGetHandle (),
				key.Handle.DangerousGetHandle ());
			if (ret <= 0)
				throw ThrowError ();
		}

		public void AddIntermediateCertificate (MonoBtlsX509 x509)
		{
			CheckThrow ();

			var ret = mono_btls_ssl_add_chain_certificate (
				Handle.DangerousGetHandle (),
				x509.Handle.DangerousGetHandle ());
			if (ret <= 0)
				throw ThrowError ();
		}

		public MonoBtlsSslError Accept ()
		{
			CheckThrow ();

			var ret = mono_btls_ssl_accept (Handle.DangerousGetHandle ());

			var error = GetError (ret);
			return error;
		}

		public MonoBtlsSslError Connect ()
		{
			CheckThrow ();

			var ret = mono_btls_ssl_connect (Handle.DangerousGetHandle ());

			var error = GetError (ret);
			return error;
		}

		public MonoBtlsSslError Handshake ()
		{
			CheckThrow ();

			var ret = mono_btls_ssl_handshake (Handle.DangerousGetHandle ());

			var error = GetError (ret);
			return error;
		}

		delegate int PrintErrorsCallbackFunc (IntPtr str, IntPtr len, IntPtr ctx);

#if MONOTOUCH
		[MonoPInvokeCallback (typeof (PrintErrorsCallbackFunc))]
#endif
		static int PrintErrorsCallback (IntPtr str, IntPtr len, IntPtr ctx)
		{
			var sb = (StringBuilder)GCHandle.FromIntPtr (ctx).Target;
			try {
				var text = Marshal.PtrToStringAnsi (str, (int)len);
				sb.Append (text);
				return 1;
			} catch {
				return 0;
			}
		}

		public string GetErrors ()
		{
			var text = new StringBuilder ();
			var handle = GCHandle.Alloc (text);

			try {
				mono_btls_ssl_print_errors_cb (printErrorsFuncPtr, GCHandle.ToIntPtr (handle));
				return text.ToString ();
			} finally {
				if (handle.IsAllocated)
					handle.Free ();
			}
		}

		public void PrintErrors ()
		{
			var errors = GetErrors ();
			if (string.IsNullOrEmpty (errors))
				return;
			Console.Error.WriteLine (errors);
		}

		public MonoBtlsSslError Read (IntPtr data, ref int dataSize)
		{
			CheckThrow ();
			var ret = mono_btls_ssl_read (
				Handle.DangerousGetHandle (), data, dataSize);

			if (ret >= 0) {
				dataSize = ret;
				return MonoBtlsSslError.None;
			}

			var error = mono_btls_ssl_get_error (
				Handle.DangerousGetHandle (), ret);
			dataSize = 0;
			return (MonoBtlsSslError)error;
		}

		public MonoBtlsSslError Write (IntPtr data, ref int dataSize)
		{
			CheckThrow ();
			var ret = mono_btls_ssl_write (
				Handle.DangerousGetHandle (), data, dataSize);

			if (ret >= 0) {
				dataSize = ret;
				return MonoBtlsSslError.None;
			}

			var error = mono_btls_ssl_get_error (
				Handle.DangerousGetHandle (), ret);
			dataSize = 0;
			return (MonoBtlsSslError)error;
		}

		public int GetVersion ()
		{
			CheckThrow ();
			return mono_btls_ssl_get_version (Handle.DangerousGetHandle ());
		}

		public void SetMinVersion (int version)
		{
			CheckThrow ();
			mono_btls_ssl_set_min_version (Handle.DangerousGetHandle (), version);
		}

		public void SetMaxVersion (int version)
		{
			CheckThrow ();
			mono_btls_ssl_set_max_version (Handle.DangerousGetHandle (), version);
		}

		public int GetCipher ()
		{
			CheckThrow ();
			var cipher = mono_btls_ssl_get_cipher (Handle.DangerousGetHandle ());
			CheckError (cipher > 0);
			return cipher;
		}

		public short[] GetCiphers ()
		{
			CheckThrow ();
			IntPtr data;
			var count = mono_btls_ssl_get_ciphers (
				Handle.DangerousGetHandle (), out data);
			CheckError (count > 0);
			try {
				short[] ciphers = new short[count];
				Marshal.Copy (data, ciphers, 0, count);
				return ciphers;
			} finally {
				FreeDataPtr (data);
			}
		}

		public void SetCipherList (string str)
		{
			CheckThrow ();
			IntPtr strPtr = IntPtr.Zero;
			try {
				strPtr = Marshal.StringToHGlobalAnsi (str);
				var ret = mono_btls_ssl_set_cipher_list (
					Handle.DangerousGetHandle (), strPtr);
				CheckError (ret);
			} finally {
				if (strPtr != IntPtr.Zero)
					Marshal.FreeHGlobal (strPtr);
			}
		}

		public MonoBtlsX509 GetPeerCertificate ()
		{
			CheckThrow ();
			var x509 = mono_btls_ssl_get_peer_certificate (
				Handle.DangerousGetHandle ());
			if (x509 == IntPtr.Zero)
				return null;
			return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (x509));
		}

		public void SetVerifyParam (MonoBtlsX509VerifyParam param)
		{
			CheckThrow ();
			var ret = mono_btls_ssl_set_verify_param (
				Handle.DangerousGetHandle (),
				param.Handle.DangerousGetHandle ());
			CheckError (ret);
		}

		public void SetServerName (string name)
		{
			CheckThrow ();
			IntPtr namePtr = IntPtr.Zero;
			try {
				namePtr = Marshal.StringToHGlobalAnsi (name);
				var ret = mono_btls_ssl_set_server_name (
					Handle.DangerousGetHandle (), namePtr);
				CheckError (ret);
			} finally {
				if (namePtr != IntPtr.Zero)
					Marshal.FreeHGlobal (namePtr);
			}
		}

		protected override void Close ()
		{
			mono_btls_ssl_close (Handle.DangerousGetHandle ());
		}
	}
}
#endif