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 GetIssuerName (bool legacyV1Mode);
50 public abstract string GetSubjectName (bool legacyV1Mode);
52 public abstract byte[] GetRawCertData ();
54 public abstract DateTime GetValidFrom ();
56 public abstract DateTime GetValidUntil ();
58 byte[] cachedCertificateHash;
60 public byte[] GetCertHash ()
62 ThrowIfContextInvalid ();
63 if (cachedCertificateHash == null)
64 cachedCertificateHash = GetCertHash (false);
65 return cachedCertificateHash;
68 protected abstract byte[] GetCertHash (bool lazy);
70 public override int GetHashCode ()
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]);
84 public abstract bool Equals (X509CertificateImpl other, out bool result);
86 public abstract string GetKeyAlgorithm ();
88 public abstract byte[] GetKeyAlgorithmParameters ();
90 public abstract byte[] GetPublicKey ();
92 public abstract byte[] GetSerialNumber ();
94 public abstract byte[] Export (X509ContentType contentType, byte[] password);
96 public abstract string ToString (bool full);
98 public override bool Equals (object obj)
100 var other = obj as X509CertificateImpl;
104 if (!IsValid || !other.IsValid)
108 if (Equals (other, out result))
111 var ourRaw = GetRawCertData ();
112 var theirRaw = other.GetRawCertData ();
115 return theirRaw == null;
116 else if (theirRaw == null)
119 if (ourRaw.Length != theirRaw.Length)
122 for (int i = 0; i < ourRaw.Length; i++) {
123 if (ourRaw [i] != theirRaw [i])
130 public void Dispose ()
133 GC.SuppressFinalize (this);
136 protected virtual void Dispose (bool disposing)
138 cachedCertificateHash = null;
141 ~X509CertificateImpl ()