コード例 #1
0
        public static BooleanReason ImportUserCertificateFromBuffer(byte[] buffer, SecureString password, string friendlyName)
        {
            var info = new NativeMethods.CryptAttributeBlob();
            var temporaryStore = IntPtr.Zero;
            var context = IntPtr.Zero;
            var passwordPointer = IntPtr.Zero;
            try
            {
                if (buffer == null)
                    return new BooleanReason(false, "The provided certificate buffer is empty.");

                info.data = Marshal.AllocHGlobal(buffer.Length);
                info.dataSize = buffer.Length;

                if (password != null)
                    passwordPointer = Marshal.SecureStringToGlobalAllocUnicode(password);

                Marshal.Copy(buffer, 0, info.data, buffer.Length);

                temporaryStore = NativeMethods.PFXImportCertStore(ref info, passwordPointer, NativeMethods.UserKeyset);
                if (temporaryStore == IntPtr.Zero)
                {
                    var issue = new Win32Exception(Marshal.GetLastWin32Error());
                    return new BooleanReason(false, "Could not import cert data: " + issue.Message);
                }

                context = GetFirstUsableContextFromStore(temporaryStore);
                if (context == IntPtr.Zero)
                    return new BooleanReason(false, "Could not import cert data: no useable certificate in store");

                AssignFriendlyNameToContext(context, friendlyName);

                var importInfo = new NativeMethods.ContextImportInfo
                    {
                        SubjectChoice = NativeMethods.ImportFromContext,
                        context = context,
                        Password = passwordPointer
                    };
                importInfo.Size = Marshal.SizeOf(importInfo);

                var result = NativeMethods.CryptUIWizImport(NativeMethods.NoUserInterface | NativeMethods.ImportToCurrentuser, IntPtr.Zero, IntPtr.Zero, ref importInfo, IntPtr.Zero);
                if (!result)
                {
                    var issue = new Win32Exception(Marshal.GetLastWin32Error());
                    return new BooleanReason(false, issue.Message);
                }

                return new BooleanReason(true, "");
            }
            finally
            {
                if (context != IntPtr.Zero)
                    NativeMethods.CertFreeCertificateContext(context);

                if (info.data != IntPtr.Zero)
                    Marshal.FreeHGlobal(info.data);

                if (temporaryStore != IntPtr.Zero)
                    NativeMethods.CertCloseStore(temporaryStore, 0);

                if (passwordPointer != IntPtr.Zero)
                    Marshal.ZeroFreeGlobalAllocUnicode(passwordPointer);
            }
        }
コード例 #2
0
        public static BooleanReason ImportCertificateFromBuffer(IntPtr certHandle, IntPtr storeHandle,
                                                                string friendlyName)
        {
            try
            {
                AssignFriendlyNameToContext(certHandle, friendlyName);

                var importInfo = new NativeMethods.ContextImportInfo
                    {
                        SubjectChoice = NativeMethods.ImportFromContext,
                        context = certHandle,
                    };
                importInfo.Size = Marshal.SizeOf(importInfo);
                var result = NativeMethods.CryptUIWizImport(NativeMethods.NoUserInterface, IntPtr.Zero, IntPtr.Zero,
                                                            ref importInfo, storeHandle);
                if (!result)
                {
                    var issue = new Win32Exception(Marshal.GetLastWin32Error());
                    return new BooleanReason(false, issue.Message);
                }

                return new BooleanReason(true, "");
            }
            catch (Exception e)
            {
                return new BooleanReason(false, e.Message);
            }
        }