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
30 using System.Threading;
31 using System.Runtime.CompilerServices;
32 using System.Runtime.InteropServices;
33 using System.Security.Cryptography.X509Certificates;
34 using System.Security.Cryptography;
38 class MonoBtlsX509Crl : MonoBtlsObject
40 internal class BoringX509CrlHandle : MonoBtlsHandle
42 public BoringX509CrlHandle (IntPtr handle)
47 protected override bool ReleaseHandle ()
49 if (handle != IntPtr.Zero)
50 mono_btls_x509_crl_free (handle);
54 public IntPtr StealHandle ()
56 var retval = Interlocked.Exchange (ref handle, IntPtr.Zero);
61 new internal BoringX509CrlHandle Handle {
62 get { return (BoringX509CrlHandle)base.Handle; }
65 internal MonoBtlsX509Crl (BoringX509CrlHandle handle)
70 [MethodImpl (MethodImplOptions.InternalCall)]
71 extern static IntPtr mono_btls_x509_crl_ref (IntPtr handle);
73 [MethodImpl (MethodImplOptions.InternalCall)]
74 extern static void mono_btls_x509_crl_test (IntPtr handle);
76 [MethodImpl (MethodImplOptions.InternalCall)]
77 extern static IntPtr mono_btls_x509_crl_from_data (IntPtr data, int len, MonoBtlsX509Format format);
79 [MethodImpl (MethodImplOptions.InternalCall)]
80 extern static IntPtr mono_btls_x509_crl_get_by_cert (IntPtr handle, IntPtr x509);
82 [MethodImpl (MethodImplOptions.InternalCall)]
83 unsafe extern static IntPtr mono_btls_x509_crl_get_by_serial (IntPtr handle, void *serial, int len);
85 [MethodImpl (MethodImplOptions.InternalCall)]
86 extern static int mono_btls_x509_crl_get_revoked_count (IntPtr handle);
88 [MethodImpl (MethodImplOptions.InternalCall)]
89 extern static IntPtr mono_btls_x509_crl_get_revoked (IntPtr handle, int index);
91 [MethodImpl (MethodImplOptions.InternalCall)]
92 extern static long mono_btls_x509_crl_get_last_update (IntPtr handle);
94 [MethodImpl (MethodImplOptions.InternalCall)]
95 extern static long mono_btls_x509_crl_get_next_update (IntPtr handle);
97 [MethodImpl (MethodImplOptions.InternalCall)]
98 extern static long mono_btls_x509_crl_get_version (IntPtr handle);
100 [MethodImpl (MethodImplOptions.InternalCall)]
101 extern static IntPtr mono_btls_x509_crl_get_issuer (IntPtr handle);
103 [MethodImpl (MethodImplOptions.InternalCall)]
104 extern static void mono_btls_x509_crl_free (IntPtr handle);
106 public static MonoBtlsX509Crl LoadFromData (byte[] buffer, MonoBtlsX509Format format)
108 var data = Marshal.AllocHGlobal (buffer.Length);
109 if (data == IntPtr.Zero)
110 throw new OutOfMemoryException ();
113 Marshal.Copy (buffer, 0, data, buffer.Length);
114 var crl = mono_btls_x509_crl_from_data (data, buffer.Length, format);
115 if (crl == IntPtr.Zero)
116 throw new MonoBtlsException ("Failed to read CRL from data.");
118 return new MonoBtlsX509Crl (new BoringX509CrlHandle (crl));
120 Marshal.FreeHGlobal (data);
124 public MonoBtlsX509Revoked GetByCert (MonoBtlsX509 x509)
126 var revoked = mono_btls_x509_crl_get_by_cert (
127 Handle.DangerousGetHandle (),
128 x509.Handle.DangerousGetHandle ());
129 if (revoked == IntPtr.Zero)
131 return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
134 public unsafe MonoBtlsX509Revoked GetBySerial (byte[] serial)
136 fixed (void *ptr = serial)
138 var revoked = mono_btls_x509_crl_get_by_serial (
139 Handle.DangerousGetHandle (), ptr, serial.Length);
140 if (revoked == IntPtr.Zero)
142 return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
146 public int GetRevokedCount ()
148 return mono_btls_x509_crl_get_revoked_count (Handle.DangerousGetHandle ());
151 public MonoBtlsX509Revoked GetRevoked (int index)
153 if (index >= GetRevokedCount ())
154 throw new ArgumentOutOfRangeException ();
156 var revoked = mono_btls_x509_crl_get_revoked (
157 Handle.DangerousGetHandle (), index);
158 if (revoked == IntPtr.Zero)
160 return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
163 public DateTime GetLastUpdate ()
165 var ticks = mono_btls_x509_crl_get_last_update (Handle.DangerousGetHandle ());
166 return new DateTime (1970, 1, 1).AddSeconds (ticks);
169 public DateTime GetNextUpdate ()
171 var ticks = mono_btls_x509_crl_get_next_update (Handle.DangerousGetHandle ());
172 return new DateTime (1970, 1, 1).AddSeconds (ticks);
175 public long GetVersion ()
177 return mono_btls_x509_crl_get_version (Handle.DangerousGetHandle ());
180 public MonoBtlsX509Name GetIssuerName ()
182 var handle = mono_btls_x509_crl_get_issuer (Handle.DangerousGetHandle ());
183 CheckError (handle != IntPtr.Zero);
184 return new MonoBtlsX509Name (new MonoBtlsX509Name.BoringX509NameHandle (handle, false));