• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Obtaining Key Properties (C/C++)
2
3
4This topic describes how to obtain properties of a key. Before the operation, ensure that the key exists in HUKS.
5
6## Add the dynamic library in the CMake script.
7```txt
8   target_link_libraries(entry PUBLIC libhuks_ndk.z.so)
9```
10
11## How to Develop
12
131. Set parameters.
14   - **keyAlias**: key alias encapsulated in an [OH_Huks_Blob](../../reference/apis-universal-keystore-kit/_o_h___huks___blob.md) struct. The maximum length of the key alias is 128 bytes.
15   - **paramSetIn**: This parameter is reserved. Leave it empty.
16   - **paramSetOut**: result set used to hold the key properties obtained. It is an object of the [OH_Huks_ParamSet](../../reference/apis-universal-keystore-kit/_o_h___huks___param_set.md) type. Ensure that there is enough memory for storing the key properties obtained.
17
182. Use [OH_Huks_GetKeyItemParamSet](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_getkeyitemparamset) to obtain key properties.
19
203. Check the return value. If the operation is successful, obtain the key properties from **paramSetOut**. If the operation fails, an error code is returned.
21
22```c++
23#include "huks/native_huks_api.h"
24#include "huks/native_huks_param.h"
25#include <string.h>
26static napi_value GetKeyParamSet(napi_env env, napi_callback_info info)
27{
28    /* 1. Set the key alias. */
29    const char *alias = "test_key";
30    struct OH_Huks_Blob aliasBlob = { .size = (uint32_t)strlen(alias), .data = (uint8_t *)alias };
31    /* Request memory for outParamSet. */
32    struct OH_Huks_ParamSet *outParamSet = (struct OH_Huks_ParamSet *)malloc(512); // Request memory based on service requirements.
33    if (outParamSet == nullptr) {
34        return nullptr;
35    }
36    outParamSet->paramSetSize = 512;
37    struct OH_Huks_Result ohResult;
38    do {
39        /* 2. Obtain the key properties. */
40        ohResult = OH_Huks_GetKeyItemParamSet(&aliasBlob, nullptr, outParamSet);
41        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
42            break;
43        }
44        /* 3. Read key properties from outParamSet. For example, obtain OH_HUKS_TAG_PURPOSE. */
45        OH_Huks_Param *purposeParam = nullptr; // No memory needs to be requested. After the parameter is obtained, the pointer points to the memory address of the parameter in the parameter set.
46        ohResult = OH_Huks_GetParam(outParamSet, OH_HUKS_TAG_PURPOSE, &purposeParam);
47        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
48            break;
49        }
50    } while (0);
51    OH_Huks_FreeParamSet(&outParamSet);
52    napi_value ret;
53    napi_create_int32(env, ohResult.errorCode, &ret);
54    return ret;
55}
56```
57