• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "tlssocketserver_module.h"
17 
18 #include <initializer_list>
19 #include <napi/native_common.h>
20 
21 #include "common_context.h"
22 #include "event_manager.h"
23 #include "module_template.h"
24 #include "monitor_server.h"
25 #include "napi_utils.h"
26 #include "netstack_log.h"
27 #include "tls.h"
28 #include "tls_bind_context.h"
29 #include "tls_connect_context.h"
30 #include "tls_extra_context.h"
31 #include "tls_napi_context.h"
32 #include "tls_server_close_context.h"
33 #include "tls_server_napi_context.h"
34 #include "tls_server_send_context.h"
35 #include "tlssocketserver_async_work.h"
36 
37 namespace OHOS {
38 namespace NetStack {
39 namespace TlsSocketServer {
40 namespace {
41 static constexpr const char *PROTOCOL_TLSV13 = "TLSv13";
42 static constexpr const char *PROTOCOL_TLSV12 = "TLSv12";
43 static constexpr const char *NAME_STOP_SERVER = "closeTLSServer";
44 
Finalize(napi_env,void * data,void *)45 void Finalize(napi_env, void *data, void *)
46 {
47     NETSTACK_LOGI("tls socket server is finalized");
48     auto sharedManager = reinterpret_cast<std::shared_ptr<EventManager> *>(data);
49     delete sharedManager;
50 }
51 } // namespace
52 
GetCertificate(napi_env env,napi_callback_info info)53 napi_value TLSSocketServerModuleExports::TLSSocketServer::GetCertificate(napi_env env, napi_callback_info info)
54 {
55     return ModuleTemplate::InterfaceWithSharedManager<TlsSocket::GetCertificateContext>(
56         env, info, FUNCTION_GET_CERTIFICATE, nullptr, TLSSocketServerAsyncWork::ExecGetCertificate,
57         TLSSocketServerAsyncWork::GetCertificateCallback);
58 }
59 
GetProtocol(napi_env env,napi_callback_info info)60 napi_value TLSSocketServerModuleExports::TLSSocketServer::GetProtocol(napi_env env, napi_callback_info info)
61 {
62     return ModuleTemplate::InterfaceWithSharedManager<TlsSocket::GetProtocolContext>(env, info, FUNCTION_GET_PROTOCOL,
63         nullptr, TLSSocketServerAsyncWork::ExecGetProtocol, TLSSocketServerAsyncWork::GetProtocolCallback);
64 }
65 
Listen(napi_env env,napi_callback_info info)66 napi_value TLSSocketServerModuleExports::TLSSocketServer::Listen(napi_env env, napi_callback_info info)
67 {
68     return ModuleTemplate::InterfaceWithSharedManager<TlsSocket::TLSListenContext>(env, info, FUNCTION_LISTEN, nullptr,
69                                                                   TLSSocketServerAsyncWork::ExecListen,
70                                                                   TLSSocketServerAsyncWork::ListenCallback);
71 }
72 
Stop(napi_env env,napi_callback_info info)73 napi_value TLSSocketServerModuleExports::TLSSocketServer::Stop(napi_env env, napi_callback_info info)
74 {
75     return ModuleTemplate::InterfaceWithSharedManager<TlsSocket::TLSNapiContext>(env, info, NAME_STOP_SERVER, nullptr,
76                                                                   TLSSocketServerAsyncWork::ExecStop,
77                                                                   TLSSocketServerAsyncWork::StopCallback);
78 }
79 
Send(napi_env env,napi_callback_info info)80 napi_value TLSSocketServerModuleExports::TLSSocketConnection::Send(napi_env env, napi_callback_info info)
81 {
82     return ModuleTemplate::InterfaceWithSharedManager<TLSServerSendContext>(
83         env, info, FUNCTION_SEND,
84         [](napi_env theEnv, napi_value thisVal, TLSServerSendContext *context) -> bool {
85             context->clientId_ = NapiUtils::GetInt32Property(theEnv, thisVal, PROPERTY_CLIENT_ID);
86             return true;
87         },
88         TLSSocketServerAsyncWork::ExecSend, TLSSocketServerAsyncWork::SendCallback);
89 }
90 
Close(napi_env env,napi_callback_info info)91 napi_value TLSSocketServerModuleExports::TLSSocketConnection::Close(napi_env env, napi_callback_info info)
92 {
93     return ModuleTemplate::InterfaceWithSharedManager<TLSServerCloseContext>(
94         env, info, FUNCTION_CLOSE,
95         [](napi_env theEnv, napi_value thisVal, TLSServerCloseContext *context) -> bool {
96             context->clientId_ = NapiUtils::GetInt32Property(theEnv, thisVal, PROPERTY_CLIENT_ID);
97             return true;
98         },
99         TLSSocketServerAsyncWork::ExecClose, TLSSocketServerAsyncWork::CloseCallback);
100 }
101 
GetRemoteAddress(napi_env env,napi_callback_info info)102 napi_value TLSSocketServerModuleExports::TLSSocketConnection::GetRemoteAddress(napi_env env, napi_callback_info info)
103 {
104     return ModuleTemplate::InterfaceWithSharedManager<ServerTLSGetRemoteAddressContext>(
105         env, info, FUNCTION_GET_REMOTE_ADDRESS,
106         [](napi_env theEnv, napi_value thisVal, ServerTLSGetRemoteAddressContext *context) -> bool {
107             context->clientId_ = NapiUtils::GetInt32Property(theEnv, thisVal, PROPERTY_CLIENT_ID);
108             return true;
109         },
110         TLSSocketServerAsyncWork::ExecGetRemoteAddress, TLSSocketServerAsyncWork::GetRemoteAddressCallback);
111 }
112 
GetLocalAddress(napi_env env,napi_callback_info info)113 napi_value TLSSocketServerModuleExports::TLSSocketConnection::GetLocalAddress(napi_env env, napi_callback_info info)
114 {
115     return ModuleTemplate::InterfaceWithSharedManager<TLSServerGetLocalAddressContext>(
116         env, info, FUNCTION_GET_LOCAL_ADDRESS,
117         [](napi_env theEnv, napi_value thisVal, TLSServerGetLocalAddressContext *context) -> bool {
118             context->clientId_ = NapiUtils::GetInt32Property(theEnv, thisVal, PROPERTY_CLIENT_ID);
119             return true;
120         },
121         TLSSocketServerAsyncWork::ExecConnectionGetLocalAddress,
122             TLSSocketServerAsyncWork::GetConnectionLocalAddressCallback);
123 }
124 
GetRemoteCertificate(napi_env env,napi_callback_info info)125 napi_value TLSSocketServerModuleExports::TLSSocketConnection::GetRemoteCertificate(napi_env env,
126                                                                                    napi_callback_info info)
127 {
128     return ModuleTemplate::InterfaceWithSharedManager<ServerGetRemoteCertificateContext>(
129         env, info, FUNCTION_GET_REMOTE_CERTIFICATE,
130         [](napi_env theEnv, napi_value thisVal, ServerGetRemoteCertificateContext *context) -> bool {
131             context->clientId_ = NapiUtils::GetInt32Property(theEnv, thisVal, PROPERTY_CLIENT_ID);
132             return true;
133         },
134         TLSSocketServerAsyncWork::ExecGetRemoteCertificate, TLSSocketServerAsyncWork::GetRemoteCertificateCallback);
135 }
136 
GetCipherSuites(napi_env env,napi_callback_info info)137 napi_value TLSSocketServerModuleExports::TLSSocketConnection::GetCipherSuites(napi_env env, napi_callback_info info)
138 {
139     return ModuleTemplate::InterfaceWithSharedManager<ServerGetCipherSuitesContext>(
140         env, info, FUNCTION_GET_CIPHER_SUITE,
141         [](napi_env theEnv, napi_value thisVal, ServerGetCipherSuitesContext *context) -> bool {
142             context->clientId_ = NapiUtils::GetInt32Property(theEnv, thisVal, PROPERTY_CLIENT_ID);
143             return true;
144         },
145         TLSSocketServerAsyncWork::ExecGetCipherSuites, TLSSocketServerAsyncWork::GetCipherSuitesCallback);
146 }
147 
GetSignatureAlgorithms(napi_env env,napi_callback_info info)148 napi_value TLSSocketServerModuleExports::TLSSocketConnection::GetSignatureAlgorithms(napi_env env,
149                                                                                      napi_callback_info info)
150 {
151     return ModuleTemplate::InterfaceWithSharedManager<ServerGetSignatureAlgorithmsContext>(
152         env, info, FUNCTION_GET_SIGNATURE_ALGORITHMS,
153         [](napi_env theEnv, napi_value thisVal, ServerGetSignatureAlgorithmsContext *context) -> bool {
154             context->clientId_ = NapiUtils::GetInt32Property(theEnv, thisVal, PROPERTY_CLIENT_ID);
155             return true;
156         },
157         TLSSocketServerAsyncWork::ExecGetSignatureAlgorithms, TLSSocketServerAsyncWork::GetSignatureAlgorithmsCallback);
158 }
159 
On(napi_env env,napi_callback_info info)160 napi_value TLSSocketServerModuleExports::TLSSocketConnection::On(napi_env env, napi_callback_info info)
161 {
162     return DelayedSingleton<MonitorServer>::GetInstance()->ConnectionOn(env, info);
163 }
164 
Off(napi_env env,napi_callback_info info)165 napi_value TLSSocketServerModuleExports::TLSSocketConnection::Off(napi_env env, napi_callback_info info)
166 {
167     return DelayedSingleton<MonitorServer>::GetInstance()->ConnectionOff(env, info);
168 }
169 
GetCertificate(napi_env env,napi_callback_info info)170 napi_value TLSSocketServerModuleExports::TLSSocketConnection::GetCertificate(napi_env env, napi_callback_info info)
171 {
172     return ModuleTemplate::InterfaceWithSharedManager<TlsSocket::GetCertificateContext>(
173         env, info, FUNCTION_GET_CERTIFICATE, nullptr, TLSSocketServerAsyncWork::ExecGetCertificate,
174         TLSSocketServerAsyncWork::GetCertificateCallback);
175 }
176 
GetState(napi_env env,napi_callback_info info)177 napi_value TLSSocketServerModuleExports::TLSSocketServer::GetState(napi_env env, napi_callback_info info)
178 {
179     return ModuleTemplate::InterfaceWithSharedManager<TlsSocket::TLSGetStateContext>(env, info, FUNCTION_GET_STATE,
180         nullptr, TLSSocketServerAsyncWork::ExecGetState, TLSSocketServerAsyncWork::GetStateCallback);
181 }
182 
GetLocalAddress(napi_env env,napi_callback_info info)183 napi_value TLSSocketServerModuleExports::TLSSocketServer::GetLocalAddress(napi_env env, napi_callback_info info)
184 {
185     return ModuleTemplate::InterfaceWithSharedManager<TLSServerGetLocalAddressContext>(
186         env, info, FUNCTION_GET_LOCAL_ADDRESS, nullptr, TLSSocketServerAsyncWork::ExecGetLocalAddress,
187         TLSSocketServerAsyncWork::GetLocalAddressCallback);
188 }
189 
SetExtraOptions(napi_env env,napi_callback_info info)190 napi_value TLSSocketServerModuleExports::TLSSocketServer::SetExtraOptions(napi_env env, napi_callback_info info)
191 {
192     return ModuleTemplate::InterfaceWithSharedManager<TlsSocket::TLSSetExtraOptionsContext>(
193         env, info, FUNCTION_SET_EXTRA_OPTIONS, nullptr, TLSSocketServerAsyncWork::ExecSetExtraOptions,
194         TLSSocketServerAsyncWork::SetExtraOptionsCallback);
195 }
196 
On(napi_env env,napi_callback_info info)197 napi_value TLSSocketServerModuleExports::TLSSocketServer::On(napi_env env, napi_callback_info info)
198 {
199     return DelayedSingleton<MonitorServer>::GetInstance()->On(env, info);
200 }
201 
Off(napi_env env,napi_callback_info info)202 napi_value TLSSocketServerModuleExports::TLSSocketServer::Off(napi_env env, napi_callback_info info)
203 {
204     return DelayedSingleton<MonitorServer>::GetInstance()->Off(env, info);
205 }
206 
DefineTLSSocketServerClass(napi_env env,napi_value exports)207 void TLSSocketServerModuleExports::DefineTLSSocketServerClass(napi_env env, napi_value exports)
208 {
209     std::initializer_list<napi_property_descriptor> functions = {
210         DECLARE_NAPI_FUNCTION(TLSSocketServer::FUNCTION_LISTEN, TLSSocketServer::Listen),
211         DECLARE_NAPI_FUNCTION(TLSSocketServer::FUNCTION_STOP, TLSSocketServer::Stop),
212         DECLARE_NAPI_FUNCTION(TLSSocketServer::FUNCTION_GET_STATE, TLSSocketServer::GetState),
213         DECLARE_NAPI_FUNCTION(TLSSocketServer::FUNCTION_GET_LOCAL_ADDRESS, TLSSocketServer::GetLocalAddress),
214         DECLARE_NAPI_FUNCTION(TLSSocketServer::FUNCTION_SET_EXTRA_OPTIONS, TLSSocketServer::SetExtraOptions),
215         DECLARE_NAPI_FUNCTION(TLSSocketServer::FUNCTION_ON, TLSSocketServer::On),
216         DECLARE_NAPI_FUNCTION(TLSSocketServer::FUNCTION_OFF, TLSSocketServer::Off),
217         DECLARE_NAPI_FUNCTION(TLSSocketServer::FUNCTION_GET_CERTIFICATE, TLSSocketServer::GetCertificate),
218         DECLARE_NAPI_FUNCTION(TLSSocketServer::FUNCTION_GET_PROTOCOL, TLSSocketServer::GetProtocol),
219     };
220     ModuleTemplate::DefineClass(env, exports, functions, INTERFACE_TLS_SOCKET_SERVER);
221 }
222 
InitProtocol(napi_env env,napi_value exports)223 void TLSSocketServerModuleExports::InitProtocol(napi_env env, napi_value exports)
224 {
225     std::initializer_list<napi_property_descriptor> properties = {
226         DECLARE_NAPI_STATIC_PROPERTY(PROTOCOL_TLSV12, NapiUtils::CreateStringUtf8(env, TlsSocket::PROTOCOL_TLS_V12)),
227         DECLARE_NAPI_STATIC_PROPERTY(PROTOCOL_TLSV13, NapiUtils::CreateStringUtf8(env, TlsSocket::PROTOCOL_TLS_V13)),
228     };
229 
230     napi_value protocol = NapiUtils::CreateObject(env);
231     NapiUtils::DefineProperties(env, protocol, properties);
232     NapiUtils::SetNamedProperty(env, exports, INTERFACE_PROTOCOL, protocol);
233 }
234 
DefineTLSSocketConnectionClass(napi_env env,napi_value exports)235 void TlsSocketServer::TLSSocketServerModuleExports::DefineTLSSocketConnectionClass(napi_env env, napi_value exports)
236 {
237     std::initializer_list<napi_property_descriptor> functions = {
238         DECLARE_NAPI_FUNCTION(TLSSocketConnection::FUNCTION_GET_CERTIFICATE, TLSSocketConnection::GetCertificate),
239         DECLARE_NAPI_FUNCTION(TLSSocketConnection::FUNCTION_GET_REMOTE_CERTIFICATE,
240                               TLSSocketConnection::GetRemoteCertificate),
241         DECLARE_NAPI_FUNCTION(TLSSocketConnection::FUNCTION_GET_SIGNATURE_ALGORITHMS,
242                               TLSSocketConnection::GetSignatureAlgorithms),
243         DECLARE_NAPI_FUNCTION(TLSSocketConnection::FUNCTION_GET_CIPHER_SUITE, TLSSocketConnection::GetCipherSuites),
244         DECLARE_NAPI_FUNCTION(TLSSocketConnection::FUNCTION_SEND, TLSSocketConnection::Send),
245         DECLARE_NAPI_FUNCTION(TLSSocketConnection::FUNCTION_CLOSE, TLSSocketConnection::Close),
246         DECLARE_NAPI_FUNCTION(TLSSocketConnection::FUNCTION_GET_REMOTE_ADDRESS, TLSSocketConnection::GetRemoteAddress),
247         DECLARE_NAPI_FUNCTION(TLSSocketConnection::FUNCTION_GET_LOCAL_ADDRESS, TLSSocketConnection::GetLocalAddress),
248         DECLARE_NAPI_FUNCTION(TLSSocketConnection::FUNCTION_ON, TLSSocketConnection::On),
249         DECLARE_NAPI_FUNCTION(TLSSocketConnection::FUNCTION_OFF, TLSSocketConnection::Off),
250     };
251     ModuleTemplate::DefineClass(env, exports, functions, INTERFACE_TLS_SOCKET_SERVER_CONNECTION);
252 }
253 
ConstructTLSSocketServerInstance(napi_env env,napi_callback_info info)254 napi_value TLSSocketServerModuleExports::ConstructTLSSocketServerInstance(napi_env env, napi_callback_info info)
255 {
256     return ModuleTemplate::NewInstanceWithSharedManager(env, info, INTERFACE_TLS_SOCKET_SERVER, Finalize);
257 }
258 
InitTLSSocketServerProperties(napi_env env,napi_value exports)259 void TLSSocketServerModuleExports::InitTLSSocketServerProperties(napi_env env, napi_value exports)
260 {
261     std::initializer_list<napi_property_descriptor> properties = {
262         DECLARE_NAPI_FUNCTION(FUNCTION_CONSTRUCTOR_TLS_SOCKET_SERVER_INSTANCE, ConstructTLSSocketServerInstance),
263     };
264     NapiUtils::DefineProperties(env, exports, properties);
265 }
266 
InitTLSSocketServerModule(napi_env env,napi_value exports)267 napi_value TLSSocketServerModuleExports::InitTLSSocketServerModule(napi_env env, napi_value exports)
268 {
269     DefineTLSSocketServerClass(env, exports);
270     DefineTLSSocketConnectionClass(env, exports);
271     InitTLSSocketServerProperties(env, exports);
272     InitProtocol(env, exports);
273     return exports;
274 }
275 
276 } // namespace TlsSocketServer
277 } // namespace NetStack
278 } // namespace OHOS
279