EldoS | Feel safer!

Software components for data protection, secure storage and transfer

"Bad asynchronous data" trying to sign CMS with DC in ASP.NET

Also by EldoS: CallbackRegistry
A component to monitor and control Windows registry access and create virtual registry keys.
#35330
Posted: 12/31/2015 10:26:53
by javier aranda (Basic support level)
Joined: 12/30/2015
Posts: 10

I am evaluating secureblackbox as capicom replacement.

I am trying to sign a string with Java DC applet, i have reviewed the samples and documentation. I get "bad asynchronous data" when completeasyncsign is called.

PreSign:
Code
public class PreSigner : System.Web.UI.Page
    {
        protected MemoryStream output;

        public const string plainText =
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

        protected virtual void Page_Load(object sender, EventArgs e)
        {
            SBUtils.Unit.SetLicenseKey("my trial license");

            TElDCAsyncState state = null;

            int sigIndex = 0;

            TElSignedCMSMessage cms = new TElSignedCMSMessage();
            var plainBytes = Encoding.UTF8.GetBytes(plainText);
            cms.CreateNew(plainBytes, 0, plainBytes.Length);

            sigIndex = cms.AddSignature();

            cms.GetSignature(sigIndex).DigestAlgorithm = SBConstants.Unit.SB_ALGORITHM_DGST_SHA1;
            cms.GetSignature(sigIndex).SigningOptions = cms.GetSignature(sigIndex).SigningOptions & (~SBCMS.__Global.csoUseGeneralizedTimeFormat);
            cms.GetSignature(sigIndex).SigningTime = DateTime.Now;
            cms.GetSignature(sigIndex).PublicKeyAlgorithm = SBConstants.Unit.SB_CERT_ALGORITHM_SHA1_RSA_ENCRYPTION;

            cms.GetSignature(sigIndex).InitiateAsyncSign(ref state);

            TElDCBaseMessage Msg = state.FindMessageByType(TElDCOperationRequestMessage.MetaClass.Instance);
            if (Msg != null)
                ((TElDCOperationRequestMessage)Msg).IncludeKeysInResponse = true;
        
            output = new MemoryStream();
            TElDCXMLEncoding encoder = new TElDCXMLEncoding();

            state.SaveToStream(output, encoder);
            cms.Close();
            state.Dispose();
        }
    }


Result:
Code
   public partial class Result : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            SBUtils.Unit.SetLicenseKey("my trial key");

            byte[] signature = new byte[Request.InputStream.Length];
            Request.InputStream.Read(signature, 0, signature.Length);
            FinishSigning(PreSigner.plainText, signature);
        }


        protected void FinishSigning(String text, byte[] signature)
        {
            TElDCAsyncState state = new TElDCAsyncState();
            try
            {
                MemoryStream input = new MemoryStream(signature);
                state.LoadFromStream(input, SBDCXMLEnc.__Global.DCXMLEncoding());

                TElSignedCMSMessage cms = new TElSignedCMSMessage();
                var plainBytes = Encoding.UTF8.GetBytes(text);
                cms.CreateNew(plainBytes, 0, plainBytes.Length);

                int sigIndex = 0;
                try {
                    sigIndex = cms.AddSignature();
                    cms.GetSignature(sigIndex).CompleteAsyncSign(state);
                }
                finally
                {
                    cms.GetSignature(sigIndex).Dispose();
                }

            }
            finally
            {
                state.Dispose();
            }
        }
}


#35332
Posted: 12/31/2015 10:45:44
by Dmytro Bogatskyy (EldoS Corp.)

Thank you for contacting us,

Quote
I get "bad asynchronous data" when completeasyncsign is called.
Code
protected void FinishSigning(String text, byte[] signature)
...
cms.CreateNew(plainBytes, 0, plainBytes.Length);
...


In your PreSigner class, after calling InitiateAsyncSign() method you should save the CMS message to a temporary file.
Then, in the FinishSigning() method you should load this CMS message and complete async operation (CompleteAsyncSign() method).
#35333
Posted: 12/31/2015 11:13:53
by javier aranda (Basic support level)
Joined: 12/30/2015
Posts: 10

Dmytro,

Thanks for yout quick answer,

I have modified my code, same error.

PreSign:
Code
...
            cms.GetSignature(sigIndex).InitiateAsyncSign(ref state);

            var fileStream = File.Create("C:\\test.txt");
            cms.Save(fileStream);
            fileStream.Close();
...


FinishSigning
Code
        protected void FinishSigning(String text, byte[] signature)
        {
            TElDCAsyncState state = new TElDCAsyncState();
            try
            {
                MemoryStream input = new MemoryStream(signature);
                state.LoadFromStream(input, SBDCXMLEnc.__Global.DCXMLEncoding());
                
                TElSignedCMSMessage cms = new TElSignedCMSMessage();
                FileStream fs = File.OpenRead("C:\\test.txt");
                cms.Open(fs, null);
                fs.Close();
                
                int sigIndex = 0;
                try {
                    sigIndex = cms.AddSignature();
                    cms.GetSignature(sigIndex).CompleteAsyncSign(state);
                }
                finally
                {
                    cms.GetSignature(sigIndex).Dispose();
                }

            }
            finally
            {
                state.Dispose();
            }
        }


┬┐any ideas?
#35334
Posted: 12/31/2015 11:16:35
by Eugene Mayevski (EldoS Corp.)

I'd like to step back a bid and check the following thing -- did you try to run the existing samples for PDF or XML to check that your configuration generally works? This way we will narrow down the problem to either your code, our code or the system configuration. It can happen that the browser breaks/strips the payload, or that Javascript misbehaves or something like that, so it's important to ensure that the samples work.


Sincerely yours
Eugene Mayevski
#35335
Posted: 12/31/2015 11:21:21
by javier aranda (Basic support level)
Joined: 12/30/2015
Posts: 10

Eugene,

I have tested PDF signing sample and is working fine on my development machine with windows 7.

Thanks for your help.
#35337
Posted: 12/31/2015 11:32:53
by javier aranda (Basic support level)
Joined: 12/30/2015
Posts: 10

It SUCCEED, had to remove this line at FinishSigning()

Code
  sigIndex = cms.AddSignature();


Thanks for your help.
#35338
Posted: 12/31/2015 11:35:54
by Eugene Mayevski (EldoS Corp.)

Thank you for letting us know about the resolution! This will save our time and hopefully will help other users later.


Sincerely yours
Eugene Mayevski
Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.

Reply

Statistics

Topic viewed 2020 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!