-
Notifications
You must be signed in to change notification settings - Fork 203
/
openssldl.cpp
254 lines (242 loc) · 9 KB
/
openssldl.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "openssldl.h"
#ifdef __WIN32__
#include <windows.h>
#define HANDLE_DLL HMODULE
#define GETPROC GetProcAddress
#else
#include <dlfcn.h>
#define HANDLE_DLL void *
#define GETPROC dlsym
#endif
//----------------------------------------------------------------------------
static HANDLE_DLL ssl_handle1 = 0; // Aponta para DLL aberta - SSL
static HANDLE_DLL ssl_handle2 = 0; // Aponta para DLL aberta - util
//----------------------------------------------------------------------------
TSslGetError SslGetError = 0;
TSslLibraryInit SslLibraryInit = 0;
TOpensslInitSsl OpensslInitSsl = 0;
TSslLoadErrorStrings SslLoadErrorStrings = 0;
TSslCtxNew SslCtxNew = 0;
TSslCtxFree SslCtxFree = 0;
TSslSetFd SslSetFd = 0;
TSslMethodV2 SslMethodV2 = 0;
TSslMethodV3 SslMethodV3 = 0;
TSslMethodTLSV1 SslMethodTLSV1 = 0;
TSslMethodV23 SslMethodV23 = 0;
TtlsMethod TlsMethod = 0;
TSslNew SslNew = 0;
TSslFree SslFree = 0;
TSslAccept SslAccept = 0;
TSslConnect SslConnect = 0;
TSslShutdown SslShutdown = 0;
TSslRead SslRead = 0;
TSslPeek SslPeek = 0;
TSslWrite SslWrite = 0;
TSslPending SslPending = 0;
TSslPrivateKeyFile SslPrivateKeyFile = 0;
TSslCertificateFile SslCertificateFile = 0;
TOPENSSLaddallalgorithms OPENSSLaddallalgorithms = 0;
TSslGetPeerCertificate SslGetPeerCertificate = 0;
TSslX509free SslX509free = 0;
TSslX509d2i SslX509d2i = 0;
TSslX509i2d SslX509i2d = 0;
TSslSetConnectState SslSetConnectState = 0; //dosgo
TSslDoHandshake SslDoHandshake = 0; //dosgo
TSsLGetSession SsLGetSession = 0; //dosgo
TSsLGet1Session SsLGet1Session = 0; //dosgo
TSsLSetSession SsLSetSession = 0; //dosgo
TSslCtxCtrl SslCtxCtrl = 0; //dosgo
//----------------------------------------------------------------------------
void FechaSSL()
{
if (ssl_handle1 == 0)
return;
#ifdef __WIN32__
FreeLibrary(ssl_handle1);
FreeLibrary(ssl_handle2);
#else
dlclose(ssl_handle1);
dlclose(ssl_handle2);
#endif
ssl_handle1 = ssl_handle2 = 0;
}
//----------------------------------------------------------------------------
const char *AbreSSL()
{
if (ssl_handle1)
return 0;
#ifdef __WIN32__
ssl_handle1 = LoadLibrary("ssleay32.dll");
if (ssl_handle1 == 0)
ssl_handle1 = LoadLibrary("libssl32.dll");
if (ssl_handle1 == 0)
ssl_handle1 = LoadLibrary("libssl-1_1.dll");
if (ssl_handle1 == 0)
return " ssleay32.dll/libssl32.dll/libssl-1_1.dll";
ssl_handle2 = LoadLibrary("libeay32.dll");
if (ssl_handle2 == 0)
ssl_handle2 = LoadLibrary("libcrypto.dll");
if (ssl_handle2 == 0)
ssl_handle2 = LoadLibrary("libcrypto-1_1.dll");
if (ssl_handle2 == 0)
{
FreeLibrary(ssl_handle1);
ssl_handle1 = 0;
return "Erro ao carregar libeay32.dll/libcrypto-1_1.dll";
}
#else
ssl_handle1 = dlopen("libssl.so", RTLD_LAZY);
if (ssl_handle1 == 0)
ssl_handle1 = dlopen("libssl.so.1.0.0", RTLD_LAZY);
if (ssl_handle1 == 0)
ssl_handle1 = dlopen("libssl.so.1.1", RTLD_LAZY);
if (ssl_handle1 == 0)
return "Erro ao carregar libssl.so";
ssl_handle2 = dlopen("libcrypto.so", RTLD_LAZY);
if (ssl_handle2 == 0)
ssl_handle2 = dlopen("libcrypto.so.1.0.0", RTLD_LAZY);
if (ssl_handle2 == 0)
ssl_handle2 = dlopen("libcrypto.so.1.1", RTLD_LAZY);
if (ssl_handle2 == 0)
{
dlclose(ssl_handle1);
ssl_handle1 = 0;
return "Erro ao carregar libcrypto.so";
}
#endif
SslGetError = (TSslGetError)GETPROC(ssl_handle1, "SSL_get_error");
SslLibraryInit = (TSslLibraryInit)GETPROC(ssl_handle1, "SSL_library_init");
OpensslInitSsl = (TOpensslInitSsl)GETPROC(ssl_handle1, "OPENSSL_init_ssl"); //openssl1.1
SslLoadErrorStrings = (TSslLoadErrorStrings)GETPROC(ssl_handle1, "SSL_load_error_strings");
SslCtxNew = (TSslCtxNew)GETPROC(ssl_handle1, "SSL_CTX_new");
SslCtxFree = (TSslCtxFree)GETPROC(ssl_handle1, "SSL_CTX_free");
SslSetFd = (TSslSetFd)GETPROC(ssl_handle1, "SSL_set_fd");
SslMethodV2 = (TSslMethodV2)GETPROC(ssl_handle1, "SSLv2_method");
SslMethodV3 = (TSslMethodV3)GETPROC(ssl_handle1, "SSLv3_method");
SslMethodTLSV1 = (TSslMethodTLSV1)GETPROC(ssl_handle1, "TLSv1_method");
SslMethodV23 = (TSslMethodV23)GETPROC(ssl_handle1, "SSLv23_method");
TlsMethod = (TtlsMethod)GETPROC(ssl_handle1, "TLS_method"); //openssl1.1
SslNew = (TSslNew)GETPROC(ssl_handle1, "SSL_new");
SslFree = (TSslFree)GETPROC(ssl_handle1, "SSL_free");
SslAccept = (TSslAccept)GETPROC(ssl_handle1, "SSL_accept");
SslConnect = (TSslConnect)GETPROC(ssl_handle1, "SSL_connect");
SslShutdown = (TSslShutdown)GETPROC(ssl_handle1, "SSL_shutdown");
SslRead = (TSslRead)GETPROC(ssl_handle1, "SSL_read");
SslPeek = (TSslPeek)GETPROC(ssl_handle1, "SSL_peek");
SslWrite = (TSslWrite)GETPROC(ssl_handle1, "SSL_write");
SslPending = (TSslPending)GETPROC(ssl_handle1, "SSL_pending");
SslPrivateKeyFile = (TSslPrivateKeyFile)GETPROC(ssl_handle1, "SSL_CTX_use_PrivateKey_file");
SslCertificateFile = (TSslCertificateFile)GETPROC(ssl_handle1, "SSL_CTX_use_certificate_file");
OPENSSLaddallalgorithms = (TOPENSSLaddallalgorithms)GETPROC(ssl_handle2, "OPENSSL_add_all_algorithms_noconf");
SslGetPeerCertificate = (TSslGetPeerCertificate)GETPROC(ssl_handle1, "SSL_get_peer_certificate");
SslX509free = (TSslX509free)GETPROC(ssl_handle2, "X509_free");
SslX509d2i = (TSslX509d2i)GETPROC(ssl_handle2, "d2i_X509");
SslX509i2d = (TSslX509i2d)GETPROC(ssl_handle2, "i2d_X509");
SslSetConnectState = (TSslSetConnectState)GETPROC(ssl_handle1, "SSL_set_connect_state"); //dosgo
SslDoHandshake = (TSslDoHandshake)GETPROC(ssl_handle1, "SSL_do_handshake"); //dosgo
SsLGetSession = (TSsLGetSession)GETPROC(ssl_handle1, "SSL_get_session"); //dosgo
SsLGet1Session = (TSsLGet1Session)GETPROC(ssl_handle1, "SSL_get1_session"); //dosgo
SsLSetSession = (TSsLSetSession)GETPROC(ssl_handle1, "SSL_set_session"); //dosgo
SslCtxCtrl = (TSslCtxCtrl)GETPROC(ssl_handle1, "SSL_CTX_ctrl"); //dosgo
const char *erro = 0;
if (!SslGetError)
erro = "SSL_get_error";
if (!SslLibraryInit && !OpensslInitSsl) //openssl 1.0 or 1.1+
erro = "SSL_library_init/OpensslInitSsl";
if (!SslLoadErrorStrings && !OpensslInitSsl) //openssl 1.0 or 1.1+
erro = "SSL_load_error_strings";
if (!SslCtxNew)
erro = "SSL_CTX_new";
if (!SslCtxFree)
erro = "SSL_CTX_free";
if (!SslSetFd)
erro = "SSL_set_fd";
if (!SslMethodV23 && !TlsMethod) //openssl 1.0 or 1.1+
erro = "SSLv23_method/TlsMethod";
if (!SslMethodTLSV1)
erro = "TLSv1_method";
if (!SslNew)
erro = "SSL_new";
if (!SslFree)
erro = "SSL_free";
if (!SslAccept)
erro = "SSL_accept";
if (!SslConnect)
erro = "SSL_connect";
if (!SslShutdown)
erro = "SSL_shutdown";
if (!SslRead)
erro = "SSL_read";
if (!SslPeek)
erro = "SSL_peek";
if (!SslWrite)
erro = "SSL_write";
if (!SslPending)
erro = "SSL_pending";
if (!SslPrivateKeyFile)
erro = "SSL_CTX_use_PrivateKey_file";
if (!SslCertificateFile)
erro = "SSL_CTX_use_certificate_file";
//if (!OPENSSLaddallalgorithms) erro = "OPENSSL_add_all_algorithms";
if (!SslGetPeerCertificate)
erro = "SSL_get_peer_certificate";
if (!SslX509free)
erro = "X509_free";
if (!SslX509d2i)
erro = "d2i_X509";
if (!SslX509i2d)
erro = "i2d_X509";
if (!SslSetConnectState)
erro = "SSL_set_connect_state"; //dosgo
if (!SslDoHandshake)
erro = "SSL_do_handshake"; //dosgo
if (!SsLGetSession)
erro = "SSL_get_session"; //dosgo
if (!SsLSetSession)
erro = "SSL_set_session"; //dosgo
if (!SslCtxCtrl)
erro = "SSL_CTX_ctrl"; //dosgo
if (!SsLGet1Session)
erro = "SSL_get1_session"; //dosgo
if (erro)
{
FechaSSL();
return erro;
}
//if (SslLibraryInit())
//{
// FechaSSL();
// return "Erro ao inicializar OpenSSL (SSL_library_init)";
//}
//openssl 1.0
if (SslLibraryInit)
{
SslLibraryInit();
}
//openssl 1.0
if (SslLoadErrorStrings)
{
SslLoadErrorStrings();
}
//openssl 1.0
if (OPENSSLaddallalgorithms)
{
OPENSSLaddallalgorithms();
}
//openssl 1.1
if (OpensslInitSsl)
{
OpensslInitSsl(0, NULL); //==SslLibraryInit();
}
//openssl 1.1
if (OpensslInitSsl)
{
OpensslInitSsl(0x00200000L | 0x00000002L, NULL); //==SslLoadErrorStrings()
}
//RAND_screen();
return 0;
}
//----------------------------------------------------------------------------