1# Exporting a Key (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 walks you through on how to export the public key of a persistently stored asymmetric key. Currently, HUKS supports export of the ECC, RSA, Ed25519, X25519, and SM2 public keys. 11 12>**NOTE** 13> <!--RP1-->Mini-system devices<!--RP1End--> support export of only the RSA public keys. 14 15## Add the dynamic library in the CMake script. 16```txt 17target_link_libraries(entry PUBLIC libhuks_ndk.z.so) 18``` 19 20## How to Develop 21 221. Set parameters. 23 - **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. 24 - **paramSetIn**: This parameter is reserved. Leave it empty. 25 - **key**: [OH_Huks_Blob](../../reference/apis-universal-keystore-kit/capi-hukstypeapi-oh-huks-blob.md) object used to hold the key exported. Ensure that there is enough memory for storing the key exported. 26 272. Call [OH_Huks_GetKeyItemParamSet](../../reference/apis-universal-keystore-kit/capi-native-huks-api-h.md#oh_huks_getkeyitemparamset) to pass in the preceding parameters. 28 293. Check the return value. If the operation is successful, the exported key is in the **key** field in the DER format defined in X.509. For details about the format, see [Public Key Material Format](huks-concepts.md#public-key-material-format). If the operation fails, an error code is returned. 30 31```c++ 32#include "huks/native_huks_api.h" 33#include "huks/native_huks_param.h" 34#include "napi/native_api.h" 35#include <cstring> 36 37/* Generate an ECC key. */ 38OH_Huks_Result InitParamSet(struct OH_Huks_ParamSet **paramSet, const struct OH_Huks_Param *params, 39 uint32_t paramCount) 40{ 41 OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet); 42 if (ret.errorCode != OH_HUKS_SUCCESS) { 43 return ret; 44 } 45 ret = OH_Huks_AddParams(*paramSet, params, paramCount); 46 if (ret.errorCode != OH_HUKS_SUCCESS) { 47 OH_Huks_FreeParamSet(paramSet); 48 return ret; 49 } 50 ret = OH_Huks_BuildParamSet(paramSet); 51 if (ret.errorCode != OH_HUKS_SUCCESS) { 52 OH_Huks_FreeParamSet(paramSet); 53 return ret; 54 } 55 return ret; 56} 57 58struct OH_Huks_Param g_testGenerateKeyParam[] = {{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_ECC}, 59 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE}, 60 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_ECC_KEY_SIZE_256}, 61 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE}}; 62 63static OH_Huks_Result GenerateKeyHelper(const char *alias) 64{ 65 struct OH_Huks_Blob aliasBlob = {.size = (uint32_t)strlen(alias), .data = (uint8_t *)alias}; 66 struct OH_Huks_ParamSet *testGenerateKeyParamSet = nullptr; 67 struct OH_Huks_Result ohResult; 68 do { 69 /* 1. Initialize the key property set. */ 70 ohResult = InitParamSet(&testGenerateKeyParamSet, g_testGenerateKeyParam, 71 sizeof(g_testGenerateKeyParam) / sizeof(OH_Huks_Param)); 72 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 73 break; 74 } 75 /* 2. Generate a key. */ 76 ohResult = OH_Huks_GenerateKeyItem(&aliasBlob, testGenerateKeyParamSet, nullptr); 77 } while (0); 78 OH_Huks_FreeParamSet(&testGenerateKeyParamSet); 79 return ohResult; 80} 81 82static napi_value ExportKey(napi_env env, napi_callback_info info) 83{ 84 /* 1. Set the key alias. */ 85 const char *alias = "test_key"; 86 struct OH_Huks_Blob aliasBlob = { .size = (uint32_t)strlen(alias), .data = (uint8_t *)alias }; 87 88 /* Generate a key. */ 89 OH_Huks_Result genResult = GenerateKeyHelper(alias); 90 if (genResult.errorCode != OH_HUKS_SUCCESS) { 91 napi_value ret; 92 napi_create_int32(env, genResult.errorCode, &ret); 93 return ret; 94 } 95 96 /* Request the memory for holding the public key to be exported. */ 97 uint8_t *pubKey = (uint8_t *)malloc(512); // Request memory based on the key size. 98 if (pubKey == nullptr) { 99 return nullptr; 100 } 101 struct OH_Huks_Blob keyBlob = { 256, pubKey }; 102 struct OH_Huks_Result ohResult; 103 do { 104 ohResult = OH_Huks_ExportPublicKeyItem(&aliasBlob, nullptr, &keyBlob); 105 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 106 break; 107 } 108 } while (0); 109 free(pubKey); 110 napi_value ret; 111 napi_create_int32(env, ohResult.errorCode, &ret); 112 return ret; 113} 114``` 115