sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using System.Threading;
namespace FE.Libreria
{
public class Archivo
{
public HttpPostedFileBase File { get; set; }
public HttpPostedFileBase Llave { get; set; }
public string Password { get; set; }
public short id{ get; set; }
public short id1{ get; set; }
public short id2{ get; set; }
}
public class Cadena
{
public string nombre { get; set; }
public string rfc { get; set; }
public string curp { get; set; }
public string txtVigenciaDesde { get; set; }
public string txtVigenciaHasta { get; set; }
public string serial { get; set; }
public string serial2 { get; set; }
public string wValidaCert { get; set; }
public string StatusCert { get; set; }
}
public class ValKey
{
public string mensaje { get; set; }
public string Fin { get; set; }
}
public class SAT
{
public Cadena AbreCer(string certificado, string edo)
{
Cadena Cadena = new Cadena();
string wAbreCert;
string wVigencia;
string wSHA1;
string wSerial;
string wnotBefore;
string wnotAfter;
string wModulusC;
string wConvPEM;
string wConvPEM2;
string wValidacion;
string wRFC;
int differenceInDays;
//-------------------------------------------------------------------------
//Convierte el CERTIFICADO de formato DER a formato PEM con la siguiente instrucción de OpenSSL
//openssl x509 -inform DER -outform PEM -in AAA010101AAAsd.cer -pubkey > AAA010101AAA.cer.pem
// |-----------------| |-------------------|
// | |
// Cerficado original Certificado resultante
//-------------------------------------------------------------------------
//wConvPEM = "C:\\OpenSSL-Win64\\Bin\\openssl x509 -inform DER -outform PEM -in " & CertPathName & " -pubkey > C:\\OpenSSL-Win64\\Bin\\" & CertName & ".pem"
//-------------------------------------------------------------------------
//Extrae el contenido del CERTIFICADO con la siguiente instrucción de OpenSSL
//openssl x509 -in AAA010101AAA.cer.pem –text > Cert1.TXT
// |-------------------| |-------|
// | |
// Certificado en formato PEM Archivo Resultante
//-------------------------------------------------------------------------
if (File.Exists("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Cert2.BAT"))
{
File.Delete("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Cert2.BAT");
}
if (File.Exists("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Cert1.BAT"))
{
File.Delete("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Cert1.BAT");
}
if (File.Exists("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Val2.BAT"))
{
File.Delete("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Val2.BAT");
}
if (File.Exists("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Val1.BAT"))
{
File.Delete("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Val1.BAT");
}
if (File.Exists("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Cert3.txt"))
{
File.Delete("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Cert3.txt");
}
if (File.Exists("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Vigencia.txt"))
{
File.Delete("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Vigencia.txt");
}
if (File.Exists("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\sha1.txt"))
{
File.Delete("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\sha1.txt");
}
if (File.Exists("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\serial.txt"))
{
File.Delete("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\serial.txt");
}
if (File.Exists("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\NotBefore.txt"))
{
File.Delete("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\NotBefore.txt");
// Console.Write("Hola mundo");
//Console.ReadKey();
}
if (File.Exists("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\NotAfter.txt"))
{
File.Delete("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\NotAfter.txt");
}
if (File.Exists("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\RFC.txt"))
{
File.Delete("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\RFC.txt");
// Console.Write("Hola mundo");
//Console.ReadKey();
}
if (File.Exists("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\ModulusC.txt"))
{
File.Delete("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\ModulusC.txt");
}
if (File.Exists("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Validacion.txt"))
{
File.Delete("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Validacion.txt");
}
//-------------------------------------------------------------------------
//No quitar la comilla inicial a las siguientes 4 lineas, ya que son
//solo de ejemplo del uso de OpenSSL
//-------------------------------------------------------------------------
//openssl x509 -in cert.pem -sha1 'Para obtener la huella SHA1
//openssl x509 -in cert.pem -noout -sha1 -serial 'Para obtener NUMERO DE SERIE
//openssl x509 -in cert.pem -noout -sha1 -startdate 'Para obtener el campo notBefore
//openssl x509 -in cert.pem -noout -sha1 -enddate 'Para obtener el campo notAfter
wConvPEM = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl x509 -inform DER -outform PEM -in " + certificado + ".cer -pubkey > C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\" + certificado + ".pem";
wConvPEM2 = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl x509 -in " + certificado + ".cer -inform DER -out C:\\FE\\" + edo + "\\Test\\" + certificado + ".pem -outform PEM";
wAbreCert = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl x509 -in C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\" + certificado + ".pem -text > C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Cert1.txt";
wVigencia = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl x509 -in C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\" + certificado + ".pem -noout -dates > C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Vigencia.txt";
wSHA1 = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl x509 -in C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\" + certificado + ".pem -sha1 > C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\SHA1.txt";
wSerial = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl x509 -in C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\" + certificado + ".pem -noout -sha1 -serial > C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Serial.txt";
wnotBefore = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl x509 -in C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\" + certificado + ".pem -noout -sha1 -startdate > C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\NotBefore.txt";
wnotAfter = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl x509 -in C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\" + certificado + ".pem -noout -sha1 -enddate > C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\NotAfter.txt";
wRFC = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl x509 -in C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\" + certificado + ".pem -noout -sha1 -subject > C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\RFC.txt";
wModulusC = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl x509 -in C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\" + certificado + ".pem -noout -modulus > C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\ModulusC.txt";
//Crea el archivo Cert1.BAT
StreamWriter escritura = File.AppendText("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Cert1.bat");
escritura.WriteLine(wConvPEM);
escritura.WriteLine(wConvPEM2);
escritura.WriteLine(wAbreCert);
escritura.WriteLine(wSHA1);
escritura.WriteLine(wSerial);
escritura.WriteLine(wnotBefore);
escritura.WriteLine(wnotAfter);
escritura.WriteLine(wRFC);
escritura.WriteLine(wModulusC);
escritura.WriteLine(wVigencia);
escritura.Close();
//-------------------------------------------------------------------------
//Ejecuta el archivo BAT
//Se utiliza WSH en lugar de Shell wAppPath & "Bin\Cert1.bat", vbHide
//para que continue con el proceso hasta que haya terminado de ejecutar el BAT
//wRUN = oShl.Run("C:\\OpenSSL-Win64\\Bin\\Cert1.bat", SW_HIDE, True);
StreamWriter escritura5 = File.AppendText("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Cert2.bat");
escritura5.WriteLine("cd/");
escritura5.WriteLine("CD FE");
escritura5.WriteLine("CD "+ edo);
escritura5.WriteLine("CD OpenSSL-Win64");
escritura5.WriteLine("CD Bin");
escritura5.WriteLine("Start Cert1.bat");
escritura5.Close();
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.FileName = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Cert2.bat";
proc.Start();
proc.WaitForExit();
for (int i = 0; i < 5; i++)
{
Thread.Sleep(5000);
}
//Call oShl.Popup("Se ha leido la información del certificado", 4, "WSH terminado", vbInformation)
//-------------------------------------------------------------------------
StreamReader Lectura;
try
{
Lectura = File.OpenText("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\RFC.txt");
Cadena.nombre = Lectura.ReadToEnd();
Lectura.Close();
}
catch (FileNotFoundException fe)
{
Console.WriteLine("!Error¡" + fe.Message);
}
try
{
Lectura = File.OpenText("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\NotBefore.txt");
Cadena.txtVigenciaDesde = Lectura.ReadLine();
Lectura.Close();
}
catch (FileNotFoundException fe)
{
Console.WriteLine("!Error¡" + fe.Message);
}
try
{
Lectura = File.OpenText("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\NotAfter.txt");
Cadena.txtVigenciaHasta = Lectura.ReadLine();
Lectura.Close();
}
catch (FileNotFoundException fe)
{
Console.WriteLine("!Error¡" + fe.Message);
}
try
{
Lectura = File.OpenText("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Serial.txt");
Cadena.serial = Lectura.ReadLine();
Lectura.Close();
}
catch (FileNotFoundException fe)
{
Console.WriteLine("!Error¡" + fe.Message);
}
if ((Cadena.nombre)!= null)
{
Cadena.nombre = Cadena.nombre.Replace("subject", "");
Cadena.nombre = Cadena.nombre.Replace("CN", "");
Cadena.nombre = Cadena.nombre.Replace("name", "");
Cadena.nombre = Cadena.nombre.Replace("O", "");
Cadena.nombre = Cadena.nombre.Replace("C", "");
Cadena.nombre = Cadena.nombre.Replace("emailAddress", "");
Cadena.nombre = Cadena.nombre.Replace("x500UniqueIdentifier", "");
Cadena.nombre = Cadena.nombre.Replace("serialNumber", "");
Cadena.nombre = Cadena.nombre.Replace("=", "");
string[] campos = new string[8];
char[] delimitador = { '/' };
campos = Cadena.nombre.Split(delimitador);
Cadena.nombre = campos[3].Trim();
Cadena.rfc = campos[5].Trim();
Cadena.curp = campos[6].Trim();
}
if ((Cadena.txtVigenciaDesde) != null)
{
char[] MyChar = { ':', '=' };
string txtNotBefore = Cadena.txtVigenciaDesde.TrimStart(MyChar);
Cadena.txtVigenciaDesde = GetDateGS(txtNotBefore);
}
if ((Cadena.txtVigenciaHasta) != null)
{
char[] MyChar = { ':', '=' };
string txtNotAfter = Cadena.txtVigenciaHasta.TrimStart(MyChar);
Cadena.txtVigenciaHasta = GetDateGS(txtNotAfter);
// Difference in days, hours, and minutes.
TimeSpan ts = Convert.ToDateTime(Cadena.txtVigenciaHasta) - DateTime.Now;
differenceInDays = ts.Days;
switch (differenceInDays)
{
case 30:
Cadena.wValidaCert = "30 Dias antes del vencimiento";
break;
case 15:
Cadena.wValidaCert = "15 Dias antes del vencimiento";
break;
case 8:
Cadena.wValidaCert = "8 Dias antes del vencimiento";
break;
default:
if((differenceInDays)>=0){
Cadena.wValidaCert = "Vence en: " + differenceInDays + " dias ";
}
if((differenceInDays)<0){
Cadena.wValidaCert = "Vencido";
}
break;
}
}
if ((Cadena.serial) != null)
{
char[] MyChar = { 's', 'e', 'r', 'i', 'a', 'l', '=' };
Cadena.serial2 = Cadena.serial.TrimStart(MyChar);
Cadena.serial = Cadena.serial2.Substring(1, 1) + Cadena.serial2.Substring(3, 1) + Cadena.serial2.Substring(5, 1) + Cadena.serial2.Substring(7, 1) + Cadena.serial2.Substring(9, 1) + Cadena.serial2.Substring(11, 1) + Cadena.serial2.Substring(13, 1) + Cadena.serial2.Substring(15, 1) + Cadena.serial2.Substring(17, 1) + Cadena.serial2.Substring(19, 1) + Cadena.serial2.Substring(21, 1) + Cadena.serial2.Substring(23, 1) + Cadena.serial2.Substring(25, 1) + Cadena.serial2.Substring(27, 1) + Cadena.serial2.Substring(29, 1) + Cadena.serial2.Substring(31, 1) + Cadena.serial2.Substring(33, 1) + Cadena.serial2.Substring(35, 1) + Cadena.serial2.Substring(37, 1) + Cadena.serial2.Substring(39, 1);
}
if ((Cadena.wValidaCert) != "Vencido")
{
string Val1 = Cadena.serial2.Substring(23, 1);
wValidacion = "";
switch (Val1)
{
case "0":
wValidacion = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl ocsp -issuer C:\\FE\\" + edo + "\\Test\\AC0_SAT.pem -cert C:\\FE\\" + edo + "\\Test\\" + certificado + ".pem -text -url https://cfdi.sat.gob.mx/edofiel -VAfile C:\\FE\\" + edo + "\\Test\\ocsp.ac0_sat.pem -header host cfdi.sat.gob.mx > C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Validacion.txt";
break;
case "1":
wValidacion = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl ocsp -issuer C:\\FE\\" + edo + "\\Test\\AC1_SAT.pem -cert C:\\FE\\" + edo + "\\Test\\" + certificado + ".pem -text -url https://cfdi.sat.gob.mx/edofiel -VAfile C:\\FE\\" + edo + "\\Test\\ocsp.ac1_sat.pem -header host cfdi.sat.gob.mx > C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Validacion.txt";
break;
case "2":
wValidacion = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl ocsp -issuer C:\\FE\\" + edo + "\\Test\\AC2_SAT.pem -cert C:\\FE\\" + edo + "\\Test\\" + certificado + ".pem -text -url https://cfdi.sat.gob.mx/edofiel -VAfile C:\\FE\\" + edo + "\\Test\\ocsp.ac2_sat.pem -header host cfdi.sat.gob.mx > C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Validacion.txt";
break;
case "3":
wValidacion = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl ocsp -issuer C:\\FE\\" + edo + "\\Test\\AC3_SAT.pem -cert C:\\FE\\" + edo + "\\Test\\" + certificado + ".pem -text -url https://cfdi.sat.gob.mx/edofiel -VAfile C:\\FE\\" + edo + "\\Test\\ocsp.ac3_sat.pem -header host cfdi.sat.gob.mx > C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Validacion.txt";
break;
default:
wValidacion = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\openssl ocsp -issuer C:\\FE\\" + edo + "\\Test\\AC4_SAT.pem -cert C:\\FE\\" + edo + "\\Test\\" + certificado + ".pem -text -url https://cfdi.sat.gob.mx/edofiel -VAfile C:\\FE\\" + edo + "\\Test\\ocsp.ac4_sat.pem -header host cfdi.sat.gob.mx > C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Validacion.txt";
break;
}
StreamWriter escritura9 = File.AppendText("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Val1.bat");
escritura9.WriteLine(wValidacion);
escritura9.Close();
StreamWriter escritura4 = File.AppendText("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Val2.bat");
escritura4.WriteLine("cd/");
escritura4.WriteLine("CD FE");
escritura4.WriteLine("CD " + edo);
escritura4.WriteLine("CD OpenSSL-Win64");
escritura4.WriteLine("CD Bin");
escritura4.WriteLine("Start Val1.bat");
escritura4.Close();
System.Diagnostics.Process proc1 = new System.Diagnostics.Process();
proc1.EnableRaisingEvents = true;
proc1.StartInfo.FileName = "C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Val2.bat";
proc1.Start();
proc1.WaitForExit();
for (int i = 0; i < 10; ++i)
{
Thread.Sleep(3000);
}
try
{
Lectura = File.OpenText("C:\\FE\\" + edo + "\\OpenSSL-Win64\\Bin\\Validacion.txt");
Cadena.StatusCert = Lectura.ReadToEnd();
Lectura.Close();
if ((Cadena.StatusCert) != null)
{
Cadena.StatusCert = Cadena.StatusCert.Replace(" ", " ");
string[] arreglo = Cadena.StatusCert.Split(' ');
for (int i = 0; i < arreglo.Length; i++)
{
if (arreglo[i].Trim() == "good")
{
Cadena.StatusCert = "Bueno";
break;
}
if (arreglo[i].Trim() == "revoqued" || arreglo[i].Trim() == "revoked\n" || arreglo[i].Trim() == "revoked")
{
Cadena.StatusCert = "Revocado";
break;
}
}
}
else
{
Cadena.StatusCert = "Revocado";
}
}
catch (FileNotFoundException fe)
{
Console.WriteLine("!Error¡" + fe.Message);
}
}
return Cadena;
}
public static string GetDateGS(string wFechaGMT)
{
//El formato de la fecha viene en (GMT) Greenwich Mean Time
//notBefore=Aug 21 15:22:10 2008 GMT
//'notAfter=Aug 21 15:22:10 2010 GMT
string wFe1;
string wDay;
string wMonth;
string wMonthN;
string wDayN;
string wYear;
string wDate;
string resultado;
wFechaGMT = wFechaGMT.Trim();
wFechaGMT = wFechaGMT.ToUpper();
wFe1 = "";
if (wFechaGMT.Substring(0,9) == "NOTBEFORE")
{
wFe1 = wFechaGMT.Substring( 10, 21);
}
if (wFechaGMT.Substring(0, 8) == "NOTAFTER") {
wFe1 = wFechaGMT.Substring(9, 21);
}
if (wFe1 == "") {
resultado = "";
return resultado;
}
wDay = wFe1.Substring( 4, 2);
wMonth =wFe1.Substring(0, 3);
wYear = wFe1.Substring(16, 5);
switch (wDay.Trim())
{
case "1":
wDayN = "01";
break;
case "2":
wDayN = "02";
break;
case "3":
wDayN = "03";
break;
case "4":
wDayN = "04";
break;
case "5":
wDayN = "05";
break;
case "6":
wDayN = "06";
break;
case "7":
wDayN = "07";
break;
case "8":
wDayN = "08";
break;
case "9":
wDayN = "09";
break;
default:
wDayN = wDay;
break;
}
switch (wMonth)
{
case "JAN":
wMonthN = "01";
break;
case "FEB":
wMonthN = "02";
break;
case "MAR":
wMonthN = "03";
break;
case "APR":
wMonthN = "04";
break;
case "MAY":
wMonthN = "05";
break;
case "JUN":
wMonthN = "06";
break;
case "JUL":
wMonthN = "07";
break;
case "AUG":
wMonthN = "08";
break;
case "SEP":
wMonthN = "09";
break;
case "OCT":
wMonthN = "10";
break;
case "NOV" :
wMonthN = "11";
break;
case "DEC":
wMonthN = "12";
break;
default:
wMonthN = "00";
break;
}
wDate = wDayN + "/" + wMonthN + "/" + wYear;
//GetDateGS = QuitaChars(wDate, "0123456789-/");
resultado = wDate;
return resultado;
}
public ValKey AbreKey( string key, string Password, string edo1)
{
ValKey Valkey = new ValKey();
StreamReader Lectura;
string wConvPEM;
string wModulusK;
if (File.Exists("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\Key1.BAT"))
{
File.Delete("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\Key1.BAT");
}
if (File.Exists("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\Key2.BAT"))
{
File.Delete("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\Key2.BAT");
}
if (File.Exists("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\ModulKey1.BAT"))
{
File.Delete("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\ModulKey1.BAT");
}
if (File.Exists("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\ModulKey.BAT"))
{
File.Delete("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\ModulKey.BAT");
}
if (File.Exists("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\pkey.key.pem"))
{
File.Delete("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\pkey.key.pem");
}
if (File.Exists("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\ModulusK.txt"))
{
File.Delete("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\ModulusK.txt");
}
if (File.Exists("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\ModulusK.txt"))
{
File.Delete("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\ModulusK.txt");
}
//'------------------------------------------------------------------
//'Crea el archivo SELLO.BAT que utilizará para sellar las facturas
//'-------------------------------------------------------------------
//'------------------------------------------------------------------
//'Convierte la llave de formato DER a formato PEM
//'-------------------------------------------------------------------
wConvPEM = "C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\openssl pkcs8 -inform DER -in " + key + ".key -passin pass:" + Password + " -out C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\pkey.key.pem";
StreamWriter escritura1 = File.AppendText("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\Key1.bat");
escritura1.WriteLine(wConvPEM);
escritura1.Close();
//'Se utiliza WSH en lugar de Shell wAppPath & "Bin\\key1.bat", vbNormalFocus
//'para que continue con el proceso hasta que haya terminado de ejecutar el BAT
//wRUN = oShl.Run("C:\\OpenSSL-Win64\\Bin\\key1.bat", SW_SHOWNORMAL, True); //'SW_SHOWNORMAL SW_SHOWMAXIMIZED
StreamWriter escritura4 = File.AppendText("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\Key2.bat");
escritura4.WriteLine("cd/");
escritura4.WriteLine("CD FE");
escritura4.WriteLine("CD "+ edo1 +"");
escritura4.WriteLine("CD OpenSSL-Win64");
escritura4.WriteLine("CD Bin");
escritura4.WriteLine("Start Key1.bat");
escritura4.Close();
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.FileName = "C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\Key2.bat";
proc.Start();
proc.WaitForExit();
for (int i = 0; i < 10; ++i)
{
Thread.Sleep(2000);
}
try{
string text = File.ReadAllText("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\pkey.key.pem");
if (text.Length == 0)
{
Valkey.Fin="PASSWORD_MAL";
}
else
{
//'------------------------------------------------------------------
//'Crea el MODULUS de la llave para verificar que corresponda al certificado
//'-------------------------------------------------------------------
wModulusK = "C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\openssl rsa -in C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\pkey.key.pem -noout -modulus > C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\ModulusK.txt";
StreamWriter escritura2 = File.AppendText("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\ModulKey.bat");
escritura2.WriteLine(wModulusK);
escritura2.Close();
StreamWriter escrituras = File.AppendText("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\ModulKey1.bat");
escrituras.WriteLine("cd/");
escrituras.WriteLine("CD FE");
escrituras.WriteLine("CD " + edo1 + "");
escrituras.WriteLine("CD OpenSSL-Win64");
escrituras.WriteLine("CD Bin");
escrituras.WriteLine("Start ModulKey.bat");
escrituras.Close();
System.Diagnostics.Process proc1 = new System.Diagnostics.Process();
proc1.EnableRaisingEvents = true;
proc1.StartInfo.FileName = "C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\ModulKey1.bat";
proc1.Start();
//wRUN = oShl.Run("C:\\OpenSSL-Win64\\Bin\\ModulKey.bat", SW_HIDE, True);
//'------------------------------------------------------------------
//'Comprueba que la llave y el certificado correspondan each other
//'-------------------------------------------------------------------
for (int i = 0; i < 10; ++i)
{
Thread.Sleep(2000);
}
string txtModulusC = "";
string txtModulusK = "";
try
{
Lectura = File.OpenText("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\ModulusC.txt");
txtModulusC = Lectura.ReadLine();
Lectura.Close();
}
catch (FileNotFoundException fe)
{
Console.WriteLine("!Error¡" + fe.Message);
}
try
{
Lectura = File.OpenText("C:\\FE\\" + edo1 + "\\OpenSSL-Win64\\Bin\\ModulusK.txt");
txtModulusK = Lectura.ReadLine();
Lectura.Close();
}
catch (FileNotFoundException fe)
{
Console.WriteLine("!Error¡" + fe.Message);
}
if (txtModulusC != txtModulusK)
{
Valkey.mensaje ="Incorrecta";
Valkey.Fin = "Incorrecto";
}
else
{
Valkey.mensaje = "Correcta";
Valkey.Fin = "Proceso concluido exitosamente";
}
}
}
catch (FileNotFoundException fe)
{
Console.WriteLine("!Error¡" + fe.Message);
}
return Valkey;
}
public object True { get; set; }}
}
No hay comentarios:
Publicar un comentario