コード例 #1
0
        public void SignFilePDF(FileStream file, string access_token, string otp, string pin)
        {
            SBUtils.Unit.SetLicenseKey("03D250F599AFD170E8A7410AFE3EAAC635E687187762F9936518B7FA6AEDDB215DF3177560DD647433BEE43711D31EC2B6818C0797C464E7F077467EABB466DE8F21CE77A054C9D3B04B0BA859B4BE8E8B7FCD50D07E2A4CD96240FA1468D8F03CBDE4EB1D2070A4294D2426881EEFBDFFAA7A76747B30A2E0564CA06CD673089318BFBA530E88A26F6FF76E46FE2A5A65C0FBAACB09F9804BC287412E49EE832058643D8A59B8398C7637C3EDE91660E6B696F32AD416F606DB215A2FFF214B5DF58DE27687362740B591D7F3D2D33CE6A3D1601521408511476FA81D374CA32D0443BD710D4D732A8C398A953047EEAB4A62237813DA11FC5E0EBFF1E69A9D");


            TElPDFDocument doc = new TElPDFDocument();

            try
            {
                doc.Open(file);
                try
                {
                    OTP          = otp;
                    PIN          = pin;
                    Access_token = access_token;



                    //// doc.Open(docStream);
                    SBCustomCertStorage.TElCustomCertStorage certChain;


                    try
                    {
                        int index = doc.AddSignature();

                        TElPDFSignature sig = doc.get_Signatures(index);

                        sig.SigningTime = DateTime.UtcNow;

                        TElPDFAdvancedPublicKeySecurityHandler handler = new TElPDFAdvancedPublicKeySecurityHandler();

                        handler.CustomName = "Adobe.PPKLite";
                        handler.IgnoreChainValidationErrors = true;
                        handler.CertStorage        = LoadCertificate("863971CBC7BF63D49C9F14809FD5A1142B75E9AB", Access_token);
                        handler.PAdESSignatureType = TSBPAdESSignatureType.pastEnhanced;
                        //handler.OfflineMode = true;
                        handler.HashAlgorithm                   = SBConstants.__Global.SB_ALGORITHM_DGST_SHA256;
                        handler.RemoteSigningMode               = true;
                        handler.RemoteSigningCertIndex          = 0;
                        handler.SignatureType                   = TSBPDFPublicKeySignatureType.pstPKCS7SHA1;
                        handler.SignatureSizeEstimationStrategy = TSBPAdESSignatureSizeEstimationStrategy.psesSmart;
                        handler.OnBeforeSign += new TSBPDFSignEvent(OnBefore);
                        handler.OnRemoteSign += new TSBPDFRemoteSignEvent(PAdESHandler_OnRemoteSign);
                        handler.OnAfterSign  += new TSBPDFSignEvent(OnAfter);

                        sig.Handler = handler;

                        doc.Close(true);
                    }
                    catch (Exception ex) { }
                }
                catch (Exception ex) { }
            }
            catch (Exception ex) { }
        }
コード例 #2
0
        public bool SignPDF(Stream stream, string accessToken, string OTP, string PIN, string credentialID, string base_URL, string hash_algo, string sign_algo)
        {
            SBUtils.Unit.SetLicenseKey("03D250F599AFD170E8A7410AFE3EAAC635E687187762F9936518B7FA6AEDDB215DF3177560DD647433BEE43711D31EC2B6818C0797C464E7F077467EABB466DE8F21CE77A054C9D3B04B0BA859B4BE8E8B7FCD50D07E2A4CD96240FA1468D8F03CBDE4EB1D2070A4294D2426881EEFBDFFAA7A76747B30A2E0564CA06CD673089318BFBA530E88A26F6FF76E46FE2A5A65C0FBAACB09F9804BC287412E49EE832058643D8A59B8398C7637C3EDE91660E6B696F32AD416F606DB215A2FFF214B5DF58DE27687362740B591D7F3D2D33CE6A3D1601521408511476FA81D374CA32D0443BD710D4D732A8C398A953047EEAB4A62237813DA11FC5E0EBFF1E69A9D");

            pin = PIN; otp = OTP; credentialsID = credentialID; access_token = accessToken; baseURL = base_URL; hashAlgo = hash_algo; signAlgo = sign_algo;
            TElPDFDocument document = new TElPDFDocument();

            try
            {
                document.Open(stream);
                try
                {
                    int index = document.AddSignature();

                    TElPDFSignature signature = document.get_Signatures(index);

                    signature.SigningTime = DateTime.Now;

                    TElPDFAdvancedPublicKeySecurityHandler handler = new TElPDFAdvancedPublicKeySecurityHandler();

                    handler.CustomName = "Adobe.PPKLite";
                    handler.IgnoreChainValidationErrors = true;
                    handler.CertStorage        = LoadCertificate(credentialsID, access_token);
                    handler.PAdESSignatureType = TSBPAdESSignatureType.pastEnhanced;
                    if (hashAlgo == "2.16.840.1.101.3.4.2.1")
                    {
                        handler.HashAlgorithm = SBConstants.__Global.SB_ALGORITHM_DGST_SHA256;
                    }
                    else
                    {
                        handler.HashAlgorithm = SBConstants.__Global.SB_ALGORITHM_DGST_SHA1;
                    }

                    handler.RemoteSigningMode               = true;
                    handler.RemoteSigningCertIndex          = 0;
                    handler.SignatureType                   = TSBPDFPublicKeySignatureType.pstPKCS7SHA1;
                    handler.SignatureSizeEstimationStrategy = TSBPAdESSignatureSizeEstimationStrategy.psesSmart;
                    handler.OnRemoteSign += new TSBPDFRemoteSignEvent(PAdESHandler_OnRemoteSign);

                    signature.Handler = handler;

                    document.Close(true);

                    return(true);
                }
                catch (Exception ex) { return(false); }
            }
            catch (Exception ex) { return(false); }
        }
コード例 #3
0
        public async Task <object> SignPDFAsync(string accessToken, string base_URL, InputSignatureAdvanced inputSignatureAdvanced)
        {
            SBUtils.Unit.SetLicenseKey("03D250F599AFD170E8A7410AFE3EAAC635E687187762F9936518B7FA6AEDDB215DF3177560DD647433BEE43711D31EC2B6818C0797C464E7F077467EABB466DE8F21CE77A054C9D3B04B0BA859B4BE8E8B7FCD50D07E2A4CD96240FA1468D8F03CBDE4EB1D2070A4294D2426881EEFBDFFAA7A76747B30A2E0564CA06CD673089318BFBA530E88A26F6FF76E46FE2A5A65C0FBAACB09F9804BC287412E49EE832058643D8A59B8398C7637C3EDE91660E6B696F32AD416F606DB215A2FFF214B5DF58DE27687362740B591D7F3D2D33CE6A3D1601521408511476FA81D374CA32D0443BD710D4D732A8C398A953047EEAB4A62237813DA11FC5E0EBFF1E69A9D");
            pin = inputSignatureAdvanced.PIN; otp = inputSignatureAdvanced.OTP; credentialsID = inputSignatureAdvanced.credentialsID; access_token = accessToken; baseURL = base_URL; hashAlgo = inputSignatureAdvanced.hashAlgo; signAlgo = inputSignatureAdvanced.signAlgo;

            OutputError error = new OutputError()
            {
                error             = "error_pades_signature",
                error_description = "error"
            };
            TElPDFDocument document = new TElPDFDocument();

            try
            {
                var filePath = Path.GetTempFileName();
                if (inputSignatureAdvanced.documentStream.Length > 0)
                {
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        Stream memoryStream = new MemoryStream(inputSignatureAdvanced.documentStream);

                        await memoryStream.CopyToAsync(stream);

                        document.Open(stream);
                        try
                        {
                            int index = document.AddSignature();

                            TElPDFSignature signature = document.get_Signatures(index);

                            signature.SigningTime = DateTime.Now;

                            TElPDFAdvancedPublicKeySecurityHandler handler = new TElPDFAdvancedPublicKeySecurityHandler();

                            handler.CustomName = "Adobe.PPKLite";
                            handler.IgnoreChainValidationErrors = true;
                            handler.CertStorage        = LoadCertificate(credentialsID, access_token);
                            handler.PAdESSignatureType = TSBPAdESSignatureType.pastEnhanced;
                            if (hashAlgo == "2.16.840.1.101.3.4.2.1")
                            {
                                handler.HashAlgorithm = SBConstants.__Global.SB_ALGORITHM_DGST_SHA256;
                            }
                            else
                            {
                                handler.HashAlgorithm = SBConstants.__Global.SB_ALGORITHM_DGST_SHA1;
                            }
                            handler.RemoteSigningMode               = true;
                            handler.RemoteSigningCertIndex          = 0;
                            handler.SignatureType                   = TSBPDFPublicKeySignatureType.pstPKCS7SHA1;
                            handler.SignatureSizeEstimationStrategy = TSBPAdESSignatureSizeEstimationStrategy.psesSmart;
                            handler.OnRemoteSign += new TSBPDFRemoteSignEvent(PAdESHandler_OnRemoteSign);

                            signature.Handler = handler;

                            document.Close(true);

                            document.Dispose();
                            stream.Close();
                            stream.Dispose();

                            memoryStream.Close();
                            memoryStream.Dispose();


                            var signedMemory = new MemoryStream();
                            using (FileStream signedStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                            {
                                await signedStream.CopyToAsync(signedMemory);

                                byte[] result = signedMemory.GetBuffer();

                                OutputAdvancedSignature output = new OutputAdvancedSignature()
                                {
                                    signedDocument = result
                                };

                                return(output);
                            }
                        }

                        catch (Exception ex) { return(error); }
                    }
                }
                else
                {
                    return(error);
                }
            }
            catch (Exception ex)
            {
                return(error);
            }
        }