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