private void visualizarXMLDaAssinaturaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if ((lstSigners.SelectedItems.Count > 0) && (lstSigners.SelectedItems[0].Group.Header != "commonSignatures"))
            {
                if (lstSigners.SelectedItems[0].SubItems[5] != null)
                {
                    string certIdent = lstSigners.SelectedItems[0].SubItems[0].Text;
                    string filePath  = lstSigners.SelectedItems[0].SubItems[3].Text;

                    CertificadoDigital.SignatureList list = CertificadoDigital.Validate.validateFile(filePath);

                    foreach (CertificadoDigital.Signature cdSig in list)
                    {
                        if (certIdent.Substring(0, cdSig.Signer.Length).Equals(cdSig.Signer))
                        {
                            XmlDocument xDoc         = new XmlDocument();
                            string      tempFilePath = Path.GetTempFileName() + ".xml";

                            TextReader tr = new StringReader(cdSig.toXml());
                            xDoc.Load(tr);

                            TextWriter tw = new StreamWriter(tempFilePath, true, System.Text.Encoding.UTF8);
                            tw.Write(xDoc.InnerXml);
                            tw.Flush();
                            tw.Close();

                            Process.Start(tempFilePath);

                            return;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Get the File Properties (Metadata)
        /// Creator, Last Modifier, Title, Description, Subject, Created Date, Modified Date
        /// </summary>
        /// <returns>Returns a ArrayList containing the file properties</returns>
        public DocumentCoreProperties(Package package, XpsDocument xpsDocument, Types type, string pdfFile, CertificadoDigital.SignatureList pdfSigList)
        {
            if (type.Equals(Types.XpsDocument))
            {
                if (xpsDocument.CoreDocumentProperties.Creator != null)
                {
                    DocumentProperties.Add(xpsDocument.CoreDocumentProperties.Creator.ToString());
                }
                else
                {
                    DocumentProperties.Add("");
                }
                if (xpsDocument.CoreDocumentProperties.LastModifiedBy != null)
                {
                    DocumentProperties.Add(xpsDocument.CoreDocumentProperties.LastModifiedBy.ToString());
                }
                else
                {
                    DocumentProperties.Add("");
                }
                if (xpsDocument.CoreDocumentProperties.Title != null)
                {
                    DocumentProperties.Add(xpsDocument.CoreDocumentProperties.Title.ToString());
                }
                else
                {
                    DocumentProperties.Add("");
                }
                if (xpsDocument.CoreDocumentProperties.Description != null)
                {
                    DocumentProperties.Add(xpsDocument.CoreDocumentProperties.Description.ToString());
                }
                else
                {
                    DocumentProperties.Add("");
                }
                if (xpsDocument.CoreDocumentProperties.Subject != null)
                {
                    DocumentProperties.Add(xpsDocument.CoreDocumentProperties.Subject.ToString());
                }
                else
                {
                    DocumentProperties.Add("");
                }
                if (xpsDocument.CoreDocumentProperties.Created != null)
                {
                    DocumentProperties.Add(xpsDocument.CoreDocumentProperties.Created.ToString());
                }
                else
                {
                    DocumentProperties.Add("");
                }
                if (xpsDocument.CoreDocumentProperties.Modified != null)
                {
                    DocumentProperties.Add(xpsDocument.CoreDocumentProperties.Modified.ToString());
                }
                else
                {
                    DocumentProperties.Add("");
                }
            }
            else if (type.Equals(Types.PdfDocument))
            {
                /*****
                *   Creator
                *   LastModifiedBy
                *   Title
                *   Description
                *   Subject
                *   Created
                *   Modified
                *****/

                if (pdfSigList == null)
                {
                    try
                    {
                        pdfSigList = CertificadoDigital.Validate.validateFile(pdfFile);
                    }
                    catch (CertificadoDigital.NoSignatureFoundException nsfe) { }
                }

                if (pdfSigList != null && pdfSigList.Count > 0 & pdfSigList[0] != null)
                {
                    CertificadoDigital.FileProperties[] prop =
                    {
                        CertificadoDigital.FileProperties.Author,
                        CertificadoDigital.FileProperties.LastModifiedBy,
                        CertificadoDigital.FileProperties.Title,
                        CertificadoDigital.FileProperties.Description,
                        CertificadoDigital.FileProperties.Subject,
                        CertificadoDigital.FileProperties.CreationDate,
                        CertificadoDigital.FileProperties.ModDate
                    };

                    for (int i = 0; i < prop.Length; i++)
                    {
                        if (prop[i] == CertificadoDigital.FileProperties.CreationDate || prop[i] == CertificadoDigital.FileProperties.ModDate)
                        {
                            DocumentProperties.Add(
                                pdfSigList[0].FileProperties.ContainsKey(prop[i]) ?
                                DateTime.ParseExact(pdfSigList[0].FileProperties[prop[i]].Substring(2, 8), "yyyyMMdd", null).ToString("dd-MMM-yy") :
                                string.Empty);
                        }
                        else
                        {
                            DocumentProperties.Add(
                                pdfSigList[0].FileProperties.ContainsKey(prop[i]) ? pdfSigList[0].FileProperties[prop[i]] : string.Empty);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < 7; i++)
                    {
                        DocumentProperties.Add("");
                    }
                }
            }
            else
            {
                const String coreRelType = @"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties";
                PackagePart  corePart    = null;
                Uri          documentUri = null;

                foreach (PackageRelationship relationship in package.GetRelationshipsByType(coreRelType))
                {
                    documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);
                    corePart    = package.GetPart(documentUri);
                    break; //There is only one part
                }

                if (corePart != null)
                {
                    NameTable           nt    = new NameTable();
                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
                    nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
                    nsmgr.AddNamespace("cp", "http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
                    nsmgr.AddNamespace("dcterms", "http://purl.org/dc/terms/");

                    XmlDocument doc = new XmlDocument(nt);
                    doc.Load(corePart.GetStream());

                    XmlNode nodeCreator = doc.DocumentElement.SelectSingleNode("//dc:creator", nsmgr);
                    if (nodeCreator != null)
                    {
                        DocumentProperties.Add(nodeCreator.InnerText);
                    }
                    else
                    {
                        DocumentProperties.Add("");
                    }

                    XmlNode nodeModifier = doc.DocumentElement.SelectSingleNode("//cp:lastModifiedBy", nsmgr);
                    if (nodeModifier != null)
                    {
                        DocumentProperties.Add(nodeModifier.InnerText);
                    }
                    else
                    {
                        DocumentProperties.Add("");
                    }

                    XmlNode nodeTitle = doc.DocumentElement.SelectSingleNode("//dc:title", nsmgr);
                    if (nodeTitle != null)
                    {
                        DocumentProperties.Add(nodeTitle.InnerText);
                    }
                    else
                    {
                        DocumentProperties.Add("");
                    }

                    XmlNode nodeDescription = doc.DocumentElement.SelectSingleNode("//dc:description", nsmgr);
                    if (nodeDescription != null)
                    {
                        DocumentProperties.Add(nodeDescription.InnerText);
                    }
                    else
                    {
                        DocumentProperties.Add("");
                    }

                    XmlNode nodeSubject = doc.DocumentElement.SelectSingleNode("//dc:subject", nsmgr);
                    if (nodeSubject != null)
                    {
                        DocumentProperties.Add(nodeSubject.InnerText);
                    }
                    else
                    {
                        DocumentProperties.Add("");
                    }

                    XmlNode nodeCreatedDate = doc.DocumentElement.SelectSingleNode("//dcterms:created", nsmgr);
                    if (nodeCreatedDate != null)
                    {
                        DocumentProperties.Add(DateTime.Parse(nodeCreatedDate.InnerText).ToShortDateString());
                    }
                    else
                    {
                        DocumentProperties.Add("");
                    }

                    XmlNode nodeModifiedDate = doc.DocumentElement.SelectSingleNode("//dcterms:modified", nsmgr);
                    if (nodeModifiedDate != null)
                    {
                        DocumentProperties.Add(DateTime.Parse(nodeModifiedDate.InnerText).ToShortDateString());
                    }
                    else
                    {
                        DocumentProperties.Add("");
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Return a List of Signer in the package
        /// </summary>
        /// <returns>
        /// Package Path (string), Signer Name (string), Signer URI (string), Signer Issuer (string)
        /// </returns>
        public Signers GetDigitalSigners()
        {
            Signers sigs = new Signers();

            sigs.Path = signers.Path;

            List <X509Certificate2> certificateList      = new List <X509Certificate2>();
            List <XmlSignaturePart> xmlSignaturePartList = this.GetDigitalSignatures();

            if (DocumentType.Equals(Types.XpsDocument))
            {
                //To collect the information of the signature we used XPS like a System.IO.Packaging

                xpsDocument.Close();
                package = Package.Open(signers.Path, FileMode.Open, FileAccess.Read);

                PackageDigitalSignatureManager _signatures = null;
                _signatures = new PackageDigitalSignatureManager(package);
                _signatures.CertificateOption = CertificateEmbeddingOption.InSignaturePart;

                // Add the signers in the list
                foreach (PackageDigitalSignature signature in _signatures.Signatures)
                {
                    string           name   = signature.Signer.Subject.Replace("CN=", "");
                    string           uri    = signature.SignaturePart.Uri.ToString();
                    string           date   = signature.SigningTime.ToString();
                    string           issuer = signature.Signer.Issuer.Replace("CN=", "");
                    string           serial = signature.Signer.GetSerialNumberString();
                    X509Certificate2 signatureCertificate = (X509Certificate2)signature.Signer;

                    sigs.Add(name, uri, issuer, date, serial, signatureCertificate);
                }
                package.Close();
                xpsDocument = new XpsDocument(signers.Path, FileAccess.ReadWrite);
                return(sigs);
            }
            else if (DocumentType.Equals(Types.PdfDocument))
            {
                if (this.pdfSignatureList == null)
                {
                    try
                    {
                        this.pdfSignatureList = CertificadoDigital.Validate.validateFile(pdfDocumentPath);
                    }
                    catch (CertificadoDigital.NoSignatureFoundException nsfe) { }
                }

                if (this.pdfSignatureList != null)
                {
                    foreach (CertificadoDigital.Signature sig in this.pdfSignatureList)
                    {
                        string name   = sig.OfficeTemplateSubject().Replace("CN=", "");
                        string uri    = null;
                        string date   = sig.DateTime.ToString();
                        string issuer = sig.OfficeTemplateIssuer().Replace("CN=", "");
                        string serial = sig.Certificates[0].Serial;

                        sigs.Add(name, uri, issuer, date, serial, sig.X509Certificate);
                    }
                }

                return(sigs);
            }
            else
            {
                PackageDigitalSignatureManager _signatures = null;
                _signatures = new PackageDigitalSignatureManager(package);
                _signatures.CertificateOption = CertificateEmbeddingOption.InSignaturePart;

                // Add the signers in the list
                foreach (PackageDigitalSignature signature in _signatures.Signatures)
                {
                    string           name   = signature.Signer.Subject.Replace("CN=", "");
                    string           uri    = signature.SignaturePart.Uri.ToString();
                    string           date   = signature.SigningTime.ToString();
                    string           issuer = signature.Signer.Issuer.Replace("CN=", "");
                    string           serial = signature.Signer.GetSerialNumberString();
                    X509Certificate2 signatureCertificate = (X509Certificate2)signature.Signer;

                    sigs.Add(name, uri, issuer, date, serial, signatureCertificate);
                }
                return(sigs);
            }
        }