Reference Guide

24 Chapter 3: Resource Management
RSA BSAFE Crypto-C Micro Edition 3.x to 4.1.4 Migration Guide
PKCS #11 - Crypto-C ME 4.1.4
/* Loaded from configuration in real application */
const char *driver_path = "/usr/lib/";
unsigned char *driver_name = "<PKCS #11 driver name>";
int main(int argc, char **argv)
{
int ret;
R_LIB_CTX *lib_ctx = NULL;
R_PROV* p11;
const R_RES_LIST *list =
R_PROV_MES_get_default_resource_list();
ret = R_STATE_init_defaults();
if (R_ERROR_NONE != ret)
return 1;
ret = R_LIB_CTX_new_ef(list, NULL, &lib_ctx);
if (R_ERROR_NONE != ret)
goto end;
ret = R_PROV_PKCS11_new(NULL, NULL, &p11);
if (R_ERROR_NONE != ret)
goto end;
ret = R_PROV_PKCS11_set_driver_path(p11, driver_path);
if (R_ERROR_NONE != ret)
goto end;
ret = R_PROV_PKCS11_set_driver_name(p11, driver_name,
strlen(driver_name));
if (R_ERROR_NONE != ret)
goto end;
ret = R_PROV_PKCS11_load(p11);
if (R_ERROR_NONE != ret)
{
printf("Failed to load PKCS#11 provider %d\n", ret);
goto end;
}
ret = R_LIB_CTX_add_provider(lib_ctx, p11);
if (R_ERROR_NONE != ret)
goto end;
/* Add a filter to give PKCS #11 resources preference over
* others
*/
ret = R_PROV_get_info(p11, 0, R_PROV_INFO_ID_IDENTITY, &item);
if (R_ERROR_NONE != ret)
goto end;
ret = R_LIB_CTX_add_filter(lib_ctx, 0,
R_RES_FILTER_prioritize_provider, &item);
if (R_ERROR_NONE != ret)
goto end;
/* Perform cryptographic operations */
end:
R_LIB_CTX_free(lib_ctx);
R_STATE_cleanup();
return 0;
}