Merge pull request #3716 from vargaz/unbox-stobj-null
[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 IntPtr mono_btls_x509_crl_from_data (IntPtr data, int len, MonoBtlsX509Format format);
75
76                 [MethodImpl (MethodImplOptions.InternalCall)]
77                 extern static IntPtr mono_btls_x509_crl_get_by_cert (IntPtr handle, IntPtr x509);
78
79                 [MethodImpl (MethodImplOptions.InternalCall)]
80                 unsafe extern static IntPtr mono_btls_x509_crl_get_by_serial (IntPtr handle, void *serial, int len);
81
82                 [MethodImpl (MethodImplOptions.InternalCall)]
83                 extern static int mono_btls_x509_crl_get_revoked_count (IntPtr handle);
84
85                 [MethodImpl (MethodImplOptions.InternalCall)]
86                 extern static IntPtr mono_btls_x509_crl_get_revoked (IntPtr handle, int index);
87
88                 [MethodImpl (MethodImplOptions.InternalCall)]
89                 extern static long mono_btls_x509_crl_get_last_update (IntPtr handle);
90
91                 [MethodImpl (MethodImplOptions.InternalCall)]
92                 extern static long mono_btls_x509_crl_get_next_update (IntPtr handle);
93
94                 [MethodImpl (MethodImplOptions.InternalCall)]
95                 extern static long mono_btls_x509_crl_get_version (IntPtr handle);
96
97                 [MethodImpl (MethodImplOptions.InternalCall)]
98                 extern static IntPtr mono_btls_x509_crl_get_issuer (IntPtr handle);
99
100                 [MethodImpl (MethodImplOptions.InternalCall)]
101                 extern static void mono_btls_x509_crl_free (IntPtr handle);
102
103                 public static MonoBtlsX509Crl LoadFromData (byte[] buffer, MonoBtlsX509Format format)
104                 {
105                         var data = Marshal.AllocHGlobal (buffer.Length);
106                         if (data == IntPtr.Zero)
107                                 throw new OutOfMemoryException ();
108
109                         try {
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.");
114
115                                 return new MonoBtlsX509Crl (new BoringX509CrlHandle (crl));
116                         } finally {
117                                 Marshal.FreeHGlobal (data);
118                         }
119                 }
120
121                 public MonoBtlsX509Revoked GetByCert (MonoBtlsX509 x509)
122                 {
123                         var revoked = mono_btls_x509_crl_get_by_cert (
124                                 Handle.DangerousGetHandle (),
125                                 x509.Handle.DangerousGetHandle ());
126                         if (revoked == IntPtr.Zero)
127                                 return null;
128                         return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
129                 }
130
131                 public unsafe MonoBtlsX509Revoked GetBySerial (byte[] serial)
132                 {
133                         fixed (void *ptr = serial)
134                         {
135                                 var revoked = mono_btls_x509_crl_get_by_serial (
136                                         Handle.DangerousGetHandle (), ptr, serial.Length);
137                                 if (revoked == IntPtr.Zero)
138                                         return null;
139                                 return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
140                         }
141                 }
142
143                 public int GetRevokedCount ()
144                 {
145                         return mono_btls_x509_crl_get_revoked_count (Handle.DangerousGetHandle ());
146                 }
147
148                 public MonoBtlsX509Revoked GetRevoked (int index)
149                 {
150                         if (index >= GetRevokedCount ())
151                                 throw new ArgumentOutOfRangeException ();
152
153                         var revoked = mono_btls_x509_crl_get_revoked (
154                                 Handle.DangerousGetHandle (), index);
155                         if (revoked == IntPtr.Zero)
156                                 return null;
157                         return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
158                 }
159
160                 public DateTime GetLastUpdate ()
161                 {
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);
164                 }
165
166                 public DateTime GetNextUpdate ()
167                 {
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);
170                 }
171
172                 public long GetVersion ()
173                 {
174                         return mono_btls_x509_crl_get_version (Handle.DangerousGetHandle ());
175                 }
176
177                 public MonoBtlsX509Name GetIssuerName ()
178                 {
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));
182                 }
183         }
184 }
185 #endif