capi_get_key: check for NULL after allocating key