- Home
- Download

- GitHub

- News
- Documentation

- FAQ
- Tutorial
- API reference
- Examples

- XML Digital Signature
- XML Encryption
- XML Canonicalization
- Reporting Bugs
- Mailing list
- Related
- Authors

## Signing a document.

The typical signature process includes following steps:

- Prepare data for signature.

- Create or load signature template and select start <dsig:Signature/> node.

- Create signature context xmlSecDSigCtx using xmlSecDSigCtxCreate or xmlSecDSigCtxInitialize functions.

- Load signature key in keys manager or generate a session key and set it in the signature context (`signKey` member of xmlSecDSigCtx structure).

- Sign data by calling xmlSecDSigCtxSign function.

- Check returned value and consume signed data.

- Destroy signature context xmlSecDSigCtx using xmlSecDSigCtxDestroy or xmlSecDSigCtxFinalize functions.

Example 10. Signing a template.

    /**
     * sign_file:
     * @tmpl_file:		the signature template file name.
     * @key_file:		the PEM private key file name.
     *
     * Signs the #tmpl_file using private key from #key_file.
     *
     * Returns 0 on success or a negative value if an error occurs.
     */
    int
    sign_file(const char* tmpl_file, const char* key_file) {
        xmlDocPtr doc = NULL;
        xmlNodePtr node = NULL;
        xmlSecDSigCtxPtr dsigCtx = NULL;
        int res = -1;

        assert(tmpl_file);
        assert(key_file);

        /* load template */
        doc = xmlParseFile(tmpl_file);
        if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
    	fprintf(stderr, "Error: unable to parse file \"%s\"\n", tmpl_file);
    	goto done;
        }

        /* find start node */
        node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs);
        if(node == NULL) {
    	fprintf(stderr, "Error: start node not found in \"%s\"\n", tmpl_file);
    	goto done;
        }

        /* create signature context, we don't need keys manager in this example */
        dsigCtx = xmlSecDSigCtxCreate(NULL);
        if(dsigCtx == NULL) {
            fprintf(stderr,"Error: failed to create signature context\n");
    	goto done;
        }

        /* load private key, assuming that there is not password */
        dsigCtx->signKey = xmlSecCryptoAppKeyLoad(key_file, xmlSecKeyDataFormatPem, NULL, NULL, NULL);
        if(dsigCtx->signKey == NULL) {
            fprintf(stderr,"Error: failed to load private 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;
        }

        /* sign the template */
        if(xmlSecDSigCtxSign(dsigCtx, node) < 0) {
            fprintf(stderr,"Error: signature failed\n");
    	goto done;
        }

        /* print signed document to stdout */
        xmlDocDump(stdout, doc);

        /* success */
        res = 0;

    done:
        /* cleanup */
        if(dsigCtx != NULL) {
    	xmlSecDSigCtxDestroy(dsigCtx);
        }

        if(doc != NULL) {
    	xmlFreeDoc(doc);
        }
        return(res);
    }

Full program listing

Simple signature template file