EldoS | Feel safer!

Software components for data protection, secure storage and transfer

How to use ElFileTSPClient to timestamp PDF file?

Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages
#7101
Posted: 07/29/2008 05:07:22
by Dan Saul (Standard support level)
Joined: 07/23/2008
Posts: 17

Hi,

I'm new to ASP.NET (VB) and am having trouble timestamping a PDF file using PDFBlackBox without SSLBlackBox. I've successfully managed to add a digital signature and timestamp using TElHTTPSClient and TElHTTPTSPClient but cannot do it using TElFileTSPClient.

Eldos support said that "You can use TElFileTSPClient to create and parse tsp HttpWebRequest request. All you have to do is create an event handler for ElFileTSPClient.OnTimestampNeeded and send/receive data using .net classes to your remote timestamping host and get the reply."

They also referenced this link but I still don't understand how to code it: [URL=http://www.eldos.com/documentation/sbb/documentation/ref_howto_pdf_sign_timestamp.html]http://www.eldos.com/documentation/sbb/documentation/ref_howto_pdf_sign_timestamp.html[/URL]

I'd really appreciate it if someone could get me started with the VB code for this.

Many thanks,

Dan

FYI: Here is the other timestamp call code that works (but I need SSLBlackBox for this)

Code
Private HTTPClient As TElHTTPSClient = New TElHTTPTSPClient
Private TSPClient As TElHTTPTSPClient = New TElHTTPTSPClient

TSPClient.HTTPClient = HTTPClient
TSPClient.HashAlgorithm = SBConstants.Unit.SB_ALGORITHM_DGST_SHA1
TSPClient.URL = "http://tsatest1.digistamp.com/TSA"
TSPClient.HTTPClient.RequestParameters.Username = "username"
TSPClient.HTTPClient.RequestParameters.Password = "password"
PublicKeyHandler.TSPClient = TSPClient
#7106
Posted: 07/29/2008 07:22:37
by Ken Ivanov (EldoS Corp.)

The TElFileTSPClient object should be set up in the following way:

Code
    Private Sub PrepareTSPClient()
        Dim TSPClient As New TElFileTSPClient
        AddHandler TSPClient.OnTimestampNeeded, AddressOf HandleTimestampNeeded
    End Sub

    Private Sub HandleTimestampNeeded(ByVal Sender As Object, ByVal reqStream As Stream, ByVal respStream As Stream, ByRef Succeeded As Boolean)
        ' (1) sending reqStream to timestamp server
        ' and
        ' (2) writing the received response to the respStream,
        ' then
        ' (3) setting Succeeded to True if the response has been received correctly,
        ' or to False if some error has occured during negotiation to timestamping server
    End Sub
#7108
Posted: 07/29/2008 08:35:35
by Dan Saul (Standard support level)
Joined: 07/23/2008
Posts: 17

Hi Innokentiy. Thank you very much for your response. I have now declared the TElFileTSPClient and handler and have coded the HandleTimestampNeeded event but now get an "object reference not set to an instance of an object" error. Please advise.

Code
Private Sub TimeStamp()

// all the certificate and pdf signing code then...

                    ' adding certificate to certificate storage
                    CertStorage.Clear()
                    CertStorage.Add(Cert, True)
                    PublicKeyHandler.CertStorage = CertStorage
                    PublicKeyHandler.SignatureType = TSBPDFPublicKeySignatureType.pstPKCS7SHA1
                    PublicKeyHandler.CustomName = "Adobe.PPKMS"

                    ' timestamping the signature
                    TSPClient.HashAlgorithm = SBConstants.Unit.SB_ALGORITHM_DGST_SHA1
                    PublicKeyHandler.TSPClient = TSPClient
End Sub


Code
    Private Sub HandleTimestampNeeded(ByVal Sender As Object, ByVal reqStream As Stream, ByVal respStream As Stream, ByRef Succeeded As Boolean)

        Dim tspHTTPRequest As HttpWebRequest
        Dim tspHTTPResponse As HttpWebResponse

        ' (1) sending reqStream to timestamp server
        tspHTTPRequest = WebRequest.Create("http://tsatest1.digistamp.com/TSA")
        tspHTTPRequest.Method = "POST"
        tspHTTPRequest.Timeout = 10000
        tspHTTPRequest.ContentType = "application/timestamp-query"
        tspHTTPRequest.Credentials = New NetworkCredential("username", "password")

        reqStream = tspHTTPRequest.GetRequestStream()

        ' (2) writing the received response to the respStream
        tspHTTPResponse = tspHTTPRequest.GetResponse()
        respStream = tspHTTPResponse.GetResponseStream()

        ' (3) setting Succeeded to True if the response has been received correctly,
        ' or to False if some error has occured during negotiation to timestamping server
        If tspHTTPResponse.StatusCode = HttpStatusCode.OK Then
            Succeeded = True
        Else
            Succeeded = False
        End If

    End Sub
#7110
Posted: 07/29/2008 08:54:43
by Ken Ivanov (EldoS Corp.)

Code
respStream = tspHTTPResponse.GetResponseStream()

Please note, that you should *copy* the response to the stream instance assigned to the respStream parameter (not assign the result of GetResponseStream() to it). You can use the built-in CopyStream() method to do it:
Code
SBUtils.Unit.CopyStream(tspHTTPResponse.GetResponseStream(), respStream)
respStream.Position = 0


If the error persists after these changes, please post the exact call stack here. This will help us find out the exact reason for the issue.
#7111
Posted: 07/29/2008 09:12:26
by Dan Saul (Standard support level)
Joined: 07/23/2008
Posts: 17

Should I use the same copy method for the ReqStream?
#7112
Posted: 07/29/2008 09:23:10
by Ken Ivanov (EldoS Corp.)

ReqStream contains the data that should be sent to the timestamp server (timestamp request).
#7113
Posted: 07/29/2008 10:11:14
by Dan Saul (Standard support level)
Joined: 07/23/2008
Posts: 17

What I mean is should I also write:
Code
SBUtils.Unit.CopyStream(tspHTTPRequest.GetRequestStream(), reqStream)
reqStream.Position = 0


instead of:

Code
reqStream = tspHTTPRequest.GetRequestStream()
#7115
Posted: 07/29/2008 10:28:00
by Ken Ivanov (EldoS Corp.)

No. Please use the following code:
Code
Dim newStream as Stream = tspHTTPRequest.GetRequestStream()
Dim buf(65536) As Byte
Dim read as integer
reqStream.Position = 0
While true
  read = reqStream.Read(buf, 0, buf.Length)
  newStream.Write(buf, 0, read)
  If read < buf.Length Then
    Exit While
  End If
End While
newStream.Close()
#7116
Posted: 07/29/2008 11:12:07
by Dan Saul (Standard support level)
Joined: 07/23/2008
Posts: 17

Okay, I still get the error even without any code in the event handler so I am going to go back and look at the rest of the code and maybe post the whole page here if I can't figure it out.

In the meantime, here is the event handler code, please let me know if this is now correct. Thanks again.

Code
Private Sub HandleTimestampNeeded(ByVal Sender As Object, ByVal reqStream As Stream, ByVal respStream As Stream, ByRef Succeeded As Boolean)

        Dim tspHTTPRequest As HttpWebRequest
        Dim tspHTTPResponse As HttpWebResponse
        Dim tspResponseStream As Stream

        ' (1) sending reqStream to timestamp server
        tspHTTPRequest = WebRequest.Create("http://tsatest1.digistamp.com/TSA")
        tspHTTPRequest.Method = "POST"
        tspHTTPRequest.Timeout = 10000
        tspHTTPRequest.ContentType = "application/timestamp-query"
        tspHTTPRequest.Credentials = New NetworkCredential("username", "password")

        Dim newStream As Stream = tspHTTPRequest.GetRequestStream()
        Dim buf(65536) As Byte
        Dim read As Integer
        reqStream.Position = 0
        While True
            read = reqStream.Read(buf, 0, buf.Length)
            newStream.Write(buf, 0, read)
            If read < buf.Length Then
                Exit While
            End If
        End While
        newStream.Close()

        ' (2) writing the received response to the respStream
        tspHTTPResponse = tspHTTPRequest.GetResponse()
        tspResponseStream = tspHTTPResponse.GetResponseStream()
        SBUtils.Unit.CopyStream(tspResponseStream, respStream, tspResponseStream.Length)
        respStream.Position = 0

        ' (3) setting Succeeded to True if the response has been received correctly,
        ' or to False if some error has occured during negotiation to timestamping server
        If tspHTTPResponse.StatusCode = HttpStatusCode.OK Then
            Succeeded = True
        Else
            Succeeded = False
        End If

    End Sub
#7118
Posted: 07/29/2008 11:30:34
by Ken Ivanov (EldoS Corp.)

Please post the call stack that takes place when the exception is thrown. This should help finding out the exact reason.
Also by EldoS: CallbackRegistry
A component to monitor and control Windows registry access and create virtual registry keys.

Reply

Statistics

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