https证书管理适配,新增API:caPath V0.2

Signed-off-by: zoufan0330 <zoufan0330@thundersoft.com>
This commit is contained in:
zoufan0330 2023-05-03 17:29:18 +08:00
parent 3411a950be
commit 4b20893c54
17 changed files with 1156 additions and 48 deletions

View File

@ -66,12 +66,18 @@
发起请求可选参数的类型和取值范围。
| 参数 | 类型 | 必填 | 说明 |
| -------------- | ------------------------------------ | ---- | ---------------------------------------------------------- |
| method | [RequestMethod](#requestmethod) | 否 | 请求方式。 |
| extraData | string &#124; Object &#124; ArrayBuffer<sup>8+</sup> | 否 | 发送请求的额外数据。<br />- 当HTTP请求为POST、PUT等方法时此字段为HTTP请求的content支持类型为string和ArrayBuffer<sup>8+</sup><br />- 当HTTP请求为GET、OPTIONS、DELETE、TRACE、CONNECT等方法时此字段为HTTP请求的参数补充参数内容会拼接到URL中进行发送支持类型为string和Object。<br />- 开发者传入string对象开发者需要自行编码将编码后的string传入。<sup>8+</sup> |
| header | Object | 否 | HTTP请求头字段。默认{'Content-Type': 'application/json'}。 |
| readTimeout | number | 否 | 读取超时时间。单位为毫秒ms默认为60000ms。 |
| connectTimeout | number | 否 | 连接超时时间。单位为毫秒ms默认为60000ms。 |
| -------------- | --------------------------------------------- | ---- | ------------------------------------------------------------ |
| method | [RequestMethod](#requestmethod) | 否 | 请求方式默认为GET。 |
| extraData | string<sup>6+</sup> \| Object<sup>6+</sup> \| ArrayBuffer<sup>8+</sup> | 否 | 发送请求的额外数据,默认无此字段。<br />- 当HTTP请求为POST、PUT等方法时此字段为HTTP请求的content以UTF-8编码形式作为请求体。<sup>6+</sup><br />- 当HTTP请求为GET、OPTIONS、DELETE、TRACE、CONNECT等方法时此字段为HTTP请求参数的补充。开发者需传入Encode编码后的string类型参数Object类型的参数无需预编码参数内容会拼接到URL中进行发送ArrayBuffer类型的参数不会做拼接处理。<sup>6+</sup> |
| expectDataType<sup>9+</sup> | [HttpDataType](#httpdatatype) | 否 | 指定返回数据的类型,默认无此字段。如果设置了此参数,系统将优先返回指定的类型。 |
| usingCache<sup>9+</sup> | boolean | 否 | 是否使用缓存默认为true。 |
| priority<sup>9+</sup> | number | 否 | 优先级,范围[1,1000]默认是1。 |
| header | Object | 否 | HTTP请求头字段。默认{'Content-Type': 'application/json'}。 |
| readTimeout | number | 否 | 读取超时时间。单位为毫秒ms默认为60000ms。<br />设置为0表示不会出现超时情况。 |
| connectTimeout | number | 否 | 连接超时时间。单位为毫秒ms默认为60000ms。 |
| usingProtocol<sup>9+</sup> | [HttpProtocol](#httpprotocol) | 否 | 使用协议。默认值由系统自动指定。 |
| usingProxy<sup>10+</sup> | boolean \| Object | 否 | 是否使用HTTP代理默认为false不使用代理。<br />- 当usingProxy为布尔类型true时使用默认网络代理。<br />- 当usingProxy为object类型时使用指定网络代理。 |
| caPath<sup>10+</sup> | string | 否 | 如果设置了此参数系统将使用用户指定路径的CA证书否则将使用系统预设CA证书。 |
#### RequestMethod
@ -140,7 +146,23 @@ request方法回调函数的返回值类型。
| responseCode | [ResponseCode](#responsecode) &#124; number | 是 | 回调函数执行成功时,此字段为[ResponseCode](#responsecode)。若执行失败错误码将会从AsyncCallback中的err字段返回。错误码如下<br />- 200通用错误<br />- 202参数错误<br />- 300I/O错误 |
| header | Object | 是 | 发起http请求返回来的响应头。当前返回的是JSON格式字符串如需具体字段内容需开发者自行解析。常见字段及解析方式如下<br/>- Content-Typeheader['Content-Type']<br />- Status-Lineheader['Status-Line']<br />- Dateheader.Date/header['Date']<br />- Serverheader.Server/header['Server'] |
| cookies<sup>8+</sup> | string | 是 | 服务器返回的 cookies。 |
#### HttpDataType
http的数据类型。
| 名称 | 值 | 说明 |
| ------------------ | -- | ----------- |
| STRING | 0 | 字符串类型。 |
| OBJECT | 1 | 对象类型。 |
| ARRAY_BUFFER | 2 | 二进制数组类型。|
#### HttpProtocol
http协议版本。
| 名称 | 说明 |
| :-------- | :----------- |
| HTTP1_1 | 协议http1.1 |
| HTTP2 | 协议http2 |
#### 示例
```javascript

View File

@ -60,6 +60,8 @@
"inner_kits": [],
"test": [
"//foundation/communication/netstack/test/fuzztest/socket:fuzztest",
"//foundation/communication/netstack/test/fuzztest/http_fuzzer:fuzztest",
"//foundation/communication/netstack/test/unittest/http:unittest",
"//foundation/communication/netstack/test/unittest/http/cache:unittest",
"//foundation/communication/netstack/test/unittest/tlssocket:unittest",
"//foundation/communication/netstack/test/unittest/utils/common_utils:unittest"

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Copyright (c) 2021-2023 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
@ -100,6 +100,8 @@ private:
void ParseUsingHttpProxy(napi_value optionsValue);
void ParseCaPath(napi_value optionsValue);
bool GetRequestBody(napi_value extraData);
void UrlAndOptions(napi_value urlValue, napi_value optionsValue);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Copyright (c) 2021-2023 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
@ -351,6 +351,14 @@ bool RequestContext::GetRequestBody(napi_value extraData)
return false;
}
void RequestContext::ParseCaPath(napi_value optionsValue)
{
std::string caPath = NapiUtils::GetStringPropertyUtf8(GetEnv(), optionsValue, HttpConstant::PARAM_KEY_CA_PATH);
if (!caPath.empty()) {
options.SetCaPath(caPath);
}
}
void RequestContext::UrlAndOptions(napi_value urlValue, napi_value optionsValue)
{
options.SetUrl(NapiUtils::GetStringFromValueUtf8(GetEnv(), urlValue));
@ -370,6 +378,7 @@ void RequestContext::UrlAndOptions(napi_value urlValue, napi_value optionsValue)
}
ParseHeader(optionsValue);
ParseCaPath(optionsValue);
SetParseOK(true);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Copyright (c) 2021-2023 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
@ -140,6 +140,7 @@ public:
static const char *const PARAM_KEY_USING_CACHE;
static const char *const PARAM_KEY_EXPECT_DATA_TYPE;
static const char *const PARAM_KEY_PRIORITY;
static const char *const PARAM_KEY_CA_PATH;
static const char *const PARAM_KEY_USING_HTTP_PROXY;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Copyright (c) 2021-2023 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
@ -41,6 +41,7 @@ const char *const HttpConstant::PARAM_KEY_EXPECT_DATA_TYPE = "expectDataType";
const char *const HttpConstant::PARAM_KEY_PRIORITY = "priority";
const char *const HttpConstant::PARAM_KEY_USING_HTTP_PROXY = "usingProxy";
const char *const HttpConstant::PARAM_KEY_CA_PATH = "caPath";
const char *const HttpConstant::HTTP_PROXY_KEY_HOST = "host";
const char *const HttpConstant::HTTP_PROXY_KEY_PORT = "port";

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Copyright (c) 2021-2023 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
@ -551,7 +551,7 @@ bool HttpExec::SetOption(CURL *curl, RequestContext *context, struct curl_slist
NETSTACK_CURL_EASY_SET_OPTION(curl, CURLOPT_SSL_VERIFYPEER, 0L, context);
#else
#ifndef WINDOWS_PLATFORM
NETSTACK_CURL_EASY_SET_OPTION(curl, CURLOPT_CAINFO, HttpConstant::HTTP_DEFAULT_CA_PATH, context);
NETSTACK_CURL_EASY_SET_OPTION(curl, CURLOPT_CAINFO, context->options.GetCaPath().c_str(), context);
#endif // WINDOWS_PLATFORM
#endif // NO_SSL_CERTIFICATION

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Copyright (c) 2021-2023 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
@ -58,6 +58,8 @@ public:
void SetSpecifiedHttpProxy(const std::string &host, int32_t port, const std::string &exclusionList);
void SetCaPath(const std::string &SetCaPath);
[[nodiscard]] const std::string &GetUrl() const;
[[nodiscard]] const std::string &GetMethod() const;
@ -86,6 +88,8 @@ public:
void GetSpecifiedHttpProxy(std::string &host, int32_t &port, std::string &exclusionList);
[[nodiscard]] const std::string &GetCaPath() const;
private:
std::string url_;
@ -114,6 +118,8 @@ private:
int32_t httpProxyPort_;
std::string httpProxyExclusions_;
std::string caPath_;
};
} // namespace OHOS::NetStack::Http

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Copyright (c) 2021-2023 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
@ -35,6 +35,9 @@ HttpRequestOptions::HttpRequestOptions()
httpProxyPort_(0)
{
header_[CommonUtils::ToLower(HttpConstant::HTTP_CONTENT_TYPE)] = HttpConstant::HTTP_CONTENT_TYPE_JSON; // default
#ifndef WINDOWS_PLATFORM
caPath_ = HttpConstant::HTTP_DEFAULT_CA_PATH;
#endif // WINDOWS_PLATFORM
}
void HttpRequestOptions::SetUrl(const std::string &url)
@ -174,4 +177,18 @@ void HttpRequestOptions::GetSpecifiedHttpProxy(std::string &host, int32_t &port,
port = httpProxyPort_;
exclusionList = httpProxyExclusions_;
}
void HttpRequestOptions::SetCaPath(const std::string &path)
{
if (path.empty()) {
return;
}
caPath_ = path;
}
const std::string &HttpRequestOptions::GetCaPath() const
{
return caPath_;
}
} // namespace OHOS::NetStack::Http

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,69 @@
# Copyright (c) 2023 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.
#####################hydra-fuzz###################
import("//build/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
import("//foundation/communication/netstack/netstack_config.gni")
##############################fuzztest##########################################
NETSTACK_NAPI_ROOT = "$SUBSYSTEM_DIR/netstack/frameworks/js/napi/"
utils_include = [
"$SUBSYSTEM_DIR/netstack/utils/common_utils/include",
"$SUBSYSTEM_DIR/netstack/utils/log/include",
"$THIRD_PARTY_ROOT/curl/include",
]
common_external_deps = [
"c_utils:utils",
"hiviewdfx_hilog_native:libhilog",
]
ohos_fuzztest("HttpFuzzTest") {
module_out_path = "communication/netstack"
fuzz_config_file = "$NETSTACK_DIR/test/fuzztest/http_fuzzer"
include_dirs = [
"$NETSTACK_DIR/utils/napi_utils/include",
"$NETSTACK_NAPI_ROOT/http/constant/include",
"$NETSTACK_NAPI_ROOT/http/options/include",
]
include_dirs += utils_include
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [
"$NETSTACK_NAPI_ROOT/http/constant/src/constant.cpp",
"$NETSTACK_NAPI_ROOT/http/options/src/http_request_options.cpp",
"$SUBSYSTEM_DIR/netstack/utils/common_utils/src/netstack_common_utils.cpp",
"http_fuzzer.cpp",
]
deps = [ "$NETSTACK_DIR/utils/napi_utils:napi_utils" ]
external_deps = common_external_deps
}
###############################################################################
group("fuzztest") {
testonly = true
deps = [ ":HttpFuzzTest" ]
}
###############################################################################

View File

@ -0,0 +1,13 @@
# Copyright (c) 2023 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.
FUZZ

View File

@ -0,0 +1,85 @@
/*
* Copyright (c) 2023 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 <cstring>
#include <securec.h>
#include "http_request_options.h"
#include "netstack_log.h"
namespace OHOS {
namespace NetStack {
namespace Http {
namespace {
const uint8_t *g_baseFuzzData = nullptr;
size_t g_baseFuzzSize = 0;
size_t g_baseFuzzPos = 0;
constexpr size_t STR_LEN = 255;
} // namespace
template <class T> T GetData()
{
T object{};
size_t objectSize = sizeof(object);
if (g_baseFuzzData == nullptr || g_baseFuzzSize <= g_baseFuzzPos || objectSize > g_baseFuzzSize - g_baseFuzzPos) {
return object;
}
if (memcpy_s(&object, objectSize, g_baseFuzzData + g_baseFuzzPos, objectSize)) {
return {};
}
g_baseFuzzPos += objectSize;
return object;
}
std::string GetStringFromData(int strlen)
{
if (strlen < 1) {
return "";
}
char cstr[strlen];
cstr[strlen - 1] = '\0';
for (int i = 0; i < strlen - 1; i++) {
cstr[i] = GetData<char>();
}
std::string str(cstr);
return str;
}
void SetCaPathFuzzTest(const uint8_t *data, size_t size)
{
if ((data == nullptr) || (size < 1)) {
return;
}
g_baseFuzzData = data;
g_baseFuzzSize = size;
g_baseFuzzPos = 0;
HttpRequestOptions requestOptions;
std::string str = GetStringFromData(STR_LEN);
requestOptions.SetCaPath(str);
}
} // namespace Http
} // namespace NetStack
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
/* Run your code on data */
OHOS::NetStack::Http::SetCaPathFuzzTest(data, size);
return 0;
}

View File

@ -0,0 +1,21 @@
/*
* Copyright (c) 2023 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 HTTP_FUZZER_H
#define HTTP_FUZZER_H
#define FUZZ_PROJECT_NAME "http_fuzzer"
#endif // HTTP_FUZZER_H

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2023 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>300</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>4096</rss_limit_mb>
</fuzztest>
</fuzz_config>

View File

@ -0,0 +1,60 @@
# Copyright (c) 2023 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("//build/test.gni")
import("//foundation/communication/netstack/netstack_config.gni")
NETSTACK_NAPI_ROOT = "$SUBSYSTEM_DIR/netstack/frameworks/js/napi/"
utils_include = [
"$SUBSYSTEM_DIR/netstack/utils/common_utils/include",
"$SUBSYSTEM_DIR/netstack/utils/log/include",
"$THIRD_PARTY_ROOT/curl/include",
]
common_external_deps = [
"c_utils:utils",
"hiviewdfx_hilog_native:libhilog",
]
ohos_unittest("http_unittest") {
module_out_path = "netstack/http_unittest"
include_dirs = [
"$NETSTACK_NAPI_ROOT/http/cache/cache_constant/include",
"$NETSTACK_NAPI_ROOT/http/cache/cache_strategy/include",
"$NETSTACK_NAPI_ROOT/http/constant/include",
"$NETSTACK_NAPI_ROOT/http/options/include",
"$NETSTACK_NAPI_ROOT/http/cache/base64/include",
]
include_dirs += utils_include
external_deps = common_external_deps
sources = [
"$NETSTACK_NAPI_ROOT/http/constant/src/constant.cpp",
"$NETSTACK_NAPI_ROOT/http/options/src/http_request_options.cpp",
"$NETSTACK_NAPI_ROOT/http/options/src/http_response.cpp",
"$SUBSYSTEM_DIR/netstack/utils/common_utils/src/netstack_common_utils.cpp",
"HttpRequestOptionsTest.cpp",
]
part_name = "netstack"
subsystem_name = "communication"
}
group("unittest") {
testonly = true
deps = [ ":http_unittest" ]
}

View File

@ -0,0 +1,64 @@
/*
* Copyright (c) 2023 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 <cstring>
#include "gtest/gtest.h"
#include "http_request_options.h"
#include "netstack_log.h"
using namespace OHOS::NetStack::Http;
class HttpRequestOptionsTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
virtual void SetUp() {}
virtual void TearDown() {}
};
namespace {
using namespace std;
using namespace testing::ext;
constexpr char OTHER_CA_PATH[] = "/etc/ssl/certs/other.pem";
HWTEST_F(HttpRequestOptionsTest, CaPathTest001, TestSize.Level1)
{
HttpRequestOptions requestOptions;
string path = requestOptions.GetCaPath();
EXPECT_EQ(path, HttpConstant::HTTP_DEFAULT_CA_PATH);
}
HWTEST_F(HttpRequestOptionsTest, CaPathTest002, TestSize.Level1)
{
HttpRequestOptions requestOptions;
requestOptions.SetCaPath("");
string path = requestOptions.GetCaPath();
EXPECT_EQ(path, HttpConstant::HTTP_DEFAULT_CA_PATH);
}
HWTEST_F(HttpRequestOptionsTest, CaPathTest003, TestSize.Level1)
{
HttpRequestOptions requestOptions;
requestOptions.SetCaPath(OTHER_CA_PATH);
string path = requestOptions.GetCaPath();
EXPECT_EQ(path, OTHER_CA_PATH);
}
} // namespace