public static void ReadXmlSigned(this Fattura fattura, Stream stream, bool validateSignature = true)
        {
            CmsSignedData signedFile = new CmsSignedData(stream);

            if (validateSignature)
            {
                IX509Store             certStore   = signedFile.GetCertificates("Collection");
                ICollection            certs       = certStore.GetMatches(new X509CertStoreSelector());
                SignerInformationStore signerStore = signedFile.GetSignerInfos();
                ICollection            signers     = signerStore.GetSigners();

                foreach (object tempCertification in certs)
                {
                    Org.BouncyCastle.X509.X509Certificate certification = tempCertification as Org.BouncyCastle.X509.X509Certificate;

                    foreach (object tempSigner in signers)
                    {
                        SignerInformation signer = tempSigner as SignerInformation;
                        if (!signer.Verify(certification.GetPublicKey()))
                        {
                            throw new FatturaElettronicaSignatureException(Resources.ErrorMessages.SignatureException);
                        }
                    }
                }
            }

            using (var memoryStream = new MemoryStream())
            {
                signedFile.SignedContent.Write(memoryStream);
                fattura.ReadXml(memoryStream);
            }
        }
Ejemplo n.º 2
0
        public IFileInformation Convert(IFileInformation Input)
        {
            using (XmlReader _XmlReader = XmlReader.Create(new StringReader(File.ReadAllText(Input.Path.LocalPath)), _XmlReaderSettings))
            {
                _FatturaDocument = new Fattura( );

                _FatturaDocument.ReadXml(_XmlReader);
            }


            Document _PDFDocument = OpenPDFDoc(25.0f, 25.0f, Input);


            _PDFDocument.Add(GeneratePDFHeader(_FatturaDocument.FatturaElettronicaHeader));

            for (int i = 0; i < _FatturaDocument.FatturaElettronicaBody.Count; i++)
            {
                AddInvoiceBodyToPDFPage(_FatturaDocument.FatturaElettronicaBody[i], _PDFDocument, i);
            }

            String _FileName = _XMLPAPDFFileName( );

            _PDFDocument.Add(new Paragraph("\n" + _FileName, _HeaderHelvetica));


            _PDFDocument.Close( );

            return(new FileInformation(new FileFormat(EFileExtension.PDF, EFormat.PDF, "1.4"),
                                       new Uri(Input.Path.LocalPath + ".pdf"),
                                       Input.Directory + "\\" + _FileName.Replace(@"/", "") + ".pdf"));
        }
 public static void ReadXml(this Fattura fattura, string filePath)
 {
     using (var r = XmlReader.Create(filePath, new XmlReaderSettings {
         IgnoreWhitespace = true, IgnoreComments = true
     }))
     {
         fattura.ReadXml(r);
     }
 }
        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);
        }
 public static void ReadXml(this Fattura fattura, Stream stream)
 {
     stream.Position = 0;
     using (var r = XmlReader.Create(stream, new XmlReaderSettings {
         IgnoreWhitespace = true, IgnoreComments = true
     }))
     {
         fattura.ReadXml(r);
     }
 }
Ejemplo n.º 6
0
        public bool IsValidXML(String InputXML)
        {
            using (var r = XmlReader.Create(new StringReader(InputXML), _XmlReaderSettings))
            {
                _FatturaDocument = new Fattura( );

                _FatturaDocument.ReadXml(r);

                return(_FatturaDocument.Validate( ).IsValid);
            }
        }
Ejemplo n.º 7
0
        public void WriteHtml()
        {
            var f       = new Fattura();
            var outFile = Path.GetTempFileName();

            f.ReadXml("Samples/IT02182030391_32.xml");

            f.WriteHtml(outFile, "Samples/fatturaPA_v1.2.1.xsl");

            var bytes = File.ReadAllBytes(outFile);

            Assert.IsTrue(bytes.Length > 2048);
        }
        public static void ReadXmlSigned(this Fattura fattura, string filePath)
        {
            CmsSignedData          signedFile  = new CmsSignedData(new FileStream(filePath, FileMode.Open, FileAccess.Read));
            IX509Store             certStore   = signedFile.GetCertificates("Collection");
            ICollection            certs       = certStore.GetMatches(new X509CertStoreSelector());
            SignerInformationStore signerStore = signedFile.GetSignerInfos();
            ICollection            signers     = signerStore.GetSigners();

            foreach (object tempCertification in certs)
            {
                Org.BouncyCastle.X509.X509Certificate certification = tempCertification as Org.BouncyCastle.X509.X509Certificate;

                foreach (object tempSigner in signers)
                {
                    SignerInformation signer = tempSigner as SignerInformation;
                    if (!signer.Verify(certification.GetPublicKey()))
                    {
                        throw new FatturaElettronicaSignatureException(Resources.ErrorMessages.SignatureException);
                    }
                }
            }


            string outFile = Path.GetTempFileName();

            using (var fileStream = new FileStream(outFile, FileMode.Create, FileAccess.Write))
            {
                signedFile.SignedContent.Write(fileStream);
            }

            using (var r = XmlReader.Create(outFile, new XmlReaderSettings {
                IgnoreWhitespace = true, IgnoreComments = true
            }))
            {
                fattura.ReadXml(r);
            }
        }