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 {
41 * This is used in System.dll's OSX509Certificates.cs
43 public abstract IntPtr GetNativeAppleCertificate ();
45 protected void ThrowIfContextInvalid ()
48 throw X509Helper.GetInvalidContextException ();
51 public abstract X509CertificateImpl Clone ();
53 public abstract string GetIssuerName (bool legacyV1Mode);
55 public abstract string GetSubjectName (bool legacyV1Mode);
57 public abstract byte[] GetRawCertData ();
59 public abstract DateTime GetValidFrom ();
61 public abstract DateTime GetValidUntil ();
63 byte[] cachedCertificateHash;
65 public byte[] GetCertHash ()
67 ThrowIfContextInvalid ();
68 if (cachedCertificateHash == null)
69 cachedCertificateHash = GetCertHash (false);
70 return cachedCertificateHash;
73 protected abstract byte[] GetCertHash (bool lazy);
75 public override int GetHashCode ()
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]);
89 public abstract bool Equals (X509CertificateImpl other, out bool result);
91 public abstract string GetKeyAlgorithm ();
93 public abstract byte[] GetKeyAlgorithmParameters ();
95 public abstract byte[] GetPublicKey ();
97 public abstract byte[] GetSerialNumber ();
99 public abstract byte[] Export (X509ContentType contentType, byte[] password);
101 public abstract string ToString (bool full);
103 public override bool Equals (object obj)
105 var other = obj as X509CertificateImpl;
109 if (!IsValid || !other.IsValid)
113 if (Equals (other, out result))
116 var ourRaw = GetRawCertData ();
117 var theirRaw = other.GetRawCertData ();
120 return theirRaw == null;
121 else if (theirRaw == null)
124 if (ourRaw.Length != theirRaw.Length)
127 for (int i = 0; i < ourRaw.Length; i++) {
128 if (ourRaw [i] != theirRaw [i])
135 public void Dispose ()
138 GC.SuppressFinalize (this);
141 protected virtual void Dispose (bool disposing)
143 cachedCertificateHash = null;
146 ~X509CertificateImpl ()