9b8a64f67b909252fbe22af5ea5b98a49c9b7481
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsX509Crl.cs
1 //
2 // MonoBtlsX509Crl.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.Text;
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;
35
36 namespace Mono.Btls
37 {
38         class MonoBtlsX509Crl : MonoBtlsObject
39         {
40                 internal class BoringX509CrlHandle : MonoBtlsHandle
41                 {
42                         public BoringX509CrlHandle (IntPtr handle)
43                                 : base (handle, true)
44                         {
45                         }
46
47                         protected override bool ReleaseHandle ()
48                         {
49                                 if (handle != IntPtr.Zero)
50                                         mono_btls_x509_crl_free (handle);
51                                 return true;
52                         }
53
54                         public IntPtr StealHandle ()
55                         {
56                                 var retval = Interlocked.Exchange (ref handle, IntPtr.Zero);
57                                 return retval;
58                         }
59                 }
60
61                 new internal BoringX509CrlHandle Handle {
62                         get { return (BoringX509CrlHandle)base.Handle; }
63                 }
64
65                 internal MonoBtlsX509Crl (BoringX509CrlHandle handle) 
66                         : base (handle)
67                 {
68                 }
69
70                 [MethodImpl (MethodImplOptions.InternalCall)]
71                 extern static IntPtr mono_btls_x509_crl_ref (IntPtr handle);
72
73                 [MethodImpl (MethodImplOptions.InternalCall)]
74                 extern static void mono_btls_x509_crl_test (IntPtr handle);
75
76                 [MethodImpl (MethodImplOptions.InternalCall)]
77                 extern static IntPtr mono_btls_x509_crl_from_data (IntPtr data, int len, MonoBtlsX509Format format);
78
79                 [MethodImpl (MethodImplOptions.InternalCall)]
80                 extern static IntPtr mono_btls_x509_crl_get_by_cert (IntPtr handle, IntPtr x509);
81
82                 [MethodImpl (MethodImplOptions.InternalCall)]
83                 unsafe extern static IntPtr mono_btls_x509_crl_get_by_serial (IntPtr handle, void *serial, int len);
84
85                 [MethodImpl (MethodImplOptions.InternalCall)]
86                 extern static int mono_btls_x509_crl_get_revoked_count (IntPtr handle);
87
88                 [MethodImpl (MethodImplOptions.InternalCall)]
89                 extern static IntPtr mono_btls_x509_crl_get_revoked (IntPtr handle, int index);
90
91                 [MethodImpl (MethodImplOptions.InternalCall)]
92                 extern static long mono_btls_x509_crl_get_last_update (IntPtr handle);
93
94                 [MethodImpl (MethodImplOptions.InternalCall)]
95                 extern static long mono_btls_x509_crl_get_next_update (IntPtr handle);
96
97                 [MethodImpl (MethodImplOptions.InternalCall)]
98                 extern static long mono_btls_x509_crl_get_version (IntPtr handle);
99
100                 [MethodImpl (MethodImplOptions.InternalCall)]
101                 extern static IntPtr mono_btls_x509_crl_get_issuer (IntPtr handle);
102
103                 [MethodImpl (MethodImplOptions.InternalCall)]
104                 extern static void mono_btls_x509_crl_free (IntPtr handle);
105
106                 public static MonoBtlsX509Crl LoadFromData (byte[] buffer, MonoBtlsX509Format format)
107                 {
108                         var data = Marshal.AllocHGlobal (buffer.Length);
109                         if (data == IntPtr.Zero)
110                                 throw new OutOfMemoryException ();
111
112                         try {
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.");
117
118                                 return new MonoBtlsX509Crl (new BoringX509CrlHandle (crl));
119                         } finally {
120                                 Marshal.FreeHGlobal (data);
121                         }
122                 }
123
124                 public MonoBtlsX509Revoked GetByCert (MonoBtlsX509 x509)
125                 {
126                         var revoked = mono_btls_x509_crl_get_by_cert (
127                                 Handle.DangerousGetHandle (),
128                                 x509.Handle.DangerousGetHandle ());
129                         if (revoked == IntPtr.Zero)
130                                 return null;
131                         return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
132                 }
133
134                 public unsafe MonoBtlsX509Revoked GetBySerial (byte[] serial)
135                 {
136                         fixed (void *ptr = serial)
137                         {
138                                 var revoked = mono_btls_x509_crl_get_by_serial (
139                                         Handle.DangerousGetHandle (), ptr, serial.Length);
140                                 if (revoked == IntPtr.Zero)
141                                         return null;
142                                 return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
143                         }
144                 }
145
146                 public int GetRevokedCount ()
147                 {
148                         return mono_btls_x509_crl_get_revoked_count (Handle.DangerousGetHandle ());
149                 }
150
151                 public MonoBtlsX509Revoked GetRevoked (int index)
152                 {
153                         if (index >= GetRevokedCount ())
154                                 throw new ArgumentOutOfRangeException ();
155
156                         var revoked = mono_btls_x509_crl_get_revoked (
157                                 Handle.DangerousGetHandle (), index);
158                         if (revoked == IntPtr.Zero)
159                                 return null;
160                         return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
161                 }
162
163                 public DateTime GetLastUpdate ()
164                 {
165                         var ticks = mono_btls_x509_crl_get_last_update (Handle.DangerousGetHandle ());
166                         return new DateTime (1970, 1, 1).AddSeconds (ticks);
167                 }
168
169                 public DateTime GetNextUpdate ()
170                 {
171                         var ticks = mono_btls_x509_crl_get_next_update (Handle.DangerousGetHandle ());
172                         return new DateTime (1970, 1, 1).AddSeconds (ticks);
173                 }
174
175                 public long GetVersion ()
176                 {
177                         return mono_btls_x509_crl_get_version (Handle.DangerousGetHandle ());
178                 }
179
180                 public MonoBtlsX509Name GetIssuerName ()
181                 {
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));
185                 }
186         }
187 }
188 #endif