打开资源/关闭资源(C/C++)
================

#### 打开资源

从API 22开始，huksExternalCrypto提供打开/关闭资源功能接口。应用在密钥操作之前（密钥操作、通用操作、PIN码认证等），需要先调用[OH\_Huks\_OpenResource](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-api-h#oh_huks_openresource)打开资源。打开资源需要获取resourceId，resourceId通过调用证书管理系统能力提供的[证书选择接口](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-certmanagerdialog#certificatemanagerdialogopenauthorizedialog22)获取。

#### \[h2\]在CMake脚本中链接相关动态库

target\_link\_libraries(entry PUBLIC libhuks\_ndk.z.so libhuks\_external\_crypto.z.so)

#### \[h2\]开发步骤

1.  通过证书管理系统能力提供的[证书选择接口](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-certmanagerdialog#certificatemanagerdialogopenauthorizedialog22)获取[keyUri](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-certmanagerdialog#certreference22)，并将其作为resourceId。
    
2.  初始化参数集：通过[OH\_Huks\_InitExternalCryptoParamSet](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-api-h#oh_huks_initexternalcryptoparamset)、[OH\_Huks\_AddExternalCryptoParams](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-api-h#oh_huks_addexternalcryptoparams)、[OH\_Huks\_BuildExternalCryptoParamSet](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-api-h#oh_huks_buildexternalcryptoparamset)构造参数集paramSet。
    
3.  调用[OH\_Huks\_OpenResource](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-api-h#oh_huks_openresource)打开资源。
    

#### \[h2\]开发案例

#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;
}

#### 关闭资源

生态应用调用证书HAP界面，展示证书列表，用户选择证书，生态应用拿到对应的resourceId，关闭资源依赖于对应的resourceId。具体的场景介绍及规格，请参考[资源管理介绍及规格](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-resource-management-overview)。

#### \[h2\]在CMake脚本中链接相关动态库

target\_link\_libraries(entry PUBLIC libhuks\_ndk.z.so libhuks\_external\_crypto.z.so)

#### \[h2\]开发步骤

1.  通过证书管理系统能力提供的[证书选择接口](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-certmanagerdialog#certificatemanagerdialogopenauthorizedialog22)获取resourceId。
    
2.  初始化参数集：通过[OH\_Huks\_InitExternalCryptoParamSet](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-api-h#oh_huks_initexternalcryptoparamset)、[OH\_Huks\_AddExternalCryptoParams](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-api-h#oh_huks_addexternalcryptoparams)、[OH\_Huks\_BuildExternalCryptoParamSet](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-api-h#oh_huks_buildexternalcryptoparamset)构造参数集paramSet。
    
3.  调用[OH\_Huks\_CloseResource](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-api-h#oh_huks_closeresource)关闭资源。该接口会回调[onClearUkeyPinAuthState](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoextensionability#cryptoextensionabilityonclearukeypinauthstate)清理该资源关联的PIN认证状态，以及会回调[onFinishSession](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoextensionability#cryptoextensionabilityonfinishsession)清理该资源关联的会话handle。
    

#### \[h2\]开发案例

#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;
}