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