public FrmSeleccionarFirma(FirmaXades[] firmas)
        {
            InitializeComponent();

            _firmas = firmas;

            foreach (var firma in firmas)
            {
                string textoFirma = string.Format("{0} - {1}",
                    firma.XadesSignature.XadesObject.QualifyingProperties.SignedProperties.SignedSignatureProperties.SigningTime,
                    firma.Certificate.Subject);

                lstFirmas.Items.Add(textoFirma);
            }

            lstFirmas.SelectedIndex = 0;
        }
        private void btnGenerar_Click(object sender, EventArgs e)
        {
            FirmaXades firmaXades = new FirmaXades();
            string ficheroFactura = Application.StartupPath + "\\Facturae.xml";

            firmaXades.SetContentEnveloped(ficheroFactura);

            // Política de firma de factura-e 3.1
            firmaXades.PolicyIdentifier = "http://www.facturae.es/politica_de_firma_formato_facturae/politica_de_firma_formato_facturae_v3_1.pdf";
            firmaXades.PolicyHash = "Ohixl6upD6av8N7pEvDABhEL6hM=";

            firmaXades.Sign(firmaXades.SelectCertificate());

            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                firmaXades.Save(saveFileDialog1.FileName);
                MessageBox.Show("Fichero guardado correctamente.");
            }
        }
Exemple #3
0
        /// <summary>
        /// Carga un archivo de firma.
        /// </summary>
        /// <param name="xmlDocument"></param>
        public static FirmaXades[] Load(XmlDocument xmlDocument)
        {
            XmlNodeList signatureNodeList = xmlDocument.GetElementsByTagName("Signature", SignedXml.XmlDsigNamespaceUrl);

            if (signatureNodeList.Count == 0)
            {
                throw new Exception("No se ha encontrado ninguna firma.");
            }

            List <FirmaXades> firmas = new List <FirmaXades>();

            foreach (var signatureNode in signatureNodeList)
            {
                FirmaXades firma = new FirmaXades();
                firma._document = xmlDocument;

                firma._xadesSignedXml = new XadesSignedXml(firma._document);
                firma._xadesSignedXml.LoadXml((XmlElement)signatureNode);

                if (firma._xadesSignedXml.SignedInfo.SignatureMethod == RSAwithSHA1Uri)
                {
                    firma.SignMethod = SignMethod.RSAwithSHA1;
                }
                else if (firma._xadesSignedXml.SignedInfo.SignatureMethod == RSAwithSHA256Uri)
                {
                    firma.SignMethod = SignMethod.RSAwithSHA256;
                }
                else if (firma._xadesSignedXml.SignedInfo.SignatureMethod == RSAwithSHA512Uri)
                {
                    firma.SignMethod = SignMethod.RSAwithSHA512;
                }

                XmlNode keyXml = firma._xadesSignedXml.KeyInfo.GetXml().GetElementsByTagName("X509Certificate", SignedXml.XmlDsigNamespaceUrl)[0];

                firma._signCertificate = new X509Certificate2(Convert.FromBase64String(keyXml.InnerText));

                firma._xadesSignedXml.FindContentElement();

                firmas.Add(firma);
            }

            return(firmas.ToArray());
        }
        private void btnCargarFirma_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open))
                {
                    var firmas = FirmaXades.Load(fs);

                    FrmSeleccionarFirma frm = new FrmSeleccionarFirma(firmas);

                    if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        _firmaXades = frm.FirmaSeleccionada;
                    }
                    else
                    {
                        MessageBox.Show("Debe seleccionar una firma.");
                    }
                }
            }
        }
        /// <summary>
        /// Carga un archivo de firma.
        /// </summary>
        /// <param name="xmlDocument"></param>
        public static FirmaXades[] Load(XmlDocument xmlDocument)
        {
            XmlNodeList signatureNodeList = xmlDocument.GetElementsByTagName("Signature", SignedXml.XmlDsigNamespaceUrl);

            if (signatureNodeList.Count == 0)
            {
                throw new Exception("No se ha encontrado ninguna firma.");
            }

            List<FirmaXades> firmas = new List<FirmaXades>();

            foreach (var signatureNode in signatureNodeList)
            {
                FirmaXades firma = new FirmaXades();
                firma._document = xmlDocument;

                firma._xadesSignedXml = new XadesSignedXml(firma._document);
                firma._xadesSignedXml.LoadXml((XmlElement)signatureNode);

                if (firma._xadesSignedXml.SignedInfo.SignatureMethod == RSAwithSHA1Uri)
                {
                    firma.SignMethod = SignMethod.RSAwithSHA1;
                }
                else if (firma._xadesSignedXml.SignedInfo.SignatureMethod == RSAwithSHA256Uri)
                {
                    firma.SignMethod = SignMethod.RSAwithSHA256;
                }
                else if (firma._xadesSignedXml.SignedInfo.SignatureMethod == RSAwithSHA512Uri)
                {
                    firma.SignMethod = SignMethod.RSAwithSHA512;
                }

                XmlNode keyXml = firma._xadesSignedXml.KeyInfo.GetXml().GetElementsByTagName("X509Certificate", SignedXml.XmlDsigNamespaceUrl)[0];

                firma._signCertificate = new X509Certificate2(Convert.FromBase64String(keyXml.InnerText));

                firma._xadesSignedXml.FindContentElement();

                firmas.Add(firma);
            }

            return firmas.ToArray();
        }