/// <summary>
/// Valida arquivos PDF
/// </summary>
/// <param name="filePath">Caminho do arquivo a ser validado</param>
/// <param name="onlyRoot">
/// Determina se irá considerar apenas certificados de Autoridades Certificadoras.
/// True: Considera apenas certificados de Autoridades Certificadoras.
/// False: Considera todos os certificados instalados na máquina.
/// Default: True.
/// </param>
/// <returns>Lista de assinaturas, válidas ou não</returns>
internal static SignatureList validate(string filePath, bool onlyRoot = true)
{
try
{
#region [obsolete code]
/*
*
* // list of valid certificates
* List<BCX.X509Certificate> kall = new List<BCX.X509Certificate>();
*
* // get the root certificates
* getSystemCertificates(StoreName.Root, StoreLocation.CurrentUser, ref kall);
*
* // if not only root, get others certificates
* if (!onlyRoot)
* {
* getSystemCertificates(StoreName.AddressBook, StoreLocation.CurrentUser, ref kall);
* getSystemCertificates(StoreName.AuthRoot, StoreLocation.CurrentUser, ref kall);
* getSystemCertificates(StoreName.CertificateAuthority, StoreLocation.CurrentUser, ref kall);
* getSystemCertificates(StoreName.My, StoreLocation.CurrentUser, ref kall);
* getSystemCertificates(StoreName.TrustedPeople, StoreLocation.CurrentUser, ref kall);
* getSystemCertificates(StoreName.TrustedPublisher, StoreLocation.CurrentUser, ref kall);
* }
* */
#endregion
// open the pdf file
TS.PdfReader reader = new TS.PdfReader(filePath);
// get the fields inside the file
TS.AcroFields af = reader.AcroFields;
// get the signatures
List <string> names = af.GetSignatureNames();
// if don't found signature
if (names == null || names.Count == 0)
{
throw new NoSignatureFoundException();
}
// signatures to return
SignatureList signatures = new SignatureList();
// for each signature in pdf file
foreach (string name in names)
{
// verify the signature
TSS.PdfPKCS7 pk = af.VerifySignature(name);
// get the datetime of signature
DateTime cal = pk.SignDate;
cal = (pk.TimeStampToken != null ? pk.TimeStampDate : cal);
// create the signature
Signature sig = new Signature
(
filePath, // file path
FileFormat.PDFDocument, // pdf format
pk.Reason, // objective
getSubject(pk.SigningCertificate.SubjectDN), // subject
cal, // date time
//verifySignature(pk.SignCertificateChain, kall, cal, pk.SigningCertificate), // signature validate, obsolete
verifySignature(pk.SigningCertificate),
getSignatureCertificates(pk.SignCertificateChain) // get the certificates
);
// set the x509certificates
sig.SX509Certificate = convertCertificate(pk.SigningCertificate);
// set the issuer
sig.SIssuer = pk.SigningCertificate.IssuerDN.ToString();
// set the file properties
foreach (KeyValuePair <string, string> prop in reader.Info)
{
FileProperties?fp = null;
try
{
fp = (FileProperties)Enum.Parse(typeof(FileProperties), prop.Key, true);
}
catch { }
if (fp.HasValue)
{
sig.addProperties(fp.Value, prop.Value);
}
}
// add signature to the list
signatures.Add(sig);
}
return(signatures);
}
catch (Exception ex)
{
throw ex;
}
}