mirror of
https://gitee.com/openharmony/security_crypto_framework
synced 2024-11-23 06:49:44 +00:00
add init code
Signed-off-by: lvyuanmin <lvyuanmin@huawei.com>
This commit is contained in:
parent
d4a8bc00f7
commit
11f54db889
45
BUILD.gn
Normal file
45
BUILD.gn
Normal file
@ -0,0 +1,45 @@
|
||||
# Copyright (C) 2022 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.
|
||||
|
||||
group("crypto_framework_lib") {
|
||||
if (os_level == "standard") {
|
||||
deps = [
|
||||
"//base/security/crypto_framework/frameworks:crypto_framework_lib",
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
group("crypto_openssl_plugin_lib") {
|
||||
if (os_level == "standard") {
|
||||
deps = [
|
||||
"//base/security/crypto_framework/plugin:crypto_openssl_plugin_lib",
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
group("cryptoframework_napi") {
|
||||
if (os_level == "standard") {
|
||||
deps = [
|
||||
"//base/security/crypto_framework/frameworks:cryptoframework_napi",
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
group("crypto_framework_test") {
|
||||
testonly = true
|
||||
if (os_level == "standard") {
|
||||
deps = [
|
||||
"//base/security/crypto_framework/test/unittest:crypto_framework_test",
|
||||
]
|
||||
}
|
||||
}
|
177
LICENSE
Normal file
177
LICENSE
Normal file
@ -0,0 +1,177 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
36
README.en.md
36
README.en.md
@ -1,36 +0,0 @@
|
||||
# security_crypto_framework
|
||||
|
||||
#### Description
|
||||
{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**}
|
||||
|
||||
#### Software Architecture
|
||||
Software architecture description
|
||||
|
||||
#### Installation
|
||||
|
||||
1. xxxx
|
||||
2. xxxx
|
||||
3. xxxx
|
||||
|
||||
#### Instructions
|
||||
|
||||
1. xxxx
|
||||
2. xxxx
|
||||
3. xxxx
|
||||
|
||||
#### Contribution
|
||||
|
||||
1. Fork the repository
|
||||
2. Create Feat_xxx branch
|
||||
3. Commit your code
|
||||
4. Create Pull Request
|
||||
|
||||
|
||||
#### Gitee Feature
|
||||
|
||||
1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
|
||||
2. Gitee blog [blog.gitee.com](https://blog.gitee.com)
|
||||
3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
|
||||
4. The most valuable open source project [GVP](https://gitee.com/gvp)
|
||||
5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
|
||||
6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
|
39
README.md
39
README.md
@ -1,39 +0,0 @@
|
||||
# security_crypto_framework
|
||||
|
||||
#### 介绍
|
||||
{**以下是 Gitee 平台说明,您可以替换此简介**
|
||||
Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台
|
||||
无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)}
|
||||
|
||||
#### 软件架构
|
||||
软件架构说明
|
||||
|
||||
|
||||
#### 安装教程
|
||||
|
||||
1. xxxx
|
||||
2. xxxx
|
||||
3. xxxx
|
||||
|
||||
#### 使用说明
|
||||
|
||||
1. xxxx
|
||||
2. xxxx
|
||||
3. xxxx
|
||||
|
||||
#### 参与贡献
|
||||
|
||||
1. Fork 本仓库
|
||||
2. 新建 Feat_xxx 分支
|
||||
3. 提交代码
|
||||
4. 新建 Pull Request
|
||||
|
||||
|
||||
#### 特技
|
||||
|
||||
1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md
|
||||
2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com)
|
||||
3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目
|
||||
4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
|
||||
5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help)
|
||||
6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
|
30
README_zh.md
Normal file
30
README_zh.md
Normal file
@ -0,0 +1,30 @@
|
||||
# 加解密算法库框架
|
||||
|
||||
## 简介
|
||||
为屏蔽底层硬件和算法库,向上提供统一的密码算法库加解密和证书相关接口。
|
||||
|
||||
## 目录
|
||||
```
|
||||
base/security/crypto_framwork
|
||||
├── build # 配置构建相关
|
||||
├── interfaces # 对外接口目录
|
||||
├── test # unitest
|
||||
├── common # 内部依赖的公共方法
|
||||
├── plugin # 算法适配的插件
|
||||
│ └── openssl_plugin # openssl 插件
|
||||
├── frameworks # api调用SPI的实现
|
||||
│ ├── spi # 放SPI的头文件
|
||||
│ ├── js
|
||||
│ └── napi # 通过napi封装的JS接口代码实现
|
||||
│ ├── algorithm_parameter # 算法参数
|
||||
│ ├── certificate # 证书
|
||||
│ ├── crypto_operation # 算法操作,包括mac、md、加解密、签名验签、秘钥协商
|
||||
│ ├── key
|
||||
│ └── rand
|
||||
```
|
||||
|
||||
## 相关仓
|
||||
|
||||
**安全子系统**
|
||||
|
||||
[security\_crypto\_framwork](https://gitee.com/openharmony/security_crypto_framwork)
|
15
build/config.gni
Normal file
15
build/config.gni
Normal file
@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
45
bundle.json
Normal file
45
bundle.json
Normal file
@ -0,0 +1,45 @@
|
||||
{
|
||||
"name": "@ohos/crypto_framework",
|
||||
"version": "3.1",
|
||||
"description": "Openharmony's crypto framework.",
|
||||
"publishAs": "code-segment",
|
||||
"segment": {
|
||||
"destPath": "base/security/crypto_framework"
|
||||
},
|
||||
"dirs": {},
|
||||
"scripts": {},
|
||||
"author": {},
|
||||
"repository": "",
|
||||
"license": "Apache License 2.0",
|
||||
"component": {
|
||||
"name": "crypto_framework",
|
||||
"subsystem": "security",
|
||||
"syscap": [ "SystemCapability.Security.CryptoFramework" ],
|
||||
"features": [],
|
||||
"adapted_system_type": [
|
||||
"standard"
|
||||
],
|
||||
"rom": "1024KB",
|
||||
"ram": "",
|
||||
"deps": {
|
||||
"components": [
|
||||
"hiviewdfx_hilog_native"
|
||||
],
|
||||
"third_party": [
|
||||
"bounds_checking_function",
|
||||
"openssl"
|
||||
]
|
||||
},
|
||||
"build": {
|
||||
"sub_component": [
|
||||
"//base/security/crypto_framework:crypto_framework_lib",
|
||||
"//base/security/crypto_framework:crypto_openssl_plugin_lib",
|
||||
"//base/security/crypto_framework:cryptoframework_napi"
|
||||
],
|
||||
"inner_kits": [],
|
||||
"test": [
|
||||
"//base/security/crypto_framework:crypto_framework_test"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
42
common/BUILD.gn
Normal file
42
common/BUILD.gn
Normal file
@ -0,0 +1,42 @@
|
||||
# Copyright (C) 2022 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("//build/ohos.gni")
|
||||
import("//base/security/crypto_framework/common/common.gni")
|
||||
|
||||
ohos_static_library("crypto_plugin_common") {
|
||||
subsystem_name = "security"
|
||||
part_name = "crypto_framework"
|
||||
include_dirs = [
|
||||
"//utils/native/base/include",
|
||||
"//base/hiviewdfx/hilog/interfaces/native/innerkits/include",
|
||||
]
|
||||
include_dirs += crypto_framwork_common_inc_path
|
||||
|
||||
sources = crypto_framwork_common_files
|
||||
|
||||
defines = [ "HILOG_ENABLE" ]
|
||||
cflags = [
|
||||
"-DHILOG_ENABLE",
|
||||
"-fPIC",
|
||||
"-Wall",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//utils/native/base:utils",
|
||||
]
|
||||
|
||||
external_deps = [
|
||||
"hiviewdfx_hilog_native:libhilog",
|
||||
]
|
||||
}
|
29
common/common.gni
Normal file
29
common/common.gni
Normal file
@ -0,0 +1,29 @@
|
||||
# Copyright (C) 2022 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.
|
||||
|
||||
crypto_framwork_common_inc_path = [
|
||||
"//base/security/crypto_framework/interfaces/innerkits/common",
|
||||
"//base/security/crypto_framework/common/inc",
|
||||
]
|
||||
|
||||
framework_common_util_files = [
|
||||
"//base/security/crypto_framework/common/src/blob.c",
|
||||
"//base/security/crypto_framework/common/src/utils.c",
|
||||
"//base/security/crypto_framework/common/src/log.c",
|
||||
"//base/security/crypto_framework/common/src/memory.c",
|
||||
"//base/security/crypto_framework/common/src/hcf_parcel.c",
|
||||
"//base/security/crypto_framework/common/src/hcf_string.c",
|
||||
"//base/security/crypto_framework/common/src/params_parser.c",
|
||||
]
|
||||
|
||||
crypto_framwork_common_files = framework_common_util_files
|
24
common/inc/config.h
Normal file
24
common/inc/config.h
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_CONFIG_H
|
||||
#define HCF_CONFIG_H
|
||||
|
||||
#define HCF_MAX_STR_LEN 1024 // input string parameter max length limit, include \0
|
||||
#define HCF_MAX_ALGO_NAME_LEN 128 // input algoName parameter max length limit, include \0
|
||||
#define LOG_PRINT_MAX_LEN 1024 // log max length limit
|
||||
#define HCF_MAX_BUFFER_LEN 8192
|
||||
|
||||
#endif
|
29
common/inc/fwk_class.h
Normal file
29
common/inc/fwk_class.h
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_FWK_CLASS_H
|
||||
#define HCF_FWK_CLASS_H
|
||||
|
||||
#include "x509_certificate.h"
|
||||
#include "x509_certificate_spi.h"
|
||||
|
||||
typedef struct {
|
||||
HcfX509Certificate base;
|
||||
HcfX509CertificateSpi *spiObj;
|
||||
} HcfX509CertificateImpl;
|
||||
|
||||
#define HCF_X509_CERTIFICATE_CLASS "HcfX509Certificate"
|
||||
|
||||
#endif
|
97
common/inc/hcf_parcel.h
Normal file
97
common/inc/hcf_parcel.h
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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.
|
||||
*/
|
||||
|
||||
#ifndef HC_PARCEL_H
|
||||
#define HC_PARCEL_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "memory.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define PARCEL_DEFAULT_LENGTH 0
|
||||
#define PARCEL_DEFAULT_ALLOC_UNIT 0
|
||||
|
||||
typedef struct {
|
||||
char *data;
|
||||
unsigned int beginPos;
|
||||
unsigned int endPos;
|
||||
unsigned int length;
|
||||
unsigned int allocUnit;
|
||||
} HcParcel;
|
||||
|
||||
HcParcel CreateParcel(uint32_t size, uint32_t allocUnit);
|
||||
void DeleteParcel(HcParcel *parcel);
|
||||
void ClearParcel(HcParcel *parcel);
|
||||
void ResetParcel(HcParcel *parcel, uint32_t size, uint32_t allocUnit);
|
||||
bool ParcelReadWithoutPopData(HcParcel *parcel, void *dst, uint32_t dataSize);
|
||||
bool ParcelRead(HcParcel *parcel, void *dst, uint32_t dataSize);
|
||||
bool ParcelWrite(HcParcel *parcel, const void *src, uint32_t dataSize);
|
||||
bool ParcelReadRevert(HcParcel *parcel, void *dst, uint32_t dataSize);
|
||||
bool ParcelWriteRevert(HcParcel *parcel, const void *src, uint32_t dataSize);
|
||||
uint32_t GetParcelDataSize(const HcParcel *parcel);
|
||||
const char *GetParcelData(const HcParcel *parcel);
|
||||
const char* GetParcelLastChar(const HcParcel *parcel);
|
||||
|
||||
bool ParcelReadInt32(HcParcel *parcel, int *dst);
|
||||
bool ParcelReadUint32(HcParcel *parcel, uint32_t *dst);
|
||||
bool ParcelReadInt16(HcParcel *parcel, short *dst);
|
||||
bool ParcelReadUint16(HcParcel *parcel, uint16_t *dst);
|
||||
bool ParcelReadInt8(HcParcel *parcel, char *dst);
|
||||
bool ParcelReadUint8(HcParcel *parcel, uint8_t *dst);
|
||||
bool ParcelReadUint64(HcParcel *parcel, uint64_t *dst);
|
||||
bool ParcelReadInt64(HcParcel *parcel, int64_t *dst);
|
||||
bool ParcelWriteInt32(HcParcel *parcel, int src);
|
||||
bool ParcelWriteUint32(HcParcel *parcel, uint32_t src);
|
||||
bool ParcelWriteInt16(HcParcel *parcel, short src);
|
||||
bool ParcelWriteUint16(HcParcel *parcel, uint16_t src);
|
||||
bool ParcelWriteInt8(HcParcel *parcel, char src);
|
||||
bool ParcelWriteUint8(HcParcel *parcel, uint8_t src);
|
||||
bool ParcelWriteUint64(HcParcel *parcel, uint64_t src);
|
||||
bool ParcelWriteInt64(HcParcel *parcel, int64_t src);
|
||||
bool ParcelWriteString(HcParcel *parcel, const char *str);
|
||||
bool ParcelReadString(HcParcel *parcel, char **str);
|
||||
bool ParcelReadParcel(HcParcel *src, HcParcel *dst, uint32_t size, bool copy);
|
||||
bool ParcelCopy(HcParcel *src, HcParcel *dst);
|
||||
|
||||
bool ParcelReadInt32Revert(HcParcel *parcel, int32_t *dst);
|
||||
bool ParcelReadUint32Revert(HcParcel *parcel, uint32_t *dst);
|
||||
bool ParcelReadInt16Revert(HcParcel *parcel, short *dst);
|
||||
bool ParcelReadUint16Revert(HcParcel *parcel, uint16_t *dst);
|
||||
bool ParcelReadInt8Revert(HcParcel *parcel, char *dst);
|
||||
bool ParcelReadUint8Revert(HcParcel *parcel, uint8_t *dst);
|
||||
bool ParcelReadUint64Revert(HcParcel *parcel, uint64_t *dst);
|
||||
bool ParcelReadInt64Revert(HcParcel *parcel, int64_t *dst);
|
||||
bool ParcelWriteInt32Revert(HcParcel *parcel, int src);
|
||||
bool ParcelWriteUint32Revert(HcParcel *parcel, uint32_t src);
|
||||
bool ParcelWriteInt16Revert(HcParcel *parcel, short src);
|
||||
bool ParcelWriteUint16Revert(HcParcel *parcel, uint16_t src);
|
||||
bool ParcelWriteInt8Revert(HcParcel *parcel, char src);
|
||||
bool ParcelWriteUint8Revert(HcParcel *parcel, uint8_t src);
|
||||
bool ParcelWriteUint64Revert(HcParcel *parcel, uint64_t src);
|
||||
bool ParcelWriteInt64Revert(HcParcel *parcel, int64_t src);
|
||||
|
||||
void DataRevert(void *data, uint32_t length);
|
||||
bool ParcelPopBack(HcParcel *parcel, uint32_t size);
|
||||
bool ParcelPopFront(HcParcel *parcel, uint32_t size);
|
||||
bool ParcelEraseBlock(HcParcel *parcel, uint32_t start, uint32_t data_size, void *dst);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
147
common/inc/hcf_string.h
Normal file
147
common/inc/hcf_string.h
Normal file
@ -0,0 +1,147 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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.
|
||||
*/
|
||||
|
||||
#ifndef HC_STRING_H
|
||||
#define HC_STRING_H
|
||||
|
||||
#include "hcf_parcel.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct HcString {
|
||||
HcParcel parcel; // parcel data, used to storage the string data
|
||||
} HcString;
|
||||
|
||||
/*
|
||||
* Append a HcString
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: append string.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringAppend(HcString *self, HcString str);
|
||||
|
||||
/*
|
||||
* Append string pointer
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: string pointer.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringAppendPointer(HcString *self, const char *str);
|
||||
|
||||
/*
|
||||
* Append a char
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: char.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringAppendChar(HcString *self, char c);
|
||||
|
||||
/*
|
||||
* Assign a value to the HcString
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: assign value of ta_sting.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringSet(HcString *self, HcString str);
|
||||
|
||||
/*
|
||||
* Assign a value to the HcString
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: assign value of string pointer.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringSetPointer(HcString *self, const char *str);
|
||||
|
||||
/*
|
||||
* Assign a value to the HcString with fixed length
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: assign value of string pointer.
|
||||
* @param len: the length of string.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringSetPointerWithLength(HcString* self, const char *str, uint32_t len);
|
||||
|
||||
/*
|
||||
* Get the string pointer data
|
||||
* @param self: self pointer.
|
||||
* @return the pointer data of the string
|
||||
*/
|
||||
const char* StringGet(const HcString *self);
|
||||
|
||||
/*
|
||||
* Get the length of the string
|
||||
* @param self: self pointer.
|
||||
* @return the length of the string
|
||||
*/
|
||||
uint32_t StringLength(const HcString *self);
|
||||
|
||||
/*
|
||||
* Find a char from string
|
||||
* @param self: self pointer.
|
||||
* @param c: the char you want find
|
||||
* @param begin: the position find from
|
||||
* @return the position of the char
|
||||
*/
|
||||
int StringFind(const HcString *self, char c, uint32_t begin);
|
||||
|
||||
/*
|
||||
* Get sub string from a string.
|
||||
* @param self: self pointer.
|
||||
* @param begin: the begin position of the sub string.
|
||||
* @param len: the length of the sub string.
|
||||
* @param dst: the string pointer which saved the sub string content.
|
||||
* @return the operation result.
|
||||
*/
|
||||
bool StringSubString(const HcString *self, uint32_t begin, uint32_t len, HcString* dst);
|
||||
|
||||
/*
|
||||
* Compare the string with another string.
|
||||
* @param self: self pointer.
|
||||
* @param dst: the pointer of another string.
|
||||
* @return the compare result.
|
||||
* -1: self is smaller than dst
|
||||
* 0: self is equal with dst
|
||||
* 1: self is bigger than dst
|
||||
*/
|
||||
int StringCompare(const HcString *self, const char* dst);
|
||||
|
||||
/*
|
||||
* Create a string.
|
||||
* Notice: You should delete string when you don't need the string anymore.
|
||||
* @return the created string.
|
||||
*/
|
||||
HcString CreateString(void);
|
||||
|
||||
/*
|
||||
* Delete a string. In fact it will not destroy the string,
|
||||
* but only free the allocated memory of the string and reset the member's value
|
||||
* of the string. You can continue to use the string if you want.
|
||||
* Notice: You should delete the string when you don't need it any more to avoid memory leak.
|
||||
* @param str: The string you want to delete.
|
||||
*/
|
||||
void DeleteString(HcString *str);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
61
common/inc/log.h
Normal file
61
common/inc/log.h
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_LOG_H
|
||||
#define HCF_LOG_H
|
||||
|
||||
#ifdef HILOG_ENABLE
|
||||
|
||||
typedef enum {
|
||||
HCF_LOG_LEVEL_DEBUG = 0,
|
||||
HCF_LOG_LEVEL_INFO = 1,
|
||||
HCF_LOG_LEVEL_WARN = 2,
|
||||
HCF_LOG_LEVEL_ERROR = 3
|
||||
} HcfLogLevel;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void HcfLogPrint(HcfLogLevel level, const char *funName, const char *fmt, ...);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#include "hilog/log.h"
|
||||
|
||||
#define LOGD(fmt, ...) (HcfLogPrint(HCF_LOG_LEVEL_DEBUG, __FUNCTION__, fmt, ##__VA_ARGS__))
|
||||
#define LOGI(fmt, ...) (HcfLogPrint(HCF_LOG_LEVEL_INFO, __FUNCTION__, fmt, ##__VA_ARGS__))
|
||||
#define LOGW(fmt, ...) (HcfLogPrint(HCF_LOG_LEVEL_WARN, __FUNCTION__, fmt, ##__VA_ARGS__))
|
||||
#define LOGE(fmt, ...) (HcfLogPrint(HCF_LOG_LEVEL_ERROR, __FUNCTION__, fmt, ##__VA_ARGS__))
|
||||
|
||||
#define HCF_LOG_DEBUG(fmt, ...) HiLogPrint(LOG_CORE, LOG_DEBUG, LOG_DOMAIN, "[HCF]", "%{public}s", buf)
|
||||
#define HCF_LOG_INFO(buf) HiLogPrint(LOG_CORE, LOG_INFO, LOG_DOMAIN, "[HCF]", "%{public}s", buf)
|
||||
#define HCF_LOG_WARN(buf) HiLogPrint(LOG_CORE, LOG_WARN, LOG_DOMAIN, "[HCF]", "%{public}s", buf)
|
||||
#define HCF_LOG_ERROR(buf) HiLogPrint(LOG_CORE, LOG_ERROR, LOG_DOMAIN, "[HCF]", "%{public}s", buf)
|
||||
|
||||
#else
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LOGD(fmt, ...) printf("[HCF][D][%s]: " fmt "\n", __FUNCTION__, ##__VA_ARGS__)
|
||||
#define LOGI(fmt, ...) printf("[HCF][I][%s]: " fmt "\n", __FUNCTION__, ##__VA_ARGS__)
|
||||
#define LOGW(fmt, ...) printf("[HCF][W][%s]: " fmt "\n", __FUNCTION__, ##__VA_ARGS__)
|
||||
#define LOGE(fmt, ...) printf("[HCF][E][%s]: " fmt "\n", __FUNCTION__, ##__VA_ARGS__)
|
||||
|
||||
#endif
|
||||
#endif
|
32
common/inc/memory.h
Normal file
32
common/inc/memory.h
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_MEMORY_H
|
||||
#define HCF_MEMORY_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void* HcfMalloc(uint32_t size, char val);
|
||||
void HcfFree(void* addr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
142
common/inc/params_parser.h
Normal file
142
common/inc/params_parser.h
Normal file
@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_PARAMS_PARSER_H
|
||||
#define HCF_PARAMS_PARSER_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "result.h"
|
||||
|
||||
typedef enum {
|
||||
HCF_ALG_TYPE = 1,
|
||||
HCF_ALG_KEY_TYPE,
|
||||
HCF_ALG_MODE,
|
||||
HCF_ALG_PADDING_TYPE,
|
||||
HCF_ALG_PRIMES,
|
||||
HCF_ALG_DIGEST,
|
||||
HCF_ALG_MGF1_DIGEST,
|
||||
} HCF_ALG_PARA_TYPE;
|
||||
|
||||
typedef enum {
|
||||
HCF_ALG_AES = 1,
|
||||
HCF_ALG_DES,
|
||||
HCF_ALG_RSA,
|
||||
HCF_ALG_ECC,
|
||||
} HCF_ALG_VALUE;
|
||||
|
||||
typedef enum {
|
||||
HCF_ALG_ECC_224 = 1,
|
||||
HCF_ALG_ECC_256,
|
||||
HCF_ALG_ECC_384,
|
||||
HCF_ALG_ECC_512,
|
||||
|
||||
HCF_ALG_AES_128,
|
||||
HCF_ALG_AES_192,
|
||||
HCF_ALG_AES_256,
|
||||
HCF_ALG_3DES_192,
|
||||
|
||||
HCF_ALG_MODE_NONE,
|
||||
HCF_ALG_MODE_ECB,
|
||||
HCF_ALG_MODE_CBC,
|
||||
HCF_ALG_MODE_CTR,
|
||||
HCF_ALG_MODE_OFB,
|
||||
HCF_ALG_MODE_CFB,
|
||||
HCF_ALG_MODE_CFB1,
|
||||
HCF_ALG_MODE_CFB8,
|
||||
HCF_ALG_MODE_CFB64,
|
||||
HCF_ALG_MODE_CFB128,
|
||||
HCF_ALG_MODE_CCM,
|
||||
HCF_ALG_MODE_GCM,
|
||||
|
||||
HCF_ALG_NOPADDING,
|
||||
HCF_ALG_PADDING_PKCS5,
|
||||
HCF_ALG_PADDING_PKCS7,
|
||||
|
||||
// rsa keysize
|
||||
HCF_OPENSSL_RSA_512,
|
||||
HCF_OPENSSL_RSA_768,
|
||||
HCF_OPENSSL_RSA_1024,
|
||||
HCF_OPENSSL_RSA_2048,
|
||||
HCF_OPENSSL_RSA_3072,
|
||||
HCF_OPENSSL_RSA_4096,
|
||||
HCF_OPENSSL_RSA_8192,
|
||||
|
||||
// rsa cipher padding,
|
||||
HCF_OPENSSL_RSA_PKCS1_PADDING,
|
||||
HCF_OPENSSL_RSA_PKCS1_OAEP_PADDING,
|
||||
HCF_OPENSSL_RSA_PSS_PADDING,
|
||||
|
||||
// digest
|
||||
HCF_OPENSSL_DIGEST_NONE,
|
||||
HCF_OPENSSL_DIGEST_MD5,
|
||||
HCF_OPENSSL_DIGEST_SHA1,
|
||||
HCF_OPENSSL_DIGEST_SHA224,
|
||||
HCF_OPENSSL_DIGEST_SHA256,
|
||||
HCF_OPENSSL_DIGEST_SHA384,
|
||||
HCF_OPENSSL_DIGEST_SHA512,
|
||||
|
||||
// primes
|
||||
HCF_OPENSSL_PRIMES_2,
|
||||
HCF_OPENSSL_PRIMES_3,
|
||||
HCF_OPENSSL_PRIMES_4,
|
||||
HCF_OPENSSL_PRIMES_5,
|
||||
} HCF_ALG_PARA_VALUE;
|
||||
|
||||
typedef struct {
|
||||
const char* tag;
|
||||
HCF_ALG_PARA_TYPE para_type;
|
||||
HCF_ALG_PARA_VALUE para_value;
|
||||
} HcfParaConfig;
|
||||
|
||||
typedef struct {
|
||||
HCF_ALG_VALUE algo;
|
||||
HCF_ALG_PARA_VALUE keySize;
|
||||
HCF_ALG_PARA_VALUE mode;
|
||||
HCF_ALG_PARA_VALUE paddingMode;
|
||||
HCF_ALG_PARA_VALUE md;
|
||||
HCF_ALG_PARA_VALUE mgf1md;
|
||||
} CipherAttr;
|
||||
|
||||
typedef struct {
|
||||
HCF_ALG_VALUE algo; // algType
|
||||
int32_t bits; // keyLen
|
||||
int32_t primes; // number of primes
|
||||
} HcfAsyKeyGenParams;
|
||||
|
||||
typedef struct {
|
||||
HCF_ALG_VALUE algo; // algType
|
||||
HCF_ALG_PARA_VALUE keyLen;
|
||||
HCF_ALG_PARA_VALUE padding;
|
||||
HCF_ALG_PARA_VALUE md;
|
||||
HCF_ALG_PARA_VALUE mgf1md;
|
||||
} HcfSignatureParams;
|
||||
|
||||
typedef struct {
|
||||
HCF_ALG_VALUE algo;
|
||||
HCF_ALG_PARA_VALUE keyLen;
|
||||
} HcfKeyAgreementParams;
|
||||
|
||||
typedef HcfResult (*SetParameterFunc) (const HcfParaConfig* config, void *params);
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
HcfResult ParseAndSetParameter(const char* paramsStr, void *params, SetParameterFunc setFunc);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
36
common/inc/utils.h
Normal file
36
common/inc/utils.h
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_UTILS_H
|
||||
#define HCF_UTILS_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "blob.h"
|
||||
#include "object_base.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
bool IsStrValid(const char *str, uint32_t maxLen);
|
||||
bool IsBlobValid(const HcfBlob *blob);
|
||||
bool IsClassMatch(const HcfObjectBase *obj, const char *className);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
68
common/src/blob.c
Normal file
68
common/src/blob.c
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "blob.h"
|
||||
|
||||
#include <securec.h>
|
||||
#include "memory.h"
|
||||
#include "log.h"
|
||||
|
||||
void HcfBlobDataFree(HcfBlob *blob)
|
||||
{
|
||||
if ((blob == NULL) || (blob->data == NULL)) {
|
||||
return;
|
||||
}
|
||||
HcfFree(blob->data);
|
||||
blob->data = NULL;
|
||||
blob->len = 0;
|
||||
}
|
||||
|
||||
void HcfBlobDataClearAndFree(HcfBlob *blob)
|
||||
{
|
||||
if ((blob == NULL) || (blob->data == NULL)) {
|
||||
LOGD("The input blob is null, no need to free.");
|
||||
return;
|
||||
}
|
||||
(void)memset_s(blob->data, blob->len, 0, blob->len);
|
||||
HcfFree(blob->data);
|
||||
blob->data = NULL;
|
||||
blob->len = 0;
|
||||
}
|
||||
|
||||
void HcfEncodingBlobDataFree(HcfEncodingBlob *encodingBlob)
|
||||
{
|
||||
if ((encodingBlob == NULL) || (encodingBlob->data == NULL)) {
|
||||
LOGD("The input encodingBlob is null, no need to free.");
|
||||
return;
|
||||
}
|
||||
HcfFree(encodingBlob->data);
|
||||
encodingBlob->data = NULL;
|
||||
encodingBlob->len = 0;
|
||||
}
|
||||
|
||||
void HcfArrayDataClearAndFree(HcfArray *array)
|
||||
{
|
||||
if (array == NULL) {
|
||||
LOGD("The input array is null, no need to free.");
|
||||
return;
|
||||
}
|
||||
for (uint32_t i = 0; i < array->count; ++i) {
|
||||
HcfFree(array->data[i].data);
|
||||
array->data[i].data = NULL;
|
||||
array->data[i].len = 0;
|
||||
}
|
||||
HcfFree(array->data);
|
||||
array->data = NULL;
|
||||
}
|
539
common/src/hcf_parcel.c
Normal file
539
common/src/hcf_parcel.c
Normal file
@ -0,0 +1,539 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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 "hcf_parcel.h"
|
||||
#include "securec.h"
|
||||
#include "memory.h"
|
||||
|
||||
const int PARCEL_DEFAULT_INCREASE_STEP = 16;
|
||||
const uint32_t PARCEL_UINT_MAX = 0xffffffffU;
|
||||
const int HALF_LEN = 2;
|
||||
|
||||
HcParcel CreateParcel(uint32_t size, uint32_t allocUnit)
|
||||
{
|
||||
HcParcel parcel;
|
||||
(void)memset_s(&parcel, sizeof(parcel), 0, sizeof(parcel));
|
||||
parcel.allocUnit = allocUnit;
|
||||
if (parcel.allocUnit == 0) {
|
||||
parcel.allocUnit = PARCEL_DEFAULT_INCREASE_STEP;
|
||||
}
|
||||
if (size > 0) {
|
||||
parcel.data = (char *)HcfMalloc(size, 0);
|
||||
if (parcel.data != NULL) {
|
||||
parcel.length = size;
|
||||
}
|
||||
}
|
||||
return parcel;
|
||||
}
|
||||
|
||||
void DeleteParcel(HcParcel *parcel)
|
||||
{
|
||||
if (parcel == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (parcel->data != NULL) {
|
||||
HcfFree(parcel->data);
|
||||
parcel->data = 0;
|
||||
}
|
||||
parcel->length = 0;
|
||||
parcel->beginPos = 0;
|
||||
parcel->endPos = 0;
|
||||
}
|
||||
|
||||
void ClearParcel(HcParcel *parcel)
|
||||
{
|
||||
if (parcel != NULL) {
|
||||
parcel->beginPos = 0;
|
||||
parcel->endPos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void ResetParcel(HcParcel *parcel, uint32_t size, uint32_t allocUnit)
|
||||
{
|
||||
if (parcel != NULL) {
|
||||
DeleteParcel(parcel);
|
||||
HcParcel newParcel = CreateParcel(size, allocUnit);
|
||||
(void)memcpy_s(parcel, sizeof(HcParcel), &newParcel, sizeof(HcParcel));
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t GetParcelDataSize(const HcParcel *parcel)
|
||||
{
|
||||
if (parcel == NULL) {
|
||||
return 0;
|
||||
}
|
||||
if (parcel->endPos >= parcel->beginPos) {
|
||||
return parcel->endPos - parcel->beginPos;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *GetParcelData(const HcParcel *parcel)
|
||||
{
|
||||
if (parcel == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return parcel->data + parcel->beginPos;
|
||||
}
|
||||
|
||||
const char *GetParcelLastChar(const HcParcel *parcel)
|
||||
{
|
||||
if (parcel == NULL || GetParcelDataSize(parcel) == 0) {
|
||||
return NULL;
|
||||
}
|
||||
return parcel->data + parcel->endPos - 1;
|
||||
}
|
||||
|
||||
bool ParcelReadWithoutPopData(HcParcel *parcel, void *dst, uint32_t dataSize)
|
||||
{
|
||||
#ifdef IS_BIG_ENDIAN
|
||||
bool ret = ParcelReadRevert(parcel, dst, dataSize);
|
||||
#else
|
||||
bool ret = ParcelRead(parcel, dst, dataSize);
|
||||
#endif
|
||||
if (ret == true) {
|
||||
parcel->beginPos -= dataSize;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ParcelRead(HcParcel *parcel, void *dst, uint32_t dataSize)
|
||||
{
|
||||
errno_t rc;
|
||||
if (parcel == NULL || dst == NULL || dataSize == 0) {
|
||||
return false;
|
||||
}
|
||||
if (parcel->beginPos > PARCEL_UINT_MAX - dataSize) {
|
||||
return false;
|
||||
}
|
||||
if (parcel->beginPos + dataSize > parcel->endPos) {
|
||||
return false;
|
||||
}
|
||||
rc = memmove_s(dst, dataSize, parcel->data + parcel->beginPos, dataSize);
|
||||
if (rc != EOK) {
|
||||
return false;
|
||||
}
|
||||
parcel->beginPos += dataSize;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ParcelEraseBlock(HcParcel *parcel, uint32_t start, uint32_t dataSize, void *dst)
|
||||
{
|
||||
errno_t rc;
|
||||
if (parcel == NULL || dst == NULL || dataSize == 0) {
|
||||
return false;
|
||||
}
|
||||
if (start > PARCEL_UINT_MAX - dataSize) {
|
||||
return false;
|
||||
}
|
||||
uint32_t parcelSizeOrg = GetParcelDataSize(parcel);
|
||||
if (parcelSizeOrg < start + dataSize) {
|
||||
return false;
|
||||
}
|
||||
char *beginCopy = parcel->data + parcel->beginPos + start;
|
||||
uint32_t copySize = parcelSizeOrg - start - dataSize;
|
||||
|
||||
rc = memmove_s(dst, dataSize, beginCopy, dataSize);
|
||||
if (rc != EOK) {
|
||||
return false;
|
||||
}
|
||||
if (copySize != 0) {
|
||||
rc = memmove_s(beginCopy, copySize, beginCopy + dataSize, copySize);
|
||||
if (rc != EOK) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
parcel->endPos -= dataSize;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ParcelReadRevert(HcParcel *parcel, void *dst, uint32_t dataSize)
|
||||
{
|
||||
if (ParcelRead(parcel, dst, dataSize)) {
|
||||
DataRevert(dst, dataSize);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ParcelWriteRevert(HcParcel *parcel, const void *src, uint32_t dataSize)
|
||||
{
|
||||
errno_t rc;
|
||||
void *srcCopy = HcfMalloc(dataSize, 0);
|
||||
if (srcCopy == NULL) {
|
||||
return false;
|
||||
}
|
||||
rc = memmove_s(srcCopy, dataSize, src, dataSize);
|
||||
if (rc != EOK) {
|
||||
HcfFree(srcCopy);
|
||||
return false;
|
||||
}
|
||||
DataRevert(srcCopy, dataSize);
|
||||
bool ret = ParcelWrite(parcel, srcCopy, dataSize);
|
||||
HcfFree(srcCopy);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ParcelReadInt32(HcParcel *parcel, int *dst)
|
||||
{
|
||||
return ParcelRead(parcel, dst, sizeof(int));
|
||||
}
|
||||
|
||||
bool ParcelReadUint32(HcParcel *parcel, uint32_t *dst)
|
||||
{
|
||||
return ParcelRead(parcel, dst, sizeof(uint32_t));
|
||||
}
|
||||
|
||||
bool ParcelReadInt16(HcParcel *parcel, short *dst)
|
||||
{
|
||||
return ParcelRead(parcel, dst, sizeof(short));
|
||||
}
|
||||
|
||||
bool ParcelReadUint16(HcParcel *parcel, uint16_t *dst)
|
||||
{
|
||||
return ParcelRead(parcel, dst, sizeof(uint16_t));
|
||||
}
|
||||
|
||||
bool ParcelReadInt8(HcParcel *parcel, char *dst)
|
||||
{
|
||||
return ParcelRead(parcel, dst, sizeof(char));
|
||||
}
|
||||
|
||||
bool ParcelReadUint8(HcParcel *parcel, uint8_t *dst)
|
||||
{
|
||||
return ParcelRead(parcel, dst, sizeof(uint8_t));
|
||||
}
|
||||
|
||||
bool ParcelReadUint64(HcParcel *parcel, uint64_t *dst)
|
||||
{
|
||||
return ParcelRead(parcel, dst, sizeof(uint64_t));
|
||||
}
|
||||
|
||||
bool ParcelReadInt64(HcParcel *parcel, int64_t *dst)
|
||||
{
|
||||
return ParcelRead(parcel, dst, sizeof(int64_t));
|
||||
}
|
||||
|
||||
static bool ParcelRealloc(HcParcel *parcel, uint32_t size)
|
||||
{
|
||||
if (parcel->length >= size) {
|
||||
return false;
|
||||
}
|
||||
char *newData = (char *)HcfMalloc(size, 0);
|
||||
if (newData == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (memcpy_s(newData, size, parcel->data, parcel->length) != EOK) {
|
||||
HcfFree(newData);
|
||||
return false;
|
||||
}
|
||||
HcfFree(parcel->data);
|
||||
parcel->data = newData;
|
||||
parcel->length = size;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool ParcelIncrease(HcParcel *parcel, uint32_t size)
|
||||
{
|
||||
if (parcel == NULL || size == 0) {
|
||||
return false;
|
||||
}
|
||||
if (parcel->data == NULL) {
|
||||
if (parcel->length != 0) {
|
||||
return false;
|
||||
}
|
||||
*parcel = CreateParcel(size, parcel->allocUnit);
|
||||
if (parcel->data == NULL) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return ParcelRealloc(parcel, size);
|
||||
}
|
||||
}
|
||||
|
||||
static void ParcelRecycle(HcParcel *parcel)
|
||||
{
|
||||
if (parcel == NULL) {
|
||||
return;
|
||||
}
|
||||
if (parcel->data == NULL || parcel->beginPos < parcel->allocUnit) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t contentSize = parcel->endPos - parcel->beginPos;
|
||||
if (contentSize > 0) {
|
||||
if (memmove_s(parcel->data, parcel->endPos - parcel->beginPos,
|
||||
parcel->data + parcel->beginPos, parcel->endPos - parcel->beginPos) != EOK) {
|
||||
}
|
||||
}
|
||||
parcel->beginPos = 0;
|
||||
parcel->endPos = contentSize;
|
||||
}
|
||||
|
||||
static uint32_t GetParcelIncreaseSize(HcParcel *parcel, uint32_t newSize)
|
||||
{
|
||||
if (parcel == NULL || parcel->allocUnit == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (newSize % parcel->allocUnit) {
|
||||
return (newSize / parcel->allocUnit + 1) * parcel->allocUnit;
|
||||
} else {
|
||||
return (newSize / parcel->allocUnit) * parcel->allocUnit;
|
||||
}
|
||||
}
|
||||
|
||||
bool ParcelWrite(HcParcel *parcel, const void *src, uint32_t dataSize)
|
||||
{
|
||||
errno_t rc;
|
||||
if (parcel == NULL || src == NULL || dataSize == 0) {
|
||||
return false;
|
||||
}
|
||||
if (parcel->endPos > PARCEL_UINT_MAX - dataSize) {
|
||||
return false;
|
||||
}
|
||||
if (parcel->endPos + dataSize > parcel->length) {
|
||||
ParcelRecycle(parcel);
|
||||
if (parcel->endPos + dataSize > parcel->length) {
|
||||
uint32_t newSize = GetParcelIncreaseSize(parcel, parcel->endPos + dataSize);
|
||||
if (!ParcelIncrease(parcel, newSize)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
rc = memmove_s(parcel->data + parcel->endPos, dataSize, src, dataSize);
|
||||
if (rc != EOK) {
|
||||
return false;
|
||||
}
|
||||
parcel->endPos += dataSize;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ParcelWriteInt32(HcParcel *parcel, int src)
|
||||
{
|
||||
return ParcelWrite(parcel, &src, sizeof(src));
|
||||
}
|
||||
|
||||
bool ParcelWriteUint32(HcParcel *parcel, uint32_t src)
|
||||
{
|
||||
return ParcelWrite(parcel, &src, sizeof(src));
|
||||
}
|
||||
|
||||
bool ParcelWriteInt16(HcParcel *parcel, short src)
|
||||
{
|
||||
return ParcelWrite(parcel, &src, sizeof(src));
|
||||
}
|
||||
|
||||
bool ParcelWriteUint16(HcParcel *parcel, uint16_t src)
|
||||
{
|
||||
return ParcelWrite(parcel, &src, sizeof(src));
|
||||
}
|
||||
|
||||
bool ParcelWriteInt8(HcParcel *parcel, char src)
|
||||
{
|
||||
return ParcelWrite(parcel, &src, sizeof(src));
|
||||
}
|
||||
|
||||
bool ParcelWriteUint8(HcParcel *parcel, uint8_t src)
|
||||
{
|
||||
return ParcelWrite(parcel, &src, sizeof(src));
|
||||
}
|
||||
|
||||
bool ParcelWriteUint64(HcParcel *parcel, uint64_t src)
|
||||
{
|
||||
return ParcelWrite(parcel, &src, sizeof(src));
|
||||
}
|
||||
|
||||
bool ParcelWriteInt64(HcParcel *parcel, int64_t src)
|
||||
{
|
||||
return ParcelWrite(parcel, &src, sizeof(src));
|
||||
}
|
||||
|
||||
bool ParcelReadParcel(HcParcel *src, HcParcel *dst, uint32_t size, bool copy)
|
||||
{
|
||||
if (src == NULL || dst == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (GetParcelDataSize(src) < size) {
|
||||
return false;
|
||||
}
|
||||
if (!ParcelWrite(dst, (void *)GetParcelData(src), size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!copy) {
|
||||
src->beginPos += size;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ParcelCopy(HcParcel *src, HcParcel *dst)
|
||||
{
|
||||
if (src == NULL || dst == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (GetParcelDataSize(src) == 0) {
|
||||
return true;
|
||||
}
|
||||
return ParcelReadParcel(src, dst, GetParcelDataSize(src), true);
|
||||
}
|
||||
|
||||
void DataRevert(void *data, uint32_t length)
|
||||
{
|
||||
if (data != NULL) {
|
||||
uint8_t *pc = (uint8_t *)data;
|
||||
uint32_t i = 0;
|
||||
for (; i < length / HALF_LEN; ++i) { /* half of the length */
|
||||
/* swap p[i] and p[length-i-1] */
|
||||
pc[i] ^= pc[length - i - 1];
|
||||
pc[length - i - 1] ^= pc[i];
|
||||
pc[i] ^= pc[length - i - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ParcelReadInt32Revert(HcParcel *parcel, int32_t *dst)
|
||||
{
|
||||
bool ret = ParcelRead(parcel, dst, sizeof(int));
|
||||
if (ret) {
|
||||
DataRevert(dst, sizeof(int));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ParcelReadUint32Revert(HcParcel *parcel, uint32_t *dst)
|
||||
{
|
||||
bool ret = ParcelRead(parcel, dst, sizeof(uint32_t));
|
||||
if (ret) {
|
||||
DataRevert(dst, sizeof(uint32_t));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ParcelReadInt16Revert(HcParcel *parcel, short *dst)
|
||||
{
|
||||
bool ret = ParcelRead(parcel, dst, sizeof(short));
|
||||
if (ret) {
|
||||
DataRevert(dst, sizeof(short));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ParcelReadUint16Revert(HcParcel *parcel, uint16_t *dst)
|
||||
{
|
||||
if (parcel == NULL || dst == NULL) {
|
||||
return false;
|
||||
}
|
||||
bool ret = ParcelRead(parcel, dst, sizeof(uint16_t));
|
||||
if (ret) {
|
||||
DataRevert(dst, sizeof(uint16_t));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ParcelReadInt8Revert(HcParcel *parcel, char *dst)
|
||||
{
|
||||
return ParcelRead(parcel, dst, sizeof(char));
|
||||
}
|
||||
|
||||
bool ParcelReadUint8Revert(HcParcel *parcel, uint8_t *dst)
|
||||
{
|
||||
return ParcelRead(parcel, dst, sizeof(uint8_t));
|
||||
}
|
||||
|
||||
bool ParcelReadUint64Revert(HcParcel *parcel, uint64_t *dst)
|
||||
{
|
||||
bool ret = ParcelRead(parcel, dst, sizeof(uint64_t));
|
||||
if (ret) {
|
||||
DataRevert(dst, sizeof(uint64_t));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ParcelReadInt64Revert(HcParcel *parcel, int64_t *dst)
|
||||
{
|
||||
bool ret = ParcelRead(parcel, dst, sizeof(int64_t));
|
||||
if (ret) {
|
||||
DataRevert(dst, sizeof(int64_t));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ParcelWriteInt32Revert(HcParcel *parcel, int src)
|
||||
{
|
||||
DataRevert(&src, sizeof(src));
|
||||
return ParcelWriteInt32(parcel, src);
|
||||
}
|
||||
|
||||
bool ParcelWriteUint32Revert(HcParcel *parcel, uint32_t src)
|
||||
{
|
||||
DataRevert(&src, sizeof(src));
|
||||
return ParcelWriteUint32(parcel, src);
|
||||
}
|
||||
|
||||
bool ParcelWriteInt16Revert(HcParcel *parcel, short src)
|
||||
{
|
||||
DataRevert(&src, sizeof(src));
|
||||
return ParcelWriteInt16(parcel, src);
|
||||
}
|
||||
|
||||
bool ParcelWriteUint16Revert(HcParcel *parcel, uint16_t src)
|
||||
{
|
||||
DataRevert(&src, sizeof(src));
|
||||
return ParcelWriteUint16(parcel, src);
|
||||
}
|
||||
|
||||
bool ParcelWriteInt8Revert(HcParcel *parcel, char src)
|
||||
{
|
||||
return ParcelWriteInt8(parcel, src);
|
||||
}
|
||||
|
||||
bool ParcelWriteUint8Revert(HcParcel *parcel, uint8_t src)
|
||||
{
|
||||
return ParcelWriteUint8(parcel, src);
|
||||
}
|
||||
|
||||
bool ParcelWriteUint64Revert(HcParcel *parcel, uint64_t src)
|
||||
{
|
||||
DataRevert(&src, sizeof(src));
|
||||
return ParcelWriteUint64(parcel, src);
|
||||
}
|
||||
|
||||
bool ParcelWriteInt64Revert(HcParcel *parcel, int64_t src)
|
||||
{
|
||||
DataRevert(&src, sizeof(src));
|
||||
return ParcelWriteInt64(parcel, src);
|
||||
}
|
||||
|
||||
bool ParcelPopBack(HcParcel *parcel, uint32_t size)
|
||||
{
|
||||
if (parcel != NULL && size > 0 && GetParcelDataSize(parcel) >= size) {
|
||||
parcel->endPos -= size;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ParcelPopFront(HcParcel *parcel, uint32_t size)
|
||||
{
|
||||
if ((parcel != NULL) && (size > 0) && (GetParcelDataSize(parcel) >= size)) {
|
||||
parcel->beginPos += size;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
291
common/src/hcf_string.c
Normal file
291
common/src/hcf_string.c
Normal file
@ -0,0 +1,291 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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 <string.h>
|
||||
#include "hcf_string.h"
|
||||
|
||||
const uint32_t STRING_ALLOC_SIZE = 10;
|
||||
const uint32_t STRING_END_CHAR_LENGTH = 1;
|
||||
const char STRING_END_CHAR = '\0';
|
||||
#define MAX_INT 0x7FFFFFFF
|
||||
#define MAX_UINT 0xFFFFFFFF
|
||||
|
||||
/*
|
||||
* Append a HcString
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: append string.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringAppend(HcString *self, HcString str)
|
||||
{
|
||||
uint32_t length = GetParcelDataSize(&str.parcel);
|
||||
if (self != NULL && length > 0) {
|
||||
// remove '\0'
|
||||
ParcelPopBack(&self->parcel, STRING_END_CHAR_LENGTH);
|
||||
// append string(include '\0')
|
||||
return StringAppendPointer(self, GetParcelData(&str.parcel));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Append string pointer
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: string pointer.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringAppendPointer(HcString *self, const char *str)
|
||||
{
|
||||
if (self != NULL && str != NULL) {
|
||||
// remove '\0'
|
||||
ParcelPopBack(&self->parcel, STRING_END_CHAR_LENGTH);
|
||||
// append string (include '\0')
|
||||
return ParcelWrite(&self->parcel, (void *)str, strlen(str) + 1);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Append a char
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: char.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringAppendChar(HcString *self, char c)
|
||||
{
|
||||
if (self != NULL && c != STRING_END_CHAR) {
|
||||
// remove '\0'
|
||||
ParcelPopBack(&self->parcel, STRING_END_CHAR_LENGTH);
|
||||
|
||||
if (ParcelWriteInt8(&self->parcel, c)) {
|
||||
return ParcelWriteInt8(&self->parcel, (uint32_t)STRING_END_CHAR);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Assign a value to the HcString
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: assign value of ta_sting.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringSet(HcString *self, HcString str)
|
||||
{
|
||||
if (self != NULL) {
|
||||
DeleteParcel(&self->parcel);
|
||||
return StringAppend(self, str);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Assign a value to the HcString
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: assign value of string pointer.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringSetPointer(HcString *self, const char *str)
|
||||
{
|
||||
if (self != NULL) {
|
||||
DeleteParcel(&self->parcel);
|
||||
return StringAppendPointer(self, str);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Assign a value to the HcString with fixed length
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: assign value of string pointer.
|
||||
* @param len: the length of string.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringSetPointerWithLength(HcString* self, const char *str, uint32_t len)
|
||||
{
|
||||
if (self == NULL || str == NULL) {
|
||||
return false;
|
||||
}
|
||||
uint32_t strLen = strlen(str);
|
||||
if (strLen < len) {
|
||||
return false;
|
||||
}
|
||||
DeleteParcel(&self->parcel);
|
||||
if (len > 0) {
|
||||
if (false == ParcelWrite(&self->parcel, str, len)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return ParcelWriteInt8(&self->parcel, (uint32_t)STRING_END_CHAR);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the string pointer data
|
||||
* @param self: self pointer.
|
||||
* @return the pointer data of the string
|
||||
*/
|
||||
const char *StringGet(const HcString *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return GetParcelData(&self->parcel);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the length of the string
|
||||
* @param self: self pointer.
|
||||
* @return the length of the string
|
||||
*/
|
||||
uint32_t StringLength(const HcString *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
return 0;
|
||||
} else {
|
||||
uint32_t length = GetParcelDataSize(&self->parcel);
|
||||
if (length > 0) {
|
||||
return length - STRING_END_CHAR_LENGTH;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Find a char from string
|
||||
* @param self: self pointer.
|
||||
* @param c: the char you want find
|
||||
* @param begin: the position find from
|
||||
* @return the position of the char
|
||||
*/
|
||||
int StringFind(const HcString *self, char c, uint32_t begin)
|
||||
{
|
||||
if (self == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// because the return value is int
|
||||
// so the string length cannot bigger than MAX_INT
|
||||
uint32_t strLen = StringLength(self);
|
||||
if (strLen >= MAX_INT) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char* curChar = StringGet(self);
|
||||
while (begin < strLen) {
|
||||
if (*(curChar + begin) == c) {
|
||||
return begin;
|
||||
}
|
||||
++begin;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get sub string from a string.
|
||||
* @param self: self pointer.
|
||||
* @param begin: the begin position of the sub string.
|
||||
* @param len: the length of the sub string.
|
||||
* @param dst: the string pointer which saved the sub string content.
|
||||
* @return the operation result.
|
||||
*/
|
||||
bool StringSubString(const HcString *self, uint32_t begin, uint32_t len, HcString* dst)
|
||||
{
|
||||
if (self == NULL || dst == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (MAX_UINT - len <= begin) {
|
||||
return false;
|
||||
}
|
||||
const char* beingPointer = StringGet(self) + begin;
|
||||
return StringSetPointerWithLength(dst, beingPointer, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compare the string with another string.
|
||||
* @param self: self pointer.
|
||||
* @param dst: the pointer of another string.
|
||||
* @return the compare result.
|
||||
* -1: self is smaller than dst
|
||||
* 0: self is equal with dst
|
||||
* 1: self is bigger than dst
|
||||
*/
|
||||
int StringCompare(const HcString *self, const char* dst)
|
||||
{
|
||||
if (self == NULL || dst == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* src = StringGet(self);
|
||||
if (src == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
do {
|
||||
if ((*src) > (*dst)) {
|
||||
return 1;
|
||||
} else if ((*src) < (*dst)) {
|
||||
return -1;
|
||||
} else {
|
||||
if ((*src) == '\0') {
|
||||
return 0;
|
||||
}
|
||||
++src;
|
||||
++dst;
|
||||
}
|
||||
} while (1);
|
||||
// should never be here
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a string.
|
||||
* Notice: You should delete_string when you don't need the string anymore.
|
||||
* @return return the created string.
|
||||
*/
|
||||
HcString CreateString(void)
|
||||
{
|
||||
HcString str;
|
||||
str.parcel = CreateParcel(0, STRING_ALLOC_SIZE);
|
||||
ParcelWriteInt8(&str.parcel, STRING_END_CHAR);
|
||||
return str;
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete a string. In fact it will not destroy the string,
|
||||
* but only free the allocate memory of the string and reset the member's value
|
||||
* of the string.
|
||||
* You can continue to use the string if you want.
|
||||
* Notice: You should delete the string when you don't need it any more to avoid memory leak.
|
||||
* @param str: The string you want to delete.
|
||||
*/
|
||||
void DeleteString(HcString *str)
|
||||
{
|
||||
if (str != NULL) {
|
||||
DeleteParcel(&str->parcel);
|
||||
}
|
||||
}
|
59
common/src/log.c
Normal file
59
common/src/log.c
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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 "log.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include "config.h"
|
||||
#include "securec.h"
|
||||
|
||||
static void HcfOutPrint(const char *buf, HcfLogLevel level)
|
||||
{
|
||||
switch (level) {
|
||||
case HCF_LOG_LEVEL_DEBUG:
|
||||
HCF_LOG_DEBUG(buf);
|
||||
break;
|
||||
case HCF_LOG_LEVEL_INFO:
|
||||
HCF_LOG_INFO(buf);
|
||||
break;
|
||||
case HCF_LOG_LEVEL_WARN:
|
||||
HCF_LOG_WARN(buf);
|
||||
break;
|
||||
case HCF_LOG_LEVEL_ERROR:
|
||||
HCF_LOG_ERROR(buf);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void HcfLogPrint(HcfLogLevel level, const char *funName, const char *fmt, ...)
|
||||
{
|
||||
int32_t ulPos = 0;
|
||||
char outStr[LOG_PRINT_MAX_LEN] = {0};
|
||||
int32_t ret = sprintf_s(outStr, sizeof(outStr), "%s: ", funName);
|
||||
if (ret < 0) {
|
||||
return;
|
||||
}
|
||||
ulPos = strlen(outStr);
|
||||
va_list arg;
|
||||
va_start(arg, fmt);
|
||||
ret = vsprintf_s(&outStr[ulPos], sizeof(outStr) - ulPos, fmt, arg);
|
||||
va_end(arg);
|
||||
if (ret < 0) {
|
||||
return;
|
||||
}
|
||||
HcfOutPrint(outStr, level);
|
||||
}
|
39
common/src/memory.c
Normal file
39
common/src/memory.c
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "memory.h"
|
||||
|
||||
#include "log.h"
|
||||
#include "securec.h"
|
||||
|
||||
void *HcfMalloc(uint32_t size, char val)
|
||||
{
|
||||
if (size == 0) {
|
||||
LOGE("malloc size is invalid");
|
||||
return NULL;
|
||||
}
|
||||
void *addr = malloc(size);
|
||||
if (addr != NULL) {
|
||||
(void)memset_s(addr, size, val, size);
|
||||
}
|
||||
return addr;
|
||||
}
|
||||
|
||||
void HcfFree(void *addr)
|
||||
{
|
||||
if (addr != NULL) {
|
||||
free(addr);
|
||||
}
|
||||
}
|
140
common/src/params_parser.c
Normal file
140
common/src/params_parser.c
Normal file
@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "params_parser.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "hcf_string.h"
|
||||
#include "log.h"
|
||||
|
||||
const HcfParaConfig gConfig[] = {
|
||||
{"ECC224", HCF_ALG_KEY_TYPE, HCF_ALG_ECC_224},
|
||||
{"ECC256", HCF_ALG_KEY_TYPE, HCF_ALG_ECC_256},
|
||||
{"ECC384", HCF_ALG_KEY_TYPE, HCF_ALG_ECC_384},
|
||||
{"ECC512", HCF_ALG_KEY_TYPE, HCF_ALG_ECC_512},
|
||||
|
||||
{"AES128", HCF_ALG_KEY_TYPE, HCF_ALG_AES_128},
|
||||
{"AES192", HCF_ALG_KEY_TYPE, HCF_ALG_AES_192},
|
||||
{"AES256", HCF_ALG_KEY_TYPE, HCF_ALG_AES_256},
|
||||
{"3DES192", HCF_ALG_KEY_TYPE, HCF_ALG_3DES_192},
|
||||
|
||||
{"ECB", HCF_ALG_MODE, HCF_ALG_MODE_ECB},
|
||||
{"CBC", HCF_ALG_MODE, HCF_ALG_MODE_CBC},
|
||||
{"CTR", HCF_ALG_MODE, HCF_ALG_MODE_CTR},
|
||||
{"OFB", HCF_ALG_MODE, HCF_ALG_MODE_OFB},
|
||||
{"CFB", HCF_ALG_MODE, HCF_ALG_MODE_CFB},
|
||||
{"CFB1", HCF_ALG_MODE, HCF_ALG_MODE_CFB1},
|
||||
{"CFB8", HCF_ALG_MODE, HCF_ALG_MODE_CFB8},
|
||||
{"CFB64", HCF_ALG_MODE, HCF_ALG_MODE_CFB64},
|
||||
{"CFB128", HCF_ALG_MODE, HCF_ALG_MODE_CFB128},
|
||||
{"CCM", HCF_ALG_MODE, HCF_ALG_MODE_CCM},
|
||||
{"GCM", HCF_ALG_MODE, HCF_ALG_MODE_GCM},
|
||||
|
||||
{"NoPadding", HCF_ALG_PADDING_TYPE, HCF_ALG_NOPADDING},
|
||||
{"PKCS5", HCF_ALG_PADDING_TYPE, HCF_ALG_PADDING_PKCS5},
|
||||
{"PKCS7", HCF_ALG_PADDING_TYPE, HCF_ALG_PADDING_PKCS7},
|
||||
|
||||
{"RSA512", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_512},
|
||||
{"RSA768", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_768},
|
||||
{"RSA1024", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_1024},
|
||||
{"RSA2048", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_2048},
|
||||
{"RSA3072", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_3072},
|
||||
{"RSA4096", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_4096},
|
||||
{"RSA8192", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_8192},
|
||||
|
||||
{"PKCS1", HCF_ALG_PADDING_TYPE, HCF_OPENSSL_RSA_PKCS1_PADDING},
|
||||
{"PKCS1_OAEP", HCF_ALG_PADDING_TYPE, HCF_OPENSSL_RSA_PKCS1_OAEP_PADDING},
|
||||
{"PSS", HCF_ALG_PADDING_TYPE, HCF_OPENSSL_RSA_PSS_PADDING},
|
||||
|
||||
{"MD5", HCF_ALG_DIGEST, HCF_OPENSSL_DIGEST_MD5},
|
||||
{"SHA1", HCF_ALG_DIGEST, HCF_OPENSSL_DIGEST_SHA1},
|
||||
{"SHA224", HCF_ALG_DIGEST, HCF_OPENSSL_DIGEST_SHA224},
|
||||
{"SHA256", HCF_ALG_DIGEST, HCF_OPENSSL_DIGEST_SHA256},
|
||||
{"SHA384", HCF_ALG_DIGEST, HCF_OPENSSL_DIGEST_SHA384},
|
||||
{"SHA512", HCF_ALG_DIGEST, HCF_OPENSSL_DIGEST_SHA512},
|
||||
|
||||
{"MGF1_MD5", HCF_ALG_MGF1_DIGEST, HCF_OPENSSL_DIGEST_MD5},
|
||||
{"MGF1_SHA1", HCF_ALG_MGF1_DIGEST, HCF_OPENSSL_DIGEST_SHA1},
|
||||
{"MGF1_SHA224", HCF_ALG_MGF1_DIGEST, HCF_OPENSSL_DIGEST_SHA224},
|
||||
{"MGF1_SHA256", HCF_ALG_MGF1_DIGEST, HCF_OPENSSL_DIGEST_SHA256},
|
||||
{"MGF1_SHA384", HCF_ALG_MGF1_DIGEST, HCF_OPENSSL_DIGEST_SHA384},
|
||||
{"MGF1_SHA512", HCF_ALG_MGF1_DIGEST, HCF_OPENSSL_DIGEST_SHA512},
|
||||
|
||||
{"PRIMES_2", HCF_ALG_PRIMES, HCF_OPENSSL_PRIMES_2},
|
||||
{"PRIMES_3", HCF_ALG_PRIMES, HCF_OPENSSL_PRIMES_3},
|
||||
{"PRIMES_4", HCF_ALG_PRIMES, HCF_OPENSSL_PRIMES_4},
|
||||
{"PRIMES_5", HCF_ALG_PRIMES, HCF_OPENSSL_PRIMES_5},
|
||||
|
||||
};
|
||||
|
||||
const HcfParaConfig* findConfig(const HcString* tag)
|
||||
{
|
||||
if (tag == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < sizeof(gConfig) / sizeof(HcfParaConfig); ++i) {
|
||||
if (StringCompare(tag, gConfig[i].tag) == 0) {
|
||||
return &gConfig[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
HcfResult ParseAndSetParameter(const char* paramsStr, void *params, SetParameterFunc setFunc)
|
||||
{
|
||||
if (paramsStr == NULL || setFunc == NULL) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcString str = CreateString();
|
||||
HcString subStr = CreateString();
|
||||
if (!StringSetPointer(&str, paramsStr)) {
|
||||
DeleteString(&subStr);
|
||||
DeleteString(&str);
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcfResult ret = HCF_INVALID_PARAMS;
|
||||
uint32_t pos = 0;
|
||||
do {
|
||||
int findPos = StringFind(&str, '|', pos);
|
||||
if (findPos >= 0) {
|
||||
if (!StringSubString(&str, pos, findPos - pos, &subStr)) {
|
||||
LOGE("StringSubString failed!");
|
||||
break;
|
||||
}
|
||||
ret = (*setFunc)(findConfig(&subStr), params);
|
||||
if (ret != HCF_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
pos = findPos + 1;
|
||||
} else {
|
||||
uint32_t strLen = StringLength(&str);
|
||||
if (strLen < pos) {
|
||||
break;
|
||||
}
|
||||
if (!StringSubString(&str, pos, strLen - pos, &subStr)) {
|
||||
LOGE("get last string failed!");
|
||||
break;
|
||||
}
|
||||
ret = (*setFunc)(findConfig(&subStr), params);
|
||||
break;
|
||||
}
|
||||
} while (true);
|
||||
|
||||
DeleteString(&subStr);
|
||||
DeleteString(&str);
|
||||
return ret;
|
||||
}
|
51
common/src/utils.c
Normal file
51
common/src/utils.c
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "utils.h"
|
||||
|
||||
#include <string.h>
|
||||
#include "log.h"
|
||||
|
||||
bool IsStrValid(const char *str, uint32_t maxLen)
|
||||
{
|
||||
if (str == NULL) {
|
||||
LOGE("input string is NULL ptr");
|
||||
return false;
|
||||
}
|
||||
// One byte must be reserved for the terminator.
|
||||
if (strnlen(str, maxLen) >= maxLen) {
|
||||
LOGE("input string is beyond max length");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IsBlobValid(const HcfBlob *blob)
|
||||
{
|
||||
return ((blob != NULL) && (blob->data != NULL) && (blob->len > 0));
|
||||
}
|
||||
|
||||
bool IsClassMatch(const HcfObjectBase *obj, const char *className)
|
||||
{
|
||||
if ((obj == NULL) || (obj->getClass() == NULL) || (className == NULL)) {
|
||||
return false;
|
||||
}
|
||||
if (strcmp(obj->getClass(), className) == 0) {
|
||||
return true;
|
||||
} else {
|
||||
LOGE("class is not match. except class: %s, input class: %s", className, obj->getClass());
|
||||
return false;
|
||||
}
|
||||
}
|
101
frameworks/BUILD.gn
Normal file
101
frameworks/BUILD.gn
Normal file
@ -0,0 +1,101 @@
|
||||
# Copyright (C) 2022 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("//build/ohos.gni")
|
||||
import("//base/security/crypto_framework/frameworks/frameworks.gni")
|
||||
import("//base/security/crypto_framework/common/common.gni")
|
||||
|
||||
config("framework_config") {
|
||||
include_dirs = [
|
||||
"//base/security/crypto_framework/interfaces/innerkits/algorithm_parameter",
|
||||
"//base/security/crypto_framework/interfaces/innerkits/certificate",
|
||||
"//base/security/crypto_framework/interfaces/innerkits/common",
|
||||
"//base/security/crypto_framework/interfaces/innerkits/crypto_operation",
|
||||
"//base/security/crypto_framework/interfaces/innerkits/key",
|
||||
"//base/security/crypto_framework/interfaces/innerkits/rand",
|
||||
]
|
||||
}
|
||||
|
||||
ohos_shared_library("crypto_framework_lib") {
|
||||
subsystem_name = "security"
|
||||
part_name = "crypto_framework"
|
||||
public_configs = [ ":framework_config" ]
|
||||
include_dirs = [
|
||||
"//utils/native/base/include",
|
||||
]
|
||||
include_dirs += framework_inc_path + crypto_framwork_common_inc_path
|
||||
|
||||
sources = framework_files
|
||||
|
||||
cflags = [
|
||||
"-fPIC",
|
||||
"-Wall",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//utils/native/base:utils",
|
||||
"//third_party/openssl:libcrypto_shared",
|
||||
"//base/security/crypto_framework:crypto_openssl_plugin_lib",
|
||||
"//base/security/crypto_framework/common:crypto_plugin_common",
|
||||
]
|
||||
}
|
||||
|
||||
ohos_shared_library("cryptoframework_napi") {
|
||||
subsystem_name = "security"
|
||||
part_name = "crypto_framework"
|
||||
relative_install_dir = "module/security"
|
||||
include_dirs = [
|
||||
"//base/security/crypto_framework/interfaces/kits/js",
|
||||
"//third_party/bounds_checking_function/include",
|
||||
"//utils/native/base/include",
|
||||
"//base/security/crypto_framework/frameworks/js/napi/inc",
|
||||
]
|
||||
include_dirs += framework_inc_path
|
||||
|
||||
cflags = [
|
||||
"-fPIC",
|
||||
"-g3",
|
||||
]
|
||||
|
||||
sources = [
|
||||
"js/napi/src/napi_init.cpp",
|
||||
"js/napi/src/napi_utils.cpp",
|
||||
"js/napi/src/napi_key_pair.cpp",
|
||||
"js/napi/src/napi_pri_key.cpp",
|
||||
"js/napi/src/napi_pub_key.cpp",
|
||||
"js/napi/src/napi_asy_key_generator.cpp",
|
||||
"js/napi/src/napi_sign.cpp",
|
||||
"js/napi/src/napi_verify.cpp",
|
||||
"js/napi/src/napi_key_agreement.cpp",
|
||||
"js/napi/src/napi_x509_certificate.cpp",
|
||||
"js/napi/src/napi_mac.cpp",
|
||||
"js/napi/src/napi_md.cpp",
|
||||
"js/napi/src/napi_rand.cpp",
|
||||
"js/napi/src/napi_x509_crl.cpp",
|
||||
"js/napi/src/napi_x509_crl_entry.cpp",
|
||||
"js/napi/src/napi_sym_key_generator.cpp",
|
||||
"js/napi/src/napi_cipher.cpp",
|
||||
"js/napi/src/napi_key.cpp",
|
||||
"js/napi/src/napi_sym_key.cpp",
|
||||
"js/napi/src/napi_cert_chain_validator.cpp",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//base/security/crypto_framework/frameworks:crypto_framework_lib",
|
||||
"//third_party/bounds_checking_function:libsec_static",
|
||||
]
|
||||
|
||||
external_deps = [
|
||||
"napi:ace_napi",
|
||||
]
|
||||
}
|
217
frameworks/certificate/cert_chain_validator.c
Normal file
217
frameworks/certificate/cert_chain_validator.c
Normal file
@ -0,0 +1,217 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "cert_chain_validator.h"
|
||||
|
||||
#include <securec.h>
|
||||
|
||||
#include "blob.h"
|
||||
#include "cert_chain_validator_spi.h"
|
||||
#include "config.h"
|
||||
#include "result.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
#include "result.h"
|
||||
#include "utils.h"
|
||||
#include "x509_cert_chain_validator_openssl.h"
|
||||
|
||||
#define LV_LENGTH_LEN sizeof(uint16_t)
|
||||
#define MAX_CERT_PATH_DATA_LEM 8192
|
||||
|
||||
typedef HcfResult (*CertChainValidatorSpiCreateFunc)(HcfCertChainValidatorSpi **);
|
||||
|
||||
typedef struct {
|
||||
HcfCertChainValidator base;
|
||||
HcfCertChainValidatorSpi *spiObj;
|
||||
char *algorithm;
|
||||
} CertChainValidatorImpl;
|
||||
|
||||
typedef struct {
|
||||
CertChainValidatorSpiCreateFunc createFunc;
|
||||
} HcfCertChainValidatorFuncSet;
|
||||
|
||||
typedef struct {
|
||||
const char *algorithm;
|
||||
HcfCertChainValidatorFuncSet funcSet;
|
||||
} HcfCertChainValidatorAbility;
|
||||
|
||||
static const HcfCertChainValidatorAbility CERT_PATH_VALIDATOR_ABILITY_SET[] = {
|
||||
{ "PKIX", { HcfCertChainValidatorSpiCreate } }
|
||||
};
|
||||
|
||||
static const HcfCertChainValidatorFuncSet *FindAbility(const char *algorithm)
|
||||
{
|
||||
for (uint32_t i = 0; i < sizeof(CERT_PATH_VALIDATOR_ABILITY_SET); i++) {
|
||||
if (strcmp(CERT_PATH_VALIDATOR_ABILITY_SET[i].algorithm, algorithm) == 0) {
|
||||
return &(CERT_PATH_VALIDATOR_ABILITY_SET[i].funcSet);
|
||||
}
|
||||
}
|
||||
LOGE("Algorithm for certChain validator is not support! [algorithm]: %s", algorithm);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *GetCertChainValidatorClass()
|
||||
{
|
||||
return "HcfCertChainValidator";
|
||||
}
|
||||
|
||||
static void DestroyCertChainValidator(HcfObjectBase *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
}
|
||||
if (!IsClassMatch(self, GetCertChainValidatorClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return;
|
||||
}
|
||||
CertChainValidatorImpl *validatorImpl = (CertChainValidatorImpl *)self;
|
||||
OH_HCF_ObjDestroy(validatorImpl->spiObj);
|
||||
HcfFree(validatorImpl->algorithm);
|
||||
validatorImpl->algorithm = NULL;
|
||||
HcfFree(validatorImpl);
|
||||
}
|
||||
|
||||
static HcfResult ConvertCertBuffer2List(const HcfCertChainData *certChainData, HcfArray *certsList)
|
||||
{
|
||||
uint8_t *msg = certChainData->data;
|
||||
const uint8_t *boundary = certChainData->data + certChainData->dataLen;
|
||||
uint32_t index = 0;
|
||||
HcfResult res = HCF_SUCCESS;
|
||||
while (msg < boundary) {
|
||||
if (index >= certsList->count || (msg + LV_LENGTH_LEN > boundary)) {
|
||||
LOGE("Invalid index for l-v len!");
|
||||
res = HCF_INVALID_PARAMS;
|
||||
break;
|
||||
}
|
||||
uint16_t entryLen = 0;
|
||||
if (memcpy_s(&entryLen, LV_LENGTH_LEN, msg, LV_LENGTH_LEN) != EOK) {
|
||||
LOGE("Input data in too long.");
|
||||
return HCF_ERR_COPY;
|
||||
}
|
||||
msg = msg + LV_LENGTH_LEN;
|
||||
certsList->data[index].data = (uint8_t *)HcfMalloc(entryLen, 0);
|
||||
if (certsList->data[index].data == NULL) {
|
||||
LOGE("Failed to malloc data for cert, index = %u.", index);
|
||||
res = HCF_ERR_MALLOC;
|
||||
break;
|
||||
}
|
||||
if (msg + entryLen > boundary) {
|
||||
LOGE("Entry len is overflow for boundary!");
|
||||
res = HCF_INVALID_PARAMS;
|
||||
break;
|
||||
}
|
||||
if (memcpy_s(certsList->data[index].data, entryLen, msg, entryLen) != EOK) {
|
||||
res = HCF_ERR_COPY;
|
||||
break;
|
||||
}
|
||||
certsList->data[index].len = entryLen;
|
||||
msg = msg + entryLen;
|
||||
index++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static HcfResult Validate(HcfCertChainValidator *self, const HcfCertChainData *certChainData)
|
||||
{
|
||||
if ((self == NULL) || (certChainData == NULL) || (certChainData->dataLen > MAX_CERT_PATH_DATA_LEM)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetCertChainValidatorClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
CertChainValidatorImpl *impl = (CertChainValidatorImpl *)self;
|
||||
HcfArray certsList = { NULL, 0 };
|
||||
certsList.format = certChainData->format;
|
||||
certsList.count = certChainData->count;
|
||||
uint32_t certsLen = sizeof(HcfBlob) * certsList.count;
|
||||
certsList.data = (HcfBlob *)HcfMalloc(certsLen, 0);
|
||||
if (certsList.data == NULL) {
|
||||
LOGE("Failed to new memory for certs.");
|
||||
return HCF_ERR_MALLOC;
|
||||
}
|
||||
HcfResult res = ConvertCertBuffer2List(certChainData, &certsList);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("Failed to convert buffer to lists.");
|
||||
HcfArrayDataClearAndFree(&certsList);
|
||||
return res;
|
||||
}
|
||||
res = impl->spiObj->engineValidate(impl->spiObj, &certsList);
|
||||
HcfArrayDataClearAndFree(&certsList);
|
||||
return res;
|
||||
}
|
||||
|
||||
static const char *GetAlgorithm(HcfCertChainValidator *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return NULL;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetCertChainValidatorClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return NULL;
|
||||
}
|
||||
CertChainValidatorImpl *impl = (CertChainValidatorImpl *)self;
|
||||
const char *algo = (const char *)impl->algorithm;
|
||||
return algo;
|
||||
}
|
||||
|
||||
HcfResult HcfCertChainValidatorCreate(const char *algorithm, HcfCertChainValidator **pathValidator)
|
||||
{
|
||||
if (!IsStrValid(algorithm, HCF_MAX_STR_LEN) || (pathValidator == NULL)) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
const HcfCertChainValidatorFuncSet *func = FindAbility(algorithm);
|
||||
if (func == NULL) {
|
||||
LOGE("Func is null!");
|
||||
return HCF_NOT_SUPPORT;
|
||||
}
|
||||
|
||||
HcfCertChainValidatorSpi *spiObj = NULL;
|
||||
HcfResult res = func->createFunc(&spiObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("Failed to create certChain validator spi object!");
|
||||
return res;
|
||||
}
|
||||
CertChainValidatorImpl *returnValidator = (CertChainValidatorImpl *)HcfMalloc(sizeof(CertChainValidatorImpl), 0);
|
||||
if (returnValidator == NULL) {
|
||||
LOGE("Failed to allocate returnValidator memory!");
|
||||
return HCF_ERR_MALLOC;
|
||||
}
|
||||
returnValidator->base.validate = Validate;
|
||||
returnValidator->base.getAlgorithm = GetAlgorithm;
|
||||
returnValidator->base.base.destroy = DestroyCertChainValidator;
|
||||
returnValidator->base.base.getClass = GetCertChainValidatorClass;
|
||||
returnValidator->spiObj = spiObj;
|
||||
uint32_t algoNameLen = strlen(algorithm) + 1;
|
||||
returnValidator->algorithm = (char *)HcfMalloc(algoNameLen, 0);
|
||||
if (returnValidator->algorithm == NULL) {
|
||||
LOGE("Failed to allocate algorithm memory!");
|
||||
HcfFree(returnValidator);
|
||||
return HCF_ERR_MALLOC;
|
||||
}
|
||||
if (memcpy_s(returnValidator->algorithm, algoNameLen, algorithm, algoNameLen) != HCF_SUCCESS) {
|
||||
LOGE("Failed to copy for validator algorithm!");
|
||||
HcfFree(returnValidator->algorithm);
|
||||
returnValidator->algorithm = NULL;
|
||||
HcfFree(returnValidator);
|
||||
returnValidator = NULL;
|
||||
return HCF_ERR_COPY;
|
||||
}
|
||||
|
||||
*pathValidator = (HcfCertChainValidator *)returnValidator;
|
||||
return HCF_SUCCESS;
|
||||
}
|
389
frameworks/certificate/x509_certificate.c
Normal file
389
frameworks/certificate/x509_certificate.c
Normal file
@ -0,0 +1,389 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "x509_certificate.h"
|
||||
|
||||
#include <securec.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "fwk_class.h"
|
||||
#include "x509_certificate_openssl.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
#include "utils.h"
|
||||
|
||||
typedef HcfResult (*HcfX509CertificateSpiCreateFunc)(const HcfEncodingBlob *, HcfX509CertificateSpi **);
|
||||
|
||||
typedef struct {
|
||||
HcfX509CertificateSpiCreateFunc createFunc;
|
||||
} HcfX509CertificateFuncSet;
|
||||
|
||||
typedef struct {
|
||||
char *certType;
|
||||
HcfX509CertificateFuncSet funcSet;
|
||||
} HcfCCertFactoryAbility;
|
||||
|
||||
static const char *GetX509CertificateClass(void)
|
||||
{
|
||||
return HCF_X509_CERTIFICATE_CLASS;
|
||||
}
|
||||
|
||||
static const HcfCCertFactoryAbility X509_CERTIFICATE_ABILITY_SET[] = {
|
||||
{ "X509", { OpensslX509CertSpiCreate, } }
|
||||
};
|
||||
|
||||
static const HcfX509CertificateFuncSet *FindAbility(const char *certType)
|
||||
{
|
||||
if (certType == NULL) {
|
||||
LOGE("CertType is null!");
|
||||
return NULL;
|
||||
}
|
||||
for (uint32_t i = 0; i < sizeof(X509_CERTIFICATE_ABILITY_SET); i++) {
|
||||
if (strcmp(X509_CERTIFICATE_ABILITY_SET[i].certType, certType) == 0) {
|
||||
return &(X509_CERTIFICATE_ABILITY_SET[i].funcSet);
|
||||
}
|
||||
}
|
||||
LOGE("Cert not support! [cert]: %s", certType);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void DestroyX509Certificate(HcfObjectBase *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return;
|
||||
}
|
||||
if (!IsClassMatch(self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return;
|
||||
}
|
||||
HcfX509CertificateImpl *impl = (HcfX509CertificateImpl *)self;
|
||||
OH_HCF_ObjDestroy(impl->spiObj);
|
||||
HcfFree(impl);
|
||||
}
|
||||
|
||||
static HcfResult Verify(HcfCertificate *self, HcfPubKey *key)
|
||||
{
|
||||
if ((self == NULL) || (key == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineVerify(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, key);
|
||||
}
|
||||
|
||||
static HcfResult GetEncoded(HcfCertificate *self, HcfEncodingBlob *encodedByte)
|
||||
{
|
||||
if ((self == NULL) || (encodedByte == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetEncoded(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, encodedByte);
|
||||
}
|
||||
|
||||
static HcfResult GetPublicKey(HcfCertificate *self, HcfPubKey **keyOut)
|
||||
{
|
||||
if ((self == NULL) || (keyOut == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetPublicKey(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, keyOut);
|
||||
}
|
||||
|
||||
static HcfResult CheckValidityWithDate(HcfX509Certificate *self, const char *date)
|
||||
{
|
||||
if ((self == NULL) || (date == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineCheckValidityWithDate(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, date);
|
||||
}
|
||||
|
||||
long GetVersion(HcfX509Certificate *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return -1;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return -1;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetVersion(
|
||||
((HcfX509CertificateImpl *)self)->spiObj);
|
||||
}
|
||||
|
||||
long GetSerialNumber(HcfX509Certificate *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return -1;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return -1;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetSerialNumber(
|
||||
((HcfX509CertificateImpl *)self)->spiObj);
|
||||
}
|
||||
|
||||
static HcfResult GetIssuerName(HcfX509Certificate *self, HcfBlob *out)
|
||||
{
|
||||
if ((self == NULL) || (out == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetIssuerName(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, out);
|
||||
}
|
||||
|
||||
static HcfResult GetSubjectName(HcfX509Certificate *self, HcfBlob *out)
|
||||
{
|
||||
if ((self == NULL) || (out == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetSubjectName(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, out);
|
||||
}
|
||||
|
||||
static HcfResult GetNotBeforeTime(HcfX509Certificate *self, HcfBlob *outDate)
|
||||
{
|
||||
if ((self == NULL) || (outDate == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetNotBeforeTime(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, outDate);
|
||||
}
|
||||
|
||||
static HcfResult GetNotAfterTime(HcfX509Certificate *self, HcfBlob *outDate)
|
||||
{
|
||||
if ((self == NULL) || (outDate == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetNotAfterTime(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, outDate);
|
||||
}
|
||||
|
||||
static HcfResult GetSignature(HcfX509Certificate *self, HcfBlob *sigOut)
|
||||
{
|
||||
if ((self == NULL) || (sigOut == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetSignature(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, sigOut);
|
||||
}
|
||||
|
||||
static HcfResult GetSignatureAlgName(HcfX509Certificate *self, HcfBlob *outName)
|
||||
{
|
||||
if ((self == NULL) || (outName == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetSignatureAlgName(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, outName);
|
||||
}
|
||||
|
||||
static HcfResult GetSignatureAlgOid(HcfX509Certificate *self, HcfBlob *out)
|
||||
{
|
||||
if ((self == NULL) || (out == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetSignatureAlgOid(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, out);
|
||||
}
|
||||
|
||||
static HcfResult GetSignatureAlgParams(HcfX509Certificate *self, HcfBlob *sigAlgParamsOut)
|
||||
{
|
||||
if ((self == NULL) || (sigAlgParamsOut == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetSignatureAlgParams(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, sigAlgParamsOut);
|
||||
}
|
||||
|
||||
static HcfResult GetKeyUsage(HcfX509Certificate *self, HcfBlob *boolArr)
|
||||
{
|
||||
if ((self == NULL) || (boolArr == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetKeyUsage(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, boolArr);
|
||||
}
|
||||
|
||||
static HcfResult GetExtKeyUsage(HcfX509Certificate *self, HcfArray *keyUsageOut)
|
||||
{
|
||||
if ((self == NULL) || (keyUsageOut == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetExtKeyUsage(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, keyUsageOut);
|
||||
}
|
||||
|
||||
static int32_t GetBasicConstraints(HcfX509Certificate *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetBasicConstraints(
|
||||
((HcfX509CertificateImpl *)self)->spiObj);
|
||||
}
|
||||
|
||||
static HcfResult GetSubjectAltNames(HcfX509Certificate *self, HcfArray *outName)
|
||||
{
|
||||
if ((self == NULL) || (outName == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetSubjectAltNames(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, outName);
|
||||
}
|
||||
|
||||
static HcfResult GetIssuerAltNames(HcfX509Certificate *self, HcfArray *outName)
|
||||
{
|
||||
if ((self == NULL) || (outName == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CertificateClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CertificateImpl *)self)->spiObj->engineGetIssuerAltNames(
|
||||
((HcfX509CertificateImpl *)self)->spiObj, outName);
|
||||
}
|
||||
|
||||
HcfResult HcfX509CertificateCreate(const HcfEncodingBlob *inStream, HcfX509Certificate **returnObj)
|
||||
{
|
||||
if ((inStream == NULL) || (inStream->len > HCF_MAX_BUFFER_LEN) || (returnObj == NULL)) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
const HcfX509CertificateFuncSet *funcSet = FindAbility("X509");
|
||||
if (funcSet == NULL) {
|
||||
LOGE("FuncSet is null!");
|
||||
return HCF_NOT_SUPPORT;
|
||||
}
|
||||
HcfX509CertificateSpi *spiObj = NULL;
|
||||
HcfResult res = funcSet->createFunc(inStream, &spiObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("Failed to create spi object!");
|
||||
return res;
|
||||
}
|
||||
HcfX509CertificateImpl *x509CertImpl = (HcfX509CertificateImpl *)HcfMalloc(sizeof(HcfX509CertificateImpl), 0);
|
||||
if (x509CertImpl == NULL) {
|
||||
LOGE("Failed to allocate x509CertImpl memory!");
|
||||
return HCF_ERR_MALLOC;
|
||||
}
|
||||
x509CertImpl->base.base.base.getClass = GetX509CertificateClass;
|
||||
x509CertImpl->base.base.base.destroy = DestroyX509Certificate;
|
||||
x509CertImpl->base.base.verify = Verify;
|
||||
x509CertImpl->base.base.getEncoded = GetEncoded;
|
||||
x509CertImpl->base.base.getPublicKey = GetPublicKey;
|
||||
x509CertImpl->base.checkValidityWithDate = CheckValidityWithDate;
|
||||
x509CertImpl->base.getVersion = GetVersion;
|
||||
x509CertImpl->base.getSerialNumber = GetSerialNumber;
|
||||
x509CertImpl->base.getIssuerName = GetIssuerName;
|
||||
x509CertImpl->base.getSubjectName = GetSubjectName;
|
||||
x509CertImpl->base.getNotBeforeTime = GetNotBeforeTime;
|
||||
x509CertImpl->base.getNotAfterTime = GetNotAfterTime;
|
||||
x509CertImpl->base.getSignature = GetSignature;
|
||||
x509CertImpl->base.getSignatureAlgName = GetSignatureAlgName;
|
||||
x509CertImpl->base.getSignatureAlgOid = GetSignatureAlgOid;
|
||||
x509CertImpl->base.getSignatureAlgParams = GetSignatureAlgParams;
|
||||
x509CertImpl->base.getKeyUsage = GetKeyUsage;
|
||||
x509CertImpl->base.getExtKeyUsage = GetExtKeyUsage;
|
||||
x509CertImpl->base.getBasicConstraints = GetBasicConstraints;
|
||||
x509CertImpl->base.getSubjectAltNames = GetSubjectAltNames;
|
||||
x509CertImpl->base.getIssuerAltNames = GetIssuerAltNames;
|
||||
|
||||
x509CertImpl->spiObj = spiObj;
|
||||
*returnObj = (HcfX509Certificate *)x509CertImpl;
|
||||
return HCF_SUCCESS;
|
||||
}
|
354
frameworks/certificate/x509_crl.c
Normal file
354
frameworks/certificate/x509_crl.c
Normal file
@ -0,0 +1,354 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "x509_crl.h"
|
||||
|
||||
#include "securec.h"
|
||||
|
||||
#include "config.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
#include "utils.h"
|
||||
#include "x509_crl.h"
|
||||
#include "x509_crl_openssl.h"
|
||||
#include "x509_crl_spi.h"
|
||||
|
||||
#define HCF_X509_CRL_CLASS "HcfX509Crl"
|
||||
#define OPENSSL_INVALID_VERSION (-1)
|
||||
|
||||
typedef HcfResult (*HcfX509CrlSpiCreateFunc)(const HcfEncodingBlob *, HcfX509CrlSpi **);
|
||||
|
||||
typedef struct {
|
||||
HcfX509Crl base;
|
||||
HcfX509CrlSpi *spiObj;
|
||||
const char *certType;
|
||||
} HcfX509CrlImpl;
|
||||
|
||||
typedef struct {
|
||||
HcfX509CrlSpiCreateFunc createFunc;
|
||||
} HcfX509CrlFuncSet;
|
||||
|
||||
typedef struct {
|
||||
char *certType;
|
||||
HcfX509CrlFuncSet funcSet;
|
||||
} HcfCCertFactoryAbility;
|
||||
|
||||
static const char *GetX509CrlClass(void)
|
||||
{
|
||||
return HCF_X509_CRL_CLASS;
|
||||
}
|
||||
|
||||
static const HcfCCertFactoryAbility X509_CRL_ABILITY_SET[] = {
|
||||
{ "X509", { HcfCX509CrlSpiCreate, } }
|
||||
};
|
||||
|
||||
static const HcfX509CrlFuncSet *FindAbility(const char *certType)
|
||||
{
|
||||
if (certType == NULL) {
|
||||
LOGE("CertType is null!");
|
||||
return NULL;
|
||||
}
|
||||
for (uint32_t i = 0; i < sizeof(X509_CRL_ABILITY_SET); i++) {
|
||||
if (strcmp(X509_CRL_ABILITY_SET[i].certType, certType) == 0) {
|
||||
return &(X509_CRL_ABILITY_SET[i].funcSet);
|
||||
}
|
||||
}
|
||||
LOGE("Cert not support! [cert]: %s", certType);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void DestroyX509Crl(HcfObjectBase *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return;
|
||||
}
|
||||
if (!IsClassMatch(self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return;
|
||||
}
|
||||
HcfX509CrlImpl *impl = (HcfX509CrlImpl *)self;
|
||||
OH_HCF_ObjDestroy(impl->spiObj);
|
||||
HcfFree(impl);
|
||||
}
|
||||
|
||||
static const char *GetType(HcfCrl *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return NULL;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return NULL;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineGetType(
|
||||
((HcfX509CrlImpl *)self)->spiObj);
|
||||
}
|
||||
|
||||
static bool IsRevoked(HcfCrl *self, const HcfCertificate *cert)
|
||||
{
|
||||
if ((self == NULL) || (cert == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return false;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return false;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineIsRevoked(
|
||||
((HcfX509CrlImpl *)self)->spiObj, cert);
|
||||
}
|
||||
|
||||
static HcfResult Verify(HcfX509Crl *self, HcfPubKey *key)
|
||||
{
|
||||
if ((self == NULL) || (key == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineVerify(
|
||||
((HcfX509CrlImpl *)self)->spiObj, key);
|
||||
}
|
||||
|
||||
static HcfResult GetEncoded(HcfX509Crl *self, HcfEncodingBlob *encodedByte)
|
||||
{
|
||||
if ((self == NULL) || (encodedByte == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineGetEncoded(
|
||||
((HcfX509CrlImpl *)self)->spiObj, encodedByte);
|
||||
}
|
||||
|
||||
static long GetVersion(HcfX509Crl *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return OPENSSL_INVALID_VERSION;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return OPENSSL_INVALID_VERSION;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineGetVersion(
|
||||
((HcfX509CrlImpl *)self)->spiObj);
|
||||
}
|
||||
|
||||
static HcfResult GetIssuerName(HcfX509Crl *self, HcfBlob *out)
|
||||
{
|
||||
if ((self == NULL) || (out == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineGetIssuerName(
|
||||
((HcfX509CrlImpl *)self)->spiObj, out);
|
||||
}
|
||||
|
||||
static HcfResult GetLastUpdate(HcfX509Crl *self, HcfBlob *out)
|
||||
{
|
||||
if ((self == NULL) || (out == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineGetLastUpdate(
|
||||
((HcfX509CrlImpl *)self)->spiObj, out);
|
||||
}
|
||||
|
||||
static HcfResult GetNextUpdate(HcfX509Crl *self, HcfBlob *out)
|
||||
{
|
||||
if ((self == NULL) || (out == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineGetNextUpdate(
|
||||
((HcfX509CrlImpl *)self)->spiObj, out);
|
||||
}
|
||||
|
||||
static HcfResult GetRevokedCert(HcfX509Crl *self, long serialNumber, HcfX509CrlEntry **entryOut)
|
||||
{
|
||||
if (self == NULL || entryOut == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineGetRevokedCert(
|
||||
((HcfX509CrlImpl *)self)->spiObj, serialNumber, entryOut);
|
||||
}
|
||||
|
||||
static HcfResult GetRevokedCertWithCert(HcfX509Crl *self, HcfX509Certificate *cert, HcfX509CrlEntry **entryOut)
|
||||
{
|
||||
if ((self == NULL) || (cert == NULL) || (entryOut == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineGetRevokedCertWithCert(
|
||||
((HcfX509CrlImpl *)self)->spiObj, cert, entryOut);
|
||||
}
|
||||
|
||||
static HcfResult GetRevokedCerts(HcfX509Crl *self, HcfArray *entrysOut)
|
||||
{
|
||||
if ((self == NULL) || (entrysOut == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineGetRevokedCerts(
|
||||
((HcfX509CrlImpl *)self)->spiObj, entrysOut);
|
||||
}
|
||||
|
||||
static HcfResult GetTbsInfo(HcfX509Crl *self, HcfBlob *tbsCertListOut)
|
||||
{
|
||||
if ((self == NULL) || (tbsCertListOut == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineGetTbsInfo(
|
||||
((HcfX509CrlImpl *)self)->spiObj, tbsCertListOut);
|
||||
}
|
||||
|
||||
static HcfResult GetSignature(HcfX509Crl *self, HcfBlob *signature)
|
||||
{
|
||||
if ((self == NULL) || (signature == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineGetSignature(
|
||||
((HcfX509CrlImpl *)self)->spiObj, signature);
|
||||
}
|
||||
|
||||
static HcfResult GetSignatureAlgName(HcfX509Crl *self, HcfBlob *out)
|
||||
{
|
||||
if ((self == NULL) || (out == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineGetSignatureAlgName(
|
||||
((HcfX509CrlImpl *)self)->spiObj, out);
|
||||
}
|
||||
|
||||
static HcfResult GetSignatureAlgOid(HcfX509Crl *self, HcfBlob *out)
|
||||
{
|
||||
if ((self == NULL) || (out == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineGetSignatureAlgOid(
|
||||
((HcfX509CrlImpl *)self)->spiObj, out);
|
||||
}
|
||||
|
||||
static HcfResult GetSignatureAlgParams(HcfX509Crl *self, HcfBlob *sigAlgParamOut)
|
||||
{
|
||||
if ((self == NULL) || (sigAlgParamOut == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetX509CrlClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfX509CrlImpl *)self)->spiObj->engineGetSignatureAlgParams(
|
||||
((HcfX509CrlImpl *)self)->spiObj, sigAlgParamOut);
|
||||
}
|
||||
|
||||
HcfResult HcfX509CrlCreate(const HcfEncodingBlob *inStream, HcfX509Crl **returnObj)
|
||||
{
|
||||
if ((inStream == NULL) || (inStream->data == NULL) || (inStream->len > HCF_MAX_BUFFER_LEN) || (returnObj == NULL)) {
|
||||
LOGE("FuncSet is null!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
const HcfX509CrlFuncSet *funcSet = FindAbility("X509");
|
||||
if (funcSet == NULL) {
|
||||
LOGE("FuncSet is null!");
|
||||
return HCF_NOT_SUPPORT;
|
||||
}
|
||||
HcfX509CrlSpi *spiObj = NULL;
|
||||
HcfResult res = funcSet->createFunc(inStream, &spiObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("Failed to create spi object!");
|
||||
return res;
|
||||
}
|
||||
HcfX509CrlImpl *x509CertImpl = (HcfX509CrlImpl *)HcfMalloc(sizeof(HcfX509CrlImpl), 0);
|
||||
if (x509CertImpl == NULL) {
|
||||
LOGE("Failed to allocate x509CertImpl memory!");
|
||||
return HCF_ERR_MALLOC;
|
||||
}
|
||||
x509CertImpl->base.base.base.getClass = GetX509CrlClass;
|
||||
x509CertImpl->base.base.base.destroy = DestroyX509Crl;
|
||||
x509CertImpl->base.base.getType = GetType;
|
||||
x509CertImpl->base.base.isRevoked = IsRevoked;
|
||||
x509CertImpl->base.verify = Verify;
|
||||
x509CertImpl->base.getEncoded = GetEncoded;
|
||||
x509CertImpl->base.getVersion = GetVersion;
|
||||
x509CertImpl->base.getIssuerName = GetIssuerName;
|
||||
x509CertImpl->base.getLastUpdate = GetLastUpdate;
|
||||
x509CertImpl->base.getNextUpdate = GetNextUpdate;
|
||||
x509CertImpl->base.getRevokedCert = GetRevokedCert;
|
||||
x509CertImpl->base.getRevokedCertWithCert = GetRevokedCertWithCert;
|
||||
x509CertImpl->base.getRevokedCerts = GetRevokedCerts;
|
||||
x509CertImpl->base.getTbsInfo = GetTbsInfo;
|
||||
x509CertImpl->base.getSignature = GetSignature;
|
||||
x509CertImpl->base.getSignatureAlgName = GetSignatureAlgName;
|
||||
x509CertImpl->base.getSignatureAlgOid = GetSignatureAlgOid;
|
||||
x509CertImpl->base.getSignatureAlgParams = GetSignatureAlgParams;
|
||||
x509CertImpl->spiObj = spiObj;
|
||||
*returnObj = (HcfX509Crl *)x509CertImpl;
|
||||
return HCF_SUCCESS;
|
||||
}
|
272
frameworks/crypto_operation/cipher.c
Normal file
272
frameworks/crypto_operation/cipher.c
Normal file
@ -0,0 +1,272 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "cipher.h"
|
||||
#include "aes_openssl.h"
|
||||
#include "config.h"
|
||||
#include "aes_openssl_common.h"
|
||||
#include "securec.h"
|
||||
#include "result.h"
|
||||
#include "string.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
#include "cipher_rsa_openssl.h"
|
||||
#include "utils.h"
|
||||
|
||||
typedef HcfResult (*HcfCipherGeneratorSpiCreateFunc)(CipherAttr *, OH_HCF_CipherGeneratorSpi **);
|
||||
|
||||
typedef struct {
|
||||
HcfCipher super;
|
||||
OH_HCF_CipherGeneratorSpi *spiObj;
|
||||
char algoName[HCF_MAX_ALGO_NAME_LEN];
|
||||
} CipherGenImpl;
|
||||
|
||||
typedef struct {
|
||||
HcfCipherGeneratorSpiCreateFunc createFunc;
|
||||
} HcfCipherGenFuncSet;
|
||||
|
||||
typedef struct {
|
||||
HCF_ALG_VALUE algo;
|
||||
HcfCipherGenFuncSet funcSet;
|
||||
} HcfCipherGenAbility;
|
||||
|
||||
static const HcfCipherGenAbility CIPHER_ABILITY_SET[] = {
|
||||
{ HCF_ALG_RSA, { HcfCipherRsaCipherSpiCreate } },
|
||||
{ HCF_ALG_AES, { HcfCipherAesGeneratorSpiCreate } },
|
||||
{ HCF_ALG_DES, { HcfCipherDesGeneratorSpiCreate } }
|
||||
};
|
||||
|
||||
static void SetKeyLength(HCF_ALG_PARA_VALUE value, void *cipher)
|
||||
{
|
||||
CipherAttr *cipherAttr = (CipherAttr *)cipher;
|
||||
|
||||
cipherAttr->keySize = value;
|
||||
|
||||
switch (value) {
|
||||
case HCF_ALG_AES_128:
|
||||
case HCF_ALG_AES_192:
|
||||
case HCF_ALG_AES_256:
|
||||
cipherAttr->algo = HCF_ALG_AES;
|
||||
break;
|
||||
case HCF_ALG_3DES_192:
|
||||
cipherAttr->algo = HCF_ALG_DES;
|
||||
break;
|
||||
case HCF_OPENSSL_RSA_512:
|
||||
case HCF_OPENSSL_RSA_768:
|
||||
case HCF_OPENSSL_RSA_1024:
|
||||
case HCF_OPENSSL_RSA_2048:
|
||||
case HCF_OPENSSL_RSA_3072:
|
||||
case HCF_OPENSSL_RSA_4096:
|
||||
case HCF_OPENSSL_RSA_8192:
|
||||
cipherAttr->algo = HCF_ALG_RSA;
|
||||
break;
|
||||
default:
|
||||
LOGE("Invalid algo %u.", value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void SetMode(HCF_ALG_PARA_VALUE value, void *cipher)
|
||||
{
|
||||
CipherAttr *cipherAttr = (CipherAttr *)cipher;
|
||||
cipherAttr->mode = value ;
|
||||
}
|
||||
|
||||
static void SetPadding(HCF_ALG_PARA_VALUE value, void *cipher)
|
||||
{
|
||||
CipherAttr *cipherAttr = (CipherAttr *)cipher;
|
||||
cipherAttr->paddingMode = value;
|
||||
}
|
||||
|
||||
static void SetDigest(HCF_ALG_PARA_VALUE value, CipherAttr *cipher)
|
||||
{
|
||||
cipher->md = value;
|
||||
}
|
||||
|
||||
static void SetMgf1Digest(HCF_ALG_PARA_VALUE value, CipherAttr *cipher)
|
||||
{
|
||||
cipher->mgf1md = value;
|
||||
}
|
||||
|
||||
static HcfResult OnSetParameter(const HcfParaConfig *config, void *cipher)
|
||||
{
|
||||
if ((config == NULL) || (cipher == NULL)) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcfResult ret = HCF_SUCCESS;
|
||||
LOGD("Set Parameter:%s", config->tag);
|
||||
switch (config->para_type) {
|
||||
case HCF_ALG_TYPE:
|
||||
case HCF_ALG_KEY_TYPE:
|
||||
SetKeyLength(config->para_value, cipher);
|
||||
break;
|
||||
case HCF_ALG_MODE:
|
||||
SetMode(config->para_value, cipher);
|
||||
break;
|
||||
case HCF_ALG_PADDING_TYPE:
|
||||
SetPadding(config->para_value, cipher);
|
||||
break;
|
||||
case HCF_ALG_DIGEST:
|
||||
SetDigest(config->para_value, cipher);
|
||||
break;
|
||||
case HCF_ALG_MGF1_DIGEST:
|
||||
SetMgf1Digest(config->para_value, cipher);
|
||||
break;
|
||||
default:
|
||||
ret = HCF_INVALID_PARAMS;
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const char *GetCipherGeneratorClass()
|
||||
{
|
||||
return "OH_HCF_CipherGenerator";
|
||||
}
|
||||
|
||||
const char *GetAlogrithm(HcfCipher *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("The input self ptr is NULL!");
|
||||
return NULL;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetCipherGeneratorClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return NULL;
|
||||
}
|
||||
return ((CipherGenImpl *)self)->algoName;
|
||||
}
|
||||
|
||||
static void CipherDestroy(HcfObjectBase *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
return;
|
||||
}
|
||||
if (!IsClassMatch(self, GetCipherGeneratorClass())) {
|
||||
LOGE("Class not match.");
|
||||
return;
|
||||
}
|
||||
CipherGenImpl *impl = (CipherGenImpl *)self;
|
||||
OH_HCF_ObjDestroy(impl->spiObj);
|
||||
HcfFree(impl);
|
||||
}
|
||||
|
||||
static HcfResult CipherInit(HcfCipher *self, enum HcfCryptoMode opMode,
|
||||
HcfKey *key, HcfParamsSpec *params)
|
||||
{
|
||||
if (self == NULL || key == NULL) { /* params maybe is NULL */
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetCipherGeneratorClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
CipherGenImpl *impl = (CipherGenImpl *)self;
|
||||
return impl->spiObj->init(impl->spiObj, opMode, key, params);
|
||||
}
|
||||
|
||||
static HcfResult CipherUpdate(HcfCipher *self, HcfBlob *input, HcfBlob *output)
|
||||
{
|
||||
if ((self == NULL) || (input == NULL) || (output == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetCipherGeneratorClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
CipherGenImpl *impl = (CipherGenImpl *)self;
|
||||
return impl->spiObj->update(impl->spiObj, input, output);
|
||||
}
|
||||
|
||||
static HcfResult CipherFinal(HcfCipher *self, HcfBlob *input, HcfBlob *output)
|
||||
{
|
||||
if ((self == NULL) || (output == NULL)) {
|
||||
LOGE("Invalid input parameter!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetCipherGeneratorClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
CipherGenImpl *impl = (CipherGenImpl *)self;
|
||||
return impl->spiObj->doFinal(impl->spiObj, input, output);
|
||||
}
|
||||
|
||||
static void InitCipher(OH_HCF_CipherGeneratorSpi *spiObj, CipherGenImpl *cipher)
|
||||
{
|
||||
cipher->super.init = CipherInit;
|
||||
cipher->super.update = CipherUpdate;
|
||||
cipher->super.doFinal = CipherFinal;
|
||||
cipher->super.getAlgorithm = GetAlogrithm;
|
||||
cipher->super.base.destroy = CipherDestroy;
|
||||
cipher->super.base.getClass = GetCipherGeneratorClass;
|
||||
}
|
||||
|
||||
static const HcfCipherGenFuncSet *FindAbility(CipherAttr *attr)
|
||||
{
|
||||
if (attr == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
for (uint32_t i = 0; i < sizeof(CIPHER_ABILITY_SET) / sizeof(HcfCipherGenAbility); i++) {
|
||||
if (CIPHER_ABILITY_SET[i].algo == attr->algo) {
|
||||
return &(CIPHER_ABILITY_SET[i].funcSet);
|
||||
}
|
||||
}
|
||||
LOGE("Algo not support! [Algo]: %d", attr->algo);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
HcfResult HcfCipherCreate(const char *transformation, HcfCipher **cipher)
|
||||
{
|
||||
CipherAttr attr = {0};
|
||||
if (!IsStrValid(transformation, HCF_MAX_ALGO_NAME_LEN) || (cipher == NULL)) {
|
||||
LOGE("Invalid input params while creating cipher!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (ParseAndSetParameter(transformation, (void *)&attr, OnSetParameter) != HCF_SUCCESS) {
|
||||
LOGE("ParseAndSetParameter failed!");
|
||||
return HCF_NOT_SUPPORT;
|
||||
}
|
||||
|
||||
const HcfCipherGenFuncSet *funcSet = FindAbility(&attr);
|
||||
if (funcSet == NULL) {
|
||||
LOGE("FindAbility failed!");
|
||||
return HCF_NOT_SUPPORT;
|
||||
}
|
||||
CipherGenImpl *returnGenerator = (CipherGenImpl *)HcfMalloc(sizeof(CipherGenImpl), 0);
|
||||
if (returnGenerator == NULL) {
|
||||
LOGE("failed to allocate returnGenerator memory!");
|
||||
return HCF_ERR_MALLOC;
|
||||
}
|
||||
if (strcpy_s(returnGenerator->algoName, HCF_MAX_ALGO_NAME_LEN, transformation) != EOK) {
|
||||
LOGE("Failed to copy algoName!");
|
||||
HcfFree(returnGenerator);
|
||||
return HCF_ERR_COPY;
|
||||
}
|
||||
OH_HCF_CipherGeneratorSpi *spiObj = NULL;
|
||||
HcfResult res = funcSet->createFunc(&attr, &spiObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("Failed to create spi object!");
|
||||
HcfFree(returnGenerator);
|
||||
return res;
|
||||
}
|
||||
returnGenerator->spiObj = spiObj;
|
||||
InitCipher(spiObj, returnGenerator);
|
||||
|
||||
*cipher = (HcfCipher *)returnGenerator;
|
||||
return res;
|
||||
}
|
182
frameworks/crypto_operation/key_agreement.c
Normal file
182
frameworks/crypto_operation/key_agreement.c
Normal file
@ -0,0 +1,182 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "key_agreement.h"
|
||||
|
||||
#include <securec.h>
|
||||
|
||||
#include "key_agreement_spi.h"
|
||||
#include "config.h"
|
||||
#include "ecdh_openssl.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
#include "params_parser.h"
|
||||
#include "utils.h"
|
||||
|
||||
typedef HcfResult (*HcfKeyAgreementSpiCreateFunc)(HcfKeyAgreementParams *, HcfKeyAgreementSpi **);
|
||||
|
||||
typedef struct {
|
||||
HcfKeyAgreement base;
|
||||
|
||||
HcfKeyAgreementSpi *spiObj;
|
||||
|
||||
char algoName[HCF_MAX_ALGO_NAME_LEN];
|
||||
} HcfKeyAgreementImpl;
|
||||
|
||||
typedef struct {
|
||||
HCF_ALG_VALUE algo;
|
||||
|
||||
HcfKeyAgreementSpiCreateFunc createSpifunc;
|
||||
} HcfKeyAgreementGenAbility;
|
||||
|
||||
static const HcfKeyAgreementGenAbility KEY_AGREEMENT_GEN_ABILITY_SET[] = {
|
||||
{ HCF_ALG_ECC, HcfKeyAgreementSpiEcdhCreate }
|
||||
};
|
||||
|
||||
static HcfKeyAgreementSpiCreateFunc FindAbility(HcfKeyAgreementParams *params)
|
||||
{
|
||||
for (uint32_t i = 0; i < sizeof(KEY_AGREEMENT_GEN_ABILITY_SET) / sizeof(KEY_AGREEMENT_GEN_ABILITY_SET[0]); i++) {
|
||||
if (KEY_AGREEMENT_GEN_ABILITY_SET[i].algo == params->algo) {
|
||||
return KEY_AGREEMENT_GEN_ABILITY_SET[i].createSpifunc;
|
||||
}
|
||||
}
|
||||
LOGE("Algo not support! [Algo]: %d", params->algo);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void SetKeyType(HCF_ALG_PARA_VALUE value, HcfKeyAgreementParams *paramsObj)
|
||||
{
|
||||
switch (value) {
|
||||
case HCF_ALG_ECC_224:
|
||||
case HCF_ALG_ECC_256:
|
||||
case HCF_ALG_ECC_384:
|
||||
case HCF_ALG_ECC_512:
|
||||
paramsObj->keyLen = value;
|
||||
paramsObj->algo = HCF_ALG_ECC;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static HcfResult ParseKeyAgreementParams(const HcfParaConfig* config, void *params)
|
||||
{
|
||||
if (config == NULL || params == NULL) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcfResult ret = HCF_SUCCESS;
|
||||
HcfKeyAgreementParams *paramsObj = (HcfKeyAgreementParams *)params;
|
||||
LOGI("Set Parameter: %s", config->tag);
|
||||
switch (config->para_type) {
|
||||
case HCF_ALG_KEY_TYPE:
|
||||
SetKeyType(config->para_value, paramsObj);
|
||||
break;
|
||||
default:
|
||||
ret = HCF_INVALID_PARAMS;
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// export interfaces
|
||||
static const char *GetKeyAgreementClass(void)
|
||||
{
|
||||
return "HcfKeyAgreement";
|
||||
}
|
||||
|
||||
static const char *GetAlgoName(HcfKeyAgreement *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("The input self ptr is NULL!");
|
||||
return NULL;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetKeyAgreementClass())) {
|
||||
return NULL;
|
||||
}
|
||||
return ((HcfKeyAgreementImpl *)self)->algoName;
|
||||
}
|
||||
|
||||
static HcfResult GenerateSecret(HcfKeyAgreement *self, HcfPriKey *priKey,
|
||||
HcfPubKey *pubKey, HcfBlob *returnSecret)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetKeyAgreementClass())) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
return ((HcfKeyAgreementImpl *)self)->spiObj->engineGenerateSecret(
|
||||
((HcfKeyAgreementImpl *)self)->spiObj, priKey, pubKey, returnSecret);
|
||||
}
|
||||
|
||||
static void DestroyKeyAgreement(HcfObjectBase *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
return;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetKeyAgreementClass())) {
|
||||
return;
|
||||
}
|
||||
HcfKeyAgreementImpl *impl = (HcfKeyAgreementImpl *)self;
|
||||
OH_HCF_ObjDestroy(impl->spiObj);
|
||||
impl->spiObj = NULL;
|
||||
HcfFree(impl);
|
||||
}
|
||||
|
||||
HcfResult HcfKeyAgreementCreate(const char *algoName, HcfKeyAgreement **returnObj)
|
||||
{
|
||||
if ((!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN)) || (returnObj == NULL)) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
HcfKeyAgreementParams params = { 0 };
|
||||
if (ParseAndSetParameter(algoName, ¶ms, ParseKeyAgreementParams) != HCF_SUCCESS) {
|
||||
LOGE("Failed to parser parmas!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
HcfKeyAgreementSpiCreateFunc createSpifunc = FindAbility(¶ms);
|
||||
if (createSpifunc == NULL) {
|
||||
return HCF_NOT_SUPPORT;
|
||||
}
|
||||
|
||||
HcfKeyAgreementImpl *returnGenerator = (HcfKeyAgreementImpl *)HcfMalloc(sizeof(HcfKeyAgreementImpl), 0);
|
||||
if (returnGenerator == NULL) {
|
||||
LOGE("Failed to allocate returnGenerator memory!");
|
||||
return HCF_ERR_MALLOC;
|
||||
}
|
||||
if (strcpy_s(returnGenerator->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) {
|
||||
LOGE("Failed to copy algoName!");
|
||||
HcfFree(returnGenerator);
|
||||
return HCF_ERR_COPY;
|
||||
}
|
||||
HcfKeyAgreementSpi *spiObj = NULL;
|
||||
int32_t res = createSpifunc(¶ms, &spiObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("Failed to create spi object!");
|
||||
HcfFree(returnGenerator);
|
||||
return res;
|
||||
}
|
||||
returnGenerator->base.base.destroy = DestroyKeyAgreement;
|
||||
returnGenerator->base.base.getClass = GetKeyAgreementClass;
|
||||
returnGenerator->base.generateSecret = GenerateSecret;
|
||||
returnGenerator->base.getAlgoName = GetAlgoName;
|
||||
returnGenerator->spiObj = spiObj;
|
||||
|
||||
*returnObj = (HcfKeyAgreement *)returnGenerator;
|
||||
return HCF_SUCCESS;
|
||||
}
|
190
frameworks/crypto_operation/mac.c
Normal file
190
frameworks/crypto_operation/mac.c
Normal file
@ -0,0 +1,190 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "mac.h"
|
||||
|
||||
#include <securec.h>
|
||||
|
||||
#include "mac_spi.h"
|
||||
#include "mac_openssl.h"
|
||||
|
||||
#include "log.h"
|
||||
#include "config.h"
|
||||
#include "memory.h"
|
||||
#include "utils.h"
|
||||
|
||||
typedef HcfResult (*HcfMacSpiCreateFunc)(const char *, HcfMacSpi **);
|
||||
|
||||
typedef struct {
|
||||
HcfMac base;
|
||||
|
||||
HcfMacSpi *spiObj;
|
||||
|
||||
char algoName[HCF_MAX_ALGO_NAME_LEN];
|
||||
} HcfMacImpl;
|
||||
|
||||
typedef struct {
|
||||
char *algoName;
|
||||
|
||||
HcfMacSpiCreateFunc createSpifunc;
|
||||
} HcfMacAbility;
|
||||
|
||||
static const HcfMacAbility MAC_ABILITY_SET[] = {
|
||||
{ "SHA1", OpensslMacSpiCreate },
|
||||
{ "SHA224", OpensslMacSpiCreate },
|
||||
{ "SHA256", OpensslMacSpiCreate },
|
||||
{ "SHA384", OpensslMacSpiCreate },
|
||||
{ "SHA512", OpensslMacSpiCreate },
|
||||
};
|
||||
|
||||
static const char *GetMacClass(void)
|
||||
{
|
||||
return "HMAC";
|
||||
}
|
||||
|
||||
static HcfMacSpiCreateFunc FindAbility(const char *algoName)
|
||||
{
|
||||
for (uint32_t i = 0; i < (sizeof(MAC_ABILITY_SET) / sizeof(MAC_ABILITY_SET[0])); i++) {
|
||||
if (strcmp(MAC_ABILITY_SET[i].algoName, algoName) == 0) {
|
||||
return MAC_ABILITY_SET[i].createSpifunc;
|
||||
}
|
||||
}
|
||||
LOGE("Algo not support! [Algo]: %s", algoName);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static HcfResult Init(HcfMac *self, const HcfSymKey *key)
|
||||
{
|
||||
if ((self == NULL) || (key == NULL)) {
|
||||
LOGE("The input self ptr or key is NULL!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetMacClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfMacImpl *)self)->spiObj->engineInitMac(
|
||||
((HcfMacImpl *)self)->spiObj, key);
|
||||
}
|
||||
|
||||
static HcfResult Update(HcfMac *self, HcfBlob *input)
|
||||
{
|
||||
if ((self == NULL) || (!IsBlobValid(input))) {
|
||||
LOGE("The input self ptr or dataBlob is NULL!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetMacClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfMacImpl *)self)->spiObj->engineUpdateMac(
|
||||
((HcfMacImpl *)self)->spiObj, input);
|
||||
}
|
||||
|
||||
static HcfResult DoFinal(HcfMac *self, HcfBlob *output)
|
||||
{
|
||||
if ((self == NULL) || (output == NULL)) {
|
||||
LOGE("The input self ptr or dataBlob is NULL!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetMacClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfMacImpl *)self)->spiObj->engineDoFinalMac(
|
||||
((HcfMacImpl *)self)->spiObj, output);
|
||||
}
|
||||
|
||||
static uint32_t GetMacLength(HcfMac *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("The input self ptr is NULL!");
|
||||
return 0;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetMacClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return 0;
|
||||
}
|
||||
return ((HcfMacImpl *)self)->spiObj->engineGetMacLength(
|
||||
((HcfMacImpl *)self)->spiObj);
|
||||
}
|
||||
|
||||
static const char *GetAlgoName(HcfMac *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("The input self ptr is NULL!");
|
||||
return NULL;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetMacClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return NULL;
|
||||
}
|
||||
return ((HcfMacImpl *)self)->algoName;
|
||||
}
|
||||
|
||||
static void MacDestroy(HcfObjectBase *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("The input self ptr is NULL!");
|
||||
return;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetMacClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return;
|
||||
}
|
||||
HcfMacImpl *impl = (HcfMacImpl *)self;
|
||||
OH_HCF_ObjDestroy(impl->spiObj);
|
||||
HcfFree(impl);
|
||||
}
|
||||
|
||||
HcfResult HcfMacCreate(const char *algoName, HcfMac **macApi)
|
||||
{
|
||||
if (!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (macApi == NULL)) {
|
||||
LOGE("Invalid input params while creating mac!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcfMacSpiCreateFunc createSpifunc = FindAbility(algoName);
|
||||
if (createSpifunc == NULL) {
|
||||
LOGE("Algo not supported!");
|
||||
return HCF_NOT_SUPPORT;
|
||||
}
|
||||
HcfMacImpl *returnMacApi = (HcfMacImpl *)HcfMalloc(sizeof(HcfMacImpl), 0);
|
||||
if (returnMacApi == NULL) {
|
||||
LOGE("Failed to allocate Mac Obj memory!");
|
||||
return HCF_ERR_MALLOC;
|
||||
}
|
||||
if (strcpy_s(returnMacApi->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) {
|
||||
LOGE("Failed to copy algoName!");
|
||||
HcfFree(returnMacApi);
|
||||
return HCF_ERR_COPY;
|
||||
}
|
||||
HcfMacSpi *spiObj = NULL;
|
||||
HcfResult res = createSpifunc(algoName, &spiObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("Failed to create spi object!");
|
||||
HcfFree(returnMacApi);
|
||||
return res;
|
||||
}
|
||||
returnMacApi->base.base.getClass = GetMacClass;
|
||||
returnMacApi->base.base.destroy = MacDestroy;
|
||||
returnMacApi->base.init = Init;
|
||||
returnMacApi->base.update = Update;
|
||||
returnMacApi->base.doFinal = DoFinal;
|
||||
returnMacApi->base.getMacLength = GetMacLength;
|
||||
returnMacApi->base.getAlgoName = GetAlgoName;
|
||||
returnMacApi->spiObj = spiObj;
|
||||
*macApi = (HcfMac *)returnMacApi;
|
||||
return HCF_SUCCESS;
|
||||
}
|
177
frameworks/crypto_operation/md.c
Normal file
177
frameworks/crypto_operation/md.c
Normal file
@ -0,0 +1,177 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "md.h"
|
||||
|
||||
#include <securec.h>
|
||||
|
||||
#include "sym_key.h"
|
||||
#include "md_spi.h"
|
||||
#include "md_openssl.h"
|
||||
|
||||
#include "log.h"
|
||||
#include "config.h"
|
||||
#include "memory.h"
|
||||
#include "utils.h"
|
||||
|
||||
typedef HcfResult (*HcfMdSpiCreateFunc)(const char *, HcfMdSpi **);
|
||||
|
||||
typedef struct {
|
||||
HcfMd base;
|
||||
|
||||
HcfMdSpi *spiObj;
|
||||
|
||||
char algoName[HCF_MAX_ALGO_NAME_LEN];
|
||||
} HcfMdImpl;
|
||||
|
||||
typedef struct {
|
||||
char *algoName;
|
||||
|
||||
HcfMdSpiCreateFunc createSpifunc;
|
||||
} HcfMdAbility;
|
||||
|
||||
static const HcfMdAbility MD_ABILITY_SET[] = {
|
||||
{ "SHA1", OpensslMdSpiCreate },
|
||||
{ "SHA224", OpensslMdSpiCreate },
|
||||
{ "SHA256", OpensslMdSpiCreate },
|
||||
{ "SHA384", OpensslMdSpiCreate },
|
||||
{ "SHA512", OpensslMdSpiCreate },
|
||||
{ "MD5", OpensslMdSpiCreate },
|
||||
};
|
||||
|
||||
static const char *GetMdClass(void)
|
||||
{
|
||||
return "Md";
|
||||
}
|
||||
|
||||
static HcfMdSpiCreateFunc FindAbility(const char *algoName)
|
||||
{
|
||||
for (uint32_t i = 0; i < (sizeof(MD_ABILITY_SET) / sizeof(MD_ABILITY_SET[0])); i++) {
|
||||
if (strcmp(MD_ABILITY_SET[i].algoName, algoName) == 0) {
|
||||
return MD_ABILITY_SET[i].createSpifunc;
|
||||
}
|
||||
}
|
||||
LOGE("Algo not support! [Algo]: %s", algoName);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static HcfResult Update(HcfMd *self, HcfBlob *input)
|
||||
{
|
||||
if ((self == NULL) || (!IsBlobValid(input))) {
|
||||
LOGE("The input self ptr or dataBlob is NULL!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetMdClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfMdImpl *)self)->spiObj->engineUpdateMd(
|
||||
((HcfMdImpl *)self)->spiObj, input);
|
||||
}
|
||||
|
||||
static HcfResult DoFinal(HcfMd *self, HcfBlob *output)
|
||||
{
|
||||
if ((self == NULL) || (output == NULL)) {
|
||||
LOGE("The input self ptr or dataBlob is NULL!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetMdClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfMdImpl *)self)->spiObj->engineDoFinalMd(
|
||||
((HcfMdImpl *)self)->spiObj, output);
|
||||
}
|
||||
|
||||
static uint32_t GetMdLength(HcfMd *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("The input self ptr is NULL!");
|
||||
return 0;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetMdClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return 0;
|
||||
}
|
||||
return ((HcfMdImpl *)self)->spiObj->engineGetMdLength(
|
||||
((HcfMdImpl *)self)->spiObj);
|
||||
}
|
||||
|
||||
static const char *GetAlgoName(HcfMd *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("The input self ptr is NULL!");
|
||||
return NULL;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetMdClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return NULL;
|
||||
}
|
||||
return ((HcfMdImpl *)self)->algoName;
|
||||
}
|
||||
|
||||
static void MdDestroy(HcfObjectBase *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("The input self ptr is NULL!");
|
||||
return;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetMdClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return;
|
||||
}
|
||||
HcfMdImpl *impl = (HcfMdImpl *)self;
|
||||
OH_HCF_ObjDestroy(impl->spiObj);
|
||||
HcfFree(impl);
|
||||
}
|
||||
|
||||
HcfResult HcfMdCreate(const char *algoName, HcfMd **mdApi)
|
||||
{
|
||||
if (!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (mdApi == NULL)) {
|
||||
LOGE("Invalid input params while creating md!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcfMdSpiCreateFunc createSpifunc = FindAbility(algoName);
|
||||
if (createSpifunc == NULL) {
|
||||
LOGE("Algo not supported!");
|
||||
return HCF_NOT_SUPPORT;
|
||||
}
|
||||
HcfMdImpl *returnMdApi = (HcfMdImpl *)HcfMalloc(sizeof(HcfMdImpl), 0);
|
||||
if (returnMdApi == NULL) {
|
||||
LOGE("Failed to allocate Md Obj memory!");
|
||||
return HCF_ERR_MALLOC;
|
||||
}
|
||||
if (strcpy_s(returnMdApi->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) {
|
||||
LOGE("Failed to copy algoName!");
|
||||
HcfFree(returnMdApi);
|
||||
return HCF_ERR_COPY;
|
||||
}
|
||||
HcfMdSpi *spiObj = NULL;
|
||||
HcfResult res = createSpifunc(algoName, &spiObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("Failed to create spi object!");
|
||||
HcfFree(returnMdApi);
|
||||
return res;
|
||||
}
|
||||
returnMdApi->base.base.getClass = GetMdClass;
|
||||
returnMdApi->base.base.destroy = MdDestroy;
|
||||
returnMdApi->base.update = Update;
|
||||
returnMdApi->base.doFinal = DoFinal;
|
||||
returnMdApi->base.getMdLength = GetMdLength;
|
||||
returnMdApi->base.getAlgoName = GetAlgoName;
|
||||
returnMdApi->spiObj = spiObj;
|
||||
*mdApi = (HcfMd *)returnMdApi;
|
||||
return HCF_SUCCESS;
|
||||
}
|
387
frameworks/crypto_operation/signature.c
Normal file
387
frameworks/crypto_operation/signature.c
Normal file
@ -0,0 +1,387 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "signature.h"
|
||||
|
||||
#include <securec.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "ecdsa_openssl.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
#include "params_parser.h"
|
||||
#include "signature_spi.h"
|
||||
#include "signature_rsa_openssl.h"
|
||||
#include "utils.h"
|
||||
|
||||
typedef HcfResult (*HcfSignSpiCreateFunc)(HcfSignatureParams *, HcfSignSpi **);
|
||||
typedef HcfResult (*HcfVerifySpiCreateFunc)(HcfSignatureParams *, HcfVerifySpi **);
|
||||
|
||||
typedef struct {
|
||||
HcfSign base;
|
||||
|
||||
HcfSignSpi *spiObj;
|
||||
|
||||
char algoName[HCF_MAX_ALGO_NAME_LEN];
|
||||
} HcfSignImpl;
|
||||
|
||||
typedef struct {
|
||||
HcfVerify base;
|
||||
|
||||
HcfVerifySpi *spiObj;
|
||||
|
||||
char algoName[HCF_MAX_ALGO_NAME_LEN];
|
||||
} HcfVerifyImpl;
|
||||
|
||||
typedef struct {
|
||||
const char *algoType;
|
||||
|
||||
uint32_t digestAlg;
|
||||
|
||||
int32_t keyLen;
|
||||
|
||||
int32_t padding;
|
||||
} HcfEcdsaParams;
|
||||
|
||||
typedef struct {
|
||||
HCF_ALG_VALUE algo;
|
||||
|
||||
HcfSignSpiCreateFunc createFunc;
|
||||
} HcfSignGenAbility;
|
||||
|
||||
typedef struct {
|
||||
HCF_ALG_VALUE algo;
|
||||
|
||||
HcfVerifySpiCreateFunc createFunc;
|
||||
} HcfVerifyGenAbility;
|
||||
|
||||
static const HcfSignGenAbility SIGN_GEN_ABILITY_SET[] = {
|
||||
{ HCF_ALG_ECC, HcfSignSpiEcdsaCreate },
|
||||
{ HCF_ALG_RSA, HcfSignSpiRsaCreate }
|
||||
};
|
||||
|
||||
static const HcfVerifyGenAbility VERIFY_GEN_ABILITY_SET[] = {
|
||||
{ HCF_ALG_ECC, HcfVerifySpiEcdsaCreate },
|
||||
{ HCF_ALG_RSA, HcfVerifySpiRsaCreate }
|
||||
};
|
||||
|
||||
static HcfSignSpiCreateFunc FindSignAbility(HcfSignatureParams *params)
|
||||
{
|
||||
for (uint32_t i = 0; i < sizeof(SIGN_GEN_ABILITY_SET) / sizeof(SIGN_GEN_ABILITY_SET[0]); i++) {
|
||||
if (SIGN_GEN_ABILITY_SET[i].algo == params->algo) {
|
||||
return SIGN_GEN_ABILITY_SET[i].createFunc;
|
||||
}
|
||||
}
|
||||
LOGE("Algo not support! [Algo]: %d", params->algo);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static HcfVerifySpiCreateFunc FindVerifyAbility(HcfSignatureParams *params)
|
||||
{
|
||||
for (uint32_t i = 0; i < sizeof(VERIFY_GEN_ABILITY_SET) / sizeof(VERIFY_GEN_ABILITY_SET[0]); i++) {
|
||||
if (VERIFY_GEN_ABILITY_SET[i].algo == params->algo) {
|
||||
return VERIFY_GEN_ABILITY_SET[i].createFunc;
|
||||
}
|
||||
}
|
||||
LOGE("Algo not support! [Algo]: %d", params->algo);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void SetKeyType(HCF_ALG_PARA_VALUE value, HcfSignatureParams *paramsObj)
|
||||
{
|
||||
switch (value) {
|
||||
case HCF_ALG_ECC_224:
|
||||
case HCF_ALG_ECC_256:
|
||||
case HCF_ALG_ECC_384:
|
||||
case HCF_ALG_ECC_512:
|
||||
paramsObj->keyLen = value;
|
||||
paramsObj->algo = HCF_ALG_ECC;
|
||||
break;
|
||||
case HCF_OPENSSL_RSA_512:
|
||||
case HCF_OPENSSL_RSA_768:
|
||||
case HCF_OPENSSL_RSA_1024:
|
||||
case HCF_OPENSSL_RSA_2048:
|
||||
case HCF_OPENSSL_RSA_3072:
|
||||
case HCF_OPENSSL_RSA_4096:
|
||||
case HCF_OPENSSL_RSA_8192:
|
||||
paramsObj->algo = HCF_ALG_RSA;
|
||||
break;
|
||||
default:
|
||||
LOGE("there is not matched algorithm.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static HcfResult ParseSignatureParams(const HcfParaConfig* config, void *params)
|
||||
{
|
||||
if (config == NULL || params == NULL) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcfResult ret = HCF_SUCCESS;
|
||||
HcfSignatureParams *paramsObj = (HcfSignatureParams *)params;
|
||||
LOGI("Set Parameter: %s", config->tag);
|
||||
switch (config->para_type) {
|
||||
case HCF_ALG_KEY_TYPE:
|
||||
SetKeyType(config->para_value, paramsObj);
|
||||
break;
|
||||
case HCF_ALG_DIGEST:
|
||||
paramsObj->md = config->para_value;
|
||||
break;
|
||||
case HCF_ALG_PADDING_TYPE:
|
||||
paramsObj->padding = config->para_value;
|
||||
break;
|
||||
case HCF_ALG_MGF1_DIGEST:
|
||||
paramsObj->mgf1md = config->para_value;
|
||||
break;
|
||||
default:
|
||||
ret = HCF_INVALID_PARAMS;
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const char *GetSignClass(void)
|
||||
{
|
||||
return "HcfSign";
|
||||
}
|
||||
|
||||
static const char *GetVerifyClass(void)
|
||||
{
|
||||
return "HcfVerify";
|
||||
}
|
||||
|
||||
static const char *GetSignAlgoName(HcfSign *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("The input self ptr is NULL!");
|
||||
return NULL;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetSignClass())) {
|
||||
return NULL;
|
||||
}
|
||||
return ((HcfSignImpl *)self)->algoName;
|
||||
}
|
||||
|
||||
static const char *GetVerifyAlgoName(HcfVerify *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("The input self ptr is NULL!");
|
||||
return NULL;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetVerifyClass())) {
|
||||
return NULL;
|
||||
}
|
||||
return ((HcfVerifyImpl *)self)->algoName;
|
||||
}
|
||||
|
||||
static void DestroySign(HcfObjectBase *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
return;
|
||||
}
|
||||
if (!IsClassMatch(self, GetSignClass())) {
|
||||
return;
|
||||
}
|
||||
HcfSignImpl *impl = (HcfSignImpl *)self;
|
||||
OH_HCF_ObjDestroy(impl->spiObj);
|
||||
impl->spiObj = NULL;
|
||||
HcfFree(impl);
|
||||
}
|
||||
|
||||
static void DestroyVerify(HcfObjectBase *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
return;
|
||||
}
|
||||
if (!IsClassMatch(self, GetVerifyClass())) {
|
||||
return;
|
||||
}
|
||||
HcfVerifyImpl *impl = (HcfVerifyImpl *)self;
|
||||
OH_HCF_ObjDestroy(impl->spiObj);
|
||||
impl->spiObj = NULL;
|
||||
HcfFree(impl);
|
||||
}
|
||||
|
||||
static HcfResult SignInit(HcfSign *self, HcfParamsSpec *params, HcfPriKey *privateKey)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetSignClass())) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfSignImpl *)self)->spiObj->engineInit(((HcfSignImpl *)self)->spiObj, params, privateKey);
|
||||
}
|
||||
|
||||
static HcfResult SignUpdate(HcfSign *self, HcfBlob *data)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetSignClass())) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfSignImpl *)self)->spiObj->engineUpdate(((HcfSignImpl *)self)->spiObj, data);
|
||||
}
|
||||
|
||||
static HcfResult SignDoFinal(HcfSign *self, HcfBlob *data, HcfBlob *returnSignatureData)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetSignClass())) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfSignImpl *)self)->spiObj->engineSign(((HcfSignImpl *)self)->spiObj, data, returnSignatureData);
|
||||
}
|
||||
|
||||
static HcfResult VerifyInit(HcfVerify *self, HcfParamsSpec *params, HcfPubKey *publicKey)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetVerifyClass())) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfVerifyImpl *)self)->spiObj->engineInit(((HcfVerifyImpl *)self)->spiObj, params, publicKey);
|
||||
}
|
||||
|
||||
static HcfResult VerifyUpdate(HcfVerify *self, HcfBlob *data)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetVerifyClass())) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfVerifyImpl *)self)->spiObj->engineUpdate(((HcfVerifyImpl *)self)->spiObj, data);
|
||||
}
|
||||
|
||||
static bool VerifyDoFinal(HcfVerify *self, HcfBlob *data, HcfBlob *signatureData)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return false;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetVerifyClass())) {
|
||||
return false;
|
||||
}
|
||||
return ((HcfVerifyImpl *)self)->spiObj->engineVerify(((HcfVerifyImpl *)self)->spiObj, data, signatureData);
|
||||
}
|
||||
|
||||
HcfResult HcfSignCreate(const char *algoName, HcfSign **returnObj)
|
||||
{
|
||||
LOGI("HcfSignCreate start");
|
||||
if ((!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN)) || (returnObj == NULL)) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
HcfSignatureParams params = { 0 };
|
||||
if (ParseAndSetParameter(algoName, ¶ms, ParseSignatureParams) != HCF_SUCCESS) {
|
||||
LOGE("Failed to parser parmas!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
HcfSignSpiCreateFunc createSpifunc = FindSignAbility(¶ms);
|
||||
if (createSpifunc == NULL) {
|
||||
LOGE("Can not find ability.");
|
||||
return HCF_NOT_SUPPORT;
|
||||
}
|
||||
|
||||
HcfSignImpl *returnSign = (HcfSignImpl *)HcfMalloc(sizeof(HcfSignImpl), 0);
|
||||
if (returnSign == NULL) {
|
||||
LOGE("Failed to allocate returnSign memory!");
|
||||
return HCF_ERR_MALLOC;
|
||||
}
|
||||
if (strcpy_s(returnSign->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) {
|
||||
LOGE("Failed to copy algoName!");
|
||||
HcfFree(returnSign);
|
||||
return HCF_ERR_COPY;
|
||||
}
|
||||
HcfSignSpi *spiObj = NULL;
|
||||
int32_t res = createSpifunc(¶ms, &spiObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("Failed to create spi object!");
|
||||
HcfFree(returnSign);
|
||||
return res;
|
||||
}
|
||||
returnSign->base.base.destroy = DestroySign;
|
||||
returnSign->base.base.getClass = GetSignClass;
|
||||
returnSign->base.getAlgoName = GetSignAlgoName;
|
||||
returnSign->base.init = SignInit;
|
||||
returnSign->base.update = SignUpdate;
|
||||
returnSign->base.sign = SignDoFinal;
|
||||
returnSign->spiObj = spiObj;
|
||||
|
||||
*returnObj = (HcfSign *)returnSign;
|
||||
LOGI("HcfSignCreate end");
|
||||
return HCF_SUCCESS;
|
||||
}
|
||||
|
||||
HcfResult HcfVerifyCreate(const char *algoName, HcfVerify **returnObj)
|
||||
{
|
||||
LOGI("HcfVerifyCreate start");
|
||||
if ((!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN)) || (returnObj == NULL)) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcfSignatureParams params = {0};
|
||||
if (ParseAndSetParameter(algoName, ¶ms, ParseSignatureParams) != HCF_SUCCESS) {
|
||||
LOGE("Failed to parser parmas!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
HcfVerifySpiCreateFunc createSpifunc = FindVerifyAbility(¶ms);
|
||||
if (createSpifunc == NULL) {
|
||||
return HCF_NOT_SUPPORT;
|
||||
}
|
||||
|
||||
HcfVerifyImpl *returnVerify = (HcfVerifyImpl *)HcfMalloc(sizeof(HcfVerifyImpl), 0);
|
||||
if (returnVerify == NULL) {
|
||||
LOGE("Failed to allocate returnVerify memory!");
|
||||
return HCF_ERR_MALLOC;
|
||||
}
|
||||
if (strcpy_s(returnVerify->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) {
|
||||
LOGE("Failed to copy algoName!");
|
||||
HcfFree(returnVerify);
|
||||
return HCF_ERR_COPY;
|
||||
}
|
||||
HcfVerifySpi *spiObj = NULL;
|
||||
int32_t res = createSpifunc(¶ms, &spiObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("Failed to create spi object!");
|
||||
HcfFree(returnVerify);
|
||||
return res;
|
||||
}
|
||||
returnVerify->base.base.destroy = DestroyVerify;
|
||||
returnVerify->base.base.getClass = GetVerifyClass;
|
||||
returnVerify->base.getAlgoName = GetVerifyAlgoName;
|
||||
returnVerify->base.init = VerifyInit;
|
||||
returnVerify->base.update = VerifyUpdate;
|
||||
returnVerify->base.verify = VerifyDoFinal;
|
||||
returnVerify->spiObj = spiObj;
|
||||
*returnObj = (HcfVerify *)returnVerify;
|
||||
LOGI("HcfVerifyCreate end");
|
||||
return HCF_SUCCESS;
|
||||
}
|
78
frameworks/frameworks.gni
Normal file
78
frameworks/frameworks.gni
Normal file
@ -0,0 +1,78 @@
|
||||
# Copyright (C) 2022 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.
|
||||
|
||||
base_path = "//base/security/crypto_framework"
|
||||
framework_path = "//base/security/crypto_framework/frameworks"
|
||||
plugin_path = "//base/security/crypto_framework/plugin"
|
||||
|
||||
framework_inc_path = [
|
||||
"${base_path}/interfaces/innerkits/algorithm_parameter",
|
||||
"${base_path}/interfaces/innerkits/certificate",
|
||||
"${base_path}/interfaces/innerkits/common",
|
||||
"${base_path}/interfaces/innerkits/crypto_operation",
|
||||
"${base_path}/interfaces/innerkits/key",
|
||||
"${base_path}/interfaces/innerkits/rand",
|
||||
"${base_path}/common/inc",
|
||||
"${plugin_path}/openssl_plugin/aes/inc",
|
||||
"${plugin_path}/openssl_plugin/certificate/inc",
|
||||
"${plugin_path}/openssl_plugin/crypto_operation/key_agreement/inc",
|
||||
"${plugin_path}/openssl_plugin/crypto_operation/signature/inc",
|
||||
"${plugin_path}/openssl_plugin/crypto_operation/aes/inc",
|
||||
"${plugin_path}/openssl_plugin/key/sym_key_generator/inc",
|
||||
"${plugin_path}/openssl_plugin/key/asy_key_generator/inc",
|
||||
"${plugin_path}/openssl_plugin/certificate/inc",
|
||||
"${plugin_path}/openssl_plugin/crypto_operation/hmac/inc",
|
||||
"${plugin_path}/openssl_plugin/crypto_operation/md/inc",
|
||||
"${plugin_path}/openssl_plugin/crypto_operation/rsa/inc",
|
||||
"${plugin_path}/openssl_plugin/rand/inc",
|
||||
"${framework_path}/spi",
|
||||
]
|
||||
|
||||
framework_certificate_files = [
|
||||
"${framework_path}/certificate/cert_chain_validator.c",
|
||||
"${framework_path}/certificate/x509_certificate.c",
|
||||
"${framework_path}/certificate/x509_crl.c",
|
||||
]
|
||||
|
||||
framework_cipher_files = [
|
||||
"${framework_path}/crypto_operation/cipher.c",
|
||||
]
|
||||
|
||||
framework_signature_files = [
|
||||
"${framework_path}/crypto_operation/signature.c",
|
||||
]
|
||||
|
||||
framework_key_agreement_files = [
|
||||
"${framework_path}/crypto_operation/key_agreement.c",
|
||||
]
|
||||
|
||||
framework_key_files = [
|
||||
"${framework_path}/key/asy_key_generator.c",
|
||||
"${framework_path}/key/sym_key_generator.c",
|
||||
]
|
||||
|
||||
framework_mac_files = [
|
||||
"${framework_path}/crypto_operation/mac.c",
|
||||
]
|
||||
|
||||
framework_rand_files = [
|
||||
"${framework_path}/rand/rand.c",
|
||||
]
|
||||
|
||||
framework_md_files = [
|
||||
"${framework_path}/crypto_operation/md.c",
|
||||
]
|
||||
|
||||
framework_files = framework_certificate_files + framework_key_agreement_files +
|
||||
framework_signature_files + framework_cipher_files + framework_key_files + framework_mac_files +
|
||||
framework_rand_files + framework_md_files
|
48
frameworks/js/napi/inc/napi_asy_key_generator.h
Normal file
48
frameworks/js/napi/inc/napi_asy_key_generator.h
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_NAPI_ASY_KEY_GENERATOR_H
|
||||
#define HCF_NAPI_ASY_KEY_GENERATOR_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "asy_key_generator.h"
|
||||
#include "log.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
class NapiAsyKeyGenerator {
|
||||
public:
|
||||
NapiAsyKeyGenerator(HcfAsyKeyGenerator *generator);
|
||||
~NapiAsyKeyGenerator();
|
||||
|
||||
HcfAsyKeyGenerator *GetAsyKeyGenerator();
|
||||
|
||||
static void DefineAsyKeyGeneratorJSClass(napi_env env, napi_value exports);
|
||||
static napi_value AsyKeyGeneratorConstructor(napi_env env, napi_callback_info info);
|
||||
static napi_value CreateJsAsyKeyGenerator(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value JsGenerateKeyPair(napi_env env, napi_callback_info info);
|
||||
static napi_value JsConvertKey(napi_env env, napi_callback_info info);
|
||||
|
||||
static thread_local napi_ref classRef_;
|
||||
|
||||
private:
|
||||
HcfAsyKeyGenerator *generator_ = nullptr;
|
||||
};
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
#endif
|
52
frameworks/js/napi/inc/napi_cert_chain_validator.h
Normal file
52
frameworks/js/napi/inc/napi_cert_chain_validator.h
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef NAPI_CERT_CHAIN_VALIDATOR_H
|
||||
#define NAPI_CERT_CHAIN_VALIDATOR_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_common.h"
|
||||
#include "cert_chain_validator.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
class NapiCertChainValidator {
|
||||
public:
|
||||
NapiCertChainValidator(HcfCertChainValidator *certChainValidator);
|
||||
~NapiCertChainValidator();
|
||||
|
||||
static void DefineCertChainValidatorJSClass(napi_env env, napi_value exports);
|
||||
static napi_value CreateCertChainValidator(napi_env env, napi_callback_info info);
|
||||
|
||||
napi_value Validate(napi_env env, napi_callback_info info);
|
||||
|
||||
HcfCertChainValidator *GetCertChainValidator()
|
||||
{
|
||||
return certChainValidator_;
|
||||
}
|
||||
|
||||
static napi_ref classRef_;
|
||||
|
||||
private:
|
||||
HcfCertChainValidator *certChainValidator_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
|
||||
#endif // NAPI_CERT_CHAIN_VALIDATOR_H
|
51
frameworks/js/napi/inc/napi_cipher.h
Normal file
51
frameworks/js/napi/inc/napi_cipher.h
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef NAPI_CIPHER_H
|
||||
#define NAPI_CIPHER_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_common.h"
|
||||
#include "napi/native_node_api.h"
|
||||
#include "cipher.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
class NapiCipher {
|
||||
public:
|
||||
NapiCipher(HcfCipher *cipher);
|
||||
~NapiCipher();
|
||||
|
||||
static void DefineCipherJSClass(napi_env env, napi_value exports);
|
||||
static napi_value CreateCipher(napi_env env, napi_callback_info info);
|
||||
static napi_value CipherConstructor(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value JsCipherInit(napi_env env, napi_callback_info info);
|
||||
static napi_value JsCipherUpdate(napi_env env, napi_callback_info info);
|
||||
static napi_value JsCipherDoFinal(napi_env env, napi_callback_info info);
|
||||
static napi_value JsGetAlgorithm(napi_env env, napi_callback_info info);
|
||||
HcfCipher *GetCipher();
|
||||
|
||||
static thread_local napi_ref classRef_;
|
||||
private:
|
||||
HcfCipher *cipher_;
|
||||
};
|
||||
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
#endif
|
73
frameworks/js/napi/inc/napi_crypto_framework_defines.h
Normal file
73
frameworks/js/napi/inc/napi_crypto_framework_defines.h
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef NAPI_CRYPTO_FRAMEWORK_DEFINES_H
|
||||
#define NAPI_CRYPTO_FRAMEWORK_DEFINES_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
constexpr size_t CALLBACK_SIZE = 1;
|
||||
constexpr size_t ARGS_SIZE_ZERO = 0;
|
||||
constexpr size_t ARGS_SIZE_ONE = 1;
|
||||
constexpr size_t ARGS_SIZE_TWO = 2;
|
||||
constexpr size_t ARGS_SIZE_THREE = 3;
|
||||
constexpr size_t ARGS_SIZE_FOUR = 4;
|
||||
constexpr size_t GCM_AUTH_TAG_LEN = 16;
|
||||
constexpr size_t CCM_AUTH_TAG_LEN = 12;
|
||||
constexpr int32_t PARAM0 = 0;
|
||||
constexpr int32_t PARAM1 = 1;
|
||||
constexpr int32_t PARAM2 = 2;
|
||||
constexpr uint32_t JS_ERR_DEFAULT_ERR = 0;
|
||||
constexpr uint32_t JS_ERR_INVALID_PARAMS = 401;
|
||||
constexpr uint32_t JS_ERR_NOT_SUPPORT = 801;
|
||||
constexpr uint32_t JS_ERR_OUT_OF_MEMORY = 17620001;
|
||||
constexpr uint32_t JS_ERR_INTERNAL_ERROR = 17620002;
|
||||
constexpr uint32_t JS_ERR_CRYPTO_OPERATION = 17630001;
|
||||
constexpr uint32_t JS_ERR_CERT_SIGNATURE_FAILURE = 17630002;
|
||||
constexpr uint32_t JS_ERR_CERT_NOT_YET_VALID = 17630003;
|
||||
constexpr uint32_t JS_ERR_CERT_HAS_EXPIRED = 17630004;
|
||||
constexpr uint32_t JS_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY = 17630005;
|
||||
constexpr uint32_t JS_ERR_KEYUSAGE_NO_CERTSIGN = 17630006;
|
||||
constexpr uint32_t JS_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE = 17630007;
|
||||
const std::string CRYPTO_TAG_DATA = "data";
|
||||
const std::string CRYPTO_TAG_ERR_CODE = "code";
|
||||
const std::string CRYPTO_TAG_ERR_MSG = "message";
|
||||
const std::string CRYPTO_TAG_COUNT = "count";
|
||||
const std::string CRYPTO_TAG_ENCODING_FORMAT = "encodingFormat";
|
||||
const std::string CRYPTO_TAG_ALGORITHM = "algorithm";
|
||||
const std::string CRYPTO_TAG_ALG_NAME = "algName";
|
||||
const std::string CRYPTO_TAG_FORMAT = "format";
|
||||
const std::string CRYPTO_TAG_PUB_KEY = "pubKey";
|
||||
const std::string CRYPTO_TAG_PRI_KEY = "priKey";
|
||||
const std::string IV_PARAMS = "iv";
|
||||
const std::string AAD_PARAMS = "aad";
|
||||
const std::string AUTHTAG_PARAMS = "authTag";
|
||||
const std::string ALGO_PARAMS = "algoName";
|
||||
const std::string IV_PARAMS_SPEC = "IvParamsSpec";
|
||||
const std::string GCM_PARAMS_SPEC = "GcmParamsSpec";
|
||||
const std::string CCM_PARAMS_SPEC = "CcmParamsSpec";
|
||||
const std::string COMMON_ERR_MSG = "An exception occurs.";
|
||||
|
||||
enum CfAsyncType {
|
||||
ASYNC_TYPE_CALLBACK = 1,
|
||||
ASYNC_TYPE_PROMISE = 2
|
||||
};
|
||||
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
|
||||
#endif // NAPI_CRYPTO_FRAMEWORK_DEFINES_H
|
47
frameworks/js/napi/inc/napi_key.h
Normal file
47
frameworks/js/napi/inc/napi_key.h
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_NAPI_HCF_KEY_H
|
||||
#define HCF_NAPI_HCF_KEY_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "log.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
#include "key.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
class NapiKey {
|
||||
public:
|
||||
NapiKey(HcfKey *symKey);
|
||||
~NapiKey();
|
||||
HcfKey *GetHcfKey();
|
||||
|
||||
static void DefineHcfKeyJSClass(napi_env env);
|
||||
static napi_value CreateHcfKey(napi_env env);
|
||||
static napi_value KeyConstructor(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value JsGetAlgorithm(napi_env env, napi_callback_info info);
|
||||
static napi_value JsGetEncoded(napi_env env, napi_callback_info info);
|
||||
static napi_value JsGetFormat(napi_env env, napi_callback_info info);
|
||||
|
||||
static thread_local napi_ref classRef_;
|
||||
private:
|
||||
HcfKey *hcfKey_;
|
||||
};
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
#endif
|
47
frameworks/js/napi/inc/napi_key_agreement.h
Normal file
47
frameworks/js/napi/inc/napi_key_agreement.h
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_NAPI_KEY_AGREEMENT_H
|
||||
#define HCF_NAPI_KEY_AGREEMENT_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "log.h"
|
||||
#include "key_agreement.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
class NapiKeyAgreement {
|
||||
public:
|
||||
NapiKeyAgreement(HcfKeyAgreement *keyAgreement);
|
||||
~NapiKeyAgreement();
|
||||
|
||||
HcfKeyAgreement *GetKeyAgreement();
|
||||
|
||||
static void DefineKeyAgreementJSClass(napi_env env, napi_value exports);
|
||||
static napi_value KeyAgreementConstructor(napi_env env, napi_callback_info info);
|
||||
static napi_value CreateJsKeyAgreement(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value JsGenerateSecret(napi_env env, napi_callback_info info);
|
||||
|
||||
static thread_local napi_ref classRef_;
|
||||
|
||||
private:
|
||||
HcfKeyAgreement *keyAgreement_ = nullptr;
|
||||
};
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
#endif
|
44
frameworks/js/napi/inc/napi_key_pair.h
Normal file
44
frameworks/js/napi/inc/napi_key_pair.h
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_NAPI_KEY_PAIR_H
|
||||
#define HCF_NAPI_KEY_PAIR_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "log.h"
|
||||
#include "key_pair.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
class NapiKeyPair {
|
||||
public:
|
||||
NapiKeyPair(HcfKeyPair *keyPair);
|
||||
~NapiKeyPair();
|
||||
|
||||
napi_value ConvertToJsKeyPair(napi_env env);
|
||||
|
||||
static void DefineKeyPairJSClass(napi_env env);
|
||||
static napi_value KeyPairConstructor(napi_env env, napi_callback_info info);
|
||||
|
||||
static thread_local napi_ref classRef_;
|
||||
|
||||
private:
|
||||
HcfKeyPair *keyPair_ = nullptr;
|
||||
};
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
#endif
|
57
frameworks/js/napi/inc/napi_mac.h
Normal file
57
frameworks/js/napi/inc/napi_mac.h
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef NAPI_MAC_H
|
||||
#define NAPI_MAC_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_common.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
#include "mac.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
class NapiMac {
|
||||
public:
|
||||
NapiMac(HcfMac *macObj);
|
||||
~NapiMac();
|
||||
static napi_ref classRef_;
|
||||
|
||||
static void DefineMacJSClass(napi_env env, napi_value exports);
|
||||
static napi_value CreateMac(napi_env env, napi_callback_info info);
|
||||
static napi_value MacConstructor(napi_env env, napi_callback_info info);
|
||||
|
||||
napi_value MacInit(napi_env env, napi_callback_info info);
|
||||
napi_value MacUpdate(napi_env env, napi_callback_info info);
|
||||
napi_value MacDoFinal(napi_env env, napi_callback_info info);
|
||||
napi_value GetMacLength(napi_env env, napi_callback_info info);
|
||||
|
||||
HcfMac *GetMac()
|
||||
{
|
||||
return macObj_;
|
||||
}
|
||||
|
||||
private:
|
||||
HcfMac *macObj_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
|
||||
#endif // NAPI_X509_CERTIFICATE_H
|
56
frameworks/js/napi/inc/napi_md.h
Normal file
56
frameworks/js/napi/inc/napi_md.h
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef NAPI_MD_H
|
||||
#define NAPI_MD_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_common.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
#include "md.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
class NapiMd {
|
||||
public:
|
||||
NapiMd(HcfMd *mdObj);
|
||||
~NapiMd();
|
||||
static napi_ref classRef_;
|
||||
|
||||
static void DefineMdJSClass(napi_env env, napi_value exports);
|
||||
static napi_value CreateMd(napi_env env, napi_callback_info info);
|
||||
static napi_value MdConstructor(napi_env env, napi_callback_info info);
|
||||
|
||||
napi_value MdUpdate(napi_env env, napi_callback_info info);
|
||||
napi_value MdDoFinal(napi_env env, napi_callback_info info);
|
||||
napi_value GetMdLength(napi_env env, napi_callback_info info);
|
||||
|
||||
HcfMd *GetMd()
|
||||
{
|
||||
return mdObj_;
|
||||
}
|
||||
|
||||
private:
|
||||
HcfMd *mdObj_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
|
||||
#endif // NAPI_X509_CERTIFICATE_H
|
48
frameworks/js/napi/inc/napi_pri_key.h
Normal file
48
frameworks/js/napi/inc/napi_pri_key.h
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_NAPI_PRI_KEY_H
|
||||
#define HCF_NAPI_PRI_KEY_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "log.h"
|
||||
#include "pri_key.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
class NapiPriKey {
|
||||
public:
|
||||
NapiPriKey(HcfPriKey *priKey);
|
||||
~NapiPriKey();
|
||||
|
||||
HcfPriKey *GetPriKey();
|
||||
napi_value ConvertToJsPriKey(napi_env env);
|
||||
|
||||
static void DefinePriKeyJSClass(napi_env env);
|
||||
static napi_value PriKeyConstructor(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value JsGetEncoded(napi_env env, napi_callback_info info);
|
||||
static napi_value JsClearMem(napi_env env, napi_callback_info info);
|
||||
|
||||
static thread_local napi_ref classRef_;
|
||||
|
||||
private:
|
||||
HcfPriKey *priKey_ = nullptr;
|
||||
};
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
#endif
|
47
frameworks/js/napi/inc/napi_pub_key.h
Normal file
47
frameworks/js/napi/inc/napi_pub_key.h
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_NAPI_PUB_KEY_H
|
||||
#define HCF_NAPI_PUB_KEY_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "log.h"
|
||||
#include "pub_key.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
class NapiPubKey {
|
||||
public:
|
||||
NapiPubKey(HcfPubKey *pubKey);
|
||||
~NapiPubKey();
|
||||
|
||||
HcfPubKey *GetPubKey();
|
||||
napi_value ConvertToJsPubKey(napi_env env);
|
||||
|
||||
static void DefinePubKeyJSClass(napi_env env);
|
||||
static napi_value PubKeyConstructor(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value JsGetEncoded(napi_env env, napi_callback_info info);
|
||||
|
||||
static thread_local napi_ref classRef_;
|
||||
|
||||
private:
|
||||
HcfPubKey *pubKey_ = nullptr;
|
||||
};
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
#endif
|
54
frameworks/js/napi/inc/napi_rand.h
Normal file
54
frameworks/js/napi/inc/napi_rand.h
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef NAPI_RAND_H
|
||||
#define NAPI_RAND_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_common.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
#include "rand.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
class NapiRand {
|
||||
public:
|
||||
NapiRand(HcfRand *randObj);
|
||||
~NapiRand();
|
||||
static napi_ref classRef_;
|
||||
|
||||
static void DefineRandJSClass(napi_env env, napi_value exports);
|
||||
static napi_value CreateRand(napi_env env, napi_callback_info info);
|
||||
static napi_value RandConstructor(napi_env env, napi_callback_info info);
|
||||
|
||||
napi_value GenerateRandom(napi_env env, napi_callback_info info);
|
||||
napi_value SetSeed(napi_env env, napi_callback_info info);
|
||||
|
||||
HcfRand *GetRand()
|
||||
{
|
||||
return randObj_;
|
||||
}
|
||||
|
||||
private:
|
||||
HcfRand *randObj_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
#endif
|
49
frameworks/js/napi/inc/napi_sign.h
Normal file
49
frameworks/js/napi/inc/napi_sign.h
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_NAPI_SIGNATURE_H
|
||||
#define HCF_NAPI_SIGNATURE_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "log.h"
|
||||
#include "signature.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
class NapiSign {
|
||||
public:
|
||||
NapiSign(HcfSign *sign);
|
||||
~NapiSign();
|
||||
|
||||
HcfSign *GetSign();
|
||||
|
||||
static void DefineSignJSClass(napi_env env, napi_value exports);
|
||||
static napi_value SignConstructor(napi_env env, napi_callback_info info);
|
||||
static napi_value CreateJsSign(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value JsInit(napi_env env, napi_callback_info info);
|
||||
static napi_value JsUpdate(napi_env env, napi_callback_info info);
|
||||
static napi_value JsSign(napi_env env, napi_callback_info info);
|
||||
|
||||
static thread_local napi_ref classRef_;
|
||||
|
||||
private:
|
||||
HcfSign *sign_ = nullptr;
|
||||
};
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
#endif
|
47
frameworks/js/napi/inc/napi_sym_key.h
Normal file
47
frameworks/js/napi/inc/napi_sym_key.h
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_NAPI_SYM_KEY_H
|
||||
#define HCF_NAPI_SYM_KEY_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "log.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
#include "sym_key.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
class NapiSymKey {
|
||||
public:
|
||||
NapiSymKey(HcfSymKey *symKey);
|
||||
~NapiSymKey();
|
||||
HcfSymKey *GetSymKey();
|
||||
|
||||
static void DefineSymKeyJSClass(napi_env env);
|
||||
static napi_value CreateSymKey(napi_env env);
|
||||
static napi_value SymKeyConstructor(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value JsGetAlgorithm(napi_env env, napi_callback_info info);
|
||||
static napi_value JsGetEncoded(napi_env env, napi_callback_info info);
|
||||
static napi_value JsGetFormat(napi_env env, napi_callback_info info);
|
||||
|
||||
static thread_local napi_ref classRef_;
|
||||
private:
|
||||
HcfSymKey *symKey_;
|
||||
};
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
#endif
|
46
frameworks/js/napi/inc/napi_sym_key_generator.h
Normal file
46
frameworks/js/napi/inc/napi_sym_key_generator.h
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_NAPI_SYM_KEY_GENERATOR_H
|
||||
#define HCF_NAPI_SYM_KEY_GENERATOR_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "log.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
#include "sym_key_generator.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
class NapiSymKeyGenerator {
|
||||
public:
|
||||
NapiSymKeyGenerator(HcfSymKeyGenerator *generator);
|
||||
~NapiSymKeyGenerator();
|
||||
HcfSymKeyGenerator *GetSymKeyGenerator();
|
||||
|
||||
static void DefineSymKeyGeneratorJSClass(napi_env env, napi_value exports);
|
||||
static napi_value CreateSymKeyGenerator(napi_env env, napi_callback_info info);
|
||||
static napi_value SymKeyGeneratorConstructor(napi_env env, napi_callback_info info);
|
||||
static napi_value JsGenerateSymKey(napi_env env, napi_callback_info info);
|
||||
static napi_value JsGetAlgorithm(napi_env env, napi_callback_info info);
|
||||
static napi_value JsConvertKey(napi_env env, napi_callback_info info);
|
||||
|
||||
static thread_local napi_ref classRef_;
|
||||
private:
|
||||
HcfSymKeyGenerator *generator_;
|
||||
};
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
#endif
|
69
frameworks/js/napi/inc/napi_utils.h
Normal file
69
frameworks/js/napi/inc/napi_utils.h
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_NAPI_UILTS_H
|
||||
#define HCF_NAPI_UILTS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
#include "blob.h"
|
||||
#include "cert_chain_validator.h"
|
||||
#include "algorithm_parameter.h"
|
||||
#include "cipher.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
#define PARAMS_NUM_ONE 1
|
||||
#define PARAMS_NUM_TWO 2
|
||||
#define PARAMS_NUM_THREE 3
|
||||
#define PARAMS_NUM_FOUR 4
|
||||
|
||||
enum AsyncType {
|
||||
ASYNC_CALLBACK = 1,
|
||||
ASYNC_PROMISE = 2
|
||||
};
|
||||
|
||||
inline void AddUint32Property(napi_env env, napi_value object, const char *name, uint32_t value)
|
||||
{
|
||||
napi_value property = nullptr;
|
||||
napi_create_uint32(env, value, &property);
|
||||
napi_set_named_property(env, object, name, property);
|
||||
}
|
||||
|
||||
HcfBlob *GetBlobFromNapiValue(napi_env env, napi_value arg);
|
||||
bool GetParamsSpecFormNapiValue(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec);
|
||||
napi_value ConvertBlobToNapiValue(napi_env env, HcfBlob *blob);
|
||||
|
||||
bool GetStringFromJSParams(napi_env env, napi_value arg, std::string &returnStr);
|
||||
bool GetInt32FromJSParams(napi_env env, napi_value arg, int32_t &returnInt);
|
||||
bool GetUint32FromJSParams(napi_env env, napi_value arg, uint32_t &returnInt);
|
||||
bool GetCallbackFromJSParams(napi_env env, napi_value arg, napi_ref *returnCb);
|
||||
bool ParseArrayBuffer(napi_env env, napi_value args, uint8_t **data, size_t &size);
|
||||
bool GetEncodingBlobFromValue(napi_env env, napi_value object, HcfEncodingBlob **encodingBlob);
|
||||
bool GetCertChainFromValue(napi_env env, napi_value object, HcfCertChainData **certChainData);
|
||||
bool CheckArgsCount(napi_env env, size_t argc, size_t expectedCount, bool isSync);
|
||||
napi_value GetResourceName(napi_env env, const char *name);
|
||||
napi_value GenerateArrayBuffer(napi_env env, uint8_t *data, uint32_t size);
|
||||
napi_value NapiGetNull(napi_env env);
|
||||
napi_value ConvertArrayToNapiValue(napi_env env, HcfArray *array);
|
||||
napi_value ConvertEncodingBlobToNapiValue(napi_env env, HcfEncodingBlob *encodingBlob);
|
||||
napi_value GenerateBusinessError(napi_env env, int32_t errCode, const char *errMsg);
|
||||
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
#endif
|
49
frameworks/js/napi/inc/napi_verify.h
Normal file
49
frameworks/js/napi/inc/napi_verify.h
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_NAPI_VERIFY_H
|
||||
#define HCF_NAPI_VERIFY_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "log.h"
|
||||
#include "signature.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
class NapiVerify {
|
||||
public:
|
||||
NapiVerify(HcfVerify *verify);
|
||||
~NapiVerify();
|
||||
|
||||
HcfVerify *GetVerify();
|
||||
|
||||
static void DefineVerifyJSClass(napi_env env, napi_value exports);
|
||||
static napi_value VerifyConstructor(napi_env env, napi_callback_info info);
|
||||
static napi_value CreateJsVerify(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value JsInit(napi_env env, napi_callback_info info);
|
||||
static napi_value JsUpdate(napi_env env, napi_callback_info info);
|
||||
static napi_value JsVerify(napi_env env, napi_callback_info info);
|
||||
|
||||
static thread_local napi_ref classRef_;
|
||||
|
||||
private:
|
||||
HcfVerify *verify_ = nullptr;
|
||||
};
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
#endif
|
75
frameworks/js/napi/inc/napi_x509_certificate.h
Normal file
75
frameworks/js/napi/inc/napi_x509_certificate.h
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef NAPI_X509_CERTIFICATE_H
|
||||
#define NAPI_X509_CERTIFICATE_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_common.h"
|
||||
#include "x509_certificate.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
class NapiX509Cert {
|
||||
public:
|
||||
NapiX509Cert(HcfX509Certificate *x509Cert);
|
||||
~NapiX509Cert();
|
||||
|
||||
static void DefineX509CertJSClass(napi_env env, napi_value exports);
|
||||
static napi_value NapiCreateX509Cert(napi_env env, napi_callback_info info);
|
||||
static void CreateX509CertExecute(napi_env env, void *data);
|
||||
static void CreateX509CertComplete(napi_env env, napi_status status, void *data);
|
||||
static napi_value CreateX509Cert(napi_env env);
|
||||
|
||||
napi_value Verify(napi_env env, napi_callback_info info);
|
||||
napi_value GetEncoded(napi_env env, napi_callback_info info);
|
||||
napi_value GetPublicKey(napi_env env, napi_callback_info info);
|
||||
napi_value CheckValidityWithDate(napi_env env, napi_callback_info info);
|
||||
napi_value GetVersion(napi_env env, napi_callback_info info);
|
||||
napi_value GetSerialNumber(napi_env env, napi_callback_info info);
|
||||
napi_value GetIssuerName(napi_env env, napi_callback_info info);
|
||||
napi_value GetSubjectName(napi_env env, napi_callback_info info);
|
||||
napi_value GetNotBeforeTime(napi_env env, napi_callback_info info);
|
||||
napi_value GetNotAfterTime(napi_env env, napi_callback_info info);
|
||||
napi_value GetSignature(napi_env env, napi_callback_info info);
|
||||
napi_value GetSigAlgName(napi_env env, napi_callback_info info);
|
||||
napi_value GetSigAlgOID(napi_env env, napi_callback_info info);
|
||||
napi_value GetSigAlgParams(napi_env env, napi_callback_info info);
|
||||
napi_value GetIssuerUniqueID(napi_env env, napi_callback_info info);
|
||||
napi_value GetSubjectUniqueID(napi_env env, napi_callback_info info);
|
||||
napi_value GetKeyUsage(napi_env env, napi_callback_info info);
|
||||
napi_value GetExtendedKeyUsage(napi_env env, napi_callback_info info);
|
||||
napi_value GetBasicConstraints(napi_env env, napi_callback_info info);
|
||||
napi_value GetSubjectAlternativeNames(napi_env env, napi_callback_info info);
|
||||
napi_value GetIssuerAlternativeNames(napi_env env, napi_callback_info info);
|
||||
|
||||
HcfX509Certificate *GetX509Cert()
|
||||
{
|
||||
return x509Cert_;
|
||||
}
|
||||
|
||||
static napi_ref classRef_;
|
||||
|
||||
private:
|
||||
HcfX509Certificate *x509Cert_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
|
||||
#endif // NAPI_X509_CERTIFICATE_H
|
70
frameworks/js/napi/inc/napi_x509_crl.h
Normal file
70
frameworks/js/napi/inc/napi_x509_crl.h
Normal file
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef NAPI_X509_CRL_H
|
||||
#define NAPI_X509_CRL_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_common.h"
|
||||
#include "x509_crl.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
class NapiX509Crl {
|
||||
public:
|
||||
NapiX509Crl(HcfX509Crl *x509Crl);
|
||||
~NapiX509Crl();
|
||||
|
||||
static void DefineX509CrlJSClass(napi_env env, napi_value exports);
|
||||
static napi_value NapiCreateX509Crl(napi_env env, napi_callback_info info);
|
||||
static void CreateX509CrlExecute(napi_env env, void *data);
|
||||
static void CreateX509CrlComplete(napi_env env, napi_status status, void *data);
|
||||
static napi_value CreateX509Crl(napi_env env);
|
||||
|
||||
napi_value IsRevoked(napi_env env, napi_callback_info info);
|
||||
napi_value GetType(napi_env env, napi_callback_info info);
|
||||
napi_value GetEncoded(napi_env env, napi_callback_info info);
|
||||
napi_value Verify(napi_env env, napi_callback_info info);
|
||||
napi_value GetVersion(napi_env env, napi_callback_info info);
|
||||
napi_value GetIssuerDN(napi_env env, napi_callback_info info);
|
||||
napi_value GetThisUpdate(napi_env env, napi_callback_info info);
|
||||
napi_value GetNextUpdate(napi_env env, napi_callback_info info);
|
||||
napi_value GetRevokedCertificate(napi_env env, napi_callback_info info);
|
||||
napi_value GetRevokedCertificateWithCert(napi_env env, napi_callback_info info);
|
||||
napi_value GetRevokedCertificates(napi_env env, napi_callback_info info);
|
||||
napi_value GetTBSCertList(napi_env env, napi_callback_info info);
|
||||
napi_value GetSignature(napi_env env, napi_callback_info info);
|
||||
napi_value GetSigAlgName(napi_env env, napi_callback_info info);
|
||||
napi_value GetSigAlgOID(napi_env env, napi_callback_info info);
|
||||
napi_value GetSigAlgParams(napi_env env, napi_callback_info info);
|
||||
|
||||
HcfX509Crl *GetX509Crl()
|
||||
{
|
||||
return x509Crl_;
|
||||
}
|
||||
|
||||
static napi_ref classRef_;
|
||||
|
||||
private:
|
||||
HcfX509Crl *x509Crl_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
|
||||
#endif // NAPI_X509_CRL_H
|
56
frameworks/js/napi/inc/napi_x509_crl_entry.h
Normal file
56
frameworks/js/napi/inc/napi_x509_crl_entry.h
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef NAPI_X509_CRL_ENTRY_H
|
||||
#define NAPI_X509_CRL_ENTRY_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_common.h"
|
||||
#include "x509_crl_entry.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
class NapiX509CrlEntry {
|
||||
public:
|
||||
NapiX509CrlEntry(HcfX509CrlEntry *x509CrlEntry);
|
||||
~NapiX509CrlEntry();
|
||||
|
||||
static void DefineX509CrlEntryJSClass(napi_env env);
|
||||
static napi_value CreateX509CrlEntry(napi_env env);
|
||||
|
||||
napi_value GetEncoded(napi_env env, napi_callback_info info);
|
||||
napi_value GetSerialNumber(napi_env env, napi_callback_info info);
|
||||
napi_value GetCertificateIssuer(napi_env env, napi_callback_info info);
|
||||
napi_value GetRevocationDate(napi_env env, napi_callback_info info);
|
||||
napi_value GetExtensionValue(napi_env env, napi_callback_info info);
|
||||
|
||||
HcfX509CrlEntry *GetX509CrlEntry()
|
||||
{
|
||||
return x509CrlEntry_;
|
||||
}
|
||||
|
||||
static napi_ref classRef_;
|
||||
|
||||
private:
|
||||
HcfX509CrlEntry *x509CrlEntry_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
||||
|
||||
#endif // NAPI_X509_CRL_ENTRY_H
|
535
frameworks/js/napi/src/napi_asy_key_generator.cpp
Normal file
535
frameworks/js/napi/src/napi_asy_key_generator.cpp
Normal file
@ -0,0 +1,535 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_asy_key_generator.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
#include "napi_utils.h"
|
||||
#include "napi_key_pair.h"
|
||||
#include "napi_pri_key.h"
|
||||
#include "napi_pub_key.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
struct GenKeyPairCtx {
|
||||
napi_env env = nullptr;
|
||||
|
||||
AsyncType asyncType = ASYNC_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_value promise = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
HcfAsyKeyGenerator *generator;
|
||||
HcfParamsSpec *params;
|
||||
|
||||
HcfResult result;
|
||||
HcfKeyPair *returnKeyPair;
|
||||
};
|
||||
|
||||
struct ConvertKeyCtx {
|
||||
napi_env env = nullptr;
|
||||
|
||||
AsyncType asyncType = ASYNC_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_value promise = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
HcfAsyKeyGenerator *generator;
|
||||
HcfParamsSpec *params;
|
||||
HcfBlob *pubKey;
|
||||
HcfBlob *priKey;
|
||||
|
||||
HcfResult result;
|
||||
HcfKeyPair *returnKeyPair;
|
||||
};
|
||||
|
||||
thread_local napi_ref NapiAsyKeyGenerator::classRef_ = nullptr;
|
||||
|
||||
static void FreeGenKeyPairCtx(napi_env env, GenKeyPairCtx *ctx)
|
||||
{
|
||||
if (ctx == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, ctx->asyncWork);
|
||||
ctx->asyncWork = nullptr;
|
||||
}
|
||||
|
||||
if (ctx->callback != nullptr) {
|
||||
napi_delete_reference(env, ctx->callback);
|
||||
ctx->callback = nullptr;
|
||||
}
|
||||
|
||||
HcfFree(ctx);
|
||||
}
|
||||
|
||||
static void FreeConvertKeyCtx(napi_env env, ConvertKeyCtx *ctx)
|
||||
{
|
||||
if (ctx == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, ctx->asyncWork);
|
||||
ctx->asyncWork = nullptr;
|
||||
}
|
||||
|
||||
if (ctx->callback != nullptr) {
|
||||
napi_delete_reference(env, ctx->callback);
|
||||
ctx->callback = nullptr;
|
||||
}
|
||||
|
||||
HcfBlobDataFree(ctx->pubKey);
|
||||
HcfFree(ctx->pubKey);
|
||||
HcfBlobDataFree(ctx->priKey);
|
||||
HcfFree(ctx->priKey);
|
||||
HcfFree(ctx);
|
||||
}
|
||||
|
||||
static bool BuildGenKeyPairCtx(napi_env env, napi_callback_info info, GenKeyPairCtx *ctx)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t expectedArgc = PARAMS_NUM_ONE;
|
||||
size_t argc;
|
||||
napi_value argv[PARAMS_NUM_ONE] = { nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (argc != expectedArgc && argc != expectedArgc - 1) {
|
||||
LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc);
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error."));
|
||||
return false;
|
||||
}
|
||||
ctx->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
|
||||
|
||||
NapiAsyKeyGenerator *napiGenerator;
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)(&napiGenerator));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi asyKeyGenerator obj.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error."));
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx->generator = napiGenerator->GetAsyKeyGenerator();
|
||||
ctx->params = nullptr;
|
||||
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
napi_create_promise(env, &ctx->deferred, &ctx->promise);
|
||||
return true;
|
||||
} else {
|
||||
return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback);
|
||||
}
|
||||
}
|
||||
|
||||
static bool GetPkAndSkBlobFromNapiValueIfInput(napi_env env, napi_value pkValue, napi_value skValue,
|
||||
HcfBlob **returnPubKey, HcfBlob **returnPriKey)
|
||||
{
|
||||
napi_valuetype valueType;
|
||||
napi_typeof(env, pkValue, &valueType);
|
||||
HcfBlob *pubKey = nullptr;
|
||||
if (valueType != napi_null) {
|
||||
pubKey = GetBlobFromNapiValue(env, pkValue);
|
||||
if (pubKey == nullptr) {
|
||||
LOGE("failed to get pubKey.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PubKey]: must be of the DataBlob type."));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
napi_typeof(env, skValue, &valueType);
|
||||
HcfBlob *priKey = nullptr;
|
||||
if (valueType != napi_null) {
|
||||
priKey = GetBlobFromNapiValue(env, skValue);
|
||||
if (priKey == nullptr) {
|
||||
LOGE("failed to get priKey.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PriKey]: must be of the DataBlob type."));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
*returnPubKey = pubKey;
|
||||
*returnPriKey = priKey;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool BuildConvertKeyCtx(napi_env env, napi_callback_info info, ConvertKeyCtx *ctx)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t expectedArgc = PARAMS_NUM_THREE;
|
||||
size_t argc;
|
||||
napi_value argv[PARAMS_NUM_THREE] = { nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (argc != expectedArgc && argc != expectedArgc - 1) {
|
||||
LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc);
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error."));
|
||||
return false;
|
||||
}
|
||||
ctx->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
|
||||
|
||||
NapiAsyKeyGenerator *napiGenerator;
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)(&napiGenerator));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi asyKeyGenerator obj.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error."));
|
||||
return false;
|
||||
}
|
||||
|
||||
HcfBlob *pubKey = nullptr;
|
||||
HcfBlob *priKey = nullptr;
|
||||
if (!GetPkAndSkBlobFromNapiValueIfInput(env, argv[PARAM0], argv[PARAM1], &pubKey, &priKey)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx->generator = napiGenerator->GetAsyKeyGenerator();
|
||||
ctx->params = nullptr;
|
||||
ctx->pubKey = pubKey;
|
||||
ctx->priKey = priKey;
|
||||
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
napi_create_promise(env, &ctx->deferred, &ctx->promise);
|
||||
return true;
|
||||
} else {
|
||||
return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback);
|
||||
}
|
||||
}
|
||||
|
||||
static void ReturnGenKeyPairCallbackResult(napi_env env, GenKeyPairCtx *ctx, napi_value result)
|
||||
{
|
||||
napi_value businessError = nullptr;
|
||||
if (ctx->result != HCF_SUCCESS) {
|
||||
businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str());
|
||||
}
|
||||
|
||||
napi_value params[ARGS_SIZE_TWO] = { businessError, result };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, ctx->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnGenKeyPairPromiseResult(napi_env env, GenKeyPairCtx *ctx, napi_value result)
|
||||
{
|
||||
if (ctx->result == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, ctx->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
static void ReturnConvertKeyCallbackResult(napi_env env, ConvertKeyCtx *ctx, napi_value result)
|
||||
{
|
||||
napi_value businessError = nullptr;
|
||||
if (ctx->result != HCF_SUCCESS) {
|
||||
businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str());
|
||||
}
|
||||
|
||||
napi_value params[ARGS_SIZE_TWO] = { businessError, result };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, ctx->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnConvertKeyPromiseResult(napi_env env, ConvertKeyCtx *ctx, napi_value result)
|
||||
{
|
||||
if (ctx->result == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, ctx->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
static void GenKeyPairAsyncWorkProcess(napi_env env, void *data)
|
||||
{
|
||||
GenKeyPairCtx *ctx = static_cast<GenKeyPairCtx *>(data);
|
||||
|
||||
HcfResult res = ctx->generator->generateKeyPair(ctx->generator, ctx->params, &(ctx->returnKeyPair));
|
||||
|
||||
ctx->result = res;
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("generate key pair fail.");
|
||||
}
|
||||
}
|
||||
|
||||
static void GenKeyPairAsyncWorkReturn(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
GenKeyPairCtx *ctx = static_cast<GenKeyPairCtx *>(data);
|
||||
|
||||
napi_value instance = nullptr;
|
||||
if (ctx->result == HCF_SUCCESS) {
|
||||
NapiKeyPair *napiKeyPair = new NapiKeyPair(ctx->returnKeyPair);
|
||||
instance = napiKeyPair->ConvertToJsKeyPair(env);
|
||||
|
||||
napi_wrap(
|
||||
env, instance, napiKeyPair,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
NapiKeyPair *keyPair = static_cast<NapiKeyPair *>(data);
|
||||
delete keyPair;
|
||||
return;
|
||||
},
|
||||
nullptr, nullptr);
|
||||
}
|
||||
|
||||
if (ctx->asyncType == ASYNC_CALLBACK) {
|
||||
ReturnGenKeyPairCallbackResult(env, ctx, instance);
|
||||
} else {
|
||||
ReturnGenKeyPairPromiseResult(env, ctx, instance);
|
||||
}
|
||||
FreeGenKeyPairCtx(env, ctx);
|
||||
}
|
||||
|
||||
static void ConvertKeyAsyncWorkProcess(napi_env env, void *data)
|
||||
{
|
||||
ConvertKeyCtx *ctx = static_cast<ConvertKeyCtx *>(data);
|
||||
|
||||
HcfResult res = ctx->generator->convertKey(ctx->generator, ctx->params,
|
||||
ctx->pubKey, ctx->priKey, &(ctx->returnKeyPair));
|
||||
|
||||
ctx->result = res;
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("convert key fail.");
|
||||
}
|
||||
}
|
||||
|
||||
static void ConvertKeyAsyncWorkReturn(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
ConvertKeyCtx *ctx = static_cast<ConvertKeyCtx *>(data);
|
||||
|
||||
napi_value instance = nullptr;
|
||||
if (ctx->result == HCF_SUCCESS) {
|
||||
NapiKeyPair *napiKeyPair = new NapiKeyPair(ctx->returnKeyPair);
|
||||
napi_value instance = napiKeyPair->ConvertToJsKeyPair(env);
|
||||
|
||||
napi_wrap(
|
||||
env, instance, napiKeyPair,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
NapiKeyPair *keyPair = static_cast<NapiKeyPair *>(data);
|
||||
delete keyPair;
|
||||
return;
|
||||
},
|
||||
nullptr, nullptr);
|
||||
}
|
||||
|
||||
if (ctx->asyncType == ASYNC_CALLBACK) {
|
||||
ReturnConvertKeyCallbackResult(env, ctx, instance);
|
||||
} else {
|
||||
ReturnConvertKeyPromiseResult(env, ctx, instance);
|
||||
}
|
||||
FreeConvertKeyCtx(env, ctx);
|
||||
}
|
||||
|
||||
static napi_value NewGenKeyPairAsyncWork(napi_env env, GenKeyPairCtx *ctx)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, "generatorKeyPair", NAPI_AUTO_LENGTH, &resourceName);
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, resourceName,
|
||||
[](napi_env env, void *data) {
|
||||
GenKeyPairAsyncWorkProcess(env, data);
|
||||
return;
|
||||
},
|
||||
[](napi_env env, napi_status status, void *data) {
|
||||
GenKeyPairAsyncWorkReturn(env, status, data);
|
||||
return;
|
||||
},
|
||||
(void *)ctx,
|
||||
&ctx->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, ctx->asyncWork);
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
return ctx->promise;
|
||||
} else {
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
static napi_value NewConvertKeyAsyncWork(napi_env env, ConvertKeyCtx *ctx)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, "convertKey", NAPI_AUTO_LENGTH, &resourceName);
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, resourceName,
|
||||
[](napi_env env, void *data) {
|
||||
ConvertKeyAsyncWorkProcess(env, data);
|
||||
return;
|
||||
},
|
||||
[](napi_env env, napi_status status, void *data) {
|
||||
ConvertKeyAsyncWorkReturn(env, status, data);
|
||||
return;
|
||||
},
|
||||
(void *)ctx,
|
||||
&ctx->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, ctx->asyncWork);
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
return ctx->promise;
|
||||
} else {
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
NapiAsyKeyGenerator::NapiAsyKeyGenerator(HcfAsyKeyGenerator *generator)
|
||||
{
|
||||
this->generator_ = generator;
|
||||
}
|
||||
|
||||
NapiAsyKeyGenerator::~NapiAsyKeyGenerator()
|
||||
{
|
||||
OH_HCF_ObjDestroy(this->generator_);
|
||||
}
|
||||
|
||||
HcfAsyKeyGenerator *NapiAsyKeyGenerator::GetAsyKeyGenerator()
|
||||
{
|
||||
return this->generator_;
|
||||
}
|
||||
|
||||
napi_value NapiAsyKeyGenerator::JsGenerateKeyPair(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
GenKeyPairCtx *ctx = (GenKeyPairCtx *)HcfMalloc(sizeof(GenKeyPairCtx), 0);
|
||||
if (ctx == NULL) {
|
||||
LOGE("create context fail.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc ctx fail."));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!BuildGenKeyPairCtx(env, info, ctx)) {
|
||||
LOGE("build context fail.");
|
||||
FreeGenKeyPairCtx(env, ctx);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NewGenKeyPairAsyncWork(env, ctx);
|
||||
}
|
||||
|
||||
napi_value NapiAsyKeyGenerator::JsConvertKey(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
ConvertKeyCtx *ctx = (ConvertKeyCtx *)HcfMalloc(sizeof(ConvertKeyCtx), 0);
|
||||
if (ctx == NULL) {
|
||||
LOGE("create context fail.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!BuildConvertKeyCtx(env, info, ctx)) {
|
||||
LOGE("build context fail.");
|
||||
FreeConvertKeyCtx(env, ctx);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NewConvertKeyAsyncWork(env, ctx);
|
||||
}
|
||||
|
||||
napi_value NapiAsyKeyGenerator::AsyKeyGeneratorConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
|
||||
LOGI("out ...");
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiAsyKeyGenerator::CreateJsAsyKeyGenerator(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
size_t exceptedArgc = PARAMS_NUM_ONE;
|
||||
size_t argc;
|
||||
napi_value argv[PARAMS_NUM_ONE] = { nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
|
||||
if (argc != exceptedArgc) {
|
||||
LOGE("The input args num is invalid.");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
|
||||
napi_value instance;
|
||||
napi_value constructor = nullptr;
|
||||
napi_get_reference_value(env, classRef_, &constructor);
|
||||
napi_new_instance(env, constructor, argc, argv, &instance);
|
||||
|
||||
std::string algName;
|
||||
if (!GetStringFromJSParams(env, argv[0], algName)) {
|
||||
LOGE("failed to get algoName.");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
|
||||
HcfAsyKeyGenerator *generator = NULL;
|
||||
int32_t res = HcfAsyKeyGeneratorCreate(algName.c_str(), &generator);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("create c generator fail.");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
|
||||
NapiAsyKeyGenerator *napiAsyKeyGenerator = new NapiAsyKeyGenerator(generator);
|
||||
|
||||
napi_wrap(
|
||||
env, instance, napiAsyKeyGenerator,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
NapiAsyKeyGenerator *napiAsyKeyGenerator = (NapiAsyKeyGenerator *)(data);
|
||||
delete napiAsyKeyGenerator;
|
||||
return;
|
||||
},
|
||||
nullptr,
|
||||
nullptr);
|
||||
|
||||
napi_value napiAlgName = nullptr;
|
||||
napi_create_string_utf8(env, algName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName);
|
||||
napi_set_named_property(env, instance, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName);
|
||||
|
||||
LOGI("out ...");
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NapiAsyKeyGenerator::DefineAsyKeyGeneratorJSClass(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("createAsyKeyGenerator", NapiAsyKeyGenerator::CreateJsAsyKeyGenerator),
|
||||
};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
|
||||
napi_property_descriptor classDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("generateKeyPair", NapiAsyKeyGenerator::JsGenerateKeyPair),
|
||||
DECLARE_NAPI_FUNCTION("convertKey", NapiAsyKeyGenerator::JsConvertKey),
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "AsyKeyGenerator", NAPI_AUTO_LENGTH, NapiAsyKeyGenerator::AsyKeyGeneratorConstructor,
|
||||
nullptr, sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
|
||||
} // CryptoFramework
|
||||
} // OHOS
|
267
frameworks/js/napi/src/napi_cert_chain_validator.cpp
Normal file
267
frameworks/js/napi/src/napi_cert_chain_validator.cpp
Normal file
@ -0,0 +1,267 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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 "napi_cert_chain_validator.h"
|
||||
|
||||
#include "napi/native_node_api.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
#include "utils.h"
|
||||
#include "result.h"
|
||||
#include "object_base.h"
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
#include "napi_utils.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
napi_ref NapiCertChainValidator::classRef_ = nullptr;
|
||||
|
||||
struct CfCtx {
|
||||
CfAsyncType asyncType = ASYNC_TYPE_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
NapiCertChainValidator *ccvClass = nullptr;
|
||||
HcfCertChainData *certChainData = nullptr;
|
||||
|
||||
int32_t errCode = 0;
|
||||
const char *errMsg = nullptr;
|
||||
};
|
||||
|
||||
NapiCertChainValidator::NapiCertChainValidator(HcfCertChainValidator *certChainValidator)
|
||||
{
|
||||
this->certChainValidator_ = certChainValidator;
|
||||
}
|
||||
|
||||
NapiCertChainValidator::~NapiCertChainValidator()
|
||||
{
|
||||
OH_HCF_ObjDestroy(this->certChainValidator_);
|
||||
}
|
||||
|
||||
static void FreeCryptoFwkCtx(napi_env env, CfCtx *context)
|
||||
{
|
||||
if (context == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (context->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, context->asyncWork);
|
||||
}
|
||||
|
||||
if (context->callback != nullptr) {
|
||||
napi_delete_reference(env, context->callback);
|
||||
}
|
||||
|
||||
if (context->certChainData != nullptr) {
|
||||
HcfFree(context->certChainData->data);
|
||||
context->certChainData->data = nullptr;
|
||||
HcfFree(context->certChainData);
|
||||
context->certChainData = nullptr;
|
||||
}
|
||||
|
||||
HcfFree(context);
|
||||
context = nullptr;
|
||||
}
|
||||
|
||||
static void ReturnCallbackResult(napi_env env, CfCtx *context, napi_value result)
|
||||
{
|
||||
napi_value businessError = nullptr;
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
businessError = GenerateBusinessError(env, context->errCode, context->errMsg);
|
||||
}
|
||||
napi_value params[ARGS_SIZE_TWO] = { businessError, result };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, context->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnPromiseResult(napi_env env, CfCtx *context, napi_value result)
|
||||
{
|
||||
if (context->errCode == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, context->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg));
|
||||
}
|
||||
}
|
||||
|
||||
static void ReturnResult(napi_env env, CfCtx *context, napi_value result)
|
||||
{
|
||||
if (context->asyncType == ASYNC_TYPE_CALLBACK) {
|
||||
ReturnCallbackResult(env, context, result);
|
||||
} else {
|
||||
ReturnPromiseResult(env, context, result);
|
||||
}
|
||||
}
|
||||
|
||||
static void ValidateExecute(napi_env env, void *data)
|
||||
{
|
||||
CfCtx *context = static_cast<CfCtx *>(data);
|
||||
HcfCertChainValidator *validator = context->ccvClass->GetCertChainValidator();
|
||||
context->errCode = validator->validate(validator, context->certChainData);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
LOGE("validate cert chain failed!");
|
||||
context->errMsg = "validate cert chain failed";
|
||||
}
|
||||
}
|
||||
|
||||
static void ValidateComplete(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
CfCtx *context = static_cast<CfCtx *>(data);
|
||||
ReturnResult(env, context, NapiGetNull(env));
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
}
|
||||
|
||||
napi_value NapiCertChainValidator::Validate(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argc = ARGS_SIZE_TWO;
|
||||
napi_value argv[ARGS_SIZE_TWO] = { 0 };
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) {
|
||||
return nullptr;
|
||||
}
|
||||
CfCtx *context = (CfCtx *)HcfMalloc(sizeof(CfCtx), 0);
|
||||
if (context == nullptr) {
|
||||
LOGE("malloc context failed!");
|
||||
return nullptr;
|
||||
}
|
||||
context->ccvClass = this;
|
||||
|
||||
context->asyncType = (argc == ARGS_SIZE_TWO) ? ASYNC_TYPE_CALLBACK : ASYNC_TYPE_PROMISE;
|
||||
if (!GetCertChainFromValue(env, argv[PARAM0], &context->certChainData)) {
|
||||
LOGE("get cert chain data from napi value failed!");
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
napi_value promise = nullptr;
|
||||
if (context->asyncType == ASYNC_TYPE_CALLBACK) {
|
||||
if (!GetCallbackFromJSParams(env, argv[PARAM1], &context->callback)) {
|
||||
LOGE("get callback failed!");
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
} else {
|
||||
napi_create_promise(env, &context->deferred, &promise);
|
||||
}
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, GetResourceName(env, "Validate"),
|
||||
ValidateExecute,
|
||||
ValidateComplete,
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_TYPE_PROMISE) {
|
||||
return promise;
|
||||
} else {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
}
|
||||
|
||||
static napi_value NapiValidate(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("start to validate cert chain.");
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiCertChainValidator *certChainValidator = nullptr;
|
||||
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&certChainValidator));
|
||||
if (certChainValidator == nullptr) {
|
||||
LOGE("certChainValidator is nullptr!");
|
||||
return nullptr;
|
||||
}
|
||||
return certChainValidator->Validate(env, info);
|
||||
}
|
||||
|
||||
static napi_value CertChainValidatorConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiCertChainValidator::CreateCertChainValidator(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("start to create cert chain validator.");
|
||||
napi_value thisVar = nullptr;
|
||||
size_t argc = ARGS_SIZE_ONE;
|
||||
napi_value argv[ARGS_SIZE_ONE] = { 0 };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
|
||||
if (argc != ARGS_SIZE_ONE) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count"));
|
||||
LOGE("invalid params count!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string algorithm;
|
||||
if (!GetStringFromJSParams(env, argv[PARAM0], algorithm)) {
|
||||
LOGE("Failed to get algorithm.");
|
||||
return nullptr;
|
||||
}
|
||||
HcfCertChainValidator *certChainValidator = nullptr;
|
||||
HcfResult res = HcfCertChainValidatorCreate(algorithm.c_str(), &certChainValidator);
|
||||
if (res != HCF_SUCCESS) {
|
||||
napi_throw(env, GenerateBusinessError(env, res, "create cert chain validator failed"));
|
||||
LOGE("Failed to create c cert chain validator.");
|
||||
return nullptr;
|
||||
}
|
||||
const char *returnAlgorithm = certChainValidator->getAlgorithm(certChainValidator);
|
||||
napi_value algValue = nullptr;
|
||||
napi_create_string_utf8(env, returnAlgorithm, NAPI_AUTO_LENGTH, &algValue);
|
||||
napi_value constructor = nullptr;
|
||||
napi_value validatorInstance = nullptr;
|
||||
napi_get_reference_value(env, classRef_, &constructor);
|
||||
napi_new_instance(env, constructor, 0, nullptr, &validatorInstance);
|
||||
napi_set_named_property(env, validatorInstance, CRYPTO_TAG_ALGORITHM.c_str(), algValue);
|
||||
NapiCertChainValidator *ccvClass = new NapiCertChainValidator(certChainValidator);
|
||||
napi_wrap(
|
||||
env, validatorInstance, ccvClass,
|
||||
[](napi_env env, void* data, void *hint) {
|
||||
NapiCertChainValidator *ccv = (NapiCertChainValidator *)data;
|
||||
delete ccv;
|
||||
},
|
||||
nullptr,
|
||||
nullptr);
|
||||
|
||||
return validatorInstance;
|
||||
}
|
||||
|
||||
void NapiCertChainValidator::DefineCertChainValidatorJSClass(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("createCertChainValidator", CreateCertChainValidator),
|
||||
};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
|
||||
napi_property_descriptor validatorDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("validate", NapiValidate),
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "CertChainValidator", NAPI_AUTO_LENGTH, CertChainValidatorConstructor, nullptr,
|
||||
sizeof(validatorDesc) / sizeof(validatorDesc[0]), validatorDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
672
frameworks/js/napi/src/napi_cipher.cpp
Normal file
672
frameworks/js/napi/src/napi_cipher.cpp
Normal file
@ -0,0 +1,672 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_cipher.h"
|
||||
#include "napi_key.h"
|
||||
#include "securec.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
|
||||
#include "cipher.h"
|
||||
#include "napi_utils.h"
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
#include "detailed_iv_params.h"
|
||||
#include "detailed_gcm_params.h"
|
||||
#include "detailed_ccm_params.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
thread_local napi_ref NapiCipher::classRef_ = nullptr;
|
||||
|
||||
struct CipherFwkCtxT {
|
||||
napi_env env = nullptr;
|
||||
|
||||
AsyncType asyncType = ASYNC_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_value promise = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
HcfCipher *cipher = nullptr;
|
||||
HcfKey *key = nullptr;
|
||||
HcfParamsSpec *paramsSpec = nullptr;
|
||||
|
||||
HcfBlob input = { 0 };
|
||||
HcfBlob output = { 0 };
|
||||
enum HcfCryptoMode opMode = ENCRYPT_MODE;
|
||||
int32_t errCode = 0;
|
||||
};
|
||||
|
||||
using CipherFwkCtx = CipherFwkCtxT *;
|
||||
|
||||
static void FreeParamsSpec(HcfParamsSpec *paramsSpec)
|
||||
{
|
||||
if (paramsSpec != nullptr) {
|
||||
return;
|
||||
}
|
||||
if (IV_PARAMS_SPEC.compare(paramsSpec->getType()) == 0) {
|
||||
HcfIvParamsSpec *iv = (HcfIvParamsSpec *)paramsSpec;
|
||||
HcfFree(iv->iv.data);
|
||||
iv->iv.data = nullptr;
|
||||
iv->iv.len = 0;
|
||||
}
|
||||
if (GCM_PARAMS_SPEC.compare(paramsSpec->getType()) == 0) {
|
||||
HcfGcmParamsSpec *gcm = (HcfGcmParamsSpec *)paramsSpec;
|
||||
HcfFree(gcm->iv.data);
|
||||
HcfFree(gcm->aad.data);
|
||||
HcfFree(gcm->tag.data);
|
||||
gcm->iv.len = 0;
|
||||
gcm->aad.len = 0;
|
||||
gcm->tag.len = 0;
|
||||
gcm->iv.data = nullptr;
|
||||
gcm->aad.data = nullptr;
|
||||
gcm->tag.data = nullptr;
|
||||
}
|
||||
if (CCM_PARAMS_SPEC.compare(paramsSpec->getType()) == 0) {
|
||||
HcfCcmParamsSpec *ccm = (HcfCcmParamsSpec *)paramsSpec;
|
||||
HcfFree(ccm->iv.data);
|
||||
HcfFree(ccm->aad.data);
|
||||
HcfFree(ccm->tag.data);
|
||||
ccm->iv.len = 0;
|
||||
ccm->aad.len = 0;
|
||||
ccm->tag.len = 0;
|
||||
ccm->iv.data = nullptr;
|
||||
ccm->aad.data = nullptr;
|
||||
ccm->tag.data = nullptr;
|
||||
}
|
||||
HcfFree(paramsSpec);
|
||||
}
|
||||
|
||||
static void FreeCipherFwkCtx(napi_env env, CipherFwkCtx &context)
|
||||
{
|
||||
if (context == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (context->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, context->asyncWork);
|
||||
context->asyncWork = nullptr;
|
||||
}
|
||||
|
||||
if (context->callback != nullptr) {
|
||||
napi_delete_reference(env, context->callback);
|
||||
context->callback = nullptr;
|
||||
}
|
||||
if (context->input.data != nullptr) {
|
||||
HcfFree(context->input.data);
|
||||
context->input.data = nullptr;
|
||||
context->input.len = 0;
|
||||
}
|
||||
if (context->output.data != nullptr) {
|
||||
HcfFree(context->output.data);
|
||||
context->output.data = nullptr;
|
||||
context->output.len = 0;
|
||||
}
|
||||
FreeParamsSpec(context->paramsSpec);
|
||||
context->paramsSpec = nullptr;
|
||||
|
||||
context->cipher = nullptr;
|
||||
context->key = nullptr;
|
||||
HcfFree(context);
|
||||
context = nullptr;
|
||||
}
|
||||
|
||||
bool BuildContextForInit(napi_env env, napi_callback_info info, CipherFwkCtx context)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
NapiCipher *napiCipher = nullptr;
|
||||
NapiKey *napiKey = nullptr;
|
||||
size_t expectedArgc = ARGS_SIZE_FOUR;
|
||||
size_t argc = ARGS_SIZE_FOUR;
|
||||
napi_value argv[ARGS_SIZE_FOUR] = { nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (argc != expectedArgc && argc != expectedArgc - 1) {
|
||||
LOGE("wrong argument num. require 3 or 4 arguments. [Argc]: %zu!", argc);
|
||||
return false;
|
||||
}
|
||||
context->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
|
||||
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)&napiCipher);
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi napiCipher obj!");
|
||||
return false;
|
||||
}
|
||||
|
||||
context->cipher = napiCipher->GetCipher();
|
||||
// get opMode, type is object
|
||||
size_t index = 0;
|
||||
if (napi_get_value_uint32(env, argv[index++], (uint32_t *)&(context->opMode)) != napi_ok) {
|
||||
LOGE("get opMode failed!");
|
||||
return false;
|
||||
}
|
||||
|
||||
// get key, unwrap from JS
|
||||
status = napi_unwrap(env, argv[index++], (void **)&napiKey);
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi napiSymKey obj!");
|
||||
return false;
|
||||
}
|
||||
|
||||
context->key = napiKey->GetHcfKey();
|
||||
|
||||
// get paramsSpec, unwrap from JS
|
||||
napi_valuetype valueType;
|
||||
napi_typeof(env, argv[index], &valueType);
|
||||
if (valueType != napi_null) {
|
||||
if (!GetParamsSpecFormNapiValue(env, argv[index], context->opMode, &context->paramsSpec)) {
|
||||
LOGE("GetParamsSpecFormNapiValue failed!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
index++;
|
||||
|
||||
if (context->asyncType == ASYNC_PROMISE) {
|
||||
napi_create_promise(env, &context->deferred, &context->promise);
|
||||
return true;
|
||||
} else {
|
||||
return GetCallbackFromJSParams(env, argv[index], &context->callback);
|
||||
}
|
||||
}
|
||||
|
||||
bool BuildContextForUpdate(napi_env env, napi_callback_info info, CipherFwkCtx context)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
NapiCipher *napiCipher = nullptr;
|
||||
size_t expectedArgc = ARGS_SIZE_TWO;
|
||||
size_t argc = ARGS_SIZE_TWO;
|
||||
napi_value argv[ARGS_SIZE_TWO] = { nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (argc != expectedArgc && argc != expectedArgc - 1) {
|
||||
LOGE("wrong argument num. require 1 or 2 arguments. [Argc]: %zu!", argc);
|
||||
return false;
|
||||
}
|
||||
context->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
|
||||
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)&napiCipher);
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi napiCipher obj!");
|
||||
return false;
|
||||
}
|
||||
context->cipher = napiCipher->GetCipher();
|
||||
// get input, type is blob
|
||||
size_t index = 0;
|
||||
HcfBlob *input = nullptr;
|
||||
input = GetBlobFromNapiValue(env, argv[index++]);
|
||||
if (input == nullptr) {
|
||||
LOGE("GetBlobFromNapiValue failed!");
|
||||
return false;
|
||||
}
|
||||
context->input.data = input->data;
|
||||
context->input.len = input->len;
|
||||
HcfFree(input);
|
||||
if (context->asyncType == ASYNC_PROMISE) {
|
||||
napi_create_promise(env, &context->deferred, &context->promise);
|
||||
return true;
|
||||
} else {
|
||||
return GetCallbackFromJSParams(env, argv[index], &context->callback);
|
||||
}
|
||||
}
|
||||
|
||||
bool BuildContextForFinal(napi_env env, napi_callback_info info, CipherFwkCtx context)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
NapiCipher *napiCipher = nullptr;
|
||||
size_t expectedArgc = ARGS_SIZE_TWO;
|
||||
size_t argc = ARGS_SIZE_TWO;
|
||||
napi_value argv[ARGS_SIZE_TWO] = { nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (argc != expectedArgc && argc != expectedArgc - 1) {
|
||||
LOGE("wrong argument num. require 1 or 2 arguments. [Argc]: %zu!", argc);
|
||||
return false;
|
||||
}
|
||||
context->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
|
||||
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)&napiCipher);
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi napiCipher obj!");
|
||||
return false;
|
||||
}
|
||||
context->cipher = napiCipher->GetCipher();
|
||||
|
||||
// get input, type is blob
|
||||
size_t index = 0;
|
||||
napi_valuetype valueType;
|
||||
napi_typeof(env, argv[index], &valueType);
|
||||
if (valueType != napi_null) {
|
||||
HcfBlob *input = nullptr;
|
||||
input = GetBlobFromNapiValue(env, argv[index]);
|
||||
if (input == nullptr) {
|
||||
LOGE("GetBlobFromNapiValue failed!");
|
||||
return false;
|
||||
}
|
||||
context->input.data = input->data;
|
||||
context->input.len = input->len;
|
||||
HcfFree(input);
|
||||
}
|
||||
index++;
|
||||
if (context->asyncType == ASYNC_PROMISE) {
|
||||
napi_create_promise(env, &context->deferred, &context->promise);
|
||||
return true;
|
||||
} else {
|
||||
return GetCallbackFromJSParams(env, argv[index], &context->callback);
|
||||
}
|
||||
}
|
||||
|
||||
static napi_value GenerateBusinessError(napi_env env, int32_t errorCode)
|
||||
{
|
||||
napi_value businessError = nullptr;
|
||||
napi_create_object(env, &businessError);
|
||||
|
||||
napi_value code = nullptr;
|
||||
napi_create_int32(env, errorCode, &code);
|
||||
napi_set_named_property(env, businessError, "code", code);
|
||||
|
||||
return businessError;
|
||||
}
|
||||
|
||||
static void ReturnCallbackResult(napi_env env, CipherFwkCtx context, napi_value result)
|
||||
{
|
||||
napi_value businessError = GenerateBusinessError(env, context->errCode);
|
||||
napi_value params[ARGS_SIZE_TWO] = { businessError, result };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, context->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnPromiseResult(napi_env env, CipherFwkCtx context, napi_value result)
|
||||
{
|
||||
if (context->errCode == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, context->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, context->deferred, result);
|
||||
}
|
||||
}
|
||||
|
||||
// init execute
|
||||
void AsyncInitProcess(napi_env env, void *data)
|
||||
{
|
||||
if (data == nullptr) {
|
||||
return;
|
||||
}
|
||||
CipherFwkCtx context = static_cast<CipherFwkCtx>(data);
|
||||
HcfCipher *cipher = context->cipher;
|
||||
HcfParamsSpec *params = context->paramsSpec;
|
||||
HcfKey *key = context->key;
|
||||
|
||||
HcfResult res = cipher->init(cipher, context->opMode, key, params);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("init ret:%d", (int32_t)res);
|
||||
context->errCode = res;
|
||||
return;
|
||||
}
|
||||
context->errCode = HCF_SUCCESS;
|
||||
}
|
||||
|
||||
// update execute
|
||||
void AsyncUpdateProcess(napi_env env, void *data)
|
||||
{
|
||||
if (data == nullptr) {
|
||||
return;
|
||||
}
|
||||
CipherFwkCtx context = static_cast<CipherFwkCtx>(data);
|
||||
HcfCipher *cipher = context->cipher;
|
||||
HcfResult res = cipher->update(cipher, &context->input, &context->output);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("Update ret:%d!", (int32_t)res);
|
||||
context->errCode = res;
|
||||
return;
|
||||
}
|
||||
context->errCode = HCF_SUCCESS;
|
||||
}
|
||||
|
||||
void AsyncDoFinalProcess(napi_env env, void *data)
|
||||
{
|
||||
if (data == nullptr) {
|
||||
return;
|
||||
}
|
||||
CipherFwkCtx context = static_cast<CipherFwkCtx>(data);
|
||||
HcfCipher *cipher = context->cipher;
|
||||
|
||||
HcfResult res = cipher->doFinal(cipher, &context->input, &context->output);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("doFinal ret:%d!", (int32_t)res);
|
||||
context->errCode = res;
|
||||
return;
|
||||
}
|
||||
context->errCode = HCF_SUCCESS;
|
||||
}
|
||||
|
||||
napi_value GetNapiNull(napi_env env)
|
||||
{
|
||||
napi_value output = nullptr;
|
||||
napi_status status = napi_get_null(env, &output);
|
||||
if (status != napi_ok) {
|
||||
LOGE("create null napi value failed");
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
void AsyncInitReturn(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
CipherFwkCtx context = static_cast<CipherFwkCtx>(data);
|
||||
napi_value result = GetNapiNull(env);
|
||||
|
||||
if (context->asyncType == ASYNC_CALLBACK) {
|
||||
ReturnCallbackResult(env, context, result);
|
||||
} else {
|
||||
ReturnPromiseResult(env, context, result);
|
||||
}
|
||||
FreeCipherFwkCtx(env, context);
|
||||
}
|
||||
|
||||
void AsyncUpdateReturn(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
CipherFwkCtx context = static_cast<CipherFwkCtx>(data);
|
||||
napi_value instance = ConvertBlobToNapiValue(env, &context->output);
|
||||
if (instance == nullptr) {
|
||||
LOGE("May be nullptr!");
|
||||
instance = GetNapiNull(env);
|
||||
}
|
||||
|
||||
if (context->asyncType == ASYNC_CALLBACK) {
|
||||
ReturnCallbackResult(env, context, instance);
|
||||
} else {
|
||||
ReturnPromiseResult(env, context, instance);
|
||||
}
|
||||
FreeCipherFwkCtx(env, context);
|
||||
}
|
||||
|
||||
void AsyncDoFinalReturn(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
CipherFwkCtx context = static_cast<CipherFwkCtx>(data);
|
||||
napi_value instance = ConvertBlobToNapiValue(env, &context->output);
|
||||
if (instance == nullptr) {
|
||||
LOGE("Maybe in decrypt mode, or CCM crypto maybe occur!");
|
||||
instance = GetNapiNull(env);
|
||||
}
|
||||
|
||||
if (context->asyncType == ASYNC_CALLBACK) {
|
||||
ReturnCallbackResult(env, context, instance);
|
||||
} else {
|
||||
ReturnPromiseResult(env, context, instance);
|
||||
}
|
||||
FreeCipherFwkCtx(env, context);
|
||||
}
|
||||
|
||||
napi_value NewAsyncInit(napi_env env, CipherFwkCtx context)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, "init", NAPI_AUTO_LENGTH, &resourceName);
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, resourceName,
|
||||
[](napi_env env, void *data) {
|
||||
AsyncInitProcess(env, data);
|
||||
return;
|
||||
},
|
||||
[](napi_env env, napi_status status, void *data) {
|
||||
AsyncInitReturn(env, status, data);
|
||||
return;
|
||||
},
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
napi_value NewAsyncUpdate(napi_env env, CipherFwkCtx context)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, "update", NAPI_AUTO_LENGTH, &resourceName);
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, resourceName,
|
||||
[](napi_env env, void *data) {
|
||||
AsyncUpdateProcess(env, data);
|
||||
return;
|
||||
},
|
||||
[](napi_env env, napi_status status, void *data) {
|
||||
AsyncUpdateReturn(env, status, data);
|
||||
return;
|
||||
},
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
napi_value NewAsyncDoFinal(napi_env env, CipherFwkCtx context)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, "DoFinal", NAPI_AUTO_LENGTH, &resourceName);
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, resourceName,
|
||||
[](napi_env env, void *data) {
|
||||
AsyncDoFinalProcess(env, data);
|
||||
return;
|
||||
},
|
||||
[](napi_env env, napi_status status, void *data) {
|
||||
AsyncDoFinalReturn(env, status, data);
|
||||
return;
|
||||
},
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
NapiCipher::NapiCipher(HcfCipher *cipher)
|
||||
{
|
||||
this->cipher_ = cipher;
|
||||
}
|
||||
|
||||
NapiCipher::~NapiCipher()
|
||||
{
|
||||
OH_HCF_ObjDestroy(this->cipher_);
|
||||
}
|
||||
|
||||
HcfCipher *NapiCipher::GetCipher()
|
||||
{
|
||||
return this->cipher_;
|
||||
}
|
||||
|
||||
napi_value NapiCipher::JsCipherInit(napi_env env, napi_callback_info info)
|
||||
{
|
||||
CipherFwkCtx context = (CipherFwkCtx)HcfMalloc(sizeof(CipherFwkCtxT), 0);
|
||||
if (context == nullptr) {
|
||||
LOGE("create context fail!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!BuildContextForInit(env, info, context)) {
|
||||
LOGE("build context for init fail!");
|
||||
FreeCipherFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NewAsyncInit(env, context);
|
||||
}
|
||||
|
||||
napi_value NapiCipher::JsCipherUpdate(napi_env env, napi_callback_info info)
|
||||
{
|
||||
CipherFwkCtx context = (CipherFwkCtx)HcfMalloc(sizeof(CipherFwkCtxT), 0);
|
||||
if (context == nullptr) {
|
||||
LOGE("create context fail!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!BuildContextForUpdate(env, info, context)) {
|
||||
LOGE("build context for update fail!");
|
||||
FreeCipherFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NewAsyncUpdate(env, context);
|
||||
}
|
||||
|
||||
napi_value NapiCipher::JsCipherDoFinal(napi_env env, napi_callback_info info)
|
||||
{
|
||||
CipherFwkCtx context = (CipherFwkCtx)HcfMalloc(sizeof(CipherFwkCtxT), 0);
|
||||
if (context == NULL) {
|
||||
LOGE("create context fail!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!BuildContextForFinal(env, info, context)) {
|
||||
LOGE("build context for final fail!");
|
||||
FreeCipherFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
return NewAsyncDoFinal(env, context);
|
||||
}
|
||||
|
||||
napi_value NapiCipher::JsGetAlgorithm(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
NapiCipher *napiCipher = nullptr;
|
||||
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
||||
|
||||
// get HcfSymKeyGenerator pointer
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)&napiCipher);
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napiCipher obj!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
HcfCipher *cipher = napiCipher->GetCipher();
|
||||
if (cipher == nullptr) {
|
||||
LOGE("failed to get cipher obj!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// execute C function
|
||||
const char *algo = cipher->getAlgorithm(cipher);
|
||||
napi_value instance = nullptr;
|
||||
napi_create_string_utf8(env, (const char *)algo, NAPI_AUTO_LENGTH, &instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
napi_value NapiCipher::CipherConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
||||
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiCipher::CreateCipher(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t exceptedArgc = 1;
|
||||
size_t argc;
|
||||
napi_value argv[1] = { nullptr };
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
|
||||
|
||||
if (argc != exceptedArgc) {
|
||||
LOGE("The input args num is invalid.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// create instance according to input js object
|
||||
napi_value instance;
|
||||
napi_value constructor = nullptr;
|
||||
NAPI_CALL(env, napi_get_reference_value(env, classRef_, &constructor));
|
||||
NAPI_CALL(env, napi_new_instance(env, constructor, argc, argv, &instance));
|
||||
|
||||
// parse input string
|
||||
std::string algoName;
|
||||
if (!GetStringFromJSParams(env, argv[0], algoName)) {
|
||||
LOGE("GetStringFromJSParams failed!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// execute C function, generate C object
|
||||
HcfCipher *cipher = NULL;
|
||||
HcfResult res = HcfCipherCreate(algoName.c_str(), &cipher);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("create c cipher fail!");
|
||||
return nullptr;
|
||||
}
|
||||
NapiCipher *napiCipher = new NapiCipher(cipher);
|
||||
if (napiCipher == nullptr) {
|
||||
LOGE("new napiCipher failed!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_status status = napi_wrap(env, instance, napiCipher,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
NapiCipher *napiCipher = static_cast<NapiCipher *>(data);
|
||||
delete napiCipher;
|
||||
return;
|
||||
},
|
||||
nullptr,
|
||||
nullptr);
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to wrap napiCipher obj!");
|
||||
delete napiCipher;
|
||||
return nullptr;
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NapiCipher::DefineCipherJSClass(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("createCipher", CreateCipher),
|
||||
};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
|
||||
napi_property_descriptor classDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("init", NapiCipher::JsCipherInit),
|
||||
DECLARE_NAPI_FUNCTION("update", NapiCipher::JsCipherUpdate),
|
||||
DECLARE_NAPI_FUNCTION("doFinal", NapiCipher::JsCipherDoFinal),
|
||||
{ .utf8name = "algName", .getter = NapiCipher::JsGetAlgorithm },
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "Cipher", NAPI_AUTO_LENGTH, NapiCipher::CipherConstructor, nullptr,
|
||||
sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
|
||||
} // CryptoFramework
|
||||
} // OHOS
|
161
frameworks/js/napi/src/napi_init.cpp
Normal file
161
frameworks/js/napi/src/napi_init.cpp
Normal file
@ -0,0 +1,161 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "securec.h"
|
||||
#include "log.h"
|
||||
#include "math.h"
|
||||
#include "memory.h"
|
||||
|
||||
#include "napi_x509_certificate.h"
|
||||
#include "napi_asy_key_generator.h"
|
||||
#include "napi_sym_key_generator.h"
|
||||
#include "napi_cipher.h"
|
||||
#include "napi_cert_chain_validator.h"
|
||||
#include "napi_key_pair.h"
|
||||
#include "napi_pri_key.h"
|
||||
#include "napi_pub_key.h"
|
||||
#include "napi_sign.h"
|
||||
#include "napi_verify.h"
|
||||
#include "napi_key_agreement.h"
|
||||
#include "napi_mac.h"
|
||||
#include "napi_md.h"
|
||||
#include "napi_rand.h"
|
||||
#include "napi_sym_key.h"
|
||||
#include "napi_key.h"
|
||||
#include "napi_utils.h"
|
||||
#include "napi_x509_crl_entry.h"
|
||||
#include "napi_x509_crl.h"
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
static napi_value CreateEncodingFormat(napi_env env)
|
||||
{
|
||||
napi_value encodingFormat = nullptr;
|
||||
napi_create_object(env, &encodingFormat);
|
||||
|
||||
AddUint32Property(env, encodingFormat, "FORMAT_DER", HCF_FORMAT_DER);
|
||||
AddUint32Property(env, encodingFormat, "FORMAT_PEM", HCF_FORMAT_PEM);
|
||||
|
||||
return encodingFormat;
|
||||
}
|
||||
|
||||
static void DefineEncodingFormatProperties(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_PROPERTY("EncodingFormat", CreateEncodingFormat(env)),
|
||||
};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
}
|
||||
|
||||
static napi_value CreateCryptoMode(napi_env env)
|
||||
{
|
||||
napi_value cryptoMode = nullptr;
|
||||
napi_create_object(env, &cryptoMode);
|
||||
|
||||
AddUint32Property(env, cryptoMode, "ENCRYPT_MODE", ENCRYPT_MODE);
|
||||
AddUint32Property(env, cryptoMode, "DECRYPT_MODE", DECRYPT_MODE);
|
||||
return cryptoMode;
|
||||
}
|
||||
|
||||
static void DefineCryptoModeProperties(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_PROPERTY("CryptoMode", CreateCryptoMode(env)),
|
||||
};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
}
|
||||
|
||||
static napi_value CreateResultCode(napi_env env)
|
||||
{
|
||||
napi_value resultCode = nullptr;
|
||||
napi_create_object(env, &resultCode);
|
||||
|
||||
AddUint32Property(env, resultCode, "INVALID_PARAMS", JS_ERR_INVALID_PARAMS);
|
||||
AddUint32Property(env, resultCode, "NOT_SUPPORT", JS_ERR_NOT_SUPPORT);
|
||||
AddUint32Property(env, resultCode, "ERR_OUT_OF_MEMORY", JS_ERR_OUT_OF_MEMORY);
|
||||
AddUint32Property(env, resultCode, "ERR_INTERNAL_ERROR", JS_ERR_INTERNAL_ERROR);
|
||||
AddUint32Property(env, resultCode, "ERR_CRYPTO_OPERATION", JS_ERR_CRYPTO_OPERATION);
|
||||
AddUint32Property(env, resultCode, "ERR_CERT_SIGNATURE_FAILURE", JS_ERR_CERT_SIGNATURE_FAILURE);
|
||||
AddUint32Property(env, resultCode, "ERR_CERT_NOT_YET_VALID", JS_ERR_CERT_NOT_YET_VALID);
|
||||
AddUint32Property(env, resultCode, "ERR_CERT_HAS_EXPIRED", JS_ERR_CERT_HAS_EXPIRED);
|
||||
AddUint32Property(env, resultCode, "ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY",
|
||||
JS_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY);
|
||||
AddUint32Property(env, resultCode, "ERR_KEYUSAGE_NO_CERTSIGN", JS_ERR_KEYUSAGE_NO_CERTSIGN);
|
||||
AddUint32Property(env, resultCode, "ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE", JS_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE);
|
||||
|
||||
return resultCode;
|
||||
}
|
||||
|
||||
static void DefineResultCodeProperties(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_PROPERTY("Result", CreateResultCode(env)),
|
||||
};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
}
|
||||
|
||||
/***********************************************
|
||||
* Module export and register
|
||||
***********************************************/
|
||||
static napi_value ModuleExport(napi_env env, napi_value exports)
|
||||
{
|
||||
LOGI("module init start.");
|
||||
|
||||
DefineEncodingFormatProperties(env, exports);
|
||||
DefineCryptoModeProperties(env, exports);
|
||||
DefineResultCodeProperties(env, exports);
|
||||
NapiAsyKeyGenerator::DefineAsyKeyGeneratorJSClass(env, exports);
|
||||
NapiKeyPair::DefineKeyPairJSClass(env);
|
||||
NapiPubKey::DefinePubKeyJSClass(env);
|
||||
NapiPriKey::DefinePriKeyJSClass(env);
|
||||
|
||||
NapiSign::DefineSignJSClass(env, exports);
|
||||
NapiVerify::DefineVerifyJSClass(env, exports);
|
||||
NapiKeyAgreement::DefineKeyAgreementJSClass(env, exports);
|
||||
// NapiCertFactory::DefineCertFactoryJSClass(env, exports);
|
||||
NapiCertChainValidator::DefineCertChainValidatorJSClass(env, exports);
|
||||
NapiMac::DefineMacJSClass(env, exports);
|
||||
NapiMd::DefineMdJSClass(env, exports);
|
||||
NapiPubKey::DefinePubKeyJSClass(env);
|
||||
NapiRand::DefineRandJSClass(env, exports);
|
||||
NapiSymKeyGenerator::DefineSymKeyGeneratorJSClass(env, exports);
|
||||
NapiCipher::DefineCipherJSClass(env, exports);
|
||||
NapiSymKey::DefineSymKeyJSClass(env);
|
||||
NapiKey::DefineHcfKeyJSClass(env);
|
||||
NapiX509Cert::DefineX509CertJSClass(env, exports);
|
||||
NapiX509CrlEntry::DefineX509CrlEntryJSClass(env);
|
||||
NapiX509Crl::DefineX509CrlJSClass(env, exports);
|
||||
LOGI("module init end.");
|
||||
return exports;
|
||||
}
|
||||
|
||||
static napi_module cryptoFrameworkModule = {
|
||||
.nm_version = 1,
|
||||
.nm_flags = 0,
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = ModuleExport,
|
||||
.nm_modname = "security.cryptoFramework",
|
||||
.nm_priv = ((void*)0),
|
||||
.reserved = { 0 },
|
||||
};
|
||||
|
||||
extern "C" __attribute__((constructor)) void RegisterModule(void)
|
||||
{
|
||||
napi_module_register(&cryptoFrameworkModule);
|
||||
}
|
||||
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
125
frameworks/js/napi/src/napi_key.cpp
Normal file
125
frameworks/js/napi/src/napi_key.cpp
Normal file
@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_key.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
|
||||
#include "napi_utils.h"
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
thread_local napi_ref NapiKey::classRef_ = nullptr;
|
||||
|
||||
NapiKey::NapiKey(HcfKey *hcfKey)
|
||||
{
|
||||
this->hcfKey_ = hcfKey;
|
||||
}
|
||||
|
||||
NapiKey::~NapiKey()
|
||||
{
|
||||
OH_HCF_ObjDestroy(this->hcfKey_);
|
||||
}
|
||||
|
||||
HcfKey *NapiKey::GetHcfKey()
|
||||
{
|
||||
return this->hcfKey_;
|
||||
}
|
||||
|
||||
napi_value NapiKey::JsGetAlgorithm(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
NapiKey *napiKey = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
|
||||
(void)napi_unwrap(env, thisVar, (void **)&napiKey);
|
||||
HcfKey *key = napiKey->GetHcfKey();
|
||||
|
||||
const char *algo = key->getAlgorithm(key);
|
||||
napi_value instance = nullptr;
|
||||
napi_create_string_utf8(env, (const char *)algo, NAPI_AUTO_LENGTH, &instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
napi_value NapiKey::JsGetFormat(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
NapiKey *napiKey = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
|
||||
(void)napi_unwrap(env, thisVar, (void **)&napiKey);
|
||||
HcfKey *key = napiKey->GetHcfKey();
|
||||
|
||||
const char *format = key->getFormat(key);
|
||||
napi_value instance = nullptr;
|
||||
napi_create_string_utf8(env, (const char *)format, NAPI_AUTO_LENGTH, &instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
napi_value NapiKey::JsGetEncoded(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
NapiKey *napiKey = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
|
||||
(void)napi_unwrap(env, thisVar, (void **)&napiKey);
|
||||
HcfKey *key = napiKey->GetHcfKey();
|
||||
|
||||
HcfBlob blob = {0};
|
||||
HcfResult res = key->getEncoded(key, &blob);
|
||||
if (res != 0) {
|
||||
LOGE("getEncoded failed!");
|
||||
return nullptr;
|
||||
}
|
||||
napi_value instance = ConvertBlobToNapiValue(env, &blob);
|
||||
HcfFree(blob.data);
|
||||
return instance;
|
||||
}
|
||||
|
||||
napi_value NapiKey::KeyConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiKey::CreateHcfKey(napi_env env)
|
||||
{
|
||||
napi_value instance;
|
||||
napi_value constructor = nullptr;
|
||||
napi_get_reference_value(env, classRef_, &constructor);
|
||||
napi_new_instance(env, constructor, 0, nullptr, &instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NapiKey::DefineHcfKeyJSClass(napi_env env)
|
||||
{
|
||||
napi_property_descriptor classDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("getEncoded", NapiKey::JsGetEncoded),
|
||||
{.utf8name = "format", .getter = NapiKey::JsGetFormat},
|
||||
{.utf8name = "algName", .getter = NapiKey::JsGetAlgorithm},
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "HcfKey", NAPI_AUTO_LENGTH, KeyConstructor, nullptr,
|
||||
sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
|
||||
} // CryptoFramework
|
||||
} // OHOS
|
321
frameworks/js/napi/src/napi_key_agreement.cpp
Normal file
321
frameworks/js/napi/src/napi_key_agreement.cpp
Normal file
@ -0,0 +1,321 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_key_agreement.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
#include "napi_pri_key.h"
|
||||
#include "napi_pub_key.h"
|
||||
#include "napi_utils.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
struct KeyAgreementCtx {
|
||||
napi_env env = nullptr;
|
||||
|
||||
AsyncType asyncType = ASYNC_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_value promise = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
HcfKeyAgreement *keyAgreement;
|
||||
HcfPriKey *priKey;
|
||||
HcfPubKey *pubKey;
|
||||
|
||||
HcfResult result;
|
||||
HcfBlob returnSecret;
|
||||
};
|
||||
|
||||
thread_local napi_ref NapiKeyAgreement::classRef_ = nullptr;
|
||||
|
||||
static void FreeKeyAgreementCtx(napi_env env, KeyAgreementCtx *ctx)
|
||||
{
|
||||
if (ctx == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, ctx->asyncWork);
|
||||
ctx->asyncWork = nullptr;
|
||||
}
|
||||
|
||||
if (ctx->callback != nullptr) {
|
||||
napi_delete_reference(env, ctx->callback);
|
||||
ctx->callback = nullptr;
|
||||
}
|
||||
|
||||
if (ctx->returnSecret.data != nullptr) {
|
||||
HcfFree(ctx->returnSecret.data);
|
||||
ctx->returnSecret.data = nullptr;
|
||||
ctx->returnSecret.len = 0;
|
||||
}
|
||||
|
||||
HcfFree(ctx);
|
||||
}
|
||||
|
||||
static bool BuildKeyAgreementJsCtx(napi_env env, napi_callback_info info, KeyAgreementCtx *ctx)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t expectedArgc = PARAMS_NUM_THREE;
|
||||
size_t argc;
|
||||
napi_value argv[PARAMS_NUM_THREE] = { nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (argc != expectedArgc && argc != expectedArgc - 1) {
|
||||
LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc);
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error."));
|
||||
return false;
|
||||
}
|
||||
ctx->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
|
||||
|
||||
NapiKeyAgreement *napiKeyAgreement = NULL;
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)(&napiKeyAgreement));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi verify obj.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error."));
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
NapiPriKey *napiPriKey = NULL;
|
||||
status = napi_unwrap(env, argv[index], (void **)(&napiPriKey));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap priKey verify obj.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PriKey]: param unwarp error."));
|
||||
return false;
|
||||
}
|
||||
|
||||
index++;
|
||||
NapiPubKey *napiPubKey = NULL;
|
||||
status = napi_unwrap(env, argv[index], (void **)(&napiPubKey));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi pubKey obj.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PubKey]: param unwarp error."));
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx->keyAgreement = napiKeyAgreement->GetKeyAgreement();
|
||||
ctx->priKey = napiPriKey->GetPriKey();
|
||||
ctx->pubKey = napiPubKey->GetPubKey();
|
||||
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
napi_create_promise(env, &ctx->deferred, &ctx->promise);
|
||||
return true;
|
||||
} else {
|
||||
return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback);
|
||||
}
|
||||
}
|
||||
|
||||
static void ReturnCallbackResult(napi_env env, KeyAgreementCtx *ctx, napi_value result)
|
||||
{
|
||||
napi_value businessError = nullptr;
|
||||
if (ctx->result != HCF_SUCCESS) {
|
||||
businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str());
|
||||
}
|
||||
|
||||
napi_value params[ARGS_SIZE_TWO] = { businessError, result };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, ctx->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnPromiseResult(napi_env env, KeyAgreementCtx *ctx, napi_value result)
|
||||
{
|
||||
if (ctx->result == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, ctx->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
void KeyAgreementAsyncWorkProcess(napi_env env, void *data)
|
||||
{
|
||||
KeyAgreementCtx *ctx = static_cast<KeyAgreementCtx *>(data);
|
||||
|
||||
HcfResult res = ctx->keyAgreement->generateSecret(ctx->keyAgreement,
|
||||
ctx->priKey, ctx->pubKey, &ctx->returnSecret);
|
||||
|
||||
ctx->result = res;
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("generate secret fail.");
|
||||
}
|
||||
}
|
||||
|
||||
void KeyAgreementAsyncWorkReturn(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
KeyAgreementCtx *ctx = static_cast<KeyAgreementCtx *>(data);
|
||||
|
||||
napi_value dataBlob = nullptr;
|
||||
if (ctx->result == HCF_SUCCESS) {
|
||||
dataBlob = ConvertBlobToNapiValue(env, &ctx->returnSecret);
|
||||
}
|
||||
|
||||
if (ctx->asyncType == ASYNC_CALLBACK) {
|
||||
ReturnCallbackResult(env, ctx, dataBlob);
|
||||
} else {
|
||||
ReturnPromiseResult(env, ctx, dataBlob);
|
||||
}
|
||||
FreeKeyAgreementCtx(env, ctx);
|
||||
}
|
||||
|
||||
static napi_value NewKeyAgreementAsyncWork(napi_env env, KeyAgreementCtx *ctx)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, "generateSecret", NAPI_AUTO_LENGTH, &resourceName);
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, resourceName,
|
||||
[](napi_env env, void *data) {
|
||||
KeyAgreementAsyncWorkProcess(env, data);
|
||||
return;
|
||||
},
|
||||
[](napi_env env, napi_status status, void *data) {
|
||||
KeyAgreementAsyncWorkReturn(env, status, data);
|
||||
return;
|
||||
},
|
||||
(void *)ctx,
|
||||
&ctx->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, ctx->asyncWork);
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
return ctx->promise;
|
||||
} else {
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
NapiKeyAgreement::NapiKeyAgreement(HcfKeyAgreement *keyAgreement)
|
||||
{
|
||||
this->keyAgreement_ = keyAgreement;
|
||||
}
|
||||
|
||||
NapiKeyAgreement::~NapiKeyAgreement()
|
||||
{
|
||||
OH_HCF_ObjDestroy(this->keyAgreement_);
|
||||
}
|
||||
|
||||
HcfKeyAgreement *NapiKeyAgreement::GetKeyAgreement()
|
||||
{
|
||||
return this->keyAgreement_;
|
||||
}
|
||||
|
||||
napi_value NapiKeyAgreement::JsGenerateSecret(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
KeyAgreementCtx *ctx = (KeyAgreementCtx *)HcfMalloc(sizeof(KeyAgreementCtx), 0);
|
||||
if (ctx == NULL) {
|
||||
LOGE("create context fail.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!BuildKeyAgreementJsCtx(env, info, ctx)) {
|
||||
LOGE("build context fail.");
|
||||
FreeKeyAgreementCtx(env, ctx);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NewKeyAgreementAsyncWork(env, ctx);
|
||||
}
|
||||
|
||||
napi_value NapiKeyAgreement::KeyAgreementConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
|
||||
LOGI("out ...");
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiKeyAgreement::CreateJsKeyAgreement(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
size_t exceptedArgc = PARAMS_NUM_ONE;
|
||||
size_t argc;
|
||||
napi_value argv[PARAMS_NUM_ONE] = { nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
|
||||
if (argc != exceptedArgc) {
|
||||
LOGE("The input args num is invalid.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value instance;
|
||||
napi_value constructor = nullptr;
|
||||
napi_get_reference_value(env, classRef_, &constructor);
|
||||
napi_new_instance(env, constructor, argc, argv, &instance);
|
||||
|
||||
std::string algName;
|
||||
if (!GetStringFromJSParams(env, argv[0], algName)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
HcfKeyAgreement *keyAgreement = NULL;
|
||||
int32_t res = HcfKeyAgreementCreate(algName.c_str(), &keyAgreement);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("create c keyAgreement fail.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
NapiKeyAgreement *napiKeyAgreement = new NapiKeyAgreement(keyAgreement);
|
||||
|
||||
napi_wrap(
|
||||
env, instance, napiKeyAgreement,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
NapiKeyAgreement *napiKeyAgreement = static_cast<NapiKeyAgreement *>(data);
|
||||
delete napiKeyAgreement;
|
||||
return;
|
||||
},
|
||||
nullptr,
|
||||
nullptr);
|
||||
|
||||
napi_value napiAlgName = nullptr;
|
||||
napi_create_string_utf8(env, algName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName);
|
||||
napi_set_named_property(env, instance, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName);
|
||||
|
||||
LOGI("out ...");
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NapiKeyAgreement::DefineKeyAgreementJSClass(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("createKeyAgreement", NapiKeyAgreement::CreateJsKeyAgreement),
|
||||
};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
|
||||
napi_property_descriptor classDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("generateSecret", NapiKeyAgreement::JsGenerateSecret),
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "KeyAgreement", NAPI_AUTO_LENGTH, NapiKeyAgreement::KeyAgreementConstructor, nullptr,
|
||||
sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
} // CryptoFramework
|
||||
} // OHOS
|
100
frameworks/js/napi/src/napi_key_pair.cpp
Normal file
100
frameworks/js/napi/src/napi_key_pair.cpp
Normal file
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_key_pair.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
#include "napi_pri_key.h"
|
||||
#include "napi_pub_key.h"
|
||||
|
||||
#include "napi_utils.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
thread_local napi_ref NapiKeyPair::classRef_ = nullptr;
|
||||
|
||||
NapiKeyPair::NapiKeyPair(HcfKeyPair *keyPair)
|
||||
{
|
||||
this->keyPair_ = keyPair;
|
||||
}
|
||||
|
||||
NapiKeyPair::~NapiKeyPair()
|
||||
{
|
||||
OH_HCF_ObjDestroy(this->keyPair_);
|
||||
}
|
||||
|
||||
napi_value NapiKeyPair::KeyPairConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
LOGI("out ...");
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiKeyPair::ConvertToJsKeyPair(napi_env env)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
|
||||
napi_value instance;
|
||||
napi_value constructor = nullptr;
|
||||
napi_get_reference_value(env, classRef_, &constructor);
|
||||
napi_new_instance(env, constructor, 0, nullptr, &instance);
|
||||
|
||||
NapiPubKey *napiPubKey = new NapiPubKey(this->keyPair_->pubKey);
|
||||
NapiPriKey *napiPriKey = new NapiPriKey(this->keyPair_->priKey);
|
||||
|
||||
napi_value pubKey = napiPubKey->ConvertToJsPubKey(env);
|
||||
napi_value priKey = napiPriKey->ConvertToJsPriKey(env);
|
||||
|
||||
napi_wrap(
|
||||
env, pubKey, napiPubKey,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
NapiPubKey *napiPubKey = static_cast<NapiPubKey *>(data);
|
||||
delete napiPubKey;
|
||||
return;
|
||||
},
|
||||
nullptr, nullptr);
|
||||
napi_wrap(
|
||||
env, priKey, napiPriKey,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
NapiPriKey *napiPriKey = static_cast<NapiPriKey *>(data);
|
||||
delete napiPriKey;
|
||||
return;
|
||||
},
|
||||
nullptr, nullptr);
|
||||
|
||||
napi_set_named_property(env, instance, CRYPTO_TAG_PUB_KEY.c_str(), pubKey);
|
||||
napi_set_named_property(env, instance, CRYPTO_TAG_PRI_KEY.c_str(), priKey);
|
||||
|
||||
LOGI("out ...");
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NapiKeyPair::DefineKeyPairJSClass(napi_env env)
|
||||
{
|
||||
napi_property_descriptor classDesc[] = {};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "KeyPair", NAPI_AUTO_LENGTH, KeyPairConstructor, nullptr,
|
||||
sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
|
||||
} // CryptoFramework
|
||||
} // OHOS
|
484
frameworks/js/napi/src/napi_mac.cpp
Normal file
484
frameworks/js/napi/src/napi_mac.cpp
Normal file
@ -0,0 +1,484 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_mac.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
|
||||
#include "napi_sym_key.h"
|
||||
#include "napi_utils.h"
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
napi_ref NapiMac::classRef_ = nullptr;
|
||||
|
||||
struct MacCtx {
|
||||
napi_env env = nullptr;
|
||||
|
||||
CfAsyncType asyncType = ASYNC_TYPE_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_value promise = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
NapiMac *macClass = nullptr;
|
||||
std::string algoName = "";
|
||||
HcfSymKey *symKey = nullptr;
|
||||
HcfBlob *inBlob = nullptr;
|
||||
|
||||
HcfResult errCode = HCF_SUCCESS;
|
||||
const char *errMsg = nullptr;
|
||||
HcfBlob *outBlob = nullptr;
|
||||
};
|
||||
|
||||
static void FreeCryptoFwkCtx(napi_env env, MacCtx *context)
|
||||
{
|
||||
if (context == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (context->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, context->asyncWork);
|
||||
context->asyncWork = nullptr;
|
||||
}
|
||||
if (context->callback != nullptr) {
|
||||
napi_delete_reference(env, context->callback);
|
||||
context->callback = nullptr;
|
||||
}
|
||||
if (context->symKey != nullptr) {
|
||||
OH_HCF_ObjDestroy(context->symKey);
|
||||
}
|
||||
if (context->inBlob != nullptr) {
|
||||
HcfFree(context->inBlob->data);
|
||||
context->inBlob->data = nullptr;
|
||||
context->inBlob->len = 0;
|
||||
}
|
||||
if (context->outBlob != nullptr) {
|
||||
HcfFree(context->outBlob->data);
|
||||
context->outBlob->data = nullptr;
|
||||
context->outBlob->len = 0;
|
||||
}
|
||||
HcfFree(context);
|
||||
}
|
||||
|
||||
static void ReturnCallbackResult(napi_env env, MacCtx *context, napi_value result)
|
||||
{
|
||||
napi_value businessError = nullptr;
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
businessError = GenerateBusinessError(env, context->errCode, context->errMsg);
|
||||
}
|
||||
napi_value params[ARGS_SIZE_TWO] = { businessError, result };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, context->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnPromiseResult(napi_env env, MacCtx *context, napi_value result)
|
||||
{
|
||||
if (context->errCode == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, context->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg));
|
||||
}
|
||||
}
|
||||
|
||||
static bool CreateCallbackAndPromise(napi_env env, MacCtx *context, size_t argc,
|
||||
size_t maxCount, napi_value callbackValue)
|
||||
{
|
||||
context->asyncType = (argc == maxCount) ? ASYNC_TYPE_CALLBACK : ASYNC_TYPE_PROMISE;
|
||||
if (context->asyncType == ASYNC_TYPE_CALLBACK) {
|
||||
if (!GetCallbackFromJSParams(env, callbackValue, &context->callback)) {
|
||||
LOGE("get callback failed!");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
napi_create_promise(env, &context->deferred, &context->promise);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
NapiMac::NapiMac(HcfMac *macObj)
|
||||
{
|
||||
this->macObj_ = macObj;
|
||||
}
|
||||
|
||||
NapiMac::~NapiMac()
|
||||
{
|
||||
OH_HCF_ObjDestroy(this->macObj_);
|
||||
}
|
||||
|
||||
static void MacInitExecute(napi_env env, void *data)
|
||||
{
|
||||
LOGI("enter MacInitExecute ...");
|
||||
MacCtx *context = static_cast<MacCtx *>(data);
|
||||
NapiMac *macClass = context->macClass;
|
||||
HcfMac *macObj = macClass->GetMac();
|
||||
HcfSymKey *symKey = (HcfSymKey *)context->symKey;
|
||||
context->errCode = macObj->init(macObj, symKey);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
LOGE("init failed!");
|
||||
context->errMsg = "init failed";
|
||||
}
|
||||
}
|
||||
|
||||
static void MacInitComplete(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
MacCtx *context = static_cast<MacCtx *>(data);
|
||||
napi_value nullInstance = nullptr;
|
||||
napi_get_null(env, &nullInstance);
|
||||
if (context->asyncType == ASYNC_TYPE_CALLBACK) {
|
||||
ReturnCallbackResult(env, context, nullInstance);
|
||||
} else {
|
||||
ReturnPromiseResult(env, context, nullInstance);
|
||||
}
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
}
|
||||
|
||||
static void MacUpdateExecute(napi_env env, void *data)
|
||||
{
|
||||
MacCtx *context = static_cast<MacCtx *>(data);
|
||||
NapiMac *macClass = context->macClass;
|
||||
HcfMac *macObj = macClass->GetMac();
|
||||
HcfBlob *inBlob = static_cast<HcfBlob *>(context->inBlob);
|
||||
context->errCode = macObj->update(macObj, inBlob);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
LOGE("update failed!");
|
||||
context->errMsg = "update failed";
|
||||
}
|
||||
}
|
||||
|
||||
static void MacUpdateComplete(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
MacCtx *context = static_cast<MacCtx *>(data);
|
||||
napi_value nullInstance = nullptr;
|
||||
napi_get_null(env, &nullInstance);
|
||||
if (context->asyncType == ASYNC_TYPE_CALLBACK) {
|
||||
ReturnCallbackResult(env, context, nullInstance);
|
||||
} else {
|
||||
ReturnPromiseResult(env, context, nullInstance);
|
||||
}
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
}
|
||||
|
||||
static void MacDoFinalExecute(napi_env env, void *data)
|
||||
{
|
||||
MacCtx *context = static_cast<MacCtx *>(data);
|
||||
NapiMac *macClass = context->macClass;
|
||||
HcfMac *macObj = macClass->GetMac();
|
||||
HcfBlob *outBlob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0);
|
||||
if (outBlob == nullptr) {
|
||||
LOGE("outBlob is null!");
|
||||
context->errCode = HCF_ERR_MALLOC;
|
||||
context->errMsg = "malloc data blob failed";
|
||||
return;
|
||||
}
|
||||
context->errCode = macObj->doFinal(macObj, outBlob);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
LOGE("doFinal failed!");
|
||||
context->errMsg = "doFinal failed";
|
||||
return;
|
||||
}
|
||||
context->outBlob = outBlob;
|
||||
}
|
||||
|
||||
static void MacDoFinalComplete(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
LOGI("enter MacDoFinalComplete ...");
|
||||
MacCtx *context = static_cast<MacCtx *>(data);
|
||||
napi_value returnOutBlob = ConvertBlobToNapiValue(env, context->outBlob);
|
||||
if (returnOutBlob == nullptr) {
|
||||
LOGE("returnOutBlob is nullptr!");
|
||||
returnOutBlob = NapiGetNull(env);
|
||||
}
|
||||
if (context->asyncType == ASYNC_TYPE_CALLBACK) {
|
||||
ReturnCallbackResult(env, context, returnOutBlob);
|
||||
} else {
|
||||
ReturnPromiseResult(env, context, returnOutBlob);
|
||||
}
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
}
|
||||
|
||||
napi_value NapiMac::MacInit(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t expectedArgsCount = ARGS_SIZE_TWO;
|
||||
size_t argc = expectedArgsCount;
|
||||
napi_value argv[ARGS_SIZE_TWO] = { 0 };
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) {
|
||||
return nullptr;
|
||||
}
|
||||
MacCtx *context = (MacCtx *)HcfMalloc(sizeof(MacCtx), 0);
|
||||
if (context == nullptr) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed"));
|
||||
LOGE("malloc context failed!");
|
||||
return nullptr;
|
||||
}
|
||||
context->macClass = this;
|
||||
NapiSymKey *symKey = nullptr;
|
||||
napi_unwrap(env, argv[PARAM0], (void**)&symKey);
|
||||
if (symKey == nullptr) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "symKey is null"));
|
||||
LOGE("symKey is null!");
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
context->symKey = symKey->GetSymKey();
|
||||
context->asyncType = (argc == expectedArgsCount) ? ASYNC_TYPE_CALLBACK : ASYNC_TYPE_PROMISE;
|
||||
if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_TWO, argv[PARAM1])) {
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
napi_create_async_work(
|
||||
env, nullptr, GetResourceName(env, "Init"),
|
||||
MacInitExecute,
|
||||
MacInitComplete,
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_TYPE_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
}
|
||||
|
||||
napi_value NapiMac::MacUpdate(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t expectedArgsCount = ARGS_SIZE_TWO;
|
||||
size_t argc = expectedArgsCount;
|
||||
napi_value argv[ARGS_SIZE_TWO] = { 0 };
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) {
|
||||
return nullptr;
|
||||
}
|
||||
MacCtx *context = (MacCtx *)HcfMalloc(sizeof(MacCtx), 0);
|
||||
if (context == nullptr) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed"));
|
||||
LOGE("malloc context failed!");
|
||||
return nullptr;
|
||||
}
|
||||
context->macClass = this;
|
||||
context->inBlob = GetBlobFromNapiValue(env, argv[PARAM0]);
|
||||
if (context->inBlob == nullptr) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "inBlob is null"));
|
||||
LOGE("inBlob is null!");
|
||||
return nullptr;
|
||||
}
|
||||
if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_TWO, argv[PARAM1])) {
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
napi_create_async_work(
|
||||
env, nullptr, GetResourceName(env, "MacUpate"),
|
||||
MacUpdateExecute,
|
||||
MacUpdateComplete,
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_TYPE_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
}
|
||||
|
||||
napi_value NapiMac::MacDoFinal(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t expectedArgsCount = ARGS_SIZE_ONE;
|
||||
size_t argc = expectedArgsCount;
|
||||
napi_value argv[ARGS_SIZE_ONE] = { 0 };
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) {
|
||||
return nullptr;
|
||||
}
|
||||
MacCtx *context = (MacCtx *)HcfMalloc(sizeof(MacCtx), 0);
|
||||
if (context == nullptr) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed"));
|
||||
LOGE("malloc context failed!");
|
||||
return nullptr;
|
||||
}
|
||||
context->macClass = this;
|
||||
context->asyncType = (argc == expectedArgsCount) ? ASYNC_TYPE_CALLBACK : ASYNC_TYPE_PROMISE;
|
||||
if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_ONE, argv[PARAM0])) {
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
napi_create_async_work(
|
||||
env, nullptr, GetResourceName(env, "MacDoFinal"),
|
||||
MacDoFinalExecute,
|
||||
MacDoFinalComplete,
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_TYPE_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
}
|
||||
|
||||
napi_value NapiMac::GetMacLength(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t expectedArgsCount = ARGS_SIZE_ZERO;
|
||||
size_t argc = expectedArgsCount;
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
|
||||
if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) {
|
||||
return nullptr;
|
||||
}
|
||||
HcfMac *macObj = GetMac();
|
||||
uint32_t retLen = macObj->getMacLength(macObj);
|
||||
napi_value napiLen = nullptr;
|
||||
napi_create_uint32(env, retLen, &napiLen);
|
||||
return napiLen;
|
||||
}
|
||||
|
||||
static napi_value NapiMacInit(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiMac *macObj = nullptr;
|
||||
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&macObj));
|
||||
if (macObj == nullptr) {
|
||||
LOGE("macObj is nullptr!");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
return macObj->MacInit(env, info);
|
||||
}
|
||||
|
||||
static napi_value NapiMacUpdate(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiMac *macObj = nullptr;
|
||||
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&macObj));
|
||||
if (macObj == nullptr) {
|
||||
LOGE("macObj is nullptr!");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
return macObj->MacUpdate(env, info);
|
||||
}
|
||||
|
||||
static napi_value NapiMacDoFinal(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiMac *macObj = nullptr;
|
||||
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&macObj));
|
||||
if (macObj == nullptr) {
|
||||
LOGE("macObj is nullptr!");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
return macObj->MacDoFinal(env, info);
|
||||
}
|
||||
|
||||
static napi_value NapiGetMacLength(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiMac *macObj = nullptr;
|
||||
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&macObj));
|
||||
if (macObj == nullptr) {
|
||||
LOGE("macObj is nullptr!");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
return macObj->GetMacLength(env, info);
|
||||
}
|
||||
|
||||
napi_value NapiMac::MacConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
printf("enter MacConstructor...");
|
||||
napi_value thisVar = nullptr;
|
||||
size_t exceptedArgc = ARGS_SIZE_ONE;
|
||||
size_t argc = exceptedArgc;
|
||||
napi_value argv[ARGS_SIZE_ONE] = { 0 };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
std::string algoName;
|
||||
if (!GetStringFromJSParams(env, argv[0], algoName)) {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
HcfMac *macObj = nullptr;
|
||||
int32_t res = HcfMacCreate(algoName.c_str(), &macObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("create c macObj failed.");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
NapiMac *macNapiObj = new NapiMac(macObj);
|
||||
napi_wrap(
|
||||
env, thisVar, macNapiObj,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
NapiMac *mac = (NapiMac *)(data);
|
||||
delete mac;
|
||||
return;
|
||||
},
|
||||
nullptr,
|
||||
nullptr);
|
||||
napi_value napiAlgName = nullptr;
|
||||
napi_create_string_utf8(env, algoName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName);
|
||||
napi_set_named_property(env, thisVar, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName);
|
||||
printf("out MacConstructor...");
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiMac::CreateMac(napi_env env, napi_callback_info info)
|
||||
{
|
||||
printf("enter CreateMac...");
|
||||
size_t exceptedArgc = ARGS_SIZE_ONE;
|
||||
size_t argc;
|
||||
napi_value argv[ARGS_SIZE_ONE] = { 0 };
|
||||
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
if (argc != exceptedArgc) {
|
||||
LOGE("The input args num is invalid.");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
napi_value instance;
|
||||
napi_value constructor = nullptr;
|
||||
napi_get_reference_value(env, classRef_, &constructor);
|
||||
napi_new_instance(env, constructor, argc, argv, &instance);
|
||||
printf("out CreateMac...");
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NapiMac::DefineMacJSClass(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("createMac", CreateMac),
|
||||
};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
napi_property_descriptor classDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("init", NapiMacInit),
|
||||
DECLARE_NAPI_FUNCTION("update", NapiMacUpdate),
|
||||
DECLARE_NAPI_FUNCTION("doFinal", NapiMacDoFinal),
|
||||
DECLARE_NAPI_FUNCTION("getMacLength", NapiGetMacLength),
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "Mac", NAPI_AUTO_LENGTH, MacConstructor, nullptr,
|
||||
sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
|
||||
} // CryptoFramework
|
||||
} // OHOS
|
390
frameworks/js/napi/src/napi_md.cpp
Normal file
390
frameworks/js/napi/src/napi_md.cpp
Normal file
@ -0,0 +1,390 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_md.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
|
||||
#include "napi_utils.h"
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
napi_ref NapiMd::classRef_ = nullptr;
|
||||
|
||||
struct MdCtx {
|
||||
napi_env env = nullptr;
|
||||
|
||||
CfAsyncType asyncType = ASYNC_TYPE_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_value promise = nullptr;
|
||||
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
NapiMd *mdClass = nullptr;
|
||||
std::string algoName = "";
|
||||
HcfBlob *inBlob = nullptr;
|
||||
|
||||
HcfResult errCode = HCF_SUCCESS;
|
||||
const char *errMsg = nullptr;
|
||||
HcfBlob *outBlob = nullptr;
|
||||
};
|
||||
|
||||
static void FreeCryptoFwkCtx(napi_env env, MdCtx *context)
|
||||
{
|
||||
if (context == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (context->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, context->asyncWork);
|
||||
context->asyncWork = nullptr;
|
||||
}
|
||||
if (context->callback != nullptr) {
|
||||
napi_delete_reference(env, context->callback);
|
||||
context->callback = nullptr;
|
||||
}
|
||||
if (context->inBlob != nullptr) {
|
||||
HcfFree(context->inBlob->data);
|
||||
context->inBlob->data = nullptr;
|
||||
context->inBlob->len = 0;
|
||||
}
|
||||
if (context->outBlob != nullptr) {
|
||||
HcfFree(context->outBlob->data);
|
||||
context->outBlob->data = nullptr;
|
||||
context->outBlob->len = 0;
|
||||
}
|
||||
HcfFree(context);
|
||||
}
|
||||
|
||||
static void ReturnCallbackResult(napi_env env, MdCtx *context, napi_value result)
|
||||
{
|
||||
napi_value businessError = GenerateBusinessError(env, context->errCode, context->errMsg);
|
||||
napi_value params[ARGS_SIZE_TWO] = { businessError, result };
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, context->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnPromiseResult(napi_env env, MdCtx *context, napi_value result)
|
||||
{
|
||||
if (context->errCode == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, context->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg));
|
||||
}
|
||||
}
|
||||
|
||||
static bool CreateCallbackAndPromise(napi_env env, MdCtx *context, size_t argc,
|
||||
size_t maxCount, napi_value callbackValue)
|
||||
{
|
||||
context->asyncType = (argc == maxCount) ? ASYNC_TYPE_CALLBACK : ASYNC_TYPE_PROMISE;
|
||||
if (context->asyncType == ASYNC_TYPE_CALLBACK) {
|
||||
if (!GetCallbackFromJSParams(env, callbackValue, &context->callback)) {
|
||||
LOGE("get callback failed!");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
napi_create_promise(env, &context->deferred, &context->promise);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
NapiMd::NapiMd(HcfMd *mdObj)
|
||||
{
|
||||
this->mdObj_ = mdObj;
|
||||
}
|
||||
|
||||
NapiMd::~NapiMd()
|
||||
{
|
||||
OH_HCF_ObjDestroy(this->mdObj_);
|
||||
}
|
||||
|
||||
static void MdUpdateExecute(napi_env env, void *data)
|
||||
{
|
||||
MdCtx *context = static_cast<MdCtx *>(data);
|
||||
NapiMd *mdClass = context->mdClass;
|
||||
HcfMd *mdObj = mdClass->GetMd();
|
||||
context->errCode = mdObj->update(mdObj, context->inBlob);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
LOGE("update failed!");
|
||||
context->errMsg = "update failed";
|
||||
}
|
||||
}
|
||||
|
||||
static void MdUpdateComplete(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
MdCtx *context = static_cast<MdCtx *>(data);
|
||||
napi_value nullInstance = nullptr;
|
||||
napi_get_null(env, &nullInstance);
|
||||
if (context->asyncType == ASYNC_TYPE_CALLBACK) {
|
||||
ReturnCallbackResult(env, context, nullInstance);
|
||||
} else {
|
||||
ReturnPromiseResult(env, context, nullInstance);
|
||||
}
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
}
|
||||
|
||||
static void MdDoFinalExecute(napi_env env, void *data)
|
||||
{
|
||||
MdCtx *context = static_cast<MdCtx *>(data);
|
||||
NapiMd *mdClass = context->mdClass;
|
||||
HcfMd *mdObj = mdClass->GetMd();
|
||||
HcfBlob *outBlob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0);
|
||||
if (outBlob == nullptr) {
|
||||
LOGE("outBlob is null!");
|
||||
context->errCode = HCF_ERR_MALLOC;
|
||||
context->errMsg = "malloc data blob failed";
|
||||
return;
|
||||
}
|
||||
context->errCode = mdObj->doFinal(mdObj, outBlob);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
LOGE("doFinal failed!");
|
||||
context->errMsg = "doFinal failed";
|
||||
return;
|
||||
}
|
||||
context->outBlob = outBlob;
|
||||
}
|
||||
|
||||
static void MdDoFinalComplete(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
MdCtx *context = static_cast<MdCtx *>(data);
|
||||
napi_value returnOutBlob = ConvertBlobToNapiValue(env, context->outBlob);
|
||||
if (returnOutBlob == nullptr) {
|
||||
LOGE("returnOutBlob is nullptr!");
|
||||
returnOutBlob = NapiGetNull(env);
|
||||
}
|
||||
if (context->asyncType == ASYNC_TYPE_CALLBACK) {
|
||||
ReturnCallbackResult(env, context, returnOutBlob);
|
||||
} else {
|
||||
ReturnPromiseResult(env, context, returnOutBlob);
|
||||
}
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
}
|
||||
|
||||
napi_value NapiMd::MdUpdate(napi_env env, napi_callback_info info)
|
||||
{
|
||||
// check param count
|
||||
size_t argc = ARGS_SIZE_TWO;
|
||||
napi_value argv[ARGS_SIZE_TWO] = { 0 };
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) {
|
||||
return nullptr;
|
||||
}
|
||||
MdCtx *context = (MdCtx *)HcfMalloc(sizeof(MdCtx), 0);
|
||||
if (context == nullptr) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed"));
|
||||
LOGE("malloc context failed!");
|
||||
return nullptr;
|
||||
}
|
||||
context->mdClass = this;
|
||||
context->inBlob = GetBlobFromNapiValue(env, argv[PARAM0]);
|
||||
if (context->inBlob == nullptr) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "inBlob is null"));
|
||||
LOGE("inBlob is null!");
|
||||
return nullptr;
|
||||
}
|
||||
if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_TWO, argv[PARAM1])) {
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
napi_create_async_work(
|
||||
env, nullptr, GetResourceName(env, "MdUpdate"),
|
||||
MdUpdateExecute,
|
||||
MdUpdateComplete,
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_TYPE_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
}
|
||||
|
||||
napi_value NapiMd::MdDoFinal(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t expectedArgsCount = ARGS_SIZE_ONE;
|
||||
size_t argc = expectedArgsCount;
|
||||
napi_value argv[ARGS_SIZE_ONE] = { 0 };
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) {
|
||||
return nullptr;
|
||||
}
|
||||
MdCtx *context = (MdCtx *)HcfMalloc(sizeof(MdCtx), 0);
|
||||
if (context == nullptr) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed"));
|
||||
LOGE("malloc context failed!");
|
||||
return nullptr;
|
||||
}
|
||||
context->mdClass = this;
|
||||
if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_ONE, argv[PARAM0])) {
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
napi_create_async_work(
|
||||
env, nullptr, GetResourceName(env, "MdDoFinal"),
|
||||
MdDoFinalExecute,
|
||||
MdDoFinalComplete,
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_TYPE_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
}
|
||||
|
||||
napi_value NapiMd::GetMdLength(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t expectedArgsCount = ARGS_SIZE_ZERO;
|
||||
size_t argc = expectedArgsCount;
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
|
||||
if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) {
|
||||
return nullptr;
|
||||
}
|
||||
HcfMd *mdObj = GetMd();
|
||||
uint32_t retLen = mdObj->getMdLength(mdObj);
|
||||
napi_value napiLen = nullptr;
|
||||
napi_create_uint32(env, retLen, &napiLen);
|
||||
return napiLen;
|
||||
}
|
||||
|
||||
static napi_value NapiMdUpdate(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter NapiMdUpdate ...");
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiMd *mdObj = nullptr;
|
||||
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&mdObj));
|
||||
if (mdObj == nullptr) {
|
||||
LOGE("mdObj is nullptr!");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
return mdObj->MdUpdate(env, info);
|
||||
}
|
||||
|
||||
static napi_value NapiMdDoFinal(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter NapiMdDoFinal ...");
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiMd *mdObj = nullptr;
|
||||
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&mdObj));
|
||||
if (mdObj == nullptr) {
|
||||
LOGE("mdObj is nullptr!");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
return mdObj->MdDoFinal(env, info);
|
||||
}
|
||||
|
||||
static napi_value NapiGetMdLength(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter NapiGetMdLength ...");
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiMd *mdObj = nullptr;
|
||||
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&mdObj));
|
||||
if (mdObj == nullptr) {
|
||||
LOGE("mdObj is nullptr!");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
return mdObj->GetMdLength(env, info);
|
||||
}
|
||||
|
||||
napi_value NapiMd::MdConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
printf("enter MdConstructor...");
|
||||
napi_value thisVar = nullptr;
|
||||
size_t exceptedArgc = ARGS_SIZE_ONE;
|
||||
size_t argc = exceptedArgc;
|
||||
napi_value argv[ARGS_SIZE_ONE] = { 0 };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
std::string algoName;
|
||||
if (!GetStringFromJSParams(env, argv[PARAM0], algoName)) {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
HcfMd *mdObj = nullptr;
|
||||
int32_t res = HcfMdCreate(algoName.c_str(), &mdObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("create c mdObj failed.");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
NapiMd *mdNapiObj = new NapiMd(mdObj);
|
||||
napi_wrap(
|
||||
env, thisVar, mdNapiObj,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
NapiMd *md = (NapiMd *)(data);
|
||||
delete md;
|
||||
return;
|
||||
},
|
||||
nullptr,
|
||||
nullptr);
|
||||
napi_value napiAlgName = nullptr;
|
||||
napi_create_string_utf8(env, algoName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName);
|
||||
napi_set_named_property(env, thisVar, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName);
|
||||
printf("out MdConstructor...");
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiMd::CreateMd(napi_env env, napi_callback_info info)
|
||||
{
|
||||
printf("enter CreateMd...");
|
||||
size_t exceptedArgc = ARGS_SIZE_ONE;
|
||||
size_t argc;
|
||||
napi_value argv[ARGS_SIZE_ONE] = { 0 };
|
||||
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
if (argc != exceptedArgc) {
|
||||
LOGE("The input args num is invalid.");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
napi_value instance;
|
||||
napi_value constructor = nullptr;
|
||||
napi_get_reference_value(env, classRef_, &constructor);
|
||||
napi_new_instance(env, constructor, argc, argv, &instance);
|
||||
printf("out CreateMd...");
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NapiMd::DefineMdJSClass(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("createMd", CreateMd),
|
||||
};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
napi_property_descriptor classDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("update", NapiMdUpdate),
|
||||
DECLARE_NAPI_FUNCTION("digest", NapiMdDoFinal),
|
||||
DECLARE_NAPI_FUNCTION("getMdLength", NapiGetMdLength),
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "Md", NAPI_AUTO_LENGTH, MdConstructor, nullptr,
|
||||
sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
|
||||
} // CryptoFramework
|
||||
} // OHOS
|
122
frameworks/js/napi/src/napi_pri_key.cpp
Normal file
122
frameworks/js/napi/src/napi_pri_key.cpp
Normal file
@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_pri_key.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
#include "napi_utils.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
thread_local napi_ref NapiPriKey::classRef_ = nullptr;
|
||||
|
||||
NapiPriKey::NapiPriKey(HcfPriKey *priKey)
|
||||
{
|
||||
this->priKey_ = priKey;
|
||||
}
|
||||
|
||||
NapiPriKey::~NapiPriKey() {}
|
||||
|
||||
HcfPriKey *NapiPriKey::GetPriKey()
|
||||
{
|
||||
return this->priKey_;
|
||||
}
|
||||
|
||||
napi_value NapiPriKey::PriKeyConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
|
||||
LOGI("out ...");
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiPriKey::ConvertToJsPriKey(napi_env env)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
|
||||
napi_value instance;
|
||||
napi_value constructor = nullptr;
|
||||
napi_get_reference_value(env, classRef_, &constructor);
|
||||
napi_new_instance(env, constructor, 0, nullptr, &instance);
|
||||
|
||||
const char *algName = this->priKey_->base.getAlgorithm(&(this->priKey_->base));
|
||||
const char *format = this->priKey_->base.getFormat(&(this->priKey_->base));
|
||||
|
||||
napi_value napiAlgName = nullptr;
|
||||
napi_create_string_utf8(env, algName, NAPI_AUTO_LENGTH, &napiAlgName);
|
||||
napi_set_named_property(env, instance, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName);
|
||||
|
||||
napi_value napiFormat = nullptr;
|
||||
napi_create_string_utf8(env, format, NAPI_AUTO_LENGTH, &napiFormat);
|
||||
napi_set_named_property(env, instance, CRYPTO_TAG_FORMAT.c_str(), napiFormat);
|
||||
|
||||
LOGI("out ...");
|
||||
return instance;
|
||||
}
|
||||
|
||||
napi_value NapiPriKey::JsGetEncoded(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiPriKey *napiPriKey = nullptr;
|
||||
napi_unwrap(env, thisVar, (void **)(&napiPriKey));
|
||||
|
||||
HcfPriKey *priKey = napiPriKey->GetPriKey();
|
||||
|
||||
HcfBlob returnBlob;
|
||||
HcfResult res = priKey->base.getEncoded(&priKey->base, &returnBlob);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("c getEncoded fail.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return ConvertBlobToNapiValue(env, &returnBlob);
|
||||
}
|
||||
|
||||
napi_value NapiPriKey::JsClearMem(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiPriKey *napiPriKey = nullptr;
|
||||
napi_unwrap(env, thisVar, (void **)(&napiPriKey));
|
||||
|
||||
HcfPriKey *priKey = napiPriKey->GetPriKey();
|
||||
|
||||
priKey->clearMem(priKey);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void NapiPriKey::DefinePriKeyJSClass(napi_env env)
|
||||
{
|
||||
napi_property_descriptor classDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("getEncoded", NapiPriKey::JsGetEncoded),
|
||||
DECLARE_NAPI_FUNCTION("clearMem", NapiPriKey::JsClearMem),
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "PriKey", NAPI_AUTO_LENGTH, NapiPriKey::PriKeyConstructor, nullptr,
|
||||
sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
|
||||
} // CryptoFramework
|
||||
} // OHOS
|
107
frameworks/js/napi/src/napi_pub_key.cpp
Normal file
107
frameworks/js/napi/src/napi_pub_key.cpp
Normal file
@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_pub_key.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
|
||||
#include "napi_utils.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
thread_local napi_ref NapiPubKey::classRef_ = nullptr;
|
||||
|
||||
NapiPubKey::NapiPubKey(HcfPubKey *pubKey)
|
||||
{
|
||||
this->pubKey_ = pubKey;
|
||||
}
|
||||
|
||||
NapiPubKey::~NapiPubKey() {}
|
||||
|
||||
HcfPubKey *NapiPubKey::GetPubKey()
|
||||
{
|
||||
return this->pubKey_;
|
||||
}
|
||||
|
||||
napi_value NapiPubKey::PubKeyConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
|
||||
LOGI("out ...");
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiPubKey::ConvertToJsPubKey(napi_env env)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
|
||||
napi_value instance;
|
||||
napi_value constructor = nullptr;
|
||||
napi_get_reference_value(env, classRef_, &constructor);
|
||||
napi_new_instance(env, constructor, 0, nullptr, &instance);
|
||||
|
||||
const char *algName = this->pubKey_->base.getAlgorithm(&(this->pubKey_->base));
|
||||
const char *format = this->pubKey_->base.getFormat(&(this->pubKey_->base));
|
||||
|
||||
napi_value napiAlgName = nullptr;
|
||||
napi_create_string_utf8(env, algName, NAPI_AUTO_LENGTH, &napiAlgName);
|
||||
napi_set_named_property(env, instance, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName);
|
||||
|
||||
napi_value napiFormat = nullptr;
|
||||
napi_create_string_utf8(env, format, NAPI_AUTO_LENGTH, &napiFormat);
|
||||
napi_set_named_property(env, instance, CRYPTO_TAG_FORMAT.c_str(), napiFormat);
|
||||
|
||||
LOGI("out ...");
|
||||
return instance;
|
||||
}
|
||||
|
||||
napi_value NapiPubKey::JsGetEncoded(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiPubKey *napiPubKey = nullptr;
|
||||
napi_unwrap(env, thisVar, (void **)(&napiPubKey));
|
||||
|
||||
HcfPubKey *pubKey = napiPubKey->GetPubKey();
|
||||
HcfBlob returnBlob;
|
||||
HcfResult res = pubKey->base.getEncoded(&pubKey->base, &returnBlob);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("c getEncoded fail.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return ConvertBlobToNapiValue(env, &returnBlob);
|
||||
}
|
||||
|
||||
void NapiPubKey::DefinePubKeyJSClass(napi_env env)
|
||||
{
|
||||
napi_property_descriptor classDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("getEncoded", NapiPubKey::JsGetEncoded),
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "PubKey", NAPI_AUTO_LENGTH, NapiPubKey::PubKeyConstructor, nullptr,
|
||||
sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
|
||||
} // CryptoFramework
|
||||
} // OHOS
|
365
frameworks/js/napi/src/napi_rand.cpp
Normal file
365
frameworks/js/napi/src/napi_rand.cpp
Normal file
@ -0,0 +1,365 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_rand.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
|
||||
#include "napi_utils.h"
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
napi_ref NapiRand::classRef_ = nullptr;
|
||||
|
||||
struct RandCtx {
|
||||
napi_env env = nullptr;
|
||||
|
||||
CfAsyncType asyncType = ASYNC_TYPE_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_value promise = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
NapiRand *randClass = nullptr;
|
||||
uint32_t numBytes = 0;
|
||||
HcfBlob *seedBlob = nullptr;
|
||||
|
||||
HcfResult errCode = HCF_SUCCESS;
|
||||
const char *errMsg = nullptr;
|
||||
HcfBlob *randBlob = nullptr;
|
||||
};
|
||||
|
||||
static void FreeCryptoFwkCtx(napi_env env, RandCtx *context)
|
||||
{
|
||||
if (context == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (context->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, context->asyncWork);
|
||||
context->asyncWork = nullptr;
|
||||
}
|
||||
|
||||
if (context->callback != nullptr) {
|
||||
napi_delete_reference(env, context->callback);
|
||||
context->callback = nullptr;
|
||||
}
|
||||
if (context->seedBlob != nullptr) {
|
||||
HcfFree(context->seedBlob->data);
|
||||
context->seedBlob->data = nullptr;
|
||||
context->seedBlob->len = 0;
|
||||
}
|
||||
if (context->randBlob != nullptr) {
|
||||
HcfFree(context->randBlob->data);
|
||||
context->randBlob->data = nullptr;
|
||||
context->randBlob->len = 0;
|
||||
}
|
||||
HcfFree(context);
|
||||
}
|
||||
|
||||
static void ReturnCallbackResult(napi_env env, RandCtx *context, napi_value result)
|
||||
{
|
||||
napi_value businessError = GenerateBusinessError(env, context->errCode, context->errMsg);
|
||||
|
||||
napi_value params[ARGS_SIZE_TWO] = { businessError, result };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, context->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnPromiseResult(napi_env env, RandCtx *context, napi_value result)
|
||||
{
|
||||
if (context->errCode == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, context->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg));
|
||||
}
|
||||
}
|
||||
|
||||
static bool CreateCallbackAndPromise(napi_env env, RandCtx *context, size_t argc,
|
||||
size_t maxCount, napi_value callbackValue)
|
||||
{
|
||||
context->asyncType = (argc == maxCount) ? ASYNC_TYPE_CALLBACK : ASYNC_TYPE_PROMISE;
|
||||
if (context->asyncType == ASYNC_TYPE_CALLBACK) {
|
||||
if (!GetCallbackFromJSParams(env, callbackValue, &context->callback)) {
|
||||
LOGE("get callback failed!");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
napi_create_promise(env, &context->deferred, &context->promise);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
NapiRand::NapiRand(HcfRand *randObj)
|
||||
{
|
||||
this->randObj_ = randObj;
|
||||
}
|
||||
|
||||
NapiRand::~NapiRand()
|
||||
{
|
||||
OH_HCF_ObjDestroy(this->randObj_);
|
||||
}
|
||||
|
||||
static void GenerateRandomExecute(napi_env env, void *data)
|
||||
{
|
||||
RandCtx *context = static_cast<RandCtx *>(data);
|
||||
NapiRand *randClass = context->randClass;
|
||||
HcfRand *randObj = randClass->GetRand();
|
||||
HcfBlob *randBlob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0);
|
||||
if (randBlob == nullptr) {
|
||||
LOGE("randBlob is null!");
|
||||
context->errCode = HCF_ERR_MALLOC;
|
||||
context->errMsg = "malloc data blob failed";
|
||||
return;
|
||||
}
|
||||
uint32_t numBytes = context->numBytes;
|
||||
context->errCode = randObj->generateRandom(randObj, numBytes, randBlob);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
LOGE("generateRandom failed!");
|
||||
context->errMsg = "generateRandom failed";
|
||||
return;
|
||||
}
|
||||
context->randBlob = randBlob;
|
||||
}
|
||||
|
||||
static void GenerateRandomComplete(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
RandCtx *context = static_cast<RandCtx *>(data);
|
||||
napi_value returnRandBlob = ConvertBlobToNapiValue(env, context->randBlob);
|
||||
if (returnRandBlob == nullptr) {
|
||||
LOGE("returnOutBlob is nullptr!");
|
||||
returnRandBlob = NapiGetNull(env);
|
||||
}
|
||||
if (context->asyncType == ASYNC_TYPE_CALLBACK) {
|
||||
ReturnCallbackResult(env, context, returnRandBlob);
|
||||
} else {
|
||||
ReturnPromiseResult(env, context, returnRandBlob);
|
||||
}
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
}
|
||||
|
||||
static void SetSeedExecute(napi_env env, void *data)
|
||||
{
|
||||
RandCtx *context = static_cast<RandCtx *>(data);
|
||||
NapiRand *randClass = context->randClass;
|
||||
HcfRand *randObj = randClass->GetRand();
|
||||
HcfBlob *seedBlob = static_cast<HcfBlob *>(context->seedBlob);
|
||||
context->errCode = randObj->setSeed(randObj, seedBlob);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
LOGE("setSeed failed!");
|
||||
context->errMsg = "setSeed failed";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void SetSeedComplete(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
LOGI("enter SetSeedComplete ...");
|
||||
RandCtx *context = static_cast<RandCtx *>(data);
|
||||
napi_value nullInstance = nullptr;
|
||||
napi_get_null(env, &nullInstance);
|
||||
if (context->asyncType == ASYNC_TYPE_CALLBACK) {
|
||||
ReturnCallbackResult(env, context, nullInstance);
|
||||
} else {
|
||||
ReturnPromiseResult(env, context, nullInstance);
|
||||
}
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
}
|
||||
|
||||
napi_value NapiRand::GenerateRandom(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t expectedArgsCount = ARGS_SIZE_TWO;
|
||||
size_t argc = expectedArgsCount;
|
||||
napi_value argv[ARGS_SIZE_TWO] = { 0 };
|
||||
napi_value thisVar = nullptr;
|
||||
napi_value ret = NapiGetNull(env);
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if ((argc != expectedArgsCount) && (argc != expectedArgsCount - CALLBACK_SIZE)) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count"));
|
||||
LOGE("The arguments count is not expected!");
|
||||
return ret;
|
||||
}
|
||||
RandCtx *context = (RandCtx *)HcfMalloc(sizeof(RandCtx), 0);
|
||||
if (context == nullptr) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed"));
|
||||
LOGE("malloc context failed!");
|
||||
return ret;
|
||||
}
|
||||
context->randClass = this;
|
||||
if (!GetUint32FromJSParams(env, argv[PARAM0], context->numBytes)) {
|
||||
LOGE("get numBytes failed!");
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return ret;
|
||||
}
|
||||
if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_TWO, argv[PARAM1])) {
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
napi_create_async_work(
|
||||
env, nullptr, GetResourceName(env, "GenerateRandom"),
|
||||
GenerateRandomExecute,
|
||||
GenerateRandomComplete,
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_TYPE_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
}
|
||||
|
||||
napi_value NapiRand::SetSeed(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t expectedArgsCount = ARGS_SIZE_TWO;
|
||||
size_t argc = expectedArgsCount;
|
||||
napi_value argv[ARGS_SIZE_TWO] = { 0 };
|
||||
napi_value thisVar = nullptr;
|
||||
napi_value ret = NapiGetNull(env);
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if ((argc != expectedArgsCount) && (argc != expectedArgsCount - CALLBACK_SIZE)) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count"));
|
||||
LOGE("The arguments count is not expected!");
|
||||
return ret;
|
||||
}
|
||||
RandCtx *context = (RandCtx *)HcfMalloc(sizeof(RandCtx), 0);
|
||||
if (context == nullptr) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed"));
|
||||
LOGE("malloc context failed!");
|
||||
return ret;
|
||||
}
|
||||
context->randClass = this;
|
||||
context->randBlob = GetBlobFromNapiValue(env, argv[PARAM0]);
|
||||
if (context->randBlob == nullptr) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "randBlob is null"));
|
||||
LOGE("randBlob is null!");
|
||||
return ret;
|
||||
}
|
||||
if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_TWO, argv[PARAM1])) {
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
napi_create_async_work(
|
||||
env, nullptr, GetResourceName(env, "SetSeed"),
|
||||
SetSeedExecute,
|
||||
SetSeedComplete,
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_TYPE_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
}
|
||||
|
||||
static napi_value NapiGenerateRandom(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter NapiCreateRand ...");
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiRand *randObj = nullptr;
|
||||
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&randObj));
|
||||
if (randObj == nullptr) {
|
||||
LOGE("randObj is nullptr!");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
return randObj->GenerateRandom(env, info);
|
||||
}
|
||||
|
||||
static napi_value NapiSetSeed(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter NapiSetSeed ...");
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiRand *randObj = nullptr;
|
||||
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&randObj));
|
||||
if (randObj == nullptr) {
|
||||
LOGE("randObj is nullptr!");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
return randObj->SetSeed(env, info);
|
||||
}
|
||||
|
||||
napi_value NapiRand::RandConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
printf("enter RandConstructor...");
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
HcfRand *randObj = nullptr;
|
||||
HcfResult res = HcfRandCreate(&randObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("create c rand fail.");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
NapiRand *randNapiObj = new NapiRand(randObj);
|
||||
napi_wrap(
|
||||
env, thisVar, randNapiObj,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
NapiRand *rand = (NapiRand *)(data);
|
||||
delete rand;
|
||||
return;
|
||||
},
|
||||
nullptr,
|
||||
nullptr);
|
||||
printf("out RandConstructor...");
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiRand::CreateRand(napi_env env, napi_callback_info info)
|
||||
{
|
||||
printf("enter CreateRand...");
|
||||
size_t exceptedArgc = ARGS_SIZE_ZERO;
|
||||
size_t argc;
|
||||
napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr);
|
||||
if (argc != exceptedArgc) {
|
||||
LOGE("The input args num is invalid.");
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
napi_value instance;
|
||||
napi_value constructor = nullptr;
|
||||
napi_get_reference_value(env, classRef_, &constructor);
|
||||
napi_new_instance(env, constructor, ARGS_SIZE_ZERO, nullptr, &instance);
|
||||
printf("out CreateRand...");
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NapiRand::DefineRandJSClass(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("createRandom", CreateRand),
|
||||
};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
napi_property_descriptor classDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("generateRandom", NapiGenerateRandom),
|
||||
DECLARE_NAPI_FUNCTION("setSeed", NapiSetSeed),
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "Random", NAPI_AUTO_LENGTH, RandConstructor, nullptr,
|
||||
sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
|
||||
} // CryptoFramework
|
||||
} // OHOS
|
662
frameworks/js/napi/src/napi_sign.cpp
Normal file
662
frameworks/js/napi/src/napi_sign.cpp
Normal file
@ -0,0 +1,662 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_sign.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
#include "napi_pri_key.h"
|
||||
#include "napi_pub_key.h"
|
||||
#include "napi_utils.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
struct SignInitCtx {
|
||||
napi_env env = nullptr;
|
||||
|
||||
AsyncType asyncType = ASYNC_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_value promise = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
HcfSign *sign;
|
||||
HcfParamsSpec *params;
|
||||
HcfPriKey *priKey;
|
||||
|
||||
HcfResult result;
|
||||
};
|
||||
|
||||
struct SignUpdateCtx {
|
||||
napi_env env = nullptr;
|
||||
|
||||
AsyncType asyncType = ASYNC_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_value promise = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
HcfSign *sign;
|
||||
HcfBlob *data;
|
||||
|
||||
HcfResult result;
|
||||
};
|
||||
|
||||
struct SignDoFinalCtx {
|
||||
napi_env env = nullptr;
|
||||
|
||||
AsyncType asyncType = ASYNC_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_value promise = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
HcfSign *sign;
|
||||
HcfBlob *data;
|
||||
|
||||
HcfResult result;
|
||||
HcfBlob returnSignatureData;
|
||||
};
|
||||
|
||||
thread_local napi_ref NapiSign::classRef_ = nullptr;
|
||||
|
||||
static void FreeSignInitCtx(napi_env env, SignInitCtx *ctx)
|
||||
{
|
||||
if (ctx == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, ctx->asyncWork);
|
||||
}
|
||||
|
||||
if (ctx->callback != nullptr) {
|
||||
napi_delete_reference(env, ctx->callback);
|
||||
}
|
||||
|
||||
HcfFree(ctx);
|
||||
}
|
||||
|
||||
static void FreeSignUpdateCtx(napi_env env, SignUpdateCtx *ctx)
|
||||
{
|
||||
if (ctx == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, ctx->asyncWork);
|
||||
}
|
||||
|
||||
if (ctx->callback != nullptr) {
|
||||
napi_delete_reference(env, ctx->callback);
|
||||
}
|
||||
|
||||
HcfBlobDataFree(ctx->data);
|
||||
HcfFree(ctx->data);
|
||||
HcfFree(ctx);
|
||||
}
|
||||
|
||||
static void FreeSignDoFinalCtx(napi_env env, SignDoFinalCtx *ctx)
|
||||
{
|
||||
if (ctx == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, ctx->asyncWork);
|
||||
ctx->asyncWork = nullptr;
|
||||
}
|
||||
|
||||
if (ctx->callback != nullptr) {
|
||||
napi_delete_reference(env, ctx->callback);
|
||||
ctx->callback = nullptr;
|
||||
}
|
||||
|
||||
if (ctx->returnSignatureData.data != nullptr) {
|
||||
HcfFree(ctx->returnSignatureData.data);
|
||||
ctx->returnSignatureData.data = nullptr;
|
||||
ctx->returnSignatureData.len = 0;
|
||||
}
|
||||
|
||||
HcfBlobDataFree(ctx->data);
|
||||
HcfFree(ctx->data);
|
||||
HcfFree(ctx);
|
||||
}
|
||||
|
||||
static bool BuildSignJsInitCtx(napi_env env, napi_callback_info info, SignInitCtx *ctx)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t expectedArgc = PARAMS_NUM_TWO;
|
||||
size_t argc;
|
||||
napi_value argv[PARAMS_NUM_TWO] = { nullptr, nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (argc != expectedArgc && argc != expectedArgc - 1) {
|
||||
LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc);
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error."));
|
||||
return false;
|
||||
}
|
||||
ctx->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
|
||||
|
||||
NapiSign *napiSign = NULL;
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)(&napiSign));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi sign obj.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error."));
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
NapiPriKey *napiPriKey = NULL;
|
||||
status = napi_unwrap(env, argv[index], (void **)(&napiPriKey));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi priKey obj.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PriKey]: param unwarp error."));
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx->sign = napiSign->GetSign();
|
||||
ctx->params = nullptr;
|
||||
ctx->priKey = napiPriKey->GetPriKey();
|
||||
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
napi_create_promise(env, &ctx->deferred, &ctx->promise);
|
||||
return true;
|
||||
} else {
|
||||
return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback);
|
||||
}
|
||||
}
|
||||
|
||||
static bool BuildSignJsUpdateCtx(napi_env env, napi_callback_info info, SignUpdateCtx *ctx)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t expectedArgc = PARAMS_NUM_TWO;
|
||||
size_t argc;
|
||||
napi_value argv[PARAMS_NUM_TWO] = { nullptr, nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if ((argc != expectedArgc) && (argc != expectedArgc - 1)) {
|
||||
LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc);
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error."));
|
||||
return false;
|
||||
}
|
||||
ctx->asyncType = (argc == PARAMS_NUM_TWO) ? ASYNC_CALLBACK : ASYNC_PROMISE;
|
||||
|
||||
NapiSign *napiSign = NULL;
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)(&napiSign));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi sign obj.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error."));
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
HcfBlob *blob = GetBlobFromNapiValue(env, argv[index]);
|
||||
if (blob == nullptr) {
|
||||
LOGE("failed to get data.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Data]: must be of the DataBlob type."));
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx->sign = napiSign->GetSign();
|
||||
ctx->data = blob;
|
||||
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
napi_create_promise(env, &ctx->deferred, &ctx->promise);
|
||||
return true;
|
||||
} else {
|
||||
return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback);
|
||||
}
|
||||
}
|
||||
|
||||
static bool BuildSignJsDoFinalCtx(napi_env env, napi_callback_info info, SignDoFinalCtx *ctx)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t expectedArgc = PARAMS_NUM_TWO;
|
||||
size_t argc;
|
||||
napi_value argv[PARAMS_NUM_TWO] = { nullptr, nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if ((argc != expectedArgc) && (argc != expectedArgc - 1)) {
|
||||
LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc);
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error."));
|
||||
return false;
|
||||
}
|
||||
ctx->asyncType = (argc == PARAMS_NUM_TWO) ? ASYNC_CALLBACK : ASYNC_PROMISE;
|
||||
|
||||
NapiSign *napiSign = nullptr;
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)(&napiSign));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi sign obj.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error."));
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
napi_valuetype valueType;
|
||||
napi_typeof(env, argv[index], &valueType);
|
||||
HcfBlob *data = nullptr;
|
||||
if (valueType != napi_null) {
|
||||
data = GetBlobFromNapiValue(env, argv[index]);
|
||||
if (data == nullptr) {
|
||||
LOGE("failed to get data.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Data]: must be of the DataBlob type."));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
ctx->sign = napiSign->GetSign();
|
||||
ctx->data = data;
|
||||
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
napi_create_promise(env, &ctx->deferred, &ctx->promise);
|
||||
return true;
|
||||
} else {
|
||||
return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback);
|
||||
}
|
||||
}
|
||||
|
||||
static void ReturnInitCallbackResult(napi_env env, SignInitCtx *ctx, napi_value result)
|
||||
{
|
||||
napi_value businessError = nullptr;
|
||||
if (ctx->result != HCF_SUCCESS) {
|
||||
businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str());
|
||||
}
|
||||
|
||||
napi_value params[ARGS_SIZE_ONE] = { businessError };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, ctx->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_ONE, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnInitPromiseResult(napi_env env, SignInitCtx *ctx, napi_value result)
|
||||
{
|
||||
if (ctx->result == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, ctx->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
static void ReturnUpdateCallbackResult(napi_env env, SignUpdateCtx *ctx, napi_value result)
|
||||
{
|
||||
napi_value businessError = nullptr;
|
||||
if (ctx->result != HCF_SUCCESS) {
|
||||
businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str());
|
||||
}
|
||||
|
||||
napi_value params[ARGS_SIZE_ONE] = { businessError };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, ctx->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_ONE, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnUpdatePromiseResult(napi_env env, SignUpdateCtx *ctx, napi_value result)
|
||||
{
|
||||
if (ctx->result == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, ctx->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
static void ReturnDoFinalCallbackResult(napi_env env, SignDoFinalCtx *ctx, napi_value result)
|
||||
{
|
||||
napi_value businessError = nullptr;
|
||||
if (ctx->result != HCF_SUCCESS) {
|
||||
businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str());
|
||||
}
|
||||
|
||||
napi_value params[ARGS_SIZE_TWO] = { businessError, result };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, ctx->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnDoFinalPromiseResult(napi_env env, SignDoFinalCtx *ctx, napi_value result)
|
||||
{
|
||||
if (ctx->result == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, ctx->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
void SignJsInitAsyncWorkProcess(napi_env env, void *data)
|
||||
{
|
||||
SignInitCtx *ctx = static_cast<SignInitCtx *>(data);
|
||||
|
||||
HcfResult res = ctx->sign->init(ctx->sign, ctx->params, ctx->priKey);
|
||||
|
||||
ctx->result = res;
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("sign init fail.");
|
||||
}
|
||||
}
|
||||
|
||||
void SignJsInitAsyncWorkReturn(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
SignInitCtx *ctx = static_cast<SignInitCtx *>(data);
|
||||
|
||||
if (ctx->asyncType == ASYNC_CALLBACK) {
|
||||
ReturnInitCallbackResult(env, ctx, NapiGetNull(env));
|
||||
} else {
|
||||
ReturnInitPromiseResult(env, ctx, NapiGetNull(env));
|
||||
}
|
||||
FreeSignInitCtx(env, ctx);
|
||||
}
|
||||
|
||||
void SignJsUpdateAsyncWorkProcess(napi_env env, void *data)
|
||||
{
|
||||
SignUpdateCtx *ctx = static_cast<SignUpdateCtx *>(data);
|
||||
|
||||
HcfResult res = ctx->sign->update(ctx->sign, ctx->data);
|
||||
|
||||
ctx->result = res;
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("sign update fail.");
|
||||
}
|
||||
}
|
||||
|
||||
void SignJsUpdateAsyncWorkReturn(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
SignUpdateCtx *ctx = static_cast<SignUpdateCtx *>(data);
|
||||
|
||||
if (ctx->asyncType == ASYNC_CALLBACK) {
|
||||
ReturnUpdateCallbackResult(env, ctx, NapiGetNull(env));
|
||||
} else {
|
||||
ReturnUpdatePromiseResult(env, ctx, NapiGetNull(env));
|
||||
}
|
||||
FreeSignUpdateCtx(env, ctx);
|
||||
}
|
||||
|
||||
void SignJsDoFinalAsyncWorkProcess(napi_env env, void *data)
|
||||
{
|
||||
SignDoFinalCtx *ctx = static_cast<SignDoFinalCtx *>(data);
|
||||
|
||||
HcfResult res = ctx->sign->sign(ctx->sign, ctx->data, &ctx->returnSignatureData);
|
||||
|
||||
ctx->result = res;
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("sign doFinal fail.");
|
||||
}
|
||||
}
|
||||
|
||||
void SignJsDoFinalAsyncWorkReturn(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
SignDoFinalCtx *ctx = static_cast<SignDoFinalCtx *>(data);
|
||||
|
||||
napi_value dataBlob = nullptr;
|
||||
if (ctx->result == HCF_SUCCESS) {
|
||||
dataBlob = ConvertBlobToNapiValue(env, &ctx->returnSignatureData);
|
||||
}
|
||||
|
||||
if (ctx->asyncType == ASYNC_CALLBACK) {
|
||||
ReturnDoFinalCallbackResult(env, ctx, dataBlob);
|
||||
} else {
|
||||
ReturnDoFinalPromiseResult(env, ctx, dataBlob);
|
||||
}
|
||||
FreeSignDoFinalCtx(env, ctx);
|
||||
}
|
||||
|
||||
static napi_value NewSignJsInitAsyncWork(napi_env env, SignInitCtx *ctx)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, "init", NAPI_AUTO_LENGTH, &resourceName);
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, resourceName,
|
||||
[](napi_env env, void *data) {
|
||||
SignJsInitAsyncWorkProcess(env, data);
|
||||
return;
|
||||
},
|
||||
[](napi_env env, napi_status status, void *data) {
|
||||
SignJsInitAsyncWorkReturn(env, status, data);
|
||||
return;
|
||||
},
|
||||
(void *)ctx,
|
||||
&ctx->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, ctx->asyncWork);
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
return ctx->promise;
|
||||
} else {
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
static napi_value NewSignJsUpdateAsyncWork(napi_env env, SignUpdateCtx *ctx)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, "update", NAPI_AUTO_LENGTH, &resourceName);
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, resourceName,
|
||||
[](napi_env env, void *data) {
|
||||
SignJsUpdateAsyncWorkProcess(env, data);
|
||||
return;
|
||||
},
|
||||
[](napi_env env, napi_status status, void *data) {
|
||||
SignJsUpdateAsyncWorkReturn(env, status, data);
|
||||
return;
|
||||
},
|
||||
(void *)ctx,
|
||||
&ctx->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, ctx->asyncWork);
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
return ctx->promise;
|
||||
} else {
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
static napi_value NewSignJsDoFinalAsyncWork(napi_env env, SignDoFinalCtx *ctx)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, "sign", NAPI_AUTO_LENGTH, &resourceName);
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, resourceName,
|
||||
[](napi_env env, void *data) {
|
||||
SignJsDoFinalAsyncWorkProcess(env, data);
|
||||
return;
|
||||
},
|
||||
[](napi_env env, napi_status status, void *data) {
|
||||
SignJsDoFinalAsyncWorkReturn(env, status, data);
|
||||
return;
|
||||
},
|
||||
(void *)ctx,
|
||||
&ctx->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, ctx->asyncWork);
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
return ctx->promise;
|
||||
} else {
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
NapiSign::NapiSign(HcfSign *sign)
|
||||
{
|
||||
this->sign_ = sign;
|
||||
}
|
||||
|
||||
NapiSign::~NapiSign()
|
||||
{
|
||||
OH_HCF_ObjDestroy(this->sign_);
|
||||
}
|
||||
|
||||
HcfSign *NapiSign::GetSign()
|
||||
{
|
||||
return this->sign_;
|
||||
}
|
||||
|
||||
napi_value NapiSign::JsInit(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
SignInitCtx *ctx = (SignInitCtx *)HcfMalloc(sizeof(SignInitCtx), 0);
|
||||
if (ctx == NULL) {
|
||||
LOGE("create context fail.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!BuildSignJsInitCtx(env, info, ctx)) {
|
||||
LOGE("build context fail.");
|
||||
FreeSignInitCtx(env, ctx);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NewSignJsInitAsyncWork(env, ctx);
|
||||
}
|
||||
|
||||
napi_value NapiSign::JsUpdate(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
SignUpdateCtx *ctx = (SignUpdateCtx *)HcfMalloc(sizeof(SignUpdateCtx), 0);
|
||||
if (ctx == NULL) {
|
||||
LOGE("create context fail.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!BuildSignJsUpdateCtx(env, info, ctx)) {
|
||||
LOGE("build context fail.");
|
||||
FreeSignUpdateCtx(env, ctx);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NewSignJsUpdateAsyncWork(env, ctx);
|
||||
}
|
||||
|
||||
napi_value NapiSign::JsSign(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
SignDoFinalCtx *ctx = (SignDoFinalCtx *)HcfMalloc(sizeof(SignDoFinalCtx), 0);
|
||||
if (ctx == NULL) {
|
||||
LOGE("create context fail.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!BuildSignJsDoFinalCtx(env, info, ctx)) {
|
||||
LOGE("build context fail.");
|
||||
FreeSignDoFinalCtx(env, ctx);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NewSignJsDoFinalAsyncWork(env, ctx);
|
||||
}
|
||||
|
||||
napi_value NapiSign::SignConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
|
||||
LOGI("out ...");
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiSign::CreateJsSign(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
size_t exceptedArgc = PARAMS_NUM_ONE;
|
||||
size_t argc;
|
||||
napi_value argv[PARAMS_NUM_ONE] = { nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
|
||||
if (argc != exceptedArgc) {
|
||||
LOGE("The input args num is invalid.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value instance;
|
||||
napi_value constructor = nullptr;
|
||||
napi_get_reference_value(env, classRef_, &constructor);
|
||||
napi_new_instance(env, constructor, argc, argv, &instance);
|
||||
|
||||
std::string algName;
|
||||
if (!GetStringFromJSParams(env, argv[0], algName)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
HcfSign *sign = NULL;
|
||||
int32_t res = HcfSignCreate(algName.c_str(), &sign);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("create c sign fail.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
NapiSign *napiSign = new NapiSign(sign);
|
||||
|
||||
napi_wrap(
|
||||
env, instance, napiSign,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
NapiSign *napiSign = (NapiSign *)(data);
|
||||
delete napiSign;
|
||||
return;
|
||||
},
|
||||
nullptr,
|
||||
nullptr);
|
||||
|
||||
napi_value napiAlgName = nullptr;
|
||||
napi_create_string_utf8(env, algName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName);
|
||||
napi_set_named_property(env, instance, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName);
|
||||
|
||||
LOGI("out ...");
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NapiSign::DefineSignJSClass(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("createSign", NapiSign::CreateJsSign),
|
||||
};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
|
||||
napi_property_descriptor classDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("init", NapiSign::JsInit),
|
||||
DECLARE_NAPI_FUNCTION("update", NapiSign::JsUpdate),
|
||||
DECLARE_NAPI_FUNCTION("sign", NapiSign::JsSign),
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "Sign", NAPI_AUTO_LENGTH, NapiSign::SignConstructor, nullptr,
|
||||
sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
} // CryptoFramework
|
||||
} // OHOS
|
126
frameworks/js/napi/src/napi_sym_key.cpp
Normal file
126
frameworks/js/napi/src/napi_sym_key.cpp
Normal file
@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_sym_key.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
|
||||
#include "napi_utils.h"
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
thread_local napi_ref NapiSymKey::classRef_ = nullptr;
|
||||
|
||||
NapiSymKey::NapiSymKey(HcfSymKey *symKey)
|
||||
{
|
||||
this->symKey_ = symKey;
|
||||
}
|
||||
|
||||
NapiSymKey::~NapiSymKey()
|
||||
{
|
||||
OH_HCF_ObjDestroy(this->symKey_);
|
||||
}
|
||||
|
||||
HcfSymKey *NapiSymKey::GetSymKey()
|
||||
{
|
||||
return this->symKey_;
|
||||
}
|
||||
|
||||
napi_value NapiSymKey::JsGetAlgorithm(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
NapiSymKey *napiSymKey = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
||||
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&napiSymKey));
|
||||
HcfSymKey *key = napiSymKey->GetSymKey();
|
||||
|
||||
const char *algo = key->key.getAlgorithm((HcfKey *)key);
|
||||
napi_value instance = nullptr;
|
||||
napi_create_string_utf8(env, (const char *)algo, NAPI_AUTO_LENGTH, &instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
napi_value NapiSymKey::JsGetFormat(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
NapiSymKey *napiSymKey = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
||||
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&napiSymKey));
|
||||
HcfSymKey *key = napiSymKey->GetSymKey();
|
||||
|
||||
const char *format = key->key.getFormat((HcfKey *)key);
|
||||
napi_value instance = nullptr;
|
||||
napi_create_string_utf8(env, (const char *)format, NAPI_AUTO_LENGTH, &instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
napi_value NapiSymKey::JsGetEncoded(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
NapiSymKey *napiSymKey = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
||||
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&napiSymKey));
|
||||
HcfSymKey *key = napiSymKey->GetSymKey();
|
||||
|
||||
HcfBlob blob = {0};
|
||||
HcfResult res = key->key.getEncoded((HcfKey *)key, &blob);
|
||||
if (res != 0) {
|
||||
napi_throw(env, GenerateBusinessError(env, res, "getEncoded failed."));
|
||||
LOGE("getEncoded failed!");
|
||||
return nullptr;
|
||||
}
|
||||
napi_value instance = ConvertBlobToNapiValue(env, &blob);
|
||||
HcfFree(blob.data);
|
||||
return instance;
|
||||
}
|
||||
|
||||
napi_value NapiSymKey::SymKeyConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiSymKey::CreateSymKey(napi_env env)
|
||||
{
|
||||
napi_value instance = nullptr;
|
||||
napi_value constructor = nullptr;
|
||||
NAPI_CALL(env, napi_get_reference_value(env, classRef_, &constructor));
|
||||
NAPI_CALL(env, napi_new_instance(env, constructor, 0, nullptr, &instance));
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NapiSymKey::DefineSymKeyJSClass(napi_env env)
|
||||
{
|
||||
napi_property_descriptor classDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("getEncoded", NapiSymKey::JsGetEncoded),
|
||||
{.utf8name = "format", .getter = NapiSymKey::JsGetFormat},
|
||||
{.utf8name = "algName", .getter = NapiSymKey::JsGetAlgorithm},
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "SymKey", NAPI_AUTO_LENGTH, SymKeyConstructor, nullptr,
|
||||
sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
|
||||
} // CryptoFramework
|
||||
} // OHOS
|
455
frameworks/js/napi/src/napi_sym_key_generator.cpp
Normal file
455
frameworks/js/napi/src/napi_sym_key_generator.cpp
Normal file
@ -0,0 +1,455 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_sym_key_generator.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
#include "napi_key.h"
|
||||
#include "napi_utils.h"
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
thread_local napi_ref NapiSymKeyGenerator::classRef_ = nullptr;
|
||||
|
||||
struct SymKeyGeneratorFwkCtxT {
|
||||
napi_env env = nullptr;
|
||||
AsyncType asyncType = ASYNC_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_value promise = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
int32_t errCode = 0;
|
||||
HcfSymKey *returnSymKey = nullptr;
|
||||
const char *errMsg = nullptr;
|
||||
|
||||
HcfSymKeyGenerator *generator = nullptr;
|
||||
HcfBlob keyMaterial = { 0 };
|
||||
};
|
||||
|
||||
using SymKeyGeneratorFwkCtx = SymKeyGeneratorFwkCtxT *;
|
||||
|
||||
static void FreeSymKeyGeneratorFwkCtx(napi_env env, SymKeyGeneratorFwkCtx &context)
|
||||
{
|
||||
if (context == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (context->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, context->asyncWork);
|
||||
context->asyncWork = nullptr;
|
||||
}
|
||||
|
||||
if (context->callback != nullptr) {
|
||||
napi_delete_reference(env, context->callback);
|
||||
context->callback = nullptr;
|
||||
}
|
||||
|
||||
if (context->keyMaterial.data != nullptr) {
|
||||
(void)memset_s(context->keyMaterial.data, context->keyMaterial.len, 0, context->keyMaterial.len);
|
||||
HcfFree(context->keyMaterial.data);
|
||||
context->keyMaterial.data = nullptr;
|
||||
context->keyMaterial.len = 0;
|
||||
}
|
||||
context->errMsg = nullptr;
|
||||
|
||||
HcfFree(context);
|
||||
context = nullptr;
|
||||
}
|
||||
|
||||
static bool BuildContextForGenerateKey(napi_env env, napi_callback_info info, SymKeyGeneratorFwkCtx context)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t expectedArgc = ARGS_SIZE_ONE;
|
||||
size_t argc = ARGS_SIZE_ONE;
|
||||
napi_value argv[ARGS_SIZE_ONE] = { nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (argc != expectedArgc && argc != expectedArgc - 1) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate key failed for wrong argument num."));
|
||||
LOGE("wrong argument num. require 0 or 1 arguments. [Argc]: %zu!", argc);
|
||||
return false;
|
||||
}
|
||||
context->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
|
||||
NapiSymKeyGenerator *napiGenerator;
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)(&napiGenerator));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap NapiSymKeyGenerator obj!");
|
||||
return false;
|
||||
}
|
||||
|
||||
context->generator = napiGenerator->GetSymKeyGenerator();
|
||||
if (context->generator == nullptr) {
|
||||
LOGE("failed to get generator obj!");
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
if (context->asyncType == ASYNC_PROMISE) {
|
||||
napi_create_promise(env, &context->deferred, &context->promise);
|
||||
return true;
|
||||
} else {
|
||||
return GetCallbackFromJSParams(env, argv[index], &context->callback);
|
||||
}
|
||||
}
|
||||
|
||||
static bool BuildContextForConvertKey(napi_env env, napi_callback_info info, SymKeyGeneratorFwkCtx context)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t expectedArgc = ARGS_SIZE_TWO;
|
||||
size_t argc = ARGS_SIZE_TWO;
|
||||
napi_value argv[ARGS_SIZE_TWO] = { nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (argc != expectedArgc && argc != expectedArgc - 1) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "convert key failed for wrong argument num."));
|
||||
LOGE("wrong argument num. require 1 or 2 arguments. [Argc]: %zu!", argc);
|
||||
return false;
|
||||
}
|
||||
context->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
|
||||
|
||||
NapiSymKeyGenerator *napiGenerator;
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)(&napiGenerator));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap NapiSymKeyGenerator obj!");
|
||||
return false;
|
||||
}
|
||||
|
||||
context->generator = napiGenerator->GetSymKeyGenerator();
|
||||
if (context->generator == nullptr) {
|
||||
LOGE("failed to get generator obj!");
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
HcfBlob *blob = GetBlobFromNapiValue(env, argv[index++]);
|
||||
if (blob == nullptr) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "convert key failed for invalid input blob."));
|
||||
LOGE("get keyMaterial failed!");
|
||||
return false;
|
||||
}
|
||||
context->keyMaterial = *blob;
|
||||
HcfFree(blob);
|
||||
if (context->asyncType == ASYNC_PROMISE) {
|
||||
napi_create_promise(env, &context->deferred, &context->promise);
|
||||
return true;
|
||||
} else {
|
||||
return GetCallbackFromJSParams(env, argv[index], &context->callback);
|
||||
}
|
||||
}
|
||||
|
||||
static void ReturnPromiseResult(napi_env env, SymKeyGeneratorFwkCtx context, napi_value result)
|
||||
{
|
||||
if (context->errCode == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, context->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg));
|
||||
}
|
||||
}
|
||||
|
||||
static void ReturnCallbackResult(napi_env env, SymKeyGeneratorFwkCtx context, napi_value result)
|
||||
{
|
||||
napi_value businessError = NapiGetNull(env);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
businessError = GenerateBusinessError(env, context->errCode, context->errMsg);
|
||||
}
|
||||
napi_value params[ARGS_SIZE_TWO] = { businessError, result };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, context->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void AsyncGenKeyProcess(napi_env env, void *data)
|
||||
{
|
||||
SymKeyGeneratorFwkCtx context = static_cast<SymKeyGeneratorFwkCtx>(data);
|
||||
HcfSymKeyGenerator *generator = context->generator;
|
||||
|
||||
HcfSymKey *key = NULL;
|
||||
HcfResult res = generator->generateSymKey(generator, &key);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("generate sym key failed.");
|
||||
context->errCode = res;
|
||||
context->errMsg = "generate sym key failed.";
|
||||
return;
|
||||
}
|
||||
|
||||
context->errCode = HCF_SUCCESS;
|
||||
context->returnSymKey = key;
|
||||
}
|
||||
|
||||
static void AsyncKeyReturn(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
napi_value instance = NapiKey::CreateHcfKey(env);
|
||||
SymKeyGeneratorFwkCtx context = static_cast<SymKeyGeneratorFwkCtx>(data);
|
||||
NapiKey *napiKey = new NapiKey((HcfKey *)context->returnSymKey);
|
||||
|
||||
napi_status ret = napi_wrap(env, instance, napiKey,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
NapiKey *napiKey = static_cast<NapiKey *>(data);
|
||||
delete napiKey;
|
||||
return;
|
||||
},
|
||||
nullptr, nullptr);
|
||||
if (ret != napi_ok) {
|
||||
LOGE("failed to wrap napiSymKey obj!");
|
||||
context->errCode = HCF_INVALID_PARAMS;
|
||||
delete napiKey;
|
||||
}
|
||||
|
||||
if (context->asyncType == ASYNC_CALLBACK) {
|
||||
ReturnCallbackResult(env, context, instance);
|
||||
} else {
|
||||
ReturnPromiseResult(env, context, instance);
|
||||
}
|
||||
FreeSymKeyGeneratorFwkCtx(env, context);
|
||||
}
|
||||
|
||||
static void AsyncConvertKeyProcess(napi_env env, void *data)
|
||||
{
|
||||
SymKeyGeneratorFwkCtx context = static_cast<SymKeyGeneratorFwkCtx>(data);
|
||||
HcfSymKeyGenerator *generator = context->generator;
|
||||
|
||||
HcfSymKey *key = NULL;
|
||||
HcfResult res = generator->convertSymKey(generator, &context->keyMaterial, &key);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("convertSymKey key failed!");
|
||||
context->errCode = res;
|
||||
context->errMsg = "convert sym key failed.";
|
||||
return;
|
||||
}
|
||||
|
||||
context->errCode = HCF_SUCCESS;
|
||||
context->returnSymKey = key;
|
||||
}
|
||||
|
||||
static napi_value NewConvertKeyAsyncWork(napi_env env, SymKeyGeneratorFwkCtx context)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, "convertSymKey", NAPI_AUTO_LENGTH, &resourceName);
|
||||
|
||||
napi_create_async_work(
|
||||
env,
|
||||
nullptr,
|
||||
resourceName,
|
||||
[](napi_env env, void *data) {
|
||||
AsyncConvertKeyProcess(env, data);
|
||||
return;
|
||||
},
|
||||
[](napi_env env, napi_status status, void *data) {
|
||||
AsyncKeyReturn(env, status, data);
|
||||
return;
|
||||
},
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
}
|
||||
|
||||
static napi_value NewGenKeyAsyncWork(napi_env env, SymKeyGeneratorFwkCtx context)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, "generatorSymKey", NAPI_AUTO_LENGTH, &resourceName);
|
||||
|
||||
napi_create_async_work(
|
||||
env,
|
||||
nullptr,
|
||||
resourceName,
|
||||
[](napi_env env, void *data) {
|
||||
AsyncGenKeyProcess(env, data);
|
||||
return;
|
||||
},
|
||||
[](napi_env env, napi_status status, void *data) {
|
||||
AsyncKeyReturn(env, status, data);
|
||||
return;
|
||||
},
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
}
|
||||
|
||||
NapiSymKeyGenerator::NapiSymKeyGenerator(HcfSymKeyGenerator *generator)
|
||||
{
|
||||
this->generator_ = generator;
|
||||
}
|
||||
|
||||
NapiSymKeyGenerator::~NapiSymKeyGenerator()
|
||||
{
|
||||
OH_HCF_ObjDestroy(this->generator_);
|
||||
}
|
||||
|
||||
HcfSymKeyGenerator *NapiSymKeyGenerator::GetSymKeyGenerator()
|
||||
{
|
||||
return this->generator_;
|
||||
}
|
||||
|
||||
napi_value NapiSymKeyGenerator::JsGenerateSymKey(napi_env env, napi_callback_info info)
|
||||
{
|
||||
SymKeyGeneratorFwkCtx context = (SymKeyGeneratorFwkCtx)HcfMalloc(sizeof(SymKeyGeneratorFwkCtxT), 0);
|
||||
if (context == nullptr) {
|
||||
LOGE("Create context failed!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!BuildContextForGenerateKey(env, info, context)) {
|
||||
LOGE("Build context fail.");
|
||||
FreeSymKeyGeneratorFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value result = NewGenKeyAsyncWork(env, context);
|
||||
if (result == nullptr) {
|
||||
LOGE("NewGenKeyAsyncWork failed!");
|
||||
FreeSymKeyGeneratorFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value NapiSymKeyGenerator::JsConvertKey(napi_env env, napi_callback_info info)
|
||||
{
|
||||
SymKeyGeneratorFwkCtx context = (SymKeyGeneratorFwkCtx)HcfMalloc(sizeof(SymKeyGeneratorFwkCtxT), 0);
|
||||
if (context == nullptr) {
|
||||
LOGE("malloc SymKeyGeneratorFwkCtx failed!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!BuildContextForConvertKey(env, info, context)) {
|
||||
LOGE("BuildContextForConvertKey failed!");
|
||||
FreeSymKeyGeneratorFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value result = NewConvertKeyAsyncWork(env, context);
|
||||
if (result == nullptr) {
|
||||
LOGE("Get deviceauth async work failed!");
|
||||
FreeSymKeyGeneratorFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value NapiSymKeyGenerator::SymKeyGeneratorConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiSymKeyGenerator::CreateSymKeyGenerator(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t exceptedArgc = ARGS_SIZE_ONE;
|
||||
size_t argc = ARGS_SIZE_ONE;
|
||||
napi_value argv[ARGS_SIZE_ONE] = { 0 };
|
||||
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
|
||||
if (argc != exceptedArgc) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "The input args num is invalid."));
|
||||
LOGE("The input args num is invalid.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value instance;
|
||||
napi_value constructor = nullptr;
|
||||
NAPI_CALL(env, napi_get_reference_value(env, classRef_, &constructor));
|
||||
NAPI_CALL(env, napi_new_instance(env, constructor, argc, argv, &instance));
|
||||
|
||||
std::string algoName;
|
||||
if (!GetStringFromJSParams(env, argv[0], algoName)) {
|
||||
LOGE("failed to get algoName.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
HcfSymKeyGenerator *generator = NULL;
|
||||
int32_t res = HcfSymKeyGeneratorCreate(algoName.c_str(), &generator);
|
||||
if (res != HCF_SUCCESS) {
|
||||
napi_throw(env, GenerateBusinessError(env, res, "create C generator fail."));
|
||||
LOGE("create C generator fail.");
|
||||
return nullptr;
|
||||
}
|
||||
NapiSymKeyGenerator *napiSymKeyGenerator = new NapiSymKeyGenerator(generator);
|
||||
if (napiSymKeyGenerator == nullptr) {
|
||||
LOGE("new napiSymKeyGenerator failed!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_status status = napi_wrap(env, instance, napiSymKeyGenerator,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
NapiSymKeyGenerator *napiSymKeyGenerator = static_cast<NapiSymKeyGenerator *>(data);
|
||||
delete napiSymKeyGenerator;
|
||||
return;
|
||||
},
|
||||
nullptr,
|
||||
nullptr);
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to wrap napiSymKeyGenerator obj!");
|
||||
delete napiSymKeyGenerator;
|
||||
return nullptr;
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
napi_value NapiSymKeyGenerator::JsGetAlgorithm(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
NapiSymKeyGenerator *napiSymKeyGenerator = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
||||
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&napiSymKeyGenerator));
|
||||
HcfSymKeyGenerator *generator = napiSymKeyGenerator->GetSymKeyGenerator();
|
||||
|
||||
const char *algo = generator->getAlgoName(generator);
|
||||
napi_value instance = nullptr;
|
||||
napi_create_string_utf8(env, (const char *)algo, NAPI_AUTO_LENGTH, &instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NapiSymKeyGenerator::DefineSymKeyGeneratorJSClass(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("createSymKeyGenerator", NapiSymKeyGenerator::CreateSymKeyGenerator),
|
||||
};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
|
||||
napi_property_descriptor classDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("generateSymKey", NapiSymKeyGenerator::JsGenerateSymKey),
|
||||
DECLARE_NAPI_FUNCTION("convertKey", NapiSymKeyGenerator::JsConvertKey),
|
||||
{ .utf8name = "algName", .getter = NapiSymKeyGenerator::JsGetAlgorithm },
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "SymKeyGenerator", NAPI_AUTO_LENGTH, NapiSymKeyGenerator::SymKeyGeneratorConstructor,
|
||||
nullptr, sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
|
||||
} // CryptoFramework
|
||||
} // OHOS
|
746
frameworks/js/napi/src/napi_utils.cpp
Normal file
746
frameworks/js/napi/src/napi_utils.cpp
Normal file
@ -0,0 +1,746 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_utils.h"
|
||||
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
#include "securec.h"
|
||||
#include "cipher.h"
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
#include "detailed_iv_params.h"
|
||||
#include "detailed_gcm_params.h"
|
||||
#include "detailed_ccm_params.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
using namespace std;
|
||||
|
||||
napi_value NapiGetNull(napi_env env)
|
||||
{
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value ConvertArrayToNapiValue(napi_env env, HcfArray *array)
|
||||
{
|
||||
if (array == nullptr) {
|
||||
LOGE("array is null!");
|
||||
return nullptr;
|
||||
}
|
||||
if (array->count == 0) {
|
||||
LOGE("array count is 0!");
|
||||
return nullptr;
|
||||
}
|
||||
napi_value returnArray = nullptr;
|
||||
napi_create_array(env, &returnArray);
|
||||
if (returnArray == nullptr) {
|
||||
LOGE("create return array failed!");
|
||||
return nullptr;
|
||||
}
|
||||
for (uint32_t i = 0; i < array->count; i++) {
|
||||
HcfBlob *blob = (HcfBlob *)(array->data + i);
|
||||
napi_value outBuffer = GenerateArrayBuffer(env, blob->data, blob->len);
|
||||
if (outBuffer == nullptr) {
|
||||
LOGE("generate array buffer failed!");
|
||||
return nullptr;
|
||||
}
|
||||
napi_value element = nullptr;
|
||||
napi_create_typedarray(env, napi_uint8_array, blob->len, outBuffer, 0, &element);
|
||||
napi_set_element(env, returnArray, i, element);
|
||||
}
|
||||
napi_value returnValue = nullptr;
|
||||
napi_create_object(env, &returnValue);
|
||||
napi_set_named_property(env, returnValue, CRYPTO_TAG_DATA.c_str(), returnArray);
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
napi_value GenerateArrayBuffer(napi_env env, uint8_t *data, uint32_t size)
|
||||
{
|
||||
uint8_t *buffer = (uint8_t *)HcfMalloc(size, 0);
|
||||
if (buffer == nullptr) {
|
||||
LOGE("malloc uint8 array buffer failed!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (memcpy_s(buffer, size, data, size) != EOK) {
|
||||
LOGE("memcpy_s data to buffer failed!");
|
||||
HcfFree(buffer);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value outBuffer = nullptr;
|
||||
napi_status status = napi_create_external_arraybuffer(
|
||||
env, buffer, size, [](napi_env env, void *data, void *hint) { HcfFree(data); }, nullptr, &outBuffer);
|
||||
if (status != napi_ok) {
|
||||
LOGE("create uint8 array buffer failed!");
|
||||
HcfFree(buffer);
|
||||
return nullptr;
|
||||
}
|
||||
buffer = nullptr;
|
||||
return outBuffer;
|
||||
}
|
||||
|
||||
static bool GetDataOfEncodingBlob(napi_env env, napi_value data, HcfEncodingBlob *encodingBlob)
|
||||
{
|
||||
napi_typedarray_type arrayType;
|
||||
napi_value arrayBuffer = nullptr;
|
||||
size_t length = 0;
|
||||
size_t offset = 0;
|
||||
void *rawData = nullptr;
|
||||
|
||||
napi_status status = napi_get_typedarray_info(env, data, &arrayType, &length,
|
||||
(void **)&rawData, &arrayBuffer, &offset);
|
||||
if (status != napi_ok) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get array data failed"));
|
||||
LOGE("failed to get array data!");
|
||||
return false;
|
||||
}
|
||||
if (arrayType != napi_uint8_array) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "array type is not uint8 array"));
|
||||
LOGE("array is not uint8 array!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (length == 0) {
|
||||
LOGE("input data length is 0");
|
||||
return false;
|
||||
}
|
||||
encodingBlob->data = (uint8_t *)HcfMalloc(length, 0);
|
||||
if (encodingBlob->data == nullptr) {
|
||||
LOGE("malloc encoding blob data failed!");
|
||||
return false;
|
||||
}
|
||||
if (memcpy_s(encodingBlob->data, length, rawData, length) != EOK) {
|
||||
LOGE("memcpy_s encoding blob data failed!");
|
||||
HcfFree(encodingBlob->data);
|
||||
encodingBlob->data = nullptr;
|
||||
return false;
|
||||
}
|
||||
encodingBlob->len = length;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetEncodingBlobFromValue(napi_env env, napi_value obj, HcfEncodingBlob **encodingBlob)
|
||||
{
|
||||
*encodingBlob = (HcfEncodingBlob *)HcfMalloc(sizeof(HcfEncodingBlob), 0);
|
||||
if (*encodingBlob == nullptr) {
|
||||
LOGE("malloc encoding blob failed!");
|
||||
return false;
|
||||
}
|
||||
napi_value data = nullptr;
|
||||
napi_status status = napi_get_named_property(env, obj, CRYPTO_TAG_DATA.c_str(), &data);
|
||||
if (status != napi_ok) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get encoding blob data failed"));
|
||||
LOGE("failed to get encoding blob data!");
|
||||
HcfFree(*encodingBlob);
|
||||
*encodingBlob = nullptr;
|
||||
return false;
|
||||
}
|
||||
if (!GetDataOfEncodingBlob(env, data, *encodingBlob)) {
|
||||
HcfFree(*encodingBlob);
|
||||
*encodingBlob = nullptr;
|
||||
return false;
|
||||
}
|
||||
napi_value format = nullptr;
|
||||
status = napi_get_named_property(env, obj, CRYPTO_TAG_ENCODING_FORMAT.c_str(), &format);
|
||||
if (status != napi_ok) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get encoding blob format failed"));
|
||||
LOGE("failed to get encoding blob format!");
|
||||
HcfFree((*encodingBlob)->data);
|
||||
(*encodingBlob)->data = nullptr;
|
||||
HcfFree(*encodingBlob);
|
||||
*encodingBlob = nullptr;
|
||||
return false;
|
||||
}
|
||||
napi_get_value_uint32(env, format, (uint32_t *)(&(*encodingBlob)->encodingFormat));
|
||||
return true;
|
||||
}
|
||||
|
||||
napi_value ConvertEncodingBlobToNapiValue(napi_env env, HcfEncodingBlob *encodingBlob)
|
||||
{
|
||||
napi_value outBuffer = GenerateArrayBuffer(env, encodingBlob->data, encodingBlob->len);
|
||||
if (outBuffer == nullptr) {
|
||||
LOGE("generate array buffer failed!");
|
||||
return nullptr;
|
||||
}
|
||||
napi_value outData = nullptr;
|
||||
napi_create_typedarray(env, napi_uint8_array, encodingBlob->len, outBuffer, 0, &outData);
|
||||
napi_value encoding = nullptr;
|
||||
napi_create_uint32(env, encodingBlob->encodingFormat, &encoding);
|
||||
napi_value returnEncodingBlob = nullptr;
|
||||
napi_create_object(env, &returnEncodingBlob);
|
||||
napi_set_named_property(env, returnEncodingBlob, CRYPTO_TAG_DATA.c_str(), outData);
|
||||
napi_set_named_property(env, returnEncodingBlob, CRYPTO_TAG_ENCODING_FORMAT.c_str(), encoding);
|
||||
return returnEncodingBlob;
|
||||
}
|
||||
|
||||
HcfBlob *GetBlobFromNapiValue(napi_env env, napi_value arg)
|
||||
{
|
||||
if ((env == nullptr) || (arg == nullptr)) {
|
||||
LOGE("Invalid parmas!");
|
||||
return nullptr;
|
||||
}
|
||||
napi_value data = nullptr;
|
||||
napi_status status = napi_get_named_property(env, arg, CRYPTO_TAG_DATA.c_str(), &data);
|
||||
if ((status != napi_ok) || (data == nullptr)) {
|
||||
LOGE("failed to get valid data property!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
size_t length = 0;
|
||||
size_t offset = 0;
|
||||
void *rawData = nullptr;
|
||||
napi_value arrayBuffer = nullptr;
|
||||
napi_typedarray_type arrayType;
|
||||
// Warning: Do not release the rawData returned by this interface because the rawData is managed by VM.
|
||||
status = napi_get_typedarray_info(env, data, &arrayType, &length, (void **)&rawData, &arrayBuffer, &offset);
|
||||
if ((status != napi_ok) || (length == 0) || (rawData == nullptr)) {
|
||||
LOGE("failed to get valid rawData.");
|
||||
return nullptr;
|
||||
}
|
||||
if (arrayType != napi_uint8_array) {
|
||||
LOGE("input data is not uint8 array.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
HcfBlob *newBlob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0);
|
||||
if (newBlob == NULL) {
|
||||
LOGE("Failed to allocate newBlob memory!");
|
||||
return nullptr;
|
||||
}
|
||||
newBlob->len = length;
|
||||
newBlob->data = (uint8_t *)HcfMalloc(length, 0);
|
||||
if (newBlob->data == nullptr) {
|
||||
LOGE("malloc blob data failed!");
|
||||
HcfFree(newBlob);
|
||||
return nullptr;
|
||||
}
|
||||
if (memcpy_s(newBlob->data, length, rawData, length) != EOK) {
|
||||
LOGE("memcpy_s blob data failed!");
|
||||
HcfFree(newBlob->data);
|
||||
HcfFree(newBlob);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return newBlob;
|
||||
}
|
||||
|
||||
static const char *GetIvParamsSpecType()
|
||||
{
|
||||
return IV_PARAMS_SPEC.c_str();
|
||||
}
|
||||
|
||||
static const char *GetGcmParamsSpecType()
|
||||
{
|
||||
return GCM_PARAMS_SPEC.c_str();
|
||||
}
|
||||
|
||||
static const char *GetCcmParamsSpecType()
|
||||
{
|
||||
return CCM_PARAMS_SPEC.c_str();
|
||||
}
|
||||
|
||||
static HcfBlob *GetBlobFromParamsSpec(napi_env env, napi_value arg, string type)
|
||||
{
|
||||
napi_value data = nullptr;
|
||||
HcfBlob *blob = nullptr;
|
||||
|
||||
napi_status status = napi_get_named_property(env, arg, type.c_str(), &data);
|
||||
if ((status != napi_ok) || (data == nullptr)) {
|
||||
LOGE("failed to get valid data property!");
|
||||
return nullptr;
|
||||
}
|
||||
blob = GetBlobFromNapiValue(env, data);
|
||||
if (blob == nullptr) {
|
||||
LOGE("GetBlobFromNapiValue failed!");
|
||||
return nullptr;
|
||||
}
|
||||
return blob;
|
||||
}
|
||||
|
||||
static bool GetIvParamsSpec(napi_env env, napi_value arg, HcfParamsSpec **paramsSpec)
|
||||
{
|
||||
HcfIvParamsSpec *ivParamsSpec = (HcfIvParamsSpec *)HcfMalloc(sizeof(HcfIvParamsSpec), 0);
|
||||
if (ivParamsSpec == nullptr) {
|
||||
LOGE("ccmParamsSpec malloc failed!");
|
||||
return false;
|
||||
}
|
||||
|
||||
HcfBlob *iv = GetBlobFromParamsSpec(env, arg, IV_PARAMS);
|
||||
if (iv == nullptr) {
|
||||
LOGE("GetBlobFromNapiValue failed!");
|
||||
HcfFree(ivParamsSpec);
|
||||
return false;
|
||||
}
|
||||
ivParamsSpec->base.getType = GetIvParamsSpecType;
|
||||
ivParamsSpec->iv = *iv;
|
||||
*paramsSpec = (HcfParamsSpec *)ivParamsSpec;
|
||||
HcfFree(iv);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool GetIvAndAadBlob(napi_env env, napi_value arg, HcfBlob **iv, HcfBlob **aad)
|
||||
{
|
||||
*iv = GetBlobFromParamsSpec(env, arg, IV_PARAMS);
|
||||
if (*iv == nullptr) {
|
||||
LOGE("get iv failed!");
|
||||
return false;
|
||||
}
|
||||
|
||||
*aad = GetBlobFromParamsSpec(env, arg, AAD_PARAMS);
|
||||
if (*aad == nullptr) {
|
||||
LOGE("get aad failed!");
|
||||
HcfFree((*iv)->data);
|
||||
HcfFree(*iv);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool GetGcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
|
||||
{
|
||||
HcfBlob *iv = nullptr;
|
||||
HcfBlob *aad = nullptr;
|
||||
HcfBlob *tag = nullptr;
|
||||
HcfBlob authTag = {};
|
||||
bool ret = false;
|
||||
|
||||
HcfGcmParamsSpec *gcmParamsSpec = (HcfGcmParamsSpec *)HcfMalloc(sizeof(HcfGcmParamsSpec), 0);
|
||||
if (gcmParamsSpec == nullptr) {
|
||||
LOGE("gcmParamsSpec malloc failed!");
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = GetIvAndAadBlob(env, arg, &iv, &aad);
|
||||
if (!ret) {
|
||||
LOGE("GetIvAndAadBlob failed!");
|
||||
goto clearup;
|
||||
}
|
||||
|
||||
if (opMode == DECRYPT_MODE) {
|
||||
tag = GetBlobFromParamsSpec(env, arg, AUTHTAG_PARAMS);
|
||||
if (tag == nullptr) {
|
||||
LOGE("get tag failed!");
|
||||
goto clearup;
|
||||
}
|
||||
} else if (opMode == ENCRYPT_MODE) {
|
||||
authTag.data = (uint8_t *)HcfMalloc(GCM_AUTH_TAG_LEN, 0);
|
||||
if (authTag.data == nullptr) {
|
||||
LOGE("get tag failed!");
|
||||
goto clearup;
|
||||
}
|
||||
authTag.len = GCM_AUTH_TAG_LEN;
|
||||
} else {
|
||||
goto clearup;
|
||||
}
|
||||
|
||||
gcmParamsSpec->base.getType = GetGcmParamsSpecType;
|
||||
gcmParamsSpec->iv = *iv;
|
||||
gcmParamsSpec->aad = *aad;
|
||||
gcmParamsSpec->tag = opMode == DECRYPT_MODE ? *tag : authTag;
|
||||
*paramsSpec = (HcfParamsSpec *)gcmParamsSpec;
|
||||
ret = true;
|
||||
clearup:
|
||||
if (ret != true) {
|
||||
HcfBlobDataFree(iv);
|
||||
HcfBlobDataFree(aad);
|
||||
HcfBlobDataFree(tag);
|
||||
HcfFree(gcmParamsSpec);
|
||||
}
|
||||
HcfFree(iv);
|
||||
HcfFree(aad);
|
||||
HcfFree(tag);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool GetCcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
|
||||
{
|
||||
HcfBlob *iv = nullptr;
|
||||
HcfBlob *aad = nullptr;
|
||||
HcfBlob *tag = nullptr;
|
||||
HcfBlob authTag = {};
|
||||
bool ret = false;
|
||||
|
||||
HcfCcmParamsSpec *ccmParamsSpec = (HcfCcmParamsSpec *)HcfMalloc(sizeof(HcfCcmParamsSpec), 0);
|
||||
if (ccmParamsSpec == nullptr) {
|
||||
LOGE("ccmParamsSpec malloc failed!");
|
||||
return ret;
|
||||
}
|
||||
ret = GetIvAndAadBlob(env, arg, &iv, &aad);
|
||||
if (!ret) {
|
||||
LOGE("GetIvAndAadBlob failed!");
|
||||
goto clearup;
|
||||
}
|
||||
|
||||
if (opMode == DECRYPT_MODE) {
|
||||
tag = GetBlobFromParamsSpec(env, arg, AUTHTAG_PARAMS);
|
||||
if (tag == nullptr) {
|
||||
LOGE("get tag failed!");
|
||||
goto clearup;
|
||||
}
|
||||
} else if (opMode == ENCRYPT_MODE) {
|
||||
authTag.data = (uint8_t *)HcfMalloc(CCM_AUTH_TAG_LEN, 0);
|
||||
if (authTag.data == nullptr) {
|
||||
LOGE("get tag failed!");
|
||||
goto clearup;
|
||||
}
|
||||
authTag.len = CCM_AUTH_TAG_LEN;
|
||||
} else {
|
||||
goto clearup;
|
||||
}
|
||||
ccmParamsSpec->base.getType = GetCcmParamsSpecType;
|
||||
ccmParamsSpec->iv = *iv;
|
||||
ccmParamsSpec->aad = *aad;
|
||||
ccmParamsSpec->tag = opMode == DECRYPT_MODE ? *tag : authTag;
|
||||
*paramsSpec = (HcfParamsSpec *)ccmParamsSpec;
|
||||
ret = true;
|
||||
clearup:
|
||||
if (ret != true) {
|
||||
HcfBlobDataFree(iv);
|
||||
HcfBlobDataFree(aad);
|
||||
HcfBlobDataFree(tag);
|
||||
HcfFree(ccmParamsSpec);
|
||||
}
|
||||
HcfFree(iv);
|
||||
HcfFree(aad);
|
||||
HcfFree(tag);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool GetParamsSpecFormNapiValue(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
|
||||
{
|
||||
napi_value data = nullptr;
|
||||
if ((env == nullptr) || (arg == nullptr) || (paramsSpec == nullptr)) {
|
||||
LOGE("Invalid parmas!");
|
||||
return false;
|
||||
}
|
||||
|
||||
napi_status status = napi_get_named_property(env, arg, ALGO_PARAMS.c_str(), &data);
|
||||
if ((status != napi_ok) || (data == nullptr)) {
|
||||
LOGE("failed to get valid data property!");
|
||||
return false;
|
||||
}
|
||||
string algoName;
|
||||
if (!GetStringFromJSParams(env, data, algoName)) {
|
||||
LOGE("GetStringFromJSParams failed!");
|
||||
return false;
|
||||
}
|
||||
if (algoName.compare(IV_PARAMS_SPEC) == 0) {
|
||||
return GetIvParamsSpec(env, arg, paramsSpec);
|
||||
} else if (algoName.compare(GCM_PARAMS_SPEC) == 0) {
|
||||
return GetGcmParamsSpec(env, arg, opMode, paramsSpec);
|
||||
} else if (algoName.compare(CCM_PARAMS_SPEC) == 0) {
|
||||
return GetCcmParamsSpec(env, arg, opMode, paramsSpec);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
napi_value ConvertBlobToNapiValue(napi_env env, HcfBlob *blob)
|
||||
{
|
||||
if (blob == nullptr || blob->data == nullptr || blob->len == 0) {
|
||||
LOGE("Invalid blob!");
|
||||
return nullptr;
|
||||
}
|
||||
uint8_t *buffer = (uint8_t *)HcfMalloc(blob->len, 0);
|
||||
if (buffer == nullptr) {
|
||||
LOGE("malloc uint8 array buffer failed!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (memcpy_s(buffer, blob->len, blob->data, blob->len) != EOK) {
|
||||
LOGE("memcpy_s data to buffer failed!");
|
||||
HcfFree(buffer);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value outBuffer = nullptr;
|
||||
napi_status status = napi_create_external_arraybuffer(
|
||||
env, buffer, blob->len, [](napi_env env, void *data, void *hint) { HcfFree(data); }, nullptr, &outBuffer);
|
||||
if (status != napi_ok) {
|
||||
LOGE("create uint8 array buffer failed!");
|
||||
HcfFree(buffer);
|
||||
return nullptr;
|
||||
}
|
||||
buffer = nullptr;
|
||||
|
||||
napi_value outData = nullptr;
|
||||
napi_create_typedarray(env, napi_uint8_array, blob->len, outBuffer, 0, &outData);
|
||||
napi_value dataBlob = nullptr;
|
||||
napi_create_object(env, &dataBlob);
|
||||
napi_set_named_property(env, dataBlob, CRYPTO_TAG_DATA.c_str(), outData);
|
||||
|
||||
return dataBlob;
|
||||
}
|
||||
|
||||
static bool GetDataOfCertChain(napi_env env, napi_value data, HcfCertChainData *certChain)
|
||||
{
|
||||
napi_typedarray_type arrayType;
|
||||
napi_value arrayBuffer = nullptr;
|
||||
size_t length = 0;
|
||||
size_t offset = 0;
|
||||
void *rawData = nullptr;
|
||||
|
||||
napi_status status = napi_get_typedarray_info(env, data, &arrayType, &length,
|
||||
(void **)&rawData, &arrayBuffer, &offset);
|
||||
if (status != napi_ok) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get array data failed"));
|
||||
LOGE("failed to get array data!");
|
||||
return false;
|
||||
}
|
||||
if (arrayType != napi_uint8_array) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "array type is not uint8 array"));
|
||||
LOGE("array is not uint8 array!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (length == 0) {
|
||||
LOGE("input data length is 0");
|
||||
return false;
|
||||
}
|
||||
certChain->data = (uint8_t *)HcfMalloc(length, 0);
|
||||
if (certChain->data == nullptr) {
|
||||
LOGE("malloc cert chain data failed!");
|
||||
return false;
|
||||
}
|
||||
if (memcpy_s(certChain->data, length, rawData, length) != EOK) {
|
||||
LOGE("memcpy_s cert chain data failed!");
|
||||
HcfFree(certChain->data);
|
||||
certChain->data = nullptr;
|
||||
return false;
|
||||
}
|
||||
certChain->dataLen = length;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetCertChainFromValue(napi_env env, napi_value obj, HcfCertChainData **certChainData)
|
||||
{
|
||||
*certChainData = (HcfCertChainData *)HcfMalloc(sizeof(HcfCertChainData), 0);
|
||||
if (*certChainData == nullptr) {
|
||||
LOGE("malloc certChainData failed!");
|
||||
return false;
|
||||
}
|
||||
napi_value data = nullptr;
|
||||
napi_status status = napi_get_named_property(env, obj, CRYPTO_TAG_DATA.c_str(), &data);
|
||||
if (status != napi_ok) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get cert chain data failed"));
|
||||
LOGE("failed to get cert chain data!");
|
||||
HcfFree(*certChainData);
|
||||
*certChainData = nullptr;
|
||||
return false;
|
||||
}
|
||||
if (!GetDataOfCertChain(env, data, *certChainData)) {
|
||||
HcfFree(*certChainData);
|
||||
*certChainData = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
napi_value certCount = nullptr;
|
||||
status = napi_get_named_property(env, obj, CRYPTO_TAG_COUNT.c_str(), &certCount);
|
||||
if (status != napi_ok) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get cert chain count failed"));
|
||||
LOGE("failed to get cert count!");
|
||||
HcfFree((*certChainData)->data);
|
||||
(*certChainData)->data = nullptr;
|
||||
HcfFree(*certChainData);
|
||||
*certChainData = nullptr;
|
||||
return false;
|
||||
}
|
||||
napi_get_value_uint32(env, certCount, (uint32_t *)(&(*certChainData)->count));
|
||||
|
||||
napi_value format = nullptr;
|
||||
status = napi_get_named_property(env, obj, CRYPTO_TAG_ENCODING_FORMAT.c_str(), &format);
|
||||
if (status != napi_ok) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get cert chain format failed"));
|
||||
LOGE("failed to get cert chain format!");
|
||||
HcfFree((*certChainData)->data);
|
||||
(*certChainData)->data = nullptr;
|
||||
HcfFree(*certChainData);
|
||||
*certChainData = nullptr;
|
||||
return false;
|
||||
}
|
||||
napi_get_value_uint32(env, format, (uint32_t *)(&(*certChainData)->format));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetStringFromJSParams(napi_env env, napi_value arg, string &returnStr)
|
||||
{
|
||||
napi_valuetype valueType;
|
||||
napi_typeof(env, arg, &valueType);
|
||||
if (valueType != napi_string) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not string"));
|
||||
LOGE("wrong argument type. expect string type. [Type]: %d", valueType);
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t length = 0;
|
||||
if (napi_get_value_string_utf8(env, arg, nullptr, 0, &length) != napi_ok) {
|
||||
LOGE("can not get string length");
|
||||
return false;
|
||||
}
|
||||
returnStr.reserve(length + 1);
|
||||
returnStr.resize(length);
|
||||
if (napi_get_value_string_utf8(env, arg, returnStr.data(), (length + 1), &length) != napi_ok) {
|
||||
LOGE("can not get string value");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetInt32FromJSParams(napi_env env, napi_value arg, int32_t &returnInt)
|
||||
{
|
||||
napi_valuetype valueType;
|
||||
napi_typeof(env, arg, &valueType);
|
||||
if (valueType != napi_number) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not number"));
|
||||
LOGE("wrong argument type. expect int type. [Type]: %d", valueType);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (napi_get_value_int32(env, arg, &returnInt) != napi_ok) {
|
||||
LOGE("can not get int value");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetUint32FromJSParams(napi_env env, napi_value arg, uint32_t &returnInt)
|
||||
{
|
||||
napi_valuetype valueType;
|
||||
napi_typeof(env, arg, &valueType);
|
||||
if (valueType != napi_number) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not number"));
|
||||
LOGE("wrong argument type. expect int type. [Type]: %d", valueType);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (napi_get_value_uint32(env, arg, &returnInt) != napi_ok) {
|
||||
LOGE("can not get int value");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetCallbackFromJSParams(napi_env env, napi_value arg, napi_ref *returnCb)
|
||||
{
|
||||
napi_valuetype valueType = napi_undefined;
|
||||
napi_typeof(env, arg, &valueType);
|
||||
if (valueType != napi_function) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not function"));
|
||||
LOGE("wrong argument type. expect callback type. [Type]: %d", valueType);
|
||||
return false;
|
||||
}
|
||||
|
||||
napi_create_reference(env, arg, 1, returnCb);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ParseArrayBuffer(napi_env env, napi_value args, uint8_t **data, size_t &size)
|
||||
{
|
||||
napi_status status;
|
||||
napi_valuetype valuetype;
|
||||
napi_typeof(env, args, &valuetype);
|
||||
|
||||
if (valuetype != napi_object) {
|
||||
LOGE("Wrong argument type(%d). object expected.", valuetype);
|
||||
return false;
|
||||
}
|
||||
|
||||
status = napi_get_arraybuffer_info(env, args, (void**)data, &size);
|
||||
if (status != napi_ok) {
|
||||
LOGE("can not get arraybuffer, error is %d", status);
|
||||
(*data)[0] = -1;
|
||||
return false;
|
||||
}
|
||||
LOGI("arraybuffer size is %zu", size);
|
||||
return true;
|
||||
}
|
||||
|
||||
static uint32_t GetJsErrValueByErrCode(int32_t errCode)
|
||||
{
|
||||
switch (errCode) {
|
||||
case HCF_INVALID_PARAMS:
|
||||
return JS_ERR_INVALID_PARAMS;
|
||||
case HCF_NOT_SUPPORT:
|
||||
return JS_ERR_NOT_SUPPORT;
|
||||
case HCF_ERR_MALLOC:
|
||||
return JS_ERR_OUT_OF_MEMORY;
|
||||
case HCF_ERR_COPY:
|
||||
return JS_ERR_INTERNAL_ERROR;
|
||||
case HCF_ERR_CRYPTO_OPERATION:
|
||||
return JS_ERR_CRYPTO_OPERATION;
|
||||
case HCF_ERR_CERT_SIGNATURE_FAILURE:
|
||||
return JS_ERR_CERT_SIGNATURE_FAILURE;
|
||||
case HCF_ERR_CERT_NOT_YET_VALID:
|
||||
return JS_ERR_CERT_NOT_YET_VALID;
|
||||
case HCF_ERR_CERT_HAS_EXPIRED:
|
||||
return JS_ERR_CERT_HAS_EXPIRED;
|
||||
case HCF_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
|
||||
return JS_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
|
||||
case HCF_ERR_KEYUSAGE_NO_CERTSIGN:
|
||||
return JS_ERR_KEYUSAGE_NO_CERTSIGN;
|
||||
case HCF_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE:
|
||||
return JS_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
|
||||
default:
|
||||
return JS_ERR_DEFAULT_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
napi_value GenerateBusinessError(napi_env env, int32_t errCode, const char *errMsg)
|
||||
{
|
||||
napi_value businessError = nullptr;
|
||||
|
||||
napi_value code = nullptr;
|
||||
napi_create_uint32(env, GetJsErrValueByErrCode(errCode), &code);
|
||||
|
||||
napi_value msg = nullptr;
|
||||
napi_create_string_utf8(env, errMsg, NAPI_AUTO_LENGTH, &msg);
|
||||
|
||||
napi_create_error(env, nullptr, msg, &businessError);
|
||||
napi_set_named_property(env, businessError, CRYPTO_TAG_ERR_CODE.c_str(), code);
|
||||
|
||||
return businessError;
|
||||
}
|
||||
|
||||
bool CheckArgsCount(napi_env env, size_t argc, size_t expectedCount, bool isSync)
|
||||
{
|
||||
if (isSync) {
|
||||
if (argc != expectedCount) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count"));
|
||||
LOGE("invalid params count!");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if ((argc != expectedCount) && (argc != (expectedCount - ARGS_SIZE_ONE))) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count"));
|
||||
LOGE("invalid params count!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
napi_value GetResourceName(napi_env env, const char *name)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, name, NAPI_AUTO_LENGTH, &resourceName);
|
||||
return resourceName;
|
||||
}
|
||||
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
686
frameworks/js/napi/src/napi_verify.cpp
Normal file
686
frameworks/js/napi/src/napi_verify.cpp
Normal file
@ -0,0 +1,686 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "napi_verify.h"
|
||||
|
||||
#include "securec.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
#include "napi_pri_key.h"
|
||||
#include "napi_pub_key.h"
|
||||
#include "napi_utils.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
struct VerifyInitCtx {
|
||||
napi_env env = nullptr;
|
||||
|
||||
AsyncType asyncType = ASYNC_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_value promise = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
HcfVerify *verify;
|
||||
HcfParamsSpec *params;
|
||||
HcfPubKey *pubKey;
|
||||
|
||||
HcfResult result;
|
||||
};
|
||||
|
||||
struct VerifyUpdateCtx {
|
||||
napi_env env = nullptr;
|
||||
|
||||
AsyncType asyncType = ASYNC_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_value promise = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
HcfVerify *verify;
|
||||
HcfBlob *data;
|
||||
|
||||
HcfResult result;
|
||||
};
|
||||
|
||||
struct VerifyDoFinalCtx {
|
||||
napi_env env = nullptr;
|
||||
|
||||
AsyncType asyncType = ASYNC_CALLBACK;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_value promise = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
HcfVerify *verify;
|
||||
HcfBlob *data;
|
||||
HcfBlob *signatureData;
|
||||
|
||||
bool result;
|
||||
};
|
||||
|
||||
thread_local napi_ref NapiVerify::classRef_ = nullptr;
|
||||
|
||||
static void FreeVerifyInitCtx(napi_env env, VerifyInitCtx *ctx)
|
||||
{
|
||||
if (ctx == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, ctx->asyncWork);
|
||||
ctx->asyncWork = nullptr;
|
||||
}
|
||||
|
||||
if (ctx->callback != nullptr) {
|
||||
napi_delete_reference(env, ctx->callback);
|
||||
ctx->callback = nullptr;
|
||||
}
|
||||
|
||||
HcfFree(ctx);
|
||||
}
|
||||
|
||||
static void FreeVerifyUpdateCtx(napi_env env, VerifyUpdateCtx *ctx)
|
||||
{
|
||||
if (ctx == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, ctx->asyncWork);
|
||||
ctx->asyncWork = nullptr;
|
||||
}
|
||||
|
||||
if (ctx->callback != nullptr) {
|
||||
napi_delete_reference(env, ctx->callback);
|
||||
ctx->callback = nullptr;
|
||||
}
|
||||
|
||||
HcfBlobDataFree(ctx->data);
|
||||
HcfFree(ctx->data);
|
||||
HcfFree(ctx);
|
||||
}
|
||||
|
||||
static void FreeVerifyDoFinalCtx(napi_env env, VerifyDoFinalCtx *ctx)
|
||||
{
|
||||
if (ctx == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, ctx->asyncWork);
|
||||
ctx->asyncWork = nullptr;
|
||||
}
|
||||
|
||||
if (ctx->callback != nullptr) {
|
||||
napi_delete_reference(env, ctx->callback);
|
||||
ctx->callback = nullptr;
|
||||
}
|
||||
|
||||
HcfBlobDataFree(ctx->data);
|
||||
HcfFree(ctx->data);
|
||||
HcfBlobDataFree(ctx->signatureData);
|
||||
HcfFree(ctx->signatureData);
|
||||
HcfFree(ctx);
|
||||
}
|
||||
|
||||
static bool BuildVerifyJsInitCtx(napi_env env, napi_callback_info info, VerifyInitCtx *ctx)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t expectedArgc = PARAMS_NUM_TWO;
|
||||
size_t argc;
|
||||
napi_value argv[PARAMS_NUM_TWO] = { nullptr, nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if ((argc != expectedArgc) && (argc != expectedArgc - 1)) {
|
||||
LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc);
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error."));
|
||||
return false;
|
||||
}
|
||||
ctx->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
|
||||
|
||||
NapiVerify *napiVerify = NULL;
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)(&napiVerify));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi verify obj.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error."));
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
NapiPubKey *napiPubKey = NULL;
|
||||
status = napi_unwrap(env, argv[index], (void **)(&napiPubKey));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi pubKey obj.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PubKey]: param unwarp error."));
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx->verify = napiVerify->GetVerify();
|
||||
ctx->params = nullptr;
|
||||
ctx->pubKey = napiPubKey->GetPubKey();
|
||||
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
napi_create_promise(env, &ctx->deferred, &ctx->promise);
|
||||
return true;
|
||||
} else {
|
||||
return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback);
|
||||
}
|
||||
}
|
||||
|
||||
static bool BuildVerifyJsUpdateCtx(napi_env env, napi_callback_info info, VerifyUpdateCtx *ctx)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t expectedArgc = PARAMS_NUM_TWO;
|
||||
size_t argc;
|
||||
napi_value argv[PARAMS_NUM_TWO] = { nullptr, nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if ((argc != expectedArgc) && (argc != expectedArgc - 1)) {
|
||||
LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc);
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error."));
|
||||
return false;
|
||||
}
|
||||
ctx->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
|
||||
|
||||
NapiVerify *napiVerify = NULL;
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)(&napiVerify));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi verify obj.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error."));
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
HcfBlob *blob = GetBlobFromNapiValue(env, argv[index]);
|
||||
if (blob == nullptr) {
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Data]: must be of the DataBlob type."));
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx->verify = napiVerify->GetVerify();
|
||||
ctx->data = blob;
|
||||
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
napi_create_promise(env, &ctx->deferred, &ctx->promise);
|
||||
return true;
|
||||
} else {
|
||||
return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback);
|
||||
}
|
||||
}
|
||||
|
||||
static bool GetDataBlobAndSignatureFromInput(napi_env env, napi_value dataValue, napi_value signatureDataValue,
|
||||
HcfBlob **returnData, HcfBlob **returnSignatureData)
|
||||
{
|
||||
napi_valuetype valueType;
|
||||
napi_typeof(env, dataValue, &valueType);
|
||||
HcfBlob *data = nullptr;
|
||||
if (valueType != napi_null) {
|
||||
data = GetBlobFromNapiValue(env, dataValue);
|
||||
if (data == nullptr) {
|
||||
LOGE("failed to get data.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Data]: must be of the DataBlob type."));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
HcfBlob *signatureData = GetBlobFromNapiValue(env, signatureDataValue);
|
||||
if (signatureData == nullptr) {
|
||||
LOGE("failed to get signature.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS,
|
||||
"[SignatureData]: must be of the DataBlob type."));
|
||||
HcfBlobDataFree(data);
|
||||
HcfFree(data);
|
||||
return false;
|
||||
}
|
||||
|
||||
*returnData = data;
|
||||
*returnSignatureData = signatureData;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool BuildVerifyJsDoFinalCtx(napi_env env, napi_callback_info info, VerifyDoFinalCtx *ctx)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t expectedArgc = PARAMS_NUM_THREE;
|
||||
size_t argc;
|
||||
napi_value argv[PARAMS_NUM_THREE] = { nullptr, nullptr, nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if ((argc != expectedArgc) && (argc != expectedArgc - 1)) {
|
||||
LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc);
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error."));
|
||||
return false;
|
||||
}
|
||||
ctx->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
|
||||
|
||||
NapiVerify *napiVerify = nullptr;
|
||||
napi_status status = napi_unwrap(env, thisVar, (void **)(&napiVerify));
|
||||
if (status != napi_ok) {
|
||||
LOGE("failed to unwrap napi verify obj.");
|
||||
napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error."));
|
||||
return false;
|
||||
}
|
||||
|
||||
HcfBlob *data = nullptr;
|
||||
HcfBlob *signatureData = nullptr;
|
||||
if (!GetDataBlobAndSignatureFromInput(env, argv[PARAM0], argv[PARAM1], &data, &signatureData)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx->verify = napiVerify->GetVerify();
|
||||
ctx->data = data;
|
||||
ctx->signatureData = signatureData;
|
||||
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
napi_create_promise(env, &ctx->deferred, &ctx->promise);
|
||||
return true;
|
||||
} else {
|
||||
return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback);
|
||||
}
|
||||
}
|
||||
|
||||
static void ReturnInitCallbackResult(napi_env env, VerifyInitCtx *ctx, napi_value result)
|
||||
{
|
||||
napi_value businessError = nullptr;
|
||||
if (ctx->result != HCF_SUCCESS) {
|
||||
businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str());
|
||||
}
|
||||
|
||||
napi_value params[ARGS_SIZE_ONE] = { businessError };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, ctx->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_ONE, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnInitPromiseResult(napi_env env, VerifyInitCtx *ctx, napi_value result)
|
||||
{
|
||||
if (ctx->result == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, ctx->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
static void ReturnUpdateCallbackResult(napi_env env, VerifyUpdateCtx *ctx, napi_value result)
|
||||
{
|
||||
napi_value businessError = nullptr;
|
||||
if (ctx->result != HCF_SUCCESS) {
|
||||
businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str());
|
||||
}
|
||||
|
||||
napi_value params[ARGS_SIZE_ONE] = { businessError };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, ctx->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_ONE, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnUpdatePromiseResult(napi_env env, VerifyUpdateCtx *ctx, napi_value result)
|
||||
{
|
||||
if (ctx->result == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, ctx->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
static void ReturnDoFinalCallbackResult(napi_env env, VerifyDoFinalCtx *ctx, napi_value result)
|
||||
{
|
||||
napi_value businessError = nullptr;
|
||||
if (ctx->result != HCF_SUCCESS) {
|
||||
businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str());
|
||||
}
|
||||
|
||||
napi_value params[ARGS_SIZE_TWO] = { businessError, result };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, ctx->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnDoFinalPromiseResult(napi_env env, VerifyDoFinalCtx *ctx, napi_value result)
|
||||
{
|
||||
if (ctx->result) {
|
||||
napi_resolve_deferred(env, ctx->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, ctx->deferred,
|
||||
GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, COMMON_ERR_MSG.c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
void VerifyJsInitAsyncWorkProcess(napi_env env, void *data)
|
||||
{
|
||||
VerifyInitCtx *ctx = static_cast<VerifyInitCtx *>(data);
|
||||
|
||||
HcfResult res = ctx->verify->init(ctx->verify, ctx->params, ctx->pubKey);
|
||||
|
||||
ctx->result = res;
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("verify init fail.");
|
||||
}
|
||||
}
|
||||
|
||||
void VerifyJsInitAsyncWorkReturn(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
VerifyInitCtx *ctx = static_cast<VerifyInitCtx *>(data);
|
||||
|
||||
if (ctx->asyncType == ASYNC_CALLBACK) {
|
||||
ReturnInitCallbackResult(env, ctx, NapiGetNull(env));
|
||||
} else {
|
||||
ReturnInitPromiseResult(env, ctx, NapiGetNull(env));
|
||||
}
|
||||
FreeVerifyInitCtx(env, ctx);
|
||||
}
|
||||
|
||||
void VerifyJsUpdateAsyncWorkProcess(napi_env env, void *data)
|
||||
{
|
||||
VerifyUpdateCtx *ctx = static_cast<VerifyUpdateCtx *>(data);
|
||||
|
||||
HcfResult res = ctx->verify->update(ctx->verify, ctx->data);
|
||||
|
||||
ctx->result = res;
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("verify update fail.");
|
||||
}
|
||||
}
|
||||
|
||||
void VerifyJsUpdateAsyncWorkReturn(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
VerifyUpdateCtx *ctx = static_cast<VerifyUpdateCtx *>(data);
|
||||
|
||||
if (ctx->asyncType == ASYNC_CALLBACK) {
|
||||
ReturnUpdateCallbackResult(env, ctx, NapiGetNull(env));
|
||||
} else {
|
||||
ReturnUpdatePromiseResult(env, ctx, NapiGetNull(env));
|
||||
}
|
||||
FreeVerifyUpdateCtx(env, ctx);
|
||||
}
|
||||
|
||||
void VerifyJsDoFinalAsyncWorkProcess(napi_env env, void *data)
|
||||
{
|
||||
VerifyDoFinalCtx *ctx = static_cast<VerifyDoFinalCtx *>(data);
|
||||
|
||||
bool res = ctx->verify->verify(ctx->verify, ctx->data, ctx->signatureData);
|
||||
|
||||
ctx->result = res;
|
||||
if (!res) {
|
||||
LOGE("verify doFinal fail.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void VerifyJsDoFinalAsyncWorkReturn(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
VerifyDoFinalCtx *ctx = static_cast<VerifyDoFinalCtx *>(data);
|
||||
|
||||
napi_value result = nullptr;
|
||||
napi_get_boolean(env, ctx->result, &result);
|
||||
|
||||
if (ctx->asyncType == ASYNC_CALLBACK) {
|
||||
ReturnDoFinalCallbackResult(env, ctx, result);
|
||||
} else {
|
||||
ReturnDoFinalPromiseResult(env, ctx, result);
|
||||
}
|
||||
FreeVerifyDoFinalCtx(env, ctx);
|
||||
}
|
||||
|
||||
static napi_value NewVerifyJsInitAsyncWork(napi_env env, VerifyInitCtx *ctx)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, "init", NAPI_AUTO_LENGTH, &resourceName);
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, resourceName,
|
||||
[](napi_env env, void *data) {
|
||||
VerifyJsInitAsyncWorkProcess(env, data);
|
||||
return;
|
||||
},
|
||||
[](napi_env env, napi_status status, void *data) {
|
||||
VerifyJsInitAsyncWorkReturn(env, status, data);
|
||||
return;
|
||||
},
|
||||
(void *)ctx,
|
||||
&ctx->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, ctx->asyncWork);
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
return ctx->promise;
|
||||
} else {
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
static napi_value NewVerifyJsUpdateAsyncWork(napi_env env, VerifyUpdateCtx *ctx)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, "update", NAPI_AUTO_LENGTH, &resourceName);
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, resourceName,
|
||||
[](napi_env env, void *data) {
|
||||
VerifyJsUpdateAsyncWorkProcess(env, data);
|
||||
return;
|
||||
},
|
||||
[](napi_env env, napi_status status, void *data) {
|
||||
VerifyJsUpdateAsyncWorkReturn(env, status, data);
|
||||
return;
|
||||
},
|
||||
(void *)ctx,
|
||||
&ctx->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, ctx->asyncWork);
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
return ctx->promise;
|
||||
} else {
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
static napi_value NewVerifyJsDoFinalAsyncWork(napi_env env, VerifyDoFinalCtx *ctx)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
napi_create_string_utf8(env, "verify", NAPI_AUTO_LENGTH, &resourceName);
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, resourceName,
|
||||
[](napi_env env, void *data) {
|
||||
VerifyJsDoFinalAsyncWorkProcess(env, data);
|
||||
return;
|
||||
},
|
||||
[](napi_env env, napi_status status, void *data) {
|
||||
VerifyJsDoFinalAsyncWorkReturn(env, status, data);
|
||||
return;
|
||||
},
|
||||
(void *)ctx,
|
||||
&ctx->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, ctx->asyncWork);
|
||||
if (ctx->asyncType == ASYNC_PROMISE) {
|
||||
return ctx->promise;
|
||||
} else {
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
NapiVerify::NapiVerify(HcfVerify *verify)
|
||||
{
|
||||
this->verify_ = verify;
|
||||
}
|
||||
|
||||
NapiVerify::~NapiVerify()
|
||||
{
|
||||
OH_HCF_ObjDestroy(this->verify_);
|
||||
}
|
||||
|
||||
HcfVerify *NapiVerify::GetVerify()
|
||||
{
|
||||
return this->verify_;
|
||||
}
|
||||
|
||||
napi_value NapiVerify::JsInit(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
VerifyInitCtx *ctx = (VerifyInitCtx *)HcfMalloc(sizeof(VerifyInitCtx), 0);
|
||||
if (ctx == NULL) {
|
||||
LOGE("create context fail.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!BuildVerifyJsInitCtx(env, info, ctx)) {
|
||||
LOGE("build context fail.");
|
||||
FreeVerifyInitCtx(env, ctx);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NewVerifyJsInitAsyncWork(env, ctx);
|
||||
}
|
||||
|
||||
napi_value NapiVerify::JsUpdate(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
VerifyUpdateCtx *ctx = (VerifyUpdateCtx *)HcfMalloc(sizeof(VerifyUpdateCtx), 0);
|
||||
if (ctx == NULL) {
|
||||
LOGE("create context fail.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!BuildVerifyJsUpdateCtx(env, info, ctx)) {
|
||||
LOGE("build context fail.");
|
||||
FreeVerifyUpdateCtx(env, ctx);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NewVerifyJsUpdateAsyncWork(env, ctx);
|
||||
}
|
||||
|
||||
napi_value NapiVerify::JsVerify(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
VerifyDoFinalCtx *ctx = (VerifyDoFinalCtx *)HcfMalloc(sizeof(VerifyDoFinalCtx), 0);
|
||||
if (ctx == NULL) {
|
||||
LOGE("create context fail.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!BuildVerifyJsDoFinalCtx(env, info, ctx)) {
|
||||
LOGE("build context fail.");
|
||||
FreeVerifyDoFinalCtx(env, ctx);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NewVerifyJsDoFinalAsyncWork(env, ctx);
|
||||
}
|
||||
|
||||
napi_value NapiVerify::VerifyConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
|
||||
LOGI("out ...");
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value NapiVerify::CreateJsVerify(napi_env env, napi_callback_info info)
|
||||
{
|
||||
LOGI("enter ...");
|
||||
size_t exceptedArgc = PARAMS_NUM_ONE;
|
||||
size_t argc;
|
||||
napi_value argv[PARAMS_NUM_ONE] = { nullptr };
|
||||
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
|
||||
if (argc != exceptedArgc) {
|
||||
LOGE("The input args num is invalid.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value instance;
|
||||
napi_value constructor = nullptr;
|
||||
napi_get_reference_value(env, classRef_, &constructor);
|
||||
napi_new_instance(env, constructor, argc, argv, &instance);
|
||||
|
||||
std::string algName;
|
||||
if (!GetStringFromJSParams(env, argv[0], algName)) {
|
||||
LOGE("failed to get algoName.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
HcfVerify *verify = NULL;
|
||||
int32_t res = HcfVerifyCreate(algName.c_str(), &verify);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("create c verify fail.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
NapiVerify *napiVerify = new NapiVerify(verify);
|
||||
|
||||
napi_wrap(
|
||||
env, instance, napiVerify,
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
NapiVerify *napiVerify = (NapiVerify *)(data);
|
||||
delete napiVerify;
|
||||
return;
|
||||
},
|
||||
nullptr,
|
||||
nullptr);
|
||||
|
||||
napi_value napiAlgName = nullptr;
|
||||
napi_create_string_utf8(env, algName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName);
|
||||
napi_set_named_property(env, instance, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName);
|
||||
|
||||
LOGI("out ...");
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NapiVerify::DefineVerifyJSClass(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("createVerify", NapiVerify::CreateJsVerify),
|
||||
};
|
||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
||||
|
||||
napi_property_descriptor classDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("init", NapiVerify::JsInit),
|
||||
DECLARE_NAPI_FUNCTION("update", NapiVerify::JsUpdate),
|
||||
DECLARE_NAPI_FUNCTION("verify", NapiVerify::JsVerify),
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "Verify", NAPI_AUTO_LENGTH, NapiVerify::VerifyConstructor, nullptr,
|
||||
sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
} // CryptoFramework
|
||||
} // OHOS
|
1181
frameworks/js/napi/src/napi_x509_certificate.cpp
Normal file
1181
frameworks/js/napi/src/napi_x509_certificate.cpp
Normal file
File diff suppressed because it is too large
Load Diff
1243
frameworks/js/napi/src/napi_x509_crl.cpp
Normal file
1243
frameworks/js/napi/src/napi_x509_crl.cpp
Normal file
File diff suppressed because it is too large
Load Diff
444
frameworks/js/napi/src/napi_x509_crl_entry.cpp
Normal file
444
frameworks/js/napi/src/napi_x509_crl_entry.cpp
Normal file
@ -0,0 +1,444 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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 "napi_x509_crl_entry.h"
|
||||
|
||||
#include "napi/native_node_api.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
#include "utils.h"
|
||||
#include "object_base.h"
|
||||
#include "result.h"
|
||||
#include "napi_crypto_framework_defines.h"
|
||||
#include "napi_utils.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace CryptoFramework {
|
||||
|
||||
napi_ref NapiX509CrlEntry::classRef_ = nullptr;
|
||||
|
||||
struct CfCtx {
|
||||
CfAsyncType asyncType = ASYNC_TYPE_CALLBACK;
|
||||
napi_value promise = nullptr;
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
|
||||
NapiX509CrlEntry *crlEntryClass = nullptr;
|
||||
|
||||
int32_t errCode = 0;
|
||||
const char *errMsg = nullptr;
|
||||
HcfEncodingBlob *encoded = nullptr;
|
||||
HcfBlob *blob = nullptr;
|
||||
};
|
||||
|
||||
static void FreeCryptoFwkCtx(napi_env env, CfCtx *context)
|
||||
{
|
||||
if (context == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (context->asyncWork != nullptr) {
|
||||
napi_delete_async_work(env, context->asyncWork);
|
||||
}
|
||||
|
||||
if (context->callback != nullptr) {
|
||||
napi_delete_reference(env, context->callback);
|
||||
}
|
||||
|
||||
HcfEncodingBlobDataFree(context->encoded);
|
||||
HcfFree(context->encoded);
|
||||
context->encoded = nullptr;
|
||||
|
||||
HcfBlobDataFree(context->blob);
|
||||
HcfFree(context->blob);
|
||||
context->blob = nullptr;
|
||||
|
||||
HcfFree(context);
|
||||
context = nullptr;
|
||||
}
|
||||
|
||||
static void ReturnCallbackResult(napi_env env, CfCtx *context, napi_value result)
|
||||
{
|
||||
napi_value businessError = nullptr;
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
businessError = GenerateBusinessError(env, context->errCode, context->errMsg);
|
||||
}
|
||||
napi_value params[ARGS_SIZE_TWO] = { businessError, result };
|
||||
|
||||
napi_value func = nullptr;
|
||||
napi_get_reference_value(env, context->callback, &func);
|
||||
|
||||
napi_value recv = nullptr;
|
||||
napi_value callFuncRet = nullptr;
|
||||
napi_get_undefined(env, &recv);
|
||||
napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
|
||||
}
|
||||
|
||||
static void ReturnPromiseResult(napi_env env, CfCtx *context, napi_value result)
|
||||
{
|
||||
if (context->errCode == HCF_SUCCESS) {
|
||||
napi_resolve_deferred(env, context->deferred, result);
|
||||
} else {
|
||||
napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg));
|
||||
}
|
||||
}
|
||||
|
||||
static void ReturnResult(napi_env env, CfCtx *context, napi_value result)
|
||||
{
|
||||
if (context->asyncType == ASYNC_TYPE_CALLBACK) {
|
||||
ReturnCallbackResult(env, context, result);
|
||||
} else {
|
||||
ReturnPromiseResult(env, context, result);
|
||||
}
|
||||
}
|
||||
|
||||
static bool CreateCallbackAndPromise(napi_env env, CfCtx *context, size_t argc,
|
||||
size_t maxCount, napi_value callbackValue)
|
||||
{
|
||||
context->asyncType = (argc == maxCount) ? ASYNC_TYPE_CALLBACK : ASYNC_TYPE_PROMISE;
|
||||
if (context->asyncType == ASYNC_TYPE_CALLBACK) {
|
||||
if (!GetCallbackFromJSParams(env, callbackValue, &context->callback)) {
|
||||
LOGE("get callback failed!");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
napi_create_promise(env, &context->deferred, &context->promise);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
NapiX509CrlEntry::NapiX509CrlEntry(HcfX509CrlEntry *x509CrlEntry)
|
||||
{
|
||||
this->x509CrlEntry_ = x509CrlEntry;
|
||||
}
|
||||
|
||||
NapiX509CrlEntry::~NapiX509CrlEntry()
|
||||
{
|
||||
OH_HCF_ObjDestroy(this->x509CrlEntry_);
|
||||
}
|
||||
|
||||
static void GetEncodedExecute(napi_env env, void *data)
|
||||
{
|
||||
CfCtx *context = static_cast<CfCtx *>(data);
|
||||
HcfX509CrlEntry *x509CrlEntry = context->crlEntryClass->GetX509CrlEntry();
|
||||
HcfEncodingBlob *encodingBlob = (HcfEncodingBlob *)HcfMalloc(sizeof(HcfEncodingBlob), 0);
|
||||
if (encodingBlob == nullptr) {
|
||||
LOGE("malloc encoding blob failed!");
|
||||
context->errCode = HCF_ERR_MALLOC;
|
||||
context->errMsg = "malloc encoding blob failed";
|
||||
return;
|
||||
}
|
||||
|
||||
context->errCode = x509CrlEntry->getEncoded(x509CrlEntry, encodingBlob);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
LOGE("get encoded failed!");
|
||||
context->errMsg = "get encoded failed";
|
||||
}
|
||||
context->encoded = encodingBlob;
|
||||
}
|
||||
|
||||
static void GetEncodedComplete(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
CfCtx *context = static_cast<CfCtx *>(data);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
ReturnResult(env, context, nullptr);
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return;
|
||||
}
|
||||
napi_value returnEncodingBlob = ConvertEncodingBlobToNapiValue(env, context->encoded);
|
||||
ReturnResult(env, context, returnEncodingBlob);
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
}
|
||||
|
||||
static void GetCertificateIssuerExecute(napi_env env, void *data)
|
||||
{
|
||||
CfCtx *context = static_cast<CfCtx *>(data);
|
||||
HcfX509CrlEntry *x509CrlEntry = context->crlEntryClass->GetX509CrlEntry();
|
||||
HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0);
|
||||
if (blob == nullptr) {
|
||||
LOGE("malloc blob failed!");
|
||||
context->errCode = HCF_ERR_MALLOC;
|
||||
context->errMsg = "malloc blob failed";
|
||||
return;
|
||||
}
|
||||
|
||||
context->errCode = x509CrlEntry->getCertIssuer(x509CrlEntry, blob);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
LOGE("get cert issuer failed!");
|
||||
context->errMsg = "get cert issuer failed";
|
||||
}
|
||||
context->blob = blob;
|
||||
}
|
||||
|
||||
static void GetCertificateIssuerComplete(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
CfCtx *context = static_cast<CfCtx *>(data);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
ReturnResult(env, context, nullptr);
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return;
|
||||
}
|
||||
napi_value returnBlob = ConvertBlobToNapiValue(env, context->blob);
|
||||
ReturnResult(env, context, returnBlob);
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
}
|
||||
|
||||
static void GetRevocationDateExecute(napi_env env, void *data)
|
||||
{
|
||||
CfCtx *context = static_cast<CfCtx *>(data);
|
||||
HcfX509CrlEntry *x509CrlEntry = context->crlEntryClass->GetX509CrlEntry();
|
||||
HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0);
|
||||
if (blob == nullptr) {
|
||||
LOGE("malloc blob failed!");
|
||||
context->errCode = HCF_ERR_MALLOC;
|
||||
context->errMsg = "malloc blob failed";
|
||||
return;
|
||||
}
|
||||
context->errCode = x509CrlEntry->getRevocationDate(x509CrlEntry, blob);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
LOGE("get revocation date failed!");
|
||||
context->errMsg = "get revocation date failed";
|
||||
}
|
||||
context->blob = blob;
|
||||
}
|
||||
|
||||
static void GetRevocationDateComplete(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
CfCtx *context = static_cast<CfCtx *>(data);
|
||||
if (context->errCode != HCF_SUCCESS) {
|
||||
ReturnResult(env, context, nullptr);
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return;
|
||||
}
|
||||
HcfBlob *blob = context->blob;
|
||||
napi_value result = nullptr;
|
||||
napi_create_string_utf8(env, (char *)blob->data, blob->len, &result);
|
||||
ReturnResult(env, context, result);
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
}
|
||||
|
||||
napi_value NapiX509CrlEntry::GetEncoded(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argc = ARGS_SIZE_ONE;
|
||||
napi_value argv[ARGS_SIZE_ONE] = { 0 };
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CfCtx *context = (CfCtx *)HcfMalloc(sizeof(CfCtx), 0);
|
||||
if (context == nullptr) {
|
||||
LOGE("malloc context failed!");
|
||||
return nullptr;
|
||||
}
|
||||
context->crlEntryClass = this;
|
||||
|
||||
if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_ONE, argv[PARAM0])) {
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, GetResourceName(env, "GetEncoded"),
|
||||
GetEncodedExecute,
|
||||
GetEncodedComplete,
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_TYPE_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
}
|
||||
|
||||
napi_value NapiX509CrlEntry::GetSerialNumber(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argc = ARGS_SIZE_ZERO;
|
||||
napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr);
|
||||
if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
|
||||
long serialNumber = x509CrlEntry->getSerialNumber(x509CrlEntry);
|
||||
napi_value result = nullptr;
|
||||
napi_create_int64(env, serialNumber, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value NapiX509CrlEntry::GetCertificateIssuer(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argc = ARGS_SIZE_ONE;
|
||||
napi_value argv[ARGS_SIZE_ONE] = { 0 };
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CfCtx *context = (CfCtx *)HcfMalloc(sizeof(CfCtx), 0);
|
||||
if (context == nullptr) {
|
||||
LOGE("malloc context failed!");
|
||||
return nullptr;
|
||||
}
|
||||
context->crlEntryClass = this;
|
||||
|
||||
if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_ONE, argv[PARAM0])) {
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, GetResourceName(env, "GetCertificateIssuer"),
|
||||
GetCertificateIssuerExecute,
|
||||
GetCertificateIssuerComplete,
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_TYPE_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
}
|
||||
|
||||
napi_value NapiX509CrlEntry::GetRevocationDate(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argc = ARGS_SIZE_ONE;
|
||||
napi_value argv[ARGS_SIZE_ONE] = { 0 };
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
||||
if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CfCtx *context = (CfCtx *)HcfMalloc(sizeof(CfCtx), 0);
|
||||
if (context == nullptr) {
|
||||
LOGE("malloc context failed!");
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
context->crlEntryClass = this;
|
||||
|
||||
if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_ONE, argv[PARAM0])) {
|
||||
FreeCryptoFwkCtx(env, context);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_create_async_work(
|
||||
env, nullptr, GetResourceName(env, "GetRevocationDate"),
|
||||
GetRevocationDateExecute,
|
||||
GetRevocationDateComplete,
|
||||
(void *)context,
|
||||
&context->asyncWork);
|
||||
|
||||
napi_queue_async_work(env, context->asyncWork);
|
||||
if (context->asyncType == ASYNC_TYPE_PROMISE) {
|
||||
return context->promise;
|
||||
} else {
|
||||
return NapiGetNull(env);
|
||||
}
|
||||
}
|
||||
|
||||
static napi_value NapiGetEncoded(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiX509CrlEntry *x509CrlEntry = nullptr;
|
||||
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
|
||||
if (x509CrlEntry == nullptr) {
|
||||
LOGE("x509CrlEntry is nullptr!");
|
||||
return nullptr;
|
||||
}
|
||||
return x509CrlEntry->GetEncoded(env, info);
|
||||
}
|
||||
|
||||
static napi_value NapiGetSerialNumber(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiX509CrlEntry *x509CrlEntry = nullptr;
|
||||
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
|
||||
if (x509CrlEntry == nullptr) {
|
||||
LOGE("x509CrlEntry is nullptr!");
|
||||
return nullptr;
|
||||
}
|
||||
return x509CrlEntry->GetSerialNumber(env, info);
|
||||
}
|
||||
|
||||
static napi_value NapiGetCertificateIssuer(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiX509CrlEntry *x509CrlEntry = nullptr;
|
||||
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
|
||||
if (x509CrlEntry == nullptr) {
|
||||
LOGE("x509CrlEntry is nullptr!");
|
||||
return nullptr;
|
||||
}
|
||||
return x509CrlEntry->GetCertificateIssuer(env, info);
|
||||
}
|
||||
|
||||
static napi_value NapiGetRevocationDate(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
NapiX509CrlEntry *x509CrlEntry = nullptr;
|
||||
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
|
||||
if (x509CrlEntry == nullptr) {
|
||||
LOGE("x509CrlEntry is nullptr!");
|
||||
return nullptr;
|
||||
}
|
||||
return x509CrlEntry->GetRevocationDate(env, info);
|
||||
}
|
||||
|
||||
static napi_value X509CrlEntryConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
void NapiX509CrlEntry::DefineX509CrlEntryJSClass(napi_env env)
|
||||
{
|
||||
napi_property_descriptor x509CrlEntryDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("getEncoded", NapiGetEncoded),
|
||||
DECLARE_NAPI_FUNCTION("getSerialNumber", NapiGetSerialNumber),
|
||||
DECLARE_NAPI_FUNCTION("getCertIssuer", NapiGetCertificateIssuer),
|
||||
DECLARE_NAPI_FUNCTION("getRevocationDate", NapiGetRevocationDate),
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_define_class(env, "X509CrlEntry", NAPI_AUTO_LENGTH, X509CrlEntryConstructor, nullptr,
|
||||
sizeof(x509CrlEntryDesc) / sizeof(x509CrlEntryDesc[0]), x509CrlEntryDesc, &constructor);
|
||||
napi_create_reference(env, constructor, 1, &classRef_);
|
||||
}
|
||||
|
||||
napi_value NapiX509CrlEntry::CreateX509CrlEntry(napi_env env)
|
||||
{
|
||||
napi_value constructor = nullptr;
|
||||
napi_value instance = nullptr;
|
||||
napi_get_reference_value(env, classRef_, &constructor);
|
||||
napi_new_instance(env, constructor, 0, nullptr, &instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
} // namespace CryptoFramework
|
||||
} // namespace OHOS
|
257
frameworks/key/asy_key_generator.c
Normal file
257
frameworks/key/asy_key_generator.c
Normal file
@ -0,0 +1,257 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "asy_key_generator.h"
|
||||
|
||||
#include <securec.h>
|
||||
|
||||
#include "asy_key_generator_spi.h"
|
||||
#include "config.h"
|
||||
#include "ecc_asy_key_generator_openssl.h"
|
||||
#include "params_parser.h"
|
||||
#include "rsa_asy_key_generator_openssl.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
#include "utils.h"
|
||||
|
||||
typedef HcfResult (*HcfAsyKeyGeneratorSpiCreateFunc)(HcfAsyKeyGenParams *, HcfAsyKeyGeneratorSpi **);
|
||||
|
||||
typedef struct {
|
||||
HcfAsyKeyGenerator base;
|
||||
|
||||
HcfAsyKeyGeneratorSpi *spiObj;
|
||||
|
||||
char algoName[HCF_MAX_ALGO_NAME_LEN];
|
||||
} HcfAsyKeyGeneratorImpl;
|
||||
|
||||
typedef struct {
|
||||
HCF_ALG_VALUE algo;
|
||||
|
||||
HcfAsyKeyGeneratorSpiCreateFunc createSpifunc;
|
||||
} HcfAsyKeyGenAbility;
|
||||
|
||||
static const HcfAsyKeyGenAbility ASY_KEY_GEN_ABILITY_SET[] = {
|
||||
{ HCF_ALG_RSA, HcfAsyKeyGeneratorSpiRsaCreate },
|
||||
{ HCF_ALG_ECC, HcfAsyKeyGeneratorSpiEccCreate }
|
||||
};
|
||||
|
||||
static HcfAsyKeyGeneratorSpiCreateFunc FindAbility(HcfAsyKeyGenParams *params)
|
||||
{
|
||||
for (uint32_t i = 0; i < sizeof(ASY_KEY_GEN_ABILITY_SET) / sizeof(ASY_KEY_GEN_ABILITY_SET[0]); i++) {
|
||||
if (ASY_KEY_GEN_ABILITY_SET[i].algo == params->algo) {
|
||||
return ASY_KEY_GEN_ABILITY_SET[i].createSpifunc;
|
||||
}
|
||||
}
|
||||
LOGE("Algo not support! [Algo]: %d", params->algo);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void SetPrimes(HCF_ALG_PARA_VALUE value, HcfAsyKeyGenParams *params)
|
||||
{
|
||||
if (params == NULL) {
|
||||
LOGE("params is null.");
|
||||
return;
|
||||
}
|
||||
switch (value) {
|
||||
case HCF_OPENSSL_PRIMES_2:
|
||||
params->primes = (int32_t)HCF_RSA_PRIMES_SIZE_2;
|
||||
break;
|
||||
case HCF_OPENSSL_PRIMES_3:
|
||||
params->primes = (int32_t)HCF_RSA_PRIMES_SIZE_3;
|
||||
break;
|
||||
case HCF_OPENSSL_PRIMES_4:
|
||||
params->primes = (int32_t)HCF_RSA_PRIMES_SIZE_4;
|
||||
break;
|
||||
case HCF_OPENSSL_PRIMES_5:
|
||||
params->primes = (int32_t)HCF_RSA_PRIMES_SIZE_5;
|
||||
break;
|
||||
default:
|
||||
params->primes = (int32_t)HCF_RSA_PRIMES_SIZE_2; // default primes is 2
|
||||
LOGD("user default primes 2");
|
||||
break;
|
||||
}
|
||||
LOGD("Set primes:%d\n", params->primes);
|
||||
}
|
||||
|
||||
static void SetKeyType(HCF_ALG_PARA_VALUE value, HcfAsyKeyGenParams *params)
|
||||
{
|
||||
switch (value) {
|
||||
case HCF_ALG_ECC_224:
|
||||
case HCF_ALG_ECC_256:
|
||||
case HCF_ALG_ECC_384:
|
||||
case HCF_ALG_ECC_512:
|
||||
params->bits = value;
|
||||
params->algo = HCF_ALG_ECC;
|
||||
break;
|
||||
case HCF_OPENSSL_RSA_512:
|
||||
params->bits = (int32_t)HCF_RSA_KEY_SIZE_512;
|
||||
params->algo = HCF_ALG_RSA;
|
||||
break;
|
||||
case HCF_OPENSSL_RSA_768:
|
||||
params->bits = (int32_t)HCF_RSA_KEY_SIZE_768;
|
||||
params->algo = HCF_ALG_RSA;
|
||||
break;
|
||||
case HCF_OPENSSL_RSA_1024:
|
||||
params->bits = (int32_t)HCF_RSA_KEY_SIZE_1024;
|
||||
params->algo = HCF_ALG_RSA;
|
||||
break;
|
||||
case HCF_OPENSSL_RSA_2048:
|
||||
params->bits = (int32_t)HCF_RSA_KEY_SIZE_2048;
|
||||
params->algo = HCF_ALG_RSA;
|
||||
break;
|
||||
case HCF_OPENSSL_RSA_3072:
|
||||
params->bits = (int32_t)HCF_RSA_KEY_SIZE_3072;
|
||||
params->algo = HCF_ALG_RSA;
|
||||
break;
|
||||
case HCF_OPENSSL_RSA_4096:
|
||||
params->bits = (int32_t)HCF_RSA_KEY_SIZE_4096;
|
||||
params->algo = HCF_ALG_RSA;
|
||||
break;
|
||||
case HCF_OPENSSL_RSA_8192:
|
||||
params->bits = (int32_t)HCF_RSA_KEY_SIZE_8192;
|
||||
params->algo = HCF_ALG_RSA;
|
||||
break;
|
||||
default:
|
||||
LOGE("there is not matched algorithm.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static HcfResult ParseAsyKeyGenParams(const HcfParaConfig* config, void *params)
|
||||
{
|
||||
if (config == NULL || params == NULL) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcfResult ret = HCF_SUCCESS;
|
||||
HcfAsyKeyGenParams *paramsObj = (HcfAsyKeyGenParams *)params;
|
||||
LOGI("Set Parameter: %s", config->tag);
|
||||
switch (config->para_type) {
|
||||
case HCF_ALG_KEY_TYPE:
|
||||
SetKeyType(config->para_value, paramsObj);
|
||||
break;
|
||||
case HCF_ALG_PRIMES:
|
||||
SetPrimes(config->para_value, paramsObj);
|
||||
break;
|
||||
default:
|
||||
ret = HCF_INVALID_PARAMS;
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// export interfaces
|
||||
static const char *GetAsyKeyGeneratorClass()
|
||||
{
|
||||
return "HcfAsyKeyGenerator";
|
||||
}
|
||||
|
||||
static const char *GetAlgoName(HcfAsyKeyGenerator *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("The input self ptr is NULL!");
|
||||
return NULL;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetAsyKeyGeneratorClass())) {
|
||||
return NULL;
|
||||
}
|
||||
return ((HcfAsyKeyGeneratorImpl *)self)->algoName;
|
||||
}
|
||||
|
||||
static HcfResult ConvertKey(HcfAsyKeyGenerator *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob,
|
||||
HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetAsyKeyGeneratorClass())) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfAsyKeyGeneratorImpl *)self)->spiObj->engineConvertKey(
|
||||
((HcfAsyKeyGeneratorImpl *)self)->spiObj, params, pubKeyBlob, priKeyBlob, returnKeyPair);
|
||||
}
|
||||
|
||||
static HcfResult GenerateKeyPair(HcfAsyKeyGenerator *self, HcfParamsSpec *params,
|
||||
HcfKeyPair **returnKeyPair)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetAsyKeyGeneratorClass())) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfAsyKeyGeneratorImpl *)self)->spiObj->engineGenerateKeyPair(
|
||||
((HcfAsyKeyGeneratorImpl *)self)->spiObj, returnKeyPair);
|
||||
}
|
||||
|
||||
static void DestroyAsyKeyGenerator(HcfObjectBase *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
return;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetAsyKeyGeneratorClass())) {
|
||||
return;
|
||||
}
|
||||
HcfAsyKeyGeneratorImpl *impl = (HcfAsyKeyGeneratorImpl *)self;
|
||||
OH_HCF_ObjDestroy(impl->spiObj);
|
||||
impl->spiObj = NULL;
|
||||
HcfFree(impl);
|
||||
}
|
||||
|
||||
HcfResult HcfAsyKeyGeneratorCreate(const char *algoName, HcfAsyKeyGenerator **returnObj)
|
||||
{
|
||||
if ((!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN)) || (returnObj == NULL)) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
HcfAsyKeyGenParams params = { 0 };
|
||||
if (ParseAndSetParameter(algoName, ¶ms, ParseAsyKeyGenParams) != HCF_SUCCESS) {
|
||||
LOGE("Failed to parser parmas!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
HcfAsyKeyGeneratorSpiCreateFunc createSpifunc = FindAbility(¶ms);
|
||||
if (createSpifunc == NULL) {
|
||||
return HCF_NOT_SUPPORT;
|
||||
}
|
||||
|
||||
HcfAsyKeyGeneratorImpl *returnGenerator = (HcfAsyKeyGeneratorImpl *)HcfMalloc(sizeof(HcfAsyKeyGeneratorImpl), 0);
|
||||
if (returnGenerator == NULL) {
|
||||
LOGE("Failed to allocate returnGenerator memory!");
|
||||
return HCF_ERR_MALLOC;
|
||||
}
|
||||
if (strcpy_s(returnGenerator->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) {
|
||||
LOGE("Failed to copy algoName!");
|
||||
HcfFree(returnGenerator);
|
||||
return HCF_ERR_COPY;
|
||||
}
|
||||
HcfAsyKeyGeneratorSpi *spiObj = NULL;
|
||||
int32_t res = HCF_SUCCESS;
|
||||
res = createSpifunc(¶ms, &spiObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("Failed to create spi object!");
|
||||
HcfFree(returnGenerator);
|
||||
return res;
|
||||
}
|
||||
returnGenerator->base.base.destroy = DestroyAsyKeyGenerator;
|
||||
returnGenerator->base.base.getClass = GetAsyKeyGeneratorClass;
|
||||
returnGenerator->base.convertKey = ConvertKey;
|
||||
returnGenerator->base.generateKeyPair = GenerateKeyPair;
|
||||
returnGenerator->base.getAlgoName = GetAlgoName;
|
||||
returnGenerator->spiObj = spiObj;
|
||||
*returnObj = (HcfAsyKeyGenerator *)returnGenerator;
|
||||
return HCF_SUCCESS;
|
||||
}
|
222
frameworks/key/sym_key_generator.c
Normal file
222
frameworks/key/sym_key_generator.c
Normal file
@ -0,0 +1,222 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "sym_key_generator.h"
|
||||
#include "sym_key_factory_spi.h"
|
||||
#include "sym_common_defines.h"
|
||||
#include "params_parser.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include <securec.h>
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
#include "result.h"
|
||||
#include "config.h"
|
||||
|
||||
#define AES_KEY_SIZE_128 128
|
||||
#define AES_KEY_SIZE_192 192
|
||||
#define AES_KEY_SIZE_256 256
|
||||
#define DES_KEY_SIZE_192 192
|
||||
|
||||
typedef HcfResult (*SymKeyGeneratorSpiCreateFunc)(SymKeyAttr *, OH_HCF_SymKeyGeneratorSpi **);
|
||||
|
||||
typedef struct {
|
||||
SymKeyGeneratorSpiCreateFunc createFunc;
|
||||
} SymKeyGenFuncSet;
|
||||
|
||||
typedef struct {
|
||||
HCF_ALG_VALUE algo;
|
||||
SymKeyGenFuncSet funcSet;
|
||||
} SymKeyGenAbility;
|
||||
|
||||
typedef struct {
|
||||
HcfSymKeyGenerator base;
|
||||
OH_HCF_SymKeyGeneratorSpi *spiObj;
|
||||
char algoName[HCF_MAX_ALGO_NAME_LEN];
|
||||
} HcfSymmKeyGeneratorImpl;
|
||||
|
||||
static const SymKeyGenAbility SYMKEY_ABILITY_SET[] = {
|
||||
{ HCF_ALG_AES, { HcfSymKeyGeneratorSpiCreate }},
|
||||
{ HCF_ALG_DES, { HcfSymKeyGeneratorSpiCreate }}
|
||||
};
|
||||
|
||||
static const SymKeyGenFuncSet *FindAbility(SymKeyAttr *attr)
|
||||
{
|
||||
if (attr == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
for (uint32_t i = 0; i < sizeof(SYMKEY_ABILITY_SET); i++) {
|
||||
if (SYMKEY_ABILITY_SET[i].algo == attr->algo) {
|
||||
return &(SYMKEY_ABILITY_SET[i].funcSet);
|
||||
}
|
||||
}
|
||||
LOGE("Algo not support! [Algo]: %d", attr->algo);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void SetKeyLength(HCF_ALG_PARA_VALUE value, void *attr)
|
||||
{
|
||||
SymKeyAttr *keyAttr = (SymKeyAttr *)attr;
|
||||
|
||||
switch (value) {
|
||||
case HCF_ALG_AES_128:
|
||||
keyAttr->algo = HCF_ALG_AES;
|
||||
keyAttr->keySize = AES_KEY_SIZE_128;
|
||||
break;
|
||||
case HCF_ALG_AES_192:
|
||||
keyAttr->algo = HCF_ALG_AES;
|
||||
keyAttr->keySize = AES_KEY_SIZE_192;
|
||||
break;
|
||||
case HCF_ALG_AES_256:
|
||||
keyAttr->algo = HCF_ALG_AES;
|
||||
keyAttr->keySize = AES_KEY_SIZE_256;
|
||||
break;
|
||||
case HCF_ALG_3DES_192:
|
||||
keyAttr->algo = HCF_ALG_DES;
|
||||
keyAttr->keySize = DES_KEY_SIZE_192;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static HcfResult OnSetSymKeyParameter(const HcfParaConfig* config, void *attr)
|
||||
{
|
||||
if ((config == NULL) || (attr == NULL)) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcfResult ret = HCF_SUCCESS;
|
||||
LOGD("Set Parameter:%s\n", config->tag);
|
||||
switch (config->para_type) {
|
||||
case HCF_ALG_TYPE:
|
||||
case HCF_ALG_KEY_TYPE:
|
||||
SetKeyLength(config->para_value, attr);
|
||||
break;
|
||||
default:
|
||||
ret = HCF_INVALID_PARAMS;
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const char *GetSymKeyGeneratorClass()
|
||||
{
|
||||
return "HcfSymKeyGenerator";
|
||||
}
|
||||
|
||||
static const char *GetAlgoName(HcfSymKeyGenerator *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("The input self ptr is NULL!");
|
||||
return NULL;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetSymKeyGeneratorClass())) {
|
||||
LOGE("Class is not match!");
|
||||
return NULL;
|
||||
}
|
||||
return ((HcfSymmKeyGeneratorImpl *)self)->algoName;
|
||||
}
|
||||
|
||||
static void DestroySymmKeyGenerator(HcfObjectBase *base)
|
||||
{
|
||||
if (base == NULL) {
|
||||
return;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)base, GetSymKeyGeneratorClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return;
|
||||
}
|
||||
HcfSymmKeyGeneratorImpl *impl = (HcfSymmKeyGeneratorImpl *)base;
|
||||
if (impl->spiObj != NULL) {
|
||||
OH_HCF_ObjDestroy((HcfObjectBase *)impl->spiObj);
|
||||
}
|
||||
HcfFree(impl);
|
||||
}
|
||||
|
||||
static HcfResult GenerateSymmKey(HcfSymKeyGenerator *self, HcfSymKey **symmKey)
|
||||
{
|
||||
if ((self == NULL) || (symmKey == NULL)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetSymKeyGeneratorClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcfSymmKeyGeneratorImpl *impl = (HcfSymmKeyGeneratorImpl *)self;
|
||||
|
||||
return impl->spiObj->engineGenerateSymmKey(impl->spiObj, symmKey);
|
||||
}
|
||||
|
||||
static HcfResult ConvertSymmKey(HcfSymKeyGenerator *self, const HcfBlob *key, HcfSymKey **symmKey)
|
||||
{
|
||||
if ((self == NULL) || (symmKey == NULL) || !IsBlobValid(key)) {
|
||||
LOGE("Invalid input parameter.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetSymKeyGeneratorClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcfSymmKeyGeneratorImpl *impl = (HcfSymmKeyGeneratorImpl *)self;
|
||||
|
||||
return impl->spiObj->engineConvertSymmKey(impl->spiObj, key, symmKey);
|
||||
}
|
||||
|
||||
HcfResult HcfSymKeyGeneratorCreate(const char *algoName, HcfSymKeyGenerator **generator)
|
||||
{
|
||||
if (!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (generator == NULL)) {
|
||||
LOGE("Invalid input params while creating symkey!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
SymKeyAttr attr = {0};
|
||||
if (ParseAndSetParameter(algoName, (void *)&attr, OnSetSymKeyParameter) != HCF_SUCCESS) {
|
||||
LOGE("ParseAndSetParameter Failed!");
|
||||
return HCF_NOT_SUPPORT;
|
||||
}
|
||||
|
||||
const SymKeyGenFuncSet *funcSet = FindAbility(&attr);
|
||||
if (funcSet == NULL) {
|
||||
LOGE("FindAbility Failed!");
|
||||
return HCF_NOT_SUPPORT;
|
||||
}
|
||||
HcfSymmKeyGeneratorImpl *returnGenerator = (HcfSymmKeyGeneratorImpl *)HcfMalloc(sizeof(HcfSymmKeyGeneratorImpl), 0);
|
||||
if (returnGenerator == NULL) {
|
||||
LOGE("Failed to allocate returnGenerator memory!");
|
||||
return HCF_ERR_MALLOC;
|
||||
}
|
||||
if (strcpy_s(returnGenerator->algoName, HCF_MAX_ALGO_NAME_LEN, algoName)) {
|
||||
LOGE("Failed to copy algoName!");
|
||||
HcfFree(returnGenerator);
|
||||
return HCF_ERR_COPY;
|
||||
}
|
||||
OH_HCF_SymKeyGeneratorSpi *spiObj = NULL;
|
||||
int32_t res = funcSet->createFunc(&attr, &spiObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("Failed to create spi object!");
|
||||
HcfFree(returnGenerator);
|
||||
return res;
|
||||
}
|
||||
returnGenerator->base.generateSymKey = GenerateSymmKey;
|
||||
returnGenerator->base.convertSymKey = ConvertSymmKey;
|
||||
returnGenerator->base.base.destroy = DestroySymmKeyGenerator;
|
||||
returnGenerator->base.base.getClass = GetSymKeyGeneratorClass;
|
||||
returnGenerator->base.getAlgoName = GetAlgoName;
|
||||
returnGenerator->spiObj = spiObj;
|
||||
|
||||
*generator = (HcfSymKeyGenerator *)returnGenerator;
|
||||
return HCF_SUCCESS;
|
||||
}
|
134
frameworks/rand/rand.c
Normal file
134
frameworks/rand/rand.c
Normal file
@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 "rand.h"
|
||||
#include <securec.h>
|
||||
#include "rand_spi.h"
|
||||
#include "rand_openssl.h"
|
||||
#include "log.h"
|
||||
#include "config.h"
|
||||
#include "memory.h"
|
||||
#include "utils.h"
|
||||
|
||||
typedef HcfResult (*HcfRandSpiCreateFunc)(HcfRandSpi **);
|
||||
|
||||
typedef struct {
|
||||
HcfRand base;
|
||||
|
||||
HcfRandSpi *spiObj;
|
||||
|
||||
const char *algoName;
|
||||
} HcfRandImpl;
|
||||
|
||||
typedef struct {
|
||||
char *algoName;
|
||||
|
||||
HcfRandSpiCreateFunc createSpifunc;
|
||||
} HcfRandAbility;
|
||||
|
||||
static const HcfRandAbility RAND_ABILITY_SET[] = {
|
||||
{ "OpensslRand", HcfRandSpiCreate }
|
||||
};
|
||||
|
||||
static const char *GetRandClass(void)
|
||||
{
|
||||
return "Rand";
|
||||
}
|
||||
|
||||
static HcfRandSpiCreateFunc FindAbility(const char *algoName)
|
||||
{
|
||||
for (uint32_t i = 0; i < (sizeof(RAND_ABILITY_SET) / sizeof(RAND_ABILITY_SET[0])); i++) {
|
||||
if (strcmp(RAND_ABILITY_SET[i].algoName, algoName) == 0) {
|
||||
return RAND_ABILITY_SET[i].createSpifunc;
|
||||
}
|
||||
}
|
||||
LOGE("Algo not support! [Algo]: %s", algoName);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static HcfResult GenerateRandom(HcfRand *self, int32_t numBytes, HcfBlob *random)
|
||||
{
|
||||
if ((self == NULL) || (numBytes <= 0) || (random == NULL)) {
|
||||
LOGE("Invalid params!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetRandClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfRandImpl *)self)->spiObj->engineGenerateRandom(
|
||||
((HcfRandImpl *)self)->spiObj, numBytes, random);
|
||||
}
|
||||
|
||||
static HcfResult SetSeed(HcfRand *self, HcfBlob *seed)
|
||||
{
|
||||
if ((self == NULL) || (!IsBlobValid(seed))) {
|
||||
LOGE("The input self ptr is NULL!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetRandClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
return ((HcfRandImpl *)self)->spiObj->engineSetSeed(
|
||||
((HcfRandImpl *)self)->spiObj, seed);
|
||||
}
|
||||
|
||||
static void HcfRandDestroy(HcfObjectBase *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
LOGE("The input self ptr is NULL!");
|
||||
return;
|
||||
}
|
||||
if (!IsClassMatch((HcfObjectBase *)self, GetRandClass())) {
|
||||
LOGE("Class is not match.");
|
||||
return;
|
||||
}
|
||||
HcfRandImpl *impl = (HcfRandImpl *)self;
|
||||
OH_HCF_ObjDestroy(impl->spiObj);
|
||||
HcfFree(impl);
|
||||
}
|
||||
|
||||
HcfResult HcfRandCreate(HcfRand **randApi)
|
||||
{
|
||||
if (randApi == NULL) {
|
||||
LOGE("Invalid input params while creating rand!");
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcfRandSpiCreateFunc createSpifunc = FindAbility("OpensslRand");
|
||||
if (createSpifunc == NULL) {
|
||||
LOGE("Algo not supported!");
|
||||
return HCF_NOT_SUPPORT;
|
||||
}
|
||||
HcfRandImpl *returnRandApi = (HcfRandImpl *)HcfMalloc(sizeof(HcfRandImpl), 0);
|
||||
if (returnRandApi == NULL) {
|
||||
LOGE("Failed to allocate Rand Obj memory!");
|
||||
return HCF_ERR_MALLOC;
|
||||
}
|
||||
HcfRandSpi *spiObj = NULL;
|
||||
HcfResult res = createSpifunc(&spiObj);
|
||||
if (res != HCF_SUCCESS) {
|
||||
LOGE("Failed to create spi object!");
|
||||
HcfFree(returnRandApi);
|
||||
return res;
|
||||
}
|
||||
returnRandApi->base.base.getClass = GetRandClass;
|
||||
returnRandApi->base.base.destroy = HcfRandDestroy;
|
||||
returnRandApi->base.generateRandom = GenerateRandom;
|
||||
returnRandApi->base.setSeed = SetSeed;
|
||||
returnRandApi->spiObj = spiObj;
|
||||
*randApi = (HcfRand *)returnRandApi;
|
||||
return HCF_SUCCESS;
|
||||
}
|
45
frameworks/spi/asy_key_generator_spi.h
Normal file
45
frameworks/spi/asy_key_generator_spi.h
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_ASY_KEY_GENERATOR_SPI_H
|
||||
#define HCF_ASY_KEY_GENERATOR_SPI_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "algorithm_parameter.h"
|
||||
#include "result.h"
|
||||
#include "key_pair.h"
|
||||
|
||||
#define OPENSSL_ASY_KEYGEN_PARAMS_TYPE "OPENSSL.ASY.KEYGENPARAMS"
|
||||
|
||||
#define OPENSSL_RSA_GENERATOR_CLASS "OPENSSL.RSA.KEYGENERATOR"
|
||||
|
||||
#define OPENSSL_RSA_KEY_FORMAT "OPENSSL.RSA.KEYFORMAT.DEFAULT"
|
||||
|
||||
#define OPENSSL_RSA_ALGORITHM "OPENSSL.RSA"
|
||||
|
||||
#define OPENSSL_RSA_KEY_FORMAT "OPENSSL.RSA.KEYFORMAT.DEFAULT"
|
||||
|
||||
typedef struct HcfAsyKeyGeneratorSpi HcfAsyKeyGeneratorSpi;
|
||||
|
||||
struct HcfAsyKeyGeneratorSpi {
|
||||
HcfObjectBase base;
|
||||
|
||||
HcfResult (*engineGenerateKeyPair)(HcfAsyKeyGeneratorSpi *self, HcfKeyPair **returnObj);
|
||||
|
||||
HcfResult (*engineConvertKey)(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob,
|
||||
HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair);
|
||||
};
|
||||
|
||||
#endif
|
30
frameworks/spi/cert_chain_validator_spi.h
Normal file
30
frameworks/spi/cert_chain_validator_spi.h
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_CERT_CHAIN_VALIDATOR_SPI_H
|
||||
#define HCF_CERT_CHAIN_VALIDATOR_SPI_H
|
||||
|
||||
#include "blob.h"
|
||||
#include "object_base.h"
|
||||
#include "result.h"
|
||||
|
||||
typedef struct HcfCertChainValidatorSpi HcfCertChainValidatorSpi;
|
||||
|
||||
struct HcfCertChainValidatorSpi {
|
||||
HcfObjectBase base;
|
||||
HcfResult (*engineValidate)(HcfCertChainValidatorSpi *self, const HcfArray *certsList);
|
||||
};
|
||||
|
||||
#endif // HCF_CERT_CHAIN_VALIDATOR_SPI_H
|
40
frameworks/spi/cipher_factory_spi.h
Normal file
40
frameworks/spi/cipher_factory_spi.h
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_CIPHER_FACTORY_SPI_H
|
||||
#define HCF_CIPHER_FACTORY_SPI_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "cipher.h"
|
||||
#include "algorithm_parameter.h"
|
||||
#include "key.h"
|
||||
#include "blob.h"
|
||||
#include "result.h"
|
||||
#include "params_parser.h"
|
||||
|
||||
typedef struct OH_HCF_CipherGeneratorSpi OH_HCF_CipherGeneratorSpi;
|
||||
|
||||
struct OH_HCF_CipherGeneratorSpi {
|
||||
HcfObjectBase base;
|
||||
|
||||
HcfResult (*init)(OH_HCF_CipherGeneratorSpi *self, enum HcfCryptoMode opMode,
|
||||
HcfKey *key, HcfParamsSpec *params);
|
||||
|
||||
HcfResult (*update)(OH_HCF_CipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output);
|
||||
|
||||
HcfResult (*doFinal)(OH_HCF_CipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output);
|
||||
};
|
||||
|
||||
#endif
|
33
frameworks/spi/key_agreement_spi.h
Normal file
33
frameworks/spi/key_agreement_spi.h
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_KEY_AGREEMENT_SPI_H
|
||||
#define HCF_KEY_AGREEMENT_SPI_H
|
||||
|
||||
#include "blob.h"
|
||||
#include "pri_key.h"
|
||||
#include "pub_key.h"
|
||||
#include "result.h"
|
||||
|
||||
typedef struct HcfKeyAgreementSpi HcfKeyAgreementSpi;
|
||||
|
||||
struct HcfKeyAgreementSpi {
|
||||
HcfObjectBase base;
|
||||
|
||||
HcfResult (*engineGenerateSecret)(HcfKeyAgreementSpi *self, HcfPriKey *priKey,
|
||||
HcfPubKey *pubKey, HcfBlob *returnSecret);
|
||||
};
|
||||
|
||||
#endif
|
37
frameworks/spi/mac_spi.h
Normal file
37
frameworks/spi/mac_spi.h
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_MAC_SPI_H
|
||||
#define HCF_MAC_SPI_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "result.h"
|
||||
#include "sym_key.h"
|
||||
|
||||
typedef struct HcfMacSpi HcfMacSpi;
|
||||
|
||||
struct HcfMacSpi {
|
||||
HcfObjectBase base;
|
||||
// init the Mac with given key
|
||||
HcfResult (*engineInitMac)(HcfMacSpi *self, const HcfSymKey *key);
|
||||
// update mac with input datablob
|
||||
HcfResult (*engineUpdateMac)(HcfMacSpi *self, HcfBlob *input);
|
||||
// output mac in output datablob
|
||||
HcfResult (*engineDoFinalMac)(HcfMacSpi *self, HcfBlob *output);
|
||||
// get the length of chosen hash algo
|
||||
uint32_t (*engineGetMacLength)(HcfMacSpi *self);
|
||||
};
|
||||
|
||||
#endif
|
36
frameworks/spi/md_spi.h
Normal file
36
frameworks/spi/md_spi.h
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_MD_SPI_H
|
||||
#define HCF_MD_SPI_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "result.h"
|
||||
#include "blob.h"
|
||||
#include "object_base.h"
|
||||
|
||||
typedef struct HcfMdSpi HcfMdSpi;
|
||||
|
||||
struct HcfMdSpi {
|
||||
HcfObjectBase base;
|
||||
|
||||
HcfResult (*engineUpdateMd)(HcfMdSpi *self, HcfBlob *input);
|
||||
|
||||
HcfResult (*engineDoFinalMd)(HcfMdSpi *self, HcfBlob *output);
|
||||
|
||||
uint32_t (*engineGetMdLength)(HcfMdSpi *self);
|
||||
};
|
||||
|
||||
#endif
|
34
frameworks/spi/rand_spi.h
Normal file
34
frameworks/spi/rand_spi.h
Normal file
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_RAND_SPI_H
|
||||
#define HCF_RAND_SPI_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "result.h"
|
||||
#include "blob.h"
|
||||
#include "object_base.h"
|
||||
|
||||
typedef struct HcfRandSpi HcfRandSpi;
|
||||
|
||||
struct HcfRandSpi {
|
||||
HcfObjectBase base;
|
||||
|
||||
HcfResult (*engineGenerateRandom)(HcfRandSpi *self, int32_t numBytes, HcfBlob *random);
|
||||
|
||||
HcfResult (*engineSetSeed)(HcfRandSpi *self, HcfBlob *seed);
|
||||
};
|
||||
|
||||
#endif
|
53
frameworks/spi/signature_spi.h
Normal file
53
frameworks/spi/signature_spi.h
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_SIGNATURE_SPI_H
|
||||
#define HCF_SIGNATURE_SPI_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "algorithm_parameter.h"
|
||||
#include "pri_key.h"
|
||||
#include "pub_key.h"
|
||||
#include "result.h"
|
||||
|
||||
#define OPENSSL_RSA_SIGN_CLASS "OPENSSL.RSA.SIGN"
|
||||
|
||||
#define OPENSSL_RSA_VERIFY_CLASS "OPENSSL.RSA.VERIFY"
|
||||
|
||||
typedef struct HcfSignSpi HcfSignSpi;
|
||||
|
||||
struct HcfSignSpi {
|
||||
HcfObjectBase base;
|
||||
|
||||
HcfResult (*engineInit)(HcfSignSpi *self, HcfParamsSpec *params, HcfPriKey *privateKey);
|
||||
|
||||
HcfResult (*engineUpdate)(HcfSignSpi *self, HcfBlob *data);
|
||||
|
||||
HcfResult (*engineSign)(HcfSignSpi *self, HcfBlob *data, HcfBlob *returnSignatureData);
|
||||
};
|
||||
|
||||
typedef struct HcfVerifySpi HcfVerifySpi;
|
||||
|
||||
struct HcfVerifySpi {
|
||||
HcfObjectBase base;
|
||||
|
||||
HcfResult (*engineInit)(HcfVerifySpi *self, HcfParamsSpec *params, HcfPubKey *publicKey);
|
||||
|
||||
HcfResult (*engineUpdate)(HcfVerifySpi *self, HcfBlob *data);
|
||||
|
||||
bool (*engineVerify)(HcfVerifySpi *self, HcfBlob *data, HcfBlob *signatureData);
|
||||
};
|
||||
|
||||
#endif
|
33
frameworks/spi/sym_key_factory_spi.h
Normal file
33
frameworks/spi/sym_key_factory_spi.h
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_SYM_KEY_FACTORY_SPI_H
|
||||
#define HCF_SYM_KEY_FACTORY_SPI_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "result.h"
|
||||
#include "sym_key.h"
|
||||
|
||||
typedef struct OH_HCF_SymKeyGeneratorSpi OH_HCF_SymKeyGeneratorSpi;
|
||||
|
||||
struct OH_HCF_SymKeyGeneratorSpi {
|
||||
HcfObjectBase base;
|
||||
HcfResult (*engineGenerateSymmKey)(OH_HCF_SymKeyGeneratorSpi *self, HcfSymKey **symmKey);
|
||||
HcfResult (*engineConvertSymmKey)(OH_HCF_SymKeyGeneratorSpi *self, const HcfBlob *key, HcfSymKey **symmKey);
|
||||
};
|
||||
#define OPENSSL_SYM_GENERATOR_CLASS "OPENSSL.SYM.KEYGENERATOR"
|
||||
#define OPENSSL_SYM_KEY_CLASS "OPENSSL.SYM.KEY"
|
||||
|
||||
#endif // HCF_SYMM_KEY_FACTORY_SPI_H
|
68
frameworks/spi/x509_certificate_spi.h
Normal file
68
frameworks/spi/x509_certificate_spi.h
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_X509_CERTIFICATE_SPI_H
|
||||
#define HCF_X509_CERTIFICATE_SPI_H
|
||||
|
||||
#include "blob.h"
|
||||
#include "object_base.h"
|
||||
#include "pub_key.h"
|
||||
#include "result.h"
|
||||
|
||||
typedef struct HcfX509CertificateSpi HcfX509CertificateSpi;
|
||||
|
||||
struct HcfX509CertificateSpi {
|
||||
HcfObjectBase base;
|
||||
|
||||
HcfResult (*engineVerify)(HcfX509CertificateSpi *self, HcfPubKey *key);
|
||||
|
||||
HcfResult (*engineGetEncoded)(HcfX509CertificateSpi *self, HcfEncodingBlob *encodedByte);
|
||||
|
||||
HcfResult (*engineGetPublicKey)(HcfX509CertificateSpi *self, HcfPubKey **keyOut);
|
||||
|
||||
HcfResult (*engineCheckValidityWithDate)(HcfX509CertificateSpi *self, const char *date);
|
||||
|
||||
long (*engineGetVersion)(HcfX509CertificateSpi *self);
|
||||
|
||||
long (*engineGetSerialNumber)(HcfX509CertificateSpi *self);
|
||||
|
||||
HcfResult (*engineGetIssuerName)(HcfX509CertificateSpi *self, HcfBlob *out);
|
||||
|
||||
HcfResult (*engineGetSubjectName)(HcfX509CertificateSpi *self, HcfBlob *out);
|
||||
|
||||
HcfResult (*engineGetNotBeforeTime)(HcfX509CertificateSpi *self, HcfBlob *outDate);
|
||||
|
||||
HcfResult (*engineGetNotAfterTime)(HcfX509CertificateSpi *self, HcfBlob *outDate);
|
||||
|
||||
HcfResult (*engineGetSignature)(HcfX509CertificateSpi *self, HcfBlob *sigOut);
|
||||
|
||||
HcfResult (*engineGetSignatureAlgName)(HcfX509CertificateSpi *self, HcfBlob *outName);
|
||||
|
||||
HcfResult (*engineGetSignatureAlgOid)(HcfX509CertificateSpi *self, HcfBlob *out);
|
||||
|
||||
HcfResult (*engineGetSignatureAlgParams)(HcfX509CertificateSpi *self, HcfBlob *sigAlgParamsOut);
|
||||
|
||||
HcfResult (*engineGetKeyUsage)(HcfX509CertificateSpi *self, HcfBlob *boolArr);
|
||||
|
||||
HcfResult (*engineGetExtKeyUsage)(HcfX509CertificateSpi *self, HcfArray *keyUsageOut);
|
||||
|
||||
int32_t (*engineGetBasicConstraints)(HcfX509CertificateSpi *self);
|
||||
|
||||
HcfResult (*engineGetSubjectAltNames)(HcfX509CertificateSpi *self, HcfArray *outName);
|
||||
|
||||
HcfResult (*engineGetIssuerAltNames)(HcfX509CertificateSpi *self, HcfArray *outName);
|
||||
};
|
||||
|
||||
#endif // HCF_X509_CERTIFICATE_SPI_H
|
65
frameworks/spi/x509_crl_spi.h
Normal file
65
frameworks/spi/x509_crl_spi.h
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_X509_CRL_SPI_H
|
||||
#define HCF_X509_CRL_SPI_H
|
||||
|
||||
#include "blob.h"
|
||||
#include "object_base.h"
|
||||
#include "pub_key.h"
|
||||
#include "result.h"
|
||||
#include "x509_certificate.h"
|
||||
#include "x509_crl_entry.h"
|
||||
|
||||
typedef struct HcfX509CrlSpi HcfX509CrlSpi;
|
||||
|
||||
struct HcfX509CrlSpi {
|
||||
HcfObjectBase base;
|
||||
|
||||
const char *(*engineGetType)(HcfX509CrlSpi *self);
|
||||
|
||||
bool (*engineIsRevoked)(HcfX509CrlSpi *self, const HcfCertificate *cert);
|
||||
|
||||
HcfResult (*engineGetEncoded)(HcfX509CrlSpi *self, HcfEncodingBlob *encodedByte);
|
||||
|
||||
HcfResult (*engineVerify)(HcfX509CrlSpi *self, HcfPubKey *key);
|
||||
|
||||
long (*engineGetVersion)(HcfX509CrlSpi *self);
|
||||
|
||||
HcfResult (*engineGetIssuerName)(HcfX509CrlSpi *self, HcfBlob *out);
|
||||
|
||||
HcfResult (*engineGetLastUpdate)(HcfX509CrlSpi *self, HcfBlob *out);
|
||||
|
||||
HcfResult (*engineGetNextUpdate)(HcfX509CrlSpi *self, HcfBlob *out);
|
||||
|
||||
HcfResult (*engineGetRevokedCert)(HcfX509CrlSpi *self, long serialNumber, HcfX509CrlEntry **entryOut);
|
||||
|
||||
HcfResult (*engineGetRevokedCertWithCert)(HcfX509CrlSpi *self, HcfX509Certificate *cert,
|
||||
HcfX509CrlEntry **entryOut);
|
||||
|
||||
HcfResult (*engineGetRevokedCerts)(HcfX509CrlSpi *self, HcfArray *entrysOut);
|
||||
|
||||
HcfResult (*engineGetTbsInfo)(HcfX509CrlSpi *self, HcfBlob *tbsCertListOut);
|
||||
|
||||
HcfResult (*engineGetSignature)(HcfX509CrlSpi *self, HcfBlob *signature);
|
||||
|
||||
HcfResult (*engineGetSignatureAlgName)(HcfX509CrlSpi *self, HcfBlob *out);
|
||||
|
||||
HcfResult (*engineGetSignatureAlgOid)(HcfX509CrlSpi *self, HcfBlob *out);
|
||||
|
||||
HcfResult (*engineGetSignatureAlgParams)(HcfX509CrlSpi *self, HcfBlob *sigAlgParamOut);
|
||||
};
|
||||
|
||||
#endif // HCF_X509_CERTIFICATE_SPI_H
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_ALGORITHM_PARAMETER_H
|
||||
#define HCF_ALGORITHM_PARAMETER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* @brief Provides an HcfParamsSpec instance.
|
||||
*
|
||||
* @since 9
|
||||
* @version 1.0
|
||||
*/
|
||||
typedef struct HcfParamsSpec HcfParamsSpec;
|
||||
|
||||
/**
|
||||
* @brief Provide transparent algorithm parameters.
|
||||
*
|
||||
* @since 9
|
||||
* @version 1.0
|
||||
*/
|
||||
struct HcfParamsSpec {
|
||||
/** Get the actual type of the current algorithm parameter. */
|
||||
const char *(*getType)(void);
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_DETAILED_CCM_PARAMS_H
|
||||
#define HCF_DETAILED_CCM_PARAMS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "algorithm_parameter.h"
|
||||
#include "blob.h"
|
||||
|
||||
typedef struct HcfCcmParamsSpec HcfCcmParamsSpec;
|
||||
|
||||
struct HcfCcmParamsSpec {
|
||||
HcfParamsSpec base;
|
||||
HcfBlob iv;
|
||||
HcfBlob aad;
|
||||
HcfBlob tag;
|
||||
};
|
||||
|
||||
#endif // HCF_DETAILED_CCM_PARAMS_H
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_DETAILED_GCM_PARAMS_H
|
||||
#define HCF_DETAILED_GCM_PARAMS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "algorithm_parameter.h"
|
||||
#include "blob.h"
|
||||
|
||||
typedef struct HcfGcmParamsSpec HcfGcmParamsSpec;
|
||||
|
||||
struct HcfGcmParamsSpec {
|
||||
HcfParamsSpec base;
|
||||
HcfBlob iv;
|
||||
HcfBlob aad;
|
||||
HcfBlob tag;
|
||||
};
|
||||
|
||||
#endif // HCF_DETAILED_GCM_PARAMS_H
|
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_DETAILED_IV_PARAMS_H
|
||||
#define HCF_DETAILED_IV_PARAMS_H
|
||||
|
||||
#include "algorithm_parameter.h"
|
||||
#include "blob.h"
|
||||
|
||||
typedef struct HcfIvParamsSpec HcfIvParamsSpec;
|
||||
|
||||
struct HcfIvParamsSpec {
|
||||
HcfParamsSpec base;
|
||||
HcfBlob iv;
|
||||
};
|
||||
|
||||
#endif // HCF_DETAILED_IV_PARAMS_H
|
58
interfaces/innerkits/certificate/cert_chain_validator.h
Normal file
58
interfaces/innerkits/certificate/cert_chain_validator.h
Normal file
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_CERT_CHAIN_VALIDATOR_H
|
||||
#define HCF_CERT_CHAIN_VALIDATOR_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "blob.h"
|
||||
#include "object_base.h"
|
||||
#include "result.h"
|
||||
|
||||
typedef struct HcfCertChainValidator HcfCertChainValidator;
|
||||
|
||||
typedef struct {
|
||||
/* data format: len-value-len-value..., size of len is 2 bytes. */
|
||||
uint8_t *data;
|
||||
uint32_t dataLen;
|
||||
uint8_t count;
|
||||
enum EncodingFormat format;
|
||||
} HcfCertChainData;
|
||||
|
||||
struct HcfCertChainValidator {
|
||||
struct HcfObjectBase base;
|
||||
|
||||
/** verify the cert chain. */
|
||||
HcfResult (*validate)(HcfCertChainValidator *self, const HcfCertChainData *certChainData);
|
||||
|
||||
/** Get algorithm name. */
|
||||
const char *(*getAlgorithm)(HcfCertChainValidator *self);
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Generate cert chain validator instance.
|
||||
*/
|
||||
HcfResult HcfCertChainValidatorCreate(const char *algorithm, HcfCertChainValidator **pathValidator);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // HCF_CERT_CHAIN_VALIDATOR_H
|
39
interfaces/innerkits/certificate/certificate.h
Normal file
39
interfaces/innerkits/certificate/certificate.h
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_CERTIFICATE_H
|
||||
#define HCF_CERTIFICATE_H
|
||||
|
||||
#include "blob.h"
|
||||
#include "object_base.h"
|
||||
#include "pub_key.h"
|
||||
#include "result.h"
|
||||
|
||||
typedef struct HcfCertificate HcfCertificate;
|
||||
|
||||
struct HcfCertificate {
|
||||
struct HcfObjectBase base;
|
||||
|
||||
/** Verify that this certificate corresponding to the specified public key. */
|
||||
HcfResult (*verify)(HcfCertificate *self, HcfPubKey *key);
|
||||
|
||||
/** Get the serialized cert data.*/
|
||||
HcfResult (*getEncoded)(HcfCertificate *self, HcfEncodingBlob *encodedByte);
|
||||
|
||||
/** Get the public key from this certificate. */
|
||||
HcfResult (*getPublicKey)(HcfCertificate *self, HcfPubKey **keyOut);
|
||||
};
|
||||
|
||||
#endif // HCF_CERTIFICATE_H
|
36
interfaces/innerkits/certificate/crl.h
Normal file
36
interfaces/innerkits/certificate/crl.h
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_CRL_H
|
||||
#define HCF_CRL_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "certificate.h"
|
||||
|
||||
typedef struct HcfCrl HcfCrl;
|
||||
|
||||
struct HcfCrl {
|
||||
/** HcfCrl inherit HcfObjectBase. */
|
||||
struct HcfObjectBase base;
|
||||
|
||||
/** Check if the given certificate is on this CRL. */
|
||||
bool (*isRevoked)(HcfCrl *self, const HcfCertificate *cert);
|
||||
|
||||
/** Returns the type of this CRL. */
|
||||
const char *(*getType)(HcfCrl *self);
|
||||
};
|
||||
|
||||
#endif // HCF_CRL_H
|
99
interfaces/innerkits/certificate/x509_certificate.h
Normal file
99
interfaces/innerkits/certificate/x509_certificate.h
Normal file
@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_X509_CERTIFICATE_H
|
||||
#define HCF_X509_CERTIFICATE_H
|
||||
|
||||
#include "certificate.h"
|
||||
#include "blob.h"
|
||||
#include "result.h"
|
||||
|
||||
typedef struct HcfX509Certificate HcfX509Certificate;
|
||||
|
||||
struct HcfX509Certificate {
|
||||
/** HcfCX509Certificate inherit HcfCertificate. */
|
||||
HcfCertificate base;
|
||||
|
||||
/** Check whether the certificate is valid at the given time.
|
||||
* time format: YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ
|
||||
*/
|
||||
HcfResult (*checkValidityWithDate)(HcfX509Certificate *self, const char *date);
|
||||
|
||||
/** Get version number from certificate. */
|
||||
long (*getVersion)(HcfX509Certificate *self);
|
||||
|
||||
/** Get serial number from certificate. */
|
||||
long (*getSerialNumber)(HcfX509Certificate *self);
|
||||
|
||||
/** Get issuer distinguished name from certificate. */
|
||||
HcfResult (*getIssuerName)(HcfX509Certificate *self, HcfBlob *out);
|
||||
|
||||
/** Get subject distinguished name from certificate. */
|
||||
HcfResult (*getSubjectName)(HcfX509Certificate *self, HcfBlob *out);
|
||||
|
||||
/** Get the not before time within the validity period of the certificate.
|
||||
* time format: YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ
|
||||
*/
|
||||
HcfResult (*getNotBeforeTime)(HcfX509Certificate *self, HcfBlob *outDate);
|
||||
|
||||
/** Get the not after time within the validity period of the certificate.
|
||||
* time format: YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ
|
||||
*/
|
||||
HcfResult (*getNotAfterTime)(HcfX509Certificate *self, HcfBlob *outDate);
|
||||
|
||||
/** Get signature value from certificate. */
|
||||
HcfResult (*getSignature)(HcfX509Certificate *self, HcfBlob *sigOut);
|
||||
|
||||
/** Get signature algorithm name from certificate. */
|
||||
HcfResult (*getSignatureAlgName)(HcfX509Certificate *self, HcfBlob *outName);
|
||||
|
||||
/** Get signature algorithm oid from certificate. */
|
||||
HcfResult (*getSignatureAlgOid)(HcfX509Certificate *self, HcfBlob *out);
|
||||
|
||||
/** Get the DER encoded signature algorithm parameters from the signature algorithm of the certificate. */
|
||||
HcfResult (*getSignatureAlgParams)(HcfX509Certificate *self, HcfBlob *sigAlgParamsOut);
|
||||
|
||||
/** Get a Boolean array representing the bits of keyuse extension.
|
||||
* The key usage extension defines the purpose of the key. */
|
||||
HcfResult (*getKeyUsage)(HcfX509Certificate *self, HcfBlob *boolArr);
|
||||
|
||||
/** Get a const string list that represents the object identifier of the extkeyusage. */
|
||||
HcfResult (*getExtKeyUsage)(HcfX509Certificate *self, HcfArray *keyUsageOut);
|
||||
|
||||
/** Get the path length of the certificate constraint from the key extensions(BasicConstraints).
|
||||
* The BasicConstraints identify whether the issuer of the certificate is CA and the depth of the cert chain.
|
||||
* Only when CA is set to true, pathLenConstraint is meaningful.
|
||||
*/
|
||||
int32_t (*getBasicConstraints)(HcfX509Certificate *self);
|
||||
|
||||
/** Get subject alternative name from certificate. */
|
||||
HcfResult (*getSubjectAltNames)(HcfX509Certificate *self, HcfArray *outName);
|
||||
|
||||
/** Get issuer alternative name from certificate. */
|
||||
HcfResult (*getIssuerAltNames)(HcfX509Certificate *self, HcfArray *outName);
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
HcfResult HcfX509CertificateCreate(const HcfEncodingBlob *inStream, HcfX509Certificate **returnObj);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // HCF_X509_CERTIFICATE_H
|
||||
|
85
interfaces/innerkits/certificate/x509_crl.h
Normal file
85
interfaces/innerkits/certificate/x509_crl.h
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_X509CRL_H
|
||||
#define HCF_X509CRL_H
|
||||
|
||||
#include "blob.h"
|
||||
#include "crl.h"
|
||||
#include "pub_key.h"
|
||||
#include "x509_certificate.h"
|
||||
#include "x509_crl_entry.h"
|
||||
|
||||
typedef struct HcfX509Crl HcfX509Crl;
|
||||
|
||||
struct HcfX509Crl {
|
||||
/** HcfX509Crl inherit HcfCrl. */
|
||||
HcfCrl base;
|
||||
|
||||
/** Get the der coding format. */
|
||||
HcfResult (*getEncoded)(HcfX509Crl *self, HcfEncodingBlob *encodedOut);
|
||||
|
||||
/** Use the public key to verify the signature of CRL. */
|
||||
HcfResult (*verify)(HcfX509Crl *self, HcfPubKey *key);
|
||||
|
||||
/** Get version number from CRL. */
|
||||
long (*getVersion)(HcfX509Crl *self);
|
||||
|
||||
/** Get the issuer name from CRL. Issuer means the entity that signs and publishes the CRL. */
|
||||
HcfResult (*getIssuerName)(HcfX509Crl *self, HcfBlob *out);
|
||||
|
||||
/** Get lastUpdate value from CRL. */
|
||||
HcfResult (*getLastUpdate)(HcfX509Crl *self, HcfBlob *out);
|
||||
|
||||
/** Get nextUpdate value from CRL. */
|
||||
HcfResult (*getNextUpdate)(HcfX509Crl *self, HcfBlob *out);
|
||||
|
||||
/** This method can be used to find CRL entries in indirect CRLs. */
|
||||
HcfResult (*getRevokedCert)(HcfX509Crl *self, long serialNumber, HcfX509CrlEntry **entryOut);
|
||||
|
||||
/** This method can be used to find CRL entries in indirect cert. */
|
||||
HcfResult (*getRevokedCertWithCert)(HcfX509Crl *self, HcfX509Certificate *cert,
|
||||
HcfX509CrlEntry **entryOut);
|
||||
|
||||
/** Get all entries in this CRL. */
|
||||
HcfResult (*getRevokedCerts)(HcfX509Crl *self, HcfArray *entrysOut);
|
||||
|
||||
/** Get the CRL information encoded by Der from this CRL. */
|
||||
HcfResult (*getTbsInfo)(HcfX509Crl *self, HcfBlob *tbsCertListOut);
|
||||
|
||||
/** Get signature value from CRL. */
|
||||
HcfResult (*getSignature)(HcfX509Crl *self, HcfBlob *signature);
|
||||
|
||||
/** Get the signature algorithm name of the CRL signature algorithm. */
|
||||
HcfResult (*getSignatureAlgName)(HcfX509Crl *self, HcfBlob *out);
|
||||
|
||||
/** Get the signature algorithm oid string from CRL. */
|
||||
HcfResult (*getSignatureAlgOid)(HcfX509Crl *self, HcfBlob *out);
|
||||
|
||||
/** Get the der encoded signature algorithm parameters from the CRL signature algorithm. */
|
||||
HcfResult (*getSignatureAlgParams)(HcfX509Crl *self, HcfBlob *sigAlgParamOut);
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
HcfResult HcfX509CrlCreate(const HcfEncodingBlob *inStream, HcfX509Crl **returnObj);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // HCF_X509CRL_H
|
42
interfaces/innerkits/certificate/x509_crl_entry.h
Normal file
42
interfaces/innerkits/certificate/x509_crl_entry.h
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_X509_CRL_ENTRY_H
|
||||
#define HCF_X509_CRL_ENTRY_H
|
||||
|
||||
#include "blob.h"
|
||||
#include "object_base.h"
|
||||
#include "result.h"
|
||||
|
||||
typedef struct HcfX509CrlEntry HcfX509CrlEntry;
|
||||
|
||||
struct HcfX509CrlEntry {
|
||||
/** HcfX509CrlEntry inherit HcfObjectBase. */
|
||||
struct HcfObjectBase base;
|
||||
|
||||
/** Returns the ASN of this CRL entry 1 der coding form, i.e. internal sequence. */
|
||||
HcfResult (*getEncoded)(HcfX509CrlEntry *self, HcfEncodingBlob *encodedOut);
|
||||
|
||||
/** Get the serial number from this x509crl entry. */
|
||||
long (*getSerialNumber)(HcfX509CrlEntry *self);
|
||||
|
||||
/** Gets the issuer of the x509 certificate described by this entry. */
|
||||
HcfResult (*getCertIssuer)(HcfX509CrlEntry *self, HcfBlob *encodedOut);
|
||||
|
||||
/** Get the revocation date from x509crl entry. */
|
||||
HcfResult (*getRevocationDate)(HcfX509CrlEntry *self, HcfBlob *out);
|
||||
};
|
||||
|
||||
#endif // HCF_X509_CRL_ENTRY_H
|
58
interfaces/innerkits/common/blob.h
Normal file
58
interfaces/innerkits/common/blob.h
Normal file
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_BLOB_H
|
||||
#define HCF_BLOB_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct HcfBlob HcfBlob;
|
||||
struct HcfBlob {
|
||||
uint8_t *data;
|
||||
size_t len;
|
||||
};
|
||||
|
||||
enum EncodingFormat {
|
||||
HCF_FORMAT_DER = 0,
|
||||
HCF_FORMAT_PEM = 1,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
uint8_t *data;
|
||||
size_t len;
|
||||
enum EncodingFormat encodingFormat;
|
||||
} HcfEncodingBlob;
|
||||
|
||||
typedef struct {
|
||||
HcfBlob *data;
|
||||
enum EncodingFormat format;
|
||||
uint32_t count;
|
||||
} HcfArray;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void HcfBlobDataFree(HcfBlob *blob);
|
||||
void HcfBlobDataClearAndFree(HcfBlob *blob);
|
||||
void HcfEncodingBlobDataFree(HcfEncodingBlob *blob);
|
||||
void HcfArrayDataClearAndFree(HcfArray *array);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
31
interfaces/innerkits/common/object_base.h
Normal file
31
interfaces/innerkits/common/object_base.h
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.
|
||||
*/
|
||||
|
||||
#ifndef HCF_OBJECT_BASE_H
|
||||
#define HCF_OBJECT_BASE_H
|
||||
|
||||
typedef struct HcfObjectBase HcfObjectBase;
|
||||
|
||||
struct HcfObjectBase {
|
||||
const char *(*getClass)(void);
|
||||
void (*destroy)(HcfObjectBase *self);
|
||||
};
|
||||
|
||||
#define OH_HCF_ObjDestroy(base) \
|
||||
if ((base) != NULL) { \
|
||||
((HcfObjectBase *)(base))->destroy((HcfObjectBase *)(base)); \
|
||||
}
|
||||
|
||||
#endif // HCF_OBJECT_BASE_H
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user