public static void SignPdfCert(String SRC, String DEST, String Reason, String Location, String certPassword, String certFile, String llx, String lly, String urx, String ury, int fontSize) { Pkcs12Store p12ks = new Pkcs12Store(); FileStream fs = new FileStream(certFile, FileMode.Open); p12ks.Load(fs, certPassword.ToCharArray()); String alias = ""; foreach (String al in p12ks.Aliases) { if (p12ks.IsKeyEntry(al) && p12ks.GetKey(al).Key.IsPrivate) { alias = al; break; } } AsymmetricKeyParameter pk = p12ks.GetKey(alias).Key; ICollection<X509Certificate> chain = new List<X509Certificate>(); foreach (X509CertificateEntry entry in p12ks.GetCertificateChain(alias)) { chain.Add(entry.Certificate); } fs.Close(); //Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser(); //Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(cert.RawData) }; IExternalSignature externalSignature = new PrivateKeySignature(pk, DigestAlgorithms.SHA512); PdfReader pdfReader = new PdfReader(SRC); FileStream signedPdf = new FileStream(DEST, FileMode.Create); //the output pdf file Program.logLine("page size" + pdfReader.GetPageSize(1)); PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '\0'); PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance; //here set signatureAppearance at your will signatureAppearance.Reason = Reason; signatureAppearance.Location = Location; BaseFont bf = BaseFont.CreateFont(); signatureAppearance.Layer2Font = new Font(bf, fontSize); signatureAppearance.SetVisibleSignature(new Rectangle(float.Parse(llx), float.Parse(lly), float.Parse(urx), float.Parse(ury)), 1, "sig"); //signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION; MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS); //MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CADES); }
internal string UploadCertificateWithPrivateKeyInPKCS12Test(ParametersValidation validationRequest, out StepType stepType, out SoapException exc, out int timeout) { int special; var r = GetCommand <string>("UploadCertificateWithPrivateKeyInPKCS12", UploadCertificateWithPrivateKeyInPKCS12, validationRequest, true, out stepType, out exc, out timeout, out special); if (0 != special) { var pkcs12Binary = (byte[])validationRequest.ValidationRules.First(rule => rule.ParameterName == "CertWithPrivateKey").Value; var passphraseID = validationRequest.ValidationRules.First(rule => rule.ParameterName == "EncryptionPassphraseID").Value; var pkcs12Store = new Org.BouncyCastle.Pkcs.Pkcs12Store(); pkcs12Store.Load(new MemoryStream(pkcs12Binary), ((null != passphraseID) ? "DefaultPassword" : "").ToArray()); m_X509CertificateFromUploadPKCS12 = pkcs12Store.GetCertificate(pkcs12Store.Aliases.OfType <string>().First()).Certificate.GetEncoded(); m_X509CertificateFromUploadPKCS12Alias = (string)validationRequest.ValidationRules.First(rule => rule.ParameterName == "CertificationPathAlias").Value; m_UploadPKCS12 = pkcs12Binary; } return(r); }
public static void Main(String[] args) { Properties properties = new Properties(); properties.Load(new FileStream("c:/home/blowagie/key.properties", FileMode.Open)); String path = properties["PRIVATE"]; char[] pass = properties["PASSWORD"].ToCharArray(); Pkcs12Store ks = new Pkcs12Store(); ks.Load(new FileStream(path, FileMode.Open), pass); String alias = ""; foreach (string al in ks.Aliases) { if (ks.IsKeyEntry(al) && ks.GetKey(al).Key.IsPrivate) { alias = al; break; } } AsymmetricKeyParameter pk = ks.GetKey(alias).Key; ICollection<X509Certificate> chain = new List<X509Certificate>(); foreach (X509CertificateEntry entry in ks.GetCertificateChain(alias)) { chain.Add(entry.Certificate); } IOcspClient ocspClient = new OcspClientBouncyCastle(); C3_01_SignWithCAcert.Sign(DEST, chain, pk, DigestAlgorithms.SHA256, CryptoStandard.CMS, "Test", "Ghent", null, ocspClient, null, 0); }
public Pkcs12Store LoadCAPfx(char[] password) { var keyStore = new Pkcs12Store(); using (var fs = new FileStream(CAKeyStore, FileMode.Open)) { keyStore.Load(fs, password); } return keyStore; }
internal static Pkcs12Store LoadCertificatesKeyStore(char[] password) { var keyStore = new Pkcs12Store(); using (var fs = new FileStream(Repository.Instance.CertificatesKeyStore, FileMode.Open)) { keyStore.Load(fs, password); } return keyStore; }
public static Pkcs12Store LoadCAPfx(char[] password) { var keyStore = new Pkcs12Store(); //PasswordWindow window = new PasswordWindow(); //if (window.ShowDialog() == true) { using (var fs = new FileStream(Repository.Instance.CAKeyStore, FileMode.Open)) { keyStore.Load(fs, password); } } return keyStore; }