public static void WriteXmlSigned(this Fattura fattura, X509Certificate2 cert, string p7mFilePath)
        {
            string res      = string.Empty;
            string tempFile = Path.GetTempFileName();

            if (!p7mFilePath.ToLowerInvariant().EndsWith(".p7m"))
            {
                p7mFilePath += ".p7m";
            }

            try
            {
                fattura.WriteXml(tempFile);

                ContentInfo content   = new ContentInfo(new Oid("1.2.840.113549.1.7.1", "PKCS 7 Data"), File.ReadAllBytes(tempFile));
                SignedCms   signedCms = new SignedCms(SubjectIdentifierType.IssuerAndSerialNumber, content, false);
                CmsSigner   signer    = new CmsSigner(cert);
                signer.IncludeOption   = X509IncludeOption.EndCertOnly;
                signer.DigestAlgorithm = new Oid("2.16.840.1.101.3.4.2.1", "SHA256");
                signer.SignedAttributes.Add(new Pkcs9SigningTime(DateTime.Now));
                try
                {
                    //PKCS7 format
                    signedCms.ComputeSignature(signer, false);
                }
                catch (CryptographicException cex)
                {
                    //To evaluate for the future https://stackoverflow.com/a/52897100

                    /*
                     * // Try re-importing the private key into a better CSP:
                     * using (RSA tmpRsa = RSA.Create())
                     * {
                     *  tmpRsa.ImportParameters(cert.GetRSAPrivateKey().ExportParameters(true));
                     *
                     *  using (X509Certificate2 tmpCertNoKey = new X509Certificate2(cert.RawData))
                     *  using (X509Certificate2 tmpCert = tmpCertNoKey.CopyWithPrivateKey(tmpRsa))
                     *  {
                     *      signer.Certificate = tmpCert;
                     *      signedCms.ComputeSignature(signer, false);
                     *  }
                     * }*/

                    throw cex;
                }
                byte[] signature = signedCms.Encode();
                File.WriteAllBytes(p7mFilePath, signature);
            }
            catch (Exception)
            {
                throw new FatturaElettronicaSignatureException(Resources.ErrorMessages.FirmaException);
            }
            finally
            {
                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }
            }
        }
Пример #2
0
        public static void WriteHtml(this Fattura fattura, string outPath, string xslPath)
        {
            if (outPath == null)
            {
                throw new ArgumentNullException(nameof(outPath));
            }
            if (xslPath == null)
            {
                throw new ArgumentNullException(nameof(xslPath));
            }

            var tempXml = Path.GetTempFileName();

            using (var w = XmlWriter.Create(tempXml, new XmlWriterSettings {
                Indent = true
            }))
            {
                fattura.WriteXml(w);
            }

            var xt = new XslCompiledTransform();

            xt.Load(xslPath);
            xt.Transform(tempXml, outPath);
        }
 public static void WriteXml(this Fattura fattura, string filePath)
 {
     using (var w = XmlWriter.Create(filePath, new XmlWriterSettings {
         Indent = true
     }))
     {
         fattura.WriteXml(w);
     }
 }
        private IFileInformation TryGenerateXMLFile(IFileInformation _FileInformation, String Destination, FileConversionUpdate _FileConversionUpdate)
        {
            String FileText = File.ReadAllText(_FileInformation.Path.LocalPath);

            if (!FileText.Contains("FatturaElettronicaHeader") && !FileText.Contains("FatturaElettronicaBody"))
            {
                throw new InvalidOperationException("Unsupported Format" + _FileInformation.Path);
            }


            int    _XMLStartIndex   = FileText.IndexOf(Conventions.HeaderGaurd) - 1;
            int    _XMLEndIndex     = FileText.LastIndexOf(Conventions.BodyGaurd) - _XMLStartIndex + Conventions.BodyGaurd.Length + 1;
            String _RawBoundedData  = FileText.Substring(_XMLStartIndex, _XMLEndIndex);
            String _PossibleXMLData = XML.Conventions.Header + Conventions.Header + CleanInvalidXmlChars(_RawBoundedData) + Conventions.Footer;

            FileInformation _ResultFileInformation = new FileInformation(new FileFormat(EFileExtension.XML, EFormat.Uknown, ""), Destination + "\\" + _FileInformation.FileName + ".xml");

            using (XmlReader _XmlReader = XmlReader.Create(new StringReader(_PossibleXMLData), new XmlReaderSettings {
                IgnoreWhitespace = true, IgnoreComments = true
            }))
            {
                Fattura _Fattura = new Fattura( );

                try
                {
                    _Fattura.ReadXml(_XmlReader);

                    if (_Fattura.Validate( ).IsValid)
                    {
                        using (XmlWriter _XmlWriter = XmlWriter.Create(_ResultFileInformation.Path.LocalPath, new XmlWriterSettings {
                            Indent = true
                        }))
                        {
                            _Fattura.WriteXml(_XmlWriter);
                        }

                        _FileConversionUpdate.AddTransformation(EFileTransformation.ConvertedToCopied, _FileInformation, _ResultFileInformation);
                    }
                    else
                    {
                        throw new ArgumentException("Invalid XMLPA FileContent ");
                    }
                }
                catch (Exception Ex)
                {
                    File.WriteAllText(_ResultFileInformation.Path.LocalPath, _PossibleXMLData);

                    _FileConversionUpdate.AddTransformation(EFileTransformation.ConvertedToCopied,
                                                            _FileInformation,
                                                            _ResultFileInformation,
                                                            new StateEventArgs(ESourceState.Unstable, Ex));
                }
            }


            return(File.Exists(_ResultFileInformation.Path.LocalPath) ? _ResultFileInformation : null);
        }