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 IntPtr mono_btls_x509_crl_from_data (IntPtr data, int len, MonoBtlsX509Format format);
76 [MethodImpl (MethodImplOptions.InternalCall)]
77 extern static IntPtr mono_btls_x509_crl_get_by_cert (IntPtr handle, IntPtr x509);
79 [MethodImpl (MethodImplOptions.InternalCall)]
80 unsafe extern static IntPtr mono_btls_x509_crl_get_by_serial (IntPtr handle, void *serial, int len);
82 [MethodImpl (MethodImplOptions.InternalCall)]
83 extern static int mono_btls_x509_crl_get_revoked_count (IntPtr handle);
85 [MethodImpl (MethodImplOptions.InternalCall)]
86 extern static IntPtr mono_btls_x509_crl_get_revoked (IntPtr handle, int index);
88 [MethodImpl (MethodImplOptions.InternalCall)]
89 extern static long mono_btls_x509_crl_get_last_update (IntPtr handle);
91 [MethodImpl (MethodImplOptions.InternalCall)]
92 extern static long mono_btls_x509_crl_get_next_update (IntPtr handle);
94 [MethodImpl (MethodImplOptions.InternalCall)]
95 extern static long mono_btls_x509_crl_get_version (IntPtr handle);
97 [MethodImpl (MethodImplOptions.InternalCall)]
98 extern static IntPtr mono_btls_x509_crl_get_issuer (IntPtr handle);
100 [MethodImpl (MethodImplOptions.InternalCall)]
101 extern static void mono_btls_x509_crl_free (IntPtr handle);
103 public static MonoBtlsX509Crl LoadFromData (byte[] buffer, MonoBtlsX509Format format)
105 var data = Marshal.AllocHGlobal (buffer.Length);
106 if (data == IntPtr.Zero)
107 throw new OutOfMemoryException ();
110 Marshal.Copy (buffer, 0, data, buffer.Length);
111 var crl = mono_btls_x509_crl_from_data (data, buffer.Length, format);
112 if (crl == IntPtr.Zero)
113 throw new MonoBtlsException ("Failed to read CRL from data.");
115 return new MonoBtlsX509Crl (new BoringX509CrlHandle (crl));
117 Marshal.FreeHGlobal (data);
121 public MonoBtlsX509Revoked GetByCert (MonoBtlsX509 x509)
123 var revoked = mono_btls_x509_crl_get_by_cert (
124 Handle.DangerousGetHandle (),
125 x509.Handle.DangerousGetHandle ());
126 if (revoked == IntPtr.Zero)
128 return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
131 public unsafe MonoBtlsX509Revoked GetBySerial (byte[] serial)
133 fixed (void *ptr = serial)
135 var revoked = mono_btls_x509_crl_get_by_serial (
136 Handle.DangerousGetHandle (), ptr, serial.Length);
137 if (revoked == IntPtr.Zero)
139 return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
143 public int GetRevokedCount ()
145 return mono_btls_x509_crl_get_revoked_count (Handle.DangerousGetHandle ());
148 public MonoBtlsX509Revoked GetRevoked (int index)
150 if (index >= GetRevokedCount ())
151 throw new ArgumentOutOfRangeException ();
153 var revoked = mono_btls_x509_crl_get_revoked (
154 Handle.DangerousGetHandle (), index);
155 if (revoked == IntPtr.Zero)
157 return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
160 public DateTime GetLastUpdate ()
162 var ticks = mono_btls_x509_crl_get_last_update (Handle.DangerousGetHandle ());
163 return new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds (ticks);
166 public DateTime GetNextUpdate ()
168 var ticks = mono_btls_x509_crl_get_next_update (Handle.DangerousGetHandle ());
169 return new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds (ticks);
172 public long GetVersion ()
174 return mono_btls_x509_crl_get_version (Handle.DangerousGetHandle ());
177 public MonoBtlsX509Name GetIssuerName ()
179 var handle = mono_btls_x509_crl_get_issuer (Handle.DangerousGetHandle ());
180 CheckError (handle != IntPtr.Zero);
181 return new MonoBtlsX509Name (new MonoBtlsX509Name.BoringX509NameHandle (handle, false));