18 private const string m_format =
"X509";
20 private string m_subjectName;
22 private string m_issuerName;
24 private byte[] m_serialNumber;
26 private byte[] m_publicKeyParameters;
28 private byte[] m_publicKeyValue;
30 private string m_publicKeyOid;
32 private byte[] m_rawData;
34 private byte[] m_thumbprint;
41 private SafeCertContextHandle m_safeCertContext;
43 private bool m_certContextCloned;
54 return m_safeCertContext.pCertContext;
63 [SecuritySafeCritical]
66 ThrowIfContextInvalid();
67 if (m_issuerName ==
null)
69 m_issuerName = X509Utils._GetIssuerName(m_safeCertContext, legacyV1Mode:
false);
80 [SecuritySafeCritical]
83 ThrowIfContextInvalid();
84 if (m_subjectName ==
null)
86 m_subjectName = X509Utils._GetSubjectInfo(m_safeCertContext, 2u, legacyV1Mode:
false);
92 internal SafeCertContextHandle CertContext
97 return m_safeCertContext;
103 [SecuritySafeCritical]
106 ThrowIfContextInvalid();
109 Win32Native.FILE_TIME fileTime =
default(Win32Native.FILE_TIME);
110 X509Utils._GetDateNotAfter(m_safeCertContext, ref fileTime);
119 [SecuritySafeCritical]
122 ThrowIfContextInvalid();
123 if (m_notBefore ==
DateTime.MinValue)
125 Win32Native.FILE_TIME fileTime =
default(Win32Native.FILE_TIME);
126 X509Utils._GetDateNotBefore(m_safeCertContext, ref fileTime);
127 m_notBefore =
DateTime.FromFileTime(fileTime.ToTicks());
133 private byte[] RawData
138 ThrowIfContextInvalid();
139 if (m_rawData ==
null)
141 m_rawData = X509Utils._GetCertRawData(m_safeCertContext);
143 return (
byte[])m_rawData.Clone();
147 private string SerialNumber
149 [SecuritySafeCritical]
152 ThrowIfContextInvalid();
153 if (m_serialNumber ==
null)
155 m_serialNumber = X509Utils._GetSerialNumber(m_safeCertContext);
157 return Hex.EncodeHexStringFromInt(m_serialNumber);
161 [SecuritySafeCritical]
164 m_safeCertContext = SafeCertContextHandle.InvalidHandle;
180 if (data !=
null && data.Length != 0)
217 LoadCertificateFromBlob(rawData, password, keyStorageFlags);
229 LoadCertificateFromBlob(rawData, password, keyStorageFlags);
236 [SecuritySafeCritical]
248 [SecuritySafeCritical]
260 [SecuritySafeCritical]
273 [SecuritySafeCritical]
277 LoadCertificateFromFile(fileName, password, keyStorageFlags);
286 [SecuritySafeCritical]
290 LoadCertificateFromFile(fileName, password, keyStorageFlags);
306 X509Utils._DuplicateCertContext(handle, ref m_safeCertContext);
313 [SecuritySafeCritical]
321 if (cert.m_safeCertContext.pCertContext !=
IntPtr.
Zero)
323 m_safeCertContext = cert.GetCertContextForCloning();
324 m_certContextCloned =
true;
335 byte[] array = (
byte[])info.GetValue(
"RawData", typeof(
byte[]));
362 [SecuritySafeCritical]
363 [Obsolete(
"This method has been deprecated. Please use the Subject property instead. http://go.microsoft.com/fwlink/?linkid=14202")]
366 ThrowIfContextInvalid();
367 return X509Utils._GetSubjectInfo(m_safeCertContext, 2u, legacyV1Mode:
true);
373 [SecuritySafeCritical]
374 [Obsolete(
"This method has been deprecated. Please use the Issuer property instead. http://go.microsoft.com/fwlink/?linkid=14202")]
377 ThrowIfContextInvalid();
378 return X509Utils._GetIssuerName(m_safeCertContext, legacyV1Mode:
true);
384 [SecuritySafeCritical]
387 ThrowIfContextInvalid();
388 if (m_serialNumber ==
null)
390 m_serialNumber = X509Utils._GetSerialNumber(m_safeCertContext);
392 return (
byte[])m_serialNumber.Clone();
405 [SecuritySafeCritical]
408 ThrowIfContextInvalid();
409 if (m_publicKeyParameters ==
null)
411 m_publicKeyParameters = X509Utils._GetPublicKeyParameters(m_safeCertContext);
413 return (
byte[])m_publicKeyParameters.Clone();
419 [SecuritySafeCritical]
422 ThrowIfContextInvalid();
429 [SecuritySafeCritical]
432 ThrowIfContextInvalid();
433 if (m_publicKeyOid ==
null)
435 m_publicKeyOid = X509Utils._GetPublicKeyOid(m_safeCertContext);
437 return m_publicKeyOid;
443 [SecuritySafeCritical]
446 ThrowIfContextInvalid();
447 if (m_publicKeyValue ==
null)
449 m_publicKeyValue = X509Utils._GetPublicKeyValue(m_safeCertContext);
451 return (
byte[])m_publicKeyValue.Clone();
463 [SecuritySafeCritical]
481 return (
byte[])m_thumbprint.Clone();
489 return Hex.EncodeHexString(m_thumbprint);
525 [SecuritySafeCritical]
532 if (m_safeCertContext.IsInvalid)
534 return other.m_safeCertContext.IsInvalid;
540 if (!SerialNumber.Equals(other.SerialNumber))
549 [SecuritySafeCritical]
552 if (m_safeCertContext.IsInvalid)
558 for (
int i = 0; i < m_thumbprint.Length && i < 4; i++)
560 num = ((num << 8) | m_thumbprint[i]);
576 [SecuritySafeCritical]
579 if (!fVerbose || m_safeCertContext.IsInvalid)
581 return GetType().FullName;
589 stringBuilder.
Append(SerialNumber);
634 public virtual void Import(
byte[] rawData)
651 LoadCertificateFromBlob(rawData, password, keyStorageFlags);
664 LoadCertificateFromBlob(rawData, password, keyStorageFlags);
673 public virtual void Import(
string fileName)
690 LoadCertificateFromFile(fileName, password, keyStorageFlags);
703 LoadCertificateFromFile(fileName, password, keyStorageFlags);
710 [SecuritySafeCritical]
714 return ExportHelper(contentType,
null);
722 [SecuritySafeCritical]
726 return ExportHelper(contentType, password);
734 [SecuritySafeCritical]
737 return ExportHelper(contentType, password);
746 m_subjectName =
null;
748 m_serialNumber =
null;
749 m_publicKeyParameters =
null;
750 m_publicKeyValue =
null;
751 m_publicKeyOid =
null;
756 if (!m_safeCertContext.IsInvalid)
758 if (!m_certContextCloned)
760 m_safeCertContext.Dispose();
762 m_safeCertContext = SafeCertContextHandle.InvalidHandle;
764 m_certContextCloned =
false;
776 [SecuritySafeCritical]
777 protected virtual void Dispose(
bool disposing)
791 if (m_safeCertContext.IsInvalid)
793 info.AddValue(
"RawData",
null);
797 info.AddValue(
"RawData", RawData);
808 internal SafeCertContextHandle GetCertContextForCloning()
810 m_certContextCloned =
true;
811 return m_safeCertContext;
815 private void ThrowIfContextInvalid()
817 if (m_safeCertContext.IsInvalid)
819 throw new CryptographicException(Environment.GetResourceString(
"Cryptography_InvalidHandle"),
"m_safeCertContext");
823 [SecuritySafeCritical]
824 private void SetThumbprint()
826 ThrowIfContextInvalid();
827 if (m_thumbprint ==
null)
829 m_thumbprint = X509Utils._GetThumbprint(m_safeCertContext);
834 private byte[] ExportHelper(
X509ContentType contentType,
object password)
841 keyContainerPermission.
Demand();
845 throw new CryptographicException(Environment.GetResourceString(
"Cryptography_X509_InvalidContentType"));
850 IntPtr intPtr = IntPtr.Zero;
852 SafeCertStoreHandle safeCertStoreHandle = X509Utils.ExportCertToMemoryStore(
this);
856 intPtr = X509Utils.PasswordToHGlobalUni(password);
857 array = X509Utils._ExportCertificatesToBlob(safeCertStoreHandle, contentType, intPtr);
861 if (intPtr != IntPtr.Zero)
865 safeCertStoreHandle.Dispose();
869 throw new CryptographicException(Environment.GetResourceString(
"Cryptography_X509_ExportFailed"));
874 [SecuritySafeCritical]
875 private void LoadCertificateFromBlob(
byte[] rawData,
object password,
X509KeyStorageFlags keyStorageFlags)
877 if (rawData ==
null || rawData.Length == 0)
879 throw new ArgumentException(Environment.GetResourceString(
"Arg_EmptyOrNullArray"),
"rawData");
881 X509ContentType x509ContentType = X509Utils.MapContentType(X509Utils._QueryCertBlobType(rawData));
885 keyContainerPermission.
Demand();
887 uint dwFlags = X509Utils.MapKeyStorageFlags(keyStorageFlags);
888 IntPtr intPtr = IntPtr.Zero;
892 intPtr = X509Utils.PasswordToHGlobalUni(password);
893 X509Utils._LoadCertFromBlob(rawData, intPtr, dwFlags, ((keyStorageFlags &
X509KeyStorageFlags.PersistKeySet) != 0) ?
true :
false, ref m_safeCertContext);
897 if (intPtr != IntPtr.Zero)
905 private void LoadCertificateFromFile(
string fileName,
object password,
X509KeyStorageFlags keyStorageFlags)
907 if (fileName ==
null)
909 throw new ArgumentNullException(
"fileName");
911 string fullPathInternal =
Path.GetFullPathInternal(fileName);
913 X509ContentType x509ContentType = X509Utils.MapContentType(X509Utils._QueryCertFileType(fileName));
917 keyContainerPermission.
Demand();
919 uint dwFlags = X509Utils.MapKeyStorageFlags(keyStorageFlags);
920 IntPtr intPtr = IntPtr.Zero;
924 intPtr = X509Utils.PasswordToHGlobalUni(password);
925 X509Utils._LoadCertFromFile(fileName, intPtr, dwFlags, ((keyStorageFlags &
X509KeyStorageFlags.PersistKeySet) != 0) ?
true :
false, ref m_safeCertContext);
929 if (intPtr != IntPtr.Zero)
virtual bool Equals(X509Certificate other)
Compares two T:System.Security.Cryptography.X509Certificates.X509Certificate objects for equality.
virtual object Clone()
Creates a copy of the current T:System.Globalization.CultureInfo.
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
X509Certificate(byte[] rawData, SecureString password)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
virtual string GetSerialNumberString()
Returns the serial number of the X.509v3 certificate as a hexadecimal string.
virtual string GetKeyAlgorithm()
Returns the key algorithm information for this X.509v3 certificate as a string.
static string NewLine
Gets the newline string defined for this environment.
virtual string GetPublicKeyString()
Returns the public key for the X.509v3 certificate as a hexadecimal string.
virtual void Import(byte[] rawData)
Populates the T:System.Security.Cryptography.X509Certificates.X509Certificate object with data from a...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
X509Certificate(byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
FileIOPermissionAccess
Specifies the type of file access requested.
X509Certificate(X509Certificate cert)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
X509Certificate(string fileName)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
static DateTime FromFileTime(long fileTime)
Converts the specified Windows file time to an equivalent local time.
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
virtual void Import(string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
Populates an T:System.Security.Cryptography.X509Certificates.X509Certificate object with information ...
static readonly DateTime MinValue
Represents the smallest possible value of T:System.DateTime. This field is read-only.
virtual void Import(byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
Populates an T:System.Security.Cryptography.X509Certificates.X509Certificate object using data from a...
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Indicates that a class is to be notified when deserialization of the entire object graph has been com...
string Issuer
Gets the name of the certificate authority that issued the X.509v3 certificate.
string Subject
Gets the subject distinguished name from the certificate.
virtual void Import(string fileName, string password, X509KeyStorageFlags keyStorageFlags)
Populates the T:System.Security.Cryptography.X509Certificates.X509Certificate object with information...
X509Certificate(byte[] rawData, string password)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
Represents an instant in time, typically expressed as a date and time of day. To browse the ....
X509ContentType
Specifies the format of an X.509 certificate.
override string ToString()
Returns a string representation of the current T:System.Security.Cryptography.X509Certificates....
Controls the ability to access key containers. This class cannot be inherited.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
X509Certificate(string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
A type representing a date and time value.
virtual string GetName()
Returns the name of the principal to which the certificate was issued.
virtual byte [] GetKeyAlgorithmParameters()
Returns the key algorithm parameters for the X.509v3 certificate as an array of bytes.
X509Certificate()
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
X509Certificate(string fileName, string password, X509KeyStorageFlags keyStorageFlags)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
X509Certificate(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
SecurityAction
Specifies the security actions that can be performed using declarative security.
int Year
Gets the year component of the date represented by this instance.
Provides information about, and means to manipulate, the current environment and platform....
static X509Certificate CreateFromSignedFile(string filename)
Creates an X.509v3 certificate from the specified signed file.
virtual string GetFormat()
Returns the name of the format of this X.509v3 certificate.
virtual string GetCertHashString()
Returns the SHA1 hash value for the X.509v3 certificate as a hexadecimal string.
virtual void Import(string fileName)
Populates the T:System.Security.Cryptography.X509Certificates.X509Certificate object with information...
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
static X509Certificate CreateFromCertFile(string filename)
Creates an X.509v3 certificate from the specified PKCS7 signed file.
Represents a collection that can contain many different types of permissions.
override string ToString()
Converts the value of the current T:System.DateTime object to its equivalent string representation us...
virtual string GetExpirationDateString()
Returns the expiration date of this X.509v3 certificate.
static void ZeroFreeGlobalAllocUnicode(IntPtr s)
Frees an unmanaged string pointer that was allocated using the M:System.Runtime.InteropServices....
void OnDeserialization(object sender)
Runs when the entire object graph has been deserialized.
void Dispose()
Releases all resources used by the current T:System.Security.Cryptography.X509Certificates....
X509Certificate(string fileName, string password)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
X509Certificate(IntPtr handle)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
Represents the Saudi Hijri (Um Al Qura) calendar.
Represents text that should be kept confidential, such as by deleting it from computer memory when no...
virtual string GetIssuerName()
Returns the name of the certification authority that issued the X.509v3 certificate.
virtual byte [] Export(X509ContentType contentType)
Exports the current T:System.Security.Cryptography.X509Certificates.X509Certificate object to a byte ...
A platform-specific type that is used to represent a pointer or a handle.
virtual void Import(byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
Populates the T:System.Security.Cryptography.X509Certificates.X509Certificate object using data from ...
static void PrepareConstrainedRegions()
Designates a body of code as a constrained execution region (CER).
virtual string ToString(bool fVerbose)
Returns a string representation of the current T:System.Security.Cryptography.X509Certificates....
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
KeyContainerPermissionFlags
Specifies the type of key container access allowed.
int Day
Gets the day of the month represented by this instance.
virtual byte [] Export(X509ContentType contentType, string password)
Exports the current T:System.Security.Cryptography.X509Certificates.X509Certificate object to a byte ...
virtual string GetRawCertDataString()
Returns the raw data for the entire X.509v3 certificate as a hexadecimal string.
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
virtual void Reset()
Resets the state of the T:System.Security.Cryptography.X509Certificates.X509Certificate2 object.
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
static string FormatDate(DateTime date)
Converts the specified date and time to a string.
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
Allows an object to control its own serialization and deserialization.
virtual byte [] Export(X509ContentType contentType, SecureString password)
Exports the current T:System.Security.Cryptography.X509Certificates.X509Certificate object to a byte ...
int Month
Gets the month component of the date represented by this instance.
virtual byte [] GetRawCertData()
Returns the raw data for the entire X.509v3 certificate as an array of bytes.
override int GetHashCode()
Returns the hash code for the X.509v3 certificate as an integer.
virtual byte [] GetPublicKey()
Returns the public key for the X.509v3 certificate as an array of bytes.
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
virtual DateTimeFormatInfo DateTimeFormat
Gets or sets a T:System.Globalization.DateTimeFormatInfo that defines the culturally appropriate form...
X509KeyStorageFlags
Defines where and how to import the private key of an X.509 certificate.
Specifies that the class can be serialized.
Represents the Hijri calendar.
X509Certificate(byte[] data)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
override bool Equals(object obj)
Compares two T:System.Security.Cryptography.X509Certificates.X509Certificate objects for equality.
virtual byte [] GetCertHash()
Returns the hash value for the X.509v3 certificate as an array of bytes.
Provides information about a specific culture (called a locale for unmanaged code development)....
Controls the ability to access files and folders. This class cannot be inherited.
X509Certificate(string fileName, SecureString password)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
SecurityPermissionFlag
Specifies access flags for the security permission object.
virtual string GetEffectiveDateString()
Returns the effective date of this X.509v3 certificate.
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data needed to serialize the ta...
Performs operations on T:System.String instances that contain file or directory path information....
virtual void Dispose(bool disposing)
Releases all of the unmanaged resources used by this T:System.Security.Cryptography....
Provides methods that help you use X.509 v.3 certificates.
IntPtr Handle
Gets a handle to a Microsoft Cryptographic API certificate context described by an unmanaged PCCERT_C...
virtual string GetKeyAlgorithmParametersString()
Returns the key algorithm parameters for the X.509v3 certificate as a hexadecimal string.
virtual byte [] GetSerialNumber()
Returns the serial number of the X.509v3 certificate as an array of bytes.
X509Certificate(byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
Provides a set of static methods and properties that provide support for compilers....