Ejemplo n.º 1
0
        public static bool IsValidated(out string errorMessage)
        {
            Company currentCompany = ((EInvoiceContext)FXContext.Current).CurrentCompany;

            errorMessage = null;
            try
            {
                ICertificateService _certSrv  = IoC.Resolve <ICertificateService>();
                KeyStores           _keyStore = KeyStoresManagement.GetKeyStore(currentCompany.id);
                if (_keyStore == null)
                {
                    errorMessage = "Chưa đăng ký chữ ký số, liên hệ nhà cung cấp để được hỗ trợ.";
                    return(false);
                }
                if (_keyStore.KeyStoresOf == 1)
                {
                    return(true);
                }
                System.Security.Cryptography.X509Certificates.X509Certificate2 cert = _keyStore.OpenSession();
                if (DateTime.Parse(cert.GetExpirationDateString()) <= DateTime.Today.AddDays(7))
                {
                    errorMessage = "Chữ ký số sắp hết hạn, liên hệ nhà cung cấp để được hỗ trợ.";
                    return(false);
                }
                return(true);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return(true);
            }
        }
 public static void ClassCleanup()
 {
     File.Delete(Path.Combine(@"C:\Users\John\BcPGP\", "testuser1@example_com_secret.asc"));
     using (KeyStoreDB keyDB = new KeyStoreDB()) {
         KeyUsers  user = keyDB.KeyUsers.Where(u => u.Email == "*****@*****.**").FirstOrDefault();
         KeyStores key  = keyDB.KeyStores.Find(user.KeyStoreID);
         keyDB.KeyUsers.Remove(user);
         keyDB.KeyStores.Remove(key);
         keyDB.SaveChanges();
     }
 }
Ejemplo n.º 3
0
 public ActionResult LaunchChoiceByPlugin(string cbid, string Pattern, string Serial, string certificate)
 {
     try
     {
         string[] idsStr = cbid.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
         int[]    ids    = (from s in idsStr select Convert.ToInt32(s)).ToArray();
         if (ids.Length < 0)
         {
             return(Json("ERROR:1"));
         }
         Company          currentCompany = ((EInvoiceContext)FXContext.Current).CurrentCompany;
         byte[]           raw            = Convert.FromBase64String(certificate);
         X509Certificate2 x509Cert       = new X509Certificate2(raw);
         KeyStores        _keyStore      = KeyStoresManagement.GetKeyStore(currentCompany.id);
         if (_keyStore == null)
         {
             log.Error("keyStores invalid");
             return(Json("ERROR:2"));
         }
         string serial4Keystore = _keyStore.SerialCert.TrimEnd().ToUpper();
         string serial4X509Cert = x509Cert.GetSerialNumberString().TrimEnd().ToUpper();
         if (serial4Keystore.CompareTo(serial4X509Cert) != 0)
         {
             log.Error("1|" + _keyStore.SerialCert.ToUpper());
             log.Error("2|" + x509Cert.GetSerialNumberString().ToUpper());
             return(Json("ERROR:2"));
         }
         if (DateTime.Parse(x509Cert.GetExpirationDateString()) < DateTime.Today)
         {
             return(Json("ERROR:3"));
         }
         IInvoiceService              IInvSrv       = InvServiceFactory.GetService(Pattern, currentCompany.id);
         IInvoice[]                   invoicesByIds = IInvSrv.GetByID(currentCompany.id, ids).OrderBy(p => p.id).ToArray();
         IList <InvoiceForPlugin>     data          = new List <InvoiceForPlugin>();
         IDictionary <string, string> dictHash      = EInvoice.Core.Launching.Launcher.Instance.GetDigestForRemote(Pattern, Serial, invoicesByIds, x509Cert);
         foreach (KeyValuePair <string, string> pair in dictHash)
         {
             data.Add(new InvoiceForPlugin()
             {
                 Key = pair.Key, Hash = pair.Value
             });
         }
         IInvSrv.UnbindSession();
         return(Json(new { hashdata = data }));
     }
     catch (Exception ex)
     {
         log.Error(ex);
         return(Json("ERROR:4"));
     }
 }
Ejemplo n.º 4
0
        private void OwnerTrust_Load(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            KeyStores key = Global.keyDb.KeyStores.Find(m_keyId);

            m_key         = key;
            txtName.Text  = key.KeyUsers.First().UserName;
            txtEmail.Text = key.KeyUsers.First().Email;
            StringBuilder buildFingerprint = new StringBuilder();

            for (int x = 0; x < key.Fingerprint.Length; x++)
            {
                buildFingerprint.Append(string.Format("{0} ", key.Fingerprint[x].ToString("X2")));
            }
            txtFingerprint.Text = buildFingerprint.ToString().TrimEnd();
            m_trust             = key.OwnerTrust;
            switch (m_trust)
            {
            case "ultimate":
                chkUltimateTrust.Checked = true;
                break;

            case "full":
                rdoFullTrust.Checked = true;
                break;

            case "marginal":
                rdoMinimalTrust.Checked = true;
                break;

            case "unknown":
                rdoNoTrust.Checked = true;
                break;

            case "never":
                rdoKnownProblem.Checked = true;
                break;

            default:
                if (m_keyType == "Public")
                {
                    rdoNoTrust.Checked = true;
                }
                break;
            }
            Cursor.Current = Cursors.Default;
        }
Ejemplo n.º 5
0
        private void ChangePassphrase_Load(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            KeyStores key = Global.keyDb.KeyStores.Find(m_keyId);

            txtName.Text  = key.KeyUsers.First().UserName;
            txtEmail.Text = key.KeyUsers.First().Email;
            m_Trust       = key.OwnerTrust;
            StringBuilder buildFingerprint = new StringBuilder();

            for (int x = 0; x < key.Fingerprint.Length; x++)
            {
                buildFingerprint.Append(string.Format("{0} ", key.Fingerprint[x].ToString("X2")));
            }
            txtFingerprint.Text = buildFingerprint.ToString().TrimEnd();
            Cursor.Current      = Cursors.Default;
        }
Ejemplo n.º 6
0
 public IHttpActionResult SetKeystores(KeystoresInfo data)
 {
     try
     {
         string            Error       = "";
         Company           currentComp = ((EInvoiceContext)FX.Context.FXContext.Current).CurrentCompany;
         IKeyStoresService keystoreSrv = IoC.Resolve <IKeyStoresService>();
         KeyStores         keyStore    = keystoreSrv.GetKeyStoreByComID(currentComp.id).FirstOrDefault();
         if (keyStore == null)
         {
             keyStore = new KeyStores();
         }
         keyStore.ComID      = currentComp.id;
         keyStore.SerialCert = data.CertSerial;
         keyStore.Password   = data.PassWord;
         keyStore.Type       = 4;
         X509Certificate2 x509Cert = new X509Certificate2(Convert.FromBase64String(data.CertData));
         Certificate      cert     = new Certificate();
         cert.ComID      = currentComp.id;
         cert.Cert       = data.CertData;
         cert.SerialCert = data.CertSerial;
         cert.Used       = true;
         cert.ValidFrom  = x509Cert.NotBefore;
         cert.ValidTo    = x509Cert.NotAfter;
         if (keyStore.Id > 0)
         {
             if (keystoreSrv.UpdateKeyStore(keyStore, cert, out Error))
             {
                 return(Ok("OK"));
             }
         }
         else
         if (keystoreSrv.CreateKeyStore(keyStore, cert, out Error))
         {
             return(Ok("OK"));
         }
         return(Ok("ERROR:1"));
     }
     catch (Exception ex)
     {
         return(NotFound());
     }
 }
Ejemplo n.º 7
0
        public ActionResult ViewKeyStores()
        {
            IKeyStoresService   _ikeySer        = IoC.Resolve <IKeyStoresService>();
            Company             _currentCompany = ((EInvoiceContext)FXContext.Current).CurrentCompany;
            ICertificateService _cerSVC         = IoC.Resolve <ICertificateService>();

            try
            {
                KeyStores oKeyStores = _ikeySer.Query.Where(a => a.ComID == _currentCompany.id).SingleOrDefault();
                if (oKeyStores == null)
                {
                    Messages.AddErrorFlashMessage("Chưa đăng ký chữ ký số, liên hệ nhà cung cấp để được hỗ trợ.");
                    return(RedirectToAction("Index", "Home"));
                }
                if (oKeyStores.Type == 2 || oKeyStores.KeyStoresOf == 1)
                {
                    Certificate cer = new Certificate();
                    cer.id             = oKeyStores.Id;
                    cer.ComID          = oKeyStores.ComID;
                    cer.SerialCert     = oKeyStores.SerialCert;
                    cer.Cert           = "";
                    cer.OrganizationCA = "";
                    cer.OwnCA          = "";
                    cer.ValidFrom      = new DateTime();
                    cer.ValidTo        = new DateTime();
                    return(View(cer));
                }
                Certificate cert = (from c in _cerSVC.Query where c.ComID == _currentCompany.id && oKeyStores.SerialCert == c.SerialCert select c).SingleOrDefault();
                if (cert == null)
                {
                    Messages.AddErrorFlashMessage("Bạn vẫn chưa cấu hình keyStore");
                    return(RedirectToAction("Index", "Home"));
                }
                return(View(cert));
            }
            catch (Exception ex)
            {
                Messages.AddErrorFlashMessage("Bạn vẫn chưa cấu hình keyStore");
                log.Error(" ViewKeyStores  -" + ex.Message);
                return(RedirectToAction("Index", "Home"));
            }
        }
Ejemplo n.º 8
0
        public ActionResult LaunchReplaceByPlugin(string invData, string NewPattern, string NewSerial, int OriNo, string OriPattern, string OriSerial, string CertBase64String)
        {
            try
            {
                Company _currentCompany = ((EInvoiceContext)FXContext.Current).CurrentCompany;
                int     comID           = _currentCompany.id;
                if (_currentCompany == null)
                {
                    return(Json(new { error = "ERR:2" }));
                }
                byte[]           raw       = Convert.FromBase64String(CertBase64String);
                X509Certificate2 x509Cert  = new X509Certificate2(raw);
                KeyStores        _keyStore = KeyStoresManagement.GetKeyStore(_currentCompany.id);
                if (_keyStore.SerialCert.ToUpper().CompareTo(x509Cert.GetSerialNumberString().ToUpper()) != 0)
                {
                    return(Json("ERROR:2"));
                }
                if (DateTime.Parse(x509Cert.GetExpirationDateString()) < DateTime.Today)
                {
                    return(Json("ERROR:3"));
                }

                IInvoiceService      invSrv   = InvServiceFactory.GetService(OriPattern, comID);
                IInvoice             OriInv   = invSrv.GetByNo(comID, OriPattern, OriSerial, OriNo);
                IRegisterTempService regisSrv = IoC.Resolve <IRegisterTempService>();
                ICustomerService     _cusSvr  = IoC.Resolve <ICustomerService>();

                IInvoice inv = InvServiceFactory.NewInstance(NewPattern, comID);
                inv.DeserializeFromJSON(invData);
                inv.Pattern = NewPattern;
                inv.Serial  = NewSerial;
                inv.Status  = InvoiceStatus.NewInv;
                DateTime dt = OriInv.ArisingDate;

                string strReplace = (_currentCompany.Config.ContainsKey("ReplaceString")) ? _currentCompany.Config["ReplaceString"] : "Hóa đơn này thay thế hóa đơn số: @InvNo, Ký hiệu: @InvSerial, Gửi ngày @InvDay Tháng @InvMonth Năm @InvYear";
                strReplace         = strReplace.Replace("@InvNo", OriInv.No.ToString(Utils.MAX_NO_LENGTH)).Replace("@InvSerial", OriInv.Serial).Replace("@InvPattern", OriInv.Pattern).Replace("@InvDay", OriInv.ArisingDate.Day.ToString()).Replace("@InvMonth", OriInv.ArisingDate.Month.ToString()).Replace("@InvYear", OriInv.ArisingDate.Year.ToString());
                inv.ProcessInvNote = strReplace;
                inv.Type           = InvoiceType.ForReplace;
                var Typecus = (from c in _cusSvr.Query where c.ComID == comID && c.Code == inv.CusCode && c.CusType == 1 select c.CusType).SingleOrDefault();
                if (Typecus == 0)
                {
                    inv.CusSignStatus = cusSignStatus.NocusSignStatus;
                }
                else
                {
                    inv.CusSignStatus = cusSignStatus.NoSignStatus;
                }
                if (OriInv.Status == InvoiceStatus.SignedInv || OriInv.Status == InvoiceStatus.AdjustedInv)
                {
                    IDictionary <string, string> dict = EInvoice.Core.Launching.Launcher.Instance.GetDigestForRemote(OriPattern, OriSerial, new IInvoice[] { inv }, x509Cert);
                    if (dict.Count == 0)
                    {
                        return(Json("ERROR:4"));
                    }
                    var dictFirst = dict.FirstOrDefault();
                    invSrv.UnbindSession();
                    return(Json(new { hashdata = dictFirst }));
                }

                return(Json("ERROR:4"));
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return(Json("ERROR:4"));
            }
        }