Пример #1
0
 public static extern IntPtr CertCreateSelfSignCertificate(
     [In, Optional]  IntPtr phProv,
     [In]                    Wincrypt.CRYPTOAPI_BLOB pSubjectIssuerBlob,
     [In]                    UInt32 flags,
     [In, Optional]  Wincrypt.CRYPT_KEY_PROV_INFO pKeyProvInfo,
     [In, Optional]  IntPtr pSignatureAlgorithm,
     [In, Optional]  Wincrypt.SystemTime pStartTime,
     [In, Optional]  Wincrypt.SystemTime pEndTime,
     [Optional]              Wincrypt.CERT_EXTENSIONS pExtensions
     );
        void postGenerate(X509Certificate2 cert)
        {
            // write key info to cert property
            var keyInfo = new Wincrypt.CRYPT_KEY_PROV_INFO {
                pwszProvName      = PrivateKeyInfo.ProviderName,
                dwProvType        = (UInt32)PrivateKeyInfo.ProviderType,
                pwszContainerName = PrivateKeyInfo.KeyContainerName,
                dwKeySpec         = (UInt32)PrivateKeyInfo.KeySpec
            };

            if (PrivateKeyInfo.MachineContext)
            {
                keyInfo.dwFlags = nCrypt2.NCRYPT_MACHINE_KEY_FLAG;
            }
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(keyInfo));

            Marshal.StructureToPtr(keyInfo, ptr, false);
            Crypt32.CertSetCertificateContextProperty(cert.Handle, X509CertificatePropertyType.ProviderInfo, 0, ptr);
            Marshal.FreeHGlobal(ptr);
            PrivateKeyInfo.Dispose();
            // friendly name
            setFriendlyName(cert);
        }