Opening and Closing Resources (C/C++)
Opening Resources
From API version 22, huksExternalCrypto provides the APIs for opening and closing resources. Before performing key operations (such as key operations, common operations, and PIN authentication), applications need to call OH_Huks_OpenResource to open resources. To open resources, you need to obtain resourceId by calling the certificate selection API provided by the certificate management system.
Linking the Dynamic Library in the CMake Script
target_link_libraries(entry PUBLIC libhuks_ndk.z.so libhuks_external_crypto.z.so)
How to Develop
-
Obtain keyUri as resourceId by calling the certificate selection API provided by the certificate management system.
-
Initialize the parameter set: Construct the parameter set paramSet by using OH_Huks_InitExternalCryptoParamSet, OH_Huks_AddExternalCryptoParams, and OH_Huks_BuildExternalCryptoParamSet.
-
Call OH_Huks_OpenResource to open the resource.
Development Cases
#include "huks/native_huks_external_crypto_api.h"
#include "huks/native_huks_param.h"
#include "napi/native_api.h"
#include <string.h>
OH_Huks_Result InitParamSet(
struct OH_Huks_ExternalCryptoParamSet **paramSet,
const struct OH_Huks_ExternalCryptoParam *params,
uint32_t paramCount)
{
OH_Huks_Result ret = OH_Huks_InitExternalCryptoParamSet(paramSet);
if (ret.errorCode != OH_HUKS_SUCCESS) {
return ret;
}
ret = OH_Huks_AddExternalCryptoParams(*paramSet, params, paramCount);
if (ret.errorCode != OH_HUKS_SUCCESS) {
OH_Huks_FreeExternalCryptoParamSet(paramSet);
return ret;
}
ret = OH_Huks_BuildExternalCryptoParamSet(paramSet);
if (ret.errorCode != OH_HUKS_SUCCESS) {
OH_Huks_FreeExternalCryptoParamSet(paramSet);
return ret;
}
return ret;
}
static const char *g_resourceId = "{\"providerName\":\"testProviderName\",\"abilityName\":\"CryptoExtension\",\"bundleName\":\"com.example.cryptoapplication\",\"index\":{\"key\":\"testKey\"}}";
static struct OH_Huks_ExternalCryptoParam g_openResourceParamsTest[] = {};
static napi_value OpenResource(napi_env env, napi_callback_info info)
{
struct OH_Huks_Blob resourceId = {
(uint32_t)strlen(g_resourceId),
(uint8_t *)g_resourceId
};
struct OH_Huks_ExternalCryptoParamSet *openResourceParamSet = nullptr;
OH_Huks_Result ohResult;
do {
ohResult = InitParamSet(&openResourceParamSet, g_openResourceParamsTest,
sizeof(g_openResourceParamsTest) / sizeof(OH_Huks_ExternalCryptoParam));
if (ohResult.errorCode != OH_HUKS_SUCCESS) {
break;
}
ohResult = OH_Huks_OpenResource(&resourceId, openResourceParamSet);
if (ohResult.errorCode != OH_HUKS_SUCCESS) {
break;
}
} while (0);
OH_Huks_FreeExternalCryptoParamSet(&openResourceParamSet);
napi_value ret;
napi_create_int32(env, ohResult.errorCode, &ret);
return ret;
}
Closing Resources
An ecosystem application calls the certificate HAP UI to display a certificate list. When a user selects a certificate, and the ecosystem application obtains resourceId that can be used to closing the corresponding resource. For details about the scenarios and specifications, see Resource Management Overview and Specifications.
Linking the Dynamic Library in the CMake Script
target_link_libraries(entry PUBLIC libhuks_ndk.z.so libhuks_external_crypto.z.so)
How to Develop
-
Obtain resourceId by calling the certificate selection API provided by the certificate management system.
-
Initialize the parameter set: Construct the parameter set paramSet by using OH_Huks_InitExternalCryptoParamSet, OH_Huks_AddExternalCryptoParams, and OH_Huks_BuildExternalCryptoParamSet.
-
Call OH_Huks_CloseResource to close the resource. This API calls onClearUkeyPinAuthState to clear the PIN authentication status associated with the resource, and calls onFinishSession to clear the session handle associated with the resource.
Development Cases
#include "huks/native_huks_external_crypto_api.h"
#include "huks/native_huks_param.h"
#include "huks/native_huks_type.h"
#include "huks/native_huks_api.h"
#include "napi/native_api.h"
#include <string.h>
OH_Huks_Result InitParamSet(
struct OH_Huks_ExternalCryptoParamSet **paramSet,
const struct OH_Huks_ExternalCryptoParam *params,
uint32_t paramCount)
{
OH_Huks_Result ret = OH_Huks_InitExternalCryptoParamSet(paramSet);
if (ret.errorCode != OH_HUKS_SUCCESS) {
return ret;
}
ret = OH_Huks_AddExternalCryptoParams(*paramSet, params, paramCount);
if (ret.errorCode != OH_HUKS_SUCCESS) {
OH_Huks_FreeExternalCryptoParamSet(paramSet);
return ret;
}
ret = OH_Huks_BuildExternalCryptoParamSet(paramSet);
if (ret.errorCode != OH_HUKS_SUCCESS) {
OH_Huks_FreeExternalCryptoParamSet(paramSet);
return ret;
}
return ret;
}
static const char *g_resourceId = "{\"providerName\":\"testProviderName\",\"abilityName\":\"CryptoExtension\",\"bundleName\":\"com.example.cryptoapplication\",\"userid\":100,\"index\":{\"key\":\"testKey\"}}";
static struct OH_Huks_ExternalCryptoParam g_closeResourceParamsTest[] = {};
static napi_value CloseResource(napi_env env, napi_callback_info info)
{
struct OH_Huks_Blob resourceId = {
(uint32_t)strlen(g_resourceId),
(uint8_t *)g_resourceId
};
struct OH_Huks_ExternalCryptoParamSet *closeResourceParamSet = nullptr;
OH_Huks_Result ohResult;
do {
ohResult = InitParamSet(&closeResourceParamSet, g_closeResourceParamsTest,
sizeof(g_closeResourceParamsTest) / sizeof(OH_Huks_ExternalCryptoParam));
if (ohResult.errorCode != OH_HUKS_SUCCESS) {
break;
}
ohResult = OH_Huks_CloseResource(&resourceId, closeResourceParamSet);
if (ohResult.errorCode != OH_HUKS_SUCCESS) {
break;
}
} while (0);
OH_Huks_FreeExternalCryptoParamSet(&closeResourceParamSet);
napi_value ret;
napi_create_int32(env, ohResult.errorCode, &ret);
return ret;
}
你可能感兴趣的鸿蒙文章
openharmony 鸿蒙 huks-key-import-overview
openharmony 鸿蒙 huks-signing-signature-verification-arkts
openharmony 鸿蒙 huks-hmac-arkts
openharmony 鸿蒙 huks-key-agreement-overview
openharmony 鸿蒙 huks-as-user-sys
openharmony 鸿蒙 huks-delete-key-ndk