Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Unicode C++ Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Unicode C++) RFC3161 Timestamp Client - Fetch from Timestamp Authority (TSA) and Verify

Sends an RFC 3161 timestamp request to a TSA (Timestamp Authority) server and validates the timestamp token response.

Note: This example requires Chilkat v9.5.0.75 or greater.

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Win Mobile 5.0/Pocket PC 2003

Solaris C/C++

FreeBSD C/C++

OpenBSD C/C++

MinGW C/C++

#include <CkCrypt2W.h>
#include <CkHttpW.h>
#include <CkBinDataW.h>
#include <CkHttpResponseW.h>
#include <CkCertW.h>

void ChilkatSample(void)
    {
    //  Note: Requires Chilkat v9.5.0.75 or greater.

    //  This requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  First sha-256 hash the data that is to be timestamped.
    //  In this example, the data is the string "Hello World"
    bool success;
    CkCrypt2W crypt;
    crypt.put_HashAlgorithm(L"sha256");
    crypt.put_EncodingMode(L"base64");
    const wchar_t *base64Hash = crypt.hashStringENC(L"Hello World");

    CkHttpW http;

    CkBinDataW requestToken;
    const wchar_t *optionalPolicyOid = L"";
    bool addNonce = false;
    bool requestTsaCert = true;

    //  Create a time-stamp request token
    success = http.CreateTimestampRequest(L"sha256",base64Hash,optionalPolicyOid,addNonce,requestTsaCert,requestToken);
    if (success != true) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    //  Send the time-stamp request token to the TSA.
    //  This is the equivalent of the following CURL command:
    //  curl -H "Content-Type: application/timestamp-query" --data-binary '@file.tsq' https://freetsa.org/tsr > file.tsr
    const wchar_t *tsaUrl = L"https://freetsa.org/tsr";
    CkHttpResponseW *resp = http.PBinaryBd(L"POST",tsaUrl,requestToken,L"application/timestamp-query",false,false);
    if (http.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    //  Get the timestamp reply from the HTTP response object.
    CkBinDataW timestampReply;
    resp->GetBodyBd(timestampReply);
    delete resp;

    //  Show the base64 encoded timestamp reply.
    wprintf(L"%s\n",timestampReply.getEncoded(L"base64"));

    //  Let's verify the timestamp reply against the TSA's cert, which we've previously downloaded.
    //  See https://freetsa.org/index_en.php
    CkCertW tsaCert;
    success = tsaCert.LoadFromFile(L"qa_data/certs/freetsa.org.cer");
    if (success != true) {
        wprintf(L"%s\n",tsaCert.lastErrorText());
        return;
    }

    //  The VerifyTimestampReply method will return one of the following values:
    //  -1:  The timestampReply does not contain a valid timestamp reply.
    //  -2: The  timestampReply is a valid timestamp reply, but failed verification using the public key of the tsaCert.
    //  0:  Granted and verified.
    //  1: Granted and verified, with mods (see RFC 3161)
    //  2: Rejected.
    //  3: Waiting.
    //  4: Revocation Warning
    //  5: Revocation Notification
    int pkiStatus = http.VerifyTimestampReply(timestampReply,tsaCert);
    if (pkiStatus < 0) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    wprintf(L"pkiStatus = %d\n",pkiStatus);
    }

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.