EldoS | Feel safer!

Software components for data protection, secure storage and transfer

CAdES signature for PDF,doc,docx files?

Also by EldoS: CallbackProcess
A component to control process creation and termination in Windows and .NET applications.
#21859
Posted: 10/08/2012 02:43:38
by Adi Vasi (Basic support level)
Joined: 09/27/2012
Posts: 19

Hi all,

I'm trying to sign a PDF document using CAdES. I have read that when using CAdES, the original data can be of any kind, including a PDF document. First I created a CAdES-BES signature using TElCAdESSignatureProcessor.CreateBES(cert,chainCerts) method. Afer that I serialized the signature. Then using CreateT() method I updated the signature to CAdES-T. Now when I'm trying to serialize again the signature for calculating the time stamping I got an exception on signer.save(stream) that sound like :"Cannot access a closed Stream." and the stacktrace :
at System.IO.__Error.StreamIsClosed()
at System.IO.MemoryStream.set_Position(Int64 value)
at SBASN1Tree.TElASN1DataSource.Read(Byte[] Buffer, Int32 StartIndex, Int32 Size, Int64 Offset)
at SBASN1Tree.TElASN1SimpleTag.SaveToStream(Stream Stream)
at SBASN1Tree.TElASN1ConstrainedTag.SaveToStream(Stream Stream)
at SBASN1Tree.TElASN1ConstrainedTag.SaveToStream(Stream Stream)
at SBASN1Tree.TElASN1ConstrainedTag.SaveToStream(Stream Stream)
at SBASN1Tree.TElASN1ConstrainedTag.SaveToStream(Stream Stream)
at SBASN1Tree.TElASN1ConstrainedTag.SaveToStream(Stream Stream)
at SBPKCS7.TElPKCS7Message.SaveToStream(Stream Stream)
at SBCMS.TElCMSMessage.Save(Stream DestStream)

Here is the sample code :

Code
signer = new TElSignedCMSMessage();
//PKCS#7 compatibility
signer.ContentType = SBPKCS7.Unit.SB_OID_PKCS7_DATA;
signer.ContentVersion = 1;
signer.UseUndefSize = false;

signer.CreateNew(input, 0, 0);

Cert = SystemStore.get_Certificates(index);
chainCerts = new TElMemoryCertStorage();
chainCerts.Add(Cert, false);

SignatureIndex = signer.AddSignature();
NewSignature = signer.get_Signatures(SignatureIndex);

cadesProcessor = new TElCAdESSignatureProcessor(NewSignature);

cadesProcessor.CreateBES(Cert, chainCerts);

ms = new MemoryStream();
signer.Save(ms);
ms.Position = 0;
signer.Open(ms, null, ms.Length, 0);
ms.Close();
ms.Dispose();
ms = null;

NewSignature = signer.get_Signatures(SignatureIndex);
cadesProcessor = new TElCAdESSignatureProcessor(NewSignature);

if (cadesProcessor.CanUpgradeToT())
{                                      
  cadesProcessor.UpgradeToT(TSPClient);
  ms1 = new MemoryStream();
  signer.Save(ms1);///error!!! -->"Cannot access a closed stream"
  ms1.Position = 0;
  signer.Open(ms1, null, 0, 0);
  ms1.Dispose();
  ms1 = null;

  NewSignature = signer.get_Signatures(SignatureIndex);
  cadesProcessor = new TElCAdESSignatureProcessor(NewSignature);
}


I got the same error for *.docx and *.doc files.It works only for *.txt and *.xml files.Do you know what can be the problem?

Thanks,
Vasi.
#21860
Posted: 10/08/2012 02:49:34
by Vsevolod Ievgiienko (EldoS Corp.)

Thank you for contacting us.

I think the problem is here:
Code
...
signer.Open(ms, null, ms.Length, 0);
ms.Close();
ms.Dispose();
ms = null;
...

You should not close the stream after Open is called because the signer will use this stream later.
#21861
Posted: 10/08/2012 02:55:00
by Adi Vasi (Basic support level)
Joined: 09/27/2012
Posts: 19

Thanks Vsevolod,
It works.

All the best,
Vasi.

Reply

Statistics

Topic viewed 927 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!