Cuberite
A lightweight, fast and extensible game server for Minecraft
SslContext.h
Go to the documentation of this file.
1 
2 // SslContext.h
3 
4 // Declares the cSslContext class that holds everything a single SSL context needs to function
5 
6 
7 
8 
9 
10 #pragma once
11 
12 #include "mbedtls/ssl.h"
13 #include "../ByteBuffer.h"
14 
15 
16 
17 
18 
19 // fwd:
20 class cCtrDrbgContext;
21 class cSslConfig;
22 
23 
24 
25 
26 
35 class cSslContext abstract
36 {
37 public:
39  cSslContext(void);
40 
41  virtual ~cSslContext();
42 
46  int Initialize(std::shared_ptr<const cSslConfig> a_Config);
47 
49  int Initialize(bool a_IsClient);
50 
52  bool IsValid(void) const { return m_IsValid; }
53 
59  void SetExpectedPeerName(const std::string_view a_ExpectedPeerName);
60 
67  int WritePlain(const void * a_Data, size_t a_NumBytes);
68 
75  int ReadPlain(void * a_Data, size_t a_MaxBytes);
76 
82  int Handshake(void);
83 
85  bool HasHandshaken(void) const { return m_HasHandshaken; }
86 
89  int NotifyClose(void);
90 
91 protected:
92 
94  std::shared_ptr<const cSslConfig> m_Config;
95 
97  mbedtls_ssl_context m_Ssl;
98 
100  bool m_IsValid;
101 
103  bool m_HasHandshaken;
104 
106  static int ReceiveEncrypted(void * a_This, unsigned char * a_Buffer, size_t a_NumBytes)
107  {
108  return (static_cast<cSslContext *>(a_This))->ReceiveEncrypted(a_Buffer, a_NumBytes);
109  }
110 
112  static int SendEncrypted(void * a_This, const unsigned char * a_Buffer, size_t a_NumBytes)
113  {
114  return (static_cast<cSslContext *>(a_This))->SendEncrypted(a_Buffer, a_NumBytes);
115  }
116 
118  virtual int ReceiveEncrypted(unsigned char * a_Buffer, size_t a_NumBytes) = 0;
119 
121  virtual int SendEncrypted(const unsigned char * a_Buffer, size_t a_NumBytes) = 0;
122 } ;
123 
124 
125 
126