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 namespace TlsSocket { 27 class TLSContext { 28 public: 29 TLSContext() = default; 30 ~TLSContext() = default; 31 static std::unique_ptr<TLSContext> CreateConfiguration(const TLSConfiguration &configuration); 32 SSL *CreateSsl(); 33 void CloseCtx(); 34 35 private: 36 static bool InitTlsContext(TLSContext *sslContext, const TLSConfiguration &configuration); 37 static bool SetCipherList(TLSContext *tlsContext, const TLSConfiguration &configuration); 38 static bool SetSignatureAlgorithms(TLSContext *tlsContext, const TLSConfiguration &configuration); 39 static void GetCiphers(TLSContext *tlsContext); 40 static void UseRemoteCipher(TLSContext *tlsContext); 41 static void SetMinAndMaxProtocol(TLSContext *tlsContext); 42 static bool SetDefaultCa(TLSContext *tlsContext, const TLSConfiguration &configuration); 43 static bool SetCaAndVerify(TLSContext *tlsContext, const TLSConfiguration &configuration); 44 static bool SetLocalCertificate(TLSContext *tlsContext, const TLSConfiguration &configuration); 45 static bool SetKeyAndCheck(TLSContext *tlsContext, const TLSConfiguration &configuration); 46 static void SetVerify(TLSContext *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