ssl_openssl_lib.pas 78 KB

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