Use lower-case names for Windows headers.
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsX509Revoked.cs
1 //
2 // MonoBtlsX509Revoked.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 MonoBtlsX509Revoked : MonoBtlsObject
39         {
40                 internal class BoringX509RevokedHandle : MonoBtlsHandle
41                 {
42                         public BoringX509RevokedHandle (IntPtr handle)
43                                 : base (handle, true)
44                         {
45                         }
46
47                         protected override bool ReleaseHandle ()
48                         {
49                                 if (handle != IntPtr.Zero)
50                                         mono_btls_x509_revoked_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 BoringX509RevokedHandle Handle {
62                         get { return (BoringX509RevokedHandle)base.Handle; }
63                 }
64
65                 internal MonoBtlsX509Revoked (BoringX509RevokedHandle handle)
66                         : base (handle)
67                 {
68                 }
69
70                 [MethodImpl (MethodImplOptions.InternalCall)]
71                 extern static int mono_btls_x509_revoked_get_serial_number (IntPtr handle, IntPtr data, int size);
72
73                 [MethodImpl (MethodImplOptions.InternalCall)]
74                 extern static long mono_btls_x509_revoked_get_revocation_date (IntPtr handle);
75
76                 [MethodImpl (MethodImplOptions.InternalCall)]
77                 extern static int mono_btls_x509_revoked_get_reason (IntPtr handle);
78
79                 [MethodImpl (MethodImplOptions.InternalCall)]
80                 extern static int mono_btls_x509_revoked_get_sequence (IntPtr handle);
81
82                 [MethodImpl (MethodImplOptions.InternalCall)]
83                 extern static void mono_btls_x509_revoked_free (IntPtr handle);
84
85                 public byte[] GetSerialNumber ()
86                 {
87                         int size = 256;
88                         IntPtr data = Marshal.AllocHGlobal (size);
89                         try {
90                                 var ret = mono_btls_x509_revoked_get_serial_number (
91                                         Handle.DangerousGetHandle (), data, size);
92                                 CheckError (ret > 0);
93                                 var buffer = new byte[ret];
94                                 Marshal.Copy (data, buffer, 0, ret);
95                                 return buffer;
96                         } finally {
97                                 if (data != IntPtr.Zero)
98                                         Marshal.FreeHGlobal (data);
99                         }
100                 }
101
102                 public DateTime GetRevocationDate ()
103                 {
104                         var ticks = mono_btls_x509_revoked_get_revocation_date (
105                                 Handle.DangerousGetHandle ());
106                         return new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds (ticks);
107                 }
108
109                 public int GetReason ()
110                 {
111                         return mono_btls_x509_revoked_get_reason (Handle.DangerousGetHandle ());
112                 }
113
114                 public int GetSequence ()
115                 {
116                         return mono_btls_x509_revoked_get_sequence (Handle.DangerousGetHandle ());
117                 }
118         }
119 }
120 #endif