Home Download GitHub News Documentation FAQ Tutorial API reference Examples XML Digital Signature XML Encryption XML Canonicalization Reporting Bugs Mailing list Related Authors
## Simple keys store. XML Security Library has a built-in simple keys store implemented using a keys list. You can use it in your application if you have a small number of keys. However, this might be not a best option from performance point of view if you have a lot of keys. In this case, you probably should implement your own keys store using an SQL database or some other keys storage. Example 16. Initializing keys manager and loading keys from PEM files.
    /**
     * load_keys:
     * @files:		the list of filenames.
     * @files_size:		the number of filenames in #files.
     *
     * Creates default keys manager and load PEM keys from #files in it.
     * The caller is responsible for destroing returned keys manager using
     * @xmlSecKeysMngrDestroy.
     *
     * Returns the pointer to newly created keys manager or NULL if an error
     * occurs.
     */
    xmlSecKeysMngrPtr
    load_keys(char** files, int files_size) {
        xmlSecKeysMngrPtr mngr;
        xmlSecKeyPtr key;
        int i;

        assert(files);
        assert(files_size > 0);

        /* create and initialize keys manager, we use a default list based
         * keys manager, implement your own xmlSecKeysStore klass if you need
         * something more sophisticated
         */
        mngr = xmlSecKeysMngrCreate();
        if(mngr == NULL) {
    	fprintf(stderr, "Error: failed to create keys manager.\n");
    	return(NULL);
        }
        if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) < 0) {
    	fprintf(stderr, "Error: failed to initialize keys manager.\n");
    	xmlSecKeysMngrDestroy(mngr);
    	return(NULL);
        }

        for(i = 0; i < files_size; ++i) {
    	assert(files[i]);

    	/* load key */
    	key = xmlSecCryptoAppKeyLoad(files[i], xmlSecKeyDataFormatPem, NULL, NULL, NULL);
    	if(key == NULL) {
        	    fprintf(stderr,"Error: failed to load pem key from \"%s\"\n", files[i]);
    	    xmlSecKeysMngrDestroy(mngr);
    	    return(NULL);
    	}

    	/* set key name to the file name, this is just an example! */
    	if(xmlSecKeySetName(key, BAD_CAST files[i]) < 0) {
        	    fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", files[i]);
    	    xmlSecKeyDestroy(key);
    	    xmlSecKeysMngrDestroy(mngr);
    	    return(NULL);
    	}

    	/* add key to keys manager, from now on keys manager is responsible
    	 * for destroying key
    	 */
    	if(xmlSecCryptoAppDefaultKeysMngrAdoptKey(mngr, key) < 0) {
        	    fprintf(stderr,"Error: failed to add key from \"%s\" to keys manager\n", files[i]);
    	    xmlSecKeyDestroy(key);
    	    xmlSecKeysMngrDestroy(mngr);
    	    return(NULL);
    	}
        }

        return(mngr);
    }
Full program listing