openssl.pas 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520
  1. unit OpenSSL;
  2. {==============================================================================|
  3. | Project : Ararat Synapse | 003.004.001 |
  4. |==============================================================================|
  5. | Content: SSL support by OpenSSL |
  6. |==============================================================================|
  7. | Copyright (c)1999-2005, Lukas Gebauer |
  8. | All rights reserved. |
  9. | |
  10. | Redistribution and use in source and binary forms, with or without |
  11. | modification, are permitted provided that the following conditions are met: |
  12. | |
  13. | Redistributions of source code must retain the above copyright notice, this |
  14. | list of conditions and the following disclaimer. |
  15. | |
  16. | Redistributions in binary form must reproduce the above copyright notice, |
  17. | this list of conditions and the following disclaimer in the documentation |
  18. | and/or other materials provided with the distribution. |
  19. | |
  20. | Neither the name of Lukas Gebauer nor the names of its contributors may |
  21. | be used to endorse or promote products derived from this software without |
  22. | specific prior written permission. |
  23. | |
  24. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
  25. | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
  26. | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
  27. | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
  28. | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
  29. | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
  30. | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
  31. | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  32. | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
  33. | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
  34. | DAMAGE. |
  35. |==============================================================================|
  36. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  37. | Portions created by Lukas Gebauer are Copyright (c)2002-2005. |
  38. | All Rights Reserved. |
  39. |==============================================================================|
  40. | Contributor(s): |
  41. |==============================================================================|
  42. | FreePascal basic cleanup (original worked too): Ales Katona |
  43. | WARNING: due to reliance on some units, I have removed the ThreadLocks init |
  44. | if need be, it should be re-added, or handled by the |
  45. | OS threading init somehow |
  46. | |
  47. | 2010 - Felipe Monteiro de Carvalho - Many improvements |
  48. |==============================================================================|
  49. | History: see HISTORY.HTM from distribution package |
  50. | (Found at URL: http://www.ararat.cz/synapse/) |
  51. |==============================================================================}
  52. {
  53. Special thanks to Gregor Ibic <[email protected]>
  54. (Intelicom d.o.o., http://www.intelicom.si)
  55. for good inspiration about begin with SSL programming.
  56. }
  57. {$MODE DELPHI}{$H+}
  58. {$Packrecords C}
  59. {:@abstract(OpenSSL support)
  60. This unit is Pascal interface to OpenSSL library (used by @link(ssl_openssl) unit).
  61. OpenSSL is loaded dynamicly on-demand. If this library is not found in system,
  62. requested OpenSSL function just return errorcode.
  63. }
  64. interface
  65. uses
  66. DynLibs, cTypes, SysUtils;
  67. var
  68. {$IFDEF WINDOWS}
  69. DLLSSLName: string = 'ssleay32.dll';
  70. DLLSSLName2: string = 'libssl32.dll';
  71. DLLUtilName: string = 'libeay32.dll';
  72. {$ELSE}
  73. DLLSSLName: string = 'libssl';
  74. DLLUtilName: string = 'libcrypto';
  75. { ADD NEW ONES WHEN THEY APPEAR!
  76. Always make .so/dylib first, then versions, in descending order!
  77. Add "." .before the version, first is always just "" }
  78. DLLVersions: array[1..10] of string = ('', '.1.0.0', '.0.9.8',
  79. '.0.9.7', '.0.9.6', '.0.9.5', '.0.9.4',
  80. '.0.9.3', '.0.9.2', '.0.9.1');
  81. {$ENDIF}
  82. const
  83. // EVP.h Constants
  84. EVP_MAX_MD_SIZE = 64; //* longest known is SHA512 */
  85. EVP_MAX_KEY_LENGTH = 32;
  86. EVP_MAX_IV_LENGTH = 16;
  87. EVP_MAX_BLOCK_LENGTH = 32;
  88. type
  89. SslPtr = Pointer;
  90. PSslPtr = ^SslPtr;
  91. PSSL_CTX = SslPtr;
  92. PSSL = SslPtr;
  93. PSSL_METHOD = SslPtr;
  94. PX509 = SslPtr;
  95. PX509_NAME = SslPtr;
  96. PEVP_MD = SslPtr;
  97. PBIO_METHOD = SslPtr;
  98. PBIO = SslPtr;
  99. EVP_PKEY = SslPtr;
  100. PRSA = SslPtr;
  101. PPRSA = ^PRSA;
  102. PASN1_UTCTIME = SslPtr;
  103. PASN1_cInt = SslPtr;
  104. PPasswdCb = SslPtr;
  105. PFunction = procedure;
  106. DES_cblock = array[0..7] of Byte;
  107. PDES_cblock = ^DES_cblock;
  108. des_ks_struct = packed record
  109. ks: DES_cblock;
  110. weak_key: cInt;
  111. end;
  112. des_key_schedule = array[1..16] of des_ks_struct;
  113. // Rand
  114. RAND_METHOD = record
  115. end;
  116. PRAND_METHOD = ^RAND_METHOD;
  117. // RSA
  118. PENGINE = Pointer;
  119. PBIGNUM = Pointer;
  120. PBN_GENCB = Pointer;
  121. PBN_MONT_CTX = Pointer;
  122. PBN_CTX = Pointer;
  123. PPByte = ^PByte;
  124. Trsa_pub_enc = function(flen: cint;
  125. const from_, to_: PByte; arsa: PRSA; padding: cint): cint;
  126. Trsa_pub_dec = function(flen: cint;
  127. const from_, to_: PByte; arsa: PRSA; padding: cint): cint;
  128. Trsa_priv_enc = function(flen: cint;
  129. const from_, to_: PByte; arsa: PRSA; padding: cint): cint;
  130. Trsa_priv_dec = function(flen: cint;
  131. const from_, to_: PByte; arsa: PRSA; padding: cint): cint;
  132. Trsa_mod_exp = function(r0: PBIGNUM; const l: PBIGNUM; arsa: PRSA; ctx: PBN_CTX): cint;
  133. Tbn_mod_exp = function(r: PBIGNUM; const a, p, m: PBIGNUM; arsa: PRSA;
  134. ctx: PBN_CTX; m_ctx: PBN_MONT_CTX): cint;
  135. Tinit = function(arsa: PRSA): cint;
  136. Tfinish = function(arsa: PRSA): cint;
  137. Trsa_sign = function(type_: cint; const m: PByte; m_length: cuint;
  138. sigret: PByte; siglen: pcuint; arsa: PRSA): cint;
  139. Trsa_verify = function(dtype: cint;
  140. const m: PByte; m_length: cuint;
  141. const sigbuf: PByte; siglen: cuint; arsa: PRSA): cint;
  142. Trsa_keygen = function(arsa: PRSA; bits: cint; e: PBIGNUM; cb: PBN_GENCB): cint;
  143. RSA_METHOD = record
  144. name: PChar;
  145. rsa_pub_enc: Trsa_pub_enc;
  146. rsa_pub_dec: Trsa_pub_dec;
  147. rsa_priv_enc: Trsa_priv_enc;
  148. rsa_priv_dec: Trsa_priv_dec;
  149. rsa_mod_exp: Trsa_mod_exp; { Can be null }
  150. bn_mod_exp: Tbn_mod_exp; { Can be null }
  151. init: Tinit; { called at new }
  152. finish: Tfinish; { called at free }
  153. flags: cint; { RSA_METHOD_FLAG_* things }
  154. app_data: PChar; { may be needed! }
  155. { New sign and verify functions: some libraries don't allow arbitrary data
  156. * to be signed/verified: this allows them to be used. Note: for this to work
  157. * the RSA_public_decrypt() and RSA_private_encrypt() should *NOT* be used
  158. * RSA_sign(), RSA_verify() should be used instead. Note: for backwards
  159. * compatibility this functionality is only enabled if the RSA_FLAG_SIGN_VER
  160. * option is set in 'flags'.
  161. }
  162. rsa_sign: Trsa_sign;
  163. rsa_verify: Trsa_verify;
  164. { If this callback is NULL, the builtin software RSA key-gen will be used. This
  165. * is for behavioural compatibility whilst the code gets rewired, but one day
  166. * it would be nice to assume there are no such things as "builtin software"
  167. * implementations. }
  168. rsa_keygen: Trsa_keygen;
  169. end;
  170. PRSA_METHOD = ^RSA_METHOD;
  171. // EVP
  172. PEVP_MD_CTX = Pointer;
  173. PEVP_CIPHER_CTX = ^EVP_CIPHER_CTX;
  174. PEVP_CIPHER = ^EVP_CIPHER;
  175. PASN1_TYPE = Pointer;
  176. EVP_CIPHER_INIT_FUNC = function(ctx: PEVP_CIPHER_CTX; const key, iv: PByte; enc: cint): cint; cdecl;
  177. EVP_CIPHER_DO_CIPHER_FUNC = function(ctx: PEVP_CIPHER_CTX; out_data: PByte; const in_data: PByte; inl: csize_t): cint; cdecl;
  178. EVP_CIPHER_CLEANUP_FUNC = function(ctx: PEVP_CIPHER_CTX): cint; cdecl;
  179. EVP_CIPHER_SET_ASN1_PARAMETERS_FUNC = function(ctx: PEVP_CIPHER_CTX; asn1_type: PASN1_TYPE): cint; cdecl;
  180. EVP_CIPHER_GET_ASN1_PARAMETERS_FUNC = function(ctx: PEVP_CIPHER_CTX; asn1_type: PASN1_TYPE): cint; cdecl;
  181. EVP_CIPHER_CTRL_FUNC = function(ctx: PEVP_CIPHER_CTX; type_, arg: cint; ptr: Pointer): cint; cdecl;
  182. EVP_CIPHER = record // Updated with EVP.h from OpenSSL 1.0.0
  183. nid: cint;
  184. block_size: cint;
  185. key_len: cint; //* Default value for variable length ciphers */
  186. iv_len: cint;
  187. flags: culong; //* Various flags */
  188. init: EVP_CIPHER_INIT_FUNC; //* init key */
  189. do_cipher: EVP_CIPHER_DO_CIPHER_FUNC;//* encrypt/decrypt data */
  190. cleanup: EVP_CIPHER_CLEANUP_FUNC; //* cleanup ctx */
  191. ctx_size: cint; //* how big ctx->cipher_data needs to be */
  192. set_asn1_parameters: EVP_CIPHER_SET_ASN1_PARAMETERS_FUNC; //* Populate a ASN1_TYPE with parameters */
  193. get_asn1_parameters: EVP_CIPHER_GET_ASN1_PARAMETERS_FUNC; //* Get parameters from a ASN1_TYPE */
  194. ctrl: EVP_CIPHER_CTRL_FUNC; //* Miscellaneous operations */
  195. app_data: Pointer; //* Application data */
  196. end;
  197. EVP_CIPHER_CTX = record // Updated with EVP.h from OpenSSL 1.0.0
  198. cipher: PEVP_CIPHER;
  199. engine: PENGINE; //* functional reference if 'cipher' is ENGINE-provided */
  200. encrypt: cint; //* encrypt or decrypt */
  201. buf_len: cint; //* number we have left */
  202. oiv: array[0..EVP_MAX_IV_LENGTH-1] of Byte; //* original iv */
  203. iv: array[0..EVP_MAX_IV_LENGTH-1] of Byte; //* working iv */
  204. buf: array[0..EVP_MAX_IV_LENGTH-1] of Byte; //* saved partial block */
  205. num: cint; //* used by cfb/ofb mode */
  206. app_data: Pointer; //* application stuff */
  207. key_len: cint; //* May change for variable length cipher */
  208. flags: culong; //* Various flags */
  209. cipher_data: Pointer; //* per EVP data */
  210. final_used: cint;
  211. block_mask: cint;
  212. final: array[0..EVP_MAX_BLOCK_LENGTH-1] of Byte; //* possible final block */
  213. final2: array[0..$1FFF] of Byte; // Extra storage space, otherwise an access violation
  214. // in the OpenSSL library will occur
  215. end;
  216. const
  217. SSL_ERROR_NONE = 0;
  218. SSL_ERROR_SSL = 1;
  219. SSL_ERROR_WANT_READ = 2;
  220. SSL_ERROR_WANT_WRITE = 3;
  221. SSL_ERROR_WANT_X509_LOOKUP = 4;
  222. SSL_ERROR_SYSCALL = 5; //look at error stack/return value/errno
  223. SSL_ERROR_ZERO_RETURN = 6;
  224. SSL_ERROR_WANT_CONNECT = 7;
  225. SSL_ERROR_WANT_ACCEPT = 8;
  226. SSL_CTRL_NEED_TMP_RSA = 1;
  227. SSL_CTRL_SET_TMP_RSA = 2;
  228. SSL_CTRL_SET_TMP_DH = 3;
  229. SSL_CTRL_SET_TMP_ECDH = 4;
  230. SSL_CTRL_SET_TMP_RSA_CB = 5;
  231. SSL_CTRL_SET_TMP_DH_CB = 6;
  232. SSL_CTRL_SET_TMP_ECDH_CB = 7;
  233. SSL_CTRL_GET_SESSION_REUSED = 8;
  234. SSL_CTRL_GET_CLIENT_CERT_REQUEST = 9;
  235. SSL_CTRL_GET_NUM_RENEGOTIATIONS = 10;
  236. SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS = 11;
  237. SSL_CTRL_GET_TOTAL_RENEGOTIATIONS = 12;
  238. SSL_CTRL_GET_FLAGS = 13;
  239. SSL_CTRL_EXTRA_CHAIN_CERT = 14;
  240. SSL_CTRL_SET_MSG_CALLBACK = 15;
  241. SSL_CTRL_SET_MSG_CALLBACK_ARG = 16;
  242. { only applies to datagram connections }
  243. SSL_CTRL_SET_MTU = 17;
  244. { Stats }
  245. SSL_CTRL_SESS_NUMBER = 20;
  246. SSL_CTRL_SESS_CONNECT = 21;
  247. SSL_CTRL_SESS_CONNECT_GOOD = 22;
  248. SSL_CTRL_SESS_CONNECT_RENEGOTIATE = 23;
  249. SSL_CTRL_SESS_ACCEPT = 24;
  250. SSL_CTRL_SESS_ACCEPT_GOOD = 25;
  251. SSL_CTRL_SESS_ACCEPT_RENEGOTIATE = 26;
  252. SSL_CTRL_SESS_HIT = 27;
  253. SSL_CTRL_SESS_CB_HIT = 28;
  254. SSL_CTRL_SESS_MISSES = 29;
  255. SSL_CTRL_SESS_TIMEOUTS = 30;
  256. SSL_CTRL_SESS_CACHE_FULL = 31;
  257. SSL_CTRL_OPTIONS = 32;
  258. SSL_CTRL_MODE = 33;
  259. SSL_CTRL_GET_READ_AHEAD = 40;
  260. SSL_CTRL_SET_READ_AHEAD = 41;
  261. SSL_CTRL_SET_SESS_CACHE_SIZE = 42;
  262. SSL_CTRL_GET_SESS_CACHE_SIZE = 43;
  263. SSL_CTRL_SET_SESS_CACHE_MODE = 44;
  264. SSL_CTRL_GET_SESS_CACHE_MODE = 45;
  265. SSL_CTRL_GET_MAX_CERT_LIST = 50;
  266. SSL_CTRL_SET_MAX_CERT_LIST = 51;
  267. {* Allow SSL_write(..., n) to return r with 0 < r < n (i.e. report success
  268. * when just a single record has been written): *}
  269. SSL_MODE_ENABLE_PARTIAL_WRITE = 1;
  270. {* Make it possible to retry SSL_write() with changed buffer location
  271. * (buffer contents must stay the same!); this is not the default to avoid
  272. * the misconception that non-blocking SSL_write() behaves like
  273. * non-blocking write(): *}
  274. SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER = 2;
  275. {* Never bother the application with retries if the transport
  276. * is blocking: *}
  277. SSL_MODE_AUTO_RETRY = 4;
  278. {* Don't attempt to automatically build certificate chain *}
  279. SSL_MODE_NO_AUTO_CHAIN = 8;
  280. SSL_OP_NO_SSLv2 = $01000000;
  281. SSL_OP_NO_SSLv3 = $02000000;
  282. SSL_OP_NO_TLSv1 = $04000000;
  283. SSL_OP_ALL = $000FFFFF;
  284. SSL_VERIFY_NONE = $00;
  285. SSL_VERIFY_PEER = $01;
  286. OPENSSL_DES_DECRYPT = 0;
  287. OPENSSL_DES_ENCRYPT = 1;
  288. X509_V_OK = 0;
  289. X509_V_ILLEGAL = 1;
  290. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT = 2;
  291. X509_V_ERR_UNABLE_TO_GET_CRL = 3;
  292. X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE = 4;
  293. X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE = 5;
  294. X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY = 6;
  295. X509_V_ERR_CERT_SIGNATURE_FAILURE = 7;
  296. X509_V_ERR_CRL_SIGNATURE_FAILURE = 8;
  297. X509_V_ERR_CERT_NOT_YET_VALID = 9;
  298. X509_V_ERR_CERT_HAS_EXPIRED = 10;
  299. X509_V_ERR_CRL_NOT_YET_VALID = 11;
  300. X509_V_ERR_CRL_HAS_EXPIRED = 12;
  301. X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD = 13;
  302. X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD = 14;
  303. X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD = 15;
  304. X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD = 16;
  305. X509_V_ERR_OUT_OF_MEM = 17;
  306. X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT = 18;
  307. X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN = 19;
  308. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY = 20;
  309. X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE = 21;
  310. X509_V_ERR_CERT_CHAIN_TOO_LONG = 22;
  311. X509_V_ERR_CERT_REVOKED = 23;
  312. X509_V_ERR_INVALID_CA = 24;
  313. X509_V_ERR_PATH_LENGTH_EXCEEDED = 25;
  314. X509_V_ERR_INVALID_PURPOSE = 26;
  315. X509_V_ERR_CERT_UNTRUSTED = 27;
  316. X509_V_ERR_CERT_REJECTED = 28;
  317. //These are 'informational' when looking for issuer cert
  318. X509_V_ERR_SUBJECT_ISSUER_MISMATCH = 29;
  319. X509_V_ERR_AKID_SKID_MISMATCH = 30;
  320. X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH = 31;
  321. X509_V_ERR_KEYUSAGE_NO_CERTSIGN = 32;
  322. X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER = 33;
  323. X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION = 34;
  324. //The application is not happy
  325. X509_V_ERR_APPLICATION_VERIFICATION = 50;
  326. SSL_FILETYPE_ASN1 = 2;
  327. SSL_FILETYPE_PEM = 1;
  328. EVP_PKEY_RSA = 6;
  329. // RSA
  330. RSA_PKCS1_PADDING = 1;
  331. RSA_SSLV23_PADDING = 2;
  332. RSA_NO_PADDING = 3;
  333. RSA_PKCS1_OAEP_PADDING = 4;
  334. var
  335. SSLLibHandle: TLibHandle = 0;
  336. SSLUtilHandle: TLibHandle = 0;
  337. SSLLibFile: string = '';
  338. SSLUtilFile: string = '';
  339. // libssl.dll
  340. function SslGetError(s: PSSL; ret_code: cInt):cInt;
  341. function SslLibraryInit:cInt;
  342. procedure SslLoadErrorStrings;
  343. // function SslCtxSetCipherList(arg0: PSSL_CTX; str: PChar):cInt;
  344. function SslCtxSetCipherList(arg0: PSSL_CTX; var str: String):cInt;
  345. function SslCtxNew(meth: PSSL_METHOD):PSSL_CTX;
  346. procedure SslCtxFree(arg0: PSSL_CTX);
  347. function SslSetFd(s: PSSL; fd: cInt):cInt;
  348. function SslCtrl(ssl: PSSL; cmd: cInt; larg: clong; parg: Pointer): cLong;
  349. function SslCTXCtrl(ctx: PSSL_CTX; cmd: cInt; larg: clong; parg: Pointer): cLong;
  350. function SSLCTXSetMode(ctx: PSSL_CTX; mode: cLong): cLong;
  351. function SSLSetMode(s: PSSL; mode: cLong): cLong;
  352. function SSLCTXGetMode(ctx: PSSL_CTX): cLong;
  353. function SSLGetMode(s: PSSL): cLong;
  354. function SslMethodV2:PSSL_METHOD;
  355. function SslMethodV3:PSSL_METHOD;
  356. function SslMethodTLSV1:PSSL_METHOD;
  357. function SslMethodV23:PSSL_METHOD;
  358. function SslCtxUsePrivateKey(ctx: PSSL_CTX; pkey: SslPtr):cInt;
  359. function SslCtxUsePrivateKeyASN1(pk: cInt; ctx: PSSL_CTX; d: String; len: cLong):cInt;
  360. // function SslCtxUsePrivateKeyFile(ctx: PSSL_CTX; const _file: PChar; _type: cInt):cInt;
  361. function SslCtxUsePrivateKeyFile(ctx: PSSL_CTX; const _file: String; _type: cInt):cInt;
  362. function SslCtxUseCertificate(ctx: PSSL_CTX; x: SslPtr):cInt;
  363. function SslCtxUseCertificateASN1(ctx: PSSL_CTX; len: cLong; d: String):cInt;
  364. function SslCtxUseCertificateFile(ctx: PSSL_CTX; const _file: String; _type: cInt):cInt;
  365. // function SslCtxUseCertificateChainFile(ctx: PSSL_CTX; const _file: PChar):cInt;
  366. function SslCtxUseCertificateChainFile(ctx: PSSL_CTX; const _file: String):cInt;
  367. function SslCtxCheckPrivateKeyFile(ctx: PSSL_CTX):cInt;
  368. procedure SslCtxSetDefaultPasswdCb(ctx: PSSL_CTX; cb: PPasswdCb);
  369. procedure SslCtxSetDefaultPasswdCbUserdata(ctx: PSSL_CTX; u: SslPtr);
  370. // function SslCtxLoadVerifyLocations(ctx: PSSL_CTX; const CAfile: PChar; const CApath: PChar):cInt;
  371. function SslCtxLoadVerifyLocations(ctx: PSSL_CTX; const CAfile: String; const CApath: String):cInt;
  372. function SslNew(ctx: PSSL_CTX):PSSL;
  373. procedure SslFree(ssl: PSSL);
  374. function SslAccept(ssl: PSSL):cInt;
  375. function SslConnect(ssl: PSSL):cInt;
  376. function SslShutdown(ssl: PSSL):cInt;
  377. function SslRead(ssl: PSSL; buf: SslPtr; num: cInt):cInt;
  378. function SslPeek(ssl: PSSL; buf: SslPtr; num: cInt):cInt;
  379. function SslWrite(ssl: PSSL; buf: SslPtr; num: cInt):cInt;
  380. function SslPending(ssl: PSSL):cInt;
  381. function SslGetVersion(ssl: PSSL):String;
  382. function SslGetPeerCertificate(ssl: PSSL):PX509;
  383. procedure SslCtxSetVerify(ctx: PSSL_CTX; mode: cInt; arg2: PFunction);
  384. function SSLGetCurrentCipher(s: PSSL):SslPtr;
  385. function SSLCipherGetName(c: SslPtr): String;
  386. function SSLCipherGetBits(c: SslPtr; var alg_bits: cInt):cInt;
  387. function SSLGetVerifyResult(ssl: PSSL):cLong;
  388. // libeay.dll
  389. function X509New: PX509;
  390. procedure X509Free(x: PX509);
  391. function X509NameOneline(a: PX509_NAME; var buf: String; size: cInt):String;
  392. function X509GetSubjectName(a: PX509):PX509_NAME;
  393. function X509GetIssuerName(a: PX509):PX509_NAME;
  394. function X509NameHash(x: PX509_NAME):cuLong;
  395. // function SslX509Digest(data: PX509; _type: PEVP_MD; md: PChar; len: PcInt):cInt;
  396. function X509Digest(data: PX509; _type: PEVP_MD; md: String; var len: cInt):cInt;
  397. function X509print(b: PBIO; a: PX509): cInt;
  398. function X509SetVersion(x: PX509; version: cInt): cInt;
  399. function X509SetPubkey(x: PX509; pkey: EVP_PKEY): cInt;
  400. function X509SetIssuerName(x: PX509; name: PX509_NAME): cInt;
  401. function X509NameAddEntryByTxt(name: PX509_NAME; field: string; _type: cInt;
  402. bytes: string; len, loc, _set: cInt): cInt;
  403. function X509Sign(x: PX509; pkey: EVP_PKEY; const md: PEVP_MD): cInt;
  404. function X509GmtimeAdj(s: PASN1_UTCTIME; adj: cInt): PASN1_UTCTIME;
  405. function X509SetNotBefore(x: PX509; tm: PASN1_UTCTIME): cInt;
  406. function X509SetNotAfter(x: PX509; tm: PASN1_UTCTIME): cInt;
  407. function X509GetSerialNumber(x: PX509): PASN1_cInt;
  408. function EvpPkeyNew: EVP_PKEY;
  409. procedure EvpPkeyFree(pk: EVP_PKEY);
  410. function EvpPkeyAssign(pkey: EVP_PKEY; _type: cInt; key: Prsa): cInt;
  411. function EvpGetDigestByName(Name: String): PEVP_MD;
  412. procedure EVPcleanup;
  413. function SSLeayversion(t: cInt): string;
  414. procedure ErrErrorString(e: cInt; var buf: string; len: cInt);
  415. function ErrGetError: cInt;
  416. procedure ErrClearError;
  417. procedure ErrFreeStrings;
  418. procedure ErrRemoveState(pid: cInt);
  419. procedure RandScreen;
  420. function BioNew(b: PBIO_METHOD): PBIO;
  421. procedure BioFreeAll(b: PBIO);
  422. function BioSMem: PBIO_METHOD;
  423. function BioCtrlPending(b: PBIO): cInt;
  424. function BioRead(b: PBIO; var Buf: String; Len: cInt): cInt;
  425. function BioWrite(b: PBIO; Buf: String; Len: cInt): cInt;
  426. function d2iPKCS12bio(b:PBIO; Pkcs12: SslPtr): SslPtr;
  427. function PKCS12parse(p12: SslPtr; pass: string; var pkey, cert, ca: SslPtr): cInt;
  428. procedure PKCS12free(p12: SslPtr);
  429. function Asn1UtctimeNew: PASN1_UTCTIME;
  430. procedure Asn1UtctimeFree(a: PASN1_UTCTIME);
  431. function i2dX509bio(b: PBIO; x: PX509): cInt;
  432. function i2dPrivateKeyBio(b: PBIO; pkey: EVP_PKEY): cInt;
  433. // 3DES functions
  434. procedure DESsetoddparity(Key: des_cblock);
  435. function DESsetkeychecked(key: des_cblock; schedule: des_key_schedule): cInt;
  436. procedure DESecbencrypt(Input: des_cblock; output: des_cblock; ks: des_key_schedule; enc: cInt);
  437. // RAND functions
  438. function RAND_set_rand_method(const meth: PRAND_METHOD): cint;
  439. function RAND_get_rand_method: PRAND_METHOD;
  440. function RAND_SSLeay: PRAND_METHOD;
  441. procedure RAND_cleanup;
  442. function RAND_bytes(buf: PByte; num: cint): cint;
  443. function RAND_pseudo_bytes(buf: PByte; num: cint): cint;
  444. procedure RAND_seed(const buf: Pointer; num: cint);
  445. procedure RAND_add(const buf: Pointer; num: cint; entropy: cdouble);
  446. function RAND_load_file(const file_name: PChar; max_bytes: clong): cint;
  447. function RAND_write_file(const file_name: PChar): cint;
  448. function RAND_file_name(file_name: PChar; num: csize_t): PChar;
  449. function RAND_status: cint;
  450. function RAND_query_egd_bytes(const path: PChar; buf: PByte; bytes: cint): cint;
  451. function RAND_egd(const path: PChar): cint;
  452. function RAND_egd_bytes(const path: PChar; bytes: cint): cint;
  453. procedure ERR_load_RAND_strings;
  454. function RAND_poll: cint;
  455. // RSA Functions
  456. function RSA_new(): PRSA;
  457. function RSA_new_method(method: PENGINE): PRSA;
  458. function RSA_size(arsa: PRSA): cint;
  459. // Deprecated Function: Don't use!
  460. // For compatibility with previous versions of this file
  461. function RsaGenerateKey(bits, e: cInt; callback: PFunction; cb_arg: SslPtr): PRSA;
  462. // New version of the previous deprecated routine
  463. function RSA_generate_key_ex(arsa: PRSA; bits: cInt; e: PBIGNUM; cb: PBN_GENCB): PRSA;
  464. //
  465. function RSA_check_key(arsa: PRSA): cint;
  466. // Next 4 return -1 on error
  467. function RSA_public_encrypt(flen: cint; from_buf, to_buf: PByte; arsa: PRSA; padding: cint): cint;
  468. function RSA_private_encrypt(flen: cint; from_buf, to_buf: PByte; arsa: PRSA; padding: cint): cint;
  469. function RSA_public_decrypt(flen: cint; from_buf, to_buf: PByte; arsa: PRSA; padding: cint): cint;
  470. function RSA_private_decrypt(flen: cint; from_buf, to_buf: PByte; arsa: PRSA; padding: cint): cint;
  471. procedure RSA_free(arsa: PRSA);
  472. //
  473. // RSA_up_flags
  474. function RSA_flags(arsa: PRSA): Integer;
  475. //
  476. procedure RSA_set_default_method(method: PRSA_METHOD);
  477. function RSA_get_default_method: PRSA_METHOD;
  478. function RSA_get_method(arsa: PRSA): PRSA_METHOD;
  479. function RSA_set_method(arsa: PRSA; method: PRSA_METHOD): PRSA_METHOD;
  480. //
  481. // RSA_memory_lock
  482. // X509 Functions
  483. function d2i_RSAPublicKey(arsa: PPRSA; pp: PPByte; len: cint): PRSA;
  484. function i2d_RSAPublicKey(arsa: PRSA; pp: PPByte): cint;
  485. function d2i_RSAPrivateKey(arsa: PPRSA; pp: PPByte; len: cint): PRSA;
  486. function i2d_RSAPrivateKey(arsa: PRSA; pp: PPByte): cint;
  487. // ERR Functions
  488. function Err_Error_String(e: cInt; buf: PChar): PChar;
  489. // Crypto Functions
  490. function SSLeay_version(t: cint): PChar;
  491. // EVP Functions - evp.h
  492. procedure OpenSSL_add_all_algorithms;
  493. procedure OpenSSL_add_all_ciphers;
  494. procedure OpenSSL_add_all_digests;
  495. //
  496. function EVP_DigestInit(ctx: PEVP_MD_CTX; type_: PEVP_MD): cint;
  497. function EVP_DigestUpdate(ctx: PEVP_MD_CTX; const data: Pointer; cnt: csize_t): cint;
  498. function EVP_DigestFinal(ctx: PEVP_MD_CTX; md: PByte; s: pcuint): cint;
  499. //
  500. function EVP_get_cipherbyname(const name: PChar): PEVP_CIPHER;
  501. function EVP_get_digestbyname(const name: PChar): PEVP_MD;
  502. //
  503. procedure EVP_CIPHER_CTX_init(a: PEVP_CIPHER_CTX);
  504. function EVP_CIPHER_CTX_cleanup(a: PEVP_CIPHER_CTX): cint;
  505. function EVP_CIPHER_CTX_set_key_length(x: PEVP_CIPHER_CTX; keylen: cint): cint;
  506. function EVP_CIPHER_CTX_ctrl(ctx: PEVP_CIPHER_CTX; type_, arg: cint; ptr: Pointer): cint;
  507. //
  508. function EVP_EncryptInit(ctx: PEVP_CIPHER_CTX; const chipher_: PEVP_CIPHER;
  509. const key, iv: PByte): cint;
  510. function EVP_EncryptUpdate(ctx: PEVP_CIPHER_CTX; out_: pcuchar;
  511. outlen: pcint; const in_: pcuchar; inlen: cint): cint;
  512. function EVP_EncryptFinal(ctx: PEVP_CIPHER_CTX; out_data: PByte; outlen: pcint): cint;
  513. //
  514. function EVP_DecryptInit(ctx: PEVP_CIPHER_CTX; chiphir_type: PEVP_CIPHER;
  515. const key, iv: PByte): cint;
  516. function EVP_DecryptUpdate(ctx: PEVP_CIPHER_CTX; out_data: PByte;
  517. outl: pcint; const in_: PByte; inl: cint): cint;
  518. function EVP_DecryptFinal(ctx: PEVP_CIPHER_CTX; outm: PByte; outlen: pcint): cint;
  519. function IsSSLloaded: Boolean;
  520. function InitSSLInterface(AVerboseLoading: Boolean = False): Boolean;
  521. function DestroySSLInterface: Boolean;
  522. var
  523. OpenSSL_unavailable_functions: string;
  524. implementation
  525. type
  526. // libssl.dll
  527. TSslGetError = function(s: PSSL; ret_code: cInt):cInt; cdecl;
  528. TSslLibraryInit = function:cInt; cdecl;
  529. TSslLoadErrorStrings = procedure; cdecl;
  530. TSslCtxSetCipherList = function(arg0: PSSL_CTX; str: PChar):cInt; cdecl;
  531. TSslCtxNew = function(meth: PSSL_METHOD):PSSL_CTX; cdecl;
  532. TSslCtxFree = procedure(arg0: PSSL_CTX); cdecl;
  533. TSslSetFd = function(s: PSSL; fd: cInt):cInt; cdecl;
  534. TSslCtrl = function(ssl: PSSL; cmd: cInt; larg: clong; parg: Pointer): cLong; cdecl;
  535. TSslCTXCtrl = function(ctx: PSSL_CTX; cmd: cInt; larg: clong; parg: Pointer): cLong; cdecl;
  536. TSslMethodV2 = function:PSSL_METHOD; cdecl;
  537. TSslMethodV3 = function:PSSL_METHOD; cdecl;
  538. TSslMethodTLSV1 = function:PSSL_METHOD; cdecl;
  539. TSslMethodV23 = function:PSSL_METHOD; cdecl;
  540. TSslCtxUsePrivateKey = function(ctx: PSSL_CTX; pkey: sslptr):cInt; cdecl;
  541. TSslCtxUsePrivateKeyASN1 = function(pk: cInt; ctx: PSSL_CTX; d: sslptr; len: cInt):cInt; cdecl;
  542. TSslCtxUsePrivateKeyFile = function(ctx: PSSL_CTX; const _file: PChar; _type: cInt):cInt; cdecl;
  543. TSslCtxUseCertificate = function(ctx: PSSL_CTX; x: SslPtr):cInt; cdecl;
  544. TSslCtxUseCertificateASN1 = function(ctx: PSSL_CTX; len: cInt; d: SslPtr):cInt; cdecl;
  545. TSslCtxUseCertificateFile = function(ctx: PSSL_CTX; const _file: PChar; _type: cInt):cInt; cdecl;
  546. TSslCtxUseCertificateChainFile = function(ctx: PSSL_CTX; const _file: PChar):cInt; cdecl;
  547. TSslCtxCheckPrivateKeyFile = function(ctx: PSSL_CTX):cInt; cdecl;
  548. TSslCtxSetDefaultPasswdCb = procedure(ctx: PSSL_CTX; cb: SslPtr); cdecl;
  549. TSslCtxSetDefaultPasswdCbUserdata = procedure(ctx: PSSL_CTX; u: SslPtr); cdecl;
  550. TSslCtxLoadVerifyLocations = function(ctx: PSSL_CTX; const CAfile: PChar; const CApath: PChar):cInt; cdecl;
  551. TSslNew = function(ctx: PSSL_CTX):PSSL; cdecl;
  552. TSslFree = procedure(ssl: PSSL); cdecl;
  553. TSslAccept = function(ssl: PSSL):cInt; cdecl;
  554. TSslConnect = function(ssl: PSSL):cInt; cdecl;
  555. TSslShutdown = function(ssl: PSSL):cInt; cdecl;
  556. TSslRead = function(ssl: PSSL; buf: PChar; num: cInt):cInt; cdecl;
  557. TSslPeek = function(ssl: PSSL; buf: PChar; num: cInt):cInt; cdecl;
  558. TSslWrite = function(ssl: PSSL; const buf: PChar; num: cInt):cInt; cdecl;
  559. TSslPending = function(ssl: PSSL):cInt; cdecl;
  560. TSslGetVersion = function(ssl: PSSL):PChar; cdecl;
  561. TSslGetPeerCertificate = function(ssl: PSSL):PX509; cdecl;
  562. TSslCtxSetVerify = procedure(ctx: PSSL_CTX; mode: cInt; arg2: SslPtr); cdecl;
  563. TSSLGetCurrentCipher = function(s: PSSL):SslPtr; cdecl;
  564. TSSLCipherGetName = function(c: Sslptr):PChar; cdecl;
  565. TSSLCipherGetBits = function(c: SslPtr; alg_bits: PcInt):cInt; cdecl;
  566. TSSLGetVerifyResult = function(ssl: PSSL):cInt; cdecl;
  567. // libeay.dll
  568. TX509New = function: PX509; cdecl;
  569. TX509Free = procedure(x: PX509); cdecl;
  570. TX509NameOneline = function(a: PX509_NAME; buf: PChar; size: cInt):PChar; cdecl;
  571. TX509GetSubjectName = function(a: PX509):PX509_NAME; cdecl;
  572. TX509GetIssuerName = function(a: PX509):PX509_NAME; cdecl;
  573. TX509NameHash = function(x: PX509_NAME):cuLong; cdecl;
  574. TX509Digest = function(data: PX509; _type: PEVP_MD; md: PChar; len: PcInt):cInt; cdecl;
  575. TX509print = function(b: PBIO; a: PX509): cInt; cdecl;
  576. TX509SetVersion = function(x: PX509; version: cInt): cInt; cdecl;
  577. TX509SetPubkey = function(x: PX509; pkey: EVP_PKEY): cInt; cdecl;
  578. TX509SetIssuerName = function(x: PX509; name: PX509_NAME): cInt; cdecl;
  579. TX509NameAddEntryByTxt = function(name: PX509_NAME; field: PChar; _type: cInt;
  580. bytes: PChar; len, loc, _set: cInt): cInt; cdecl;
  581. TX509Sign = function(x: PX509; pkey: EVP_PKEY; const md: PEVP_MD): cInt; cdecl;
  582. TX509GmtimeAdj = function(s: PASN1_UTCTIME; adj: cInt): PASN1_UTCTIME; cdecl;
  583. TX509SetNotBefore = function(x: PX509; tm: PASN1_UTCTIME): cInt; cdecl;
  584. TX509SetNotAfter = function(x: PX509; tm: PASN1_UTCTIME): cInt; cdecl;
  585. TX509GetSerialNumber = function(x: PX509): PASN1_cInt; cdecl;
  586. TEvpPkeyNew = function: EVP_PKEY; cdecl;
  587. TEvpPkeyFree = procedure(pk: EVP_PKEY); cdecl;
  588. TEvpPkeyAssign = function(pkey: EVP_PKEY; _type: cInt; key: Prsa): cInt; cdecl;
  589. TEvpGetDigestByName = function(Name: PChar): PEVP_MD; cdecl;
  590. TEVPcleanup = procedure; cdecl;
  591. TSSLeayversion = function(t: cInt): PChar; cdecl;
  592. TErrErrorString = procedure(e: cInt; buf: PChar; len: cInt); cdecl;
  593. TErrGetError = function: cInt; cdecl;
  594. TErrClearError = procedure; cdecl;
  595. TErrFreeStrings = procedure; cdecl;
  596. TErrRemoveState = procedure(pid: cInt); cdecl;
  597. TRandScreen = procedure; cdecl;
  598. TBioNew = function(b: PBIO_METHOD): PBIO; cdecl;
  599. TBioFreeAll = procedure(b: PBIO); cdecl;
  600. TBioSMem = function: PBIO_METHOD; cdecl;
  601. TBioCtrlPending = function(b: PBIO): cInt; cdecl;
  602. TBioRead = function(b: PBIO; Buf: PChar; Len: cInt): cInt; cdecl;
  603. TBioWrite = function(b: PBIO; Buf: PChar; Len: cInt): cInt; cdecl;
  604. Td2iPKCS12bio = function(b:PBIO; Pkcs12: SslPtr): SslPtr; cdecl;
  605. TPKCS12parse = function(p12: SslPtr; pass: PChar; var pkey, cert, ca: SslPtr): cInt; cdecl;
  606. TPKCS12free = procedure(p12: SslPtr); cdecl;
  607. TAsn1UtctimeNew = function: PASN1_UTCTIME; cdecl;
  608. TAsn1UtctimeFree = procedure(a: PASN1_UTCTIME); cdecl;
  609. Ti2dX509bio = function(b: PBIO; x: PX509): cInt; cdecl;
  610. Ti2dPrivateKeyBio= function(b: PBIO; pkey: EVP_PKEY): cInt; cdecl;
  611. // 3DES functions
  612. TDESsetoddparity = procedure(Key: des_cblock); cdecl;
  613. TDESsetkeychecked = function(key: des_cblock; schedule: des_key_schedule): cInt; cdecl;
  614. TDESecbencrypt = procedure(Input: des_cblock; output: des_cblock; ks: des_key_schedule; enc: cInt); cdecl;
  615. //thread lock functions
  616. TCRYPTOnumlocks = function: cInt; cdecl;
  617. TCRYPTOSetLockingCallback = procedure(cb: Sslptr); cdecl;
  618. // RAND functions
  619. TRAND_set_rand_method = function(const meth: PRAND_METHOD): cint; cdecl;
  620. TRAND_get_rand_method = function(): PRAND_METHOD; cdecl;
  621. TRAND_SSLeay = function(): PRAND_METHOD; cdecl;
  622. TRAND_cleanup = procedure(); cdecl;
  623. TRAND_bytes = function(buf: PByte; num: cint): cint; cdecl;
  624. TRAND_pseudo_bytes = function(buf: PByte; num: cint): cint; cdecl;
  625. TRAND_seed = procedure(const buf: Pointer; num: cint); cdecl;
  626. TRAND_add = procedure(const buf: Pointer; num: cint; entropy: cdouble); cdecl;
  627. TRAND_load_file = function(const file_name: PChar; max_bytes: clong): cint; cdecl;
  628. TRAND_write_file = function(const file_name: PChar): cint; cdecl;
  629. TRAND_file_name = function(file_name: PChar; num: csize_t): PChar; cdecl;
  630. TRAND_status = function(): cint; cdecl;
  631. TRAND_query_egd_bytes = function(const path: PChar; buf: PByte; bytes: cint): cint; cdecl;
  632. TRAND_egd = function(const path: PChar): cint; cdecl;
  633. TRAND_egd_bytes = function(const path: PChar; bytes: cint): cint; cdecl;
  634. TERR_load_RAND_strings = procedure(); cdecl;
  635. TRAND_poll = function(): cint; cdecl;
  636. // RSA Functions
  637. TRSA_new = function (): PRSA; cdecl;
  638. TRSA_new_method = function (method: PENGINE): PRSA; cdecl;
  639. TRSA_size = function (arsa: PRSA): cint; cdecl;
  640. TRsaGenerateKey = function(bits, e: cInt; callback: PFunction; cb_arg: SslPtr): PRSA; cdecl;
  641. TRSA_generate_key_ex = function (arsa: PRSA; bits: cInt; e: PBIGNUM; cb: PBN_GENCB): PRSA; cdecl;
  642. TRSA_check_key = function (arsa: PRSA): cint; cdecl;
  643. TRSA_public_encrypt = function (flen: cint; from_buf, to_buf: PByte; arsa: PRSA; padding: cint): cint; cdecl;
  644. TRSA_private_encrypt = function (flen: cint; from_buf, to_buf: PByte; arsa: PRSA; padding: cint): cint; cdecl;
  645. TRSA_public_decrypt = function (flen: cint; from_buf, to_buf: PByte; arsa: PRSA; padding: cint): cint; cdecl;
  646. TRSA_private_decrypt = function (flen: cint; from_buf, to_buf: PByte; arsa: PRSA; padding: cint): cint; cdecl;
  647. TRSA_free = procedure (arsa: PRSA); cdecl;
  648. TRSA_flags = function (arsa: PRSA): Integer; cdecl;
  649. TRSA_set_default_method = procedure (method: PRSA_METHOD); cdecl;
  650. TRSA_get_default_method = function : PRSA_METHOD; cdecl;
  651. TRSA_get_method = function (prsa: PRSA): PRSA_METHOD; cdecl;
  652. TRSA_set_method = function (arsa: PRSA; method: PRSA_METHOD): PRSA_METHOD; cdecl;
  653. // X509 Functions
  654. Td2i_RSAPublicKey = function (arsa: PPRSA; pp: PPByte; len: cint): PRSA; cdecl;
  655. Ti2d_RSAPublicKey = function (arsa: PRSA; pp: PPByte): cint; cdecl;
  656. Td2i_RSAPrivateKey = function (arsa: PPRSA; pp: PPByte; len: cint): PRSA; cdecl;
  657. Ti2d_RSAPrivateKey = function (arsa: PRSA; pp: PPByte): cint; cdecl;
  658. // ERR Functions
  659. TErr_Error_String = function (e: cInt; buf: PChar): PChar; cdecl;
  660. // Crypto Functions
  661. TSSLeay_version = function(t: cint): PChar; cdecl;
  662. // EVP Functions
  663. TOpenSSL_add_all_algorithms = procedure(); cdecl;
  664. TOpenSSL_add_all_ciphers = procedure(); cdecl;
  665. TOpenSSL_add_all_digests = procedure(); cdecl;
  666. //
  667. TEVP_DigestInit = function(ctx: PEVP_MD_CTX; type_: PEVP_MD): cint; cdecl;
  668. TEVP_DigestUpdate = function(ctx: PEVP_MD_CTX; const data: Pointer; cnt: csize_t): cint; cdecl;
  669. TEVP_DigestFinal = function(ctx: PEVP_MD_CTX; md: PByte; s: pcuint): cint; cdecl;
  670. //
  671. TEVP_get_cipherbyname = function(const name: PChar): PEVP_CIPHER; cdecl;
  672. TEVP_get_digestbyname = function(const name: PChar): PEVP_MD; cdecl;
  673. //
  674. TEVP_CIPHER_CTX_init = procedure(a: PEVP_CIPHER_CTX); cdecl;
  675. TEVP_CIPHER_CTX_cleanup = function(a: PEVP_CIPHER_CTX): cint; cdecl;
  676. TEVP_CIPHER_CTX_set_key_length = function(x: PEVP_CIPHER_CTX; keylen: cint): cint; cdecl;
  677. TEVP_CIPHER_CTX_ctrl = function(ctx: PEVP_CIPHER_CTX; type_, arg: cint; ptr: Pointer): cint; cdecl;
  678. //
  679. TEVP_EncryptInit = function(ctx: PEVP_CIPHER_CTX; const chipher_: PEVP_CIPHER;
  680. const key, iv: PByte): cint; cdecl;
  681. TEVP_EncryptUpdate = function(ctx: PEVP_CIPHER_CTX; out_: pcuchar;
  682. outlen: pcint; const in_: pcuchar; inlen: cint): cint; cdecl;
  683. TEVP_EncryptFinal = function(ctx: PEVP_CIPHER_CTX; out_data: PByte; outlen: pcint): cint; cdecl;
  684. //
  685. TEVP_DecryptInit = function(ctx: PEVP_CIPHER_CTX; chiphir_type: PEVP_CIPHER;
  686. const key, iv: PByte): cint; cdecl;
  687. TEVP_DecryptUpdate = function(ctx: PEVP_CIPHER_CTX; out_data: PByte;
  688. outl: pcint; const in_: PByte; inl: cint): cint; cdecl;
  689. TEVP_DecryptFinal = function(ctx: PEVP_CIPHER_CTX; outm: PByte; outlen: pcint): cint; cdecl;
  690. var
  691. // libssl.dll
  692. _SslGetError: TSslGetError = nil;
  693. _SslLibraryInit: TSslLibraryInit = nil;
  694. _SslLoadErrorStrings: TSslLoadErrorStrings = nil;
  695. _SslCtxSetCipherList: TSslCtxSetCipherList = nil;
  696. _SslCtxNew: TSslCtxNew = nil;
  697. _SslCtxFree: TSslCtxFree = nil;
  698. _SslSetFd: TSslSetFd = nil;
  699. _SslCtrl: TSslCtrl = nil;
  700. _SslCTXCtrl: TSslCTXCtrl = nil;
  701. _SslMethodV2: TSslMethodV2 = nil;
  702. _SslMethodV3: TSslMethodV3 = nil;
  703. _SslMethodTLSV1: TSslMethodTLSV1 = nil;
  704. _SslMethodV23: TSslMethodV23 = nil;
  705. _SslCtxUsePrivateKey: TSslCtxUsePrivateKey = nil;
  706. _SslCtxUsePrivateKeyASN1: TSslCtxUsePrivateKeyASN1 = nil;
  707. _SslCtxUsePrivateKeyFile: TSslCtxUsePrivateKeyFile = nil;
  708. _SslCtxUseCertificate: TSslCtxUseCertificate = nil;
  709. _SslCtxUseCertificateASN1: TSslCtxUseCertificateASN1 = nil;
  710. _SslCtxUseCertificateFile: TSslCtxUseCertificateFile = nil;
  711. _SslCtxUseCertificateChainFile: TSslCtxUseCertificateChainFile = nil;
  712. _SslCtxCheckPrivateKeyFile: TSslCtxCheckPrivateKeyFile = nil;
  713. _SslCtxSetDefaultPasswdCb: TSslCtxSetDefaultPasswdCb = nil;
  714. _SslCtxSetDefaultPasswdCbUserdata: TSslCtxSetDefaultPasswdCbUserdata = nil;
  715. _SslCtxLoadVerifyLocations: TSslCtxLoadVerifyLocations = nil;
  716. _SslNew: TSslNew = nil;
  717. _SslFree: TSslFree = nil;
  718. _SslAccept: TSslAccept = nil;
  719. _SslConnect: TSslConnect = nil;
  720. _SslShutdown: TSslShutdown = nil;
  721. _SslRead: TSslRead = nil;
  722. _SslPeek: TSslPeek = nil;
  723. _SslWrite: TSslWrite = nil;
  724. _SslPending: TSslPending = nil;
  725. _SslGetVersion: TSslGetVersion = nil;
  726. _SslGetPeerCertificate: TSslGetPeerCertificate = nil;
  727. _SslCtxSetVerify: TSslCtxSetVerify = nil;
  728. _SSLGetCurrentCipher: TSSLGetCurrentCipher = nil;
  729. _SSLCipherGetName: TSSLCipherGetName = nil;
  730. _SSLCipherGetBits: TSSLCipherGetBits = nil;
  731. _SSLGetVerifyResult: TSSLGetVerifyResult = nil;
  732. // libeay.dll
  733. _X509New: TX509New = nil;
  734. _X509Free: TX509Free = nil;
  735. _X509NameOneline: TX509NameOneline = nil;
  736. _X509GetSubjectName: TX509GetSubjectName = nil;
  737. _X509GetIssuerName: TX509GetIssuerName = nil;
  738. _X509NameHash: TX509NameHash = nil;
  739. _X509Digest: TX509Digest = nil;
  740. _X509print: TX509print = nil;
  741. _X509SetVersion: TX509SetVersion = nil;
  742. _X509SetPubkey: TX509SetPubkey = nil;
  743. _X509SetIssuerName: TX509SetIssuerName = nil;
  744. _X509NameAddEntryByTxt: TX509NameAddEntryByTxt = nil;
  745. _X509Sign: TX509Sign = nil;
  746. _X509GmtimeAdj: TX509GmtimeAdj = nil;
  747. _X509SetNotBefore: TX509SetNotBefore = nil;
  748. _X509SetNotAfter: TX509SetNotAfter = nil;
  749. _X509GetSerialNumber: TX509GetSerialNumber = nil;
  750. _EvpPkeyNew: TEvpPkeyNew = nil;
  751. _EvpPkeyFree: TEvpPkeyFree = nil;
  752. _EvpPkeyAssign: TEvpPkeyAssign = nil;
  753. _EvpGetDigestByName: TEvpGetDigestByName = nil;
  754. _EVPcleanup: TEVPcleanup = nil;
  755. _SSLeayversion: TSSLeayversion = nil;
  756. _ErrErrorString: TErrErrorString = nil;
  757. _ErrGetError: TErrGetError = nil;
  758. _ErrClearError: TErrClearError = nil;
  759. _ErrFreeStrings: TErrFreeStrings = nil;
  760. _ErrRemoveState: TErrRemoveState = nil;
  761. _RandScreen: TRandScreen = nil;
  762. _BioNew: TBioNew = nil;
  763. _BioFreeAll: TBioFreeAll = nil;
  764. _BioSMem: TBioSMem = nil;
  765. _BioCtrlPending: TBioCtrlPending = nil;
  766. _BioRead: TBioRead = nil;
  767. _BioWrite: TBioWrite = nil;
  768. _d2iPKCS12bio: Td2iPKCS12bio = nil;
  769. _PKCS12parse: TPKCS12parse = nil;
  770. _PKCS12free: TPKCS12free = nil;
  771. _Asn1UtctimeNew: TAsn1UtctimeNew = nil;
  772. _Asn1UtctimeFree: TAsn1UtctimeFree = nil;
  773. _i2dX509bio: Ti2dX509bio = nil;
  774. _i2dPrivateKeyBio: Ti2dPrivateKeyBio = nil;
  775. // 3DES functions
  776. _DESsetoddparity: TDESsetoddparity = nil;
  777. _DESsetkeychecked: TDESsetkeychecked = nil;
  778. _DESecbencrypt: TDESecbencrypt = nil;
  779. //thread lock functions
  780. _CRYPTOnumlocks: TCRYPTOnumlocks = nil;
  781. _CRYPTOSetLockingCallback: TCRYPTOSetLockingCallback = nil;
  782. // RAND functions
  783. _RAND_set_rand_method: TRAND_set_rand_method = nil;
  784. _RAND_get_rand_method: TRAND_get_rand_method = nil;
  785. _RAND_SSLeay: TRAND_SSLeay = nil;
  786. _RAND_cleanup: TRAND_cleanup = nil;
  787. _RAND_bytes: TRAND_bytes = nil;
  788. _RAND_pseudo_bytes: TRAND_pseudo_bytes = nil;
  789. _RAND_seed: TRAND_seed = nil;
  790. _RAND_add: TRAND_add = nil;
  791. _RAND_load_file: TRAND_load_file = nil;
  792. _RAND_write_file: TRAND_write_file = nil;
  793. _RAND_file_name: TRAND_file_name = nil;
  794. _RAND_status: TRAND_status = nil;
  795. _RAND_query_egd_bytes: TRAND_query_egd_bytes = nil;
  796. _RAND_egd: TRAND_egd = nil;
  797. _RAND_egd_bytes: TRAND_egd_bytes = nil;
  798. _ERR_load_RAND_strings: TERR_load_RAND_strings = nil;
  799. _RAND_poll: TRAND_poll = nil;
  800. // RSA Functions
  801. _RSA_new: TRSA_new = nil;
  802. _RSA_new_method: TRSA_new_method = nil;
  803. _RSA_size: TRSA_size = nil;
  804. _RsaGenerateKey: TRsaGenerateKey = nil;
  805. _RSA_generate_key_ex: TRSA_generate_key_ex = nil;
  806. _RSA_check_key: TRSA_check_key = nil;
  807. _RSA_public_encrypt: TRSA_public_encrypt = nil;
  808. _RSA_private_encrypt: TRSA_private_encrypt = nil;
  809. _RSA_public_decrypt: TRSA_public_decrypt = nil;
  810. _RSA_private_decrypt: TRSA_private_decrypt = nil;
  811. _RSA_free: TRSA_free = nil;
  812. _RSA_flags: TRSA_flags = nil;
  813. _RSA_set_default_method: TRSA_set_default_method = nil;
  814. _RSA_get_default_method: TRSA_get_default_method = nil;
  815. _RSA_get_method: TRSA_get_method = nil;
  816. _RSA_set_method: TRSA_set_method = nil;
  817. // X509 Functions
  818. _d2i_RSAPublicKey: Td2i_RSAPublicKey = nil;
  819. _i2d_RSAPublicKey: Ti2d_RSAPublicKey = nil;
  820. _d2i_RSAPrivateKey: Td2i_RSAPrivateKey = nil;
  821. _i2d_RSAPrivateKey: Ti2d_RSAPrivateKey = nil;
  822. // ERR Functions
  823. _Err_Error_String: TErr_Error_String = nil;
  824. // Crypto Functions
  825. _SSLeay_version: TSSLeay_version = nil;
  826. // EVP Functions
  827. _OpenSSL_add_all_algorithms: TOpenSSL_add_all_algorithms = nil;
  828. _OpenSSL_add_all_ciphers: TOpenSSL_add_all_ciphers = nil;
  829. _OpenSSL_add_all_digests: TOpenSSL_add_all_digests = nil;
  830. //
  831. _EVP_DigestInit: TEVP_DigestInit = nil;
  832. _EVP_DigestUpdate: TEVP_DigestUpdate = nil;
  833. _EVP_DigestFinal: TEVP_DigestFinal = nil;
  834. //
  835. _EVP_get_cipherbyname: TEVP_get_cipherbyname = nil;
  836. _EVP_get_digestbyname: TEVP_get_digestbyname = nil;
  837. //
  838. _EVP_CIPHER_CTX_init: TEVP_CIPHER_CTX_init = nil;
  839. _EVP_CIPHER_CTX_cleanup: TEVP_CIPHER_CTX_cleanup = nil;
  840. _EVP_CIPHER_CTX_set_key_length: TEVP_CIPHER_CTX_set_key_length = nil;
  841. _EVP_CIPHER_CTX_ctrl: TEVP_CIPHER_CTX_ctrl = nil;
  842. //
  843. _EVP_EncryptInit: TEVP_EncryptInit = nil;
  844. _EVP_EncryptUpdate: TEVP_EncryptUpdate = nil;
  845. _EVP_EncryptFinal: TEVP_EncryptFinal = nil;
  846. //
  847. _EVP_DecryptInit: TEVP_DecryptInit = nil;
  848. _EVP_DecryptUpdate: TEVP_DecryptUpdate = nil;
  849. _EVP_DecryptFinal: TEVP_DecryptFinal = nil;
  850. var
  851. SSLloaded: boolean = false;
  852. // libssl.dll
  853. function SslGetError(s: PSSL; ret_code: cInt):cInt;
  854. begin
  855. if InitSSLInterface and Assigned(_SslGetError) then
  856. Result := _SslGetError(s, ret_code)
  857. else
  858. Result := SSL_ERROR_SSL;
  859. end;
  860. function SslLibraryInit:cInt;
  861. begin
  862. if InitSSLInterface and Assigned(_SslLibraryInit) then
  863. Result := _SslLibraryInit
  864. else
  865. Result := 1;
  866. end;
  867. procedure SslLoadErrorStrings;
  868. begin
  869. if InitSSLInterface and Assigned(_SslLoadErrorStrings) then
  870. _SslLoadErrorStrings;
  871. end;
  872. function SslCtxSetCipherList(arg0: PSSL_CTX; var str: String):cInt;
  873. begin
  874. if InitSSLInterface and Assigned(_SslCtxSetCipherList) then
  875. Result := _SslCtxSetCipherList(arg0, PChar(str))
  876. else
  877. Result := 0;
  878. end;
  879. function SslCtxNew(meth: PSSL_METHOD):PSSL_CTX;
  880. begin
  881. if InitSSLInterface and Assigned(_SslCtxNew) then
  882. Result := _SslCtxNew(meth)
  883. else
  884. Result := nil;
  885. end;
  886. procedure SslCtxFree(arg0: PSSL_CTX);
  887. begin
  888. if InitSSLInterface and Assigned(_SslCtxFree) then
  889. _SslCtxFree(arg0);
  890. end;
  891. function SslSetFd(s: PSSL; fd: cInt):cInt;
  892. begin
  893. if InitSSLInterface and Assigned(_SslSetFd) then
  894. Result := _SslSetFd(s, fd)
  895. else
  896. Result := 0;
  897. end;
  898. function SslCtrl(ssl: PSSL; cmd: cInt; larg: clong; parg: Pointer): cLong;
  899. begin
  900. if InitSSLInterface and Assigned(_SslCtrl) then
  901. Result := _SslCtrl(ssl, cmd, larg, parg)
  902. else
  903. Result := 0;
  904. end;
  905. function SslCTXCtrl(ctx: PSSL_CTX; cmd: cInt; larg: clong; parg: Pointer
  906. ): cLong;
  907. begin
  908. if InitSSLInterface and Assigned(_SslCTXCtrl) then
  909. Result := _SslCTXCtrl(ctx, cmd, larg, parg)
  910. else
  911. Result := 0;
  912. end;
  913. function SSLCTXSetMode(ctx: PSSL_CTX; mode: cLong): cLong;
  914. begin
  915. Result := SslCTXCtrl(ctx, SSL_CTRL_MODE, mode, nil);
  916. end;
  917. function SSLSetMode(s: PSSL; mode: cLong): cLong;
  918. begin
  919. Result := SSLctrl(s, SSL_CTRL_MODE, mode, nil);
  920. end;
  921. function SSLCTXGetMode(ctx: PSSL_CTX): cLong;
  922. begin
  923. Result := SSLCTXctrl(ctx, SSL_CTRL_MODE, 0, nil);
  924. end;
  925. function SSLGetMode(s: PSSL): cLong;
  926. begin
  927. Result := SSLctrl(s, SSL_CTRL_MODE, 0, nil);
  928. end;
  929. function SslMethodV2:PSSL_METHOD;
  930. begin
  931. if InitSSLInterface and Assigned(_SslMethodV2) then
  932. Result := _SslMethodV2
  933. else
  934. Result := nil;
  935. end;
  936. function SslMethodV3:PSSL_METHOD;
  937. begin
  938. if InitSSLInterface and Assigned(_SslMethodV3) then
  939. Result := _SslMethodV3
  940. else
  941. Result := nil;
  942. end;
  943. function SslMethodTLSV1:PSSL_METHOD;
  944. begin
  945. if InitSSLInterface and Assigned(_SslMethodTLSV1) then
  946. Result := _SslMethodTLSV1
  947. else
  948. Result := nil;
  949. end;
  950. function SslMethodV23:PSSL_METHOD;
  951. begin
  952. if InitSSLInterface and Assigned(_SslMethodV23) then
  953. Result := _SslMethodV23
  954. else
  955. Result := nil;
  956. end;
  957. function SslCtxUsePrivateKey(ctx: PSSL_CTX; pkey: SslPtr):cInt;
  958. begin
  959. if InitSSLInterface and Assigned(_SslCtxUsePrivateKey) then
  960. Result := _SslCtxUsePrivateKey(ctx, pkey)
  961. else
  962. Result := 0;
  963. end;
  964. function SslCtxUsePrivateKeyASN1(pk: cInt; ctx: PSSL_CTX; d: String; len: cLong):cInt;
  965. begin
  966. if InitSSLInterface and Assigned(_SslCtxUsePrivateKeyASN1) then
  967. Result := _SslCtxUsePrivateKeyASN1(pk, ctx, Sslptr(d), len)
  968. else
  969. Result := 0;
  970. end;
  971. function SslCtxUsePrivateKeyFile(ctx: PSSL_CTX; const _file: String; _type: cInt):cInt;
  972. begin
  973. if InitSSLInterface and Assigned(_SslCtxUsePrivateKeyFile) then
  974. Result := _SslCtxUsePrivateKeyFile(ctx, PChar(_file), _type)
  975. else
  976. Result := 0;
  977. end;
  978. function SslCtxUseCertificate(ctx: PSSL_CTX; x: SslPtr):cInt;
  979. begin
  980. if InitSSLInterface and Assigned(_SslCtxUseCertificate) then
  981. Result := _SslCtxUseCertificate(ctx, x)
  982. else
  983. Result := 0;
  984. end;
  985. function SslCtxUseCertificateASN1(ctx: PSSL_CTX; len: cLong; d: String):cInt;
  986. begin
  987. if InitSSLInterface and Assigned(_SslCtxUseCertificateASN1) then
  988. Result := _SslCtxUseCertificateASN1(ctx, len, SslPtr(d))
  989. else
  990. Result := 0;
  991. end;
  992. function SslCtxUseCertificateFile(ctx: PSSL_CTX; const _file: String; _type: cInt):cInt;
  993. begin
  994. if InitSSLInterface and Assigned(_SslCtxUseCertificateFile) then
  995. Result := _SslCtxUseCertificateFile(ctx, PChar(_file), _type)
  996. else
  997. Result := 0;
  998. end;
  999. function SslCtxUseCertificateChainFile(ctx: PSSL_CTX; const _file: String):cInt;
  1000. begin
  1001. if InitSSLInterface and Assigned(_SslCtxUseCertificateChainFile) then
  1002. Result := _SslCtxUseCertificateChainFile(ctx, PChar(_file))
  1003. else
  1004. Result := 0;
  1005. end;
  1006. function SslCtxCheckPrivateKeyFile(ctx: PSSL_CTX):cInt;
  1007. begin
  1008. if InitSSLInterface and Assigned(_SslCtxCheckPrivateKeyFile) then
  1009. Result := _SslCtxCheckPrivateKeyFile(ctx)
  1010. else
  1011. Result := 0;
  1012. end;
  1013. procedure SslCtxSetDefaultPasswdCb(ctx: PSSL_CTX; cb: PPasswdCb);
  1014. begin
  1015. if InitSSLInterface and Assigned(_SslCtxSetDefaultPasswdCb) then
  1016. _SslCtxSetDefaultPasswdCb(ctx, cb);
  1017. end;
  1018. procedure SslCtxSetDefaultPasswdCbUserdata(ctx: PSSL_CTX; u: SslPtr);
  1019. begin
  1020. if InitSSLInterface and Assigned(_SslCtxSetDefaultPasswdCbUserdata) then
  1021. _SslCtxSetDefaultPasswdCbUserdata(ctx, u);
  1022. end;
  1023. function SslCtxLoadVerifyLocations(ctx: PSSL_CTX; const CAfile: String; const CApath: String):cInt;
  1024. begin
  1025. if InitSSLInterface and Assigned(_SslCtxLoadVerifyLocations) then
  1026. Result := _SslCtxLoadVerifyLocations(ctx, SslPtr(CAfile), SslPtr(CApath))
  1027. else
  1028. Result := 0;
  1029. end;
  1030. function SslNew(ctx: PSSL_CTX):PSSL;
  1031. begin
  1032. if InitSSLInterface and Assigned(_SslNew) then
  1033. Result := _SslNew(ctx)
  1034. else
  1035. Result := nil;
  1036. end;
  1037. procedure SslFree(ssl: PSSL);
  1038. begin
  1039. if InitSSLInterface and Assigned(_SslFree) then
  1040. _SslFree(ssl);
  1041. end;
  1042. function SslAccept(ssl: PSSL):cInt;
  1043. begin
  1044. if InitSSLInterface and Assigned(_SslAccept) then
  1045. Result := _SslAccept(ssl)
  1046. else
  1047. Result := -1;
  1048. end;
  1049. function SslConnect(ssl: PSSL):cInt;
  1050. begin
  1051. if InitSSLInterface and Assigned(_SslConnect) then
  1052. Result := _SslConnect(ssl)
  1053. else
  1054. Result := -1;
  1055. end;
  1056. function SslShutdown(ssl: PSSL):cInt;
  1057. begin
  1058. if InitSSLInterface and Assigned(_SslShutdown) then
  1059. Result := _SslShutdown(ssl)
  1060. else
  1061. Result := -1;
  1062. end;
  1063. function SslRead(ssl: PSSL; buf: SslPtr; num: cInt):cInt;
  1064. begin
  1065. if InitSSLInterface and Assigned(_SslRead) then
  1066. Result := _SslRead(ssl, PChar(buf), num)
  1067. else
  1068. Result := -1;
  1069. end;
  1070. function SslPeek(ssl: PSSL; buf: SslPtr; num: cInt):cInt;
  1071. begin
  1072. if InitSSLInterface and Assigned(_SslPeek) then
  1073. Result := _SslPeek(ssl, PChar(buf), num)
  1074. else
  1075. Result := -1;
  1076. end;
  1077. function SslWrite(ssl: PSSL; buf: SslPtr; num: cInt):cInt;
  1078. begin
  1079. if InitSSLInterface and Assigned(_SslWrite) then
  1080. Result := _SslWrite(ssl, PChar(buf), num)
  1081. else
  1082. Result := -1;
  1083. end;
  1084. function SslPending(ssl: PSSL):cInt;
  1085. begin
  1086. if InitSSLInterface and Assigned(_SslPending) then
  1087. Result := _SslPending(ssl)
  1088. else
  1089. Result := 0;
  1090. end;
  1091. //function SslGetVersion(ssl: PSSL):PChar;
  1092. function SslGetVersion(ssl: PSSL):String;
  1093. begin
  1094. if InitSSLInterface and Assigned(_SslGetVersion) then
  1095. Result := _SslGetVersion(ssl)
  1096. else
  1097. Result := '';
  1098. end;
  1099. function SslGetPeerCertificate(ssl: PSSL):PX509;
  1100. begin
  1101. if InitSSLInterface and Assigned(_SslGetPeerCertificate) then
  1102. Result := _SslGetPeerCertificate(ssl)
  1103. else
  1104. Result := nil;
  1105. end;
  1106. procedure SslCtxSetVerify(ctx: PSSL_CTX; mode: cInt; arg2: PFunction);
  1107. begin
  1108. if InitSSLInterface and Assigned(_SslCtxSetVerify) then
  1109. _SslCtxSetVerify(ctx, mode, @arg2);
  1110. end;
  1111. function SSLGetCurrentCipher(s: PSSL):SslPtr;
  1112. begin
  1113. if InitSSLInterface and Assigned(_SSLGetCurrentCipher) then
  1114. {$IFDEF CIL}
  1115. {$ELSE}
  1116. Result := _SSLGetCurrentCipher(s)
  1117. {$ENDIF}
  1118. else
  1119. Result := nil;
  1120. end;
  1121. function SSLCipherGetName(c: SslPtr):String;
  1122. begin
  1123. if InitSSLInterface and Assigned(_SSLCipherGetName) then
  1124. Result := _SSLCipherGetName(c)
  1125. else
  1126. Result := '';
  1127. end;
  1128. function SSLCipherGetBits(c: SslPtr; var alg_bits: cInt):cInt;
  1129. begin
  1130. if InitSSLInterface and Assigned(_SSLCipherGetBits) then
  1131. Result := _SSLCipherGetBits(c, @alg_bits)
  1132. else
  1133. Result := 0;
  1134. end;
  1135. function SSLGetVerifyResult(ssl: PSSL):cLong;
  1136. begin
  1137. if InitSSLInterface and Assigned(_SSLGetVerifyResult) then
  1138. Result := _SSLGetVerifyResult(ssl)
  1139. else
  1140. Result := X509_V_ERR_APPLICATION_VERIFICATION;
  1141. end;
  1142. // libeay.dll
  1143. function X509New: PX509;
  1144. begin
  1145. if InitSSLInterface and Assigned(_X509New) then
  1146. Result := _X509New
  1147. else
  1148. Result := nil;
  1149. end;
  1150. procedure X509Free(x: PX509);
  1151. begin
  1152. if InitSSLInterface and Assigned(_X509Free) then
  1153. _X509Free(x);
  1154. end;
  1155. function X509NameOneline(a: PX509_NAME; var buf: String; size: cInt):String;
  1156. begin
  1157. if InitSSLInterface and Assigned(_X509NameOneline) then
  1158. Result := _X509NameOneline(a, PChar(buf),size)
  1159. else
  1160. Result := '';
  1161. end;
  1162. function X509GetSubjectName(a: PX509):PX509_NAME;
  1163. begin
  1164. if InitSSLInterface and Assigned(_X509GetSubjectName) then
  1165. Result := _X509GetSubjectName(a)
  1166. else
  1167. Result := nil;
  1168. end;
  1169. function X509GetIssuerName(a: PX509):PX509_NAME;
  1170. begin
  1171. if InitSSLInterface and Assigned(_X509GetIssuerName) then
  1172. Result := _X509GetIssuerName(a)
  1173. else
  1174. Result := nil;
  1175. end;
  1176. function X509NameHash(x: PX509_NAME):cuLong;
  1177. begin
  1178. if InitSSLInterface and Assigned(_X509NameHash) then
  1179. Result := _X509NameHash(x)
  1180. else
  1181. Result := 0;
  1182. end;
  1183. function X509Digest(data: PX509; _type: PEVP_MD; md: String; var len: cInt):cInt;
  1184. begin
  1185. if InitSSLInterface and Assigned(_X509Digest) then
  1186. Result := _X509Digest(data, _type, PChar(md), @len)
  1187. else
  1188. Result := 0;
  1189. end;
  1190. function EvpPkeyNew: EVP_PKEY;
  1191. begin
  1192. if InitSSLInterface and Assigned(_EvpPkeyNew) then
  1193. Result := _EvpPkeyNew
  1194. else
  1195. Result := nil;
  1196. end;
  1197. procedure EvpPkeyFree(pk: EVP_PKEY);
  1198. begin
  1199. if InitSSLInterface and Assigned(_EvpPkeyFree) then
  1200. _EvpPkeyFree(pk);
  1201. end;
  1202. function SSLeayversion(t: cInt): string;
  1203. begin
  1204. if InitSSLInterface and Assigned(_SSLeayversion) then
  1205. Result := PChar(_SSLeayversion(t))
  1206. else
  1207. Result := '';
  1208. end;
  1209. procedure ErrErrorString(e: cInt; var buf: string; len: cInt);
  1210. begin
  1211. if InitSSLInterface and Assigned(_ErrErrorString) then
  1212. _ErrErrorString(e, Pointer(buf), len);
  1213. buf := PChar(Buf);
  1214. end;
  1215. function ErrGetError: cInt;
  1216. begin
  1217. if InitSSLInterface and Assigned(_ErrGetError) then
  1218. Result := _ErrGetError
  1219. else
  1220. Result := SSL_ERROR_SSL;
  1221. end;
  1222. procedure ErrClearError;
  1223. begin
  1224. if InitSSLInterface and Assigned(_ErrClearError) then
  1225. _ErrClearError;
  1226. end;
  1227. procedure ErrFreeStrings;
  1228. begin
  1229. if InitSSLInterface and Assigned(_ErrFreeStrings) then
  1230. _ErrFreeStrings;
  1231. end;
  1232. procedure ErrRemoveState(pid: cInt);
  1233. begin
  1234. if InitSSLInterface and Assigned(_ErrRemoveState) then
  1235. _ErrRemoveState(pid);
  1236. end;
  1237. procedure EVPcleanup;
  1238. begin
  1239. if InitSSLInterface and Assigned(_EVPcleanup) then
  1240. _EVPcleanup;
  1241. end;
  1242. procedure RandScreen;
  1243. begin
  1244. if InitSSLInterface and Assigned(_RandScreen) then
  1245. _RandScreen;
  1246. end;
  1247. function BioNew(b: PBIO_METHOD): PBIO;
  1248. begin
  1249. if InitSSLInterface and Assigned(_BioNew) then
  1250. Result := _BioNew(b)
  1251. else
  1252. Result := nil;
  1253. end;
  1254. procedure BioFreeAll(b: PBIO);
  1255. begin
  1256. if InitSSLInterface and Assigned(_BioFreeAll) then
  1257. _BioFreeAll(b);
  1258. end;
  1259. function BioSMem: PBIO_METHOD;
  1260. begin
  1261. if InitSSLInterface and Assigned(_BioSMem) then
  1262. Result := _BioSMem
  1263. else
  1264. Result := nil;
  1265. end;
  1266. function BioCtrlPending(b: PBIO): cInt;
  1267. begin
  1268. if InitSSLInterface and Assigned(_BioCtrlPending) then
  1269. Result := _BioCtrlPending(b)
  1270. else
  1271. Result := 0;
  1272. end;
  1273. function BioRead(b: PBIO; var Buf: String; Len: cInt): cInt;
  1274. begin
  1275. if InitSSLInterface and Assigned(_BioRead) then
  1276. Result := _BioRead(b, PChar(Buf), Len)
  1277. else
  1278. Result := -2;
  1279. end;
  1280. //function BioWrite(b: PBIO; Buf: PChar; Len: cInt): cInt;
  1281. function BioWrite(b: PBIO; Buf: String; Len: cInt): cInt;
  1282. begin
  1283. if InitSSLInterface and Assigned(_BioWrite) then
  1284. Result := _BioWrite(b, PChar(Buf), Len)
  1285. else
  1286. Result := -2;
  1287. end;
  1288. function X509print(b: PBIO; a: PX509): cInt;
  1289. begin
  1290. if InitSSLInterface and Assigned(_X509print) then
  1291. Result := _X509print(b, a)
  1292. else
  1293. Result := 0;
  1294. end;
  1295. function d2iPKCS12bio(b:PBIO; Pkcs12: SslPtr): SslPtr;
  1296. begin
  1297. if InitSSLInterface and Assigned(_d2iPKCS12bio) then
  1298. Result := _d2iPKCS12bio(b, Pkcs12)
  1299. else
  1300. Result := nil;
  1301. end;
  1302. function PKCS12parse(p12: SslPtr; pass: string; var pkey, cert, ca: SslPtr): cInt;
  1303. begin
  1304. if InitSSLInterface and Assigned(_PKCS12parse) then
  1305. Result := _PKCS12parse(p12, SslPtr(pass), pkey, cert, ca)
  1306. else
  1307. Result := 0;
  1308. end;
  1309. procedure PKCS12free(p12: SslPtr);
  1310. begin
  1311. if InitSSLInterface and Assigned(_PKCS12free) then
  1312. _PKCS12free(p12);
  1313. end;
  1314. function EvpPkeyAssign(pkey: EVP_PKEY; _type: cInt; key: Prsa): cInt;
  1315. begin
  1316. if InitSSLInterface and Assigned(_EvpPkeyAssign) then
  1317. Result := _EvpPkeyAssign(pkey, _type, key)
  1318. else
  1319. Result := 0;
  1320. end;
  1321. function X509SetVersion(x: PX509; version: cInt): cInt;
  1322. begin
  1323. if InitSSLInterface and Assigned(_X509SetVersion) then
  1324. Result := _X509SetVersion(x, version)
  1325. else
  1326. Result := 0;
  1327. end;
  1328. function X509SetPubkey(x: PX509; pkey: EVP_PKEY): cInt;
  1329. begin
  1330. if InitSSLInterface and Assigned(_X509SetPubkey) then
  1331. Result := _X509SetPubkey(x, pkey)
  1332. else
  1333. Result := 0;
  1334. end;
  1335. function X509SetIssuerName(x: PX509; name: PX509_NAME): cInt;
  1336. begin
  1337. if InitSSLInterface and Assigned(_X509SetIssuerName) then
  1338. Result := _X509SetIssuerName(x, name)
  1339. else
  1340. Result := 0;
  1341. end;
  1342. function X509NameAddEntryByTxt(name: PX509_NAME; field: string; _type: cInt;
  1343. bytes: string; len, loc, _set: cInt): cInt;
  1344. begin
  1345. if InitSSLInterface and Assigned(_X509NameAddEntryByTxt) then
  1346. Result := _X509NameAddEntryByTxt(name, PChar(field), _type, PChar(Bytes), len, loc, _set)
  1347. else
  1348. Result := 0;
  1349. end;
  1350. function X509Sign(x: PX509; pkey: EVP_PKEY; const md: PEVP_MD): cInt;
  1351. begin
  1352. if InitSSLInterface and Assigned(_X509Sign) then
  1353. Result := _X509Sign(x, pkey, md)
  1354. else
  1355. Result := 0;
  1356. end;
  1357. function Asn1UtctimeNew: PASN1_UTCTIME;
  1358. begin
  1359. if InitSSLInterface and Assigned(_Asn1UtctimeNew) then
  1360. Result := _Asn1UtctimeNew
  1361. else
  1362. Result := nil;
  1363. end;
  1364. procedure Asn1UtctimeFree(a: PASN1_UTCTIME);
  1365. begin
  1366. if InitSSLInterface and Assigned(_Asn1UtctimeFree) then
  1367. _Asn1UtctimeFree(a);
  1368. end;
  1369. function X509GmtimeAdj(s: PASN1_UTCTIME; adj: cInt): PASN1_UTCTIME;
  1370. begin
  1371. if InitSSLInterface and Assigned(_X509GmtimeAdj) then
  1372. Result := _X509GmtimeAdj(s, adj)
  1373. else
  1374. Result := nil;
  1375. end;
  1376. function X509SetNotBefore(x: PX509; tm: PASN1_UTCTIME): cInt;
  1377. begin
  1378. if InitSSLInterface and Assigned(_X509SetNotBefore) then
  1379. Result := _X509SetNotBefore(x, tm)
  1380. else
  1381. Result := 0;
  1382. end;
  1383. function X509SetNotAfter(x: PX509; tm: PASN1_UTCTIME): cInt;
  1384. begin
  1385. if InitSSLInterface and Assigned(_X509SetNotAfter) then
  1386. Result := _X509SetNotAfter(x, tm)
  1387. else
  1388. Result := 0;
  1389. end;
  1390. function i2dX509bio(b: PBIO; x: PX509): cInt;
  1391. begin
  1392. if InitSSLInterface and Assigned(_i2dX509bio) then
  1393. Result := _i2dX509bio(b, x)
  1394. else
  1395. Result := 0;
  1396. end;
  1397. function i2dPrivateKeyBio(b: PBIO; pkey: EVP_PKEY): cInt;
  1398. begin
  1399. if InitSSLInterface and Assigned(_i2dPrivateKeyBio) then
  1400. Result := _i2dPrivateKeyBio(b, pkey)
  1401. else
  1402. Result := 0;
  1403. end;
  1404. function EvpGetDigestByName(Name: String): PEVP_MD;
  1405. begin
  1406. if InitSSLInterface and Assigned(_EvpGetDigestByName) then
  1407. Result := _EvpGetDigestByName(PChar(Name))
  1408. else
  1409. Result := nil;
  1410. end;
  1411. function X509GetSerialNumber(x: PX509): PASN1_cInt;
  1412. begin
  1413. if InitSSLInterface and Assigned(_X509GetSerialNumber) then
  1414. Result := _X509GetSerialNumber(x)
  1415. else
  1416. Result := nil;
  1417. end;
  1418. // 3DES functions
  1419. procedure DESsetoddparity(Key: des_cblock);
  1420. begin
  1421. if InitSSLInterface and Assigned(_DESsetoddparity) then
  1422. _DESsetoddparity(Key);
  1423. end;
  1424. function DESsetkeychecked(key: des_cblock; schedule: des_key_schedule): cInt;
  1425. begin
  1426. if InitSSLInterface and Assigned(_DESsetkeychecked) then
  1427. Result := _DESsetkeychecked(key, schedule)
  1428. else
  1429. Result := -1;
  1430. end;
  1431. procedure DESecbencrypt(Input: des_cblock; output: des_cblock; ks: des_key_schedule; enc: cInt);
  1432. begin
  1433. if InitSSLInterface and Assigned(_DESecbencrypt) then
  1434. _DESecbencrypt(Input, output, ks, enc);
  1435. end;
  1436. // RAND functions
  1437. function RAND_set_rand_method(const meth: PRAND_METHOD): cint;
  1438. begin
  1439. if InitSSLInterface and Assigned(_RAND_set_rand_method) then
  1440. Result := _RAND_set_rand_method(meth)
  1441. else
  1442. Result := -1;
  1443. end;
  1444. function RAND_get_rand_method: PRAND_METHOD;
  1445. begin
  1446. if InitSSLInterface and Assigned(_RAND_get_rand_method) then
  1447. Result := _RAND_get_rand_method()
  1448. else
  1449. Result := nil;
  1450. end;
  1451. function RAND_SSLeay: PRAND_METHOD;
  1452. begin
  1453. if InitSSLInterface and Assigned(_RAND_SSLeay) then
  1454. Result := _RAND_SSLeay()
  1455. else
  1456. Result := nil;
  1457. end;
  1458. procedure RAND_cleanup;
  1459. begin
  1460. if InitSSLInterface and Assigned(_RAND_cleanup) then
  1461. _RAND_cleanup();
  1462. end;
  1463. function RAND_bytes(buf: PByte; num: cint): cint;
  1464. begin
  1465. if InitSSLInterface and Assigned(_RAND_bytes) then
  1466. Result := _RAND_bytes(buf, num)
  1467. else
  1468. Result := -1;
  1469. end;
  1470. function RAND_pseudo_bytes(buf: PByte; num: cint): cint;
  1471. begin
  1472. if InitSSLInterface and Assigned(_RAND_pseudo_bytes) then
  1473. Result := _RAND_pseudo_bytes(buf, num)
  1474. else
  1475. Result := -1;
  1476. end;
  1477. procedure RAND_seed(const buf: Pointer; num: cint);
  1478. begin
  1479. if InitSSLInterface and Assigned(_RAND_seed) then
  1480. _RAND_seed(buf, num);
  1481. end;
  1482. procedure RAND_add(const buf: Pointer; num: cint; entropy: cdouble);
  1483. begin
  1484. if InitSSLInterface and Assigned(_RAND_add) then
  1485. _RAND_add(buf, num, entropy);
  1486. end;
  1487. function RAND_load_file(const file_name: PChar; max_bytes: clong): cint;
  1488. begin
  1489. if InitSSLInterface and Assigned(_RAND_load_file) then
  1490. Result := _RAND_load_file(file_name, max_bytes)
  1491. else
  1492. Result := -1;
  1493. end;
  1494. function RAND_write_file(const file_name: PChar): cint;
  1495. begin
  1496. if InitSSLInterface and Assigned(_RAND_write_file) then
  1497. Result := _RAND_write_file(file_name)
  1498. else
  1499. Result := -1;
  1500. end;
  1501. function RAND_file_name(file_name: PChar; num: csize_t): PChar;
  1502. begin
  1503. if InitSSLInterface and Assigned(_RAND_file_name) then
  1504. Result := _RAND_file_name(file_name, num)
  1505. else
  1506. Result := nil;
  1507. end;
  1508. function RAND_status: cint;
  1509. begin
  1510. if InitSSLInterface and Assigned(_RAND_status) then
  1511. Result := _RAND_status()
  1512. else
  1513. Result := -1;
  1514. end;
  1515. function RAND_query_egd_bytes(const path: PChar; buf: PByte; bytes: cint): cint;
  1516. begin
  1517. if InitSSLInterface and Assigned(_RAND_query_egd_bytes) then
  1518. Result := _RAND_query_egd_bytes(path, buf, bytes)
  1519. else
  1520. Result := -1;
  1521. end;
  1522. function RAND_egd(const path: PChar): cint;
  1523. begin
  1524. if InitSSLInterface and Assigned(_RAND_egd) then
  1525. Result := _RAND_egd(path)
  1526. else
  1527. Result := -1;
  1528. end;
  1529. function RAND_egd_bytes(const path: PChar; bytes: cint): cint;
  1530. begin
  1531. if InitSSLInterface and Assigned(_RAND_egd_bytes) then
  1532. Result := _RAND_egd_bytes(path, bytes)
  1533. else
  1534. Result := -1;
  1535. end;
  1536. procedure ERR_load_RAND_strings;
  1537. begin
  1538. if InitSSLInterface and Assigned(_ERR_load_RAND_strings) then
  1539. _ERR_load_RAND_strings();
  1540. end;
  1541. function RAND_poll: cint;
  1542. begin
  1543. if InitSSLInterface and Assigned(_RAND_poll) then
  1544. Result := _RAND_poll()
  1545. else
  1546. Result := -1;
  1547. end;
  1548. // RSA Functions
  1549. function RSA_new(): PRSA;
  1550. begin
  1551. if InitSSLInterface and Assigned(_RSA_new) then
  1552. Result := _RSA_new()
  1553. else
  1554. Result := nil;
  1555. end;
  1556. function RSA_new_method(method: PENGINE): PRSA;
  1557. begin
  1558. if InitSSLInterface and Assigned(_RSA_new_method) then
  1559. Result := _RSA_new_method(method)
  1560. else
  1561. Result := nil;
  1562. end;
  1563. function RSA_size(arsa: PRSA): cint;
  1564. begin
  1565. if InitSSLInterface and Assigned(_RSA_size) then
  1566. Result := _RSA_size(arsa)
  1567. else
  1568. Result := -1;
  1569. end;
  1570. function RsaGenerateKey(bits, e: cInt; callback: PFunction; cb_arg: SslPtr): PRSA;
  1571. begin
  1572. if InitSSLInterface and Assigned(_RsaGenerateKey) then
  1573. Result := _RsaGenerateKey(bits, e, callback, cb_arg)
  1574. else
  1575. Result := nil;
  1576. end;
  1577. function RSA_generate_key_ex(arsa: PRSA; bits: cInt; e: PBIGNUM; cb: PBN_GENCB): PRSA;
  1578. begin
  1579. if InitSSLInterface and Assigned(_RSA_generate_key_ex) then
  1580. Result := _RSA_generate_key_ex(arsa, bits, e, cb)
  1581. else
  1582. Result := nil;
  1583. end;
  1584. function RSA_check_key(arsa: PRSA): cint;
  1585. begin
  1586. if InitSSLInterface and Assigned(_RSA_check_key) then
  1587. Result := _RSA_check_key(arsa)
  1588. else
  1589. Result := -1;
  1590. end;
  1591. function RSA_public_encrypt(flen: cint; from_buf, to_buf: PByte; arsa: PRSA; padding: cint): cint;
  1592. begin
  1593. if InitSSLInterface and Assigned(_RSA_public_encrypt) then
  1594. Result := _RSA_public_encrypt(flen, from_buf, to_buf, arsa, padding)
  1595. else
  1596. Result := -1;
  1597. end;
  1598. function RSA_private_encrypt(flen: cint; from_buf, to_buf: PByte; arsa: PRSA; padding: cint): cint;
  1599. begin
  1600. if InitSSLInterface and Assigned(_RSA_private_encrypt) then
  1601. Result := _RSA_private_encrypt(flen, from_buf, to_buf, arsa, padding)
  1602. else
  1603. Result := -1;
  1604. end;
  1605. function RSA_public_decrypt(flen: cint; from_buf, to_buf: PByte; arsa: PRSA; padding: cint): cint;
  1606. begin
  1607. if InitSSLInterface and Assigned(_RSA_public_decrypt) then
  1608. Result := _RSA_public_decrypt(flen, from_buf, to_buf, arsa, padding)
  1609. else
  1610. Result := -1;
  1611. end;
  1612. function RSA_private_decrypt(flen: cint; from_buf, to_buf: PByte; arsa: PRSA; padding: cint): cint;
  1613. begin
  1614. if InitSSLInterface and Assigned(_RSA_private_decrypt) then
  1615. Result := _RSA_private_decrypt(flen, from_buf, to_buf, arsa, padding)
  1616. else
  1617. Result := -1;
  1618. end;
  1619. procedure RSA_free(arsa: PRSA);
  1620. begin
  1621. if InitSSLInterface and Assigned(_RSA_free) then
  1622. _RSA_free(arsa);
  1623. end;
  1624. function RSA_flags(arsa: PRSA): Integer;
  1625. begin
  1626. if InitSSLInterface and Assigned(_RSA_flags) then
  1627. Result := _RSA_flags(arsa)
  1628. else
  1629. Result := -1;
  1630. end;
  1631. procedure RSA_set_default_method(method: PRSA_METHOD);
  1632. begin
  1633. if InitSSLInterface and Assigned(_RSA_set_default_method) then
  1634. _RSA_set_default_method(method);
  1635. end;
  1636. function RSA_get_default_method: PRSA_METHOD;
  1637. begin
  1638. if InitSSLInterface and Assigned(_RSA_get_default_method) then
  1639. Result := _RSA_get_default_method()
  1640. else
  1641. Result := nil;
  1642. end;
  1643. function RSA_get_method(arsa: PRSA): PRSA_METHOD;
  1644. begin
  1645. if InitSSLInterface and Assigned(_RSA_get_method) then
  1646. Result := _RSA_get_method(arsa)
  1647. else
  1648. Result := nil;
  1649. end;
  1650. function RSA_set_method(arsa: PRSA; method: PRSA_METHOD): PRSA_METHOD;
  1651. begin
  1652. if InitSSLInterface and Assigned(_RSA_set_method) then
  1653. Result := _RSA_set_method(arsa, method)
  1654. else
  1655. Result := nil;
  1656. end;
  1657. function d2i_RSAPublicKey(arsa: PPRSA; pp: PPByte; len: cint): PRSA;
  1658. begin
  1659. if InitSSLInterface and Assigned(_d2i_RSAPublicKey) then
  1660. Result := _d2i_RSAPublicKey(arsa, pp, len)
  1661. else
  1662. Result := nil;
  1663. end;
  1664. function i2d_RSAPublicKey(arsa: PRSA; pp: PPByte): cint;
  1665. begin
  1666. if InitSSLInterface and Assigned(_i2d_RSAPublicKey) then
  1667. Result := _i2d_RSAPublicKey(arsa, pp)
  1668. else
  1669. Result := -1;
  1670. end;
  1671. function d2i_RSAPrivateKey(arsa: PPRSA; pp: PPByte; len: cint): PRSA;
  1672. begin
  1673. if InitSSLInterface and Assigned(_d2i_RSAPrivateKey) then
  1674. Result := _d2i_RSAPrivateKey(arsa, pp, len)
  1675. else
  1676. Result := nil;
  1677. end;
  1678. function i2d_RSAPrivateKey(arsa: PRSA; pp: PPByte): cint;
  1679. begin
  1680. if InitSSLInterface and Assigned(_i2d_RSAPrivateKey) then
  1681. Result := _i2d_RSAPrivateKey(arsa, pp)
  1682. else
  1683. Result := -1;
  1684. end;
  1685. // ERR Functions
  1686. function Err_Error_String(e: cInt; buf: PChar): PChar;
  1687. begin
  1688. if InitSSLInterface and Assigned(_Err_Error_String) then
  1689. Result := _Err_Error_String(e, buf)
  1690. else
  1691. Result := nil;
  1692. end;
  1693. // Crypto Functions
  1694. function SSLeay_version(t: cint): PChar;
  1695. begin
  1696. if InitSSLInterface and Assigned(_SSLeay_version) then
  1697. Result := _SSLeay_version(t)
  1698. else
  1699. Result := nil;
  1700. end;
  1701. // EVP Functions
  1702. procedure OpenSSL_add_all_algorithms;
  1703. begin
  1704. if InitSSLInterface and Assigned(_OpenSSL_add_all_algorithms) then
  1705. _OpenSSL_add_all_algorithms();
  1706. end;
  1707. procedure OpenSSL_add_all_ciphers;
  1708. begin
  1709. if InitSSLInterface and Assigned(_OpenSSL_add_all_ciphers) then
  1710. _OpenSSL_add_all_ciphers();
  1711. end;
  1712. procedure OpenSSL_add_all_digests;
  1713. begin
  1714. if InitSSLInterface and Assigned(_OpenSSL_add_all_digests) then
  1715. _OpenSSL_add_all_digests();
  1716. end;
  1717. //
  1718. function EVP_DigestInit(ctx: PEVP_MD_CTX; type_: PEVP_MD): cint;
  1719. begin
  1720. if InitSSLInterface and Assigned(_EVP_DigestInit) then
  1721. Result := _EVP_DigestInit(ctx, type_)
  1722. else
  1723. Result := -1;
  1724. end;
  1725. function EVP_DigestUpdate(ctx: PEVP_MD_CTX; const data: Pointer; cnt: csize_t): cint;
  1726. begin
  1727. if InitSSLInterface and Assigned(_EVP_DigestUpdate) then
  1728. Result := _EVP_DigestUpdate(ctx, data, cnt)
  1729. else
  1730. Result := -1;
  1731. end;
  1732. function EVP_DigestFinal(ctx: PEVP_MD_CTX; md: PByte; s: pcuint): cint;
  1733. begin
  1734. if InitSSLInterface and Assigned(_EVP_DigestFinal) then
  1735. Result := _EVP_DigestFinal(ctx, md, s)
  1736. else
  1737. Result := -1;
  1738. end;
  1739. //
  1740. function EVP_get_cipherbyname(const name: PChar): PEVP_CIPHER;
  1741. begin
  1742. if InitSSLInterface and Assigned(_EVP_get_cipherbyname) then
  1743. Result := _EVP_get_cipherbyname(name)
  1744. else
  1745. Result := nil;
  1746. end;
  1747. function EVP_get_digestbyname(const name: PChar): PEVP_MD;
  1748. begin
  1749. if InitSSLInterface and Assigned(_EVP_get_digestbyname) then
  1750. Result := _EVP_get_digestbyname(name)
  1751. else
  1752. Result := nil;
  1753. end;
  1754. //
  1755. procedure EVP_CIPHER_CTX_init(a: PEVP_CIPHER_CTX);
  1756. begin
  1757. if InitSSLInterface and Assigned(_EVP_CIPHER_CTX_init) then
  1758. _EVP_CIPHER_CTX_init(a);
  1759. end;
  1760. function EVP_CIPHER_CTX_cleanup(a: PEVP_CIPHER_CTX): cint;
  1761. begin
  1762. if InitSSLInterface and Assigned(_EVP_CIPHER_CTX_cleanup) then
  1763. Result := _EVP_CIPHER_CTX_cleanup(a)
  1764. else
  1765. Result := -1;
  1766. end;
  1767. function EVP_CIPHER_CTX_set_key_length(x: PEVP_CIPHER_CTX; keylen: cint): cint;
  1768. begin
  1769. if InitSSLInterface and Assigned(_EVP_CIPHER_CTX_set_key_length) then
  1770. Result := _EVP_CIPHER_CTX_set_key_length(x, keylen)
  1771. else
  1772. Result := -1;
  1773. end;
  1774. function EVP_CIPHER_CTX_ctrl(ctx: PEVP_CIPHER_CTX; type_, arg: cint; ptr: Pointer): cint;
  1775. begin
  1776. if InitSSLInterface and Assigned(_EVP_CIPHER_CTX_ctrl) then
  1777. Result := _EVP_CIPHER_CTX_ctrl(ctx, type_, arg, ptr)
  1778. else
  1779. Result := -1;
  1780. end;
  1781. //
  1782. function EVP_EncryptInit(ctx: PEVP_CIPHER_CTX; const chipher_: PEVP_CIPHER;
  1783. const key, iv: PByte): cint;
  1784. begin
  1785. if InitSSLInterface and Assigned(_EVP_EncryptInit) then
  1786. Result := _EVP_EncryptInit(ctx, chipher_, key, iv)
  1787. else
  1788. Result := -1;
  1789. end;
  1790. function EVP_EncryptUpdate(ctx: PEVP_CIPHER_CTX; out_: pcuchar;
  1791. outlen: pcint; const in_: pcuchar; inlen: cint): cint;
  1792. begin
  1793. if InitSSLInterface and Assigned(_EVP_EncryptUpdate) then
  1794. Result := _EVP_EncryptUpdate(ctx, out_, outlen, in_, inlen)
  1795. else
  1796. Result := -1;
  1797. end;
  1798. function EVP_EncryptFinal(ctx: PEVP_CIPHER_CTX; out_data: PByte; outlen: pcint): cint;
  1799. begin
  1800. if InitSSLInterface and Assigned(_EVP_EncryptFinal) then
  1801. Result := _EVP_EncryptFinal(ctx, out_data, outlen)
  1802. else
  1803. Result := -1;
  1804. end;
  1805. //
  1806. function EVP_DecryptInit(ctx: PEVP_CIPHER_CTX; chiphir_type: PEVP_CIPHER;
  1807. const key, iv: PByte): cint;
  1808. begin
  1809. if InitSSLInterface and Assigned(_EVP_DecryptInit) then
  1810. Result := _EVP_DecryptInit(ctx, chiphir_type, key, iv)
  1811. else
  1812. Result := -1;
  1813. end;
  1814. function EVP_DecryptUpdate(ctx: PEVP_CIPHER_CTX; out_data: PByte;
  1815. outl: pcint; const in_: PByte; inl: cint): cint;
  1816. begin
  1817. if InitSSLInterface and Assigned(_EVP_DecryptUpdate) then
  1818. Result := _EVP_DecryptUpdate(ctx, out_data, outl, in_, inl)
  1819. else
  1820. Result := -1;
  1821. end;
  1822. function EVP_DecryptFinal(ctx: PEVP_CIPHER_CTX; outm: PByte; outlen: pcint): cint;
  1823. begin
  1824. if InitSSLInterface and Assigned(_EVP_DecryptFinal) then
  1825. Result := _EVP_DecryptFinal(ctx, outm, outlen)
  1826. else
  1827. Result := -1;
  1828. end;
  1829. {$IFNDEF WINDOWS}
  1830. { Try to load all library versions until you find or run out }
  1831. function LoadLibHack(const Value: String): HModule;
  1832. var
  1833. i: cInt;
  1834. begin
  1835. Result := NilHandle;
  1836. for i := Low(DLLVersions) to High(DLLVersions) do begin
  1837. {$IFDEF DARWIN}
  1838. Result := LoadLibrary(Value + DLLVersions[i] + '.dylib');
  1839. {$ELSE}
  1840. Result := LoadLibrary(Value + '.so' + DLLVersions[i]);
  1841. {$ENDIF}
  1842. if Result <> NilHandle then
  1843. Break;
  1844. end;
  1845. end;
  1846. {$ENDIF}
  1847. function LoadLib(const Value: String): HModule;
  1848. begin
  1849. {$IFDEF WINDOWS}
  1850. Result := LoadLibrary(Value);
  1851. {$ELSE}
  1852. Result := LoadLibHack(Value);
  1853. {$ENDIF}
  1854. end;
  1855. function GetProcAddr(module: HModule; const ProcName: string;
  1856. AVerboseLoading: Boolean): SslPtr;
  1857. begin
  1858. Result := GetProcAddress(module, PChar(ProcName));
  1859. if AVerboseLoading and (Result = nil) then
  1860. OpenSSL_unavailable_functions := OpenSSL_unavailable_functions + ProcName + LineEnding;
  1861. end;
  1862. // The AVerboseLoading parameter can be used to check which particular
  1863. // functions weren't loaded correctly. They will be available in the
  1864. // global variable OpenSSL_unavailable_functions
  1865. function InitSSLInterface(AVerboseLoading: Boolean = False): Boolean;
  1866. begin
  1867. if not IsSSLloaded then
  1868. begin
  1869. SSLLibHandle := LoadLib(DLLSSLName);
  1870. SSLUtilHandle := LoadLib(DLLUtilName);
  1871. {$IFNDEF UNIX}
  1872. if (SSLLibHandle = 0) then
  1873. SSLLibHandle := LoadLib(DLLSSLName2);
  1874. {$ENDIF}
  1875. if (SSLLibHandle <> 0) and (SSLUtilHandle <> 0) then
  1876. begin
  1877. _SslGetError := GetProcAddr(SSLLibHandle, 'SSL_get_error', AVerboseLoading);
  1878. _SslLibraryInit := GetProcAddr(SSLLibHandle, 'SSL_library_init', AVerboseLoading);
  1879. _SslLoadErrorStrings := GetProcAddr(SSLLibHandle, 'SSL_load_error_strings', AVerboseLoading);
  1880. _SslCtxSetCipherList := GetProcAddr(SSLLibHandle, 'SSL_CTX_set_cipher_list', AVerboseLoading);
  1881. _SslCtxNew := GetProcAddr(SSLLibHandle, 'SSL_CTX_new', AVerboseLoading);
  1882. _SslCtxFree := GetProcAddr(SSLLibHandle, 'SSL_CTX_free', AVerboseLoading);
  1883. _SslSetFd := GetProcAddr(SSLLibHandle, 'SSL_set_fd', AVerboseLoading);
  1884. _SslCtrl := GetProcAddr(SSLLibHandle, 'SSL_ctrl', AVerboseLoading);
  1885. _SslCTXCtrl := GetProcAddr(SSLLibHandle, 'SSL_CTX_ctrl', AVerboseLoading);
  1886. _SslMethodV2 := GetProcAddr(SSLLibHandle, 'SSLv2_method', AVerboseLoading);
  1887. _SslMethodV3 := GetProcAddr(SSLLibHandle, 'SSLv3_method', AVerboseLoading);
  1888. _SslMethodTLSV1 := GetProcAddr(SSLLibHandle, 'TLSv1_method', AVerboseLoading);
  1889. _SslMethodV23 := GetProcAddr(SSLLibHandle, 'SSLv23_method', AVerboseLoading);
  1890. _SslCtxUsePrivateKey := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_PrivateKey', AVerboseLoading);
  1891. _SslCtxUsePrivateKeyASN1 := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_PrivateKey_ASN1', AVerboseLoading);
  1892. //use SSL_CTX_use_RSAPrivateKey_file instead SSL_CTX_use_PrivateKey_file,
  1893. //because SSL_CTX_use_PrivateKey_file not support DER format. :-O
  1894. _SslCtxUsePrivateKeyFile := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_RSAPrivateKey_file', AVerboseLoading);
  1895. _SslCtxUseCertificate := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_certificate', AVerboseLoading);
  1896. _SslCtxUseCertificateASN1 := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_certificate_ASN1', AVerboseLoading);
  1897. _SslCtxUseCertificateFile := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_certificate_file', AVerboseLoading);
  1898. _SslCtxUseCertificateChainFile := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_certificate_chain_file', AVerboseLoading);
  1899. _SslCtxCheckPrivateKeyFile := GetProcAddr(SSLLibHandle, 'SSL_CTX_check_private_key', AVerboseLoading);
  1900. _SslCtxSetDefaultPasswdCb := GetProcAddr(SSLLibHandle, 'SSL_CTX_set_default_passwd_cb', AVerboseLoading);
  1901. _SslCtxSetDefaultPasswdCbUserdata := GetProcAddr(SSLLibHandle, 'SSL_CTX_set_default_passwd_cb_userdata', AVerboseLoading);
  1902. _SslCtxLoadVerifyLocations := GetProcAddr(SSLLibHandle, 'SSL_CTX_load_verify_locations', AVerboseLoading);
  1903. _SslNew := GetProcAddr(SSLLibHandle, 'SSL_new', AVerboseLoading);
  1904. _SslFree := GetProcAddr(SSLLibHandle, 'SSL_free', AVerboseLoading);
  1905. _SslAccept := GetProcAddr(SSLLibHandle, 'SSL_accept', AVerboseLoading);
  1906. _SslConnect := GetProcAddr(SSLLibHandle, 'SSL_connect', AVerboseLoading);
  1907. _SslShutdown := GetProcAddr(SSLLibHandle, 'SSL_shutdown', AVerboseLoading);
  1908. _SslRead := GetProcAddr(SSLLibHandle, 'SSL_read', AVerboseLoading);
  1909. _SslPeek := GetProcAddr(SSLLibHandle, 'SSL_peek', AVerboseLoading);
  1910. _SslWrite := GetProcAddr(SSLLibHandle, 'SSL_write', AVerboseLoading);
  1911. _SslPending := GetProcAddr(SSLLibHandle, 'SSL_pending', AVerboseLoading);
  1912. _SslGetPeerCertificate := GetProcAddr(SSLLibHandle, 'SSL_get_peer_certificate', AVerboseLoading);
  1913. _SslGetVersion := GetProcAddr(SSLLibHandle, 'SSL_get_version', AVerboseLoading);
  1914. _SslCtxSetVerify := GetProcAddr(SSLLibHandle, 'SSL_CTX_set_verify', AVerboseLoading);
  1915. _SslGetCurrentCipher := GetProcAddr(SSLLibHandle, 'SSL_get_current_cipher', AVerboseLoading);
  1916. _SslCipherGetName := GetProcAddr(SSLLibHandle, 'SSL_CIPHER_get_name', AVerboseLoading);
  1917. _SslCipherGetBits := GetProcAddr(SSLLibHandle, 'SSL_CIPHER_get_bits', AVerboseLoading);
  1918. _SslGetVerifyResult := GetProcAddr(SSLLibHandle, 'SSL_get_verify_result', AVerboseLoading);
  1919. _X509New := GetProcAddr(SSLUtilHandle, 'X509_new', AVerboseLoading);
  1920. _X509Free := GetProcAddr(SSLUtilHandle, 'X509_free', AVerboseLoading);
  1921. _X509NameOneline := GetProcAddr(SSLUtilHandle, 'X509_NAME_oneline', AVerboseLoading);
  1922. _X509GetSubjectName := GetProcAddr(SSLUtilHandle, 'X509_get_subject_name', AVerboseLoading);
  1923. _X509GetIssuerName := GetProcAddr(SSLUtilHandle, 'X509_get_issuer_name', AVerboseLoading);
  1924. _X509NameHash := GetProcAddr(SSLUtilHandle, 'X509_NAME_hash', AVerboseLoading);
  1925. _X509Digest := GetProcAddr(SSLUtilHandle, 'X509_digest', AVerboseLoading);
  1926. _X509print := GetProcAddr(SSLUtilHandle, 'X509_print', AVerboseLoading);
  1927. _X509SetVersion := GetProcAddr(SSLUtilHandle, 'X509_set_version', AVerboseLoading);
  1928. _X509SetPubkey := GetProcAddr(SSLUtilHandle, 'X509_set_pubkey', AVerboseLoading);
  1929. _X509SetIssuerName := GetProcAddr(SSLUtilHandle, 'X509_set_issuer_name', AVerboseLoading);
  1930. _X509NameAddEntryByTxt := GetProcAddr(SSLUtilHandle, 'X509_NAME_add_entry_by_txt', AVerboseLoading);
  1931. _X509Sign := GetProcAddr(SSLUtilHandle, 'X509_sign', AVerboseLoading);
  1932. _X509GmtimeAdj := GetProcAddr(SSLUtilHandle, 'X509_gmtime_adj', AVerboseLoading);
  1933. _X509SetNotBefore := GetProcAddr(SSLUtilHandle, 'X509_set_notBefore', AVerboseLoading);
  1934. _X509SetNotAfter := GetProcAddr(SSLUtilHandle, 'X509_set_notAfter', AVerboseLoading);
  1935. _X509GetSerialNumber := GetProcAddr(SSLUtilHandle, 'X509_get_serialNumber', AVerboseLoading);
  1936. _EvpPkeyNew := GetProcAddr(SSLUtilHandle, 'EVP_PKEY_new', AVerboseLoading);
  1937. _EvpPkeyFree := GetProcAddr(SSLUtilHandle, 'EVP_PKEY_free', AVerboseLoading);
  1938. _EvpPkeyAssign := GetProcAddr(SSLUtilHandle, 'EVP_PKEY_assign', AVerboseLoading);
  1939. _EVPCleanup := GetProcAddr(SSLUtilHandle, 'EVP_cleanup', AVerboseLoading);
  1940. _EvpGetDigestByName := GetProcAddr(SSLUtilHandle, 'EVP_get_digestbyname', AVerboseLoading);
  1941. _SSLeayversion := GetProcAddr(SSLUtilHandle, 'SSLeay_version', AVerboseLoading);
  1942. _ErrErrorString := GetProcAddr(SSLUtilHandle, 'ERR_error_string_n', AVerboseLoading);
  1943. _ErrGetError := GetProcAddr(SSLUtilHandle, 'ERR_get_error', AVerboseLoading);
  1944. _ErrClearError := GetProcAddr(SSLUtilHandle, 'ERR_clear_error', AVerboseLoading);
  1945. _ErrFreeStrings := GetProcAddr(SSLUtilHandle, 'ERR_free_strings', AVerboseLoading);
  1946. _ErrRemoveState := GetProcAddr(SSLUtilHandle, 'ERR_remove_state', AVerboseLoading);
  1947. _RandScreen := GetProcAddr(SSLUtilHandle, 'RAND_screen', AVerboseLoading);
  1948. _BioNew := GetProcAddr(SSLUtilHandle, 'BIO_new', AVerboseLoading);
  1949. _BioFreeAll := GetProcAddr(SSLUtilHandle, 'BIO_free_all', AVerboseLoading);
  1950. _BioSMem := GetProcAddr(SSLUtilHandle, 'BIO_s_mem', AVerboseLoading);
  1951. _BioCtrlPending := GetProcAddr(SSLUtilHandle, 'BIO_ctrl_pending', AVerboseLoading);
  1952. _BioRead := GetProcAddr(SSLUtilHandle, 'BIO_read', AVerboseLoading);
  1953. _BioWrite := GetProcAddr(SSLUtilHandle, 'BIO_write', AVerboseLoading);
  1954. _d2iPKCS12bio := GetProcAddr(SSLUtilHandle, 'd2i_PKCS12_bio', AVerboseLoading);
  1955. _PKCS12parse := GetProcAddr(SSLUtilHandle, 'PKCS12_parse', AVerboseLoading);
  1956. _PKCS12free := GetProcAddr(SSLUtilHandle, 'PKCS12_free', AVerboseLoading);
  1957. _Asn1UtctimeNew := GetProcAddr(SSLUtilHandle, 'ASN1_UTCTIME_new', AVerboseLoading);
  1958. _Asn1UtctimeFree := GetProcAddr(SSLUtilHandle, 'ASN1_UTCTIME_free', AVerboseLoading);
  1959. _i2dX509bio := GetProcAddr(SSLUtilHandle, 'i2d_X509_bio', AVerboseLoading);
  1960. _i2dPrivateKeyBio := GetProcAddr(SSLUtilHandle, 'i2d_PrivateKey_bio', AVerboseLoading);
  1961. // 3DES functions
  1962. _DESsetoddparity := GetProcAddr(SSLUtilHandle, 'des_set_odd_parity', AVerboseLoading);
  1963. _DESsetkeychecked := GetProcAddr(SSLUtilHandle, 'des_set_key_checked', AVerboseLoading);
  1964. _DESecbencrypt := GetProcAddr(SSLUtilHandle, 'des_ecb_encrypt', AVerboseLoading);
  1965. //
  1966. _CRYPTOnumlocks := GetProcAddr(SSLUtilHandle, 'CRYPTO_num_locks', AVerboseLoading);
  1967. _CRYPTOsetlockingcallback := GetProcAddr(SSLUtilHandle, 'CRYPTO_set_locking_callback', AVerboseLoading);
  1968. // RAND functions
  1969. _RAND_set_rand_method := GetProcAddr(SSLUtilHandle, 'RAND_set_rand_method', AVerboseLoading);
  1970. _RAND_get_rand_method := GetProcAddr(SSLUtilHandle, 'RAND_get_rand_method', AVerboseLoading);
  1971. _RAND_SSLeay := GetProcAddr(SSLUtilHandle, 'RAND_SSLeay', AVerboseLoading);
  1972. _RAND_cleanup := GetProcAddr(SSLUtilHandle, 'RAND_cleanup', AVerboseLoading);
  1973. _RAND_bytes := GetProcAddr(SSLUtilHandle, 'RAND_bytes', AVerboseLoading);
  1974. _RAND_pseudo_bytes := GetProcAddr(SSLUtilHandle, 'RAND_pseudo_bytes', AVerboseLoading);
  1975. _RAND_seed := GetProcAddr(SSLUtilHandle, 'RAND_seed', AVerboseLoading);
  1976. _RAND_add := GetProcAddr(SSLUtilHandle, 'RAND_add', AVerboseLoading);
  1977. _RAND_load_file := GetProcAddr(SSLUtilHandle, 'RAND_load_file', AVerboseLoading);
  1978. _RAND_write_file := GetProcAddr(SSLUtilHandle, 'RAND_write_file', AVerboseLoading);
  1979. _RAND_file_name := GetProcAddr(SSLUtilHandle, 'RAND_file_name', AVerboseLoading);
  1980. _RAND_status := GetProcAddr(SSLUtilHandle, 'RAND_status', AVerboseLoading);
  1981. _RAND_query_egd_bytes := GetProcAddr(SSLUtilHandle, 'RAND_query_egd_bytes', AVerboseLoading); // 0.9.7+
  1982. _RAND_egd := GetProcAddr(SSLUtilHandle, 'RAND_egd', AVerboseLoading);
  1983. _RAND_egd_bytes := GetProcAddr(SSLUtilHandle, 'RAND_egd_bytes', AVerboseLoading);
  1984. _ERR_load_RAND_strings := GetProcAddr(SSLUtilHandle, 'ERR_load_RAND_strings', AVerboseLoading);
  1985. _RAND_poll := GetProcAddr(SSLUtilHandle, 'RAND_poll', AVerboseLoading);
  1986. // RSA Functions
  1987. _RSA_new := GetProcAddr(SSLUtilHandle, 'RSA_new', AVerboseLoading);
  1988. _RSA_new_method := GetProcAddr(SSLUtilHandle, 'RSA_new_method', AVerboseLoading);
  1989. _RSA_size := GetProcAddr(SSLUtilHandle, 'RSA_size', AVerboseLoading);
  1990. _RsaGenerateKey := GetProcAddr(SSLUtilHandle, 'RSA_generate_key', AVerboseLoading);
  1991. _RSA_generate_key_ex := GetProcAddr(SSLUtilHandle, 'RSA_generate_key_ex', AVerboseLoading);
  1992. _RSA_check_key := GetProcAddr(SSLUtilHandle, 'RSA_check_key', AVerboseLoading);
  1993. _RSA_public_encrypt := GetProcAddr(SSLUtilHandle, 'RSA_public_encrypt', AVerboseLoading);
  1994. _RSA_private_encrypt := GetProcAddr(SSLUtilHandle, 'RSA_private_encrypt', AVerboseLoading);
  1995. _RSA_public_decrypt := GetProcAddr(SSLUtilHandle, 'RSA_public_decrypt', AVerboseLoading);
  1996. _RSA_private_decrypt := GetProcAddr(SSLUtilHandle, 'RSA_private_decrypt', AVerboseLoading);
  1997. _RSA_free := GetProcAddr(SSLUtilHandle, 'RSA_free', AVerboseLoading);
  1998. _RSA_flags := GetProcAddr(SSLUtilHandle, 'RSA_flags', AVerboseLoading);
  1999. _RSA_set_default_method := GetProcAddr(SSLUtilHandle, 'RSA_set_default_method', AVerboseLoading);
  2000. _RSA_get_default_method := GetProcAddr(SSLUtilHandle, 'RSA_get_default_method', AVerboseLoading);
  2001. _RSA_get_method := GetProcAddr(SSLUtilHandle, 'RSA_get_method', AVerboseLoading);
  2002. _RSA_set_method := GetProcAddr(SSLUtilHandle, 'RSA_set_method', AVerboseLoading);
  2003. // X509 Functions
  2004. _d2i_RSAPublicKey := GetProcAddr(SSLUtilHandle, 'd2i_RSAPublicKey', AVerboseLoading);
  2005. _i2d_RSAPublicKey := GetProcAddr(SSLUtilHandle, 'i2d_RSAPublicKey', AVerboseLoading);
  2006. _d2i_RSAPrivateKey := GetProcAddr(SSLUtilHandle, 'd2i_RSAPrivateKey', AVerboseLoading);
  2007. _i2d_RSAPrivateKey := GetProcAddr(SSLUtilHandle, 'i2d_RSAPrivateKey', AVerboseLoading);
  2008. // ERR Functions
  2009. _ERR_error_string := GetProcAddr(SSLUtilHandle, 'ERR_error_string', AVerboseLoading);
  2010. // Crypto Functions
  2011. _SSLeay_version := GetProcAddr(SSLLibHandle, 'SSLeay_version', AVerboseLoading);
  2012. // EVP Functions
  2013. _OpenSSL_add_all_algorithms := GetProcAddr(SSLUtilHandle, 'OpenSSL_add_all_algorithms', AVerboseLoading);
  2014. _OpenSSL_add_all_ciphers := GetProcAddr(SSLUtilHandle, 'OpenSSL_add_all_ciphers', AVerboseLoading);
  2015. _OpenSSL_add_all_digests := GetProcAddr(SSLUtilHandle, 'OpenSSL_add_all_digests', AVerboseLoading);
  2016. //
  2017. _EVP_DigestInit := GetProcAddr(SSLUtilHandle, 'EVP_DigestInit', AVerboseLoading);
  2018. _EVP_DigestUpdate := GetProcAddr(SSLUtilHandle, 'EVP_DigestUpdate', AVerboseLoading);
  2019. _EVP_DigestFinal := GetProcAddr(SSLUtilHandle, 'EVP_DigestFinal', AVerboseLoading);
  2020. //
  2021. _EVP_get_cipherbyname := GetProcAddr(SSLUtilHandle, 'EVP_get_cipherbyname', AVerboseLoading);
  2022. _EVP_get_digestbyname := GetProcAddr(SSLUtilHandle, 'EVP_get_digestbyname', AVerboseLoading);
  2023. //
  2024. _EVP_CIPHER_CTX_init := GetProcAddr(SSLUtilHandle, 'EVP_CIPHER_CTX_init', AVerboseLoading);
  2025. _EVP_CIPHER_CTX_cleanup := GetProcAddr(SSLUtilHandle, 'EVP_CIPHER_CTX_cleanup', AVerboseLoading);
  2026. _EVP_CIPHER_CTX_set_key_length := GetProcAddr(SSLUtilHandle, 'EVP_CIPHER_CTX_set_key_length', AVerboseLoading);
  2027. _EVP_CIPHER_CTX_ctrl := GetProcAddr(SSLUtilHandle, 'EVP_CIPHER_CTX_ctrl', AVerboseLoading);
  2028. //
  2029. _EVP_EncryptInit := GetProcAddr(SSLUtilHandle, 'EVP_EncryptInit', AVerboseLoading);
  2030. _EVP_EncryptUpdate := GetProcAddr(SSLUtilHandle, 'EVP_EncryptUpdate', AVerboseLoading);
  2031. _EVP_EncryptFinal := GetProcAddr(SSLUtilHandle, 'EVP_EncryptFinal', AVerboseLoading);
  2032. //
  2033. _EVP_DecryptInit := GetProcAddr(SSLUtilHandle, 'EVP_DecryptInit', AVerboseLoading);
  2034. _EVP_DecryptUpdate := GetProcAddr(SSLUtilHandle, 'EVP_DecryptUpdate', AVerboseLoading);
  2035. _EVP_DecryptFinal := GetProcAddr(SSLUtilHandle, 'EVP_DecryptFinal', AVerboseLoading);
  2036. //init library
  2037. if assigned(_SslLibraryInit) then
  2038. _SslLibraryInit;
  2039. if assigned(_SslLoadErrorStrings) then
  2040. _SslLoadErrorStrings;
  2041. if assigned(_OPENSSL_add_all_algorithms) then
  2042. _OPENSSL_add_all_algorithms;
  2043. if assigned(_RandScreen) then
  2044. _RandScreen;
  2045. Result := True;
  2046. SSLloaded := True;
  2047. end
  2048. else
  2049. begin
  2050. //load failed!
  2051. if SSLLibHandle <> 0 then
  2052. begin
  2053. FreeLibrary(SSLLibHandle);
  2054. SSLLibHandle := 0;
  2055. end;
  2056. if SSLUtilHandle <> 0 then
  2057. begin
  2058. FreeLibrary(SSLUtilHandle);
  2059. SSLLibHandle := 0;
  2060. end;
  2061. Result := False;
  2062. end;
  2063. end
  2064. else
  2065. //loaded before...
  2066. Result := true;
  2067. end;
  2068. function DestroySSLInterface: Boolean;
  2069. begin
  2070. if IsSSLLoaded then
  2071. begin
  2072. //deinit library
  2073. EVPCleanup;
  2074. ErrRemoveState(0);
  2075. end;
  2076. SSLloaded := false;
  2077. if SSLLibHandle <> 0 then
  2078. begin
  2079. FreeLibrary(SSLLibHandle);
  2080. SSLLibHandle := 0;
  2081. end;
  2082. if SSLUtilHandle <> 0 then
  2083. begin
  2084. FreeLibrary(SSLUtilHandle);
  2085. SSLLibHandle := 0;
  2086. end;
  2087. _SslGetError := nil;
  2088. _SslLibraryInit := nil;
  2089. _SslLoadErrorStrings := nil;
  2090. _SslCtxSetCipherList := nil;
  2091. _SslCtxNew := nil;
  2092. _SslCtxFree := nil;
  2093. _SslSetFd := nil;
  2094. _SslCtrl := nil;
  2095. _SslCTXCtrl := nil;
  2096. _SslMethodV2 := nil;
  2097. _SslMethodV3 := nil;
  2098. _SslMethodTLSV1 := nil;
  2099. _SslMethodV23 := nil;
  2100. _SslCtxUsePrivateKey := nil;
  2101. _SslCtxUsePrivateKeyASN1 := nil;
  2102. _SslCtxUsePrivateKeyFile := nil;
  2103. _SslCtxUseCertificate := nil;
  2104. _SslCtxUseCertificateASN1 := nil;
  2105. _SslCtxUseCertificateFile := nil;
  2106. _SslCtxUseCertificateChainFile := nil;
  2107. _SslCtxCheckPrivateKeyFile := nil;
  2108. _SslCtxSetDefaultPasswdCb := nil;
  2109. _SslCtxSetDefaultPasswdCbUserdata := nil;
  2110. _SslCtxLoadVerifyLocations := nil;
  2111. _SslNew := nil;
  2112. _SslFree := nil;
  2113. _SslAccept := nil;
  2114. _SslConnect := nil;
  2115. _SslShutdown := nil;
  2116. _SslRead := nil;
  2117. _SslPeek := nil;
  2118. _SslWrite := nil;
  2119. _SslPending := nil;
  2120. _SslGetPeerCertificate := nil;
  2121. _SslGetVersion := nil;
  2122. _SslCtxSetVerify := nil;
  2123. _SslGetCurrentCipher := nil;
  2124. _SslCipherGetName := nil;
  2125. _SslCipherGetBits := nil;
  2126. _SslGetVerifyResult := nil;
  2127. _X509New := nil;
  2128. _X509Free := nil;
  2129. _X509NameOneline := nil;
  2130. _X509GetSubjectName := nil;
  2131. _X509GetIssuerName := nil;
  2132. _X509NameHash := nil;
  2133. _X509Digest := nil;
  2134. _X509print := nil;
  2135. _X509SetVersion := nil;
  2136. _X509SetPubkey := nil;
  2137. _X509SetIssuerName := nil;
  2138. _X509NameAddEntryByTxt := nil;
  2139. _X509Sign := nil;
  2140. _X509GmtimeAdj := nil;
  2141. _X509SetNotBefore := nil;
  2142. _X509SetNotAfter := nil;
  2143. _X509GetSerialNumber := nil;
  2144. _EvpPkeyNew := nil;
  2145. _EvpPkeyFree := nil;
  2146. _EvpPkeyAssign := nil;
  2147. _EVPCleanup := nil;
  2148. _EvpGetDigestByName := nil;
  2149. _SSLeayversion := nil;
  2150. _ErrErrorString := nil;
  2151. _ErrGetError := nil;
  2152. _ErrClearError := nil;
  2153. _ErrFreeStrings := nil;
  2154. _ErrRemoveState := nil;
  2155. _RandScreen := nil;
  2156. _BioNew := nil;
  2157. _BioFreeAll := nil;
  2158. _BioSMem := nil;
  2159. _BioCtrlPending := nil;
  2160. _BioRead := nil;
  2161. _BioWrite := nil;
  2162. _d2iPKCS12bio := nil;
  2163. _PKCS12parse := nil;
  2164. _PKCS12free := nil;
  2165. _Asn1UtctimeNew := nil;
  2166. _Asn1UtctimeFree := nil;
  2167. _i2dX509bio := nil;
  2168. _i2dPrivateKeyBio := nil;
  2169. // 3DES functions
  2170. _DESsetoddparity := nil;
  2171. _DESsetkeychecked := nil;
  2172. _DESecbencrypt := nil;
  2173. //
  2174. _CRYPTOnumlocks := nil;
  2175. _CRYPTOsetlockingcallback := nil;
  2176. // RAND functions
  2177. _RAND_set_rand_method := nil;
  2178. _RAND_get_rand_method := nil;
  2179. _RAND_SSLeay := nil;
  2180. _RAND_cleanup := nil;
  2181. _RAND_bytes := nil;
  2182. _RAND_pseudo_bytes := nil;
  2183. _RAND_seed := nil;
  2184. _RAND_add := nil;
  2185. _RAND_load_file := nil;
  2186. _RAND_write_file := nil;
  2187. _RAND_file_name := nil;
  2188. _RAND_status := nil;
  2189. _RAND_query_egd_bytes := nil;
  2190. _RAND_egd := nil;
  2191. _RAND_egd_bytes := nil;
  2192. _ERR_load_RAND_strings := nil;
  2193. _RAND_poll := nil;
  2194. // RSA Functions
  2195. _RSA_new := nil;
  2196. _RSA_new_method := nil;
  2197. _RSA_size := nil;
  2198. _RsaGenerateKey := nil;
  2199. _RSA_generate_key_ex := nil;
  2200. _RSA_check_key := nil;
  2201. _RSA_public_encrypt := nil;
  2202. _RSA_private_encrypt := nil;
  2203. _RSA_public_decrypt := nil;
  2204. _RSA_private_decrypt := nil;
  2205. _RSA_free := nil;
  2206. _RSA_flags := nil;
  2207. _RSA_set_default_method := nil;
  2208. _RSA_get_default_method := nil;
  2209. _RSA_get_method := nil;
  2210. _RSA_set_method := nil;
  2211. // X509 Functions
  2212. _d2i_RSAPublicKey := nil;
  2213. _i2d_RSAPublicKey := nil;
  2214. _d2i_RSAPrivateKey := nil;
  2215. _i2d_RSAPrivateKey := nil;
  2216. // ERR Functions
  2217. _ERR_error_string := nil;
  2218. // Crypto Functions
  2219. _SSLeay_version := nil;
  2220. // EVP Functions
  2221. _OpenSSL_add_all_algorithms := nil;
  2222. _OpenSSL_add_all_ciphers := nil;
  2223. _OpenSSL_add_all_digests := nil;
  2224. //
  2225. _EVP_DigestInit := nil;
  2226. _EVP_DigestUpdate := nil;
  2227. _EVP_DigestFinal := nil;
  2228. //
  2229. _EVP_get_cipherbyname := nil;
  2230. _EVP_get_digestbyname := nil;
  2231. //
  2232. _EVP_CIPHER_CTX_init := nil;
  2233. _EVP_CIPHER_CTX_cleanup := nil;
  2234. _EVP_CIPHER_CTX_set_key_length := nil;
  2235. _EVP_CIPHER_CTX_ctrl := nil;
  2236. //
  2237. _EVP_EncryptInit := nil;
  2238. _EVP_EncryptUpdate := nil;
  2239. _EVP_EncryptFinal := nil;
  2240. //
  2241. _EVP_DecryptInit := nil;
  2242. _EVP_DecryptUpdate := nil;
  2243. _EVP_DecryptFinal := nil;
  2244. Result := True;
  2245. end;
  2246. function IsSSLloaded: Boolean;
  2247. begin
  2248. Result := SSLLoaded;
  2249. end;
  2250. finalization
  2251. DestroySSLInterface;
  2252. end.