public X509Certificate2 CreateSelfSignedCertificate(SelfSignedCertProperties properties)
        {
            this.ThrowIfDisposedOrNotOpen();
            this.GenerateKeyExchangeKey(properties.IsPrivateKeyExportable, properties.KeyBitLength);
            byte[]   rawData  = properties.Name.RawData;
            GCHandle gCHandle = GCHandle.Alloc(rawData, GCHandleType.Pinned);

            Win32Native.CryptKeyProviderInformation keyProviderInfo = new Win32Native.CryptKeyProviderInformation
            {
                ContainerName = this.ContainerName,
                KeySpec       = 1,
                ProviderType  = (int)ProviderTypes.PROV_RSA_FULL
            };

            IntPtr intPtr = Win32Native.CertCreateSelfSignCertificate(this.handle, new Win32Native.CryptoApiBlob(rawData.Length, gCHandle.AddrOfPinnedObject()), 0, keyProviderInfo, IntPtr.Zero, this.ToSystemTime(properties.ValidFrom), this.ToSystemTime(properties.ValidTo), IntPtr.Zero);

            gCHandle.Free();

            if (IntPtr.Zero == intPtr)
            {
                Win32ErrorHelper.ThrowExceptionIfGetLastErrorIsNotZero();
            }

            X509Certificate2 result = new X509Certificate2(intPtr);

            if (!Win32Native.CertFreeCertificateContext(intPtr))
            {
                Win32ErrorHelper.ThrowExceptionIfGetLastErrorIsNotZero();
            }
            return(result);
        }
 internal static extern IntPtr CertCreateSelfSignCertificate(IntPtr providerHandle, [In] Win32Native.CryptoApiBlob subjectIssuerBlob, int flags, [In] Win32Native.CryptKeyProviderInformation keyProviderInfo, IntPtr signatureAlgorithm, [In] Win32Native.SystemTime startTime, [In] Win32Native.SystemTime endTime, IntPtr extensions);