SecureTime API Toolkit
Toolkit Home  

Example C Program: Time stamp data, Verify the time stamp


The program demonstrates time stamping an arbitrary data.   The hash of the data is then time stamped by the DigiStamp service using an Internet call.  The resulting time stamp token is decoded to extract the time stamp authorities time of signing.  The time stamp is then verified.

To execute the time stamp verfiy you will need to retrieve the public key from the DigiStamp web site and set up a DigiStamp account..

This example program illustrates the following tasks and combined use of the DigiStamp SecureTime API and MS CryptoAPI function..

  • MS CrypoAPI function is used to open the certificate store that contains the DigiStamp public time stamp keys.
  • Initializing a DGS_TIMESTAMP_PARA   time-stamp service structure to be used in a call to DgsTimeStampRequest
  • Time stamp the data with DgsTimeStampRequest
  • Verify the time stamp using using DgsTimeStampVerify
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Example code for time stamping a data object.

#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
#include "DgsTimeStamp.h"

#define dgs_timestamp_servers1 "TSATEST1"
#define dgs_timestamp_servers2 "TSATEST2"
#define dgs_timestamp_servers3 "TSA1"

#define CERTIFICATE_STORE_NAME L"MY"

//--------------------------------------------------------------------
//    Declare local functions.

void main(int argc,char **argv)
{
        //--------------------------------------------------------------------
        // Declare and initialize variables. This includes getting a pointer 
        // to the message content. This sample program creates the message 
        // content and gets a pointer to it. In most situations, 
        // the content will exist somewhere and a pointer to it
        // will get passed to the application. 
        
         //--------------------------------------------------------------------
         //   The message to be time stamped.
        BYTE* pbContent = (BYTE*) "I've enjoyed a very challenging day.";
        
        //--------------------------------------------------------------------
        //   Local utility variables
        unsigned int loopCounter=0;
        
        //--------------------------------------------------------------------
        //   The length of the message. This must be one more than the value 
        //   returned by strlen() to include the terminal NULL character.
        DWORD cbContent = strlen((char *) pbContent)+1;
        
        //--------------------------------------------------------------------
        //   The certificate store the holds DigiStamp public keys.
        HCERTSTORE hSystemStoreHandle;     // System Certificate store handle.
        
        //--------------------------------------------------------------------
        //  The following variables are used in the time stamp phase.
        DWORD cbEncodedTSBlob;
        BYTE *pbEncodedTSBlob = NULL;
        LPSTR tsTime;
        DGS_TIMESTAMP_PARA   TimeStampPara;
        CRYPT_ALGORITHM_IDENTIFIER   AlgId;
        // Data structure to hold the hash algorithm identifier
        
        if(argc <3 \n");
                printf("DigiStampSampleProgram: Usage egMSClient <AccountID><password>\n");
                printf("DigiStampSampleProgram: Exiting ......\n");
                exit(1);
        }
        
        
        if(hSystemStoreHandle = CertOpenStore(  // Open a certificate store.
                CERT_STORE_PROV_SYSTEM,
                0,
                0,
                CERT_SYSTEM_STORE_CURRENT_USER,
                CERTIFICATE_STORE_NAME)){
                printf("DigiStampSampleProgram: The cerrtificate store is open. \n");
        }
        else{
                DgsHandleError( "Failed to open certificate repository");
        }
        
        //--------------------------------------------------------------------
        // Initialize the DGS_TIMESTAMP_PARA structure.
        // First, use memset to set all members to zero or NULL.
        // Then set the values of all members that must be non-zero.
        memset(&TimeStampPara, 0, sizeof(DGS_TIMESTAMP_PARA));
        TimeStampPara.cbSize = sizeof(DGS_TIMESTAMP_PARA);
        TimeStampPara.listOfServers[0] = dgs_timestamp_servers1;
        TimeStampPara.listOfServers[1] = dgs_timestamp_servers2;
        TimeStampPara.listOfServers[2] = dgs_timestamp_servers3;
        TimeStampPara.roundRobin=TRUE;
        TimeStampPara.retryNum=3;
        TimeStampPara.acctId=argv[1];
        TimeStampPara.password=argv[2];
        
        //--------------------------------------------------------------------
        //  Requesting the the time stamp be created for the SHA1 hash
        //     of my data.
        //  Initialize the CRYPT_ALGORITHM_IDENTIFIER data structure.
        AlgId.pszObjId=szOID_OIWSEC_sha1;
        AlgId.Parameters.cbData=0;
        
                                
        //--------------------------------------------------------------------
        //  In two steps, create a time stamp for the signed message.
        //  First, get the number of bytes requred for the buffer
        //  to hold the time stamp.
        
        //  We also want to see the value of "time of the time stamp" from 
        //  time stamp authority clock
        if(!(tsTime = (BYTE *)malloc(20)))   //  Allocation for time in GMT 
                DgsHandleError("Failed to allocate memory for GMT time of Time Stamp");
        
        //--------------------------------------------------------------------
        //  Ask for memory requirements
        if( DgsTimeStampRequest(
                &TimeStampPara,
                AlgId,        // time stamp is of sha1 hash of signature
                "DATA",       // content to be time stamped is data
                pbContent,    // the data to be time stamped
                cbContent,
                0,     
                25000,         // Nonce
                0,
                &cbEncodedTSBlob,
                tsTime)){
                printf("DigiStampSampleProgram: The Time Stamp request length is 
                                        %d \n",cbEncodedTSBlob);
        }
        else{
                DgsHandleError("Faild to get the Time Stamp request length");
        }
        
        //--------------------------------------------------------------------
        //   Allocate memory for the required buffer.
        if(!(pbEncodedTSBlob = (BYTE *)malloc(cbEncodedTSBlob)))
                DgsHandleError("Failed to allocate memory for Time Stamp");
        
        //--------------------------------------------------------------------
        //   Call DgsTimeStampRequest a second time to
        //   copy the encoded time stamp to the buffer.
        if( DgsTimeStampRequest(
                &TimeStampPara,
                AlgId,        // time stamp is of sha1 hash of signature
                "DATA",       // content to be time stamped is data
                pbContent,    // the data to be time stamped
                cbContent,
                0,     
                25000,            //Nonce
                pbEncodedTSBlob,
                &cbEncodedTSBlob,
                tsTime)){          
                
                printf("DigiStampSampleProgram: Successfully created the Time 
                                        stamp at GMT time: %s \n",(LPSTR) tsTime);
                printf("DigiStampSampleProgram: The time stamp is:\n");
                for(loopCounter=0;loopCounter < cbEncodedTSBlob; loopCounter++)
                {
                        fprintf(stderr,"%#04X ",pbEncodedTSBlob[loopCounter]);
                }
                fprintf(stderr,"\n");
                
        }
        else{
                 DgsHandleError("Failed to create Time stamp");
        }
        
        //--------------------------------------------------------------------
        // Verify the time stamp token
        printf("DigiStampSampleProgram: Now will verify the time stamp.\n");
        if(DgsTimeStampVerify(
                "DATA",
                pbContent,            // the data that was time stamped
                cbContent,
                0,                 
                pbEncodedTSBlob,
                &cbEncodedTSBlob,
                hSystemStoreHandle,   // handle to the certificate store
                tsTime)){
                printf("DigiStampSampleProgram: Verification of time stamp succeeded.
                                 The time stampe GMT time is %s \n",tsTime);
        }
        else{
                  DgsHandleError("Failed to verify the Time stamp");
        }
        
        //--------------------------------------------------------------------
        // Clean up all memory allocations
        if (pbEncodedTSBlob)
                free(pbEncodedTSBlob);
        if (tsTime)
                free(tsTime);
        if(hSystemStoreHandle)
                CertCloseStore(hSystemStoreHandle,CERT_CLOSE_STORE_FORCE_FLAG);
        printf("DigiStampSampleProgram: The sample program completed successfully.\n");
        
        
} //  End of main



Home

  Feedback      Related Links   Contact Us   Mailing List    Privacy Statement
Copyright 2000-2005 DigiStamp, Inc.
All Rights Reserved
SecureTime, IPVault, IPProtector, and e-TimeStamp  are service marks of the DigiStamp, Inc.