diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/.gitignore b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/.gitignore new file mode 100755 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/AppScope/app.json5 b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/AppScope/app.json5 new file mode 100755 index 0000000000000000000000000000000000000000..6af55fd49d7ec489d3165ea6a80e42cb2f5b31d5 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.keyexport", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/AppScope/resources/base/element/string.json b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/AppScope/resources/base/element/string.json new file mode 100755 index 0000000000000000000000000000000000000000..6b14bfbaf86178383bb299c464fdf00b2eef2c6e --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "KeyExport" + } + ] +} diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/AppScope/resources/base/media/app_icon.png b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/AppScope/resources/base/media/app_icon.png new file mode 100755 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/AppScope/resources/base/media/app_icon.png differ diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/README.md b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/README.md new file mode 100755 index 0000000000000000000000000000000000000000..5af6b4831ea0266892a5ca4ae57e1b6aa545a190 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/README.md @@ -0,0 +1,87 @@ +## 密钥导出(ArkTS) + +### 介绍 + +1. 本工程主要实现了对以下指南文档中 https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/UniversalKeystoreKit/huks-export-key-arkts.md 示例代码片段的工程化,主要目标是实现指南中示例代码需要与sample工程文件同源。 + +#### KeyExport + +##### 介绍 + +1. 本示例主要介绍密钥导出,业务需要获取持久化存储的非对称密钥的公钥时使用,轻量级设备仅支持RSA公钥导出。 + +##### 效果预览 + +| 主页 | 公钥导出 | +| -------------------------------------------------------- | -------------------------------------------------------- | +| <img src="./screenshots/KeyExport_1.png" width="360;" /> | <img src="./screenshots/KeyExport_2.png" width="360;" /> | + +使用说明 + +1. 点击Call KeyExport按钮调用接口exportKeyItem,在outData字段中获取以标准的X.509规范的DER格式封装的公钥明文。 + +## 密钥导出(C/C++) + +### 介绍 + +1. 本工程主要实现了对以下指南文档中 https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/UniversalKeystoreKit/huks-export-key-ndk.md 示例代码片段的工程化,主要目标是实现指南中示例代码需要与sample工程文件同源。 + +#### Call C/C++ + +##### 介绍 + +1. 本示例主要介绍密钥导出,业务需要获取持久化存储的非对称密钥的公钥时使用,轻量级设备仅支持RSA公钥导出。 + +##### 效果预览 + +| 主页 | 公钥导出 | +| -------------------------------------------------- | -------------------------------------------------- | +| <img src="./screenshots/Cpp_1.png" width="360;" /> | <img src="./screenshots/Cpp_2.png" width="360;" /> | + +使用说明 + +1. 点击Call C/C++按钮调用接口OH_Huks_GetKeyItemParamSet,从参数key中导出以标准的X.509规范的DER格式封装的公钥。 + +## 工程目录 + +``` +entry/src/main/ +|---ets +|---|---entryability +|---|---|---EntryAbility.ets +|---|---pages +|---|---|---KeyExport.ets +|---|---|---Index.ets // 首页 +|---cpp +|---resources // 静态资源 +|---ohosTest +|---|---ets +|---|---|---tests +|---|---|---|---KeyExport.test.ets // 自动化测试用例 +``` + +## 相关权限 + +无。 + +## 依赖 + +不涉及。 + +## 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:RK3568。 +2. 本示例支持API14版本SDK,SDK版本号(API Version 14 Release)。 +3. 本示例需要使用 DevEco Studio 版本号(5.0.1Release)才可编译运行。 + +## 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/build-profile.json5 b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/build-profile.json5 new file mode 100755 index 0000000000000000000000000000000000000000..df8b8004f9207069a86cc1b12716cdcdb73f9072 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/build-profile.json5 @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compileSdkVersion": 14, + "compatibleSdkVersion": 14, + "targetSdkVersion": 14, + "runtimeOS": "OpenHarmony", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/code-linter.json5 b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/code-linter.json5 new file mode 100755 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/.gitignore b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/.gitignore new file mode 100755 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/build-profile.json5 b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/build-profile.json5 new file mode 100755 index 0000000000000000000000000000000000000000..5612a9a1db33c953c5d474210a9a9576d1afd4d4 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/build-profile.json5 @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + "abiFilters": [ + "arm64-v8a", + "x86_64", + "armeabi-v7a" + ] + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/hvigorfile.ts b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/hvigorfile.ts new file mode 100755 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/oh-package.json5 b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/oh-package.json5 new file mode 100755 index 0000000000000000000000000000000000000000..5d993e5251fd56950970aa593aefef1b8d71e976 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libentry.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/cpp/CMakeLists.txt b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/cpp/CMakeLists.txt new file mode 100755 index 0000000000000000000000000000000000000000..482cdaa452e5b8a51cd008ed046196a420d69f4d --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/cpp/CMakeLists.txt @@ -0,0 +1,15 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(KeyExport) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +if(DEFINED PACKAGE_FIND_FILE) + include(${PACKAGE_FIND_FILE}) +endif() + +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(entry SHARED napi_init.cpp) +target_link_libraries(entry PUBLIC libace_napi.z.so libhuks_ndk.z.so) \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/cpp/napi_init.cpp b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/cpp/napi_init.cpp new file mode 100755 index 0000000000000000000000000000000000000000..33abf36c07a3e2a4553f7e3771027c69162f76a7 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/cpp/napi_init.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "huks/native_huks_api.h" +#include "huks/native_huks_param.h" +#include "napi/native_api.h" +#include <cstring> + +/* 以下以生成ECC密钥为例 */ +OH_Huks_Result InitParamSet(struct OH_Huks_ParamSet **paramSet, const struct OH_Huks_Param *params, + uint32_t paramCount) +{ + OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet); + if (ret.errorCode != OH_HUKS_SUCCESS) { + return ret; + } + ret = OH_Huks_AddParams(*paramSet, params, paramCount); + if (ret.errorCode != OH_HUKS_SUCCESS) { + OH_Huks_FreeParamSet(paramSet); + return ret; + } + ret = OH_Huks_BuildParamSet(paramSet); + if (ret.errorCode != OH_HUKS_SUCCESS) { + OH_Huks_FreeParamSet(paramSet); + return ret; + } + return ret; +} + +struct OH_Huks_Param g_testGenerateKeyParam[] = {{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_ECC}, + {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE}, + {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_ECC_KEY_SIZE_256}, + {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE}}; + +static OH_Huks_Result GenerateKeyHelper(const char *alias) +{ + struct OH_Huks_Blob aliasBlob = {.size = (uint32_t)strlen(alias), .data = (uint8_t *)alias}; + struct OH_Huks_ParamSet *testGenerateKeyParamSet = nullptr; + struct OH_Huks_Result ohResult; + do { + /* 1.初始化密钥属性集 */ + ohResult = InitParamSet(&testGenerateKeyParamSet, g_testGenerateKeyParam, + sizeof(g_testGenerateKeyParam) / sizeof(OH_Huks_Param)); + if (ohResult.errorCode != OH_HUKS_SUCCESS) { + break; + } + /* 2.生成密钥 */ + ohResult = OH_Huks_GenerateKeyItem(&aliasBlob, testGenerateKeyParamSet, nullptr); + } while (0); + OH_Huks_FreeParamSet(&testGenerateKeyParamSet); + return ohResult; +} + +static napi_value ExportKey(napi_env env, napi_callback_info info) +{ + /* 1. 参数构造:确定密钥别名 */ + const char *alias = "test_key"; + struct OH_Huks_Blob aliasBlob = { .size = (uint32_t)strlen(alias), .data = (uint8_t *)alias }; + /* 生成密钥 */ + OH_Huks_Result genResult = GenerateKeyHelper(alias); + if (genResult.errorCode != OH_HUKS_SUCCESS) { + napi_value ret; + napi_create_int32(env, genResult.errorCode, &ret); + return ret; + } + /* 构造参数:为待导出公钥申请内存 */ + uint8_t *pubKey = (uint8_t *)malloc(512); // 请业务按实际密钥大小评估申请 + if (pubKey == nullptr) { + return nullptr; + } + struct OH_Huks_Blob keyBlob = { 256, pubKey }; + struct OH_Huks_Result ohResult; + do { + ohResult = OH_Huks_ExportPublicKeyItem(&aliasBlob, nullptr, &keyBlob); + if (ohResult.errorCode != OH_HUKS_SUCCESS) { + break; + } + } while (0); + free(pubKey); + napi_value ret; + napi_create_int32(env, ohResult.errorCode, &ret); + return ret; +} + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + { "exportKey", nullptr, ExportKey, nullptr, nullptr, nullptr, napi_default, nullptr } + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "entry", + .nm_priv = ((void*)0), + .reserved = { 0 }, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) +{ + napi_module_register(&demoModule); +} diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/cpp/types/libentry/index.d.ts b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/cpp/types/libentry/index.d.ts new file mode 100755 index 0000000000000000000000000000000000000000..406132e484e49351c4d87c51a8ec0195ce8eca55 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/cpp/types/libentry/index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const exportKey: () => number; \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/cpp/types/libentry/oh-package.json5 b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/cpp/types/libentry/oh-package.json5 new file mode 100755 index 0000000000000000000000000000000000000000..e8f69f3d7619ec71489946af3b6f83522b32de76 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libentry.so", + "types": "./index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/ets/entryability/EntryAbility.ets b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100755 index 0000000000000000000000000000000000000000..3e77e4b9f7946d80aa77e6b2dcfe40b23ccf6178 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100755 index 0000000000000000000000000000000000000000..1504a74f09dfdcfae408be979f99369a2c5affab --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/ets/pages/Index.ets b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/ets/pages/Index.ets new file mode 100755 index 0000000000000000000000000000000000000000..d10edfea46d172c589150df8e52ed94926056126 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { router } from '@kit.ArkUI'; +import testNapi from 'libentry.so'; + +@Entry +@Component +struct Index { + @State message: string = 'KeyExport'; + + goToSample(url: string): void { + router.pushUrl({ + url: url, + }, router.RouterMode.Single, (err) => { + if (err) { + console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); + return; + } + console.info('pushUrl success'); + }) + } + + build() { + Column({ space: 5 }) { + Text(this.message) + .fontSize(25) + .fontWeight(FontWeight.Bold) + Button('KeyExport') + .width('70%') + .onClick(() => { + this.goToSample('pages/KeyExport'); + }) + Button('Call C/C++') + .width('70%') + .onClick(() => { + let ret: number = testNapi.exportKey(); + this.message = 'Call C/C++ Result: ' + ret.toString(); + }) + } + .height('100%') + .width('100%') + } +} diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/ets/pages/KeyExport.ets b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/ets/pages/KeyExport.ets new file mode 100755 index 0000000000000000000000000000000000000000..15d80d00cfd78f92016c85aac788ff9c9feabafe --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/ets/pages/KeyExport.ets @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { huks } from '@kit.UniversalKeystoreKit'; + +/* 1. 设置密钥别名 */ +let keyAlias = 'keyAlias'; +/* option对象传空 */ +let emptyOptions: huks.HuksOptions = { + properties: [] +}; + +let properties1: huks.HuksParam[] = [ + { + tag: huks.HuksTag.HUKS_TAG_ALGORITHM, + value: huks.HuksKeyAlg.HUKS_ALG_DH + }, + { + tag: huks.HuksTag.HUKS_TAG_PURPOSE, + value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE + }, + { + tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, + value: huks.HuksKeySize.HUKS_DH_KEY_SIZE_2048 + } +]; + +let huksOptions: huks.HuksOptions = { + properties: properties1, + inData: new Uint8Array([]) +} + +/* 3.生成密钥 */ +function generateKeyItem(keyAlias: string, huksOptions: huks.HuksOptions) { + return new Promise<void>((resolve, reject) => { + try { + huks.generateKeyItem(keyAlias, huksOptions, (error, data) => { + if (error) { + reject(error); + } else { + resolve(data); + } + }); + } catch (error) { + throw (error as Error); + } + }); +} + +async function publicGenKeyFunc(keyAlias: string, huksOptions: huks.HuksOptions): Promise<string> { + console.info(`enter promise generateKeyItem`); + try { + await generateKeyItem(keyAlias, huksOptions) + .then((data) => { + console.info(`promise: generateKeyItem success, data = ${JSON.stringify(data)}`); + }) + .catch((error: Error) => { + console.error(`promise: generateKeyItem failed, ${JSON.stringify(error)}`); + }); + return 'Success'; + } catch (error) { + console.error(`promise: generateKeyItem input arg invalid, ${JSON.stringify(error)}`); + return 'Failed'; + } +} + +async function testGenKey(): Promise<string> { + let ret = await publicGenKeyFunc(keyAlias, huksOptions); + return ret; +} + +function check(): string { + try { + /* 1. 生成密钥 */ + testGenKey() + /* 2. 导出密钥 */ + huks.exportKeyItem(keyAlias, emptyOptions, (error, data) => { + if (error) { + console.error(`callback: exportKeyItem failed, ` + error); + } else { + console.info(`callback: exportKeyItem success, data = ${JSON.stringify(data)}`); + } + }); + return 'Success'; + } catch (error) { + console.error(`callback: exportKeyItem input arg invalid, ${JSON.stringify(error)}`); + return 'Failed'; + } +} + +@Entry +@Component +struct Index { + @State message: string = 'KeyExport'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_keyExport')) + .width('70%') + .onClick(() => { + let ret: string = check(); + this.message = 'KeyExport Result: ' + ret; + }) + } + .height('100%') + .width('100%') + } +} diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/module.json5 b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/module.json5 new file mode 100755 index 0000000000000000000000000000000000000000..776ce01475229d674487a2830790c4c87b777379 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/element/color.json b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/element/color.json new file mode 100755 index 0000000000000000000000000000000000000000..d66f9a7d4ac61fb8d215239ab3620b7bcd77bf33 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/element/string.json b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/element/string.json new file mode 100755 index 0000000000000000000000000000000000000000..988f20262f3d0014216be867c59f612409f27d8c --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/element/string.json @@ -0,0 +1,28 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "KeyExport" + }, + { + "name": "call_keyExport", + "value": "Call KeyExport" + }, + { + "name": "key_export", + "value": "KeyExport" + }, + { + "name": "call_cpp", + "value": "Call C/C++" + } + ] +} diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/media/background.png b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/media/background.png new file mode 100755 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/media/background.png differ diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/media/foreground.png b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/media/foreground.png new file mode 100755 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/media/foreground.png differ diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/media/layered_image.json b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/media/layered_image.json new file mode 100755 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/media/startIcon.png b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/media/startIcon.png new file mode 100755 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/media/startIcon.png differ diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/profile/backup_config.json b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/profile/backup_config.json new file mode 100755 index 0000000000000000000000000000000000000000..d742c2f96e7dd0f406f499941f3147345e998f95 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/profile/main_pages.json b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/profile/main_pages.json new file mode 100755 index 0000000000000000000000000000000000000000..9ae4a8cd630b1c33a08c0267c461661f6d38500d --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,6 @@ +{ + "src": [ + "pages/Index", + "pages/KeyExport" + ] +} diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/dark/element/color.json b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/dark/element/color.json new file mode 100755 index 0000000000000000000000000000000000000000..438d5bc43bb23c59c210d586b96635a72da5b64a --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/ohosTest/ets/test/Ability.test.ets b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100755 index 0000000000000000000000000000000000000000..0f8ce9a2c012f8fe36114cef65216ef0b6254f41 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }) + }) +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/ohosTest/ets/test/KeyExport.test.ets b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/ohosTest/ets/test/KeyExport.test.ets new file mode 100755 index 0000000000000000000000000000000000000000..f233530c8d70c16f81d6b7d15d2f7b5c6f6d11ce --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/ohosTest/ets/test/KeyExport.test.ets @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, it, expect, beforeAll } from '@ohos/hypium'; +// 导入测试依赖kit +import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit'; +import { UIAbility, Want } from '@kit.AbilityKit'; + +const delegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); +const bundleName = abilityDelegatorRegistry.getArguments().bundleName; +let want: Want; + +export default function keyExportTest() { + describe('keyExportTest', () => { + /** + * 打开应用 + */ + beforeAll(async () => { + want = { + bundleName: bundleName, + abilityName: 'EntryAbility' + }; + await delegator.startAbility(want); + const driver = Driver.create(); + await driver.delayMs(1000); + const ability: UIAbility = await delegator.getCurrentTopAbility(); + console.info('get top ability'); + await driver.delayMs(1000); + expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); + }) + + /** + * 点击按钮,密钥导出(ArkTS) + */ + it('testKeyExport001', 0, async () => { + console.info('uitest: testKeyExport001 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('KeyExport').type('Button')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call KeyExport')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('KeyExport Result: Success')); + console.info('uitest: testKeyExport001 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,密钥导出(C/C++) + */ + it('testKeyExport002', 0, async () => { + console.info('uitest: testKeyExport002 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Call C/C++')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Call C/C++ Result: 0')); + console.info('uitest: testKeyExport002 end'); + await driver.pressBack(); + }) + }) +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/ohosTest/ets/test/List.test.ets b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/ohosTest/ets/test/List.test.ets new file mode 100755 index 0000000000000000000000000000000000000000..d38e02e89e3f9185727f0b6e4dea59e28b87e399 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import keyExportTest from './KeyExport.test'; + +export default function testsuite() { + keyExportTest(); +} diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/ohosTest/module.json5 b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/ohosTest/module.json5 new file mode 100755 index 0000000000000000000000000000000000000000..9983b2ba4e55e31a172f0328c82c9a75bfa00ded --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/test/List.test.ets b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/test/List.test.ets new file mode 100755 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/test/LocalUnit.test.ets b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/test/LocalUnit.test.ets new file mode 100755 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/hvigor/hvigor-config.json5 b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/hvigor/hvigor-config.json5 new file mode 100755 index 0000000000000000000000000000000000000000..cef74543b1f97c82275e4bf3434b162395bff2eb --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/hvigorfile.ts b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/hvigorfile.ts new file mode 100755 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/oh-package.json5 b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/oh-package.json5 new file mode 100755 index 0000000000000000000000000000000000000000..eaef52736f22c14e04fca386ba01611ab32a7e4a --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/ohosTest.md b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/ohosTest.md new file mode 100755 index 0000000000000000000000000000000000000000..695e9b34ac4a800119752adaff6701939f5ed23c --- /dev/null +++ b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/ohosTest.md @@ -0,0 +1,9 @@ +# 密钥导出测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ------------------------- | ------------ | ---- | ------------------ | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 点击按钮,密钥导出(ArkTS) | 成功拉起应用 | | 成功执行,输出日志 | 是 | Pass | +| 点击按钮,密钥导出(C/C++) | 成功拉起应用 | | 成功执行,输出日志 | 是 | Pass | \ No newline at end of file diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/screenshots/Cpp_1.png b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/screenshots/Cpp_1.png new file mode 100755 index 0000000000000000000000000000000000000000..46eb269b6ea5804c189b69ea6e2bd4b3ef8a787c Binary files /dev/null and b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/screenshots/Cpp_1.png differ diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/screenshots/Cpp_2.png b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/screenshots/Cpp_2.png new file mode 100755 index 0000000000000000000000000000000000000000..3b94d27ef51554f759ebd8be0fc3881eff293599 Binary files /dev/null and b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/screenshots/Cpp_2.png differ diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/screenshots/KeyExport_1.png b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/screenshots/KeyExport_1.png new file mode 100755 index 0000000000000000000000000000000000000000..64adb68cc032214ffca6fb5c09f9f4224e99c155 Binary files /dev/null and b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/screenshots/KeyExport_1.png differ diff --git a/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/screenshots/KeyExport_2.png b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/screenshots/KeyExport_2.png new file mode 100755 index 0000000000000000000000000000000000000000..c50c89da7db53d4ac46059c337f87f96f1c7bac7 Binary files /dev/null and b/code/DocsSample/Security/UniversalKeystoreKit/OtherOperations/KeyExport/screenshots/KeyExport_2.png differ