public static X509Certificate2 getDadosCadeiaCertificadao(CertSimples oCert, out List <ICrlClient> crlList) { X509Store KeyStore = new X509Store(StoreName.My, StoreLocation.CurrentUser); // Abre o Store KeyStore.Open(OpenFlags.ReadOnly); // Obtém a coleção dos certificados da Store X509Certificate2Collection Certificados = KeyStore.Certificates; X509Certificate2Collection certificates = new X509Certificate2Collection(); certificates.Add(oCert.Certificado); List <X509Certificate> chain = new List <X509Certificate>(); X509Certificate2 cert = null; if (certificates.Count > 0) { X509Certificate2Enumerator certificatesEn = certificates.GetEnumerator(); certificatesEn.MoveNext(); cert = certificatesEn.Current; X509Chain x509chain = new X509Chain(); x509chain.Build(cert); foreach (X509ChainElement x509ChainElement in x509chain.ChainElements) { chain.Add(DotNetUtilities.FromX509Certificate(x509ChainElement.Certificate)); } } crlList = new List <ICrlClient>(); crlList.Add(new CrlClientOnline(chain.ToArray())); return(cert); }
public static void AssinaPDF(byte[] File, out byte[] SignFile, CertSimples oCert, int Pagina, int X, int Y, int Rotation, bool AddTimeStamper = true, string urlTimeStamper = "https://freetsa.org/tsr", string timeStampUser = "", string timeStampPass = "", string Reason = "Assinatura Digital", bool AplicaPolitica = false, string Contact = "", TipoAssinatura Tipo = TipoAssinatura.Normal, string Cargo = "", string CREACRM = "") { List <ICrlClient> crlList; X509Certificate2 cert; cert = getDadosCadeiaCertificadao(oCert, out crlList); AssinaComCertificado(crlList, File, out SignFile, oCert, X, Y, Pagina, Rotation, AddTimeStamper, urlTimeStamper, timeStampUser, timeStampPass, Reason, AplicaPolitica, null, null, null, null, Tipo, Cargo, CREACRM); }
private static void ConfiguraAparencia(PdfSigner objStamper, CertSimples cert, int X, int Y, int Largura, int Altura, int Pagina, int Rotation, string Contact = "", string Reason = "Assinatura Digital", string Location = "Indústrias Nucleares do Brasil S/A - INB", string Creator = "Assinador da INB", TipoAssinatura Tipo = TipoAssinatura.Normal, string Cargo = "", string CREACRM = "") { PdfDocument pdfDoc = objStamper.GetDocument(); PdfPage oPage = pdfDoc.GetPage(Pagina); int LarguraAssinatura = Largura; int AlturaAssinatura = Altura; int X_Ajustado, Y_Ajustado; X_Ajustado = X; Y_Ajustado = Y; var crop = oPage.GetCropBox(); float Left, Top, Width, Height; Bitmap bmp = Graphic.ConfiguraBMP(cert, out Altura, Tipo); if (Rotation == 270) { Left = (int)crop.GetLeft() + X_Ajustado; Top = (int)crop.GetRight() - (Y_Ajustado + AlturaAssinatura + 5); } else if (Rotation == 90) { Left = (int)crop.GetRight() + X_Ajustado; Top = (int)crop.GetLeft() - (Y_Ajustado + AlturaAssinatura + 5); } else { Left = (int)crop.GetLeft() + X_Ajustado; Top = (int)crop.GetTop() - (Y_Ajustado + AlturaAssinatura + 5); } Width = LarguraAssinatura; Height = AlturaAssinatura; iText.Kernel.Geom.Rectangle oRetangulo = new iText.Kernel.Geom.Rectangle(Left, Top, Width, Height); var pageSize = pdfDoc.GetPage(Pagina).GetMediaBox(); var signaturePosition = new iText.Kernel.Geom.Rectangle(pageSize.GetLeft(), pageSize.GetBottom(), pageSize.GetWidth(), pageSize.GetHeight()); PdfSignatureAppearance signatureAppearance = objStamper.GetSignatureAppearance(); var memoryStream = new MemoryStream(); bmp.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg); iText.IO.Image.ImageData pic = iText.IO.Image.ImageDataFactory.Create(memoryStream.ToArray()); signatureAppearance.SetReason(Reason); signatureAppearance.SetLocation(Location); signatureAppearance.SetSignatureCreator(Creator); signatureAppearance.SetSignatureGraphic(pic); signatureAppearance.SetRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC); signatureAppearance.SetPageRect(oRetangulo); signatureAppearance.SetPageNumber(Pagina); }
//public static Bitmap ConfiguraBMP(X509Certificate2 cert, bool SeloCargo, bool SeloCREA, bool SeloCRM, string Cargo, string CREACRM, out int Altura, bool SeloCertifico) //{ // Bitmap bmp; // if (SeloCertifico) // { // bmp = new Bitmap(Properties.Resources.seloCertifico); // Altura = 90; // } // else // { // if (SeloCargo == true && (SeloCREA == false && SeloCRM == false)) // { // //SOMENTE SELO DE CARGO // bmp = new Bitmap(Properties.Resources.seloCargo); // Altura = 75; // } // else if (SeloCargo == false && (SeloCREA == true || SeloCRM == true)) // { // //SOMENTE SELO DO CREA OU DO CRM. // if (SeloCREA) // { // bmp = new Bitmap(Properties.Resources.seloCREA); // } // else // { // bmp = new Bitmap(Properties.Resources.seloCRM); // } // Altura = 75; // } // else if (SeloCargo == true && (SeloCREA == true || SeloCRM == true)) // { // //SELO COM CARGO CREA OU CRM // if (SeloCREA) // { // bmp = new Bitmap(Properties.Resources.seloCargoCREA); // } // else // { // bmp = new Bitmap(Properties.Resources.seloCargoCRM); // } // Altura = 90; // } // else // { // bmp = new Bitmap(Properties.Resources.selo); // Altura = 63; // //SELO NORMAL // } // } // bmp = GeraSelo(cert, bmp, Cargo, CREACRM, SeloCertifico); // return bmp; //} public static Bitmap ConfiguraBMP(CertSimples cert, out int Altura, TipoAssinatura Tipo, string Cargo = "", string CREACRM = "") { Bitmap bmp; if (Tipo == TipoAssinatura.Certifico) { bmp = new Bitmap(Properties.Resources.seloCertifico); Altura = 90; } else if (Tipo == TipoAssinatura.Cargo) { //SOMENTE SELO DE CARGO bmp = new Bitmap(Properties.Resources.seloCargo); Altura = 75; } else if (Tipo == TipoAssinatura.CargoCrea) { bmp = new Bitmap(Properties.Resources.seloCargoCREA); Altura = 90; } else if (Tipo == TipoAssinatura.CargoCRM) { bmp = new Bitmap(Properties.Resources.seloCargoCRM); Altura = 90; } else if (Tipo == TipoAssinatura.CREA) { bmp = new Bitmap(Properties.Resources.seloCREA); Altura = 75; } else if (Tipo == TipoAssinatura.CRM) { bmp = new Bitmap(Properties.Resources.seloCRM); Altura = 75; } else if (Tipo == TipoAssinatura.Fortaleza) { bmp = new Bitmap(Properties.Resources.CarimboFortaleza); Altura = 90; } else if (Tipo == TipoAssinatura.Resende) { bmp = new Bitmap(Properties.Resources.CarimboResende); Altura = 90; } else if (Tipo == TipoAssinatura.RioDeJaneiro) { bmp = new Bitmap(Properties.Resources.CarimboRJ); Altura = 90; } else if (Tipo == TipoAssinatura.Caetite) { bmp = new Bitmap(Properties.Resources.CarimboCaetite); Altura = 90; } else if (Tipo == TipoAssinatura.Buena) { bmp = new Bitmap(Properties.Resources.CarimboBuena); Altura = 90; } else if (Tipo == TipoAssinatura.SaoPaulo) { bmp = new Bitmap(Properties.Resources.CarimboSP); Altura = 90; } else if (Tipo == TipoAssinatura.Caldas) { bmp = new Bitmap(Properties.Resources.CarimboCaldas); Altura = 90; } else if (Tipo == TipoAssinatura.ConferidoOriginal) { bmp = new Bitmap(Properties.Resources.ConferidoOriginal); Altura = 90; } else if (Tipo == TipoAssinatura.ChancelaJuridica) { bmp = new Bitmap(Properties.Resources.SeloChancela); Altura = 90; } else { bmp = new Bitmap(Properties.Resources.selo); Altura = 63; //SELO NORMAL } bmp = GeraSelo(cert, bmp, Tipo, Cargo, CREACRM); return(bmp); }
public static Bitmap GeraSelo(CertSimples cert, Bitmap Selo, TipoAssinatura Tipo, string Cargo = "", string CREACRM = "") { Graphics g = Graphics.FromImage(Selo); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; RectangleF qNome; RectangleF qCPF; RectangleF qData; RectangleF qCargo; RectangleF qCREACRM; if (Tipo == TipoAssinatura.Certifico) { qNome = new RectangleF(7, 91, 220, 30); qCPF = new RectangleF(7, 115, 90, 30); qData = new RectangleF(130, 115, 120, 30); } else if (Tipo == TipoAssinatura.Buena || Tipo == TipoAssinatura.Caetite || Tipo == TipoAssinatura.Caldas || Tipo == TipoAssinatura.Fortaleza || Tipo == TipoAssinatura.Resende || Tipo == TipoAssinatura.RioDeJaneiro || Tipo == TipoAssinatura.SaoPaulo) { qNome = new RectangleF(7, 91, 220, 30); qCPF = new RectangleF(7, 115, 90, 30); qData = new RectangleF(130, 115, 120, 30); } else if (Tipo == TipoAssinatura.ConferidoOriginal) { qNome = new RectangleF(7, 91, 220, 30); qCPF = new RectangleF(7, 115, 90, 30); qData = new RectangleF(130, 115, 120, 30); } else if (Tipo == TipoAssinatura.ChancelaJuridica) { qNome = new RectangleF(7, 91, 220, 30); qCPF = new RectangleF(7, 115, 90, 30); qData = new RectangleF(130, 115, 120, 30); } else if (Tipo == TipoAssinatura.Normal) { qNome = new RectangleF(7, 36, 220, 30); qCPF = new RectangleF(7, 66, 90, 30); qData = new RectangleF(130, 66, 120, 30); } else { qNome = new RectangleF(7, 36, 220, 30); qCPF = new RectangleF(7, 66, 90, 30); qData = new RectangleF(130, 66, 120, 30); if (Cargo.Trim() != "" && CREACRM.Trim() == "") { qCargo = new RectangleF(7, 91, 220, 30); g.DrawString(Cargo, new System.Drawing.Font("Tahoma", 7, FontStyle.Bold), Brushes.Black, qCargo); } else if (Cargo.Trim() == "" && CREACRM.Trim() != "") { qCREACRM = new RectangleF(7, 91, 220, 30); g.DrawString(CREACRM, new System.Drawing.Font("Tahoma", 7, FontStyle.Bold), Brushes.Black, qCREACRM); } else if (Cargo.Trim() != "" && CREACRM.Trim() != "") { qCargo = new RectangleF(7, 91, 220, 30); qCREACRM = new RectangleF(7, 115, 220, 30); g.DrawString(Cargo, new System.Drawing.Font("Tahoma", 7, FontStyle.Bold), Brushes.Black, qCargo); g.DrawString(CREACRM, new System.Drawing.Font("Tahoma", 7, FontStyle.Bold), Brushes.Black, qCREACRM); } } string CPFCNPJ; if (cert.Tipo == "F") { CPFCNPJ = cert.CPF.Substring(0, 3) + "." + cert.CPF.Substring(3, 3) + "." + cert.CPF.Substring(6, 3) + "-" + cert.CPF.Substring(9, 2); } else { //formatar CNPJ; CPFCNPJ = cert.CNPJ; } g.DrawString(CPFCNPJ, new System.Drawing.Font("Tahoma", 7, FontStyle.Bold), Brushes.Black, qCPF); int fontSize = 7; if (cert.Nome.Trim().Length > 38) { fontSize = 6; } g.DrawString(cert.Nome, new System.Drawing.Font("Tahoma", fontSize, FontStyle.Bold), Brushes.Black, qNome); g.DrawString(System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"), new System.Drawing.Font("Tahoma", 7, FontStyle.Bold), Brushes.Black, qData); g.Flush(); return(Selo); }
public static List <CertSimples> ListaCertificado(X509Certificate2Collection Certificados) { List <CertSimples> oLista = new List <CertSimples>(); for (int i = 0; i < Certificados.Count; i++) { X509Certificate2 oCertificado = Certificados[i]; CertSimples oCert = new CertSimples(); oCert.SerialNumber = oCertificado.SerialNumber; oCert.Subject = oCertificado.Subject; try { string[] DadosSubject = oCertificado.Subject.Split(','); if (DadosSubject[0].IndexOf(":") > -1) { oCert.Nome = DadosSubject[0].Substring(3, DadosSubject[0].IndexOf(":") - 3); } else { oCert.Nome = DadosSubject[0].Substring(3); } } catch (Exception ex) { oCert.Nome = oCert.Subject; } foreach (var obj in oCertificado.Extensions) { if (obj.Oid.Value == "2.5.29.17") //otherName { byte[] Dados = obj.RawData; Stream sm = new MemoryStream(Dados); // StreamReader oSr = new StreamReader(sm); //string teste = System.Text.Encoding.ASCII.GetString(Dados); DerSequence otherName = (DerSequence)Asn1Object.FromStream(sm); var objCollection = otherName.GetEnumerator(); while (objCollection.MoveNext()) { Org.BouncyCastle.Asn1.DerTaggedObject iSub = (Org.BouncyCastle.Asn1.DerTaggedObject)objCollection.Current; Asn1Object derObject = iSub.GetObject(); if (derObject.GetType().Name.Contains("DerSequence")) { var objSubCollection = ((DerSequence)derObject).GetEnumerator(); byte count = 0; string strOID = ""; DerOctetString strOctet;// = (DerOctetString)derObject; string strTexto = ""; while (objSubCollection.MoveNext()) { var Conteudo = objSubCollection.Current; if (count == 0) { strOID = Conteudo.ToString(); } else { Org.BouncyCastle.Asn1.DerTaggedObject subCampos = (Org.BouncyCastle.Asn1.DerTaggedObject)Conteudo; Asn1Object derSub = subCampos.GetObject(); try { if (derSub.GetType().Name.Contains("DerOctetString")) { strOctet = (DerOctetString)derSub; byte[] Texto = strOctet.GetOctets(); strTexto = System.Text.Encoding.ASCII.GetString(Texto); } else { DerPrintableString strPtrString = (DerPrintableString)derSub; strTexto = strPtrString.GetString(); } } catch (Exception ex) { strTexto = derSub.ToString(); } } count++; } if (strOID == "2.16.76.1.3.1") //PESSOA FÍSICA { //i· OID = 2.16.76.1.3.1 e conteúdo = nas primeiras 8(oito) posições, a data de nascimento do titular, no formato ddmmaaaa; nas 11(onze) posições subseqüentes, o Cadastro de Pessoa Física(CPF) do titular; nas 11(onze) posições subseqüentes, o Número de Identificação Social – NIS(PIS, PASEP ou CI); nas 15(quinze) posições subseqüentes, o número do Registro Geral(RG) do titular; nas 10(dez) posições subseqüentes, as siglas do órgão expedidor do RG e respectiva unidade da federação; try { oCert.DataNascimento = strTexto.Substring(0, 8); oCert.CPF = strTexto.Substring(8, 11); oCert.NIS = strTexto.Substring(19, 11); oCert.RG = strTexto.Substring(30, 15); oCert.OrgaoExpedidor = strTexto.Substring(45); oCert.Tipo = "F"; } catch (Exception ex) { throw new Exception("Erro na leitura da OID=2.16.76.1.3.1:" + ex.Message, ex); } } else if (strOID == "2.16.76.1.3.6") //PESSOA FÍSICA { //ii· OID = 2.16.76.1.3.6 e conteúdo = nas 12 (doze) posições o número do Cadastro Específico do INSS (CEI) da pessoa física titular do certificado; } else if (strOID == "2.16.76.1.3.6") //PESSOA FÍSICA { try { //iii· OID = 2.16.76.1.3.5 e conteúdo nas primeiras 12(doze) posições, o número de inscrição do Título de Eleitor; nas 3(três) posições subseqüentes, a Zona Eleitoral; nas 4(quatro) posições seguintes, a Seção; nas 22(vinte e duas) posições subseqüentes, o município e a UF do Título de Eleitor. oCert.TituloEleitor = strTexto.Substring(0, 12); oCert.ZonaEleitoral = strTexto.Substring(12, 3); oCert.SecaoEleitoral = strTexto.Substring(15, 4); oCert.MunicipioEleitoral = strTexto.Substring(19, 22); } catch (Exception ex) { throw new Exception("Erro na leitura da OID=2.16.76.1.3.6:" + ex.Message, ex); } } else if (strOID == "2.16.76.1.4.2.1.1") { try { oCert.OAB = strTexto; } catch (Exception ex) { throw new Exception("Erro na leitura da OID=2.16.76.1.4.2.1.1:" + ex.Message, ex); } } else if (strOID == "2.16.76.1.3.4") //PESSOA JURÍDICA { try { oCert.Tipo = "J"; //i· OID = 2.16.76.1.3.4 e conteúdo = nas primeiras 8(oito) posições, a data de nascimento do responsável pelo certificado, no formato ddmmaaaa; nas 11(onze) posições subseqüentes, o Cadastro de Pessoa Física(CPF) do responsável; nas 11(onze) posições subseqüentes, o Número de Identificação Social – NIS(PIS, PASEP ou CI); nas 15(quinze) posições subseqüentes, o número do Registro Geral(RG) do responsável; nas 10(dez) posições subseqüentes, as siglas do órgão expedidor do RG e respectiva Unidade da Federação; oCert.DataNascimento = strTexto.Substring(0, 8); oCert.CPF = strTexto.Substring(8, 11); try { oCert.NIS = strTexto.Substring(19, 11); oCert.RG = strTexto.Substring(30, 15); oCert.OrgaoExpedidor = strTexto.Substring(45, 10); } catch (Exception ex) { } } catch (Exception ex) { throw new Exception("Erro na leitura da OID=2.16.76.1.3.4:" + strTexto + "." + ex.Message, ex); } } else if (strOID == "2.16.76.1.3.2") //PESSOA JURÍDICA { //ii· OID = 2.16.76.1.3.2 e conteúdo = nome do responsável pelo certificado; try { oCert.NomeResponsavel = strTexto; } catch (Exception ex) { throw new Exception("Erro na leitura da OID=2.16.76.1.3.2:" + ex.Message, ex); } } else if (strOID == "2.16.76.1.3.3") //PESSOA JURÍDICA { //iii· OID = 2.16.76.1.3.3 e conteúdo = nas 14(quatorze) posições o número do Cadastro Nacional de Pessoa Jurídica(CNPJ) da pessoa jurídica titular do certificado; try { oCert.CNPJ = strTexto; } catch (Exception ex) { throw new Exception("Erro na leitura da OID=2.16.76.1.3.3:" + ex.Message, ex); } } else if (strOID == "2.16.76.1.3.7") //PESSOA JURÍDICA { //iv. OID = 2.16.76.1.3.7 e conteúdo = nas 12 (doze) posições o número do Cadastro Específico do INSS (CEI) da pessoa jurídica titular do certificado. } count = 0; } else { //i. rfc822Name contendo o endereço e-mail do titular do certificado. if (derObject.GetType().Name == "DerOctetString") { DerOctetString strOctet = (DerOctetString)derObject; byte[] Texto = strOctet.GetOctets(); string strTexto = System.Text.Encoding.ASCII.GetString(Texto); oCert.Email = strTexto; } else { string texto = derObject.GetType().Name; } } } sm.Close(); } } oCert.Certificado = oCertificado; oLista.Add(oCert); } return(oLista); }
public static void AssinaComCertificado(List <ICrlClient> crlList, string FileName, string SignFileName, CertSimples cert, int X, int Y, int Pagina, int Rotation, bool AddTimeStamper = true, string urlTimeStamper = "https://freetsa.org/tsr", string timeStampUser = "", string timeStampPass = "", string Reason = "Assinatura Digital", bool AplicaPolitica = false, string MyDigestAlgorithm = "SHA-256", string Contact = "", string Location = "Indústrias Nucleares do Brasil S/A - INB", string Creator = "Assinador da INB", TipoAssinatura Tipo = TipoAssinatura.Normal, string Cargo = "", string CREACRM = "") { string SourcePdfFileName = FileName; string DestPdfFileName = SignFileName; int Largura = 140; int Altura = 63; PdfReader pdfReader = new PdfReader(SourcePdfFileName); FileStream signedPdf = new FileStream(DestPdfFileName, FileMode.Create, FileAccess.ReadWrite); StampingProperties osp = new StampingProperties(); osp.UseAppendMode(); PdfSigner objStamper = new PdfSigner(pdfReader, signedPdf, osp); ITSAClient tsaClient = null; IOcspClient ocspClient = null; ConfiguraAparencia(objStamper, cert, X, Y, Largura, Altura, Pagina, Rotation, Contact, Reason, Location, Creator, Tipo); Org.BouncyCastle.X509.X509Certificate vert = Org.BouncyCastle.Security.DotNetUtilities.FromX509Certificate(cert.Certificado); Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser(); Org.BouncyCastle.X509.X509Certificate[] Arraychain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(cert.Certificado.RawData) }; X509CertificateParser objCP = new X509CertificateParser(); RSACryptoServiceProvider rsa; RSACryptoServiceProvider Provider; IExternalSignature externalSignature; if (cert.Certificado.PrivateKey is RSACryptoServiceProvider) { rsa = (RSACryptoServiceProvider)cert.Certificado.PrivateKey; Provider = (RSACryptoServiceProvider)cert.Certificado.PrivateKey; externalSignature = new AsymmetricAlgorithmSignature(Provider, MyDigestAlgorithm); } else { //RETIRAR ESSA PARTE PARA IMPLEMENTAR OS DEMAIS MÉTODOS, OLHANDO OUTROS TIPOS DE CERTIFICADO rsa = (RSACryptoServiceProvider)cert.Certificado.PrivateKey; Provider = (RSACryptoServiceProvider)cert.Certificado.PrivateKey; externalSignature = new AsymmetricAlgorithmSignature(Provider, MyDigestAlgorithm); } if (AddTimeStamper) { tsaClient = new TSAClientBouncyCastle(urlTimeStamper, timeStampUser, timeStampPass); } OCSPVerifier ocspVerifier = new OCSPVerifier(null, null); ocspClient = new OcspClientBouncyCastle(ocspVerifier); if (AplicaPolitica) { SignaturePolicyInfo spi = getPolitica(); objStamper.SignDetached(externalSignature, Arraychain, crlList, ocspClient, tsaClient, 0, PdfSigner.CryptoStandard.CADES, spi); } else { objStamper.SignDetached(externalSignature, Arraychain, crlList, ocspClient, tsaClient, 0, PdfSigner.CryptoStandard.CADES); } try { signedPdf.Flush(); } catch { } try { signedPdf.Close(); } catch { }; pdfReader.Close(); }
public static void AssinaComCertificado(List <ICrlClient> crlList, byte[] File, out byte[] SignFile, CertSimples cert, int X, int Y, int Pagina, int Rotation, bool AddTimeStamper = true, string urlTimeStamper = "https://freetsa.org/tsr", string timeStampUser = "", string timeStampPass = "", string Reason = "Assinatura Digital", bool AplicaPolitica = false, string MyDigestAlgorithm = "SHA-256", string Contact = "", string Location = "Indústrias Nucleares do Brasil S/A - INB", string Creator = "Assinador da INB", TipoAssinatura Tipo = TipoAssinatura.Normal, string Cargo = "", string CREACRM = "") { int Largura = 140; int Altura = 63; MemoryStream ArquivoOrigem = new MemoryStream(File); PdfReader pdfReader = new PdfReader(ArquivoOrigem); MemoryStream signedPdf = new MemoryStream(); StampingProperties osp = new StampingProperties(); osp.UseAppendMode(); PdfSigner objStamper = new PdfSigner(pdfReader, signedPdf, osp); ITSAClient tsaClient = null; IOcspClient ocspClient = null; ConfiguraAparencia(objStamper, cert, X, Y, Largura, Altura, Pagina, Rotation, Contact, Reason, Location, Creator, Tipo, Cargo, CREACRM); Org.BouncyCastle.X509.X509Certificate vert = Org.BouncyCastle.Security.DotNetUtilities.FromX509Certificate(cert.Certificado); Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser(); Org.BouncyCastle.X509.X509Certificate[] Arraychain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(cert.Certificado.RawData) }; X509CertificateParser objCP = new X509CertificateParser(); RSACryptoServiceProvider rsa; RSACryptoServiceProvider Provider; IExternalSignature externalSignature; if (cert.Certificado.PrivateKey is RSACryptoServiceProvider) { rsa = (RSACryptoServiceProvider)cert.Certificado.PrivateKey; Provider = (RSACryptoServiceProvider)cert.Certificado.PrivateKey; externalSignature = new AsymmetricAlgorithmSignature(Provider, MyDigestAlgorithm); } else { RSA rsaTeste = cert.Certificado.GetRSAPrivateKey(); rsa = (RSACryptoServiceProvider)cert.Certificado.PrivateKey; Provider = (RSACryptoServiceProvider)cert.Certificado.PrivateKey; externalSignature = new AsymmetricAlgorithmSignature(Provider, MyDigestAlgorithm); } if (AddTimeStamper) { tsaClient = new TSAClientBouncyCastle(urlTimeStamper, timeStampUser, timeStampPass); } OCSPVerifier ocspVerifier = new OCSPVerifier(null, null); ocspClient = new OcspClientBouncyCastle(ocspVerifier); if (AplicaPolitica) { SignaturePolicyInfo spi = getPolitica(); objStamper.SignDetached(externalSignature, Arraychain, crlList, ocspClient, tsaClient, 0, PdfSigner.CryptoStandard.CADES, spi); } else { objStamper.SignDetached(externalSignature, Arraychain, crlList, ocspClient, tsaClient, 0, PdfSigner.CryptoStandard.CADES); } try { SignFile = signedPdf.ToArray(); try { signedPdf.Close(); signedPdf.Dispose(); } catch { } } catch (Exception ex) { SignFile = null; throw ex; } try { signedPdf.Close(); } catch (Exception ex) { } pdfReader.Close(); }