Obtaining a timestamp

The following sample code demonstrates creation of a timestamp request, posting the request using PostRequestURL and verification of the result.

Supported Languages

C++

void ObtainingATimestamp()
{
      //As with most of the samples, exception handling has been omitted
 

      //Create a message to timestamp
      CPKIFBufferPtr msg(new CPKIFBuffer((unsigned char*)g_buf, g_bufSize)); 

      //Setup a message imprint structure to hash the message
      CPKIFMessageImprintPtr messageImprint(new CPKIFMessageImprint);
      messageImprint->HashAndSet(SHA1, msg); 

      //Create a timestamp request
      CPKIFTimeStampRequest req; 

      //Associate the messageImprint object with the request, use
      //a nonce for replay protection and request the TSA to
      //include its certificate in the response.
      req.SetMessageImprint(messageImprint);
      req.SetGenerateNonce(true);
      req.SetReqCert(true); 

      //Encode the request
      CPKIFBufferPtr encTSR = req.Encode(); 

      //Send the request to the server (as of 7/2004 Edelweb operated a TSA
      //for interoperability testing at the URL below).  Details are available at:
      // http://timestamping.edelweb.fr
      CPKIFBufferPtr resp;
      PostRequestURL(encTSR, resp, "http://www.edelweb.fr/cgi-bin/service-tsp", PKIF_TSA); 

      //Decode the response
      CPKIFTimeStampResponse tspResp;
      tspResp.Decode(resp); 

      //See if request was successful
      CPKIFPKIStatusInfoPtr psip = tspResp.GetStatus();
      PKIStatus s = psip->GetStatus();
      if(GRANTED == s || GRANTEDWITHMODS == s)
      {
            //Extract the timestamp token
            CPKIFContentInfoPtr ci = tspResp.GetTimeStampToken(); 

            //Create a mediator object
            IPKIFMediatorPtr m = MakeDefaultMediator(); 

            //Create a timestamp verification object, pass it the mediator
            //and set the minimum acceptable verification status for the TSA certificate
            CPKIFTimestampVerifier v;
            v.SetMediator(m);
            v.SetMinimumVerificationStatus(PVS_CERT_PATH_VERIFIED);
            try
            {
                  //Pass a message imprint of the original text
                  v.SetDataHash(messageImprint->GetHashedMessage(), SHA1);                 

                  //Verify the timestamp
                  v.Verify(ci); 

                  //Extract relevant details established during the verification,
                  //some of which will only be available if the verification was
                  //successful.
                  CMSVerificationStatus s = v.GetVerificationStatus();
                  CPKIFTimePtr t = v.GetTSADateTime();
                  CPKIFCertificatePtr c = v.GetTSACertificate();
                  CPKIFCertificatePathPtr p = v.GetCertificatePath();
                  CPKIFPathValidationResultsPtr pvr = v.GetPathValidationResults();
            }
            catch(CPKIFException& e)
            {
                  cout << "Unexpected exception thrown by ObtainingATimestamp: ";
                  cout << e.print()->c_str() << endl; 

                  return;
            }
      }
      else
      {
            //request was not successful
      }
}

C#

public void ObtainingATimestamp()
{
    //As with most of the samples, exception handling has been omitted
 

    //Create a message to timestamp
    CPKIFBufferPtr msg = pkif_module.StringToBuffer(g_buf); 

    //Setup a message imprint structure to hash the message
    CPKIFMessageImprintPtr messageImprint = pkif_module.make_CPKIFMessageImprintPtr();
    messageImprint.HashAndSet(HASH_ALG.SHA1, msg); 

    //Create a timestamp request
    CPKIFTimeStampRequest req = new CPKIFTimeStampRequest(); 

    //Associate the messageImprint object with the request, use
    //a nonce for replay protection and request the TSA to
    //include its certificate in the response.
    req.SetMessageImprint(messageImprint);
    req.SetGenerateNonce(true);
    req.SetReqCert(true);

    //Encode the request
    CPKIFBufferPtr encTSR = req.Encode(); 

    //Send the request to the server (as of 7/2004 Edelweb operated a TSA
    //for interoperability testing at the URL below).  Details are available at:
    // http://timestamping.edelweb.fr
    CPKIFBufferPtr resp = pkif_module.make_CPKIFBufferPtr();
    pkif_module.PostRequestURL(encTSR, resp, "http://www.edelweb.fr/cgi-bin/service-tsp", PKIFServiceType.PKIF_TSA); 

    //Decode the response
    CPKIFTimeStampResponse tspResp = new CPKIFTimeStampResponse();
    tspResp.Decode(resp); 

    //See if request was successful
    CPKIFPKIStatusInfoPtr psip = tspResp.GetStatus();
    PKIStatus s = psip.GetStatus();
    if (PKIStatus.GRANTED == s || PKIStatus.GRANTEDWITHMODS == s)
    {
        //Extract the timestamp token
        CPKIFContentInfoPtr ci = tspResp.GetTimeStampToken(); 

        //Create a mediator object
        IPKIFColleaguePtr m = pkif_module.MakeDefaultMediator(); 

        //Create a timestamp verification object, pass it the mediator
        //and set the minimum acceptable verification status for the TSA certificate
        CPKIFTimestampVerifier v = new CPKIFTimestampVerifier();
        v.SetMediator(m);
        v.SetMinimumVerificationStatus(CMSPathValidationStatus.PVS_CERT_PATH_VERIFIED);
        try
        {
            //Pass a message imprint of the original text
            v.SetDataHash(messageImprint.GetHashedMessage(), HASH_ALG.SHA1); 

            //Verify the timestamp
            v.Verify(ci); 

            //Extract relevant details established during the verification,
            //some of which will only be available if the verification was
            //successful.
            CMSVerificationStatus status = v.GetVerificationStatus();
            CPKIFTimePtr t = v.GetTSADateTime();
            CPKIFCertificatePtr c = v.GetTSACertificate();
            CPKIFCertificatePathPtr p = v.GetCertificatePath();
            CPKIFPathValidationResultsPtr pvr = v.GetPathValidationResults();
        }
        catch (Exception e)
        {
            Console.WriteLine("Unexpected exception thrown by ObtainingATimestamp: ");
            Console.WriteLine(e.Message);
            return;
        }
    }
    else
    {
        //request was not successful
    }
}

JAVA

public void ObtainingATimestamp()
{
   
//As with most of the samples, exception handling has been omitted
    //Create a message to timestamp
    CPKIFBufferPtr msg = pkif_module.StringToBuffer(g_buf); 

    //Setup a message imprint structure to hash the message
    CPKIFMessageImprintPtr messageImprint = pkif_module.make_CPKIFMessageImprintPtr();
    messageImprint.HashAndSet(HASH_ALG.
SHA1, msg); 

    //Create a timestamp request
    CPKIFTimeStampRequest req = new CPKIFTimeStampRequest(); 

    //Associate the messageImprint object with the request, use
    //a nonce for replay protection and request the TSA to
    //include its certificate in the response.
    req.SetMessageImprint(messageImprint);
    req.SetGenerateNonce(
true);
    req.SetReqCert(
true);

    //Encode the request
    CPKIFBufferPtr encTSR = req.Encode(); 

    //Send the request to the server (as of 7/2004 Edelweb operated a TSA
    //for interoperability testing at the URL below).  Details are available at:
    // http://timestamping.edelweb.fr
    CPKIFBufferPtr resp = pkif_module.make_CPKIFBufferPtr();
    pkif_module.PostRequestURL(encTSR, resp,
"http://www.edelweb.fr/cgi-bin/service-tsp", PKIFServiceType.PKIF_TSA); 

    //Decode the response
    CPKIFTimeStampResponse tspResp = new CPKIFTimeStampResponse();
    tspResp.Decode(resp);
 

    //See if request was successful
    CPKIFPKIStatusInfoPtr psip = tspResp.GetStatus();
    PKIStatus s = psip.GetStatus();
   
if (PKIStatus.GRANTED == s || PKIStatus.GRANTEDWITHMODS == s)
    {
       
//Extract the timestamp token
        CPKIFContentInfoPtr ci = tspResp.GetTimeStampToken();

        //Create a mediator object
        IPKIFColleaguePtr m = pkif_module.MakeDefaultMediator(); 

        //Create a timestamp verification object, pass it the mediator
        //and set the minimum acceptable verification status for the TSA certificate
        CPKIFTimestampVerifier v = new CPKIFTimestampVerifier();
        v.SetMediator(m);
        v.SetMinimumVerificationStatus(CMSPathValidationStatus.
PVS_CERT_PATH_VERIFIED);
       
try
        {
           
//Pass a message imprint of the original text
            v.SetDataHash(messageImprint.GetHashedMessage(), HASH_ALG.SHA1); 

            //Verify the timestamp
            v.Verify(ci); 

            //Extract relevant details established during the verification,
            //some of which will only be available if the verification was
            //successful.
            CMSVerificationStatus status = v.GetVerificationStatus();
            CPKIFTimePtr t = v.GetTSADateTime();
            CPKIFCertificatePtr c = v.GetTSACertificate();
            CPKIFCertificatePathPtr p = v.GetCertificatePath();
            CPKIFPathValidationResultsPtr pvr = v.GetPathValidationResults();
        }
       
catch (Exception e)
        {
            System.
out.println("Unexpected exception thrown by ObtainingATimestamp: ");
            System.
out.println(e.getMessage());

            return;
        }
    }
   
else
    {
       
//request was not successful
    }
}