1# Obtaining Key Properties (C/C++) 2 3<!--Kit: Universal Keystore Kit--> 4<!--Subsystem: Security--> 5<!--Owner: @wutiantian-gitee--> 6<!--Designer: @HighLowWorld--> 7<!--Tester: @wxy1234564846--> 8<!--Adviser: @zengyawen--> 9 10This topic describes how to obtain properties of a key. Before the operation, ensure that the key exists in HUKS. 11>**NOTE**<br> 12> The mini-system devices do not support the operation for obtaining key properties. 13 14## Add the dynamic library in the CMake script. 15```txt 16target_link_libraries(entry PUBLIC libhuks_ndk.z.so) 17``` 18## How to Develop 19 201. Set parameters. 21 - **keyAlias**: key alias encapsulated in an [OH_Huks_Blob](../../reference/apis-universal-keystore-kit/capi-hukstypeapi-oh-huks-blob.md) struct. The maximum length of the key alias is 128 bytes. 22 - **paramSetIn**: This parameter is reserved. Leave it empty. 23 - **paramSetOut**: result set used to hold the key properties obtained. It is an object of the [OH_Huks_ParamSet](../../reference/apis-universal-keystore-kit/capi-hukstypeapi-oh-huks-paramset.md) type. Ensure that there is enough memory for storing the key properties obtained. 24 252. Call [OH_Huks_GetKeyItemParamSet](../../reference/apis-universal-keystore-kit/capi-native-huks-api-h.md#oh_huks_getkeyitemparamset) to pass in the preceding parameters. 26 273. Check the return value. If the operation is successful, obtain the key properties from **paramSetOut**. If the operation fails, an error code is returned. 28 29```c++ 30#include "huks/native_huks_api.h" 31#include "huks/native_huks_param.h" 32#include "napi/native_api.h" 33#include <cstring> 34 35OH_Huks_Result InitParamSet(struct OH_Huks_ParamSet **paramSet, const struct OH_Huks_Param *params, 36 uint32_t paramCount) 37{ 38 OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet); 39 if (ret.errorCode != OH_HUKS_SUCCESS) { 40 return ret; 41 } 42 ret = OH_Huks_AddParams(*paramSet, params, paramCount); 43 if (ret.errorCode != OH_HUKS_SUCCESS) { 44 OH_Huks_FreeParamSet(paramSet); 45 return ret; 46 } 47 ret = OH_Huks_BuildParamSet(paramSet); 48 if (ret.errorCode != OH_HUKS_SUCCESS) { 49 OH_Huks_FreeParamSet(paramSet); 50 return ret; 51 } 52 return ret; 53} 54 55struct OH_Huks_Param g_testGenerateKeyParam[] = {{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_ECC}, 56 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE}, 57 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_ECC_KEY_SIZE_256}, 58 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE}}; 59 60static OH_Huks_Result GenerateKeyHelper(const char *alias) 61{ 62 struct OH_Huks_Blob aliasBlob = {.size = (uint32_t)strlen(alias), .data = (uint8_t *)alias}; 63 struct OH_Huks_ParamSet *testGenerateKeyParamSet = nullptr; 64 struct OH_Huks_Result ohResult; 65 do { 66 /* 1. Initialize the key property set. */ 67 ohResult = InitParamSet(&testGenerateKeyParamSet, g_testGenerateKeyParam, 68 sizeof(g_testGenerateKeyParam) / sizeof(OH_Huks_Param)); 69 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 70 break; 71 } 72 /* 1. Generate a key. */ 73 ohResult = OH_Huks_GenerateKeyItem(&aliasBlob, testGenerateKeyParamSet, nullptr); 74 } while (0); 75 OH_Huks_FreeParamSet(&testGenerateKeyParamSet); 76 return ohResult; 77} 78 79static napi_value GetKeyParamSet(napi_env env, napi_callback_info info) 80{ 81 /* 1. Set the key alias. */ 82 const char *alias = "test_key"; 83 struct OH_Huks_Blob aliasBlob = { .size = (uint32_t)strlen(alias), .data = (uint8_t *)alias }; 84 85 /* Generate a key. */ 86 OH_Huks_Result genResult = GenerateKeyHelper(alias); 87 if (genResult.errorCode != OH_HUKS_SUCCESS) { 88 napi_value ret; 89 napi_create_int32(env, genResult.errorCode, &ret); 90 return ret; 91 } 92 93 const size_t paramSetSize = 512; 94 /* Request memory for outParamSet. */ 95 * Request memory according to your evaluated needs. 96 */ 97 struct OH_Huks_ParamSet *outParamSet = static_cast<struct OH_Huks_ParamSet *>(malloc(paramSetSize)); 98 if (outParamSet == nullptr) { 99 return nullptr; 100 } 101 outParamSet->paramSetSize = paramSetSize; 102 struct OH_Huks_Result ohResult; 103 do { 104 /* 2. Obtain the key properties. */ 105 ohResult = OH_Huks_GetKeyItemParamSet(&aliasBlob, nullptr, outParamSet); 106 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 107 break; 108 } 109 /* 3. Read key properties from outParamSet. For example, obtain OH_HUKS_TAG_PURPOSE. */ 110 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. 111 ohResult = OH_Huks_GetParam(outParamSet, OH_HUKS_TAG_PURPOSE, &purposeParam); 112 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 113 break; 114 } 115 } while (0); 116 OH_Huks_FreeParamSet(&outParamSet); 117 napi_value ret; 118 napi_create_int32(env, ohResult.errorCode, &ret); 119 return ret; 120} 121``` 122