diff --git a/bundle.json b/bundle.json index a7bb58493adcf20482b420fd4acd12c7aef2c845..a017bcd8e946d7b63cea0bf15e730e7ab833a09f 100644 --- a/bundle.json +++ b/bundle.json @@ -49,6 +49,7 @@ "//foundation/communication/netstack/frameworks/js/napi:fetch" ], "fwk_group": [ + "//foundation/communication/netstack/frameworks/js/napi/tls:tlssocket" ], "service_group": [ ] diff --git a/frameworks/js/napi/tls/BUILD.gn b/frameworks/js/napi/tls/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..eb35736149055cd636dbc90bd68f44302ea32c29 --- /dev/null +++ b/frameworks/js/napi/tls/BUILD.gn @@ -0,0 +1,55 @@ +# 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 +# +# 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("//foundation/communication/netstack/netstack_config.gni") + +config("tlssocket_config") { + visibility = [ ":tlssocket" ] + include_dirs = [ + "include", + "include/context", + "NETSTACK_INNERKITS_DIR/tls_socket/include", + ] +} + +ohos_shared_library("tlssocket") { + sources = [ + "src/context/close_context.cpp", + "src/context/connect_context.cpp", + "src/context/get_certificate_context.cpp", + "src/context/get_cipher_suites_context.cpp", + "src/context/get_protocol_context.cpp", + "src/context/get_remote_certificate_context.cpp", + "src/context/get_signature_algorithms_context.cpp", + "src/tlssocket_async_work.cpp", + "src/tlssocket_exec.cpp", + "src/tlssocket_module.cpp", + ] + + configs = [ ":tlssocket_config" ] + + deps = [ + "$NETSTACK_DIR/frameworks/native/tls_socket:tls_socket", + "$NETSTACK_DIR/utils/napi_utils:napi_utils", + ] + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "napi:ace_napi", + ] + + relative_install_dir = "module/net" + part_name = "netstack" + subsystem_name = "communication" +} diff --git a/frameworks/js/napi/tls/include/constant.h b/frameworks/js/napi/tls/include/constant.h new file mode 100644 index 0000000000000000000000000000000000000000..14b50f8caf55262b6853a06367cc029b3e2a99d0 --- /dev/null +++ b/frameworks/js/napi/tls/include/constant.h @@ -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 TLS_CONSTANT_H +#define TLS_CONSTANT_H + +namespace OHOS { +namespace NetStack { +constexpr int PARAM_NONE = 0; +constexpr int PARAM_JUST_OPTIONS = 1; +constexpr int PARAM_JUST_CALLBACK = 1; +constexpr int PARAM_DOUBLE_OPTIONS = 2; +constexpr int PARAM_TRIPLE_OPTIONS = 3; +constexpr int PARAM_OPTIONS_AND_CALLBACK = 2; +constexpr int PARAM_DOUBLE_OPTIONS_AND_CALLBACK = 3; +constexpr int PARAM_TRIPLE_OPTIONS_AND_CALLBACK = 4; + +enum { + ARG_NUM_0, + ARG_NUM_1, + ARG_NUM_2, +}; + +enum { + ARG_INDEX_0 = 0, + ARG_INDEX_1, + ARG_INDEX_2, + ARG_INDEX_3, +}; +} // namespace NetStack +} //namespace OHOS +#endif // TLS_CONSTANT_H diff --git a/frameworks/js/napi/tls/include/context/close_context.h b/frameworks/js/napi/tls/include/context/close_context.h new file mode 100644 index 0000000000000000000000000000000000000000..9bba6a603c23b4617432f8401888d7c95014c961 --- /dev/null +++ b/frameworks/js/napi/tls/include/context/close_context.h @@ -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 TLS_CONTEXT_CLOSE_CONTEXT_H +#define TLS_CONTEXT_CLOSE_CONTEXT_H + +#include + +#include + +#include "base_context.h" +#include "event_manager.h" +#include "nocopyable.h" + +namespace OHOS { +namespace NetStack { +class CloseContext final : public BaseContext { +public: + DISALLOW_COPY_AND_MOVE(CloseContext); + + CloseContext() = delete; + explicit CloseContext(napi_env env, EventManager *manager); + + 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_CLOSE_CONTEXT_H diff --git a/frameworks/js/napi/tls/include/context/connect_context.h b/frameworks/js/napi/tls/include/context/connect_context.h new file mode 100644 index 0000000000000000000000000000000000000000..d412efc7d0b1d76861d7c253d823fdd8e2234b3a --- /dev/null +++ b/frameworks/js/napi/tls/include/context/connect_context.h @@ -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 TLS_CONTEXT_CONNECT_CONTEXT_H +#define TLS_CONTEXT_CONNECT_CONTEXT_H + +#include + +#include + +#include "base_context.h" +#include "event_manager.h" +#include "nocopyable.h" +#include "tls.h" +#include "tls_socket.h" + +namespace OHOS { +namespace NetStack { +class ConnectContext final : public BaseContext { +public: + DISALLOW_COPY_AND_MOVE(ConnectContext); + + ConnectContext() = delete; + explicit ConnectContext(napi_env env, EventManager *manager); + + TLSConnectOptions connectOptions_; + bool ok_ = false; + + void ParseParams(napi_value *params, size_t paramsCount); + +private: + bool CheckParamsType(napi_value *params, size_t paramsCount); + TLSConnectOptions ReadTlsConnectOptions(napi_env env, napi_value *params); + TLSSecureOptions ReadTlsSecureOptions(napi_env env, napi_value *params); + NetAddress ReadNetAddress(napi_env env, napi_value *params); +}; +} // namespace NetStack +} // namespace OHOS +#endif // TLS_CONTEXT_CONNECT_CONTEXT_H diff --git a/frameworks/js/napi/tls/include/context/get_certificate_context.h b/frameworks/js/napi/tls/include/context/get_certificate_context.h new file mode 100644 index 0000000000000000000000000000000000000000..6b34c767c7471649e996a12a3f231799d0cbe26c --- /dev/null +++ b/frameworks/js/napi/tls/include/context/get_certificate_context.h @@ -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 TLS_CONTEXT_GETCERTIFICATE_CONTEXT_H +#define TLS_CONTEXT_GETCERTIFICATE_CONTEXT_H + +#include +#include + +#include + +#include "base_context.h" +#include "event_manager.h" +#include "nocopyable.h" + +namespace OHOS { +namespace NetStack { +class GetCertificateContext final : public BaseContext { +public: + DISALLOW_COPY_AND_MOVE(GetCertificateContext); + + GetCertificateContext() = delete; + explicit GetCertificateContext(napi_env env, EventManager *manager); + + std::string cert_; + 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_GETCERTIFICATE_CONTEXT_H diff --git a/frameworks/js/napi/tls/include/context/get_cipher_suites_context.h b/frameworks/js/napi/tls/include/context/get_cipher_suites_context.h new file mode 100644 index 0000000000000000000000000000000000000000..6bd1865ebb59246700a757e241f0d6e43514aa77 --- /dev/null +++ b/frameworks/js/napi/tls/include/context/get_cipher_suites_context.h @@ -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_GETCIPHERSUITES_CONTEXT_H +#define TLS_CONTEXT_GETCIPHERSUITES_CONTEXT_H + +#include +#include +#include + +#include + +#include "base_context.h" +#include "event_manager.h" +#include "nocopyable.h" +#include "tls.h" + +namespace OHOS { +namespace NetStack { +class GetCipherSuitesContext final : public BaseContext { +public: + DISALLOW_COPY_AND_MOVE(GetCipherSuitesContext); + + GetCipherSuitesContext() = delete; + explicit GetCipherSuitesContext(napi_env env, EventManager *manager); + + std::vector cipherSuites_; + 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_GETCIPHERSUITES_CONTEXT_H diff --git a/frameworks/js/napi/tls/include/context/get_protocol_context.h b/frameworks/js/napi/tls/include/context/get_protocol_context.h new file mode 100644 index 0000000000000000000000000000000000000000..4d799c92ac2ff5a9bffd01572c96581175a5fdad --- /dev/null +++ b/frameworks/js/napi/tls/include/context/get_protocol_context.h @@ -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 TLS_CONTEXT_GETPROTOCOL_CONTEXT_H +#define TLS_CONTEXT_GETPROTOCOL_CONTEXT_H + +#include +#include + +#include + +#include "base_context.h" +#include "event_manager.h" +#include "nocopyable.h" + +namespace OHOS { +namespace NetStack { +class GetProtocolContext final :public BaseContext { +public: + DISALLOW_COPY_AND_MOVE(GetProtocolContext); + + GetProtocolContext() = delete; + explicit GetProtocolContext(napi_env env, EventManager *manager); + + std::string protocol_; + 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_GETPROTOCOL_CONTEXT_H diff --git a/frameworks/js/napi/tls/include/context/get_remote_certificate_context.h b/frameworks/js/napi/tls/include/context/get_remote_certificate_context.h new file mode 100644 index 0000000000000000000000000000000000000000..f6753bbbf2f196208a6b7265480272b543cc9b81 --- /dev/null +++ b/frameworks/js/napi/tls/include/context/get_remote_certificate_context.h @@ -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 TLS_CONTEXT_GETREMOTECERTIFICATE_CONTEXT_H +#define TLS_CONTEXT_GETREMOTECERTIFICATE_CONTEXT_H + +#include +#include + +#include + +#include "base_context.h" +#include "event_manager.h" +#include "nocopyable.h" + +namespace OHOS { +namespace NetStack { +class GetRemoteCertificateContext final : public BaseContext { +public: + DISALLOW_COPY_AND_MOVE(GetRemoteCertificateContext); + + GetRemoteCertificateContext() = delete; + explicit GetRemoteCertificateContext(napi_env env, EventManager *manager); + + bool needChain_ = false; + bool ok_ = false; + std::string remoteCert_; + + void ParseParams(napi_value *params, size_t paramsCount); + +private: + bool CheckParamsType(napi_value *params, size_t paramsCount); +}; +} // namespace NetStack +} // namespace OHOS +#endif // TLS_CONTEXT_GETREMOTECERTIFICATE_CONTEXT_H diff --git a/frameworks/js/napi/tls/include/context/get_signature_algorithms_context.h b/frameworks/js/napi/tls/include/context/get_signature_algorithms_context.h new file mode 100644 index 0000000000000000000000000000000000000000..7dbcaba8d35e631a5ce7d1b39be89014130524a6 --- /dev/null +++ b/frameworks/js/napi/tls/include/context/get_signature_algorithms_context.h @@ -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 TLS_CONTEXT_GET_SIGNATURE_ALGORITHMS_CONTEXT_H +#define TLS_CONTEXT_GET_SIGNATURE_ALGORITHMS_CONTEXT_H + +#include +#include +#include + +#include + +#include "base_context.h" +#include "event_manager.h" +#include "nocopyable.h" + +namespace OHOS { +namespace NetStack { +class GetSignatureAlgorithmsContext final : public BaseContext { +public: + DISALLOW_COPY_AND_MOVE(GetSignatureAlgorithmsContext); + + GetSignatureAlgorithmsContext() = delete; + explicit GetSignatureAlgorithmsContext(napi_env env, EventManager *manager); + + std::vector signatureAlgorithms_; + 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_GET_SIGNATURE_ALGORITHMS_CONTEXT_H diff --git a/frameworks/js/napi/tls/include/tlssocket_async_work.h b/frameworks/js/napi/tls/include/tlssocket_async_work.h new file mode 100644 index 0000000000000000000000000000000000000000..102c921762bda522017d7a65b08f0377761733fb --- /dev/null +++ b/frameworks/js/napi/tls/include/tlssocket_async_work.h @@ -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_TLSSOCKET_ASYNC_WORK_H +#define TLS_TLSSOCKET_ASYNC_WORK_H + +#include + +#include "nocopyable.h" + +namespace OHOS { +namespace NetStack { +class TlsSocketAsyncWork final { +public: + DISALLOW_COPY_AND_MOVE(TlsSocketAsyncWork); + TlsSocketAsyncWork() = delete; + ~TlsSocketAsyncWork() = delete; + + static void ExecGetCertificate(napi_env env, void *data); + static void ExecConnect(napi_env env, void *data); + static void ExecGetCipherSuites(napi_env env, void *data); + 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 ExecClose(napi_env env, void *data); + + static void GetCertificateCallback(napi_env env, napi_status status, void *data); + static void ConnectCallback(napi_env env, napi_status status, void *data); + static void GetCipherSuitesCallback(napi_env env, napi_status status, void *data); + 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 CloseCallback(napi_env env, napi_status status, void *data); +}; +} // namespace NetStack +} // namespace OHOS +#endif // TLS_TLSSOCKET_ASYNC_WORK_H diff --git a/frameworks/js/napi/tls/include/tlssocket_exec.h b/frameworks/js/napi/tls/include/tlssocket_exec.h new file mode 100644 index 0000000000000000000000000000000000000000..f3239e9926eb2c12f3e0fbefca6d02c8d3ab1184 --- /dev/null +++ b/frameworks/js/napi/tls/include/tlssocket_exec.h @@ -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 TLS_TLSSOCKET_EXEC_H +#define TLS_TLSSOCKET_EXEC_H + +#include + +#include "connect_context.h" +#include "close_context.h" +#include "get_certificate_context.h" +#include "get_cipher_suites_context.h" +#include "get_protocol_context.h" +#include "get_remote_certificate_context.h" +#include "get_signature_algorithms_context.h" + +namespace OHOS { +namespace NetStack { +class TlsSocketExec final { +public: + DISALLOW_COPY_AND_MOVE(TlsSocketExec); + + TlsSocketExec() = delete; + ~TlsSocketExec() = delete; + + static bool ExecGetCertificate(GetCertificateContext *context); + static bool ExecConnect(ConnectContext *context); + static bool ExecGetCipherSuites(GetCipherSuitesContext *context); + static bool ExecGetRemoteCertificate(GetRemoteCertificateContext *context); + static bool ExecGetProtocol(GetProtocolContext *context); + static bool ExecGetSignatureAlgorithms(GetSignatureAlgorithmsContext *context); + static bool ExecClose(CloseContext *context); + + static napi_value GetCertificateCallback(GetCertificateContext *context); + static napi_value ConnectCallback(ConnectContext *context); + static napi_value GetCipherSuitesCallback(GetCipherSuitesContext *context); + static napi_value GetRemoteCertificateCallback(GetRemoteCertificateContext *context); + static napi_value GetProtocolCallback(GetProtocolContext *context); + static napi_value GetSignatureAlgorithmsCallback(GetSignatureAlgorithmsContext *context); + static napi_value CloseCallback(CloseContext *context); +}; +} // namespace NetStack +} // namespace OHOS +#endif // TLS_TLSSOCKET_EXEC_H diff --git a/frameworks/js/napi/tls/include/tlssocket_module.h b/frameworks/js/napi/tls/include/tlssocket_module.h new file mode 100644 index 0000000000000000000000000000000000000000..82f1c5b9fc33a05e51d0d4fe36f3bcaa12ec0fe2 --- /dev/null +++ b/frameworks/js/napi/tls/include/tlssocket_module.h @@ -0,0 +1,57 @@ +/* + * 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 + * + * 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_TLSSOCKET_MODULE_H +#define TLS_TLSSOCKET_MODULE_H + +#include "napi/native_api.h" + +namespace OHOS { +namespace NetStack { +class TLSSocketModuleExports { +public: + class TLSSocket { + 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_CIPHER_SUITES = "getCipherSuites"; + static constexpr const char *FUNCTION_CLOSE = "close"; + + static napi_value GetCertificate(napi_env env, napi_callback_info info); + static napi_value GetProtocol(napi_env env, napi_callback_info info); + static napi_value Connect(napi_env env, napi_callback_info info); + 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 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 napi_value InitTLSSocketModule(napi_env env, napi_value exports); + +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); +}; +} // namespace NetStack +} // namespace OHOS +#endif // TLS_TLSSOCKET_MODULE_H diff --git a/frameworks/js/napi/tls/src/context/close_context.cpp b/frameworks/js/napi/tls/src/context/close_context.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac0b9beb7e583caa37de43e9087f11329b8d62f8 --- /dev/null +++ b/frameworks/js/napi/tls/src/context/close_context.cpp @@ -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. + */ + +#include "close_context.h" + +#include "constant.h" +#include "napi_utils.h" +#include "netstack_log.h" + +namespace OHOS { +namespace NetStack { +CloseContext::CloseContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {} + +void CloseContext::ParseParams(napi_value *params, size_t paramsCount) +{ + if (!CheckParamsType(params, paramsCount)) { + return; + } + if (paramsCount == PARAM_JUST_CALLBACK) { + SetParseOK(SetCallback(params[ARG_INDEX_0]) == napi_ok); + return; + } + SetParseOK(true); +} + +bool CloseContext::CheckParamsType(napi_value *params, size_t paramsCount) +{ + if (paramsCount == PARAM_NONE) { + return true; + } + + if (paramsCount == PARAM_JUST_CALLBACK) { + if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) != napi_function) { + NETSTACK_LOGE("CloseContext first param is not function"); + return false; + } + return true; + } + return false; +} +} // namespace NetStack +} // namespace OHOS diff --git a/frameworks/js/napi/tls/src/context/connect_context.cpp b/frameworks/js/napi/tls/src/context/connect_context.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ae85a1399926e372d6eb7d3d081d8da77ea758c --- /dev/null +++ b/frameworks/js/napi/tls/src/context/connect_context.cpp @@ -0,0 +1,154 @@ +/* + * 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 "connect_context.h" + +#include +#include +#include + +#include "constant.h" +#include "napi_utils.h" +#include "netstack_log.h" + +namespace OHOS { +namespace NetStack { +ConnectContext::ConnectContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {} + +void ConnectContext::ParseParams(napi_value *params, size_t paramsCount) +{ + if (!CheckParamsType(params, paramsCount)) { + return; + } + connectOptions_ = ReadTlsConnectOptions(GetEnv(), params); + + if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) { + SetParseOK(SetCallback(params[ARG_INDEX_1]) == napi_ok); + return; + } + SetParseOK(true); +} + +bool ConnectContext::CheckParamsType(napi_value *params, size_t paramsCount) +{ + if (paramsCount == PARAM_JUST_OPTIONS) { + if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) != napi_object) { + NETSTACK_LOGE("tlsConnectContext first param is not object"); + return false; + } + return true; + } + + 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"); + return false; + } + if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_1]) != napi_function) { + NETSTACK_LOGE(" tls ConnectContext second param is not function"); + return false; + } + return true; + } + return false; +} + +TLSConnectOptions ConnectContext::ReadTlsConnectOptions(napi_env env, napi_value *params) +{ + TLSConnectOptions options; + NetAddress address = ReadNetAddress(GetEnv(), params); + 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"); + uint32_t arrayLength = NapiUtils::GetArrayLength(GetEnv(), alpnProtocols); + napi_value elementValue = nullptr; + std::vector alpnProtocolVec; + for (uint32_t i = 0; i < arrayLength; i++) { + elementValue = NapiUtils::GetArrayElement(GetEnv(), alpnProtocols, i); + std::string alpnProtocol = NapiUtils::GetStringFromValueUtf8(GetEnv(), elementValue); + alpnProtocolVec.push_back(alpnProtocol); + } + options.SetAlpnProtocols(alpnProtocolVec); + } + NETSTACK_LOGI("ConnectContext::ReadTlsConnectOptions(napi_env env, napi_value *params) end"); + return options; +} + +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 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); + } + //key + std::string key = NapiUtils::GetStringPropertyUtf8(env, secureOptions, "key"); + secureOption.SetKey(key); + //cert + 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 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); + return secureOption; +} + +NetAddress ConnectContext::ReadNetAddress(napi_env env, napi_value *params) +{ + NetAddress address; + napi_value netAddress = NapiUtils::GetNamedProperty(GetEnv(), params[0], "address"); + + std::string addr = NapiUtils::GetStringPropertyUtf8(GetEnv(), netAddress, "address"); + address.SetAddress(addr); + uint32_t family = NapiUtils::GetUint32Property(GetEnv(), netAddress, "family"); + if (family == 1) { + address.SetFamilyBySaFamily(AF_INET); + } else { + address.SetFamilyBySaFamily(AF_INET6); + } + if (NapiUtils::HasNamedProperty(GetEnv(), netAddress, "port")) { + uint16_t port = static_cast(NapiUtils::GetUint32Property(GetEnv(), netAddress, "port")); + address.SetPort(port); + } + return address; +} +} // namespace NetStack +} // namespace OHOS diff --git a/frameworks/js/napi/tls/src/context/get_certificate_context.cpp b/frameworks/js/napi/tls/src/context/get_certificate_context.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6c7cd0fed52d52fa6f3893e80d899e179e5b4f0 --- /dev/null +++ b/frameworks/js/napi/tls/src/context/get_certificate_context.cpp @@ -0,0 +1,55 @@ +/* + * 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 "get_certificate_context.h" + +#include "constant.h" +#include "napi_utils.h" +#include "netstack_log.h" + +namespace OHOS { +namespace NetStack { +GetCertificateContext::GetCertificateContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {} + +void GetCertificateContext::ParseParams(napi_value *params, size_t paramsCount) +{ + NETSTACK_LOGI("GetCertificateContext paramsCount is %{public}zu", paramsCount); + if (!CheckParamsType(params, paramsCount)) { + return; + } + if (paramsCount == PARAM_JUST_CALLBACK) { + SetParseOK(SetCallback(params[ARG_INDEX_0]) == napi_ok); + return; + } + SetParseOK(true); +} + +bool GetCertificateContext::CheckParamsType(napi_value *params, size_t paramsCount) +{ + if (paramsCount == PARAM_NONE) { + return true; + } + + if (paramsCount == PARAM_JUST_CALLBACK) { + if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) != napi_function) { + NETSTACK_LOGE("GetCertificateContext first param is not function"); + return false; + } + return true; + } + return false; +} +} // namespace NetStack +} // namespace OHOS diff --git a/frameworks/js/napi/tls/src/context/get_cipher_suites_context.cpp b/frameworks/js/napi/tls/src/context/get_cipher_suites_context.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cf2e1eed5b7de3950802ff420cd6626cbdc3ead --- /dev/null +++ b/frameworks/js/napi/tls/src/context/get_cipher_suites_context.cpp @@ -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. + */ + +#include "get_cipher_suites_context.h" + +#include "constant.h" +#include "napi_utils.h" +#include "netstack_log.h" + +namespace OHOS { +namespace NetStack { +GetCipherSuitesContext::GetCipherSuitesContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {} + +void GetCipherSuitesContext::ParseParams(napi_value *params, size_t paramsCount) +{ + if (!CheckParamsType(params, paramsCount)) { + return; + } + if (paramsCount == PARAM_JUST_CALLBACK) { + SetParseOK(SetCallback(params[ARG_INDEX_0]) == napi_ok); + return; + } + SetParseOK(true); +} + +bool GetCipherSuitesContext::CheckParamsType(napi_value *params, size_t paramsCount) +{ + if (paramsCount == PARAM_NONE) { + return true; + } + + if (paramsCount == PARAM_JUST_CALLBACK) { + if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) != napi_function) { + NETSTACK_LOGE("GetCipherSuitesContext first param is not function"); + return false; + } + return true; + } + return false; +} +} // namespace NetStack +} // namespace OHOS diff --git a/frameworks/js/napi/tls/src/context/get_protocol_context.cpp b/frameworks/js/napi/tls/src/context/get_protocol_context.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25ff067cc59d817b70db91461fec477416a171bd --- /dev/null +++ b/frameworks/js/napi/tls/src/context/get_protocol_context.cpp @@ -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. + */ + +#include "get_protocol_context.h" + +#include "constant.h" +#include "napi_utils.h" +#include "netstack_log.h" + +namespace OHOS { +namespace NetStack { +GetProtocolContext::GetProtocolContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {} + +void GetProtocolContext::ParseParams(napi_value *params, size_t paramsCount) +{ + if (!CheckParamsType(params, paramsCount)) { + return; + } + if (paramsCount == PARAM_JUST_CALLBACK) { + SetParseOK(SetCallback(params[ARG_INDEX_0]) == napi_ok); + return; + } + SetParseOK(true); +} + +bool GetProtocolContext::CheckParamsType(napi_value *params, size_t paramsCount) +{ + if (paramsCount == PARAM_NONE) { + return true; + } + + if (paramsCount == PARAM_JUST_CALLBACK) { + if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) != napi_function) { + NETSTACK_LOGE("GetProtocolContext first param is not function"); + return false; + } + return true; + } + return false; +} +} // namespace NetStack +} // namespace OHOS diff --git a/frameworks/js/napi/tls/src/context/get_remote_certificate_context.cpp b/frameworks/js/napi/tls/src/context/get_remote_certificate_context.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f53aec9099f6149985d6228eac54539510daf92e --- /dev/null +++ b/frameworks/js/napi/tls/src/context/get_remote_certificate_context.cpp @@ -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. + */ + +#include "get_remote_certificate_context.h" + +#include "constant.h" +#include "napi_utils.h" +#include "netstack_log.h" + +namespace OHOS { +namespace NetStack { +GetRemoteCertificateContext::GetRemoteCertificateContext(napi_env env, EventManager *manager) + : BaseContext(env, manager) {} + +void GetRemoteCertificateContext::ParseParams(napi_value *params, size_t paramsCount) +{ + if (!CheckParamsType(params, paramsCount)) { + return; + } + if (paramsCount == PARAM_JUST_CALLBACK) { + SetParseOK(SetCallback(params[ARG_INDEX_0]) == napi_ok); + return; + } + SetParseOK(true); +} + +bool GetRemoteCertificateContext::CheckParamsType(napi_value *params, size_t paramsCount) +{ + if (paramsCount == PARAM_NONE) { + return true; + } + if (paramsCount == PARAM_JUST_CALLBACK) { + if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) != napi_function) { + NETSTACK_LOGE("GetRemoteCertificateContext first param is not function"); + return false; + } + return true; + } + return false; +} +} // namespace NetStack +} // namespace OHOS diff --git a/frameworks/js/napi/tls/src/context/get_signature_algorithms_context.cpp b/frameworks/js/napi/tls/src/context/get_signature_algorithms_context.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd3941a45207dd0a2895f29b2ee4da82c0bae4d1 --- /dev/null +++ b/frameworks/js/napi/tls/src/context/get_signature_algorithms_context.cpp @@ -0,0 +1,55 @@ +/* + * 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 "get_signature_algorithms_context.h" + +#include "constant.h" +#include "napi_utils.h" +#include "netstack_log.h" + +namespace OHOS { +namespace NetStack { +GetSignatureAlgorithmsContext::GetSignatureAlgorithmsContext(napi_env env, EventManager *manager) + : BaseContext(env, manager) {} + +void GetSignatureAlgorithmsContext::ParseParams(napi_value *params, size_t paramsCount) +{ + if (!CheckParamsType(params, paramsCount)) { + return; + } + if (paramsCount == PARAM_JUST_CALLBACK) { + SetParseOK(SetCallback(params[ARG_INDEX_0]) == napi_ok); + return; + } + SetParseOK(true); +} + +bool GetSignatureAlgorithmsContext::CheckParamsType(napi_value *params, size_t paramsCount) +{ + if (paramsCount == PARAM_NONE) { + return true; + } + + if (paramsCount == PARAM_JUST_CALLBACK) { + if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) != napi_function) { + NETSTACK_LOGE("GetSignatureAlgorithmsContext first param is not function"); + return false; + } + return true; + } + return false; +} +} // namespace NetStack +} // namespace OHOS diff --git a/frameworks/js/napi/tls/src/tlssocket_async_work.cpp b/frameworks/js/napi/tls/src/tlssocket_async_work.cpp new file mode 100644 index 0000000000000000000000000000000000000000..163f041c0dd7c3c6f70a985363817026a4e80cd7 --- /dev/null +++ b/frameworks/js/napi/tls/src/tlssocket_async_work.cpp @@ -0,0 +1,98 @@ +/* + * 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 "tlssocket_async_work.h" + +#include "base_async_work.h" +#include "connect_context.h" +#include "close_context.h" +#include "get_certificate_context.h" +#include "get_cipher_suites_context.h" +#include "get_remote_certificate_context.h" +#include "tlssocket_exec.h" + +namespace OHOS { +namespace NetStack { +void TlsSocketAsyncWork::ExecGetCertificate(napi_env env, void *data) +{ + BaseAsyncWork::ExecAsyncWork(env, data); +} + +void TlsSocketAsyncWork::ExecConnect(napi_env env, void *data) +{ + BaseAsyncWork::ExecAsyncWork(env, data); +} + +void TlsSocketAsyncWork::ExecGetCipherSuites(napi_env env, void *data) +{ + BaseAsyncWork::ExecAsyncWork(env, data); +} + +void TlsSocketAsyncWork::ExecGetRemoteCertificate(napi_env env, void *data) +{ + BaseAsyncWork::ExecAsyncWork(env, data); +} + +void TlsSocketAsyncWork::ExecGetProtocol(napi_env env, void *data) +{ + BaseAsyncWork::ExecAsyncWork(env, data); +} + +void TlsSocketAsyncWork::ExecGetSignatureAlgorithms(napi_env env, void *data) +{ + BaseAsyncWork::ExecAsyncWork(env, data); +} + +void TlsSocketAsyncWork::ExecClose(napi_env env, void *data) +{ + BaseAsyncWork::ExecAsyncWork(env, data); +} + +void TlsSocketAsyncWork::GetCertificateCallback(napi_env env, napi_status status, void *data) +{ + BaseAsyncWork::AsyncWorkCallback(env, status, data); +} + +void TlsSocketAsyncWork::ConnectCallback(napi_env env, napi_status status, void *data) +{ + BaseAsyncWork::AsyncWorkCallback(env, status, data); +} + +void TlsSocketAsyncWork::GetCipherSuitesCallback(napi_env env, napi_status status, void *data) +{ + BaseAsyncWork::AsyncWorkCallback(env, status, data); +} + +void TlsSocketAsyncWork::GetRemoteCertificateCallback(napi_env env, napi_status status, void *data) +{ + BaseAsyncWork::AsyncWorkCallback(env, status, data); +} + +void TlsSocketAsyncWork::GetProtocolCallback(napi_env env, napi_status status, void *data) +{ + BaseAsyncWork::AsyncWorkCallback(env, status, data); +} + +void TlsSocketAsyncWork::GetSignatureAlgorithmsCallback(napi_env env, napi_status status, void *data) +{ + BaseAsyncWork::AsyncWorkCallback(env, status, data); +} + +void TlsSocketAsyncWork::CloseCallback(napi_env env, napi_status status, void *data) +{ + BaseAsyncWork::AsyncWorkCallback(env, status, data); +} +} // namespace NetStack +} // namespace OHOS diff --git a/frameworks/js/napi/tls/src/tlssocket_exec.cpp b/frameworks/js/napi/tls/src/tlssocket_exec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5799fc126ff2a0b267034aa2bd6d090df3c5b6f --- /dev/null +++ b/frameworks/js/napi/tls/src/tlssocket_exec.cpp @@ -0,0 +1,228 @@ +/* + * 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 "tlssocket_exec.h" + +#include + +#include "napi_utils.h" +#include "netstack_log.h" +#include "tls_socket.h" + +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(); + if (manager == nullptr) { + NETSTACK_LOGE("manager is nullptr"); + return false; + } + auto tlsSocket = reinterpret_cast(manager->GetData()); + if (tlsSocket == nullptr) { + NETSTACK_LOGE("TlsSocketExec::ExecGetCipherSuites tlsSocket is null"); + return false; + } + tlsSocket->GetCertificate([&context](bool ok, const std::string &cert) { + context->ok_ = ok; + context->cert_ = cert; + }); + return context->ok_; +} + +bool TlsSocketExec::ExecConnect(ConnectContext *context) +{ + auto manager = context->GetManager(); + if (manager == nullptr) { + NETSTACK_LOGE("manager is nullptr"); + return false; + } + if (manager->GetData() != nullptr) { + NETSTACK_LOGE("tls tlsSocket is existed"); + return false; + } + auto tlsSocket = new TLSSocket(); + 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; +} + +bool TlsSocketExec::ExecGetCipherSuites(GetCipherSuitesContext *context) +{ + auto manager = context->GetManager(); + if (manager == nullptr) { + NETSTACK_LOGE("manager is nullptr"); + return false; + } + auto tlsSocket = reinterpret_cast(manager->GetData()); + if (tlsSocket == nullptr) { + NETSTACK_LOGE("TlsSocketExec::ExecGetCipherSuites tlsSocket is null"); + return false; + } + tlsSocket->GetCipherSuite([&context](bool ok, const std::vector &suite) { + context->ok_ = ok; + context->cipherSuites_ = suite; + }); + return context->ok_; +} + +bool TlsSocketExec::ExecGetRemoteCertificate(GetRemoteCertificateContext *context) +{ + auto manager = context->GetManager(); + if (manager == nullptr) { + NETSTACK_LOGE("manager is nullptr"); + return false; + } + auto tlsSocket = reinterpret_cast(manager->GetData()); + if (tlsSocket == nullptr) { + NETSTACK_LOGE("TlsSocketExec::ExecGetRemoteCertificate tlsSocket is null"); + return false; + } + tlsSocket->GetRemoteCertificate([&context](bool ok, const std::string &cert) { + context->ok_ = ok; + context->remoteCert_ = cert; + + }); + return context->ok_; +} + +bool TlsSocketExec::ExecGetProtocol(GetProtocolContext *context) +{ + auto manager = context->GetManager(); + if (manager == nullptr) { + NETSTACK_LOGE("manager is nullptr"); + return false; + } + auto tlsSocket = reinterpret_cast(manager->GetData()); + if (tlsSocket == nullptr) { + NETSTACK_LOGE("TlsSocketExec::ExecGetProtocol tlsSocket is null"); + return false; + } + tlsSocket->GetProtocol([&context](bool ok, const std::string &protocol) { + context->ok_ = ok; + context->protocol_ = protocol; + }); + return context->ok_; +} + +bool TlsSocketExec::ExecGetSignatureAlgorithms(GetSignatureAlgorithmsContext *context) +{ + auto manager = context->GetManager(); + if (manager == nullptr) { + NETSTACK_LOGE("manager is nullptr"); + return false; + } + auto tlsSocket = reinterpret_cast(manager->GetData()); + if (tlsSocket == nullptr) { + NETSTACK_LOGE("TlsSocketExec::ExecGetSignatureAlgorithms tlsSocket is null"); + return false; + } + tlsSocket->GetSignatureAlgorithms([&context](bool ok, const std::vector &algorithms) { + context->ok_ = ok; + context->signatureAlgorithms_ = algorithms; + }); + 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(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_; +} + +napi_value TlsSocketExec::GetCertificateCallback(GetCertificateContext *context) +{ + return NapiUtils::CreateStringUtf8(context->GetEnv(), context->cert_); +} + +napi_value TlsSocketExec::ConnectCallback(ConnectContext *context) +{ + return NapiUtils::GetUndefined(context->GetEnv()); +} + +napi_value TlsSocketExec::GetCipherSuitesCallback(GetCipherSuitesContext *context) +{ + napi_value cipherSuites = NapiUtils::CreateArray(context->GetEnv(), 0); + int index = 0; + for (const auto &cipher : context->cipherSuites_) { + napi_value cipherSuite = NapiUtils::CreateStringUtf8(context->GetEnv(), cipher); + NapiUtils::SetArrayElement(context->GetEnv(), cipherSuites, index++, cipherSuite); + } + return cipherSuites; +} + +napi_value TlsSocketExec::GetRemoteCertificateCallback(GetRemoteCertificateContext *context) +{ + return NapiUtils::CreateStringUtf8(context->GetEnv(), context->remoteCert_); +} + +napi_value TlsSocketExec::GetProtocolCallback(GetProtocolContext *context) +{ + return NapiUtils::CreateStringUtf8(context->GetEnv(), context->protocol_); +} + +napi_value TlsSocketExec::GetSignatureAlgorithmsCallback(GetSignatureAlgorithmsContext *context) +{ + napi_value signatureAlgorithms = NapiUtils::CreateArray(context->GetEnv(), 0); + int index = 0; + for (const auto &algorithm : context->signatureAlgorithms_) { + napi_value signatureAlgorithm = NapiUtils::CreateStringUtf8(context->GetEnv(), algorithm); + NapiUtils::SetArrayElement(context->GetEnv(), signatureAlgorithms, index++, signatureAlgorithm); + } + return signatureAlgorithms; +} + +napi_value TlsSocketExec::CloseCallback(CloseContext *context) +{ + return NapiUtils::GetBoolean(context->GetEnv(), true); +} +} // namespace NetStack +} // namespace OHOS diff --git a/frameworks/js/napi/tls/src/tlssocket_module.cpp b/frameworks/js/napi/tls/src/tlssocket_module.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bbaee645f65bac67cd904c7015e09873e5ce871a --- /dev/null +++ b/frameworks/js/napi/tls/src/tlssocket_module.cpp @@ -0,0 +1,144 @@ +/* + * 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 + * + * 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 "tlssocket_module.h" + +#include + +#include +#include + +#include "connect_context.h" +#include "close_context.h" +#include "event_manager.h" +#include "get_certificate_context.h" +#include "get_remote_certificate_context.h" +#include "get_signature_algorithms_context.h" +#include "get_cipher_suites_context.h" +#include "module_template.h" +#include "napi_utils.h" +#include "netstack_log.h" +#include "tlssocket_async_work.h" + +namespace OHOS { +namespace NetStack { +namespace { +constexpr const char *TLS_SOCKET_MODULE_NAME = "net.tlssocket"; +} // namespace + +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) +{ + return ModuleTemplate::Interface(env, info, FUNCTION_GET_CERTIFICATE, nullptr, + TlsSocketAsyncWork::ExecGetCertificate, + TlsSocketAsyncWork::GetCertificateCallback); +} + +napi_value TLSSocketModuleExports::TLSSocket::GetProtocol(napi_env env , napi_callback_info info) +{ + return ModuleTemplate::Interface(env, info, FUNCTION_GET_PROTOCOL, nullptr, + TlsSocketAsyncWork::ExecGetProtocol, + TlsSocketAsyncWork::GetProtocolCallback); +} + +napi_value TLSSocketModuleExports::TLSSocket::Connect(napi_env env , napi_callback_info info) +{ + return ModuleTemplate::Interface(env, info, FUNCTION_CONNECT, nullptr, + TlsSocketAsyncWork::ExecConnect, + TlsSocketAsyncWork::ConnectCallback); +} + +napi_value TLSSocketModuleExports::TLSSocket::GetCipherSuites(napi_env env , napi_callback_info info) +{ + return ModuleTemplate::Interface(env, info, FUNCTION_GET_CIPHER_SUITES, nullptr, + TlsSocketAsyncWork::ExecGetCipherSuites, + TlsSocketAsyncWork::GetCipherSuitesCallback); +} + +napi_value TLSSocketModuleExports::TLSSocket::GetRemoteCertificate(napi_env env , napi_callback_info info) +{ + return ModuleTemplate::Interface(env, info, FUNCTION_GET_REMOTE_CERTIFICATE, nullptr, + TlsSocketAsyncWork::ExecGetRemoteCertificate, + TlsSocketAsyncWork::GetRemoteCertificateCallback); +} + +napi_value TLSSocketModuleExports::TLSSocket::GetSignatureAlgorithms(napi_env env , napi_callback_info info) +{ + return ModuleTemplate::Interface(env, info, FUNCTION_GET_SIGNATURE_ALGORITHMS, nullptr, + TlsSocketAsyncWork::ExecGetSignatureAlgorithms, + TlsSocketAsyncWork::GetSignatureAlgorithmsCallback); +} + +napi_value TLSSocketModuleExports::TLSSocket::Close(napi_env env , napi_callback_info info) +{ + return ModuleTemplate::Interface(env, info, FUNCTION_CLOSE, nullptr, + TlsSocketAsyncWork::ExecClose, + TlsSocketAsyncWork::CloseCallback); +} + +void TLSSocketModuleExports::DefineTLSSocketClass(napi_env env, napi_value exports) +{ + std::initializer_list functions = { + DECLARE_NAPI_FUNCTION(TLSSocket::FUNCTION_GET_CERTIFICATE, TLSSocket::GetCertificate), + DECLARE_NAPI_FUNCTION(TLSSocket::FUNCTION_GET_REMOTE_CERTIFICATE, TLSSocket::GetRemoteCertificate), + DECLARE_NAPI_FUNCTION(TLSSocket::FUNCTION_GET_SIGNATURE_ALGORITHMS, TLSSocket::GetSignatureAlgorithms), + 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_CLOSE, TLSSocket::Close), + }; + ModuleTemplate::DefineClass(env, exports, functions, INTERFACE_TLS_SOCKET); +} + +napi_value TLSSocketModuleExports::ConstructTLSSocketInstance(napi_env env, napi_callback_info info) +{ + return ModuleTemplate::NewInstance(env, info, INTERFACE_TLS_SOCKET, Finalize); +} + +void TLSSocketModuleExports::InitTLSSocketProperties(napi_env env, napi_value exports) +{ + std::initializer_list properties = { + DECLARE_NAPI_FUNCTION(FUNCTION_CONSTRUCTOR_TLS_SOCKET_INSTANCE, ConstructTLSSocketInstance), + }; + NapiUtils::DefineProperties(env, exports, properties); +} + +napi_value TLSSocketModuleExports::InitTLSSocketModule(napi_env env, napi_value exports) +{ + DefineTLSSocketClass(env, exports); + InitTLSSocketProperties(env, exports); + return exports; +} + +static napi_module g_tlssocketModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = TLSSocketModuleExports::InitTLSSocketModule, + .nm_modname = TLS_SOCKET_MODULE_NAME, + .nm_priv = nullptr, + .reserved = {nullptr}, +}; + +extern "C" __attribute__((constructor)) void RegisterTlsSocket(void) +{ + napi_module_register(&g_tlssocketModule); +} +} // namespace NetStack +} // namespace OHOS