[btls] Convert BTLS icalls to pinvokes by invoking them using [DllImp… (#3799)
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsX509Chain.cs
1 //
2 // MonoBtlsX509Chain.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
27 using System;
28 using System.IO;
29 using System.Security.Cryptography.X509Certificates;
30 using System.Runtime.CompilerServices;
31 using System.Runtime.InteropServices;
32
33 namespace Mono.Btls
34 {
35         class MonoBtlsX509Chain : MonoBtlsObject
36         {
37                 internal class BoringX509ChainHandle : MonoBtlsHandle
38                 {
39                         public BoringX509ChainHandle (IntPtr handle)
40                                 : base (handle, true)
41                         {
42                         }
43
44                         protected override bool ReleaseHandle ()
45                         {
46                                 mono_btls_x509_chain_free (handle);
47                                 return true;
48                         }
49                 }
50
51                 new internal BoringX509ChainHandle Handle {
52                         get { return (BoringX509ChainHandle)base.Handle; }
53                 }
54
55                 [DllImport (BTLS_DYLIB)]
56                 extern static IntPtr mono_btls_x509_chain_new ();
57
58                 [DllImport (BTLS_DYLIB)]
59                 extern static int mono_btls_x509_chain_get_count (IntPtr handle);
60
61                 [DllImport (BTLS_DYLIB)]
62                 extern static IntPtr mono_btls_x509_chain_get_cert (IntPtr Handle, int index);
63
64                 [DllImport (BTLS_DYLIB)]
65                 extern static int mono_btls_x509_chain_add_cert (IntPtr chain, IntPtr x509);
66
67                 [DllImport (BTLS_DYLIB)]
68                 extern static IntPtr mono_btls_x509_chain_up_ref (IntPtr handle);
69
70                 [DllImport (BTLS_DYLIB)]
71                 extern static void mono_btls_x509_chain_free (IntPtr handle);
72
73                 public MonoBtlsX509Chain ()
74                         : base (new BoringX509ChainHandle (mono_btls_x509_chain_new ()))
75                 {
76                 }
77
78                 internal MonoBtlsX509Chain (BoringX509ChainHandle handle)
79                         : base (handle)
80                 {
81                 }
82
83                 public int Count {
84                         get { return mono_btls_x509_chain_get_count (Handle.DangerousGetHandle ()); }
85                 }
86
87                 public MonoBtlsX509 GetCertificate (int index)
88                 {
89                         if (index >= Count)
90                                 throw new IndexOutOfRangeException ();
91                         var handle = mono_btls_x509_chain_get_cert (
92                                 Handle.DangerousGetHandle (), index);
93                         CheckError (handle != IntPtr.Zero);
94                         return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (handle));
95                 }
96
97                 public void Dump ()
98                 {
99                         Console.Error.WriteLine ("CHAIN: {0:x} {1}", Handle, Count);
100                         for (int i = 0; i < Count; i++) {
101                                 using (var cert = GetCertificate (i)) {
102                                         Console.Error.WriteLine ("  CERT #{0}: {1}", i, cert.GetSubjectNameString ());
103                                 }
104                         }
105                 }
106
107                 public void AddCertificate (MonoBtlsX509 x509)
108                 {
109                         mono_btls_x509_chain_add_cert (
110                                 Handle.DangerousGetHandle (),
111                                 x509.Handle.DangerousGetHandle ());
112                 }
113
114                 internal MonoBtlsX509Chain Copy ()
115                 {
116                         var copy = mono_btls_x509_chain_up_ref (Handle.DangerousGetHandle ());
117                         CheckError (copy != IntPtr.Zero);
118                         return new MonoBtlsX509Chain (new BoringX509ChainHandle (copy));
119                 }
120         }
121 }
122 #endif