Azure Key Vault: Create an Attached CAdES-BES Signature
See more Azure Key Vault Examples
Requires Chilkat 11.6.0 or later. Keep qa_data/azure/keyvault.json and token/PFX files private, outside web content and source control, with access restricted to the application identity. Use placeholders replaced by your configuration, never commit credentials. Create output directories before running file-writing examples. These examples target Azure public-cloud vaults, not Managed HSM or sovereign-cloud endpoints.
Uses Chilkat's cloud convenience integration with client_id, client_secret, tenant_id (directory GUID), vault_name, and cert_name in private configuration. The installed Chilkat library manages authentication and service API versions for this helper; this example does not select a REST api-version. Use the explicit Http examples when that control is required. Keep the runtime current and inspect diagnostics privately.
Set explicit cert_version and load its matching public certificate from qa_data/certs/myCert.cer. Keep the version pinned rather than implicitly following certificate rotation. The backing key must permit signing, and the application needs certificates/get and keys/sign. The private key remains in Key Vault; the document is assembled locally and its digest is signed remotely. A cryptographically valid signature does not establish certificate trust, revocation status, or a trusted signing time.
Creates an attached .p7m with SHA-256 and signingCertificateV2. This example does not add a timestamp or long-term validation evidence.
Chilkat Unicode C++ Downloads
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkCertW.h>
#include <CkCrypt2W.h>
void ChilkatSample(void)
{
bool success = false;
CkStringBuilderW stringCompare;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this configuration private, outside public web content and source control.
CkJsonObjectW config;
config.put_Strict(true);
success = config.LoadFile(L"qa_data/azure/keyvault.json");
if (success != true) {
wprintf(L"Could not load private Key Vault configuration.\n");
return;
}
stringCompare.SetString(config.stringOf(L"vault_name"));
bool stringMatch1 = stringCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"vault_name") != 1) || (stringMatch1 == true)) {
wprintf(L"Missing or invalid vault_name.\n");
return;
}
CkStringBuilderW validate1;
CkStringBuilderW alphabet1;
success = validate1.Append(config.stringOf(L"vault_name"));
if (success != true) {
wprintf(L"Could not validate vault_name.\n");
return;
}
success = alphabet1.Append(L"abcdefghijklmnopqrstuvwxyz0123456789-");
if (success != true) {
wprintf(L"Could not initialize validation.\n");
return;
}
if ((validate1.get_Length() < 3) || (validate1.get_Length() > 24)) {
wprintf(L"Invalid vault_name length.\n");
return;
}
int charIndex1 = 0;
while ((charIndex1 < validate1.get_Length())) {
if (alphabet1.Contains(validate1.getRange(charIndex1,1,false),true) != true) {
wprintf(L"Invalid character in vault_name.\n");
return;
}
charIndex1 = charIndex1 + 1;
}
CkStringBuilderW vaultName;
vaultName.Append(config.stringOf(L"vault_name"));
CkStringBuilderW letters;
letters.Append(L"abcdefghijklmnopqrstuvwxyz");
if ((letters.Contains(vaultName.getRange(0,1,false),true) != true) || (vaultName.EndsWith(L"-",true) == true) || (vaultName.Contains(L"--",true) == true)) {
wprintf(L"Invalid vault_name.\n");
return;
}
CkStringBuilderW origin;
success = origin.Append(L"https://");
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
success = origin.Append(config.stringOf(L"vault_name"));
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
success = origin.Append(L".vault.azure.net");
if (success != true) {
wprintf(L"Could not construct URL or identifier.\n");
return;
}
stringCompare.SetString(config.stringOf(L"cert_name"));
bool stringMatch2 = stringCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"cert_name") != 1) || (stringMatch2 == true)) {
wprintf(L"Missing or invalid cert_name.\n");
return;
}
CkStringBuilderW validate2;
CkStringBuilderW alphabet2;
success = validate2.Append(config.stringOf(L"cert_name"));
if (success != true) {
wprintf(L"Could not validate cert_name.\n");
return;
}
success = alphabet2.Append(L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-");
if (success != true) {
wprintf(L"Could not initialize validation.\n");
return;
}
if ((validate2.get_Length() < 1) || (validate2.get_Length() > 127)) {
wprintf(L"Invalid cert_name length.\n");
return;
}
int charIndex2 = 0;
while ((charIndex2 < validate2.get_Length())) {
if (alphabet2.Contains(validate2.getRange(charIndex2,1,false),true) != true) {
wprintf(L"Invalid character in cert_name.\n");
return;
}
charIndex2 = charIndex2 + 1;
}
stringCompare.SetString(config.stringOf(L"cert_version"));
bool stringMatch3 = stringCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"cert_version") != 1) || (stringMatch3 == true)) {
wprintf(L"Missing or invalid cert_version.\n");
return;
}
CkStringBuilderW validate3;
CkStringBuilderW alphabet3;
success = validate3.Append(config.stringOf(L"cert_version"));
if (success != true) {
wprintf(L"Could not validate cert_version.\n");
return;
}
success = alphabet3.Append(L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-");
if (success != true) {
wprintf(L"Could not initialize validation.\n");
return;
}
if ((validate3.get_Length() < 1) || (validate3.get_Length() > 127)) {
wprintf(L"Invalid cert_version length.\n");
return;
}
int charIndex3 = 0;
while ((charIndex3 < validate3.get_Length())) {
if (alphabet3.Contains(validate3.getRange(charIndex3,1,false),true) != true) {
wprintf(L"Invalid character in cert_version.\n");
return;
}
charIndex3 = charIndex3 + 1;
}
stringCompare.SetString(config.stringOf(L"client_id"));
bool stringMatch4 = stringCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"client_id") != 1) || (stringMatch4 == true)) {
wprintf(L"Missing or invalid client_id.\n");
return;
}
stringCompare.SetString(config.stringOf(L"client_secret"));
bool stringMatch5 = stringCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"client_secret") != 1) || (stringMatch5 == true)) {
wprintf(L"Missing or invalid client_secret.\n");
return;
}
stringCompare.SetString(config.stringOf(L"tenant_id"));
bool stringMatch6 = stringCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"tenant_id") != 1) || (stringMatch6 == true)) {
wprintf(L"Missing or invalid tenant_id.\n");
return;
}
CkStringBuilderW validate4;
CkStringBuilderW alphabet4;
success = validate4.Append(config.stringOf(L"tenant_id"));
if (success != true) {
wprintf(L"Could not validate tenant GUID.\n");
return;
}
success = alphabet4.Append(L"0123456789abcdefABCDEF-");
if (success != true) {
wprintf(L"Could not initialize validation.\n");
return;
}
if ((validate4.get_Length() < 36) || (validate4.get_Length() > 36)) {
wprintf(L"Invalid tenant GUID length.\n");
return;
}
int charIndex4 = 0;
while ((charIndex4 < validate4.get_Length())) {
if (alphabet4.Contains(validate4.getRange(charIndex4,1,false),true) != true) {
wprintf(L"Invalid character in tenant GUID.\n");
return;
}
charIndex4 = charIndex4 + 1;
}
CkCertW cert;
success = cert.LoadFromFile(L"qa_data/certs/myCert.cer");
if (success != true) {
wprintf(L"Could not load the public signing certificate.\n");
return;
}
CkJsonObjectW cloud;
success = cloud.UpdateString(L"service",L"azure_keyvault");
if (success != true) {
wprintf(L"Could not set service.\n");
return;
}
success = cloud.UpdateString(L"client_id",config.stringOf(L"client_id"));
if (success != true) {
wprintf(L"Could not set client_id.\n");
return;
}
success = cloud.UpdateString(L"client_secret",config.stringOf(L"client_secret"));
if (success != true) {
wprintf(L"Could not set client_secret.\n");
return;
}
success = cloud.UpdateString(L"tenant_id",config.stringOf(L"tenant_id"));
if (success != true) {
wprintf(L"Could not set tenant_id.\n");
return;
}
success = cloud.UpdateString(L"vault_name",config.stringOf(L"vault_name"));
if (success != true) {
wprintf(L"Could not set vault_name.\n");
return;
}
success = cloud.UpdateString(L"cert_name",config.stringOf(L"cert_name"));
if (success != true) {
wprintf(L"Could not set cert_name.\n");
return;
}
success = cloud.UpdateString(L"cert_version",config.stringOf(L"cert_version"));
if (success != true) {
wprintf(L"Could not set cert_version.\n");
return;
}
success = cert.SetCloudSigner(cloud);
if (success != true) {
wprintf(L"Could not configure the Key Vault signer; inspect diagnostics privately.\n");
return;
}
CkCrypt2W crypt;
success = crypt.SetSigningCert(cert);
if (success != true) {
wprintf(L"Could not select signing certificate.\n");
return;
}
crypt.put_CadesEnabled(true);
crypt.put_HashAlgorithm(L"sha256");
CkJsonObjectW signedAttrs;
success = signedAttrs.UpdateInt(L"contentType",1);
if (success != true) {
wprintf(L"Could not set signed attribute.\n");
return;
}
success = signedAttrs.UpdateInt(L"signingTime",1);
if (success != true) {
wprintf(L"Could not set signed attribute.\n");
return;
}
success = signedAttrs.UpdateInt(L"messageDigest",1);
if (success != true) {
wprintf(L"Could not set signed attribute.\n");
return;
}
success = signedAttrs.UpdateInt(L"signingCertificateV2",1);
if (success != true) {
wprintf(L"Could not set signed attribute.\n");
return;
}
crypt.put_SigningAttributes(signedAttrs.emit());
// The attached .p7m includes the original file. Key Vault signs the digest remotely.
success = crypt.CreateP7M(L"qa_data/e-Invoice.xml",L"qa_output/signed.p7m");
if (success != true) {
wprintf(L"CAdES signing failed; inspect diagnostics privately.\n");
return;
}
wprintf(L"Attached CAdES-BES signature saved to qa_output/signed.p7m\n");
}