1 /*
2  * Copyright (c) 2022 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 #ifndef COMMUNICATION_NETSTACK_TLS_CONTEXT_SERVER_H
17 #define COMMUNICATION_NETSTACK_TLS_CONTEXT_SERVER_H
18 
19 #include <memory>
20 
21 #include "tls.h"
22 #include "tls_configuration.h"
23 
24 namespace OHOS {
25 namespace NetStack {
26 namespace TlsSocket {
27 class TLSContextServer {
28 public:
29     TLSContextServer() = default;
30     ~TLSContextServer() = default;
31     static std::unique_ptr<TLSContextServer> CreateConfiguration(const TLSConfiguration &configuration);
32     SSL *CreateSsl();
33     void CloseCtx();
34 
35 private:
36     static bool InitTlsContext(TLSContextServer *sslContext, const TLSConfiguration &configuration);
37     static bool SetCipherList(TLSContextServer *tlsContext, const TLSConfiguration &configuration);
38     static bool SetSignatureAlgorithms(TLSContextServer *tlsContext, const TLSConfiguration &configuration);
39     static void GetCiphers(TLSContextServer *tlsContext);
40     static void UseRemoteCipher(TLSContextServer *tlsContext);
41     static void SetMinAndMaxProtocol(TLSContextServer *tlsContext);
42     static bool SetDefaultCa(TLSContextServer *tlsContext, const TLSConfiguration &configuration);
43     static bool SetCaAndVerify(TLSContextServer *tlsContext, const TLSConfiguration &configuration);
44     static bool SetLocalCertificate(TLSContextServer *tlsContext, const TLSConfiguration &configuration);
45     static bool SetKeyAndCheck(TLSContextServer *tlsContext, const TLSConfiguration &configuration);
46     static void SetVerify(TLSContextServer *tlsContext);
47 
48 private:
49     SSL_CTX *ctx_ = nullptr;
50     EVP_PKEY *pkey_ = nullptr;
51     SSL *ctxSsl_ = nullptr;
52     TLSConfiguration tlsConfiguration_;
53     static VerifyMode verifyMode_;
54 };
55 } // namespace TlsSocket
56 } // namespace NetStack
57 } // namespace OHOS
58 #endif // COMMUNICATION_NETSTACK_TLS_CONTEXT_H
59