Example #1
0
        /// <summary>
        /// Creates the unsigned certificate
        /// </summary>
        /// <param name="keycontainer">The key-container name</param>
        /// <param name="DN">The x509 name of certificate</param>
        /// <param name="provider">The cryptography provider (MS_DEF_PROV/MS_STRONG_PROV/MS_ENHANCED_PROV)</param>
        /// <param name="KEYSPEC">The key specification (AT_KEYEXCHANGE/AT_SIGNATURE) </param>
        /// <param name="cspflags">The CSP flags (only 0 = 'Current User' is used)</param>
        /// <returns>Pointer to created certificate context</returns>
        /// <exception cref="System.ApplicationException">Error occurred while trying to create certificate. Error is:  +  e.Message</exception>
        internal static IntPtr CreateUnsignedCertCntxt(String keycontainer, String DN, String provider = MS_DEF_PROV, uint KEYSPEC = AT_KEYEXCHANGE, uint cspflags = 0)
        {
            IntPtr hCertCntxt = IntPtr.Zero;
            byte[] encodedName = null;
            uint cbName = 0;

            if (provider != MS_DEF_PROV && provider != MS_STRONG_PROV && provider != MS_ENHANCED_PROV)
            {
                return IntPtr.Zero;
            }

            if (keycontainer == "")
            {
                return IntPtr.Zero;
            }

            if (KEYSPEC != AT_SIGNATURE && KEYSPEC != AT_KEYEXCHANGE)
            {
                return IntPtr.Zero;
            }

            if (cspflags != 0 && cspflags != CRYPT_MACHINE_KEYSET)   //only 0 (Current User) keyset is currently used.
            {
                return IntPtr.Zero;
            }

            if (DN == "")
            {
                return IntPtr.Zero;
            }

            if (UnsafeNativeMethods.CertStrToName(X509_ASN_ENCODING, DN, CERT_X500_NAME_STR, IntPtr.Zero, null, ref cbName, IntPtr.Zero))
            {
                encodedName = new byte[cbName];
                UnsafeNativeMethods.CertStrToName(X509_ASN_ENCODING, DN, CERT_X500_NAME_STR, IntPtr.Zero, encodedName, ref cbName, IntPtr.Zero);
            }

            UnsafeNativeMethods.CERT_NAME_BLOB subjectblob = new UnsafeNativeMethods.CERT_NAME_BLOB();
            subjectblob.pbData = Marshal.AllocHGlobal(encodedName.Length);
            Marshal.Copy(encodedName, 0, subjectblob.pbData, encodedName.Length);
            subjectblob.cbData = encodedName.Length;

            UnsafeNativeMethods.CRYPT_KEY_PROV_INFO pInfo = new UnsafeNativeMethods.CRYPT_KEY_PROV_INFO();
            pInfo.pwszContainerName = keycontainer;
            pInfo.pwszProvName = provider;
            pInfo.dwProvType = PROV_RSA_FULL;
            pInfo.dwFlags = cspflags;
            pInfo.cProvParam = 0;
            pInfo.rgProvParam = IntPtr.Zero;
            pInfo.dwKeySpec = KEYSPEC;

            try
            {
                hCertCntxt = UnsafeNativeMethods.CertCreateSelfSignCertificate(IntPtr.Zero, ref subjectblob, CERT_CREATE_SELFSIGN_NO_SIGN, ref pInfo, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                if (hCertCntxt == IntPtr.Zero)
                {
                    System.ComponentModel.Win32Exception e = new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
                    throw new ApplicationException("Error occurred while trying to create certificate. Error is: " +  e.Message, e);
                }

                return hCertCntxt;
            }
            finally
            {
                Marshal.FreeHGlobal(subjectblob.pbData);
            }
        }
Example #2
0
        /// <summary>
        /// Creates the unsigned certificate
        /// </summary>
        /// <param name="keycontainer">The key-container name</param>
        /// <param name="DN">The x509 name of certificate</param>
        /// <param name="provider">The cryptography provider (MS_DEF_PROV/MS_STRONG_PROV/MS_ENHANCED_PROV)</param>
        /// <param name="KEYSPEC">The key specification (AT_KEYEXCHANGE/AT_SIGNATURE) </param>
        /// <param name="cspflags">The CSP flags (only 0 = 'Current User' is used)</param>
        /// <returns>Pointer to created certificate context</returns>
        /// <exception cref="System.ApplicationException">Error occurred while trying to create certificate. Error is:  +  e.Message</exception>
        internal static IntPtr CreateUnsignedCertCntxt(String keycontainer, String DN, String provider = MS_DEF_PROV, uint KEYSPEC = AT_KEYEXCHANGE, uint cspflags = 0)
        {
            IntPtr hCertCntxt = IntPtr.Zero;

            byte[] encodedName = null;
            uint   cbName      = 0;

            if (provider != MS_DEF_PROV && provider != MS_STRONG_PROV && provider != MS_ENHANCED_PROV)
            {
                return(IntPtr.Zero);
            }

            if (keycontainer == "")
            {
                return(IntPtr.Zero);
            }

            if (KEYSPEC != AT_SIGNATURE && KEYSPEC != AT_KEYEXCHANGE)
            {
                return(IntPtr.Zero);
            }

            if (cspflags != 0 && cspflags != CRYPT_MACHINE_KEYSET)   //only 0 (Current User) keyset is currently used.
            {
                return(IntPtr.Zero);
            }

            if (DN == "")
            {
                return(IntPtr.Zero);
            }


            if (UnsafeNativeMethods.CertStrToName(X509_ASN_ENCODING, DN, CERT_X500_NAME_STR, IntPtr.Zero, null, ref cbName, IntPtr.Zero))
            {
                encodedName = new byte[cbName];
                UnsafeNativeMethods.CertStrToName(X509_ASN_ENCODING, DN, CERT_X500_NAME_STR, IntPtr.Zero, encodedName, ref cbName, IntPtr.Zero);
            }

            UnsafeNativeMethods.CERT_NAME_BLOB subjectblob = new UnsafeNativeMethods.CERT_NAME_BLOB();
            subjectblob.pbData = Marshal.AllocHGlobal(encodedName.Length);
            Marshal.Copy(encodedName, 0, subjectblob.pbData, encodedName.Length);
            subjectblob.cbData = encodedName.Length;

            UnsafeNativeMethods.CRYPT_KEY_PROV_INFO pInfo = new UnsafeNativeMethods.CRYPT_KEY_PROV_INFO();
            pInfo.pwszContainerName = keycontainer;
            pInfo.pwszProvName      = provider;
            pInfo.dwProvType        = PROV_RSA_FULL;
            pInfo.dwFlags           = cspflags;
            pInfo.cProvParam        = 0;
            pInfo.rgProvParam       = IntPtr.Zero;
            pInfo.dwKeySpec         = KEYSPEC;

            try
            {
                hCertCntxt = UnsafeNativeMethods.CertCreateSelfSignCertificate(IntPtr.Zero, ref subjectblob, CERT_CREATE_SELFSIGN_NO_SIGN, ref pInfo, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                if (hCertCntxt == IntPtr.Zero)
                {
                    System.ComponentModel.Win32Exception e = new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
                    throw new ApplicationException("Error occurred while trying to create certificate. Error is: " + e.Message, e);
                }

                return(hCertCntxt);
            }
            finally
            {
                Marshal.FreeHGlobal(subjectblob.pbData);
            }
        }