Exemplo n.º 1
0
        public static void Check(UInt32 resultCode)
        {
            if (resultCode == VecsAdaptor.ERROR_NO_MORE_ITEMS)
            {
                return;
            }
            if (resultCode == VecsAdaptor.SUCCESS)
            {
                return;
            }
            if (resultCode == 183)
            {
                throw new VecsException(resultCode, "Entry already exist!");
            }

            string errorString    = "Unknown Error";
            var    errorStringPtr = new IntPtr();
            UInt32 dwError        = VecsAdaptor.VmAfdGetErrorMsgByCode(resultCode, out errorStringPtr);

            if (dwError == 0)
            {
                errorString = Marshal.PtrToStringAnsi(errorStringPtr);
            }
            throw new VecsException(resultCode, errorString);
        }
Exemplo n.º 2
0
 public void DeleteStore(string storeName)
 {
     ErrorHelper.CatchAndThrow(delegate() {
         var result = VecsAdaptor.VecsDeleteCertStoreHA(ServerContext, storeName);
         VecsError.Check(result);
     });
 }
Exemplo n.º 3
0
 public void Close()
 {
     try {
         VecsAdaptor.VecsCloseCertStore(_hStore);
     } catch (Exception e) {
         throw e;
     }
 }
Exemplo n.º 4
0
 public void DeleteCertificate(string alias)
 {
     ErrorHelper.CatchAndThrow(delegate() {
         var result = VecsAdaptor.VecsDeleteEntryA(
             StoreHandle,
             alias);
         VecsError.Check(result);
     });
 }
Exemplo n.º 5
0
 public IntPtr CreateStore(string serverName, string password)
 {
     try {
         IntPtr storeHandle = IntPtr.Zero;
         var    result      = VecsAdaptor.VecsCreateCertStoreHA(ServerContext, serverName, password, out storeHandle);
         VecsError.Check(result);
         return(storeHandle);
     } catch (Exception e) {
         throw e;
     }
 }
Exemplo n.º 6
0
 public void AddSecretKeyEntry(string alias, string secretKey, string password, X509Certificate2 cert)
 {
     ErrorHelper.CatchAndThrow(delegate() {
         var result = VecsAdaptor.VecsAddEntryA(
             StoreHandle,
             VecsAdaptor.CertEntryType.SecretKey,
             alias,
             null,
             secretKey,
             password,
             false);
         VecsError.Check(result);
     });
 }
Exemplo n.º 7
0
 public void AddCertificateEntry(string alias, string privateKey, string password, string certificate)
 {
     ErrorHelper.CatchAndThrow(delegate() {
         String cert = File.ReadAllText(certificate);
         var result  = VecsAdaptor.VecsAddEntryA(
             StoreHandle,
             VecsAdaptor.CertEntryType.TrustedCert,
             alias,
             cert,
             privateKey,
             password,
             false);
         VecsError.Check(result);
     });
 }
Exemplo n.º 8
0
        public string[] GetStores()
        {
            try {
                string[] storeNamesOut  = null;
                UInt32   numberOfStores = 0;
                IntPtr   storeNames     = IntPtr.Zero;

                var result = VecsAdaptor.VecsEnumCertStoreHA(ServerContext, out storeNames, out numberOfStores);
                VecsError.Check(result);

                MarshalArrayOfStrings(storeNames, (int)numberOfStores, out storeNamesOut);

                return(storeNamesOut);
            } catch (Exception e) {
                throw e;
            }
        }
Exemplo n.º 9
0
 public void AddPrivateKeyEntry(string alias, string privateKey, string password, string certificate)
 {
     ErrorHelper.CatchAndThrow(delegate() {
         String privatekey = File.ReadAllText(privateKey);
         String cert       = File.ReadAllText(certificate);
         if (string.IsNullOrEmpty(password))
         {
             password = null;
         }
         var result = VecsAdaptor.VecsAddEntryA(
             StoreHandle,
             VecsAdaptor.CertEntryType.PrivateKey,
             alias,
             cert,
             privatekey,
             password,
             false);
         VecsError.Check(result);
     });
 }
Exemplo n.º 10
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;
        }
Exemplo n.º 11
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);
        }