Repository navigation
Expand file tree
/
Copy pathOpenSSLSocket.h
More file actions
116 lines (96 loc) · 3.61 KB
/
Copy pathOpenSSLSocket.h
File metadata and controls
116 lines (96 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#ifndef _OPEN_SSL_SOCKET_H_
#define _OPEN_SSL_SOCKET_H_
#include "lgi/common/LibraryUtils.h"
#include "lgi/common/Cancel.h"
// If you get a compile error on Linux:
// sudo apt-get install libssl-dev
//
// On windows:
// https://slproweb.com/products/Win32OpenSSL.html
#include "openssl/bio.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
#define SslSocket_LogFile "LogFile"
#define SslSocket_LogFormat "LogFmt"
#define SslSocket_DebugLogging "DebugLogging"
#define SslSocket_LibInfo "LibInfo"
#define SslSocket_CertFile "CertFile"
#define SslSocket_KeyFile "KeyFile"
#define SslSocket_SslOnConnect "SslOnConnect"
class LCapabilityClient;
class SslSocket :
public LSocketI,
virtual public LDom
{
friend class OpenSSL;
struct SslSocketPriv *d;
protected:
LMutex Lock;
BIO *Bio = nullptr;
SSL *Ssl = nullptr;
LString ErrMsg;
LStream *log = nullptr;
bool DebugLogging = false;
// Local stuff
virtual void Log(const char *Str, ssize_t Bytes, SocketMsgType Type);
void HandleError(const char *file, int line, const char *Msg);
LString GetSslErr();
LStream *GetLogStream();
void DebugTrace(const char *fmt, ...);
OsSocket GetRawSocket(BIO* bio);
public:
static LString Random(int Len);
// These constants are used to call LCapabilityClient when there is a certificate issue:
LString UserRef;
constexpr static const char *CAPS_CERT_ERROR = "SslCertError";
constexpr static const char *JSON_HOST = "host";
constexpr static const char *JSON_MESSAGE = "msg";
constexpr static const char *JSON_CERT = "certId";
constexpr static const char *JSON_REF = "ref"; // the 'UserRef' if set
using TCertData = LArray<uint8_t>;
using TCertCallback = std::function<bool(const char *host, TCertData *id)>;
SslSocket( LStream *logger = nullptr,
LCapabilityClient *caps = nullptr,
bool SslOnConnect = false,
bool RawLFCheck = false,
bool banner = true);
~SslSocket();
const char *GetClass() override { return "SslSocket"; }
void SetLog(LStream *logger);
LStreamI *GetLog() override;
void SetSslOnConnect(bool b);
LCancel *GetCancel() override;
void SetCancel(LCancel *c) override;
void SetCertCallback(TCertCallback certCallback);
// Socket
OsSocket Handle(OsSocket Set = INVALID_SOCKET) override;
bool IsOpen() override;
int Open(const char *HostAddr, int Port) override;
int Close() override;
void OnError(int ErrorCode, const char *ErrorDescription) override;
void OnInformation(const char *Str) override;
int GetTimeout() override;
void SetTimeout(int ms) override;
bool GetLocalIp(char *IpAddr) override;
bool GetRemoteIp(char *IpAddr) override;
// Server
bool SetCert(LString certFile, LString keyFile);
bool Listen(int Port = 0) override;
bool CanAccept(int TimeoutMs = 0) override;
bool Accept(LSocketI *c) override;
ssize_t Write(const void *Data, ssize_t Len, int Flags = 0) override;
ssize_t Read(void *Data, ssize_t Len, int Flags = 0) override;
void OnWrite(const char *Data, ssize_t Len) override;
void OnRead(char *Data, ssize_t Len) override;
bool IsReadable(int TimeoutMs = 0) override;
bool IsWritable(int TimeoutMs = 0) override;
bool IsBlocking() override;
void IsBlocking(bool block) override;
bool SetVariant(const char *Name, LVariant &Val, const char *Arr = nullptr) override;
bool GetVariant(const char *Name, LVariant &Val, const char *Arr = nullptr) override;
LStreamI *Clone() override;
const char *GetErrorString() override;
};
extern bool StartSSL(LString &ErrorMsg, SslSocket *Sock = nullptr);
extern void EndSSL();
#endif