|
(DataFlex) Create CAdES p7m using Azure Key Vault to Sign in the Cloud
Demonstrates how to create a CAdES p7m, using Azure Key Vault. The signing of the hash happens in the Cloud on Azure Key Vault. Everything else regarding the creation of CAdES happens locally within Chilkat.
Note: This example requires Chilkat v9.5.0.96 or greater.
Use ChilkatAx-9.5.0-win32.pkg
Procedure Test
Variant vCert
Handle hoCert
Boolean iSuccess
Variant vJsonAzure
Handle hoJsonAzure
Handle hoCrypt
Handle hoSignedAttrs
String sInputXmlPath
String sOutputP7mPath
String sTemp1
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Load the certificate used for signing. The certificate's private key is stored in
// the Azure Key Vault.
// However, we still need the certificate locally (without private key).
Get Create (RefClass(cComChilkatCert)) To hoCert
If (Not(IsComObjectCreated(hoCert))) Begin
Send CreateComObject of hoCert
End
Get ComLoadFromFile Of hoCert "qa_data/certs/myCert.cer" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoCert To sTemp1
Showln sTemp1
Procedure_Return
End
// Here's a screenshot of our certificate with private key on Azure Key Vault:
// To sign using the Azure Key Vault,
// add the following lines of code to specify your authentication credentials,
// and the name of the certificate w/ private key on the Azure server to be used.
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonAzure
If (Not(IsComObjectCreated(hoJsonAzure))) Begin
Send CreateComObject of hoJsonAzure
End
// Set the "service" equal to "azure_keyvault" to tell Chilkat to use Azure Key Vault for signing.
Get ComUpdateString Of hoJsonAzure "service" "azure_keyvault" To iSuccess
Get ComUpdateString Of hoJsonAzure "client_id" "APP_ID" To iSuccess
Get ComUpdateString Of hoJsonAzure "client_secret" "APP_PASSWORD" To iSuccess
Get ComUpdateString Of hoJsonAzure "tenant_id" "TENANT_ID" To iSuccess
// In the above screenshot, our vault name is "kvchilkat". You will use your vault name.
Get ComUpdateString Of hoJsonAzure "vault_name" "VAULT_NAME" To iSuccess
// In the above screenshot, our cert name is "ChilkatTest1". You will use your cert name.
Get ComUpdateString Of hoJsonAzure "cert_name" "CERT_NAME" To iSuccess
// In the above screenshot, our cert version is "63b94a23389546ecbc8eb6208a1bef37". You will use your cert version.
Get ComUpdateString Of hoJsonAzure "cert_version" "CERT_VERSION" To iSuccess
Get pvComObject of hoJsonAzure to vJsonAzure
Get ComSetCloudSigner Of hoCert vJsonAzure To iSuccess
Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
If (Not(IsComObjectCreated(hoCrypt))) Begin
Send CreateComObject of hoCrypt
End
Get pvComObject of hoCert to vCert
Get ComSetSigningCert Of hoCrypt vCert To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoCrypt To sTemp1
Showln sTemp1
Procedure_Return
End
// The CadesEnabled property applies to all methods that create PKCS7 signatures.
// To create a CAdES-BES signature, set this property equal to true.
Set ComCadesEnabled Of hoCrypt To True
Set ComHashAlgorithm Of hoCrypt To "sha256"
Get Create (RefClass(cComChilkatJsonObject)) To hoSignedAttrs
If (Not(IsComObjectCreated(hoSignedAttrs))) Begin
Send CreateComObject of hoSignedAttrs
End
Get ComUpdateInt Of hoSignedAttrs "contentType" 1 To iSuccess
Get ComUpdateInt Of hoSignedAttrs "signingTime" 1 To iSuccess
Get ComUpdateInt Of hoSignedAttrs "messageDigest" 1 To iSuccess
Get ComUpdateInt Of hoSignedAttrs "signingCertificateV2" 1 To iSuccess
Get ComEmit Of hoSignedAttrs To sTemp1
Set ComSigningAttributes Of hoCrypt To sTemp1
// You can sign any type of file..
Move "qa_data/e-Invoice.xml" To sInputXmlPath
Move "qa_output/signed.p7m" To sOutputP7mPath
// Create the CAdES-BES attached signature, which contains the original data.
// Chilkat will build the .p7m locally, but will (internally) use ARSS
// to do the RSA signing remotely.
Get ComCreateP7M Of hoCrypt sInputXmlPath sOutputP7mPath To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoCrypt To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Success."
End_Procedure
|