2 // X509Helpers.cs: X.509 helper and utility functions.
5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (C) 2015 Xamarin, Inc. (http://www.xamarin.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
28 namespace System.Security.Cryptography.X509Certificates
30 internal abstract class X509CertificateImpl : IDisposable
32 public abstract bool IsValid {
36 public abstract IntPtr Handle {
40 protected void ThrowIfContextInvalid ()
43 throw X509Helper.GetInvalidContextException ();
46 public abstract X509CertificateImpl Clone ();
48 public abstract string GetSubjectSummary ();
50 public abstract string GetIssuerName (bool legacyV1Mode);
52 public abstract string GetSubjectName (bool legacyV1Mode);
54 public abstract byte[] GetRawCertData ();
56 public abstract DateTime GetEffectiveDateString ();
58 public abstract DateTime GetExpirationDateString ();
60 byte[] cachedCertificateHash;
62 public byte[] GetCertHash ()
64 ThrowIfContextInvalid ();
65 if (cachedCertificateHash == null)
66 cachedCertificateHash = GetCertHash (false);
67 return cachedCertificateHash;
70 protected abstract byte[] GetCertHash (bool lazy);
72 public override int GetHashCode ()
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]);
86 public abstract bool Equals (X509CertificateImpl other, out bool result);
88 public abstract string GetKeyAlgorithm ();
90 public abstract byte[] GetKeyAlgorithmParameters ();
92 public abstract byte[] GetPublicKey ();
94 public abstract byte[] GetSerialNumber ();
96 public abstract byte[] Export (X509ContentType contentType, byte[] password);
98 public abstract string ToString (bool full);
100 public override bool Equals (object obj)
102 var other = obj as X509CertificateImpl;
106 if (!IsValid || !other.IsValid)
110 if (Equals (other, out result))
113 var ourRaw = GetRawCertData ();
114 var theirRaw = other.GetRawCertData ();
117 return theirRaw == null;
118 else if (theirRaw == null)
121 if (ourRaw.Length != theirRaw.Length)
124 for (int i = 0; i < ourRaw.Length; i++) {
125 if (ourRaw [i] != theirRaw [i])
132 public void Dispose ()
135 GC.SuppressFinalize (this);
138 protected virtual void Dispose (bool disposing)
140 cachedCertificateHash = null;
143 ~X509CertificateImpl ()