fix readme and add send interface

Signed-off-by: zhuwenchao <zhuwenchao@huawei.com>
This commit is contained in:
zhuwenchao 2022-09-27 15:01:09 +08:00
parent e39984443e
commit 787c8bbb14
15 changed files with 388 additions and 97 deletions

View File

@ -321,6 +321,101 @@ udp.send({
})
```
### TLSSocket接口说明
| 类型 | 接口 | 功能说明 |
| ---- | ---- | ---- |
| ohos.net.tlssocket | function constructTLSSocketInstance(): TLSSocket | 返回一个TLSSocket对象 |
| ohos.net.tlssocket.TLSSocket | connect(options: [TLSConnectOptions](#tlsconnectoptions), callback: AsyncCallback\<void>): void | 在TLSSocket上进行通信连接并创建和初始化TLS会话实现建立连接过程启动与服务器的TLS/SSL握手实现数据传输功能使用callback方式作为异步方法。 |
| ohos.net.tlssocket.TLSSocket | connect(options: [TLSConnectOptions](#tlsconnectoptions)): Promise\<void> | 在TLSSocket上进行通信连接并创建和初始化TLS会话实现建立连接过程启动与服务器的TLS/SSL握手实现数据传输功能使用Promise方式作为异步方法。 |
| ohos.net.tlssocket.TLSSocket | getCertificate(callback: AsyncCallback\<string>): void | 在TLSSocket通信连接之后获取本地的数字证书使用callback方式作为异步方法。 |
| ohos.net.tlssocket.TLSSocket | getCertificate():Promise\<string> | 在TLSSocket通信连接之后获取本地的数字证书使用Promise方式作为异步方法。 |
| ohos.net.tlssocket.TLSSocket | getRemoteCertificate(callback: AsyncCallback\<string>): void | 在TLSSocket通信连接之后获取对等方的数字证书使用callback方式作为异步方法。 |
| ohos.net.tlssocket.TLSSocket | getRemoteCertificate(): Promise\<string> | 在TLSSocket通信连接之后获取对等方的数字证书使用Promise方式作为异步方法。 |
| ohos.net.tlssocket.TLSSocket | getProtocol(callback: AsyncCallback\<string>): void | 在TLSSocket通信连接之后获取通信的协议使用callback方式作为异步方法。 |
| ohos.net.tlssocket.TLSSocket | getProtocol(): Promise\<string> | 在TLSSocket通信连接之后获取通信的协议使用Promise方式作为异步方法。 |
| ohos.net.tlssocket.TLSSocket | getCipherSuites(callback: AsyncCallback\<Array\<string>>): void | 在TLSSocket通信连接之后获取通信双方支持的加密套件使用callback方式作为异步方法。 |
| ohos.net.tlssocket.TLSSocket | getCipherSuites(): Promise\<Array\<string>> | 在TLSSocket通信连接之后获取通信双方支持的加密套件使用Promise方式作为异步方法。 |
| ohos.net.tlssocket.TLSSocket | getSignatureAlgorithms(callback: AsyncCallback\<Array\<string>>): void | 在TLSSocket通信连接之后获取通信双方支持的签名算法使用callback方式作为异步方法。 |
| ohos.net.tlssocket.TLSSocket | getSignatureAlgorithms(): Promise\<Array\<string>> | 在TLSSocket通信连接之后获取通信双方支持的签名算法使用Promise方式作为异步方法。 |
| ohos.net.tlssocket.TLSSocket | close(callback: AsyncCallback\<void>): void | 在TLSSocket通信连接之后断开连接使用callback方式作为异步方法。 |
| ohos.net.tlssocket.TLSSocket | close(): Promise\<void> | 在TLSSocket通信连接之后断开连接使用Promise方式作为异步方法。 |
#### TLSConnectOptions
TLS连接的操作。
| 参数名 | 类型 | 说明 |
| -------------- | ------------------------------------- | -------------- |
| address | [NetAddress](#netaddress) | 网关地址。 |
| secureOptions | [TLSSecureOptions](#tlssecureoptions) | TLS安全相关操作。|
| ALPNProtocols | Array\<string> | ALPN协议。 |
#### TLSSecureOptions
TLS安全相关操作。
| 参数名 | 类型 | 说明 |
| --------------------- | ---------------------- | ---------------------- |
| ca | string \| Array\<string> | ca证书。 |
| cert | string | 本地数字证书。 |
| key | string | 本地数字证书私钥。 |
| passwd | string | 密码。 |
| protocols | [Protocol](#protocol) \| Array\<[Protocol](#protocol)>;| 协议名。 |
| useRemoteCipherPrefer | boolean | 优先使用对等方的密码套件。 |
| signatureAlgorithms | string | 设置签名算法。 |
| cipherSuites | string | 加密套件。 |
#### Protocol
TLS通信的协议。
| **protocol 的合法值** | 说明 |
| :------------------ | :------------------ |
| TLSv12 | 使用TlsV1_2协议通信。 |
| TLSv13 | 使用TlsV1_3协议通信。 |
#### 示例
```javascript
import tlssocket from "@ohos.net.tlssocket";
let tls = tlssocket.constructTLSSocketInstance();
let Protocol = tlssocket.Protocol;
let options = {
ALPNProtocols: ["spdy/1", "http/1.1"],
address: {
address: "xxxx",
port: "xxxx",
family: 1,
},
secureOptions: {
key: "xxxx",
cert: "xxxx",
ca: ["xxxx"],
passwd: "123456",
protocols: [Protocol.TLSv12],
useRemoteCipherPrefer: true,
signatureAlgorithms: SHA256,
cipherSuites: AES256-SHA256,
},
};
tls.connect(options).then(data => {
console.info(data);
}).catch(err => {
console.error(err);
});
tls.getCertificate((err, data) => {
if (err) {
console.log("getCertificate callback error = " + err);
} else {
console.log("getCertificate callback = " + data);
}
});
tls.close().then(() =>
console.log("close success");
}).catch(err => {
console.error(err);
});
```
## 相关仓
[网络管理子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86%E5%AD%90%E7%B3%BB%E7%BB%9F.md)

View File

@ -32,6 +32,7 @@ ohos_shared_library("tlssocket") {
"src/context/get_protocol_context.cpp",
"src/context/get_remote_certificate_context.cpp",
"src/context/get_signature_algorithms_context.cpp",
"src/context/send_context.cpp",
"src/tlssocket_async_work.cpp",
"src/tlssocket_exec.cpp",
"src/tlssocket_module.cpp",

View 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 TLS_CONTEXT_SEND_CONTEXT_H
#define TLS_CONTEXT_SEND_CONTEXT_H
#include <cstddef>
#include <napi/native_api.h>
#include <string>
#include "base_context.h"
#include "event_manager.h"
#include "nocopyable.h"
#include "tls.h"
#include "tls_socket.h"
namespace OHOS {
namespace NetStack {
class SendContext final : public BaseContext {
public:
DISALLOW_COPY_AND_MOVE(SendContext);
SendContext() = delete;
explicit SendContext(napi_env env, EventManager *manager);
std::string data_;
bool ok_ = false;
void ParseParams(napi_value *params, size_t paramsCount);
private:
bool CheckParamsType(napi_value *params, size_t paramsCount);
};
} // namespace NetStack
} // namespace OHOS
#endif // TLS_CONTEXT_SEND_CONTEXT_H

View File

@ -34,6 +34,7 @@ public:
static void ExecGetRemoteCertificate(napi_env env, void *data);
static void ExecGetProtocol(napi_env env, void *data);
static void ExecGetSignatureAlgorithms(napi_env env, void *data);
static void ExecSend(napi_env env, void *data);
static void ExecClose(napi_env env, void *data);
static void GetCertificateCallback(napi_env env, napi_status status, void *data);
@ -42,6 +43,7 @@ public:
static void GetRemoteCertificateCallback(napi_env env, napi_status status, void *data);
static void GetProtocolCallback(napi_env env, napi_status status, void *data);
static void GetSignatureAlgorithmsCallback(napi_env env, napi_status status, void *data);
static void SendCallback(napi_env env, napi_status status, void *data);
static void CloseCallback(napi_env env, napi_status status, void *data);
};
} // namespace NetStack

View File

@ -25,6 +25,7 @@
#include "get_protocol_context.h"
#include "get_remote_certificate_context.h"
#include "get_signature_algorithms_context.h"
#include "send_context.h"
namespace OHOS {
namespace NetStack {
@ -41,6 +42,7 @@ public:
static bool ExecGetRemoteCertificate(GetRemoteCertificateContext *context);
static bool ExecGetProtocol(GetProtocolContext *context);
static bool ExecGetSignatureAlgorithms(GetSignatureAlgorithmsContext *context);
static bool ExecSend(SendContext *context);
static bool ExecClose(CloseContext *context);
static napi_value GetCertificateCallback(GetCertificateContext *context);
@ -49,6 +51,7 @@ public:
static napi_value GetRemoteCertificateCallback(GetRemoteCertificateContext *context);
static napi_value GetProtocolCallback(GetProtocolContext *context);
static napi_value GetSignatureAlgorithmsCallback(GetSignatureAlgorithmsContext *context);
static napi_value SendCallback(SendContext *context);
static napi_value CloseCallback(CloseContext *context);
};
} // namespace NetStack

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Copyright (c) 2021-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
@ -27,9 +27,10 @@ public:
static constexpr const char *FUNCTION_GET_CERTIFICATE = "getCertificate";
static constexpr const char *FUNCTION_GET_REMOTE_CERTIFICATE = "getRemoteCertificate";
static constexpr const char *FUNCTION_GET_SIGNATURE_ALGORITHMS = "getSignatureAlgorithms";
static constexpr const char *FUNCTION_GET_PROTOCOL ="getProtocol";
static constexpr const char *FUNCTION_CONNECT ="connect";
static constexpr const char *FUNCTION_GET_PROTOCOL = "getProtocol";
static constexpr const char *FUNCTION_CONNECT = "connect";
static constexpr const char *FUNCTION_GET_CIPHER_SUITES = "getCipherSuites";
static constexpr const char *FUNCTION_SEND = "send";
static constexpr const char *FUNCTION_CLOSE = "close";
static napi_value GetCertificate(napi_env env, napi_callback_info info);
@ -38,12 +39,14 @@ public:
static napi_value GetCipherSuites(napi_env env, napi_callback_info info);
static napi_value GetRemoteCertificate(napi_env env, napi_callback_info info);
static napi_value GetSignatureAlgorithms(napi_env env, napi_callback_info info);
static napi_value Send(napi_env env, napi_callback_info info);
static napi_value Close(napi_env env, napi_callback_info info);
};
public:
static constexpr const char *INTERFACE_TLS_SOCKET = "TLSSocket";
static constexpr const char *FUNCTION_CONSTRUCTOR_TLS_SOCKET_INSTANCE = "constructTLSSocketInstance";
static constexpr const char *INTERFACE_PROTOCOL = "Protocol";
static napi_value InitTLSSocketModule(napi_env env, napi_value exports);
@ -51,6 +54,7 @@ private:
static napi_value ConstructTLSSocketInstance(napi_env env, napi_callback_info info);
static void DefineTLSSocketClass(napi_env env, napi_value exports);
static void InitTLSSocketProperties(napi_env env, napi_value exports);
static void InitProtocol(napi_env env, napi_value exports);
};
} // namespace NetStack
} // namespace OHOS

View File

@ -53,7 +53,7 @@ bool ConnectContext::CheckParamsType(napi_value *params, size_t paramsCount)
if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) != napi_object) {
NETSTACK_LOGE("tls ConnectContext first param is not number");
NETSTACK_LOGE("tls ConnectContext first param is not object");
return false;
}
if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_1]) != napi_function) {
@ -72,9 +72,9 @@ TLSConnectOptions ConnectContext::ReadTlsConnectOptions(napi_env env, napi_value
TLSSecureOptions secureOption = ReadTlsSecureOptions(GetEnv(), params);
options.SetNetAddress(address);
options.SetTlsSecureOptions(secureOption);
//alpnProtocols
if (NapiUtils::HasNamedProperty(GetEnv(), params[0], "ALPNProtocols")) {
napi_value alpnProtocols = NapiUtils::GetNamedProperty(GetEnv(), params[0], "ALPNProtocols");
napi_value alpnProtocols = NapiUtils::GetNamedProperty(GetEnv(), params[0], "ALPNProtocols");
uint32_t arrayLength = NapiUtils::GetArrayLength(GetEnv(), alpnProtocols);
napi_value elementValue = nullptr;
std::vector<std::string> alpnProtocolVec;
@ -92,42 +92,70 @@ TLSConnectOptions ConnectContext::ReadTlsConnectOptions(napi_env env, napi_value
TLSSecureOptions ConnectContext::ReadTlsSecureOptions(napi_env env, napi_value *params)
{
TLSSecureOptions secureOption;
//caVector
napi_value secureOptions = NapiUtils::GetNamedProperty(GetEnv(), params[ARG_INDEX_0], "secureOptions");
if (NapiUtils::HasNamedProperty(GetEnv(), secureOptions, "ca")) {
napi_value caVector = NapiUtils::GetNamedProperty(GetEnv(), secureOptions, "ca");
uint32_t arrayLong = NapiUtils::GetArrayLength(GetEnv(), caVector);
napi_value element = nullptr;
std::vector<std::string> caVec;
for (uint32_t i = 0; i < arrayLong; i++) {
element = NapiUtils::GetArrayElement(GetEnv(), caVector, i);
std::string ca = NapiUtils::GetStringFromValueUtf8(GetEnv(), element);
caVec.push_back(ca);
}
secureOption.SetCaChain(caVec);
if (!NapiUtils::HasNamedProperty(GetEnv(), params[ARG_INDEX_0], "secureOptions")) {
return secureOption;
}
napi_value secureOptions = NapiUtils::GetNamedProperty(GetEnv(), params[ARG_INDEX_0], "secureOptions");
if (!NapiUtils::HasNamedProperty(GetEnv(), secureOptions, "ca")) {
return secureOption;
}
napi_value caVector = NapiUtils::GetNamedProperty(GetEnv(), secureOptions, "ca");
uint32_t arrayLong = NapiUtils::GetArrayLength(GetEnv(), caVector);
napi_value element = nullptr;
std::vector<std::string> caVec;
for (uint32_t i = 0; i < arrayLong; i++) {
element = NapiUtils::GetArrayElement(GetEnv(), caVector, i);
std::string ca = NapiUtils::GetStringFromValueUtf8(GetEnv(), element);
caVec.push_back(ca);
}
secureOption.SetCaChain(caVec);
if (!NapiUtils::HasNamedProperty(GetEnv(), secureOptions, "key")) {
return secureOption;
}
//key
std::string key = NapiUtils::GetStringPropertyUtf8(env, secureOptions, "key");
secureOption.SetKey(key);
//cert
if (!NapiUtils::HasNamedProperty(GetEnv(), secureOptions, "cert")) {
return secureOption;
}
std::string cert = NapiUtils::GetStringPropertyUtf8(env, secureOptions, "cert");
secureOption.SetCert(cert);
//passwd
std::string passwd = NapiUtils::GetStringPropertyUtf8(env, secureOptions, "passwd");
secureOption.SetPassWd(passwd);
//protocol
std::string protocol = NapiUtils::GetStringPropertyUtf8(env, secureOptions, "protocols");
std::vector<std::string> protocolVec = {protocol};
secureOption.SetProtocolChain(protocolVec);
//signatureAlgorithms
std::string signatureAlgorithms = NapiUtils::GetStringPropertyUtf8(env, secureOptions, "signatureAlgorithms");
secureOption.SetSignatureAlgorithms(signatureAlgorithms);
//useRemoteCipherPrefer
bool useRemoteCipherPrefer = NapiUtils::GetBooleanProperty(env, secureOptions, "useRemoteCipherPrefer");
secureOption.SetUseRemoteCipherPrefer(useRemoteCipherPrefer);
//cipherSuites
std::string cipherSuites = NapiUtils::GetStringPropertyUtf8(env, secureOptions, "cipherSuites");
secureOption.SetCipherSuite(cipherSuites);
if (NapiUtils::HasNamedProperty(GetEnv(), secureOptions, "passwd")) {
secureOption.SetPassWd(NapiUtils::GetStringPropertyUtf8(env, secureOptions, "passwd"));
}
if (NapiUtils::HasNamedProperty(GetEnv(), secureOptions, "protocols")) {
napi_value protocolVector = NapiUtils::GetNamedProperty(env, secureOptions, "protocols");
uint32_t num = NapiUtils::GetArrayLength(GetEnv(), protocolVector);
napi_value element = nullptr;
std::vector<std::string> protocolVec;
for (uint32_t i = 0; i < num; i++) {
element = NapiUtils::GetArrayElement(GetEnv(), protocolVector, i);
std::string protocol = NapiUtils::GetStringFromValueUtf8(GetEnv(), element);
NETSTACK_LOGI("protocol is %{public}s", protocol.c_str());
protocolVec.push_back(protocol);
}
secureOption.SetProtocolChain(protocolVec);
}
if (NapiUtils::HasNamedProperty(GetEnv(), secureOptions, "signatureAlgorithms")) {
std::string signatureAlgorithms = NapiUtils::GetStringPropertyUtf8(env, secureOptions, "signatureAlgorithms");
secureOption.SetSignatureAlgorithms(signatureAlgorithms);
}
if (NapiUtils::HasNamedProperty(GetEnv(), secureOptions, "useRemoteCipherPrefer")) {
bool useRemoteCipherPrefer = NapiUtils::GetBooleanProperty(env, secureOptions, "useRemoteCipherPrefer");
secureOption.SetUseRemoteCipherPrefer(useRemoteCipherPrefer);
}
if (NapiUtils::HasNamedProperty(GetEnv(), secureOptions, "cipherSuites")) {
std::string cipherSuites = NapiUtils::GetStringPropertyUtf8(env, secureOptions, "cipherSuites");
secureOption.SetCipherSuite(cipherSuites);
}
return secureOption;
}

View File

@ -0,0 +1,66 @@
/*
* 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 "send_context.h"
#include <cstdint>
#include "constant.h"
#include "napi_utils.h"
#include "netstack_log.h"
namespace OHOS {
namespace NetStack {
SendContext::SendContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
void SendContext::ParseParams(napi_value *params, size_t paramsCount)
{
if (!CheckParamsType(params, paramsCount)) {
return;
}
data_ = NapiUtils::GetStringFromValueUtf8(GetEnv(), params[0]);
if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
SetParseOK(SetCallback(params[ARG_INDEX_1]) == napi_ok);
return;
}
SetParseOK(true);
}
bool SendContext::CheckParamsType(napi_value *params, size_t paramsCount)
{
if (paramsCount == PARAM_JUST_OPTIONS) {
if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) != napi_string) {
NETSTACK_LOGE("SendContext first param is not string");
return false;
}
return true;
}
if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) != napi_string) {
NETSTACK_LOGE("SendContext first param is not string");
return false;
}
if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_1]) != napi_function) {
NETSTACK_LOGE("SendContext second param is not function");
return false;
}
return true;
}
return false;
}
}
}

View File

@ -21,6 +21,7 @@
#include "get_certificate_context.h"
#include "get_cipher_suites_context.h"
#include "get_remote_certificate_context.h"
#include "send_context.h"
#include "tlssocket_exec.h"
namespace OHOS {
@ -55,6 +56,11 @@ void TlsSocketAsyncWork::ExecGetSignatureAlgorithms(napi_env env, void *data)
BaseAsyncWork::ExecAsyncWork<GetSignatureAlgorithmsContext, TlsSocketExec::ExecGetSignatureAlgorithms>(env, data);
}
void TlsSocketAsyncWork::ExecSend(napi_env env, void *data)
{
BaseAsyncWork::ExecAsyncWork<SendContext, TlsSocketExec::ExecSend>(env, data);
}
void TlsSocketAsyncWork::ExecClose(napi_env env, void *data)
{
BaseAsyncWork::ExecAsyncWork<CloseContext, TlsSocketExec::ExecClose>(env, data);
@ -77,7 +83,8 @@ void TlsSocketAsyncWork::GetCipherSuitesCallback(napi_env env, napi_status statu
void TlsSocketAsyncWork::GetRemoteCertificateCallback(napi_env env, napi_status status, void *data)
{
BaseAsyncWork::AsyncWorkCallback<GetRemoteCertificateContext, TlsSocketExec::GetRemoteCertificateCallback>(env, status, data);
BaseAsyncWork::AsyncWorkCallback<GetRemoteCertificateContext, TlsSocketExec::GetRemoteCertificateCallback>(
env, status, data);
}
void TlsSocketAsyncWork::GetProtocolCallback(napi_env env, napi_status status, void *data)
@ -87,7 +94,13 @@ void TlsSocketAsyncWork::GetProtocolCallback(napi_env env, napi_status status, v
void TlsSocketAsyncWork::GetSignatureAlgorithmsCallback(napi_env env, napi_status status, void *data)
{
BaseAsyncWork::AsyncWorkCallback<GetSignatureAlgorithmsContext, TlsSocketExec::GetSignatureAlgorithmsCallback>(env, status, data);
BaseAsyncWork::AsyncWorkCallback<GetSignatureAlgorithmsContext, TlsSocketExec::GetSignatureAlgorithmsCallback>(
env, status, data);
}
void TlsSocketAsyncWork::SendCallback(napi_env env, napi_status status, void *data)
{
BaseAsyncWork::AsyncWorkCallback<SendContext, TlsSocketExec::SendCallback>(env, status, data);
}
void TlsSocketAsyncWork::CloseCallback(napi_env env, napi_status status, void *data)

View File

@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "tlssocket_exec.h"
#include <string>
@ -23,19 +23,6 @@
namespace OHOS {
namespace NetStack {
namespace {
void SendCustomData(TLSSocket *tlsSocket)
{
TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData("custom data");
tlsSocket->Send(tcpSendOptions, [](bool ok) {
if (!ok) {
NETSTACK_LOGE("send custom data is failed");
}
});
}
} // namespace
bool TlsSocketExec::ExecGetCertificate(GetCertificateContext *context)
{
auto manager = context->GetManager();
@ -43,7 +30,7 @@ bool TlsSocketExec::ExecGetCertificate(GetCertificateContext *context)
NETSTACK_LOGE("manager is nullptr");
return false;
}
auto tlsSocket = reinterpret_cast<TLSSocket*>(manager->GetData());
auto tlsSocket = reinterpret_cast<TLSSocket *>(manager->GetData());
if (tlsSocket == nullptr) {
NETSTACK_LOGE("TlsSocketExec::ExecGetCipherSuites tlsSocket is null");
return false;
@ -67,16 +54,13 @@ bool TlsSocketExec::ExecConnect(ConnectContext *context)
return false;
}
auto tlsSocket = new TLSSocket();
tlsSocket->Connect(context->connectOptions_, [&context](bool ok) {
context->ok_ = ok;
});
tlsSocket->Connect(context->connectOptions_, [&context](bool ok) { context->ok_ = ok; });
if (!context->ok_) {
NETSTACK_LOGE("TlsSocketExec::ExecConnect result is false");
delete tlsSocket;
tlsSocket = nullptr;
return false;
}
SendCustomData(tlsSocket);
manager->SetData(tlsSocket);
return true;
}
@ -88,7 +72,7 @@ bool TlsSocketExec::ExecGetCipherSuites(GetCipherSuitesContext *context)
NETSTACK_LOGE("manager is nullptr");
return false;
}
auto tlsSocket = reinterpret_cast<TLSSocket*>(manager->GetData());
auto tlsSocket = reinterpret_cast<TLSSocket *>(manager->GetData());
if (tlsSocket == nullptr) {
NETSTACK_LOGE("TlsSocketExec::ExecGetCipherSuites tlsSocket is null");
return false;
@ -107,7 +91,7 @@ bool TlsSocketExec::ExecGetRemoteCertificate(GetRemoteCertificateContext *contex
NETSTACK_LOGE("manager is nullptr");
return false;
}
auto tlsSocket = reinterpret_cast<TLSSocket*>(manager->GetData());
auto tlsSocket = reinterpret_cast<TLSSocket *>(manager->GetData());
if (tlsSocket == nullptr) {
NETSTACK_LOGE("TlsSocketExec::ExecGetRemoteCertificate tlsSocket is null");
return false;
@ -115,7 +99,6 @@ bool TlsSocketExec::ExecGetRemoteCertificate(GetRemoteCertificateContext *contex
tlsSocket->GetRemoteCertificate([&context](bool ok, const std::string &cert) {
context->ok_ = ok;
context->remoteCert_ = cert;
});
return context->ok_;
}
@ -127,7 +110,7 @@ bool TlsSocketExec::ExecGetProtocol(GetProtocolContext *context)
NETSTACK_LOGE("manager is nullptr");
return false;
}
auto tlsSocket = reinterpret_cast<TLSSocket*>(manager->GetData());
auto tlsSocket = reinterpret_cast<TLSSocket *>(manager->GetData());
if (tlsSocket == nullptr) {
NETSTACK_LOGE("TlsSocketExec::ExecGetProtocol tlsSocket is null");
return false;
@ -146,7 +129,7 @@ bool TlsSocketExec::ExecGetSignatureAlgorithms(GetSignatureAlgorithmsContext *co
NETSTACK_LOGE("manager is nullptr");
return false;
}
auto tlsSocket = reinterpret_cast<TLSSocket*>(manager->GetData());
auto tlsSocket = reinterpret_cast<TLSSocket *>(manager->GetData());
if (tlsSocket == nullptr) {
NETSTACK_LOGE("TlsSocketExec::ExecGetSignatureAlgorithms tlsSocket is null");
return false;
@ -158,8 +141,9 @@ bool TlsSocketExec::ExecGetSignatureAlgorithms(GetSignatureAlgorithmsContext *co
return context->ok_;
}
bool TlsSocketExec::ExecClose(CloseContext *context)
bool TlsSocketExec::ExecSend(SendContext *context)
{
NETSTACK_LOGI("TlsSocketExec::ExecSend is start");
auto manager = context->GetManager();
if (manager == nullptr) {
NETSTACK_LOGE("manager is nullptr");
@ -170,9 +154,30 @@ bool TlsSocketExec::ExecClose(CloseContext *context)
NETSTACK_LOGE("TlsSocketExec::ExecClose tlsSocket is null");
return false;
}
tlsSocket->Close([&context](bool ok) {
context->ok_ = ok;
TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(context->data_);
tlsSocket->Send(tcpSendOptions, [](bool ok) {
if (!ok) {
NETSTACK_LOGE("send data is failed");
}
});
NETSTACK_LOGI("TlsSocketExec::ExecSend is end");
return context->ok_;
}
bool TlsSocketExec::ExecClose(CloseContext *context)
{
auto manager = context->GetManager();
if (manager == nullptr) {
NETSTACK_LOGE("manager is nullptr");
return false;
}
auto tlsSocket = reinterpret_cast<TLSSocket *>(manager->GetData());
if (tlsSocket == nullptr) {
NETSTACK_LOGE("TlsSocketExec::ExecClose tlsSocket is null");
return false;
}
tlsSocket->Close([&context](bool ok) { context->ok_ = ok; });
delete tlsSocket;
manager->SetData(nullptr);
return context->ok_;
@ -220,6 +225,11 @@ napi_value TlsSocketExec::GetSignatureAlgorithmsCallback(GetSignatureAlgorithmsC
return signatureAlgorithms;
}
napi_value TlsSocketExec::SendCallback(SendContext *context)
{
return NapiUtils::GetBoolean(context->GetEnv(), true);
}
napi_value TlsSocketExec::CloseCallback(CloseContext *context)
{
return NapiUtils::GetBoolean(context->GetEnv(), true);

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Copyright (c) 2021-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
@ -16,12 +16,11 @@
#include "tlssocket_module.h"
#include <initializer_list>
#include <napi/native_api.h>
#include <napi/native_common.h>
#include "connect_context.h"
#include "close_context.h"
#include "connect_context.h"
#include "event_manager.h"
#include "get_certificate_context.h"
#include "get_remote_certificate_context.h"
@ -30,6 +29,7 @@
#include "module_template.h"
#include "napi_utils.h"
#include "netstack_log.h"
#include "send_context.h"
#include "tlssocket_async_work.h"
namespace OHOS {
@ -43,53 +43,58 @@ void Finalize(napi_env, void *data, void *)
NETSTACK_LOGI("socket handle is finalized");
}
napi_value TLSSocketModuleExports::TLSSocket::GetCertificate(napi_env env , napi_callback_info info)
napi_value TLSSocketModuleExports::TLSSocket::GetCertificate(napi_env env, napi_callback_info info)
{
return ModuleTemplate::Interface<GetCertificateContext>(env, info, FUNCTION_GET_CERTIFICATE, nullptr,
TlsSocketAsyncWork::ExecGetCertificate,
TlsSocketAsyncWork::GetCertificateCallback);
}
napi_value TLSSocketModuleExports::TLSSocket::GetProtocol(napi_env env , napi_callback_info info)
napi_value TLSSocketModuleExports::TLSSocket::GetProtocol(napi_env env, napi_callback_info info)
{
return ModuleTemplate::Interface<GetCipherSuitesContext>(env, info, FUNCTION_GET_PROTOCOL, nullptr,
TlsSocketAsyncWork::ExecGetProtocol,
TlsSocketAsyncWork::GetProtocolCallback);
TlsSocketAsyncWork::ExecGetProtocol,
TlsSocketAsyncWork::GetProtocolCallback);
}
napi_value TLSSocketModuleExports::TLSSocket::Connect(napi_env env , napi_callback_info info)
napi_value TLSSocketModuleExports::TLSSocket::Connect(napi_env env, napi_callback_info info)
{
return ModuleTemplate::Interface<ConnectContext>(env, info, FUNCTION_CONNECT, nullptr,
TlsSocketAsyncWork::ExecConnect,
TlsSocketAsyncWork::ConnectCallback);
return ModuleTemplate::Interface<ConnectContext>(
env, info, FUNCTION_CONNECT, nullptr, TlsSocketAsyncWork::ExecConnect, TlsSocketAsyncWork::ConnectCallback);
}
napi_value TLSSocketModuleExports::TLSSocket::GetCipherSuites(napi_env env , napi_callback_info info)
napi_value TLSSocketModuleExports::TLSSocket::GetCipherSuites(napi_env env, napi_callback_info info)
{
return ModuleTemplate::Interface<GetCipherSuitesContext>(env, info, FUNCTION_GET_CIPHER_SUITES, nullptr,
TlsSocketAsyncWork::ExecGetCipherSuites,
TlsSocketAsyncWork::GetCipherSuitesCallback);
TlsSocketAsyncWork::ExecGetCipherSuites,
TlsSocketAsyncWork::GetCipherSuitesCallback);
}
napi_value TLSSocketModuleExports::TLSSocket::GetRemoteCertificate(napi_env env , napi_callback_info info)
napi_value TLSSocketModuleExports::TLSSocket::GetRemoteCertificate(napi_env env, napi_callback_info info)
{
return ModuleTemplate::Interface<GetRemoteCertificateContext>(env, info, FUNCTION_GET_REMOTE_CERTIFICATE, nullptr,
TlsSocketAsyncWork::ExecGetRemoteCertificate,
TlsSocketAsyncWork::GetRemoteCertificateCallback);
TlsSocketAsyncWork::ExecGetRemoteCertificate,
TlsSocketAsyncWork::GetRemoteCertificateCallback);
}
napi_value TLSSocketModuleExports::TLSSocket::GetSignatureAlgorithms(napi_env env , napi_callback_info info)
napi_value TLSSocketModuleExports::TLSSocket::GetSignatureAlgorithms(napi_env env, napi_callback_info info)
{
return ModuleTemplate::Interface<GetSignatureAlgorithmsContext>(env, info, FUNCTION_GET_SIGNATURE_ALGORITHMS, nullptr,
TlsSocketAsyncWork::ExecGetSignatureAlgorithms,
TlsSocketAsyncWork::GetSignatureAlgorithmsCallback);
return ModuleTemplate::Interface<GetSignatureAlgorithmsContext>(
env, info, FUNCTION_GET_SIGNATURE_ALGORITHMS, nullptr, TlsSocketAsyncWork::ExecGetSignatureAlgorithms,
TlsSocketAsyncWork::GetSignatureAlgorithmsCallback);
}
napi_value TLSSocketModuleExports::TLSSocket::Close(napi_env env , napi_callback_info info)
napi_value TLSSocketModuleExports::TLSSocket::Send(napi_env env, napi_callback_info info)
{
return ModuleTemplate::Interface<CloseContext>(env, info, FUNCTION_CLOSE, nullptr,
TlsSocketAsyncWork::ExecClose,
TlsSocketAsyncWork::CloseCallback);
return ModuleTemplate::Interface<SendContext>(env, info, FUNCTION_SEND, nullptr,
TlsSocketAsyncWork::ExecSend,
TlsSocketAsyncWork::SendCallback);
}
napi_value TLSSocketModuleExports::TLSSocket::Close(napi_env env, napi_callback_info info)
{
return ModuleTemplate::Interface<CloseContext>(env, info, FUNCTION_CLOSE, nullptr, TlsSocketAsyncWork::ExecClose,
TlsSocketAsyncWork::CloseCallback);
}
void TLSSocketModuleExports::DefineTLSSocketClass(napi_env env, napi_value exports)
@ -101,11 +106,24 @@ void TLSSocketModuleExports::DefineTLSSocketClass(napi_env env, napi_value expor
DECLARE_NAPI_FUNCTION(TLSSocket::FUNCTION_GET_PROTOCOL, TLSSocket::GetProtocol),
DECLARE_NAPI_FUNCTION(TLSSocket::FUNCTION_CONNECT, TLSSocket::Connect),
DECLARE_NAPI_FUNCTION(TLSSocket::FUNCTION_GET_CIPHER_SUITES, TLSSocket::GetCipherSuites),
DECLARE_NAPI_FUNCTION(TLSSocket::FUNCTION_SEND, TLSSocket::Send),
DECLARE_NAPI_FUNCTION(TLSSocket::FUNCTION_CLOSE, TLSSocket::Close),
};
ModuleTemplate::DefineClass(env, exports, functions, INTERFACE_TLS_SOCKET);
}
void TLSSocketModuleExports::InitProtocol(napi_env env, napi_value exports)
{
std::initializer_list<napi_property_descriptor> properties = {
DECLARE_NAPI_STATIC_PROPERTY("TLSv12", NapiUtils::CreateStringUtf8(env, "TlsV1_2")),
DECLARE_NAPI_STATIC_PROPERTY("TLSv13", NapiUtils::CreateStringUtf8(env, "TlsV1_3")),
};
napi_value protocol = NapiUtils::CreateObject(env);
NapiUtils::DefineProperties(env, protocol, properties);
NapiUtils::SetNamedProperty(env, exports, INTERFACE_PROTOCOL, protocol);
}
napi_value TLSSocketModuleExports::ConstructTLSSocketInstance(napi_env env, napi_callback_info info)
{
return ModuleTemplate::NewInstance(env, info, INTERFACE_TLS_SOCKET, Finalize);
@ -123,6 +141,7 @@ napi_value TLSSocketModuleExports::InitTLSSocketModule(napi_env env, napi_value
{
DefineTLSSocketClass(env, exports);
InitTLSSocketProperties(env, exports);
InitProtocol(env, exports);
return exports;
}

View File

@ -66,14 +66,12 @@ void TLSConfiguration::SetPrivateKey(const TLSKey &key)
void TLSConfiguration::SetPrivateKey(const std::string &key, const std::string &passwd)
{
// TLSKey pkey(key, ALGORITHM_RSA, PEM, PRIVATE_KEY, passwd);
TLSKey pkey(key, ALGORITHM_RSA, passwd);
privateKey_ = pkey;
}
void TLSConfiguration::SetLocalCertificate(const std::string &certificate)
{
// TLSCertificate local(certificate, PEM, LOCAL_CERT);
TLSCertificate local(certificate, LOCAL_CERT);
localCertificate_ = local;
}

View File

@ -47,6 +47,9 @@ void InitEnv()
void TLSContext::SetCipherList(TLSContext *tlsContext, const TLSConfiguration &configuration)
{
NETSTACK_LOGD("GetCipherSuite = %{public}s", configuration.GetCipherSuite().c_str());
if (configuration.GetCipherSuite().empty()) {
return;
}
if (SSL_CTX_set_cipher_list(tlsContext->ctx_, configuration.GetCipherSuite().c_str()) <= 0) {
NETSTACK_LOGE("Error setting the cipher list");
}

View File

@ -612,10 +612,10 @@ void TLSSocket::Send(const OHOS::NetStack::TCPSendOptions &tcpSendOptions, const
auto res = openSslContext_.tlsSocketInternal_.Send(tcpSendOptions.GetData());
if (!res) {
CallOnErrorCallback(errno, MakeErrnoString());
CallSendCallback(false, callback);
callback(false);
return;
}
CallSendCallback(true, callback);
callback(true);
}
bool WaitConditionWithTimeout(bool *flag, int32_t timeoutMs)

View File

@ -310,11 +310,11 @@ bool TLSSocketInternal::Send(const std::string &data)
if (errorCallback_) {
errorCallback_(errno, strerror(errno));
}
NETSTACK_LOGE("data '%{public}s' send failedThe error code is %{public}d, The error message is'%{public}s'",
NETSTACK_LOGE("data '%{public}s' send failed!The error code is %{public}d, The error message is'%{public}s'",
data.c_str(), errno, strerror(errno));
return false;
} else {
NETSTACK_LOGI("data '%{public}s' Sent successfullysent in total %{public}d bytes", data.c_str(), len);
NETSTACK_LOGI("data '%{public}s' Sent successfully,sent in total %{public}d bytes!", data.c_str(), len);
}
return true;
}