예제 #1
0
        public IActionResult VerificarFirmaElectronica(VerificarFirmaElectronicaViewModel viewModel)
        {
            if (viewModel.Archivo.Length > 0)
            {
                try
                {
                    using (var fileStream = viewModel.Archivo.OpenReadStream())
                        using (var ms = new MemoryStream())
                            using (var firmaStream = viewModel.ArchivoFirmaElectronica.OpenReadStream())
                                using (var msFirma = new MemoryStream())
                                {
                                    fileStream.CopyTo(ms);
                                    var fileBytes = ms.ToArray();

                                    firmaStream.CopyTo(msFirma);
                                    var firmaBytes = msFirma.ToArray();
                                    //string s = Convert.ToBase64String(fileBytes);

                                    Dictionary <string, byte[]> archivos = CompressionHelper.UnCompressZip(firmaBytes);

                                    var certificate = new X509Certificate2(archivos["certificado.crt"]);

                                    var publicKey = certificate.GetPublicKey().Reverse <byte>().ToArray();

                                    Punto publicKeyPoint = new Punto(publicKey);

                                    var hex = publicKeyPoint.GetUncompressedByteArray().ToHexString();

                                    var result = LibreriaCriptografica.FirmaElectronica.VerificarFirmaElectronica(publicKeyPoint, fileBytes, archivos["firma.bin"].Reverse <byte>().ToArray());

                                    var commonName = certificate.GetNameInfo(X509NameType.SimpleName, false);

                                    //var organizationArea = certificate.GetNameInfo(X509NameType., false);


                                    var subjectViewModel = new CertificateSubjectViewModel();
                                    var subjects         = certificate.Subject.Split(',');

                                    foreach (var subject in subjects)
                                    {
                                        var elements = subject.Split('=');
                                        switch (elements[0].TrimStart(' ').TrimEnd(' '))
                                        {
                                        case "CN": subjectViewModel.CN = elements[1].TrimStart(' ').TrimEnd(' '); break;

                                        case "OU": subjectViewModel.OU = elements[1].TrimStart(' ').TrimEnd(' '); break;

                                        case "O": subjectViewModel.O = elements[1].TrimStart(' ').TrimEnd(' '); break;

                                        case "S": subjectViewModel.S = elements[1].TrimStart(' ').TrimEnd(' '); break;

                                        case "C": subjectViewModel.C = elements[1].TrimStart(' ').TrimEnd(' '); break;
                                        }
                                    }

                                    return(Ok(new VerificarFirmaElectronicaResultViewModel()
                                    {
                                        Result = result,
                                        Subject = subjectViewModel,
                                        FileName = viewModel.Archivo.FileName,
                                        FileSize = viewModel.Archivo.Length,
                                        FileHash = LibreriaCriptografica.FirmaElectronica.ObtenerHash(fileBytes).ToHexString()
                                    }));
                                    //return File(firmaByteArray, "application/x-msdownload", fileName);
                                }
                }
                catch (System.IO.InvalidDataException e)
                {
                    return(Ok(new Response(false, "Hubo un error")));
                }
                catch (Exception e) {
                }
            }
            return(null);
        }