Ejemplo n.º 1
0
        public IEnumerable<CertDTO> GetStoreEntries (VecsAdaptor.CertEntryType entryType)
        {
            var lst = new List<CertDTO> ();
            UInt32 certCount = 0;
            IntPtr ptrCerts = IntPtr.Zero;
            var hEnumContext = new IntPtr ();

            try {
                var result = VecsAdaptor.VecsGetEntryCount (StoreHandle, out certCount);
                VecsError.Check (result);

           
                result = VecsAdaptor.VecsBeginEnumEntries (
                    _hStore,
                    certCount,
                    VecsAdaptor.EntryInfoLevel.Level2,
                    out hEnumContext);
                VecsError.Check (result);

                if (hEnumContext != null) {
                    result = VecsAdaptor.VecsEnumEntriesA (
                        hEnumContext,
                        out ptrCerts,
                        out certCount);
                    VecsError.Check (result);

                    int sz = Marshal.SizeOf (typeof(VecsAdaptor.VECS_CERT_ENTRY));
                    var certArray = new VecsAdaptor.VECS_CERT_ENTRY[certCount];

                    for (UInt32 i = 0; i < certCount; i++) {
                        certArray [i] =
                            (VecsAdaptor.VECS_CERT_ENTRY)Marshal.PtrToStructure (
                            new IntPtr (ptrCerts.ToInt64 () + (sz * i)),
                            typeof(VecsAdaptor.VECS_CERT_ENTRY));
                        var certString = Marshal.PtrToStringAnsi (
                                             certArray [i].pszCertificate);
                        var aliasString = Marshal.PtrToStringAnsi (
                                              certArray [i].pszAlias);
                        var passwordString = Marshal.PtrToStringAnsi (
                                                 certArray [i].pszPassword);

                        //if(!string.IsNullOrEmpty(aliasString))
                        //    File.WriteAllText("c:\\temp\\" + aliasString, certString);

                        if (certArray [i].entryType != (int)entryType) {
                            continue;
                        }
                        var dto = new CertDTO {
                            Alias = aliasString != null ? aliasString : ""
                        };
                        lst.Add (dto);
                        try {
                            if (!string.IsNullOrEmpty (certString)) {
                                dto.Cert = certString.GetX509Certificate2FromString ();
                            }
                            if (!string.IsNullOrEmpty (passwordString)) {
                                dto.Password = passwordString;
                            }
                        } catch (Exception) {
                        }
                    }
                }
            } catch (Exception e) {
                throw e;
            } finally {
                if (hEnumContext != IntPtr.Zero) {
                    VecsAdaptor.VecsEndEnumEntries (hEnumContext);
                }
                if (ptrCerts != IntPtr.Zero) {
                    VecsAdaptor.VecsFreeCertEntryArrayA (ptrCerts, certCount);
                }
            }

            return lst;
        }
Ejemplo n.º 2
0
        public IEnumerable <CertDTO> GetStoreEntries(VecsAdaptor.CertEntryType entryType)
        {
            var    lst          = new List <CertDTO> ();
            UInt32 certCount    = 0;
            IntPtr ptrCerts     = IntPtr.Zero;
            var    hEnumContext = new IntPtr();

            try {
                var result = VecsAdaptor.VecsGetEntryCount(StoreHandle, out certCount);
                VecsError.Check(result);


                result = VecsAdaptor.VecsBeginEnumEntries(
                    _hStore,
                    certCount,
                    VecsAdaptor.EntryInfoLevel.Level2,
                    out hEnumContext);
                VecsError.Check(result);

                if (hEnumContext != null)
                {
                    result = VecsAdaptor.VecsEnumEntriesA(
                        hEnumContext,
                        out ptrCerts,
                        out certCount);
                    VecsError.Check(result);

                    int sz        = Marshal.SizeOf(typeof(VecsAdaptor.VECS_CERT_ENTRY));
                    var certArray = new VecsAdaptor.VECS_CERT_ENTRY[certCount];

                    for (UInt32 i = 0; i < certCount; i++)
                    {
                        certArray [i] =
                            (VecsAdaptor.VECS_CERT_ENTRY)Marshal.PtrToStructure(
                                new IntPtr(ptrCerts.ToInt64() + (sz * i)),
                                typeof(VecsAdaptor.VECS_CERT_ENTRY));
                        var certString = Marshal.PtrToStringAnsi(
                            certArray [i].pszCertificate);
                        var aliasString = Marshal.PtrToStringAnsi(
                            certArray [i].pszAlias);
                        var passwordString = Marshal.PtrToStringAnsi(
                            certArray [i].pszPassword);

                        //if(!string.IsNullOrEmpty(aliasString))
                        //    File.WriteAllText("c:\\temp\\" + aliasString, certString);

                        if (certArray [i].entryType != (int)entryType)
                        {
                            continue;
                        }
                        var dto = new CertDTO {
                            Alias = aliasString != null ? aliasString : ""
                        };
                        lst.Add(dto);
                        try {
                            if (!string.IsNullOrEmpty(certString))
                            {
                                dto.Cert = certString.GetX509Certificate2FromString();
                            }
                            if (!string.IsNullOrEmpty(passwordString))
                            {
                                dto.Password = passwordString;
                            }
                        } catch (Exception) {
                        }
                    }
                }
            } catch (Exception e) {
                throw e;
            } finally {
                if (hEnumContext != IntPtr.Zero)
                {
                    VecsAdaptor.VecsEndEnumEntries(hEnumContext);
                }
                if (ptrCerts != IntPtr.Zero)
                {
                    VecsAdaptor.VecsFreeCertEntryArrayA(ptrCerts, certCount);
                }
            }

            return(lst);
        }