2 // MonoBtlsX509Chain.cs
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
29 using System.Security.Cryptography.X509Certificates;
30 using System.Runtime.CompilerServices;
31 using System.Runtime.InteropServices;
35 class MonoBtlsX509Chain : MonoBtlsObject
37 internal class BoringX509ChainHandle : MonoBtlsHandle
39 public BoringX509ChainHandle (IntPtr handle)
44 protected override bool ReleaseHandle ()
46 mono_btls_x509_chain_free (handle);
51 new internal BoringX509ChainHandle Handle {
52 get { return (BoringX509ChainHandle)base.Handle; }
55 [DllImport (BTLS_DYLIB)]
56 extern static IntPtr mono_btls_x509_chain_new ();
58 [DllImport (BTLS_DYLIB)]
59 extern static int mono_btls_x509_chain_get_count (IntPtr handle);
61 [DllImport (BTLS_DYLIB)]
62 extern static IntPtr mono_btls_x509_chain_get_cert (IntPtr Handle, int index);
64 [DllImport (BTLS_DYLIB)]
65 extern static int mono_btls_x509_chain_add_cert (IntPtr chain, IntPtr x509);
67 [DllImport (BTLS_DYLIB)]
68 extern static IntPtr mono_btls_x509_chain_up_ref (IntPtr handle);
70 [DllImport (BTLS_DYLIB)]
71 extern static void mono_btls_x509_chain_free (IntPtr handle);
73 public MonoBtlsX509Chain ()
74 : base (new BoringX509ChainHandle (mono_btls_x509_chain_new ()))
78 internal MonoBtlsX509Chain (BoringX509ChainHandle handle)
84 get { return mono_btls_x509_chain_get_count (Handle.DangerousGetHandle ()); }
87 public MonoBtlsX509 GetCertificate (int index)
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));
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 ());
107 public void AddCertificate (MonoBtlsX509 x509)
109 mono_btls_x509_chain_add_cert (
110 Handle.DangerousGetHandle (),
111 x509.Handle.DangerousGetHandle ());
114 internal MonoBtlsX509Chain Copy ()
116 var copy = mono_btls_x509_chain_up_ref (Handle.DangerousGetHandle ());
117 CheckError (copy != IntPtr.Zero);
118 return new MonoBtlsX509Chain (new BoringX509ChainHandle (copy));