ssl_openssl_lib.pas 72 KB

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