1e41f4b71Sopenharmony_ci# Obtaining Key Properties (C/C++)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciThis topic describes how to obtain properties of a key. Before the operation, ensure that the key exists in HUKS.
5e41f4b71Sopenharmony_ci>**NOTE**<br>
6e41f4b71Sopenharmony_ci> The mini-system devices do not support the operation for obtaining key properties.
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci## Add the dynamic library in the CMake script.
9e41f4b71Sopenharmony_ci```txt
10e41f4b71Sopenharmony_ci   target_link_libraries(entry PUBLIC libhuks_ndk.z.so)
11e41f4b71Sopenharmony_ci```
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci## How to Develop
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci1. Set parameters.
16e41f4b71Sopenharmony_ci   - **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.
17e41f4b71Sopenharmony_ci   - **paramSetIn**: This parameter is reserved. Leave it empty.
18e41f4b71Sopenharmony_ci   - **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.
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci2. Use [OH_Huks_GetKeyItemParamSet](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_getkeyitemparamset) to obtain key properties.
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci3. Check the return value. If the operation is successful, obtain the key properties from **paramSetOut**. If the operation fails, an error code is returned.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci```c++
25e41f4b71Sopenharmony_ci#include "huks/native_huks_api.h"
26e41f4b71Sopenharmony_ci#include "huks/native_huks_param.h"
27e41f4b71Sopenharmony_ci#include <string.h>
28e41f4b71Sopenharmony_cistatic napi_value GetKeyParamSet(napi_env env, napi_callback_info info)
29e41f4b71Sopenharmony_ci{
30e41f4b71Sopenharmony_ci    /* 1. Set the key alias. */
31e41f4b71Sopenharmony_ci    const char *alias = "test_key";
32e41f4b71Sopenharmony_ci    struct OH_Huks_Blob aliasBlob = { .size = (uint32_t)strlen(alias), .data = (uint8_t *)alias };
33e41f4b71Sopenharmony_ci    /* Request memory for outParamSet. */
34e41f4b71Sopenharmony_ci    struct OH_Huks_ParamSet *outParamSet = (struct OH_Huks_ParamSet *)malloc(512); // Request memory based on service requirements.
35e41f4b71Sopenharmony_ci    if (outParamSet == nullptr) {
36e41f4b71Sopenharmony_ci        return nullptr;
37e41f4b71Sopenharmony_ci    }
38e41f4b71Sopenharmony_ci    outParamSet->paramSetSize = 512;
39e41f4b71Sopenharmony_ci    struct OH_Huks_Result ohResult;
40e41f4b71Sopenharmony_ci    do {
41e41f4b71Sopenharmony_ci        /* 2. Obtain the key properties. */
42e41f4b71Sopenharmony_ci        ohResult = OH_Huks_GetKeyItemParamSet(&aliasBlob, nullptr, outParamSet);
43e41f4b71Sopenharmony_ci        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
44e41f4b71Sopenharmony_ci            break;
45e41f4b71Sopenharmony_ci        }
46e41f4b71Sopenharmony_ci        /* 3. Read key properties from outParamSet. For example, obtain OH_HUKS_TAG_PURPOSE. */
47e41f4b71Sopenharmony_ci        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.
48e41f4b71Sopenharmony_ci        ohResult = OH_Huks_GetParam(outParamSet, OH_HUKS_TAG_PURPOSE, &purposeParam);
49e41f4b71Sopenharmony_ci        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
50e41f4b71Sopenharmony_ci            break;
51e41f4b71Sopenharmony_ci        }
52e41f4b71Sopenharmony_ci    } while (0);
53e41f4b71Sopenharmony_ci    OH_Huks_FreeParamSet(&outParamSet);
54e41f4b71Sopenharmony_ci    napi_value ret;
55e41f4b71Sopenharmony_ci    napi_create_int32(env, ohResult.errorCode, &ret);
56e41f4b71Sopenharmony_ci    return ret;
57e41f4b71Sopenharmony_ci}
58e41f4b71Sopenharmony_ci```
59