Beispiel #1
0
 /// <summary>
 /// Ritorna true se in un file PDF sono presenti delle firme pades
 /// </summary>
 /// <param name="fileDoc"></param>
 /// <returns></returns>
 public static bool IsPdfPades(DocsPaVO.documento.FileDocumento fileDoc)
 {
     try
     {
         int numSig = 0;
         dpaItextSharp.text.pdf.PdfReader  r  = new dpaItextSharp.text.pdf.PdfReader(fileDoc.content);
         dpaItextSharp.text.pdf.AcroFields af = r.AcroFields;
         if (af != null)
         {
             numSig = af.GetSignatureNames().Count;
             if (numSig > 0)
             {
                 return(true);
             }
         }
         return(false);
     }
     catch
     {
         return(false);
     }
 }
Beispiel #2
0
        public static bool CheckNumSignature(DocsPaVO.documento.FileDocumento fileDoc)
        {
            bool result  = true;
            int  numSign = 0;

            try
            {
                if (fileDoc != null && fileDoc.signatureResult != null && fileDoc.signatureResult.PKCS7Documents != null)
                {
                    numSign = fileDoc.signatureResult.PKCS7Documents.Count();
                    dpaItextSharp.text.pdf.PdfReader pdfReader = new dpaItextSharp.text.pdf.PdfReader(fileDoc.content);

                    PdfDictionary top = (PdfDictionary)PdfReader.GetPdfObjectRelease(pdfReader.Catalog.Get(PdfName.ACROFORM));
                    if (top == null)
                    {
                        return(true);
                    }
                    PdfArray arrfds = (PdfArray)PdfReader.GetPdfObjectRelease(top.Get(PdfName.FIELDS));
                    if (arrfds != null && arrfds.Size > numSign)
                    {
                        for (int i = 0; i < arrfds.Size; i++)
                        {
                            if (arrfds.ArrayList[i].GetType() == typeof(dpaItextSharp.text.pdf.PdfNull))
                            {
                                return(false);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                result = true;
            }

            return(result);
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileDoc"></param>
        /// <returns></returns>
        public static bool VerifyPadesSignature(DocsPaVO.documento.FileDocumento fileDoc)
        {
            SignedDocument  si = new SignedDocument();
            VerifyTimeStamp verifyTimeStamp = new VerifyTimeStamp();

            string padesSignAlgorithm = null;

            dpaItextSharp.text.pdf.PdfReader pdfReader = null;
            try
            {
                pdfReader = new dpaItextSharp.text.pdf.PdfReader(fileDoc.content);
            }
            catch
            {
                return(false);
            }

            dpaItextSharp.text.pdf.AcroFields af = pdfReader.AcroFields;
            List <string> signNames = af.GetSignatureNames().Cast <string>().ToList <string>();

            if (signNames.Count == 0) //Firma non è presente
            {
                return(false);
            }

            List <DocsPaVO.documento.SignerInfo> siList = new List <DocsPaVO.documento.SignerInfo>();
            bool verResult = true;


            foreach (string name in signNames)
            {
                List <DocsPaVO.documento.TSInfo> tsLst = new List <DocsPaVO.documento.TSInfo>();
                dpaItextSharp.text.pdf.PdfPKCS7  pk    = af.VerifySignature(name);

                try
                {
                    padesSignAlgorithm = "PADES " + pk.GetHashAlgorithm();
                }
                catch (Exception exalg)
                {
                    padesSignAlgorithm = "PADES : errore ricavando l'algo Hash:" + exalg.Message;
                }

                byte[] cert = pk.SigningCertificate.GetEncoded();
                DocsPaVO.documento.SignerInfo sinfo = si.GetCertSignersInfo(cert);
                sinfo.SignatureAlgorithm = padesSignAlgorithm;
                sinfo.SigningTime        = pk.SignDate;
                if (verResult) //fino a che è true verifica
                {
                    verResult = pk.Verify();
                }

                if (pk.TimeStampToken != null)
                {
                    //Ricavo il certificato
                    ICollection certsColl = pk.TimeStampToken.GetCertificates("COLLECTION").GetMatches(null);
                    DocsPaVO.documento.TSInfo timeStamp = verifyTimeStamp.getTSCertInfo(certsColl);

                    timeStamp.TSdateTime     = pk.TimeStampToken.TimeStampInfo.GenTime.ToLocalTime();
                    timeStamp.TSserialNumber = pk.TimeStampToken.TimeStampInfo.SerialNumber.ToString();
                    timeStamp.TSimprint      = Convert.ToBase64String(pk.TimeStampToken.TimeStampInfo.TstInfo.MessageImprint.GetEncoded());
                    timeStamp.TSType         = DocsPaVO.documento.TsType.PADES;
                    tsLst.Add(timeStamp);
                }
                if (tsLst.Count > 0)
                {
                    sinfo.SignatureTimeStampInfo = tsLst.ToArray();
                }

                siList.Add(sinfo);
            }

            DocsPaVO.documento.VerifySignatureResult result = new DocsPaVO.documento.VerifySignatureResult();

            if (verResult)
            {
                result.StatusCode        = 0;
                result.StatusDescription = "La Verifica OK, ma senza controllo CRL";
            }
            else
            {
                result.StatusCode        = -1;
                result.StatusDescription = "La Verifica di almeno un firmatario e Fallita";
            }

            List <DocsPaVO.documento.PKCS7Document> pkcsDocs = new List <DocsPaVO.documento.PKCS7Document>();

            if ((fileDoc.signatureResult != null) && (fileDoc.signatureResult.PKCS7Documents != null) && (fileDoc.signatureResult.PKCS7Documents.Length > 0))
            {
                foreach (DocsPaVO.documento.PKCS7Document docs in fileDoc.signatureResult.PKCS7Documents)
                {
                    pkcsDocs.Add(docs);
                }
            }

            pkcsDocs.Add(new DocsPaVO.documento.PKCS7Document {
                SignersInfo = siList.ToArray(), SignAlgorithm = padesSignAlgorithm, DocumentFileName = fileDoc.nomeOriginale, SignHash = "Non Disponibile per la firma PADES", SignatureType = DocsPaVO.documento.SignType.PADES
            });
            result.PKCS7Documents    = pkcsDocs.ToArray();
            result.FinalDocumentName = fileDoc.name;
            fileDoc.signatureResult  = result;

            return(false);
        }