005ffd03eaba6f5a4b0b9dd907b98ea268b7adf5
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsX509NameList.cs
1 //
2 // MonoBtlsX509NameList.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 && MONO_FEATURE_BTLS
27 using System;
28 using System.IO;
29 using System.Text;
30 using System.Runtime.InteropServices;
31 using System.Runtime.CompilerServices;
32
33 namespace Mono.Btls
34 {
35         class MonoBtlsX509NameList : MonoBtlsObject
36         {
37                 internal class BoringX509NameListHandle : MonoBtlsHandle
38                 {
39                         bool dontFree;
40
41                         internal BoringX509NameListHandle (IntPtr handle, bool ownsHandle)
42                                 : base (handle, ownsHandle)
43                         {
44                                 this.dontFree = !ownsHandle;
45                         }
46
47                         protected override bool ReleaseHandle ()
48                         {
49                                 if (!dontFree)
50                                         mono_btls_x509_name_list_free (handle);
51                                 return true;
52                         }
53                 }
54
55                 [DllImport (BTLS_DYLIB)]
56                 extern static IntPtr mono_btls_x509_name_list_new ();
57
58                 [DllImport (BTLS_DYLIB)]
59                 extern static int mono_btls_x509_name_list_get_count (IntPtr handle);
60
61                 [DllImport (BTLS_DYLIB)]
62                 extern static int mono_btls_x509_name_list_add (IntPtr handle, IntPtr name);
63
64                 [DllImport (BTLS_DYLIB)]
65                 extern static IntPtr mono_btls_x509_name_list_get_item (IntPtr handle, int index);
66
67                 [DllImport (BTLS_DYLIB)]
68                 extern static void mono_btls_x509_name_list_free (IntPtr handle);
69
70                 new internal BoringX509NameListHandle Handle {
71                         get { return (BoringX509NameListHandle)base.Handle; }
72                 }
73
74                 internal MonoBtlsX509NameList (BoringX509NameListHandle handle)
75                         : base (handle)
76                 {
77                 }
78
79                 internal MonoBtlsX509NameList ()
80                         : this (Create_internal ())
81                 {
82                 }
83
84                 static BoringX509NameListHandle Create_internal ()
85                 {
86                         var handle = mono_btls_x509_name_list_new ();
87                         if (handle == IntPtr.Zero)
88                                 throw new MonoBtlsException ();
89                         return new BoringX509NameListHandle (handle, true);
90                 }
91
92                 public int GetCount ()
93                 {
94                         CheckThrow ();
95                         return mono_btls_x509_name_list_get_count (
96                                 Handle.DangerousGetHandle ());
97                 }
98
99                 public MonoBtlsX509Name GetItem (int index)
100                 {
101                         CheckThrow ();
102                         if (index < 0 || index >= GetCount ())
103                                 throw new ArgumentOutOfRangeException ();
104                         var ptr = mono_btls_x509_name_list_get_item (
105                                 Handle.DangerousGetHandle (), index);
106                         if (ptr == IntPtr.Zero)
107                                 return null;
108                         return new MonoBtlsX509Name (
109                                 new MonoBtlsX509Name.BoringX509NameHandle (ptr, true));
110                 }
111
112                 public void Add (MonoBtlsX509Name name)
113                 {
114                         CheckThrow ();
115                         mono_btls_x509_name_list_add (
116                                 Handle.DangerousGetHandle (),
117                                 name.Handle.DangerousGetHandle ());
118                 }
119         }
120 }
121 #endif