source: trunk/Packages/synapse/source/lib/ssl_openssl_lib.pas

Last change on this file was 2, checked in by chronos, 12 years ago
  • Přidáno: Základní kostra projektu.
  • Přidáno: Knihovna synapse.
File size: 78.3 KB
Line 
1{==============================================================================|
2| Project : Ararat Synapse | 003.007.000 |
3|==============================================================================|
4| Content: SSL support by OpenSSL |
5|==============================================================================|
6| Copyright (c)1999-2012, Lukas Gebauer |
7| All rights reserved. |
8| |
9| Redistribution and use in source and binary forms, with or without |
10| modification, are permitted provided that the following conditions are met: |
11| |
12| Redistributions of source code must retain the above copyright notice, this |
13| list of conditions and the following disclaimer. |
14| |
15| Redistributions in binary form must reproduce the above copyright notice, |
16| this list of conditions and the following disclaimer in the documentation |
17| and/or other materials provided with the distribution. |
18| |
19| Neither the name of Lukas Gebauer nor the names of its contributors may |
20| be used to endorse or promote products derived from this software without |
21| specific prior written permission. |
22| |
23| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
24| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
25| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
26| ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
27| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
28| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
29| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
30| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
31| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
32| OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
33| DAMAGE. |
34|==============================================================================|
35| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
36| Portions created by Lukas Gebauer are Copyright (c)2002-2012. |
37| Portions created by Petr Fejfar are Copyright (c)2011-2012. |
38| All Rights Reserved. |
39|==============================================================================|
40| Contributor(s): |
41|==============================================================================|
42| History: see HISTORY.HTM from distribution package |
43| (Found at URL: http://www.ararat.cz/synapse/) |
44|==============================================================================}
45
46{
47Special thanks to Gregor Ibic <gregor.ibic@intelicom.si>
48 (Intelicom d.o.o., http://www.intelicom.si)
49 for good inspiration about begin with SSL programming.
50}
51
52{$IFDEF FPC}
53 {$MODE DELPHI}
54{$ENDIF}
55{$H+}
56{$IFDEF VER125}
57 {$DEFINE BCB}
58{$ENDIF}
59{$IFDEF BCB}
60 {$ObjExportAll On}
61 (*$HPPEMIT 'namespace ssl_openssl_lib { using System::Shortint; }' *)
62{$ENDIF}
63
64//old Delphi does not have MSWINDOWS define.
65{$IFDEF WIN32}
66 {$IFNDEF MSWINDOWS}
67 {$DEFINE MSWINDOWS}
68 {$ENDIF}
69{$ENDIF}
70
71{:@abstract(OpenSSL support)
72
73This unit is Pascal interface to OpenSSL library (used by @link(ssl_openssl) unit).
74OpenSSL is loaded dynamicly on-demand. If this library is not found in system,
75requested OpenSSL function just return errorcode.
76}
77unit ssl_openssl_lib;
78
79interface
80
81uses
82{$IFDEF CIL}
83 System.Runtime.InteropServices,
84 System.Text,
85{$ENDIF}
86 Classes,
87 synafpc,
88{$IFNDEF MSWINDOWS}
89 {$IFDEF FPC}
90 BaseUnix, SysUtils;
91 {$ELSE}
92 Libc, SysUtils;
93 {$ENDIF}
94{$ELSE}
95 Windows;
96{$ENDIF}
97
98
99{$IFDEF CIL}
100const
101 {$IFDEF LINUX}
102 DLLSSLName = 'libssl.so';
103 DLLUtilName = 'libcrypto.so';
104 {$ELSE}
105 DLLSSLName = 'ssleay32.dll';
106 DLLUtilName = 'libeay32.dll';
107 {$ENDIF}
108{$ELSE}
109var
110 {$IFNDEF MSWINDOWS}
111 {$IFDEF DARWIN}
112 DLLSSLName: string = 'libssl.dylib';
113 DLLUtilName: string = 'libcrypto.dylib';
114 {$ELSE}
115 DLLSSLName: string = 'libssl.so';
116 DLLUtilName: string = 'libcrypto.so';
117 {$ENDIF}
118 {$ELSE}
119 DLLSSLName: string = 'ssleay32.dll';
120 DLLSSLName2: string = 'libssl32.dll';
121 DLLUtilName: string = 'libeay32.dll';
122 {$ENDIF}
123{$ENDIF}
124
125type
126{$IFDEF CIL}
127 SslPtr = IntPtr;
128{$ELSE}
129 SslPtr = Pointer;
130{$ENDIF}
131 PSslPtr = ^SslPtr;
132 PSSL_CTX = SslPtr;
133 PSSL = SslPtr;
134 PSSL_METHOD = SslPtr;
135 PX509 = SslPtr;
136 PX509_NAME = SslPtr;
137 PEVP_MD = SslPtr;
138 PInteger = ^Integer;
139 PBIO_METHOD = SslPtr;
140 PBIO = SslPtr;
141 EVP_PKEY = SslPtr;
142 PRSA = SslPtr;
143 PASN1_UTCTIME = SslPtr;
144 PASN1_INTEGER = SslPtr;
145 PPasswdCb = SslPtr;
146 PFunction = procedure;
147 PSTACK = SslPtr; {pf}
148 TSkPopFreeFunc = procedure(p:SslPtr); cdecl; {pf}
149 TX509Free = procedure(x: PX509); cdecl; {pf}
150
151 DES_cblock = array[0..7] of Byte;
152 PDES_cblock = ^DES_cblock;
153 des_ks_struct = packed record
154 ks: DES_cblock;
155 weak_key: Integer;
156 end;
157 des_key_schedule = array[1..16] of des_ks_struct;
158
159const
160 EVP_MAX_MD_SIZE = 16 + 20;
161
162 SSL_ERROR_NONE = 0;
163 SSL_ERROR_SSL = 1;
164 SSL_ERROR_WANT_READ = 2;
165 SSL_ERROR_WANT_WRITE = 3;
166 SSL_ERROR_WANT_X509_LOOKUP = 4;
167 SSL_ERROR_SYSCALL = 5; //look at error stack/return value/errno
168 SSL_ERROR_ZERO_RETURN = 6;
169 SSL_ERROR_WANT_CONNECT = 7;
170 SSL_ERROR_WANT_ACCEPT = 8;
171
172 SSL_OP_NO_SSLv2 = $01000000;
173 SSL_OP_NO_SSLv3 = $02000000;
174 SSL_OP_NO_TLSv1 = $04000000;
175 SSL_OP_ALL = $000FFFFF;
176 SSL_VERIFY_NONE = $00;
177 SSL_VERIFY_PEER = $01;
178
179 OPENSSL_DES_DECRYPT = 0;
180 OPENSSL_DES_ENCRYPT = 1;
181
182 X509_V_OK = 0;
183 X509_V_ILLEGAL = 1;
184 X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT = 2;
185 X509_V_ERR_UNABLE_TO_GET_CRL = 3;
186 X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE = 4;
187 X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE = 5;
188 X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY = 6;
189 X509_V_ERR_CERT_SIGNATURE_FAILURE = 7;
190 X509_V_ERR_CRL_SIGNATURE_FAILURE = 8;
191 X509_V_ERR_CERT_NOT_YET_VALID = 9;
192 X509_V_ERR_CERT_HAS_EXPIRED = 10;
193 X509_V_ERR_CRL_NOT_YET_VALID = 11;
194 X509_V_ERR_CRL_HAS_EXPIRED = 12;
195 X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD = 13;
196 X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD = 14;
197 X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD = 15;
198 X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD = 16;
199 X509_V_ERR_OUT_OF_MEM = 17;
200 X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT = 18;
201 X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN = 19;
202 X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY = 20;
203 X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE = 21;
204 X509_V_ERR_CERT_CHAIN_TOO_LONG = 22;
205 X509_V_ERR_CERT_REVOKED = 23;
206 X509_V_ERR_INVALID_CA = 24;
207 X509_V_ERR_PATH_LENGTH_EXCEEDED = 25;
208 X509_V_ERR_INVALID_PURPOSE = 26;
209 X509_V_ERR_CERT_UNTRUSTED = 27;
210 X509_V_ERR_CERT_REJECTED = 28;
211 //These are 'informational' when looking for issuer cert
212 X509_V_ERR_SUBJECT_ISSUER_MISMATCH = 29;
213 X509_V_ERR_AKID_SKID_MISMATCH = 30;
214 X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH = 31;
215 X509_V_ERR_KEYUSAGE_NO_CERTSIGN = 32;
216 X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER = 33;
217 X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION = 34;
218 //The application is not happy
219 X509_V_ERR_APPLICATION_VERIFICATION = 50;
220
221 SSL_FILETYPE_ASN1 = 2;
222 SSL_FILETYPE_PEM = 1;
223 EVP_PKEY_RSA = 6;
224
225 SSL_CTRL_SET_TLSEXT_HOSTNAME = 55;
226 TLSEXT_NAMETYPE_host_name = 0;
227
228var
229 SSLLibHandle: TLibHandle = 0;
230 SSLUtilHandle: TLibHandle = 0;
231 SSLLibFile: string = '';
232 SSLUtilFile: string = '';
233
234{$IFDEF CIL}
235 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
236 SetLastError = False, CallingConvention= CallingConvention.cdecl,
237 EntryPoint = 'SSL_get_error')]
238 function SslGetError(s: PSSL; ret_code: Integer): Integer; external;
239
240 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
241 SetLastError = False, CallingConvention= CallingConvention.cdecl,
242 EntryPoint = 'SSL_library_init')]
243 function SslLibraryInit: Integer; external;
244
245 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
246 SetLastError = False, CallingConvention= CallingConvention.cdecl,
247 EntryPoint = 'SSL_load_error_strings')]
248 procedure SslLoadErrorStrings; external;
249
250 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
251 SetLastError = False, CallingConvention= CallingConvention.cdecl,
252 EntryPoint = 'SSL_CTX_set_cipher_list')]
253 function SslCtxSetCipherList(arg0: PSSL_CTX; var str: String): Integer; external;
254
255 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
256 SetLastError = False, CallingConvention= CallingConvention.cdecl,
257 EntryPoint = 'SSL_CTX_new')]
258 function SslCtxNew(meth: PSSL_METHOD):PSSL_CTX; external;
259
260 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
261 SetLastError = False, CallingConvention= CallingConvention.cdecl,
262 EntryPoint = 'SSL_CTX_free')]
263 procedure SslCtxFree (arg0: PSSL_CTX); external;
264
265 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
266 SetLastError = False, CallingConvention= CallingConvention.cdecl,
267 EntryPoint = 'SSL_set_fd')]
268 function SslSetFd(s: PSSL; fd: Integer):Integer; external;
269
270 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
271 SetLastError = False, CallingConvention= CallingConvention.cdecl,
272 EntryPoint = 'SSLv2_method')]
273 function SslMethodV2 : PSSL_METHOD; external;
274
275 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
276 SetLastError = False, CallingConvention= CallingConvention.cdecl,
277 EntryPoint = 'SSLv3_method')]
278 function SslMethodV3 : PSSL_METHOD; external;
279
280 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
281 SetLastError = False, CallingConvention= CallingConvention.cdecl,
282 EntryPoint = 'TLSv1_method')]
283 function SslMethodTLSV1:PSSL_METHOD; external;
284
285 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
286 SetLastError = False, CallingConvention= CallingConvention.cdecl,
287 EntryPoint = 'SSLv23_method')]
288 function SslMethodV23 : PSSL_METHOD; external;
289
290 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
291 SetLastError = False, CallingConvention= CallingConvention.cdecl,
292 EntryPoint = 'SSL_CTX_use_PrivateKey')]
293 function SslCtxUsePrivateKey(ctx: PSSL_CTX; pkey: SslPtr):Integer; external;
294
295 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
296 SetLastError = False, CallingConvention= CallingConvention.cdecl,
297 EntryPoint = 'SSL_CTX_use_PrivateKey_ASN1')]
298 function SslCtxUsePrivateKeyASN1(pk: integer; ctx: PSSL_CTX; d: String; len: integer):Integer; external;
299
300 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
301 SetLastError = False, CallingConvention= CallingConvention.cdecl,
302 EntryPoint = 'SSL_CTX_use_RSAPrivateKey_file')]
303 function SslCtxUsePrivateKeyFile(ctx: PSSL_CTX; const _file: String; _type: Integer):Integer; external;
304
305 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
306 SetLastError = False, CallingConvention= CallingConvention.cdecl,
307 EntryPoint = 'SSL_CTX_use_certificate')]
308 function SslCtxUseCertificate(ctx: PSSL_CTX; x: SslPtr):Integer; external;
309
310 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
311 SetLastError = False, CallingConvention= CallingConvention.cdecl,
312 EntryPoint = 'SSL_CTX_use_certificate_ASN1')]
313 function SslCtxUseCertificateASN1(ctx: PSSL_CTX; len: integer; d: String):Integer; external;
314
315 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
316 SetLastError = False, CallingConvention= CallingConvention.cdecl,
317 EntryPoint = 'SSL_CTX_use_certificate_file')]
318 function SslCtxUseCertificateFile(ctx: PSSL_CTX; const _file: String; _type: Integer):Integer;external;
319
320 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
321 SetLastError = False, CallingConvention= CallingConvention.cdecl,
322 EntryPoint = 'SSL_CTX_use_certificate_chain_file')]
323 function SslCtxUseCertificateChainFile(ctx: PSSL_CTX; const _file: String):Integer;external;
324
325 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
326 SetLastError = False, CallingConvention= CallingConvention.cdecl,
327 EntryPoint = 'SSL_CTX_check_private_key')]
328 function SslCtxCheckPrivateKeyFile(ctx: PSSL_CTX):Integer; external;
329
330 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
331 SetLastError = False, CallingConvention= CallingConvention.cdecl,
332 EntryPoint = 'SSL_CTX_set_default_passwd_cb')]
333 procedure SslCtxSetDefaultPasswdCb(ctx: PSSL_CTX; cb: PPasswdCb); external;
334
335 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
336 SetLastError = False, CallingConvention= CallingConvention.cdecl,
337 EntryPoint = 'SSL_CTX_set_default_passwd_cb_userdata')]
338 procedure SslCtxSetDefaultPasswdCbUserdata(ctx: PSSL_CTX; u: IntPtr); external;
339
340 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
341 SetLastError = False, CallingConvention= CallingConvention.cdecl,
342 EntryPoint = 'SSL_CTX_load_verify_locations')]
343 function SslCtxLoadVerifyLocations(ctx: PSSL_CTX; CAfile: string; CApath: String):Integer; external;
344
345 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
346 SetLastError = False, CallingConvention= CallingConvention.cdecl,
347 EntryPoint = 'SSL_CTX_ctrl')]
348 function SslCtxCtrl(ctx: PSSL_CTX; cmd: integer; larg: integer; parg: IntPtr): integer; external;
349
350 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
351 SetLastError = False, CallingConvention= CallingConvention.cdecl,
352 EntryPoint = 'SSL_new')]
353 function SslNew(ctx: PSSL_CTX):PSSL; external;
354
355 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
356 SetLastError = False, CallingConvention= CallingConvention.cdecl,
357 EntryPoint = 'SSL_free')]
358 procedure SslFree(ssl: PSSL); external;
359
360 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
361 SetLastError = False, CallingConvention= CallingConvention.cdecl,
362 EntryPoint = 'SSL_accept')]
363 function SslAccept(ssl: PSSL):Integer; external;
364
365 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
366 SetLastError = False, CallingConvention= CallingConvention.cdecl,
367 EntryPoint = 'SSL_connect')]
368 function SslConnect(ssl: PSSL):Integer; external;
369
370 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
371 SetLastError = False, CallingConvention= CallingConvention.cdecl,
372 EntryPoint = 'SSL_shutdown')]
373 function SslShutdown(s: PSSL):Integer; external;
374
375 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
376 SetLastError = False, CallingConvention= CallingConvention.cdecl,
377 EntryPoint = 'SSL_read')]
378 function SslRead(ssl: PSSL; buf: StringBuilder; num: Integer):Integer; external;
379
380 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
381 SetLastError = False, CallingConvention= CallingConvention.cdecl,
382 EntryPoint = 'SSL_peek')]
383 function SslPeek(ssl: PSSL; buf: StringBuilder; num: Integer):Integer; external;
384
385 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
386 SetLastError = False, CallingConvention= CallingConvention.cdecl,
387 EntryPoint = 'SSL_write')]
388 function SslWrite(ssl: PSSL; buf: String; num: Integer):Integer; external;
389
390 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
391 SetLastError = False, CallingConvention= CallingConvention.cdecl,
392 EntryPoint = 'SSL_pending')]
393 function SslPending(ssl: PSSL):Integer; external;
394
395 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
396 SetLastError = False, CallingConvention= CallingConvention.cdecl,
397 EntryPoint = 'SSL_get_version')]
398 function SslGetVersion(ssl: PSSL):String; external;
399
400 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
401 SetLastError = False, CallingConvention= CallingConvention.cdecl,
402 EntryPoint = 'SSL_get_peer_certificate')]
403 function SslGetPeerCertificate(s: PSSL):PX509; external;
404
405 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
406 SetLastError = False, CallingConvention= CallingConvention.cdecl,
407 EntryPoint = 'SSL_CTX_set_verify')]
408 procedure SslCtxSetVerify(ctx: PSSL_CTX; mode: Integer; arg2: PFunction); external;
409
410 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
411 SetLastError = False, CallingConvention= CallingConvention.cdecl,
412 EntryPoint = 'SSL_get_current_cipher')]
413 function SSLGetCurrentCipher(s: PSSL): SslPtr; external;
414
415 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
416 SetLastError = False, CallingConvention= CallingConvention.cdecl,
417 EntryPoint = 'SSL_CIPHER_get_name')]
418 function SSLCipherGetName(c: SslPtr):String; external;
419
420 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
421 SetLastError = False, CallingConvention= CallingConvention.cdecl,
422 EntryPoint = 'SSL_CIPHER_get_bits')]
423 function SSLCipherGetBits(c: SslPtr; var alg_bits: Integer):Integer; external;
424
425 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
426 SetLastError = False, CallingConvention= CallingConvention.cdecl,
427 EntryPoint = 'SSL_get_verify_result')]
428 function SSLGetVerifyResult(ssl: PSSL):Integer;external;
429
430 [DllImport(DLLSSLName, CharSet = CharSet.Ansi,
431 SetLastError = False, CallingConvention= CallingConvention.cdecl,
432 EntryPoint = 'SSL_ctrl')]
433 function SslCtrl(ssl: PSSL; cmd: integer; larg: integer; parg: IntPtr): integer; external;
434
435 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
436 SetLastError = False, CallingConvention= CallingConvention.cdecl,
437 EntryPoint = 'X509_new')]
438 function X509New: PX509; external;
439
440 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
441 SetLastError = False, CallingConvention= CallingConvention.cdecl,
442 EntryPoint = 'X509_free')]
443 procedure X509Free(x: PX509); external;
444
445 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
446 SetLastError = False, CallingConvention= CallingConvention.cdecl,
447 EntryPoint = 'X509_NAME_oneline')]
448 function X509NameOneline(a: PX509_NAME; buf: StringBuilder; size: Integer): String; external;
449
450 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
451 SetLastError = False, CallingConvention= CallingConvention.cdecl,
452 EntryPoint = 'X509_get_subject_name')]
453 function X509GetSubjectName(a: PX509):PX509_NAME; external;
454
455 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
456 SetLastError = False, CallingConvention= CallingConvention.cdecl,
457 EntryPoint = 'X509_get_issuer_name')]
458 function X509GetIssuerName(a: PX509):PX509_NAME; external;
459
460 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
461 SetLastError = False, CallingConvention= CallingConvention.cdecl,
462 EntryPoint = 'X509_NAME_hash')]
463 function X509NameHash(x: PX509_NAME):Cardinal; external;
464
465 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
466 SetLastError = False, CallingConvention= CallingConvention.cdecl,
467 EntryPoint = 'X509_digest')]
468 function X509Digest (data: PX509; _type: PEVP_MD; md: StringBuilder; var len: Integer):Integer; external;
469
470 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
471 SetLastError = False, CallingConvention= CallingConvention.cdecl,
472 EntryPoint = 'X509_set_version')]
473 function X509SetVersion(x: PX509; version: integer): integer; external;
474
475 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
476 SetLastError = False, CallingConvention= CallingConvention.cdecl,
477 EntryPoint = 'X509_set_pubkey')]
478 function X509SetPubkey(x: PX509; pkey: EVP_PKEY): integer; external;
479
480 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
481 SetLastError = False, CallingConvention= CallingConvention.cdecl,
482 EntryPoint = 'X509_set_issuer_name')]
483 function X509SetIssuerName(x: PX509; name: PX509_NAME): integer; external;
484
485 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
486 SetLastError = False, CallingConvention= CallingConvention.cdecl,
487 EntryPoint = 'X509_NAME_add_entry_by_txt')]
488 function X509NameAddEntryByTxt(name: PX509_NAME; field: string; _type: integer;
489 bytes: string; len, loc, _set: integer): integer; external;
490
491 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
492 SetLastError = False, CallingConvention= CallingConvention.cdecl,
493 EntryPoint = 'X509_sign')]
494 function X509Sign(x: PX509; pkey: EVP_PKEY; const md: PEVP_MD): integer; external;
495
496 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
497 SetLastError = False, CallingConvention= CallingConvention.cdecl,
498 EntryPoint = 'X509_print')]
499 function X509print(b: PBIO; a: PX509): integer; external;
500
501 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
502 SetLastError = False, CallingConvention= CallingConvention.cdecl,
503 EntryPoint = 'X509_gmtime_adj')]
504 function X509GmtimeAdj(s: PASN1_UTCTIME; adj: integer): PASN1_UTCTIME; external;
505
506 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
507 SetLastError = False, CallingConvention= CallingConvention.cdecl,
508 EntryPoint = 'X509_set_notBefore')]
509 function X509SetNotBefore(x: PX509; tm: PASN1_UTCTIME): integer; external;
510
511 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
512 SetLastError = False, CallingConvention= CallingConvention.cdecl,
513 EntryPoint = 'X509_set_notAfter')]
514 function X509SetNotAfter(x: PX509; tm: PASN1_UTCTIME): integer; external;
515
516 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
517 SetLastError = False, CallingConvention= CallingConvention.cdecl,
518 EntryPoint = 'X509_get_serialNumber')]
519 function X509GetSerialNumber(x: PX509): PASN1_INTEGER; external;
520
521 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
522 SetLastError = False, CallingConvention= CallingConvention.cdecl,
523 EntryPoint = 'EVP_PKEY_new')]
524 function EvpPkeyNew: EVP_PKEY; external;
525
526 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
527 SetLastError = False, CallingConvention= CallingConvention.cdecl,
528 EntryPoint = 'EVP_PKEY_free')]
529 procedure EvpPkeyFree(pk: EVP_PKEY); external;
530
531 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
532 SetLastError = False, CallingConvention= CallingConvention.cdecl,
533 EntryPoint = 'EVP_PKEY_assign')]
534 function EvpPkeyAssign(pkey: EVP_PKEY; _type: integer; key: Prsa): integer; external;
535
536 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
537 SetLastError = False, CallingConvention= CallingConvention.cdecl,
538 EntryPoint = 'EVP_get_digestbyname')]
539 function EvpGetDigestByName(Name: String): PEVP_MD; external;
540
541 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
542 SetLastError = False, CallingConvention= CallingConvention.cdecl,
543 EntryPoint = 'EVP_cleanup')]
544 procedure EVPcleanup; external;
545
546 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
547 SetLastError = False, CallingConvention= CallingConvention.cdecl,
548 EntryPoint = 'SSLeay_version')]
549 function SSLeayversion(t: integer): String; external;
550
551 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
552 SetLastError = False, CallingConvention= CallingConvention.cdecl,
553 EntryPoint = 'ERR_error_string_n')]
554 procedure ErrErrorString(e: integer; buf: StringBuilder; len: integer); external;
555
556 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
557 SetLastError = False, CallingConvention= CallingConvention.cdecl,
558 EntryPoint = 'ERR_get_error')]
559 function ErrGetError: integer; external;
560
561 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
562 SetLastError = False, CallingConvention= CallingConvention.cdecl,
563 EntryPoint = 'ERR_clear_error')]
564 procedure ErrClearError; external;
565
566 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
567 SetLastError = False, CallingConvention= CallingConvention.cdecl,
568 EntryPoint = 'ERR_free_strings')]
569 procedure ErrFreeStrings; external;
570
571 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
572 SetLastError = False, CallingConvention= CallingConvention.cdecl,
573 EntryPoint = 'ERR_remove_state')]
574 procedure ErrRemoveState(pid: integer); external;
575
576 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
577 SetLastError = False, CallingConvention= CallingConvention.cdecl,
578 EntryPoint = 'OPENSSL_add_all_algorithms_noconf')]
579 procedure OPENSSLaddallalgorithms; external;
580
581 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
582 SetLastError = False, CallingConvention= CallingConvention.cdecl,
583 EntryPoint = 'CRYPTO_cleanup_all_ex_data')]
584 procedure CRYPTOcleanupAllExData; external;
585
586 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
587 SetLastError = False, CallingConvention= CallingConvention.cdecl,
588 EntryPoint = 'RAND_screen')]
589 procedure RandScreen; external;
590
591 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
592 SetLastError = False, CallingConvention= CallingConvention.cdecl,
593 EntryPoint = 'BIO_new')]
594 function BioNew(b: PBIO_METHOD): PBIO; external;
595
596 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
597 SetLastError = False, CallingConvention= CallingConvention.cdecl,
598 EntryPoint = 'BIO_free_all')]
599 procedure BioFreeAll(b: PBIO); external;
600
601 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
602 SetLastError = False, CallingConvention= CallingConvention.cdecl,
603 EntryPoint = 'BIO_s_mem')]
604 function BioSMem: PBIO_METHOD; external;
605
606 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
607 SetLastError = False, CallingConvention= CallingConvention.cdecl,
608 EntryPoint = 'BIO_ctrl_pending')]
609 function BioCtrlPending(b: PBIO): integer; external;
610
611 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
612 SetLastError = False, CallingConvention= CallingConvention.cdecl,
613 EntryPoint = 'BIO_read')]
614 function BioRead(b: PBIO; Buf: StringBuilder; Len: integer): integer; external;
615
616 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
617 SetLastError = False, CallingConvention= CallingConvention.cdecl,
618 EntryPoint = 'BIO_write')]
619 function BioWrite(b: PBIO; var Buf: String; Len: integer): integer; external;
620
621 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
622 SetLastError = False, CallingConvention= CallingConvention.cdecl,
623 EntryPoint = 'd2i_PKCS12_bio')]
624 function d2iPKCS12bio(b:PBIO; Pkcs12: SslPtr): SslPtr; external;
625
626 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
627 SetLastError = False, CallingConvention= CallingConvention.cdecl,
628 EntryPoint = 'PKCS12_parse')]
629 function PKCS12parse(p12: SslPtr; pass: string; var pkey, cert, ca: SslPtr): integer; external;
630
631 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
632 SetLastError = False, CallingConvention= CallingConvention.cdecl,
633 EntryPoint = 'PKCS12_free')]
634 procedure PKCS12free(p12: SslPtr); external;
635
636 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
637 SetLastError = False, CallingConvention= CallingConvention.cdecl,
638 EntryPoint = 'RSA_generate_key')]
639 function RsaGenerateKey(bits, e: integer; callback: PFunction; cb_arg: SslPtr): PRSA; external;
640
641 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
642 SetLastError = False, CallingConvention= CallingConvention.cdecl,
643 EntryPoint = 'ASN1_UTCTIME_new')]
644 function Asn1UtctimeNew: PASN1_UTCTIME; external;
645
646 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
647 SetLastError = False, CallingConvention= CallingConvention.cdecl,
648 EntryPoint = 'ASN1_UTCTIME_free')]
649 procedure Asn1UtctimeFree(a: PASN1_UTCTIME); external;
650
651 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
652 SetLastError = False, CallingConvention= CallingConvention.cdecl,
653 EntryPoint = 'ASN1_INTEGER_set')]
654 function Asn1IntegerSet(a: PASN1_INTEGER; v: integer): integer; external;
655
656 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
657 SetLastError = False, CallingConvention= CallingConvention.cdecl,
658 EntryPoint = 'i2d_X509_bio')]
659 function i2dX509bio(b: PBIO; x: PX509): integer; external;
660
661 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
662 SetLastError = False, CallingConvention= CallingConvention.cdecl,
663 EntryPoint = 'i2d_PrivateKey_bio')]
664 function i2dPrivateKeyBio(b: PBIO; pkey: EVP_PKEY): integer; external;
665
666 // 3DES functions
667 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
668 SetLastError = False, CallingConvention= CallingConvention.cdecl,
669 EntryPoint = 'DES_set_odd_parity')]
670 procedure DESsetoddparity(Key: des_cblock); external;
671
672 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
673 SetLastError = False, CallingConvention= CallingConvention.cdecl,
674 EntryPoint = 'DES_set_key_checked')]
675 function DESsetkeychecked(key: des_cblock; schedule: des_key_schedule): Integer; external;
676
677 [DllImport(DLLUtilName, CharSet = CharSet.Ansi,
678 SetLastError = False, CallingConvention= CallingConvention.cdecl,
679 EntryPoint = 'DES_ecb_encrypt')]
680 procedure DESecbencrypt(Input: des_cblock; output: des_cblock; ks: des_key_schedule; enc: Integer); external;
681
682{$ELSE}
683// libssl.dll
684 function SslGetError(s: PSSL; ret_code: Integer):Integer;
685 function SslLibraryInit:Integer;
686 procedure SslLoadErrorStrings;
687// function SslCtxSetCipherList(arg0: PSSL_CTX; str: PChar):Integer;
688 function SslCtxSetCipherList(arg0: PSSL_CTX; var str: AnsiString):Integer;
689 function SslCtxNew(meth: PSSL_METHOD):PSSL_CTX;
690 procedure SslCtxFree(arg0: PSSL_CTX);
691 function SslSetFd(s: PSSL; fd: Integer):Integer;
692 function SslMethodV2:PSSL_METHOD;
693 function SslMethodV3:PSSL_METHOD;
694 function SslMethodTLSV1:PSSL_METHOD;
695 function SslMethodV23:PSSL_METHOD;
696 function SslCtxUsePrivateKey(ctx: PSSL_CTX; pkey: SslPtr):Integer;
697 function SslCtxUsePrivateKeyASN1(pk: integer; ctx: PSSL_CTX; d: AnsiString; len: integer):Integer;
698// function SslCtxUsePrivateKeyFile(ctx: PSSL_CTX; const _file: PChar; _type: Integer):Integer;
699 function SslCtxUsePrivateKeyFile(ctx: PSSL_CTX; const _file: AnsiString; _type: Integer):Integer;
700 function SslCtxUseCertificate(ctx: PSSL_CTX; x: SslPtr):Integer;
701 function SslCtxUseCertificateASN1(ctx: PSSL_CTX; len: integer; d: AnsiString):Integer;
702 function SslCtxUseCertificateFile(ctx: PSSL_CTX; const _file: AnsiString; _type: Integer):Integer;
703// function SslCtxUseCertificateChainFile(ctx: PSSL_CTX; const _file: PChar):Integer;
704 function SslCtxUseCertificateChainFile(ctx: PSSL_CTX; const _file: AnsiString):Integer;
705 function SslCtxCheckPrivateKeyFile(ctx: PSSL_CTX):Integer;
706 procedure SslCtxSetDefaultPasswdCb(ctx: PSSL_CTX; cb: PPasswdCb);
707 procedure SslCtxSetDefaultPasswdCbUserdata(ctx: PSSL_CTX; u: SslPtr);
708// function SslCtxLoadVerifyLocations(ctx: PSSL_CTX; const CAfile: PChar; const CApath: PChar):Integer;
709 function SslCtxLoadVerifyLocations(ctx: PSSL_CTX; const CAfile: AnsiString; const CApath: AnsiString):Integer;
710 function SslCtxCtrl(ctx: PSSL_CTX; cmd: integer; larg: integer; parg: SslPtr): integer;
711 function SslNew(ctx: PSSL_CTX):PSSL;
712 procedure SslFree(ssl: PSSL);
713 function SslAccept(ssl: PSSL):Integer;
714 function SslConnect(ssl: PSSL):Integer;
715 function SslShutdown(ssl: PSSL):Integer;
716 function SslRead(ssl: PSSL; buf: SslPtr; num: Integer):Integer;
717 function SslPeek(ssl: PSSL; buf: SslPtr; num: Integer):Integer;
718 function SslWrite(ssl: PSSL; buf: SslPtr; num: Integer):Integer;
719 function SslPending(ssl: PSSL):Integer;
720 function SslGetVersion(ssl: PSSL):AnsiString;
721 function SslGetPeerCertificate(ssl: PSSL):PX509;
722 procedure SslCtxSetVerify(ctx: PSSL_CTX; mode: Integer; arg2: PFunction);
723 function SSLGetCurrentCipher(s: PSSL):SslPtr;
724 function SSLCipherGetName(c: SslPtr): AnsiString;
725 function SSLCipherGetBits(c: SslPtr; var alg_bits: Integer):Integer;
726 function SSLGetVerifyResult(ssl: PSSL):Integer;
727 function SSLCtrl(ssl: PSSL; cmd: integer; larg: integer; parg: SslPtr):Integer;
728
729// libeay.dll
730 function X509New: PX509;
731 procedure X509Free(x: PX509);
732 function X509NameOneline(a: PX509_NAME; var buf: AnsiString; size: Integer):AnsiString;
733 function X509GetSubjectName(a: PX509):PX509_NAME;
734 function X509GetIssuerName(a: PX509):PX509_NAME;
735 function X509NameHash(x: PX509_NAME):Cardinal;
736// function SslX509Digest(data: PX509; _type: PEVP_MD; md: PChar; len: PInteger):Integer;
737 function X509Digest(data: PX509; _type: PEVP_MD; md: AnsiString; var len: Integer):Integer;
738 function X509print(b: PBIO; a: PX509): integer;
739 function X509SetVersion(x: PX509; version: integer): integer;
740 function X509SetPubkey(x: PX509; pkey: EVP_PKEY): integer;
741 function X509SetIssuerName(x: PX509; name: PX509_NAME): integer;
742 function X509NameAddEntryByTxt(name: PX509_NAME; field: Ansistring; _type: integer;
743 bytes: Ansistring; len, loc, _set: integer): integer;
744 function X509Sign(x: PX509; pkey: EVP_PKEY; const md: PEVP_MD): integer;
745 function X509GmtimeAdj(s: PASN1_UTCTIME; adj: integer): PASN1_UTCTIME;
746 function X509SetNotBefore(x: PX509; tm: PASN1_UTCTIME): integer;
747 function X509SetNotAfter(x: PX509; tm: PASN1_UTCTIME): integer;
748 function X509GetSerialNumber(x: PX509): PASN1_INTEGER;
749 function EvpPkeyNew: EVP_PKEY;
750 procedure EvpPkeyFree(pk: EVP_PKEY);
751 function EvpPkeyAssign(pkey: EVP_PKEY; _type: integer; key: Prsa): integer;
752 function EvpGetDigestByName(Name: AnsiString): PEVP_MD;
753 procedure EVPcleanup;
754// function ErrErrorString(e: integer; buf: PChar): PChar;
755 function SSLeayversion(t: integer): Ansistring;
756 procedure ErrErrorString(e: integer; var buf: Ansistring; len: integer);
757 function ErrGetError: integer;
758 procedure ErrClearError;
759 procedure ErrFreeStrings;
760 procedure ErrRemoveState(pid: integer);
761 procedure OPENSSLaddallalgorithms;
762 procedure CRYPTOcleanupAllExData;
763 procedure RandScreen;
764 function BioNew(b: PBIO_METHOD): PBIO;
765 procedure BioFreeAll(b: PBIO);
766 function BioSMem: PBIO_METHOD;
767 function BioCtrlPending(b: PBIO): integer;
768 function BioRead(b: PBIO; var Buf: AnsiString; Len: integer): integer;
769 function BioWrite(b: PBIO; Buf: AnsiString; Len: integer): integer;
770 function d2iPKCS12bio(b:PBIO; Pkcs12: SslPtr): SslPtr;
771 function PKCS12parse(p12: SslPtr; pass: Ansistring; var pkey, cert, ca: SslPtr): integer;
772 procedure PKCS12free(p12: SslPtr);
773 function RsaGenerateKey(bits, e: integer; callback: PFunction; cb_arg: SslPtr): PRSA;
774 function Asn1UtctimeNew: PASN1_UTCTIME;
775 procedure Asn1UtctimeFree(a: PASN1_UTCTIME);
776 function Asn1IntegerSet(a: PASN1_INTEGER; v: integer): integer;
777 function Asn1IntegerGet(a: PASN1_INTEGER): integer; {pf}
778 function i2dX509bio(b: PBIO; x: PX509): integer;
779 function d2iX509bio(b:PBIO; x:PX509): PX509; {pf}
780 function PEMReadBioX509(b:PBIO; {var x:PX509;}x:PSslPtr; callback:PFunction; cb_arg: SslPtr): PX509; {pf}
781 procedure SkX509PopFree(st: PSTACK; func: TSkPopFreeFunc); {pf}
782
783
784 function i2dPrivateKeyBio(b: PBIO; pkey: EVP_PKEY): integer;
785
786 // 3DES functions
787 procedure DESsetoddparity(Key: des_cblock);
788 function DESsetkeychecked(key: des_cblock; schedule: des_key_schedule): Integer;
789 procedure DESecbencrypt(Input: des_cblock; output: des_cblock; ks: des_key_schedule; enc: Integer);
790
791{$ENDIF}
792
793function IsSSLloaded: Boolean;
794function InitSSLInterface: Boolean;
795function DestroySSLInterface: Boolean;
796
797var
798 _X509Free: TX509Free = nil; {pf}
799
800implementation
801
802uses SyncObjs;
803
804{$IFNDEF CIL}
805type
806// libssl.dll
807 TSslGetError = function(s: PSSL; ret_code: Integer):Integer; cdecl;
808 TSslLibraryInit = function:Integer; cdecl;
809 TSslLoadErrorStrings = procedure; cdecl;
810 TSslCtxSetCipherList = function(arg0: PSSL_CTX; str: PAnsiChar):Integer; cdecl;
811 TSslCtxNew = function(meth: PSSL_METHOD):PSSL_CTX; cdecl;
812 TSslCtxFree = procedure(arg0: PSSL_CTX); cdecl;
813 TSslSetFd = function(s: PSSL; fd: Integer):Integer; cdecl;
814 TSslMethodV2 = function:PSSL_METHOD; cdecl;
815 TSslMethodV3 = function:PSSL_METHOD; cdecl;
816 TSslMethodTLSV1 = function:PSSL_METHOD; cdecl;
817 TSslMethodV23 = function:PSSL_METHOD; cdecl;
818 TSslCtxUsePrivateKey = function(ctx: PSSL_CTX; pkey: sslptr):Integer; cdecl;
819 TSslCtxUsePrivateKeyASN1 = function(pk: integer; ctx: PSSL_CTX; d: sslptr; len: integer):Integer; cdecl;
820 TSslCtxUsePrivateKeyFile = function(ctx: PSSL_CTX; const _file: PAnsiChar; _type: Integer):Integer; cdecl;
821 TSslCtxUseCertificate = function(ctx: PSSL_CTX; x: SslPtr):Integer; cdecl;
822 TSslCtxUseCertificateASN1 = function(ctx: PSSL_CTX; len: Integer; d: SslPtr):Integer; cdecl;
823 TSslCtxUseCertificateFile = function(ctx: PSSL_CTX; const _file: PAnsiChar; _type: Integer):Integer; cdecl;
824 TSslCtxUseCertificateChainFile = function(ctx: PSSL_CTX; const _file: PAnsiChar):Integer; cdecl;
825 TSslCtxCheckPrivateKeyFile = function(ctx: PSSL_CTX):Integer; cdecl;
826 TSslCtxSetDefaultPasswdCb = procedure(ctx: PSSL_CTX; cb: SslPtr); cdecl;
827 TSslCtxSetDefaultPasswdCbUserdata = procedure(ctx: PSSL_CTX; u: SslPtr); cdecl;
828 TSslCtxLoadVerifyLocations = function(ctx: PSSL_CTX; const CAfile: PAnsiChar; const CApath: PAnsiChar):Integer; cdecl;
829 TSslCtxCtrl = function(ctx: PSSL_CTX; cmd: integer; larg: integer; parg: SslPtr): integer; cdecl;
830 TSslNew = function(ctx: PSSL_CTX):PSSL; cdecl;
831 TSslFree = procedure(ssl: PSSL); cdecl;
832 TSslAccept = function(ssl: PSSL):Integer; cdecl;
833 TSslConnect = function(ssl: PSSL):Integer; cdecl;
834 TSslShutdown = function(ssl: PSSL):Integer; cdecl;
835 TSslRead = function(ssl: PSSL; buf: PAnsiChar; num: Integer):Integer; cdecl;
836 TSslPeek = function(ssl: PSSL; buf: PAnsiChar; num: Integer):Integer; cdecl;
837 TSslWrite = function(ssl: PSSL; const buf: PAnsiChar; num: Integer):Integer; cdecl;
838 TSslPending = function(ssl: PSSL):Integer; cdecl;
839 TSslGetVersion = function(ssl: PSSL):PAnsiChar; cdecl;
840 TSslGetPeerCertificate = function(ssl: PSSL):PX509; cdecl;
841 TSslCtxSetVerify = procedure(ctx: PSSL_CTX; mode: Integer; arg2: SslPtr); cdecl;
842 TSSLGetCurrentCipher = function(s: PSSL):SslPtr; cdecl;
843 TSSLCipherGetName = function(c: Sslptr):PAnsiChar; cdecl;
844 TSSLCipherGetBits = function(c: SslPtr; alg_bits: PInteger):Integer; cdecl;
845 TSSLGetVerifyResult = function(ssl: PSSL):Integer; cdecl;
846 TSSLCtrl = function(ssl: PSSL; cmd: integer; larg: integer; parg: SslPtr):Integer; cdecl;
847
848 TSSLSetTlsextHostName = function(ssl: PSSL; buf: PAnsiChar):Integer; cdecl;
849
850// libeay.dll
851 TX509New = function: PX509; cdecl;
852 TX509NameOneline = function(a: PX509_NAME; buf: PAnsiChar; size: Integer):PAnsiChar; cdecl;
853 TX509GetSubjectName = function(a: PX509):PX509_NAME; cdecl;
854 TX509GetIssuerName = function(a: PX509):PX509_NAME; cdecl;
855 TX509NameHash = function(x: PX509_NAME):Cardinal; cdecl;
856 TX509Digest = function(data: PX509; _type: PEVP_MD; md: PAnsiChar; len: PInteger):Integer; cdecl;
857 TX509print = function(b: PBIO; a: PX509): integer; cdecl;
858 TX509SetVersion = function(x: PX509; version: integer): integer; cdecl;
859 TX509SetPubkey = function(x: PX509; pkey: EVP_PKEY): integer; cdecl;
860 TX509SetIssuerName = function(x: PX509; name: PX509_NAME): integer; cdecl;
861 TX509NameAddEntryByTxt = function(name: PX509_NAME; field: PAnsiChar; _type: integer;
862 bytes: PAnsiChar; len, loc, _set: integer): integer; cdecl;
863 TX509Sign = function(x: PX509; pkey: EVP_PKEY; const md: PEVP_MD): integer; cdecl;
864 TX509GmtimeAdj = function(s: PASN1_UTCTIME; adj: integer): PASN1_UTCTIME; cdecl;
865 TX509SetNotBefore = function(x: PX509; tm: PASN1_UTCTIME): integer; cdecl;
866 TX509SetNotAfter = function(x: PX509; tm: PASN1_UTCTIME): integer; cdecl;
867 TX509GetSerialNumber = function(x: PX509): PASN1_INTEGER; cdecl;
868 TEvpPkeyNew = function: EVP_PKEY; cdecl;
869 TEvpPkeyFree = procedure(pk: EVP_PKEY); cdecl;
870 TEvpPkeyAssign = function(pkey: EVP_PKEY; _type: integer; key: Prsa): integer; cdecl;
871 TEvpGetDigestByName = function(Name: PAnsiChar): PEVP_MD; cdecl;
872 TEVPcleanup = procedure; cdecl;
873 TSSLeayversion = function(t: integer): PAnsiChar; cdecl;
874 TErrErrorString = procedure(e: integer; buf: PAnsiChar; len: integer); cdecl;
875 TErrGetError = function: integer; cdecl;
876 TErrClearError = procedure; cdecl;
877 TErrFreeStrings = procedure; cdecl;
878 TErrRemoveState = procedure(pid: integer); cdecl;
879 TOPENSSLaddallalgorithms = procedure; cdecl;
880 TCRYPTOcleanupAllExData = procedure; cdecl;
881 TRandScreen = procedure; cdecl;
882 TBioNew = function(b: PBIO_METHOD): PBIO; cdecl;
883 TBioFreeAll = procedure(b: PBIO); cdecl;
884 TBioSMem = function: PBIO_METHOD; cdecl;
885 TBioCtrlPending = function(b: PBIO): integer; cdecl;
886 TBioRead = function(b: PBIO; Buf: PAnsiChar; Len: integer): integer; cdecl;
887 TBioWrite = function(b: PBIO; Buf: PAnsiChar; Len: integer): integer; cdecl;
888 Td2iPKCS12bio = function(b:PBIO; Pkcs12: SslPtr): SslPtr; cdecl;
889 TPKCS12parse = function(p12: SslPtr; pass: PAnsiChar; var pkey, cert, ca: SslPtr): integer; cdecl;
890 TPKCS12free = procedure(p12: SslPtr); cdecl;
891 TRsaGenerateKey = function(bits, e: integer; callback: PFunction; cb_arg: SslPtr): PRSA; cdecl;
892 TAsn1UtctimeNew = function: PASN1_UTCTIME; cdecl;
893 TAsn1UtctimeFree = procedure(a: PASN1_UTCTIME); cdecl;
894 TAsn1IntegerSet = function(a: PASN1_INTEGER; v: integer): integer; cdecl;
895 TAsn1IntegerGet = function(a: PASN1_INTEGER): integer; cdecl; {pf}
896 Ti2dX509bio = function(b: PBIO; x: PX509): integer; cdecl;
897 Td2iX509bio = function(b:PBIO; x:PX509): PX509; cdecl; {pf}
898 TPEMReadBioX509 = function(b:PBIO; {var x:PX509;}x:PSslPtr; callback:PFunction; cb_arg:SslPtr): PX509; cdecl; {pf}
899 TSkX509PopFree = procedure(st: PSTACK; func: TSkPopFreeFunc); cdecl; {pf}
900 Ti2dPrivateKeyBio= function(b: PBIO; pkey: EVP_PKEY): integer; cdecl;
901
902 // 3DES functions
903 TDESsetoddparity = procedure(Key: des_cblock); cdecl;
904 TDESsetkeychecked = function(key: des_cblock; schedule: des_key_schedule): Integer; cdecl;
905 TDESecbencrypt = procedure(Input: des_cblock; output: des_cblock; ks: des_key_schedule; enc: Integer); cdecl;
906 //thread lock functions
907 TCRYPTOnumlocks = function: integer; cdecl;
908 TCRYPTOSetLockingCallback = procedure(cb: Sslptr); cdecl;
909
910var
911// libssl.dll
912 _SslGetError: TSslGetError = nil;
913 _SslLibraryInit: TSslLibraryInit = nil;
914 _SslLoadErrorStrings: TSslLoadErrorStrings = nil;
915 _SslCtxSetCipherList: TSslCtxSetCipherList = nil;
916 _SslCtxNew: TSslCtxNew = nil;
917 _SslCtxFree: TSslCtxFree = nil;
918 _SslSetFd: TSslSetFd = nil;
919 _SslMethodV2: TSslMethodV2 = nil;
920 _SslMethodV3: TSslMethodV3 = nil;
921 _SslMethodTLSV1: TSslMethodTLSV1 = nil;
922 _SslMethodV23: TSslMethodV23 = nil;
923 _SslCtxUsePrivateKey: TSslCtxUsePrivateKey = nil;
924 _SslCtxUsePrivateKeyASN1: TSslCtxUsePrivateKeyASN1 = nil;
925 _SslCtxUsePrivateKeyFile: TSslCtxUsePrivateKeyFile = nil;
926 _SslCtxUseCertificate: TSslCtxUseCertificate = nil;
927 _SslCtxUseCertificateASN1: TSslCtxUseCertificateASN1 = nil;
928 _SslCtxUseCertificateFile: TSslCtxUseCertificateFile = nil;
929 _SslCtxUseCertificateChainFile: TSslCtxUseCertificateChainFile = nil;
930 _SslCtxCheckPrivateKeyFile: TSslCtxCheckPrivateKeyFile = nil;
931 _SslCtxSetDefaultPasswdCb: TSslCtxSetDefaultPasswdCb = nil;
932 _SslCtxSetDefaultPasswdCbUserdata: TSslCtxSetDefaultPasswdCbUserdata = nil;
933 _SslCtxLoadVerifyLocations: TSslCtxLoadVerifyLocations = nil;
934 _SslCtxCtrl: TSslCtxCtrl = nil;
935 _SslNew: TSslNew = nil;
936 _SslFree: TSslFree = nil;
937 _SslAccept: TSslAccept = nil;
938 _SslConnect: TSslConnect = nil;
939 _SslShutdown: TSslShutdown = nil;
940 _SslRead: TSslRead = nil;
941 _SslPeek: TSslPeek = nil;
942 _SslWrite: TSslWrite = nil;
943 _SslPending: TSslPending = nil;
944 _SslGetVersion: TSslGetVersion = nil;
945 _SslGetPeerCertificate: TSslGetPeerCertificate = nil;
946 _SslCtxSetVerify: TSslCtxSetVerify = nil;
947 _SSLGetCurrentCipher: TSSLGetCurrentCipher = nil;
948 _SSLCipherGetName: TSSLCipherGetName = nil;
949 _SSLCipherGetBits: TSSLCipherGetBits = nil;
950 _SSLGetVerifyResult: TSSLGetVerifyResult = nil;
951 _SSLCtrl: TSSLCtrl = nil;
952
953// libeay.dll
954 _X509New: TX509New = nil;
955 _X509NameOneline: TX509NameOneline = nil;
956 _X509GetSubjectName: TX509GetSubjectName = nil;
957 _X509GetIssuerName: TX509GetIssuerName = nil;
958 _X509NameHash: TX509NameHash = nil;
959 _X509Digest: TX509Digest = nil;
960 _X509print: TX509print = nil;
961 _X509SetVersion: TX509SetVersion = nil;
962 _X509SetPubkey: TX509SetPubkey = nil;
963 _X509SetIssuerName: TX509SetIssuerName = nil;
964 _X509NameAddEntryByTxt: TX509NameAddEntryByTxt = nil;
965 _X509Sign: TX509Sign = nil;
966 _X509GmtimeAdj: TX509GmtimeAdj = nil;
967 _X509SetNotBefore: TX509SetNotBefore = nil;
968 _X509SetNotAfter: TX509SetNotAfter = nil;
969 _X509GetSerialNumber: TX509GetSerialNumber = nil;
970 _EvpPkeyNew: TEvpPkeyNew = nil;
971 _EvpPkeyFree: TEvpPkeyFree = nil;
972 _EvpPkeyAssign: TEvpPkeyAssign = nil;
973 _EvpGetDigestByName: TEvpGetDigestByName = nil;
974 _EVPcleanup: TEVPcleanup = nil;
975 _SSLeayversion: TSSLeayversion = nil;
976 _ErrErrorString: TErrErrorString = nil;
977 _ErrGetError: TErrGetError = nil;
978 _ErrClearError: TErrClearError = nil;
979 _ErrFreeStrings: TErrFreeStrings = nil;
980 _ErrRemoveState: TErrRemoveState = nil;
981 _OPENSSLaddallalgorithms: TOPENSSLaddallalgorithms = nil;
982 _CRYPTOcleanupAllExData: TCRYPTOcleanupAllExData = nil;
983 _RandScreen: TRandScreen = nil;
984 _BioNew: TBioNew = nil;
985 _BioFreeAll: TBioFreeAll = nil;
986 _BioSMem: TBioSMem = nil;
987 _BioCtrlPending: TBioCtrlPending = nil;
988 _BioRead: TBioRead = nil;
989 _BioWrite: TBioWrite = nil;
990 _d2iPKCS12bio: Td2iPKCS12bio = nil;
991 _PKCS12parse: TPKCS12parse = nil;
992 _PKCS12free: TPKCS12free = nil;
993 _RsaGenerateKey: TRsaGenerateKey = nil;
994 _Asn1UtctimeNew: TAsn1UtctimeNew = nil;
995 _Asn1UtctimeFree: TAsn1UtctimeFree = nil;
996 _Asn1IntegerSet: TAsn1IntegerSet = nil;
997 _Asn1IntegerGet: TAsn1IntegerGet = nil; {pf}
998 _i2dX509bio: Ti2dX509bio = nil;
999 _d2iX509bio: Td2iX509bio = nil; {pf}
1000 _PEMReadBioX509: TPEMReadBioX509 = nil; {pf}
1001 _SkX509PopFree: TSkX509PopFree = nil; {pf}
1002 _i2dPrivateKeyBio: Ti2dPrivateKeyBio = nil;
1003
1004 // 3DES functions
1005 _DESsetoddparity: TDESsetoddparity = nil;
1006 _DESsetkeychecked: TDESsetkeychecked = nil;
1007 _DESecbencrypt: TDESecbencrypt = nil;
1008 //thread lock functions
1009 _CRYPTOnumlocks: TCRYPTOnumlocks = nil;
1010 _CRYPTOSetLockingCallback: TCRYPTOSetLockingCallback = nil;
1011{$ENDIF}
1012
1013var
1014 SSLCS: TCriticalSection;
1015 SSLloaded: boolean = false;
1016{$IFNDEF CIL}
1017 Locks: TList;
1018{$ENDIF}
1019
1020{$IFNDEF CIL}
1021// libssl.dll
1022function SslGetError(s: PSSL; ret_code: Integer):Integer;
1023begin
1024 if InitSSLInterface and Assigned(_SslGetError) then
1025 Result := _SslGetError(s, ret_code)
1026 else
1027 Result := SSL_ERROR_SSL;
1028end;
1029
1030function SslLibraryInit:Integer;
1031begin
1032 if InitSSLInterface and Assigned(_SslLibraryInit) then
1033 Result := _SslLibraryInit
1034 else
1035 Result := 1;
1036end;
1037
1038procedure SslLoadErrorStrings;
1039begin
1040 if InitSSLInterface and Assigned(_SslLoadErrorStrings) then
1041 _SslLoadErrorStrings;
1042end;
1043
1044//function SslCtxSetCipherList(arg0: PSSL_CTX; str: PChar):Integer;
1045function SslCtxSetCipherList(arg0: PSSL_CTX; var str: AnsiString):Integer;
1046begin
1047 if InitSSLInterface and Assigned(_SslCtxSetCipherList) then
1048 Result := _SslCtxSetCipherList(arg0, PAnsiChar(str))
1049 else
1050 Result := 0;
1051end;
1052
1053function SslCtxNew(meth: PSSL_METHOD):PSSL_CTX;
1054begin
1055 if InitSSLInterface and Assigned(_SslCtxNew) then
1056 Result := _SslCtxNew(meth)
1057 else
1058 Result := nil;
1059end;
1060
1061procedure SslCtxFree(arg0: PSSL_CTX);
1062begin
1063 if InitSSLInterface and Assigned(_SslCtxFree) then
1064 _SslCtxFree(arg0);
1065end;
1066
1067function SslSetFd(s: PSSL; fd: Integer):Integer;
1068begin
1069 if InitSSLInterface and Assigned(_SslSetFd) then
1070 Result := _SslSetFd(s, fd)
1071 else
1072 Result := 0;
1073end;
1074
1075function SslMethodV2:PSSL_METHOD;
1076begin
1077 if InitSSLInterface and Assigned(_SslMethodV2) then
1078 Result := _SslMethodV2
1079 else
1080 Result := nil;
1081end;
1082
1083function SslMethodV3:PSSL_METHOD;
1084begin
1085 if InitSSLInterface and Assigned(_SslMethodV3) then
1086 Result := _SslMethodV3
1087 else
1088 Result := nil;
1089end;
1090
1091function SslMethodTLSV1:PSSL_METHOD;
1092begin
1093 if InitSSLInterface and Assigned(_SslMethodTLSV1) then
1094 Result := _SslMethodTLSV1
1095 else
1096 Result := nil;
1097end;
1098
1099function SslMethodV23:PSSL_METHOD;
1100begin
1101 if InitSSLInterface and Assigned(_SslMethodV23) then
1102 Result := _SslMethodV23
1103 else
1104 Result := nil;
1105end;
1106
1107function SslCtxUsePrivateKey(ctx: PSSL_CTX; pkey: SslPtr):Integer;
1108begin
1109 if InitSSLInterface and Assigned(_SslCtxUsePrivateKey) then
1110 Result := _SslCtxUsePrivateKey(ctx, pkey)
1111 else
1112 Result := 0;
1113end;
1114
1115function SslCtxUsePrivateKeyASN1(pk: integer; ctx: PSSL_CTX; d: AnsiString; len: integer):Integer;
1116begin
1117 if InitSSLInterface and Assigned(_SslCtxUsePrivateKeyASN1) then
1118 Result := _SslCtxUsePrivateKeyASN1(pk, ctx, Sslptr(d), len)
1119 else
1120 Result := 0;
1121end;
1122
1123//function SslCtxUsePrivateKeyFile(ctx: PSSL_CTX; const _file: PChar; _type: Integer):Integer;
1124function SslCtxUsePrivateKeyFile(ctx: PSSL_CTX; const _file: AnsiString; _type: Integer):Integer;
1125begin
1126 if InitSSLInterface and Assigned(_SslCtxUsePrivateKeyFile) then
1127 Result := _SslCtxUsePrivateKeyFile(ctx, PAnsiChar(_file), _type)
1128 else
1129 Result := 0;
1130end;
1131
1132function SslCtxUseCertificate(ctx: PSSL_CTX; x: SslPtr):Integer;
1133begin
1134 if InitSSLInterface and Assigned(_SslCtxUseCertificate) then
1135 Result := _SslCtxUseCertificate(ctx, x)
1136 else
1137 Result := 0;
1138end;
1139
1140function SslCtxUseCertificateASN1(ctx: PSSL_CTX; len: integer; d: AnsiString):Integer;
1141begin
1142 if InitSSLInterface and Assigned(_SslCtxUseCertificateASN1) then
1143 Result := _SslCtxUseCertificateASN1(ctx, len, SslPtr(d))
1144 else
1145 Result := 0;
1146end;
1147
1148function SslCtxUseCertificateFile(ctx: PSSL_CTX; const _file: AnsiString; _type: Integer):Integer;
1149begin
1150 if InitSSLInterface and Assigned(_SslCtxUseCertificateFile) then
1151 Result := _SslCtxUseCertificateFile(ctx, PAnsiChar(_file), _type)
1152 else
1153 Result := 0;
1154end;
1155
1156//function SslCtxUseCertificateChainFile(ctx: PSSL_CTX; const _file: PChar):Integer;
1157function SslCtxUseCertificateChainFile(ctx: PSSL_CTX; const _file: AnsiString):Integer;
1158begin
1159 if InitSSLInterface and Assigned(_SslCtxUseCertificateChainFile) then
1160 Result := _SslCtxUseCertificateChainFile(ctx, PAnsiChar(_file))
1161 else
1162 Result := 0;
1163end;
1164
1165function SslCtxCheckPrivateKeyFile(ctx: PSSL_CTX):Integer;
1166begin
1167 if InitSSLInterface and Assigned(_SslCtxCheckPrivateKeyFile) then
1168 Result := _SslCtxCheckPrivateKeyFile(ctx)
1169 else
1170 Result := 0;
1171end;
1172
1173procedure SslCtxSetDefaultPasswdCb(ctx: PSSL_CTX; cb: PPasswdCb);
1174begin
1175 if InitSSLInterface and Assigned(_SslCtxSetDefaultPasswdCb) then
1176 _SslCtxSetDefaultPasswdCb(ctx, cb);
1177end;
1178
1179procedure SslCtxSetDefaultPasswdCbUserdata(ctx: PSSL_CTX; u: SslPtr);
1180begin
1181 if InitSSLInterface and Assigned(_SslCtxSetDefaultPasswdCbUserdata) then
1182 _SslCtxSetDefaultPasswdCbUserdata(ctx, u);
1183end;
1184
1185//function SslCtxLoadVerifyLocations(ctx: PSSL_CTX; const CAfile: PChar; const CApath: PChar):Integer;
1186function SslCtxLoadVerifyLocations(ctx: PSSL_CTX; const CAfile: AnsiString; const CApath: AnsiString):Integer;
1187begin
1188 if InitSSLInterface and Assigned(_SslCtxLoadVerifyLocations) then
1189 Result := _SslCtxLoadVerifyLocations(ctx, SslPtr(CAfile), SslPtr(CApath))
1190 else
1191 Result := 0;
1192end;
1193
1194function SslCtxCtrl(ctx: PSSL_CTX; cmd: integer; larg: integer; parg: SslPtr): integer;
1195begin
1196 if InitSSLInterface and Assigned(_SslCtxCtrl) then
1197 Result := _SslCtxCtrl(ctx, cmd, larg, parg)
1198 else
1199 Result := 0;
1200end;
1201
1202function SslNew(ctx: PSSL_CTX):PSSL;
1203begin
1204 if InitSSLInterface and Assigned(_SslNew) then
1205 Result := _SslNew(ctx)
1206 else
1207 Result := nil;
1208end;
1209
1210procedure SslFree(ssl: PSSL);
1211begin
1212 if InitSSLInterface and Assigned(_SslFree) then
1213 _SslFree(ssl);
1214end;
1215
1216function SslAccept(ssl: PSSL):Integer;
1217begin
1218 if InitSSLInterface and Assigned(_SslAccept) then
1219 Result := _SslAccept(ssl)
1220 else
1221 Result := -1;
1222end;
1223
1224function SslConnect(ssl: PSSL):Integer;
1225begin
1226 if InitSSLInterface and Assigned(_SslConnect) then
1227 Result := _SslConnect(ssl)
1228 else
1229 Result := -1;
1230end;
1231
1232function SslShutdown(ssl: PSSL):Integer;
1233begin
1234 if InitSSLInterface and Assigned(_SslShutdown) then
1235 Result := _SslShutdown(ssl)
1236 else
1237 Result := -1;
1238end;
1239
1240//function SslRead(ssl: PSSL; buf: PChar; num: Integer):Integer;
1241function SslRead(ssl: PSSL; buf: SslPtr; num: Integer):Integer;
1242begin
1243 if InitSSLInterface and Assigned(_SslRead) then
1244 Result := _SslRead(ssl, PAnsiChar(buf), num)
1245 else
1246 Result := -1;
1247end;
1248
1249//function SslPeek(ssl: PSSL; buf: PChar; num: Integer):Integer;
1250function SslPeek(ssl: PSSL; buf: SslPtr; num: Integer):Integer;
1251begin
1252 if InitSSLInterface and Assigned(_SslPeek) then
1253 Result := _SslPeek(ssl, PAnsiChar(buf), num)
1254 else
1255 Result := -1;
1256end;
1257
1258//function SslWrite(ssl: PSSL; const buf: PChar; num: Integer):Integer;
1259function SslWrite(ssl: PSSL; buf: SslPtr; num: Integer):Integer;
1260begin
1261 if InitSSLInterface and Assigned(_SslWrite) then
1262 Result := _SslWrite(ssl, PAnsiChar(buf), num)
1263 else
1264 Result := -1;
1265end;
1266
1267function SslPending(ssl: PSSL):Integer;
1268begin
1269 if InitSSLInterface and Assigned(_SslPending) then
1270 Result := _SslPending(ssl)
1271 else
1272 Result := 0;
1273end;
1274
1275//function SslGetVersion(ssl: PSSL):PChar;
1276function SslGetVersion(ssl: PSSL):AnsiString;
1277begin
1278 if InitSSLInterface and Assigned(_SslGetVersion) then
1279 Result := _SslGetVersion(ssl)
1280 else
1281 Result := '';
1282end;
1283
1284function SslGetPeerCertificate(ssl: PSSL):PX509;
1285begin
1286 if InitSSLInterface and Assigned(_SslGetPeerCertificate) then
1287 Result := _SslGetPeerCertificate(ssl)
1288 else
1289 Result := nil;
1290end;
1291
1292//procedure SslCtxSetVerify(ctx: PSSL_CTX; mode: Integer; arg2: SslPtr);
1293procedure SslCtxSetVerify(ctx: PSSL_CTX; mode: Integer; arg2: PFunction);
1294begin
1295 if InitSSLInterface and Assigned(_SslCtxSetVerify) then
1296 _SslCtxSetVerify(ctx, mode, @arg2);
1297end;
1298
1299function SSLGetCurrentCipher(s: PSSL):SslPtr;
1300begin
1301 if InitSSLInterface and Assigned(_SSLGetCurrentCipher) then
1302{$IFDEF CIL}
1303{$ELSE}
1304 Result := _SSLGetCurrentCipher(s)
1305{$ENDIF}
1306 else
1307 Result := nil;
1308end;
1309
1310//function SSLCipherGetName(c: SslPtr):PChar;
1311function SSLCipherGetName(c: SslPtr):AnsiString;
1312begin
1313 if InitSSLInterface and Assigned(_SSLCipherGetName) then
1314 Result := _SSLCipherGetName(c)
1315 else
1316 Result := '';
1317end;
1318
1319//function SSLCipherGetBits(c: SslPtr; alg_bits: PInteger):Integer;
1320function SSLCipherGetBits(c: SslPtr; var alg_bits: Integer):Integer;
1321begin
1322 if InitSSLInterface and Assigned(_SSLCipherGetBits) then
1323 Result := _SSLCipherGetBits(c, @alg_bits)
1324 else
1325 Result := 0;
1326end;
1327
1328function SSLGetVerifyResult(ssl: PSSL):Integer;
1329begin
1330 if InitSSLInterface and Assigned(_SSLGetVerifyResult) then
1331 Result := _SSLGetVerifyResult(ssl)
1332 else
1333 Result := X509_V_ERR_APPLICATION_VERIFICATION;
1334end;
1335
1336
1337function SSLCtrl(ssl: PSSL; cmd: integer; larg: integer; parg: SslPtr):Integer;
1338begin
1339 if InitSSLInterface and Assigned(_SSLCtrl) then
1340 Result := _SSLCtrl(ssl, cmd, larg, parg)
1341 else
1342 Result := X509_V_ERR_APPLICATION_VERIFICATION;
1343end;
1344
1345// libeay.dll
1346function X509New: PX509;
1347begin
1348 if InitSSLInterface and Assigned(_X509New) then
1349 Result := _X509New
1350 else
1351 Result := nil;
1352end;
1353
1354procedure X509Free(x: PX509);
1355begin
1356 if InitSSLInterface and Assigned(_X509Free) then
1357 _X509Free(x);
1358end;
1359
1360//function SslX509NameOneline(a: PX509_NAME; buf: PChar; size: Integer):PChar;
1361function X509NameOneline(a: PX509_NAME; var buf: AnsiString; size: Integer):AnsiString;
1362begin
1363 if InitSSLInterface and Assigned(_X509NameOneline) then
1364 Result := _X509NameOneline(a, PAnsiChar(buf),size)
1365 else
1366 Result := '';
1367end;
1368
1369function X509GetSubjectName(a: PX509):PX509_NAME;
1370begin
1371 if InitSSLInterface and Assigned(_X509GetSubjectName) then
1372 Result := _X509GetSubjectName(a)
1373 else
1374 Result := nil;
1375end;
1376
1377function X509GetIssuerName(a: PX509):PX509_NAME;
1378begin
1379 if InitSSLInterface and Assigned(_X509GetIssuerName) then
1380 Result := _X509GetIssuerName(a)
1381 else
1382 Result := nil;
1383end;
1384
1385function X509NameHash(x: PX509_NAME):Cardinal;
1386begin
1387 if InitSSLInterface and Assigned(_X509NameHash) then
1388 Result := _X509NameHash(x)
1389 else
1390 Result := 0;
1391end;
1392
1393//function SslX509Digest(data: PX509; _type: PEVP_MD; md: PChar; len: PInteger):Integer;
1394function X509Digest(data: PX509; _type: PEVP_MD; md: AnsiString; var len: Integer):Integer;
1395begin
1396 if InitSSLInterface and Assigned(_X509Digest) then
1397 Result := _X509Digest(data, _type, PAnsiChar(md), @len)
1398 else
1399 Result := 0;
1400end;
1401
1402function EvpPkeyNew: EVP_PKEY;
1403begin
1404 if InitSSLInterface and Assigned(_EvpPkeyNew) then
1405 Result := _EvpPkeyNew
1406 else
1407 Result := nil;
1408end;
1409
1410procedure EvpPkeyFree(pk: EVP_PKEY);
1411begin
1412 if InitSSLInterface and Assigned(_EvpPkeyFree) then
1413 _EvpPkeyFree(pk);
1414end;
1415
1416function SSLeayversion(t: integer): Ansistring;
1417begin
1418 if InitSSLInterface and Assigned(_SSLeayversion) then
1419 Result := PAnsiChar(_SSLeayversion(t))
1420 else
1421 Result := '';
1422end;
1423
1424procedure ErrErrorString(e: integer; var buf: Ansistring; len: integer);
1425begin
1426 if InitSSLInterface and Assigned(_ErrErrorString) then
1427 _ErrErrorString(e, Pointer(buf), len);
1428 buf := PAnsiChar(Buf);
1429end;
1430
1431function ErrGetError: integer;
1432begin
1433 if InitSSLInterface and Assigned(_ErrGetError) then
1434 Result := _ErrGetError
1435 else
1436 Result := SSL_ERROR_SSL;
1437end;
1438
1439procedure ErrClearError;
1440begin
1441 if InitSSLInterface and Assigned(_ErrClearError) then
1442 _ErrClearError;
1443end;
1444
1445procedure ErrFreeStrings;
1446begin
1447 if InitSSLInterface and Assigned(_ErrFreeStrings) then
1448 _ErrFreeStrings;
1449end;
1450
1451procedure ErrRemoveState(pid: integer);
1452begin
1453 if InitSSLInterface and Assigned(_ErrRemoveState) then
1454 _ErrRemoveState(pid);
1455end;
1456
1457procedure OPENSSLaddallalgorithms;
1458begin
1459 if InitSSLInterface and Assigned(_OPENSSLaddallalgorithms) then
1460 _OPENSSLaddallalgorithms;
1461end;
1462
1463procedure EVPcleanup;
1464begin
1465 if InitSSLInterface and Assigned(_EVPcleanup) then
1466 _EVPcleanup;
1467end;
1468
1469procedure CRYPTOcleanupAllExData;
1470begin
1471 if InitSSLInterface and Assigned(_CRYPTOcleanupAllExData) then
1472 _CRYPTOcleanupAllExData;
1473end;
1474
1475procedure RandScreen;
1476begin
1477 if InitSSLInterface and Assigned(_RandScreen) then
1478 _RandScreen;
1479end;
1480
1481function BioNew(b: PBIO_METHOD): PBIO;
1482begin
1483 if InitSSLInterface and Assigned(_BioNew) then
1484 Result := _BioNew(b)
1485 else
1486 Result := nil;
1487end;
1488
1489procedure BioFreeAll(b: PBIO);
1490begin
1491 if InitSSLInterface and Assigned(_BioFreeAll) then
1492 _BioFreeAll(b);
1493end;
1494
1495function BioSMem: PBIO_METHOD;
1496begin
1497 if InitSSLInterface and Assigned(_BioSMem) then
1498 Result := _BioSMem
1499 else
1500 Result := nil;
1501end;
1502
1503function BioCtrlPending(b: PBIO): integer;
1504begin
1505 if InitSSLInterface and Assigned(_BioCtrlPending) then
1506 Result := _BioCtrlPending(b)
1507 else
1508 Result := 0;
1509end;
1510
1511//function BioRead(b: PBIO; Buf: PChar; Len: integer): integer;
1512function BioRead(b: PBIO; var Buf: AnsiString; Len: integer): integer;
1513begin
1514 if InitSSLInterface and Assigned(_BioRead) then
1515 Result := _BioRead(b, PAnsiChar(Buf), Len)
1516 else
1517 Result := -2;
1518end;
1519
1520//function BioWrite(b: PBIO; Buf: PChar; Len: integer): integer;
1521function BioWrite(b: PBIO; Buf: AnsiString; Len: integer): integer;
1522begin
1523 if InitSSLInterface and Assigned(_BioWrite) then
1524 Result := _BioWrite(b, PAnsiChar(Buf), Len)
1525 else
1526 Result := -2;
1527end;
1528
1529function X509print(b: PBIO; a: PX509): integer;
1530begin
1531 if InitSSLInterface and Assigned(_X509print) then
1532 Result := _X509print(b, a)
1533 else
1534 Result := 0;
1535end;
1536
1537function d2iPKCS12bio(b:PBIO; Pkcs12: SslPtr): SslPtr;
1538begin
1539 if InitSSLInterface and Assigned(_d2iPKCS12bio) then
1540 Result := _d2iPKCS12bio(b, Pkcs12)
1541 else
1542 Result := nil;
1543end;
1544
1545function PKCS12parse(p12: SslPtr; pass: Ansistring; var pkey, cert, ca: SslPtr): integer;
1546begin
1547 if InitSSLInterface and Assigned(_PKCS12parse) then
1548 Result := _PKCS12parse(p12, SslPtr(pass), pkey, cert, ca)
1549 else
1550 Result := 0;
1551end;
1552
1553procedure PKCS12free(p12: SslPtr);
1554begin
1555 if InitSSLInterface and Assigned(_PKCS12free) then
1556 _PKCS12free(p12);
1557end;
1558
1559function RsaGenerateKey(bits, e: integer; callback: PFunction; cb_arg: SslPtr): PRSA;
1560begin
1561 if InitSSLInterface and Assigned(_RsaGenerateKey) then
1562 Result := _RsaGenerateKey(bits, e, callback, cb_arg)
1563 else
1564 Result := nil;
1565end;
1566
1567function EvpPkeyAssign(pkey: EVP_PKEY; _type: integer; key: Prsa): integer;
1568begin
1569 if InitSSLInterface and Assigned(_EvpPkeyAssign) then
1570 Result := _EvpPkeyAssign(pkey, _type, key)
1571 else
1572 Result := 0;
1573end;
1574
1575function X509SetVersion(x: PX509; version: integer): integer;
1576begin
1577 if InitSSLInterface and Assigned(_X509SetVersion) then
1578 Result := _X509SetVersion(x, version)
1579 else
1580 Result := 0;
1581end;
1582
1583function X509SetPubkey(x: PX509; pkey: EVP_PKEY): integer;
1584begin
1585 if InitSSLInterface and Assigned(_X509SetPubkey) then
1586 Result := _X509SetPubkey(x, pkey)
1587 else
1588 Result := 0;
1589end;
1590
1591function X509SetIssuerName(x: PX509; name: PX509_NAME): integer;
1592begin
1593 if InitSSLInterface and Assigned(_X509SetIssuerName) then
1594 Result := _X509SetIssuerName(x, name)
1595 else
1596 Result := 0;
1597end;
1598
1599function X509NameAddEntryByTxt(name: PX509_NAME; field: Ansistring; _type: integer;
1600 bytes: Ansistring; len, loc, _set: integer): integer;
1601begin
1602 if InitSSLInterface and Assigned(_X509NameAddEntryByTxt) then
1603 Result := _X509NameAddEntryByTxt(name, PAnsiChar(field), _type, PAnsiChar(Bytes), len, loc, _set)
1604 else
1605 Result := 0;
1606end;
1607
1608function X509Sign(x: PX509; pkey: EVP_PKEY; const md: PEVP_MD): integer;
1609begin
1610 if InitSSLInterface and Assigned(_X509Sign) then
1611 Result := _X509Sign(x, pkey, md)
1612 else
1613 Result := 0;
1614end;
1615
1616function Asn1UtctimeNew: PASN1_UTCTIME;
1617begin
1618 if InitSSLInterface and Assigned(_Asn1UtctimeNew) then
1619 Result := _Asn1UtctimeNew
1620 else
1621 Result := nil;
1622end;
1623
1624procedure Asn1UtctimeFree(a: PASN1_UTCTIME);
1625begin
1626 if InitSSLInterface and Assigned(_Asn1UtctimeFree) then
1627 _Asn1UtctimeFree(a);
1628end;
1629
1630function X509GmtimeAdj(s: PASN1_UTCTIME; adj: integer): PASN1_UTCTIME;
1631begin
1632 if InitSSLInterface and Assigned(_X509GmtimeAdj) then
1633 Result := _X509GmtimeAdj(s, adj)
1634 else
1635 Result := nil;
1636end;
1637
1638function X509SetNotBefore(x: PX509; tm: PASN1_UTCTIME): integer;
1639begin
1640 if InitSSLInterface and Assigned(_X509SetNotBefore) then
1641 Result := _X509SetNotBefore(x, tm)
1642 else
1643 Result := 0;
1644end;
1645
1646function X509SetNotAfter(x: PX509; tm: PASN1_UTCTIME): integer;
1647begin
1648 if InitSSLInterface and Assigned(_X509SetNotAfter) then
1649 Result := _X509SetNotAfter(x, tm)
1650 else
1651 Result := 0;
1652end;
1653
1654function i2dX509bio(b: PBIO; x: PX509): integer;
1655begin
1656 if InitSSLInterface and Assigned(_i2dX509bio) then
1657 Result := _i2dX509bio(b, x)
1658 else
1659 Result := 0;
1660end;
1661
1662function d2iX509bio(b: PBIO; x: PX509): PX509; {pf}
1663begin
1664 if InitSSLInterface and Assigned(_d2iX509bio) then
1665 Result := _d2iX509bio(x,b)
1666 else
1667 Result := nil;
1668end;
1669
1670function PEMReadBioX509(b:PBIO; {var x:PX509;}x:PSslPtr; callback:PFunction; cb_arg: SslPtr): PX509; {pf}
1671begin
1672 if InitSSLInterface and Assigned(_PEMReadBioX509) then
1673 Result := _PEMReadBioX509(b,x,callback,cb_arg)
1674 else
1675 Result := nil;
1676end;
1677
1678procedure SkX509PopFree(st: PSTACK; func:TSkPopFreeFunc); {pf}
1679begin
1680 if InitSSLInterface and Assigned(_SkX509PopFree) then
1681 _SkX509PopFree(st,func);
1682end;
1683
1684function i2dPrivateKeyBio(b: PBIO; pkey: EVP_PKEY): integer;
1685begin
1686 if InitSSLInterface and Assigned(_i2dPrivateKeyBio) then
1687 Result := _i2dPrivateKeyBio(b, pkey)
1688 else
1689 Result := 0;
1690end;
1691
1692function EvpGetDigestByName(Name: AnsiString): PEVP_MD;
1693begin
1694 if InitSSLInterface and Assigned(_EvpGetDigestByName) then
1695 Result := _EvpGetDigestByName(PAnsiChar(Name))
1696 else
1697 Result := nil;
1698end;
1699
1700function Asn1IntegerSet(a: PASN1_INTEGER; v: integer): integer;
1701begin
1702 if InitSSLInterface and Assigned(_Asn1IntegerSet) then
1703 Result := _Asn1IntegerSet(a, v)
1704 else
1705 Result := 0;
1706end;
1707
1708function Asn1IntegerGet(a: PASN1_INTEGER): integer; {pf}
1709begin
1710 if InitSSLInterface and Assigned(_Asn1IntegerGet) then
1711 Result := _Asn1IntegerGet(a)
1712 else
1713 Result := 0;
1714end;
1715
1716function X509GetSerialNumber(x: PX509): PASN1_INTEGER;
1717begin
1718 if InitSSLInterface and Assigned(_X509GetSerialNumber) then
1719 Result := _X509GetSerialNumber(x)
1720 else
1721 Result := nil;
1722end;
1723
1724// 3DES functions
1725procedure DESsetoddparity(Key: des_cblock);
1726begin
1727 if InitSSLInterface and Assigned(_DESsetoddparity) then
1728 _DESsetoddparity(Key);
1729end;
1730
1731function DESsetkeychecked(key: des_cblock; schedule: des_key_schedule): Integer;
1732begin
1733 if InitSSLInterface and Assigned(_DESsetkeychecked) then
1734 Result := _DESsetkeychecked(key, schedule)
1735 else
1736 Result := -1;
1737end;
1738
1739procedure DESecbencrypt(Input: des_cblock; output: des_cblock; ks: des_key_schedule; enc: Integer);
1740begin
1741 if InitSSLInterface and Assigned(_DESecbencrypt) then
1742 _DESecbencrypt(Input, output, ks, enc);
1743end;
1744
1745procedure locking_callback(mode, ltype: integer; lfile: PChar; line: integer); cdecl;
1746begin
1747 if (mode and 1) > 0 then
1748 TCriticalSection(Locks[ltype]).Enter
1749 else
1750 TCriticalSection(Locks[ltype]).Leave;
1751end;
1752
1753procedure InitLocks;
1754var
1755 n: integer;
1756 max: integer;
1757begin
1758 Locks := TList.Create;
1759 max := _CRYPTOnumlocks;
1760 for n := 1 to max do
1761 Locks.Add(TCriticalSection.Create);
1762 _CRYPTOsetlockingcallback(@locking_callback);
1763end;
1764
1765procedure FreeLocks;
1766var
1767 n: integer;
1768begin
1769 _CRYPTOsetlockingcallback(nil);
1770 for n := 0 to Locks.Count - 1 do
1771 TCriticalSection(Locks[n]).Free;
1772 Locks.Free;
1773end;
1774
1775{$ENDIF}
1776
1777function LoadLib(const Value: String): HModule;
1778begin
1779{$IFDEF CIL}
1780 Result := LoadLibrary(Value);
1781{$ELSE}
1782 Result := LoadLibrary(PChar(Value));
1783{$ENDIF}
1784end;
1785
1786function GetProcAddr(module: HModule; const ProcName: string): SslPtr;
1787begin
1788{$IFDEF CIL}
1789 Result := GetProcAddress(module, ProcName);
1790{$ELSE}
1791 Result := GetProcAddress(module, PChar(ProcName));
1792{$ENDIF}
1793end;
1794
1795function InitSSLInterface: Boolean;
1796var
1797 s: string;
1798 x: integer;
1799begin
1800 {pf}
1801 if SSLLoaded then
1802 begin
1803 Result := TRUE;
1804 exit;
1805 end;
1806 {/pf}
1807 SSLCS.Enter;
1808 try
1809 if not IsSSLloaded then
1810 begin
1811{$IFDEF CIL}
1812 SSLLibHandle := 1;
1813 SSLUtilHandle := 1;
1814{$ELSE}
1815 SSLLibHandle := LoadLib(DLLSSLName);
1816 SSLUtilHandle := LoadLib(DLLUtilName);
1817 {$IFDEF MSWINDOWS}
1818 if (SSLLibHandle = 0) then
1819 SSLLibHandle := LoadLib(DLLSSLName2);
1820 {$ENDIF}
1821{$ENDIF}
1822 if (SSLLibHandle <> 0) and (SSLUtilHandle <> 0) then
1823 begin
1824{$IFNDEF CIL}
1825 _SslGetError := GetProcAddr(SSLLibHandle, 'SSL_get_error');
1826 _SslLibraryInit := GetProcAddr(SSLLibHandle, 'SSL_library_init');
1827 _SslLoadErrorStrings := GetProcAddr(SSLLibHandle, 'SSL_load_error_strings');
1828 _SslCtxSetCipherList := GetProcAddr(SSLLibHandle, 'SSL_CTX_set_cipher_list');
1829 _SslCtxNew := GetProcAddr(SSLLibHandle, 'SSL_CTX_new');
1830 _SslCtxFree := GetProcAddr(SSLLibHandle, 'SSL_CTX_free');
1831 _SslSetFd := GetProcAddr(SSLLibHandle, 'SSL_set_fd');
1832 _SslMethodV2 := GetProcAddr(SSLLibHandle, 'SSLv2_method');
1833 _SslMethodV3 := GetProcAddr(SSLLibHandle, 'SSLv3_method');
1834 _SslMethodTLSV1 := GetProcAddr(SSLLibHandle, 'TLSv1_method');
1835 _SslMethodV23 := GetProcAddr(SSLLibHandle, 'SSLv23_method');
1836 _SslCtxUsePrivateKey := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_PrivateKey');
1837 _SslCtxUsePrivateKeyASN1 := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_PrivateKey_ASN1');
1838 //use SSL_CTX_use_RSAPrivateKey_file instead SSL_CTX_use_PrivateKey_file,
1839 //because SSL_CTX_use_PrivateKey_file not support DER format. :-O
1840 _SslCtxUsePrivateKeyFile := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_RSAPrivateKey_file');
1841 _SslCtxUseCertificate := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_certificate');
1842 _SslCtxUseCertificateASN1 := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_certificate_ASN1');
1843 _SslCtxUseCertificateFile := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_certificate_file');
1844 _SslCtxUseCertificateChainFile := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_certificate_chain_file');
1845 _SslCtxCheckPrivateKeyFile := GetProcAddr(SSLLibHandle, 'SSL_CTX_check_private_key');
1846 _SslCtxSetDefaultPasswdCb := GetProcAddr(SSLLibHandle, 'SSL_CTX_set_default_passwd_cb');
1847 _SslCtxSetDefaultPasswdCbUserdata := GetProcAddr(SSLLibHandle, 'SSL_CTX_set_default_passwd_cb_userdata');
1848 _SslCtxLoadVerifyLocations := GetProcAddr(SSLLibHandle, 'SSL_CTX_load_verify_locations');
1849 _SslCtxCtrl := GetProcAddr(SSLLibHandle, 'SSL_CTX_ctrl');
1850 _SslNew := GetProcAddr(SSLLibHandle, 'SSL_new');
1851 _SslFree := GetProcAddr(SSLLibHandle, 'SSL_free');
1852 _SslAccept := GetProcAddr(SSLLibHandle, 'SSL_accept');
1853 _SslConnect := GetProcAddr(SSLLibHandle, 'SSL_connect');
1854 _SslShutdown := GetProcAddr(SSLLibHandle, 'SSL_shutdown');
1855 _SslRead := GetProcAddr(SSLLibHandle, 'SSL_read');
1856 _SslPeek := GetProcAddr(SSLLibHandle, 'SSL_peek');
1857 _SslWrite := GetProcAddr(SSLLibHandle, 'SSL_write');
1858 _SslPending := GetProcAddr(SSLLibHandle, 'SSL_pending');
1859 _SslGetPeerCertificate := GetProcAddr(SSLLibHandle, 'SSL_get_peer_certificate');
1860 _SslGetVersion := GetProcAddr(SSLLibHandle, 'SSL_get_version');
1861 _SslCtxSetVerify := GetProcAddr(SSLLibHandle, 'SSL_CTX_set_verify');
1862 _SslGetCurrentCipher := GetProcAddr(SSLLibHandle, 'SSL_get_current_cipher');
1863 _SslCipherGetName := GetProcAddr(SSLLibHandle, 'SSL_CIPHER_get_name');
1864 _SslCipherGetBits := GetProcAddr(SSLLibHandle, 'SSL_CIPHER_get_bits');
1865 _SslGetVerifyResult := GetProcAddr(SSLLibHandle, 'SSL_get_verify_result');
1866 _SslCtrl := GetProcAddr(SSLLibHandle, 'SSL_ctrl');
1867
1868 _X509New := GetProcAddr(SSLUtilHandle, 'X509_new');
1869 _X509Free := GetProcAddr(SSLUtilHandle, 'X509_free');
1870 _X509NameOneline := GetProcAddr(SSLUtilHandle, 'X509_NAME_oneline');
1871 _X509GetSubjectName := GetProcAddr(SSLUtilHandle, 'X509_get_subject_name');
1872 _X509GetIssuerName := GetProcAddr(SSLUtilHandle, 'X509_get_issuer_name');
1873 _X509NameHash := GetProcAddr(SSLUtilHandle, 'X509_NAME_hash');
1874 _X509Digest := GetProcAddr(SSLUtilHandle, 'X509_digest');
1875 _X509print := GetProcAddr(SSLUtilHandle, 'X509_print');
1876 _X509SetVersion := GetProcAddr(SSLUtilHandle, 'X509_set_version');
1877 _X509SetPubkey := GetProcAddr(SSLUtilHandle, 'X509_set_pubkey');
1878 _X509SetIssuerName := GetProcAddr(SSLUtilHandle, 'X509_set_issuer_name');
1879 _X509NameAddEntryByTxt := GetProcAddr(SSLUtilHandle, 'X509_NAME_add_entry_by_txt');
1880 _X509Sign := GetProcAddr(SSLUtilHandle, 'X509_sign');
1881 _X509GmtimeAdj := GetProcAddr(SSLUtilHandle, 'X509_gmtime_adj');
1882 _X509SetNotBefore := GetProcAddr(SSLUtilHandle, 'X509_set_notBefore');
1883 _X509SetNotAfter := GetProcAddr(SSLUtilHandle, 'X509_set_notAfter');
1884 _X509GetSerialNumber := GetProcAddr(SSLUtilHandle, 'X509_get_serialNumber');
1885 _EvpPkeyNew := GetProcAddr(SSLUtilHandle, 'EVP_PKEY_new');
1886 _EvpPkeyFree := GetProcAddr(SSLUtilHandle, 'EVP_PKEY_free');
1887 _EvpPkeyAssign := GetProcAddr(SSLUtilHandle, 'EVP_PKEY_assign');
1888 _EVPCleanup := GetProcAddr(SSLUtilHandle, 'EVP_cleanup');
1889 _EvpGetDigestByName := GetProcAddr(SSLUtilHandle, 'EVP_get_digestbyname');
1890 _SSLeayversion := GetProcAddr(SSLUtilHandle, 'SSLeay_version');
1891 _ErrErrorString := GetProcAddr(SSLUtilHandle, 'ERR_error_string_n');
1892 _ErrGetError := GetProcAddr(SSLUtilHandle, 'ERR_get_error');
1893 _ErrClearError := GetProcAddr(SSLUtilHandle, 'ERR_clear_error');
1894 _ErrFreeStrings := GetProcAddr(SSLUtilHandle, 'ERR_free_strings');
1895 _ErrRemoveState := GetProcAddr(SSLUtilHandle, 'ERR_remove_state');
1896 _OPENSSLaddallalgorithms := GetProcAddr(SSLUtilHandle, 'OPENSSL_add_all_algorithms_noconf');
1897 _CRYPTOcleanupAllExData := GetProcAddr(SSLUtilHandle, 'CRYPTO_cleanup_all_ex_data');
1898 _RandScreen := GetProcAddr(SSLUtilHandle, 'RAND_screen');
1899 _BioNew := GetProcAddr(SSLUtilHandle, 'BIO_new');
1900 _BioFreeAll := GetProcAddr(SSLUtilHandle, 'BIO_free_all');
1901 _BioSMem := GetProcAddr(SSLUtilHandle, 'BIO_s_mem');
1902 _BioCtrlPending := GetProcAddr(SSLUtilHandle, 'BIO_ctrl_pending');
1903 _BioRead := GetProcAddr(SSLUtilHandle, 'BIO_read');
1904 _BioWrite := GetProcAddr(SSLUtilHandle, 'BIO_write');
1905 _d2iPKCS12bio := GetProcAddr(SSLUtilHandle, 'd2i_PKCS12_bio');
1906 _PKCS12parse := GetProcAddr(SSLUtilHandle, 'PKCS12_parse');
1907 _PKCS12free := GetProcAddr(SSLUtilHandle, 'PKCS12_free');
1908 _RsaGenerateKey := GetProcAddr(SSLUtilHandle, 'RSA_generate_key');
1909 _Asn1UtctimeNew := GetProcAddr(SSLUtilHandle, 'ASN1_UTCTIME_new');
1910 _Asn1UtctimeFree := GetProcAddr(SSLUtilHandle, 'ASN1_UTCTIME_free');
1911 _Asn1IntegerSet := GetProcAddr(SSLUtilHandle, 'ASN1_INTEGER_set');
1912 _Asn1IntegerGet := GetProcAddr(SSLUtilHandle, 'ASN1_INTEGER_get'); {pf}
1913 _i2dX509bio := GetProcAddr(SSLUtilHandle, 'i2d_X509_bio');
1914 _d2iX509bio := GetProcAddr(SSLUtilHandle, 'd2i_X509_bio'); {pf}
1915 _PEMReadBioX509 := GetProcAddr(SSLUtilHandle, 'PEM_read_bio_X509'); {pf}
1916 _SkX509PopFree := GetProcAddr(SSLUtilHandle, 'SK_X509_POP_FREE'); {pf}
1917 _i2dPrivateKeyBio := GetProcAddr(SSLUtilHandle, 'i2d_PrivateKey_bio');
1918
1919 // 3DES functions
1920 _DESsetoddparity := GetProcAddr(SSLUtilHandle, 'DES_set_odd_parity');
1921 _DESsetkeychecked := GetProcAddr(SSLUtilHandle, 'DES_set_key_checked');
1922 _DESecbencrypt := GetProcAddr(SSLUtilHandle, 'DES_ecb_encrypt');
1923 //
1924 _CRYPTOnumlocks := GetProcAddr(SSLUtilHandle, 'CRYPTO_num_locks');
1925 _CRYPTOsetlockingcallback := GetProcAddr(SSLUtilHandle, 'CRYPTO_set_locking_callback');
1926{$ENDIF}
1927{$IFDEF CIL}
1928 SslLibraryInit;
1929 SslLoadErrorStrings;
1930 OPENSSLaddallalgorithms;
1931 RandScreen;
1932{$ELSE}
1933 SetLength(s, 1024);
1934 x := GetModuleFilename(SSLLibHandle,PChar(s),Length(s));
1935 SetLength(s, x);
1936 SSLLibFile := s;
1937 SetLength(s, 1024);
1938 x := GetModuleFilename(SSLUtilHandle,PChar(s),Length(s));
1939 SetLength(s, x);
1940 SSLUtilFile := s;
1941 //init library
1942 if assigned(_SslLibraryInit) then
1943 _SslLibraryInit;
1944 if assigned(_SslLoadErrorStrings) then
1945 _SslLoadErrorStrings;
1946 if assigned(_OPENSSLaddallalgorithms) then
1947 _OPENSSLaddallalgorithms;
1948 if assigned(_RandScreen) then
1949 _RandScreen;
1950 if assigned(_CRYPTOnumlocks) and assigned(_CRYPTOsetlockingcallback) then
1951 InitLocks;
1952{$ENDIF}
1953 Result := True;
1954 SSLloaded := True;
1955 end
1956 else
1957 begin
1958 //load failed!
1959 if SSLLibHandle <> 0 then
1960 begin
1961{$IFNDEF CIL}
1962 FreeLibrary(SSLLibHandle);
1963{$ENDIF}
1964 SSLLibHandle := 0;
1965 end;
1966 if SSLUtilHandle <> 0 then
1967 begin
1968{$IFNDEF CIL}
1969 FreeLibrary(SSLUtilHandle);
1970{$ENDIF}
1971 SSLLibHandle := 0;
1972 end;
1973 Result := False;
1974 end;
1975 end
1976 else
1977 //loaded before...
1978 Result := true;
1979 finally
1980 SSLCS.Leave;
1981 end;
1982end;
1983
1984function DestroySSLInterface: Boolean;
1985begin
1986 SSLCS.Enter;
1987 try
1988 if IsSSLLoaded then
1989 begin
1990 //deinit library
1991{$IFNDEF CIL}
1992 if assigned(_CRYPTOnumlocks) and assigned(_CRYPTOsetlockingcallback) then
1993 FreeLocks;
1994{$ENDIF}
1995 EVPCleanup;
1996 CRYPTOcleanupAllExData;
1997 ErrRemoveState(0);
1998 end;
1999 SSLloaded := false;
2000 if SSLLibHandle <> 0 then
2001 begin
2002{$IFNDEF CIL}
2003 FreeLibrary(SSLLibHandle);
2004{$ENDIF}
2005 SSLLibHandle := 0;
2006 end;
2007 if SSLUtilHandle <> 0 then
2008 begin
2009{$IFNDEF CIL}
2010 FreeLibrary(SSLUtilHandle);
2011{$ENDIF}
2012 SSLLibHandle := 0;
2013 end;
2014
2015{$IFNDEF CIL}
2016 _SslGetError := nil;
2017 _SslLibraryInit := nil;
2018 _SslLoadErrorStrings := nil;
2019 _SslCtxSetCipherList := nil;
2020 _SslCtxNew := nil;
2021 _SslCtxFree := nil;
2022 _SslSetFd := nil;
2023 _SslMethodV2 := nil;
2024 _SslMethodV3 := nil;
2025 _SslMethodTLSV1 := nil;
2026 _SslMethodV23 := nil;
2027 _SslCtxUsePrivateKey := nil;
2028 _SslCtxUsePrivateKeyASN1 := nil;
2029 _SslCtxUsePrivateKeyFile := nil;
2030 _SslCtxUseCertificate := nil;
2031 _SslCtxUseCertificateASN1 := nil;
2032 _SslCtxUseCertificateFile := nil;
2033 _SslCtxUseCertificateChainFile := nil;
2034 _SslCtxCheckPrivateKeyFile := nil;
2035 _SslCtxSetDefaultPasswdCb := nil;
2036 _SslCtxSetDefaultPasswdCbUserdata := nil;
2037 _SslCtxLoadVerifyLocations := nil;
2038 _SslCtxCtrl := nil;
2039 _SslNew := nil;
2040 _SslFree := nil;
2041 _SslAccept := nil;
2042 _SslConnect := nil;
2043 _SslShutdown := nil;
2044 _SslRead := nil;
2045 _SslPeek := nil;
2046 _SslWrite := nil;
2047 _SslPending := nil;
2048 _SslGetPeerCertificate := nil;
2049 _SslGetVersion := nil;
2050 _SslCtxSetVerify := nil;
2051 _SslGetCurrentCipher := nil;
2052 _SslCipherGetName := nil;
2053 _SslCipherGetBits := nil;
2054 _SslGetVerifyResult := nil;
2055 _SslCtrl := nil;
2056
2057 _X509New := nil;
2058 _X509Free := nil;
2059 _X509NameOneline := nil;
2060 _X509GetSubjectName := nil;
2061 _X509GetIssuerName := nil;
2062 _X509NameHash := nil;
2063 _X509Digest := nil;
2064 _X509print := nil;
2065 _X509SetVersion := nil;
2066 _X509SetPubkey := nil;
2067 _X509SetIssuerName := nil;
2068 _X509NameAddEntryByTxt := nil;
2069 _X509Sign := nil;
2070 _X509GmtimeAdj := nil;
2071 _X509SetNotBefore := nil;
2072 _X509SetNotAfter := nil;
2073 _X509GetSerialNumber := nil;
2074 _EvpPkeyNew := nil;
2075 _EvpPkeyFree := nil;
2076 _EvpPkeyAssign := nil;
2077 _EVPCleanup := nil;
2078 _EvpGetDigestByName := nil;
2079 _SSLeayversion := nil;
2080 _ErrErrorString := nil;
2081 _ErrGetError := nil;
2082 _ErrClearError := nil;
2083 _ErrFreeStrings := nil;
2084 _ErrRemoveState := nil;
2085 _OPENSSLaddallalgorithms := nil;
2086 _CRYPTOcleanupAllExData := nil;
2087 _RandScreen := nil;
2088 _BioNew := nil;
2089 _BioFreeAll := nil;
2090 _BioSMem := nil;
2091 _BioCtrlPending := nil;
2092 _BioRead := nil;
2093 _BioWrite := nil;
2094 _d2iPKCS12bio := nil;
2095 _PKCS12parse := nil;
2096 _PKCS12free := nil;
2097 _RsaGenerateKey := nil;
2098 _Asn1UtctimeNew := nil;
2099 _Asn1UtctimeFree := nil;
2100 _Asn1IntegerSet := nil;
2101 _Asn1IntegerGet := nil; {pf}
2102 _SkX509PopFree := nil; {pf}
2103 _i2dX509bio := nil;
2104 _i2dPrivateKeyBio := nil;
2105
2106 // 3DES functions
2107 _DESsetoddparity := nil;
2108 _DESsetkeychecked := nil;
2109 _DESecbencrypt := nil;
2110 //
2111 _CRYPTOnumlocks := nil;
2112 _CRYPTOsetlockingcallback := nil;
2113{$ENDIF}
2114 finally
2115 SSLCS.Leave;
2116 end;
2117 Result := True;
2118end;
2119
2120function IsSSLloaded: Boolean;
2121begin
2122 Result := SSLLoaded;
2123end;
2124
2125initialization
2126begin
2127 SSLCS:= TCriticalSection.Create;
2128end;
2129
2130finalization
2131begin
2132{$IFNDEF CIL}
2133 DestroySSLInterface;
2134{$ENDIF}
2135 SSLCS.Free;
2136end;
2137
2138end.
Note: See TracBrowser for help on using the repository browser.