[sgen] Clear the card table in the finishing pause
[mono.git] / mcs / class / corlib / System.Security.Cryptography.X509Certificates / X509CertificateImpl.cs
1 //
2 // X509Helpers.cs: X.509 helper and utility functions.
3 //
4 // Authors:
5 //      Martin Baulig  <martin.baulig@xamarin.com>
6 //
7 // Copyright (C) 2015 Xamarin, Inc. (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 namespace System.Security.Cryptography.X509Certificates
29 {
30         internal abstract class X509CertificateImpl : IDisposable
31         {
32                 public abstract bool IsValid {
33                         get;
34                 }
35
36                 public abstract IntPtr Handle {
37                         get;
38                 }
39
40                 protected void ThrowIfContextInvalid ()
41                 {
42                         if (!IsValid)
43                                 throw X509Helper.GetInvalidContextException ();
44                 }
45
46                 public abstract X509CertificateImpl Clone ();
47
48                 public abstract string GetSubjectSummary ();
49
50                 public abstract string GetIssuerName (bool legacyV1Mode);
51
52                 public abstract string GetSubjectName (bool legacyV1Mode);
53
54                 public abstract byte[] GetRawCertData ();
55
56                 public abstract DateTime GetEffectiveDateString ();
57
58                 public abstract DateTime GetExpirationDateString ();
59
60                 byte[] cachedCertificateHash;
61
62                 public byte[] GetCertHash ()
63                 {
64                         ThrowIfContextInvalid ();
65                         if (cachedCertificateHash == null)
66                                 cachedCertificateHash = GetCertHash (false);
67                         return cachedCertificateHash;
68                 }
69
70                 protected abstract byte[] GetCertHash (bool lazy);
71
72                 public override int GetHashCode ()
73                 {
74                         if (!IsValid)
75                                 return 0;
76                         if (cachedCertificateHash == null)
77                                 cachedCertificateHash = GetCertHash (true);
78                         // return the integer of the first 4 bytes of the cert hash
79                         if ((cachedCertificateHash != null) && (cachedCertificateHash.Length >= 4))
80                                 return ((cachedCertificateHash [0] << 24) | (cachedCertificateHash [1] << 16) |
81                                         (cachedCertificateHash [2] << 8) | cachedCertificateHash [3]);
82                         else
83                                 return 0;
84                 }
85
86                 public abstract bool Equals (X509CertificateImpl other, out bool result);
87
88                 public abstract string GetKeyAlgorithm ();
89
90                 public abstract byte[] GetKeyAlgorithmParameters ();
91
92                 public abstract byte[] GetPublicKey ();
93
94                 public abstract byte[] GetSerialNumber ();
95
96                 public abstract byte[] Export (X509ContentType contentType, byte[] password);
97
98                 public abstract string ToString (bool full);
99
100                 public override bool Equals (object obj)
101                 {
102                         var other = obj as X509CertificateImpl;
103                         if (other == null)
104                                 return false;
105
106                         if (!IsValid || !other.IsValid)
107                                 return false;
108
109                         bool result;
110                         if (Equals (other, out result))
111                                 return result;
112
113                         var ourRaw = GetRawCertData ();
114                         var theirRaw = other.GetRawCertData ();
115
116                         if (ourRaw == null)
117                                 return theirRaw == null;
118                         else if (theirRaw == null)
119                                 return false;
120
121                         if (ourRaw.Length != theirRaw.Length)
122                                 return false;
123
124                         for (int i = 0; i < ourRaw.Length; i++) {
125                                 if (ourRaw [i] != theirRaw [i])
126                                         return false;
127                         }
128
129                         return true;
130                 }
131
132                 public void Dispose ()
133                 {
134                         Dispose (true);
135                         GC.SuppressFinalize (this);
136                 }
137
138                 protected virtual void Dispose (bool disposing)
139                 {
140                         cachedCertificateHash = null;
141                 }
142
143                 ~X509CertificateImpl ()
144                 {
145                         Dispose (false);
146                 }
147         }
148 }