Merge pull request #3622 from rolfbjarne/remove-stray-file
[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                 /*
41                  * This is used in System.dll's OSX509Certificates.cs
42                  */
43                 public abstract IntPtr GetNativeAppleCertificate ();
44
45                 protected void ThrowIfContextInvalid ()
46                 {
47                         if (!IsValid)
48                                 throw X509Helper.GetInvalidContextException ();
49                 }
50
51                 public abstract X509CertificateImpl Clone ();
52
53                 public abstract string GetIssuerName (bool legacyV1Mode);
54
55                 public abstract string GetSubjectName (bool legacyV1Mode);
56
57                 public abstract byte[] GetRawCertData ();
58
59                 public abstract DateTime GetValidFrom ();
60
61                 public abstract DateTime GetValidUntil ();
62
63                 byte[] cachedCertificateHash;
64
65                 public byte[] GetCertHash ()
66                 {
67                         ThrowIfContextInvalid ();
68                         if (cachedCertificateHash == null)
69                                 cachedCertificateHash = GetCertHash (false);
70                         return cachedCertificateHash;
71                 }
72
73                 protected abstract byte[] GetCertHash (bool lazy);
74
75                 public override int GetHashCode ()
76                 {
77                         if (!IsValid)
78                                 return 0;
79                         if (cachedCertificateHash == null)
80                                 cachedCertificateHash = GetCertHash (true);
81                         // return the integer of the first 4 bytes of the cert hash
82                         if ((cachedCertificateHash != null) && (cachedCertificateHash.Length >= 4))
83                                 return ((cachedCertificateHash [0] << 24) | (cachedCertificateHash [1] << 16) |
84                                         (cachedCertificateHash [2] << 8) | cachedCertificateHash [3]);
85                         else
86                                 return 0;
87                 }
88
89                 public abstract bool Equals (X509CertificateImpl other, out bool result);
90
91                 public abstract string GetKeyAlgorithm ();
92
93                 public abstract byte[] GetKeyAlgorithmParameters ();
94
95                 public abstract byte[] GetPublicKey ();
96
97                 public abstract byte[] GetSerialNumber ();
98
99                 public abstract byte[] Export (X509ContentType contentType, byte[] password);
100
101                 public abstract string ToString (bool full);
102
103                 public override bool Equals (object obj)
104                 {
105                         var other = obj as X509CertificateImpl;
106                         if (other == null)
107                                 return false;
108
109                         if (!IsValid || !other.IsValid)
110                                 return false;
111
112                         bool result;
113                         if (Equals (other, out result))
114                                 return result;
115
116                         var ourRaw = GetRawCertData ();
117                         var theirRaw = other.GetRawCertData ();
118
119                         if (ourRaw == null)
120                                 return theirRaw == null;
121                         else if (theirRaw == null)
122                                 return false;
123
124                         if (ourRaw.Length != theirRaw.Length)
125                                 return false;
126
127                         for (int i = 0; i < ourRaw.Length; i++) {
128                                 if (ourRaw [i] != theirRaw [i])
129                                         return false;
130                         }
131
132                         return true;
133                 }
134
135                 public void Dispose ()
136                 {
137                         Dispose (true);
138                         GC.SuppressFinalize (this);
139                 }
140
141                 protected virtual void Dispose (bool disposing)
142                 {
143                         cachedCertificateHash = null;
144                 }
145
146                 ~X509CertificateImpl ()
147                 {
148                         Dispose (false);
149                 }
150         }
151 }