Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Create a CAdES-T SignatureDemonstrates how to create a signature with an external timestamp that certifies the time of signing. This requires an online TSA (Time Stamping Authority) service that is capable of producing RFC 3161 compliant timestamps. Note: This example requires Chilkat v9.5.0.78 or greater.
#include <C_CkCrypt2W.h> #include <C_CkCertW.h> #include <C_CkJsonObjectW.h> void ChilkatSample(void) { HCkCrypt2W crypt; HCkCertW cert; const wchar_t *pfxPath; const wchar_t *pfxPassword; BOOL success; HCkJsonObjectW attrs; const wchar_t *inFile; const wchar_t *outFile; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. crypt = CkCrypt2W_Create(); // This example will use a certificate + private key from a .pfx/.p12 file. // On Windows systems, it is also possible to use certs on smartcards/usb tokens or certs pre-installed in the Windows registry. cert = CkCertW_Create(); pfxPath = L"qa_data/pfx/myCertAndKey.p12"; pfxPassword = L"test123"; success = CkCertW_LoadPfxFile(cert,pfxPath,pfxPassword); if (success != TRUE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkCrypt2W_Dispose(crypt); CkCertW_Dispose(cert); return; } success = CkCrypt2W_SetSigningCert(crypt,cert); // Use SHA-256 rather than the default of SHA-1 CkCrypt2W_putHashAlgorithm(crypt,L"sha256"); // Create JSON that tells Chilkat what signing attributes to include: attrs = CkJsonObjectW_Create(); CkJsonObjectW_UpdateBool(attrs,L"contentType",TRUE); CkJsonObjectW_UpdateBool(attrs,L"signingTime",TRUE); CkJsonObjectW_UpdateBool(attrs,L"messageDigest",TRUE); CkJsonObjectW_UpdateBool(attrs,L"signingCertificateV2",TRUE); // A CAdES-T signature is one that includes a timestampToken created by an online TSA (time stamping authority). // We must include the TSA's URL, as well as a few options to indicate what is desired. // Except for the TSA URL, the options shown here are typically what you would need. CkJsonObjectW_UpdateBool(attrs,L"timestampToken.enabled",TRUE); CkJsonObjectW_UpdateString(attrs,L"timestampToken.tsaUrl",L"https://freetsa.org/tsr"); CkJsonObjectW_UpdateBool(attrs,L"timestampToken.addNonce",FALSE); CkJsonObjectW_UpdateBool(attrs,L"timestampToken.requestTsaCert",TRUE); CkJsonObjectW_UpdateString(attrs,L"timestampToken.hashAlg",L"sha256"); CkCrypt2W_putSigningAttributes(crypt,CkJsonObjectW_emit(attrs)); inFile = L"qa_data/json/sample.json"; outFile = L"qa_output/sample_cades_t.p7m"; // This creates the CAdES-T signature. During the signature creation, it // communicates with the TSA to get a timestampToken. success = CkCrypt2W_CreateP7M(crypt,inFile,outFile); if (success != TRUE) { wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt)); CkCrypt2W_Dispose(crypt); CkCertW_Dispose(cert); CkJsonObjectW_Dispose(attrs); return; } wprintf(L"Success.\n"); CkCrypt2W_Dispose(crypt); CkCertW_Dispose(cert); CkJsonObjectW_Dispose(attrs); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.