/// <summary> /// Clean up the contained managed classes that need disposing. /// </summary> /// <param name="disposing">true if called from Dispose, false if from the finalizer</param> /// <remarks> /// This class does not need a finalizer as the managed classes that wrap that native OS /// resources for certificates have finalizers. Howerver, there may be a need for a subclass to /// introduce a finalizer, so Dispose is properly implemented. /// </remarks> protected virtual void Dispose(bool disposing) { if (disposing) { if (_nativeCert != null) { _nativeCert.Dispose(); } if (_key != null) { _key.Dispose(); } if (_keyContainer != null) { _keyContainer.Dispose(); _keyContainer = null; } } _nativeCert = null; _key = null; }
/// <summary> /// Creates a certificate /// </summary> /// <param name="appID"></param> /// <param name="numberOfYears"></param> /// private void CreateCert(Guid appID, short numberOfYears) { // convert the times to SystemTime structures NativeMethods.SystemTime beginTime = new NativeMethods.SystemTime(DateTime.Now); NativeMethods.SystemTime expireTime = new NativeMethods.SystemTime(DateTime.Now); expireTime.wYear += numberOfYears; // convert the name into a X500 name CertificateName certName = new CertificateName(MakeCertSubject(appID)); GenerateKeys(appID); // create the certificate using (CryptoApiBlob nameBlob = certName.GetCryptoApiBlob()) { _nativeCert = NativeMethods.CertCreateSelfSignCertificate(_keyContainer, nameBlob, NativeMethods.SelfSignFlags.None, IntPtr.Zero, IntPtr.Zero, ref beginTime, ref expireTime, IntPtr.Zero); if (_nativeCert.IsInvalid) { _nativeCert.Dispose(); _nativeCert = null; throw new CryptographicException(String.Format( CultureInfo.InvariantCulture, ResourceRetriever.GetResourceString( "ApplicationCertificateUnableToCreateCert"), Util.GetLastErrorMessage())); } else { // okay to use DangerousGetHandle here as handle is valid and // used for creation of the certificate only. No reference is added // _certificate = new X509Certificate2(_nativeCert.DangerousGetHandle()); } } }
internal extern static bool CertAddCertificateContextToStore( CertificateStoreHandle hCertStore, CertificateHandle pCertContext, AddDisposition dwAddDisposition, [Out]out CertificateHandle ppStoreContext);