Beispiel #1
0
        public byte[] signWithPfxFile(string pfxFile, string pinCode, byte[] tobeSignBytes)
        {
            BaseSignedData bs = new BaseSignedData();

            tr.gov.tubitak.uekae.esya.api.cmssignature.ISignable content = new SignableByteArray(tobeSignBytes);
            bs.addContent(content);

            //Since SigningTime attribute is optional,add it to optional attributes list
            List <IAttribute> optionalAttributes = new List <IAttribute>();

            optionalAttributes.Add(new SigningTimeAttr(DateTime.UtcNow));

            Dictionary <string, object> params_ = new Dictionary <string, object>();
            ValidationPolicy            policy  = getPolicy();

            //necessary for certificate validation.By default,certificate validation is done
            params_[EParameters.P_CERT_VALIDATION_POLICY] = policy;

            //if the user does not want certificate validation,he can add
            //P_VALIDATE_CERTIFICATE_BEFORE_SIGNING parameter with its value set to false
            params_[EParameters.P_VALIDATE_CERTIFICATE_BEFORE_SIGNING] = false;
            PfxSigner    signer = new PfxSigner(SignatureAlg.RSA_SHA256.getName(), pfxFile, pinCode);
            ECertificate signatureCertificate = signer.getSignersCertificate();

            bs.addSigner(ESignatureType.TYPE_BES, signatureCertificate, signer, optionalAttributes, params_);
            return(bs.getEncoded());
        }
Beispiel #2
0
        public byte[] signPdfWithPfxFile(string pfxFile, string pinCode, string pdfFileName)
        {
            PfxSigner    signer = new PfxSigner(SignatureAlg.RSA_SHA256.getName(), pfxFile, pinCode);
            ECertificate signatureCertificate = signer.getSignersCertificate();
            Pkcs12Store  store = new Pkcs12Store(new FileStream(pfxFile, FileMode.Open), pinCode.ToCharArray());
            String       alias = "";
            string       dest  = AppDomain.CurrentDomain.BaseDirectory + "\\tmp.pdf";

            if (File.Exists(dest))
            {
                File.Delete(dest);
            }
            ICollection <Org.BouncyCastle.X509.X509Certificate> chain = new List <Org.BouncyCastle.X509.X509Certificate>();

            // searching for private key
            foreach (string al in store.Aliases)
            {
                if (store.IsKeyEntry(al) && store.GetKey(al).Key.IsPrivate)
                {
                    alias = al;
                    break;
                }
            }

            AsymmetricKeyEntry pk = store.GetKey(alias);

            foreach (X509CertificateEntry c in store.GetCertificateChain(alias))
            {
                chain.Add(c.Certificate);
            }

            RsaPrivateCrtKeyParameters parameters = pk.Key as RsaPrivateCrtKeyParameters;

            // Creating the reader and the stamper
            PdfReader  reader  = new PdfReader(pdfFileName);
            FileStream os      = new FileStream(dest, FileMode.Create);
            PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '\0');
            // Creating the appearance
            PdfSignatureAppearance appearance = stamper.SignatureAppearance;

            appearance.Reason   = "";
            appearance.Location = "";
            //appearance.SetVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");//don't show rectangle on pdf
            // Creating the signature
            IExternalSignature pks = new PrivateKeySignature(parameters, DigestAlgorithms.SHA256);

            MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, CryptoStandard.CADES);
            byte[] buffer = File.ReadAllBytes(dest);
            File.Delete(dest);
            return(buffer);
        }