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 CkJava Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
CkStringBuilder stringCompare = new CkStringBuilder();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this configuration private, outside public web content and source control.
CkJsonObject config = new CkJsonObject();
config.put_Strict(true);
success = config.LoadFile("qa_data/azure/keyvault.json");
if (success != true) {
System.out.println("Could not load private Key Vault configuration.");
return;
}
stringCompare.SetString(config.stringOf("vault_name"));
boolean stringMatch1 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("vault_name") != 1) || (stringMatch1 == true)) {
System.out.println("Missing or invalid vault_name.");
return;
}
CkStringBuilder validate1 = new CkStringBuilder();
CkStringBuilder alphabet1 = new CkStringBuilder();
success = validate1.Append(config.stringOf("vault_name"));
if (success != true) {
System.out.println("Could not validate vault_name.");
return;
}
success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-");
if (success != true) {
System.out.println("Could not initialize validation.");
return;
}
if ((validate1.get_Length() < 3) || (validate1.get_Length() > 24)) {
System.out.println("Invalid vault_name length.");
return;
}
int charIndex1 = 0;
while ((charIndex1 < validate1.get_Length())) {
if (alphabet1.Contains(validate1.getRange(charIndex1,1,false),true) != true) {
System.out.println("Invalid character in vault_name.");
return;
}
charIndex1 = charIndex1+1;
}
CkStringBuilder vaultName = new CkStringBuilder();
vaultName.Append(config.stringOf("vault_name"));
CkStringBuilder letters = new CkStringBuilder();
letters.Append("abcdefghijklmnopqrstuvwxyz");
if ((letters.Contains(vaultName.getRange(0,1,false),true) != true) || (vaultName.EndsWith("-",true) == true) || (vaultName.Contains("--",true) == true)) {
System.out.println("Invalid vault_name.");
return;
}
CkStringBuilder origin = new CkStringBuilder();
success = origin.Append("https://");
if (success != true) {
System.out.println("Could not construct URL or identifier.");
return;
}
success = origin.Append(config.stringOf("vault_name"));
if (success != true) {
System.out.println("Could not construct URL or identifier.");
return;
}
success = origin.Append(".vault.azure.net");
if (success != true) {
System.out.println("Could not construct URL or identifier.");
return;
}
stringCompare.SetString(config.stringOf("cert_name"));
boolean stringMatch2 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("cert_name") != 1) || (stringMatch2 == true)) {
System.out.println("Missing or invalid cert_name.");
return;
}
CkStringBuilder validate2 = new CkStringBuilder();
CkStringBuilder alphabet2 = new CkStringBuilder();
success = validate2.Append(config.stringOf("cert_name"));
if (success != true) {
System.out.println("Could not validate cert_name.");
return;
}
success = alphabet2.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-");
if (success != true) {
System.out.println("Could not initialize validation.");
return;
}
if ((validate2.get_Length() < 1) || (validate2.get_Length() > 127)) {
System.out.println("Invalid cert_name length.");
return;
}
int charIndex2 = 0;
while ((charIndex2 < validate2.get_Length())) {
if (alphabet2.Contains(validate2.getRange(charIndex2,1,false),true) != true) {
System.out.println("Invalid character in cert_name.");
return;
}
charIndex2 = charIndex2+1;
}
stringCompare.SetString(config.stringOf("cert_version"));
boolean stringMatch3 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("cert_version") != 1) || (stringMatch3 == true)) {
System.out.println("Missing or invalid cert_version.");
return;
}
CkStringBuilder validate3 = new CkStringBuilder();
CkStringBuilder alphabet3 = new CkStringBuilder();
success = validate3.Append(config.stringOf("cert_version"));
if (success != true) {
System.out.println("Could not validate cert_version.");
return;
}
success = alphabet3.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-");
if (success != true) {
System.out.println("Could not initialize validation.");
return;
}
if ((validate3.get_Length() < 1) || (validate3.get_Length() > 127)) {
System.out.println("Invalid cert_version length.");
return;
}
int charIndex3 = 0;
while ((charIndex3 < validate3.get_Length())) {
if (alphabet3.Contains(validate3.getRange(charIndex3,1,false),true) != true) {
System.out.println("Invalid character in cert_version.");
return;
}
charIndex3 = charIndex3+1;
}
stringCompare.SetString(config.stringOf("client_id"));
boolean stringMatch4 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("client_id") != 1) || (stringMatch4 == true)) {
System.out.println("Missing or invalid client_id.");
return;
}
stringCompare.SetString(config.stringOf("client_secret"));
boolean stringMatch5 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("client_secret") != 1) || (stringMatch5 == true)) {
System.out.println("Missing or invalid client_secret.");
return;
}
stringCompare.SetString(config.stringOf("tenant_id"));
boolean stringMatch6 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("tenant_id") != 1) || (stringMatch6 == true)) {
System.out.println("Missing or invalid tenant_id.");
return;
}
CkStringBuilder validate4 = new CkStringBuilder();
CkStringBuilder alphabet4 = new CkStringBuilder();
success = validate4.Append(config.stringOf("tenant_id"));
if (success != true) {
System.out.println("Could not validate tenant GUID.");
return;
}
success = alphabet4.Append("0123456789abcdefABCDEF-");
if (success != true) {
System.out.println("Could not initialize validation.");
return;
}
if ((validate4.get_Length() < 36) || (validate4.get_Length() > 36)) {
System.out.println("Invalid tenant GUID length.");
return;
}
int charIndex4 = 0;
while ((charIndex4 < validate4.get_Length())) {
if (alphabet4.Contains(validate4.getRange(charIndex4,1,false),true) != true) {
System.out.println("Invalid character in tenant GUID.");
return;
}
charIndex4 = charIndex4+1;
}
CkCert cert = new CkCert();
success = cert.LoadFromFile("qa_data/certs/myCert.cer");
if (success != true) {
System.out.println("Could not load the public signing certificate.");
return;
}
CkJsonObject cloud = new CkJsonObject();
success = cloud.UpdateString("service","azure_keyvault");
if (success != true) {
System.out.println("Could not set service.");
return;
}
success = cloud.UpdateString("client_id",config.stringOf("client_id"));
if (success != true) {
System.out.println("Could not set client_id.");
return;
}
success = cloud.UpdateString("client_secret",config.stringOf("client_secret"));
if (success != true) {
System.out.println("Could not set client_secret.");
return;
}
success = cloud.UpdateString("tenant_id",config.stringOf("tenant_id"));
if (success != true) {
System.out.println("Could not set tenant_id.");
return;
}
success = cloud.UpdateString("vault_name",config.stringOf("vault_name"));
if (success != true) {
System.out.println("Could not set vault_name.");
return;
}
success = cloud.UpdateString("cert_name",config.stringOf("cert_name"));
if (success != true) {
System.out.println("Could not set cert_name.");
return;
}
success = cloud.UpdateString("cert_version",config.stringOf("cert_version"));
if (success != true) {
System.out.println("Could not set cert_version.");
return;
}
success = cert.SetCloudSigner(cloud);
if (success != true) {
System.out.println("Could not configure the Key Vault signer; inspect diagnostics privately.");
return;
}
CkCrypt2 crypt = new CkCrypt2();
success = crypt.SetSigningCert(cert);
if (success != true) {
System.out.println("Could not select signing certificate.");
return;
}
crypt.put_CadesEnabled(true);
crypt.put_HashAlgorithm("sha256");
CkJsonObject signedAttrs = new CkJsonObject();
success = signedAttrs.UpdateInt("contentType",1);
if (success != true) {
System.out.println("Could not set signed attribute.");
return;
}
success = signedAttrs.UpdateInt("signingTime",1);
if (success != true) {
System.out.println("Could not set signed attribute.");
return;
}
success = signedAttrs.UpdateInt("messageDigest",1);
if (success != true) {
System.out.println("Could not set signed attribute.");
return;
}
success = signedAttrs.UpdateInt("signingCertificateV2",1);
if (success != true) {
System.out.println("Could not set signed attribute.");
return;
}
crypt.put_SigningAttributes(signedAttrs.emit());
// The attached .p7m includes the original file. Key Vault signs the digest remotely.
success = crypt.CreateP7M("qa_data/e-Invoice.xml","qa_output/signed.p7m");
if (success != true) {
System.out.println("CAdES signing failed; inspect diagnostics privately.");
return;
}
System.out.println("Attached CAdES-BES signature saved to qa_output/signed.p7m");
}
}