Example #1
0
        private void btnFirmarHuella_Click(object sender, EventArgs e)
        {
            if (!rbInternnallyDetached.Checked)
            {
                MessageBox.Show("Por favor, seleccione el tipo de firma internally detached.");
                return;
            }

            if (string.IsNullOrEmpty(txtFichero.Text))
            {
                MessageBox.Show("Debe seleccionar un fichero para firmar.");
                return;
            }

            XadesService xadesService = new XadesService();

            SignatureParameters parametros = ObtenerParametrosFirma();

            parametros.SignaturePackaging     = SignaturePackaging.HASH_INTERNALLY_DETACHED;
            parametros.DataFormat             = new DataFormat();
            parametros.DataFormat.MimeType    = "hash/sha256";
            parametros.DataFormat.Description = "Hash value encoded in binary format";

            using (FileStream fs = new FileStream(txtFichero.Text, FileMode.Open))
            {
                using (parametros.Signer = new Signer(CertUtil.SelectCertificate()))
                {
                    _signatureDocument = xadesService.Sign(fs, parametros);
                }
            }

            MessageBox.Show("Firma completada, ahora puede Guardar la firma o ampliarla a Xades-T.", "Test firma XADES",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #2
0
        private void btnFirmar_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFichero.Text))
            {
                MessageBox.Show("Debe seleccionar un fichero para firmar.");
                return;
            }

            CadesService        cs         = new CadesService();
            SignatureParameters parametros = ObtenerParametrosFirma();

            parametros.Certificate = CertUtil.SelectCertificate();
            parametros.MimeType    = MimeTypeInfo.GetMimeType(txtFichero.Text);

            if (rbAttachedImplicit.Checked)
            {
                parametros.SignaturePackaging = SignaturePackaging.ATTACHED_IMPLICIT;
            }
            else
            {
                parametros.SignaturePackaging = SignaturePackaging.DETACHED_EXPLICIT;
            }

            using (parametros.Signer = new Signer((X509Certificate2)parametros.Certificate))
            {
                using (FileStream fs = new FileStream(txtFichero.Text, FileMode.Open))
                {
                    _signatureDocument = cs.Sign(fs, parametros);
                }
            }

            MessageBox.Show("Signature completed, you can now save the signature or extend it to CAdES-T.", "CAdES signature test",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #3
0
        private void btnContraFirma_Click(object sender, EventArgs e)
        {
            if (_signatureDocument == null)
            {
                return;
            }

            FrmSeleccionarFirma frm = new FrmSeleccionarFirma(_signatureDocument);

            if (frm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("Debe seleccionar una firma");
                return;
            }

            SignatureParameters parametros = ObtenerParametrosFirma();

            parametros.SignaturePolicyInfo = null;
            parametros.Certificate         = CertUtil.SelectCertificate();

            using (parametros.Signer = new Signer((X509Certificate2)parametros.Certificate))
            {
                CadesService cs = new CadesService();

                _signatureDocument = cs.CounterSign(_signatureDocument, frm.SignerInfo, parametros);
            }

            MessageBox.Show("Signature completed, you can now save the signature or extend it to CAdES-T.", "CAdES signature test",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #4
0
        private void btnContraFirma_Click(object sender, EventArgs e)
        {
            SignatureParameters parametros = ObtenerParametrosFirma();

            using (parametros.Signer = new Signer(CertUtil.SelectCertificate()))
            {
                _signatureDocument = _xadesService.CounterSign(_signatureDocument, parametros);
            }

            MessageBox.Show("Firma completada correctamente.", "Test firma XADES",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #5
0
        private void btnFirmar_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFichero.Text))
            {
                MessageBox.Show("Debe seleccionar un fichero para firmar.");
                return;
            }

            XadesService        xadesService = new XadesService();
            SignatureParameters parametros   = ObtenerParametrosFirma();

            if (rbInternnallyDetached.Checked)
            {
                parametros.SignaturePolicyInfo = ObtenerPolitica();
                parametros.SignaturePackaging  = SignaturePackaging.INTERNALLY_DETACHED;
                parametros.DataFormat          = new DataFormat();
                parametros.DataFormat.MimeType = MimeTypeInfo.GetMimeType(txtFichero.Text);
            }
            else if (rbExternallyDetached.Checked)
            {
                parametros.SignaturePackaging = SignaturePackaging.EXTERNALLY_DETACHED;
                parametros.ExternalContentUri = txtFichero.Text;
            }
            else if (rbEnveloped.Checked)
            {
                parametros.SignaturePackaging = SignaturePackaging.ENVELOPED;
            }
            else if (rbEnveloping.Checked)
            {
                parametros.SignaturePackaging = SignaturePackaging.ENVELOPING;
            }

            using (parametros.Signer = new Signer(CertUtil.SelectCertificate()))
            {
                if (parametros.SignaturePackaging != SignaturePackaging.EXTERNALLY_DETACHED)
                {
                    using (FileStream fs = new FileStream(txtFichero.Text, FileMode.Open))
                    {
                        _signatureDocument = xadesService.Sign(fs, parametros);
                    }
                }
                else
                {
                    _signatureDocument = xadesService.Sign(null, parametros);
                }
            }

            MessageBox.Show("Firma completada, ahora puede Guardar la firma o ampliarla a Xades-T.", "Test firma XADES",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #6
0
        private void btnFirmar_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFichero.Text))
            {
                MessageBox.Show("Debe seleccionar un fichero para firmar.");
                return;
            }

            SignatureParameters parametros = ObtenerParametrosFirma();

            if (rbInternnallyDetached.Checked)
            {
                // TODO: gestionar correctamente los tipos MIME
                string mimeType = "application/" +
                                  System.IO.Path.GetExtension(txtFichero.Text).ToLower().Replace(".", "");

                parametros.SignaturePolicyInfo = ObtenerPolitica();
                parametros.SignaturePackaging  = SignaturePackaging.INTERNALLY_DETACHED;
                parametros.InputMimeType       = mimeType;
            }
            else if (rbExternallyDetached.Checked)
            {
                parametros.SignaturePackaging = SignaturePackaging.EXTERNALLY_DETACHED;
                parametros.ExternalContentUri = txtFichero.Text;
            }
            else if (rbEnveloped.Checked)
            {
                parametros.SignaturePackaging = SignaturePackaging.ENVELOPED;
            }

            using (parametros.Signer = new Signer(CertUtil.SelectCertificate()))
            {
                if (parametros.SignaturePackaging != SignaturePackaging.EXTERNALLY_DETACHED)
                {
                    using (FileStream fs = new FileStream(txtFichero.Text, FileMode.Open))
                    {
                        _signatureDocument = _xadesService.Sign(fs, parametros);
                    }
                }
                else
                {
                    _signatureDocument = _xadesService.Sign(null, parametros);
                }
            }

            MessageBox.Show("Firma completada, ahora puede Guardar la firma o ampliarla a Xades-T.", "Test firma XADES",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #7
0
        private void btnCoFirmar_Click(object sender, EventArgs e)
        {
            if (_signatureDocument == null)
            {
                return;
            }

            SignatureParameters parametros = ObtenerParametrosFirma();

            parametros.Certificate        = CertUtil.SelectCertificate();
            parametros.SignaturePackaging = _signatureDocument.SignaturePackaging;

            using (parametros.Signer = new Signer((X509Certificate2)parametros.Certificate))
            {
                CadesService cs = new CadesService();

                _signatureDocument = cs.CoSign(_signatureDocument, parametros);
            }

            MessageBox.Show("Signature completed, you can now save the signature or extend it to CAdES-T.", "CAdES signature test",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #8
0
        private void btnCoFirmar_Click(object sender, EventArgs e)
        {
            if (_signatureDocument == null)
            {
                return;
            }

            SignatureParameters parametros = ObtenerParametrosFirma();

            parametros.Certificate        = CertUtil.SelectCertificate();
            parametros.SignaturePackaging = _signatureDocument.SignaturePackaging;

            using (parametros.Signer = new Signer((X509Certificate2)parametros.Certificate))
            {
                CadesService cs = new CadesService();

                _signatureDocument = cs.CoSign(_signatureDocument, parametros);
            }

            MessageBox.Show("Firma completada, ahora puede Guardar la firma o ampliarla a CAdES-T.", "Test firma CAdES",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #9
0
        private void btnFirmaMavisa_Click(object sender, EventArgs e)
        {
            // DemostraciĆ³n de firma masiva

            if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DirectoryInfo   dirInfo = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
                List <FileInfo> files   = new List <FileInfo>(dirInfo.EnumerateFiles("*.pdf"));

                if (files.Count == 0)
                {
                    MessageBox.Show("No se han encontrado documentos");
                    return;
                }

                using (Signer signer = new Signer(CertUtil.SelectCertificate()))
                {
                    SignatureParameters sp = new SignatureParameters();
                    sp.Signer              = signer;
                    sp.SignaturePackaging  = SignaturePackaging.INTERNALLY_DETACHED;
                    sp.DataFormat          = new DataFormat();
                    sp.DataFormat.MimeType = "application/pdf";
                    sp.SignatureMethod     = ObtenerAlgoritmo();
                    sp.SignaturePolicyInfo = ObtenerPolitica();

                    XadesService xadesService = new XadesService();

                    foreach (var file in files)
                    {
                        SignatureDocument sigDocument = xadesService.Sign(file.OpenRead(), sp);

                        sigDocument.Save(string.Format(@"{0}\{1}.{2}", folderBrowserDialog1.SelectedPath, Path.GetFileNameWithoutExtension(file.Name), "xsig"));
                    }
                }

                MessageBox.Show("Proceso completado");
            }
        }
Example #10
0
        private void btnFirmarHuella_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtHuellaPrecalculada.Text))
            {
                MessageBox.Show("You must specify the fingerprint value to sign");
                return;
            }

            SignatureParameters parametros = ObtenerParametrosFirma();

            byte[] digestValue = Convert.FromBase64String(txtHuellaPrecalculada.Text);

            if ((parametros.DigestMethod == DigestMethod.SHA1 &&
                 digestValue.Length != 20) ||
                (parametros.DigestMethod == DigestMethod.SHA256 &&
                 digestValue.Length != 32) ||
                (parametros.DigestMethod == DigestMethod.SHA512 &&
                 digestValue.Length != 64))
            {
                MessageBox.Show("The length of the fingerprint value does not match the selected fingerprint algorithm.");
                return;
            }

            parametros.Certificate         = CertUtil.SelectCertificate();
            parametros.SignaturePolicyInfo = null;
            parametros.PreCalculatedDigest = digestValue;

            CadesService cs = new CadesService();

            using (parametros.Signer = new Signer((X509Certificate2)parametros.Certificate))
            {
                _signatureDocument = cs.Sign(null as Stream, parametros);
            }

            MessageBox.Show("Signature completed, you can now save the signature or extend it to CAdES-T.", "CAdES signature test",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #11
0
        private void btnFirmarHuella_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtHuellaPrecalculada.Text))
            {
                MessageBox.Show("Debe especificar el valor de huella a firmar");
                return;
            }

            SignatureParameters parametros = ObtenerParametrosFirma();

            byte[] digestValue = Convert.FromBase64String(txtHuellaPrecalculada.Text);

            if ((parametros.DigestMethod == DigestMethod.SHA1 &&
                 digestValue.Length != 20) ||
                (parametros.DigestMethod == DigestMethod.SHA256 &&
                 digestValue.Length != 32) ||
                (parametros.DigestMethod == DigestMethod.SHA512 &&
                 digestValue.Length != 64))
            {
                MessageBox.Show("La longitud del valor de la huella no coincide con el algoritmo de huella seleccionado.");
                return;
            }

            parametros.Certificate         = CertUtil.SelectCertificate();
            parametros.SignaturePolicyInfo = null;
            parametros.PreCalculatedDigest = digestValue;

            CadesService cs = new CadesService();

            using (parametros.Signer = new Signer((X509Certificate2)parametros.Certificate))
            {
                _signatureDocument = cs.Sign(null, parametros);
            }

            MessageBox.Show("Firma completada, ahora puede Guardar la firma o ampliarla a CAdES-T.", "Test firma CAdES",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }