Example #1
0
        public byte[] ValidateCertificate(string RFC, byte[] byte_cer, byte[] byte_key, string password)
        {
            bool validate2 = false;

            byte[] cert_pfx = null;
            try
            {
                if (null != byte_cer && null != byte_key)
                {
                    if (!CertificateValidation.MakeCert.validateModulus(byte_cer, byte_key, password))
                    {
                        throw new Exception("El certificado no corresponde con la llave");
                    }


                    X509Certificate2 installedCert = new X509Certificate2(byte_cer, password, X509KeyStorageFlags.MachineKeySet);

                    CertificateValidation.BussinessLayer.BLCertificate bl = new CertificateValidation.BussinessLayer.BLCertificate();

                    string PathSAT = ConfigurationManager.AppSettings["pathSATFiles"] + @"files SAT\certificadosSAT\";
                    validate2 = bl.validaCertificadoCA(byte_cer, PathSAT);
                    if (!validate2)
                    {
                        throw new Exception("El certificado no está emitido por una entidad certificadora");
                    }


                    X509KeyUsageExtension keyUsageExtension;
                    string KeyUsages;
                    int    isfiel = 0;

                    for (int i = 0; i <= installedCert.Extensions.Count; i++)
                    {
                        try
                        {
                            keyUsageExtension = installedCert.Extensions[i] as X509KeyUsageExtension;
                            KeyUsages         = keyUsageExtension.KeyUsages.ToString();

                            if (keyUsageExtension.KeyUsages.ToString().IndexOf("KeyAgreement") != -1 || keyUsageExtension.KeyUsages.ToString().IndexOf("DataEncipherment") != -1)
                            {
                                isfiel = 1;
                            }
                        }
                        catch (Exception e)
                        {
                            continue;
                        }
                    }

                    if (1 == isfiel)
                    {
                        if (ConfigurationManager.AppSettings["AcceptFIEL"].ToString() == "true")
                        {
                            // tenemos que validar si lo puede hacer por el hecho de que sea una persona fisica

                            if (RFC.Length != 13)
                            {
                                throw new Exception("Las personas morales no pueden entrar con un CSD");
                            }

                            // BLCFDRevocados validaLCO = new BLCFDRevocados();


                            //// CFDRevocados CFDLCO = validaLCO.spValidaListadoCSD(RFC);

                            // // tenemos que validar si lo puede hacer por el hecho de que no tenga CSD registrado previamente
                            // //if (CFDLCO != null)
                            // //{
                            // //    throw new Exception(MessageExceptions.COD_1488 + MessageExceptions.MSG_1488);

                            // //}


                            // CFDRevocados FIELLCO = validaLCO.ValidaRFCsFIEL(RFC, hexString2Ascii(installedCert.SerialNumber));
                            // // tenemos que validar si lo puede hacer por el hecho de que no tenga CSD registrado previamente
                            // if (FIELLCO == null)
                            // {
                            //     throw new ExceptionFIEL(MessageExceptions.COD_1489_2, MessageExceptions.MSG_1489_2);

                            // }
                        }
                        else
                        {
                            // throw new ExceptionFIEL(MessageExceptions.COD_1471, MessageExceptions.MSG_1471);
                        }
                    }


                    cert_pfx = CertificateValidation.MakeCert.generatePFX(byte_cer, byte_key, password);

                    installedCert = new X509Certificate2(cert_pfx, password, X509KeyStorageFlags.MachineKeySet);
                    if (null != installedCert)
                    {
                        if (!installedCert.Issuer.Contains("O=Servicio de Administración Tributaria"))
                        {
                            throw new Exception("No es un certificado emitido por el SAT");
                        }
                        if ((DateTime.Now > installedCert.NotAfter) || (DateTime.Now < installedCert.NotBefore))
                        {
                            throw new Exception("El certificado no se encuentra dentro de un periodo de validez");
                        }
                        if (!installedCert.HasPrivateKey)
                        {
                            throw new Exception("El certificado no tiene llave privada");
                        }
                        if (!installedCert.Subject.ToUpper().Contains(RFC.ToUpper()))
                        {
                            throw new Exception("El certificado no pertene al contribuyente");
                        }
                    }
                }
                else
                {
                    throw new Exception("Ni idea de que error sea 1");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }

            return(cert_pfx);
        }
Example #2
0
        public byte[] ValidateCertificate(string RFC, byte[] byte_cer, byte[] byte_key, string password)
        {


            bool validate2 = false;
            byte[] cert_pfx = null;
            try
            {
                if (null != byte_cer && null != byte_key)
                {

                    if (!CertificateValidation.MakeCert.validateModulus(byte_cer, byte_key, password))
                    {
                        throw new Exception("El certificado no corresponde con la llave");
                    }


                    X509Certificate2 installedCert = new X509Certificate2(byte_cer, password, X509KeyStorageFlags.MachineKeySet);

                    CertificateValidation.BussinessLayer.BLCertificate bl = new CertificateValidation.BussinessLayer.BLCertificate();

                    string PathSAT = ConfigurationManager.AppSettings["pathSATFiles"] + @"files SAT\certificadosSAT\";
                    validate2 = bl.validaCertificadoCA(byte_cer, PathSAT);
                    if (!validate2)
                    {
                        throw new Exception("El certificado no está emitido por una entidad certificadora");
                    }


                    X509KeyUsageExtension keyUsageExtension;
                    string KeyUsages;
                    int isfiel = 0;

                    for (int i = 0; i <= installedCert.Extensions.Count; i++)
                    {
                        try
                        {
                            keyUsageExtension = installedCert.Extensions[i] as X509KeyUsageExtension;
                            KeyUsages = keyUsageExtension.KeyUsages.ToString();

                            if (keyUsageExtension.KeyUsages.ToString().IndexOf("KeyAgreement") != -1 || keyUsageExtension.KeyUsages.ToString().IndexOf("DataEncipherment") != -1)
                            {
                                isfiel = 1;
                            }
                        }
                        catch (Exception e)
                        {
                            continue;
                        }


                    }

                    if (1 == isfiel)
                    {


                        if (ConfigurationManager.AppSettings["AcceptFIEL"].ToString() == "true")
                        {





                            // tenemos que validar si lo puede hacer por el hecho de que sea una persona fisica

                            if (RFC.Length != 13)
                            {
                                throw new Exception("Las personas morales no pueden entrar con un CSD");

                            }

                            // BLCFDRevocados validaLCO = new BLCFDRevocados();


                            //// CFDRevocados CFDLCO = validaLCO.spValidaListadoCSD(RFC);

                            // // tenemos que validar si lo puede hacer por el hecho de que no tenga CSD registrado previamente
                            // //if (CFDLCO != null)
                            // //{
                            // //    throw new Exception(MessageExceptions.COD_1488 + MessageExceptions.MSG_1488);

                            // //}


                            // CFDRevocados FIELLCO = validaLCO.ValidaRFCsFIEL(RFC, hexString2Ascii(installedCert.SerialNumber));
                            // // tenemos que validar si lo puede hacer por el hecho de que no tenga CSD registrado previamente
                            // if (FIELLCO == null)
                            // {
                            //     throw new ExceptionFIEL(MessageExceptions.COD_1489_2, MessageExceptions.MSG_1489_2);

                            // }



                        }
                        else
                        {

                            // throw new ExceptionFIEL(MessageExceptions.COD_1471, MessageExceptions.MSG_1471);

                        }
                    }


                    cert_pfx = CertificateValidation.MakeCert.generatePFX(byte_cer, byte_key, password);

                    installedCert = new X509Certificate2(cert_pfx, password, X509KeyStorageFlags.MachineKeySet);
                    if (null != installedCert)
                    {
                        if (!installedCert.Issuer.Contains("O=Servicio de Administración Tributaria"))
                            throw new Exception("No es un certificado emitido por el SAT");
                        if ((DateTime.Now > installedCert.NotAfter) || (DateTime.Now < installedCert.NotBefore))
                            throw new Exception("El certificado no se encuentra dentro de un periodo de validez");
                        if (!installedCert.HasPrivateKey)
                            throw new Exception("El certificado no tiene llave privada");
                        if (!installedCert.Subject.ToUpper().Contains(RFC.ToUpper()))
                            throw new Exception("El certificado no pertene al contribuyente");
                    }

                }
                else
                    throw new Exception("Ni idea de que error sea 1");
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message.ToString());
            }

            return cert_pfx;
        }