Verifing and decrypting documents.Overview.Since the template is just an XML file, it might be created in advance
and saved in a file. It's also possible for application to create
templates without using XML Security Library functions. Also in some
cases template should be inserted in the signed or encrypted data
(for example, if you want to create an enveloped or enveloping
signature).Signature verification and data decryption do not require template
because all the necessary information is provided in the signed or
encrypted document.
The verification or decryption processing model.Verifying a signed documentThe typical siganture verification process includes following steps:
Load keys, X509 certificates, etc. in the keys manager .
Create signature context xmlSecDSigCtx
using xmlSecDSigCtxCreate or
xmlSecDSigCtxInitialize
functions.
Select start verification
<dsig:Signature/>
node in the signed XML document.
Verify signature by calling xmlSecDSigCtxVerify
function.
Check returned value and verification status (status
member of xmlSecDSigCtx structure).
If necessary, consume returned data from the context.
Destroy signature context xmlSecDSigCtx
using xmlSecDSigCtxDestroy or
xmlSecDSigCtxFinalize
functions.
Verifying a document.signKey = xmlSecCryptoAppKeyLoad(key_file,xmlSecKeyDataFormatPem, NULL, NULL, NULL);
if(dsigCtx->signKey == NULL) {
fprintf(stderr,"Error: failed to load public pem key from \"%s\"\n", key_file);
goto done;
}
/* set key name to the file name, this is just an example! */
if(xmlSecKeySetName(dsigCtx->signKey, key_file) < 0) {
fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
goto done;
}
/* Verify signature */
if(xmlSecDSigCtxVerify(dsigCtx, node) < 0) {
fprintf(stderr,"Error: signature verify\n");
goto done;
}
/* print verification result to stdout */
if(dsigCtx->status == xmlSecDSigStatusSucceeded) {
fprintf(stdout, "Signature is OK\n");
} else {
fprintf(stdout, "Signature is INVALID\n");
}
/* success */
res = 0;
done:
/* cleanup */
if(dsigCtx != NULL) {
xmlSecDSigCtxDestroy(dsigCtx);
}
if(doc != NULL) {
xmlFreeDoc(doc);
}
return(res);
}
]]>Full Program ListingDecrypting an encrypted documentThe typical decryption process includes following steps:
Load keys, X509 certificates, etc. in the keys manager .
Create encryption context xmlSecEncCtx
using xmlSecEncCtxCreate or
xmlSecEncCtxInitialize
functions.
Select start decryption <enc:EncryptedData> node.
Decrypt by calling xmlSecencCtxDecrypt
function.
Check returned value and if necessary consume encrypted data.
Destroy encryption context xmlSecEncCtx
using xmlSecEncCtxDestroy or
xmlSecEncCtxFinalize
functions.
Decrypting a document.encKey = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file);
if(encCtx->encKey == NULL) {
fprintf(stderr,"Error: failed to load des key from binary file \"%s\"\n", key_file);
goto done;
}
/* set key name to the file name, this is just an example! */
if(xmlSecKeySetName(encCtx->encKey, key_file) < 0) {
fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
goto done;
}
/* decrypt the data */
if((xmlSecEncCtxDecrypt(encCtx, node) < 0) || (encCtx->result == NULL)) {
fprintf(stderr,"Error: decryption failed\n");
goto done;
}
/* print decrypted data to stdout */
if(encCtx->resultReplaced != 0) {
fprintf(stdout, "Decrypted XML data:\n");
xmlDocDump(stdout, doc);
} else {
fprintf(stdout, "Decrypted binary data (%d bytes):\n", xmlSecBufferGetSize(encCtx->result));
if(xmlSecBufferGetData(encCtx->result) != NULL) {
fwrite(xmlSecBufferGetData(encCtx->result),
1,
xmlSecBufferGetSize(encCtx->result),
stdout);
}
}
fprintf(stdout, "\n");
/* success */
res = 0;
done:
/* cleanup */
if(encCtx != NULL) {
xmlSecEncCtxDestroy(encCtx);
}
if(doc != NULL) {
xmlFreeDoc(doc);
}
return(res);
}
]]>Full Program Listing