Unicode C
Unicode C
Create CAdES-T Signature using Aruba TSA Server (servizi.arubapec.it)
See more CAdES Examples
Demonstrates how to create a signature with an external timestamp (using the Aruba TSA Server) that certifies the time of signing.Note: This example requires Chilkat v9.5.0.79 or greater.
Chilkat Unicode C Downloads
#include <C_CkCrypt2W.h>
#include <C_CkCertW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkCrypt2W crypt;
HCkCertW cert;
const wchar_t *pfxPath;
const wchar_t *pfxPassword;
HCkJsonObjectW attrs;
const wchar_t *inFile;
const wchar_t *outFile;
success = FALSE;
// 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.
// This example uses the Aruba TSA server, which requires a login/password for the HTTPS request.
CkJsonObjectW_UpdateBool(attrs,L"timestampToken.enabled",TRUE);
CkJsonObjectW_UpdateString(attrs,L"timestampToken.tsaUrl",L"https://servizi.arubapec.it/tsa/ngrequest.php");
// The tsaUsername/tsaPassword feature was added in Chilkat v9.5.0.79.
// Make sure you are using a version of Chilkat that is no older than v9.5.0.79
CkJsonObjectW_UpdateString(attrs,L"timestampToken.tsaUsername",L"arubaTsaUsername");
CkJsonObjectW_UpdateString(attrs,L"timestampToken.tsaPassword",L"arubaTsaPassword");
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);
}