欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Delphi如何使用最新版本的OpenSSL

程序员文章站 2022-07-13 18:03:08
...

Delphi如何使用最新版本的OpenSSL

        无论是D6还是D10,要让自带的Indy方便的使用最新版的OpenSSL及其函数,需要客制化文件其头文件的常量定义IdSSLOpenSSLHeaders.pas

        步骤:

        1、官网下载最新版的OpenSSL

        2、修改Indy中的关于OpenSSL的动态库名或静态库名:

        D6版Indy8.0.25,见行次:2632~2634

implementation
//......

const
  SSL_DLL_name = 'ssleay32.dll'; {Do not localize}
  SSLCLIB_DLL_name = 'libeay32.dll'; {Do not localize}

        D10版Indy10.6.2,见行次:19555~19591

implementation
//......

{$IFDEF STATICLOAD_OPENSSL}
const
  SSL_LIB_name         = 'libssl.a'; {Do not Localize}
  SSLCLIB_LIB_name     = 'libcrypto.a'; {Do not Localize}
{$ELSE}
  {$IFDEF UNIX}
const
  {This is a workaround for some Linux distributions and a few other things
  where the symbolic link libbsl.so and libcrypto.so do not exist}
  SSL_DLL_name         = 'libssl'; {Do not localize}
  SSLCLIB_DLL_name     = 'libcrypto'; {Do not localize}
  SSLDLLVers : array [0..10] of string = (
    '.10',
    '.1.0.2','.1.0.1',
                                              
    '.44',              // MacOS LibreSSL forked from which OpenSSL version? Sometimes found ...
    '.43',              // MacOS LibreSSL forked from which OpenSSL version? Sometimes found ...
                                                   
    '.35',              // MacOS LibreSSL forked from OpenSSL version 1.0.1, almost always found
    //
    '.1.0.0','.0.9.9','.0.9.8','.0.9.7','.0.9.6'
  );
  SSLDLLVersChar : array [0..26] of string = ('','a','b','c','d','e','f','g','h','i',
                                                 'j','k','l','m','n','o','p','q','r',
                                                 's','t','u','v','w','x','y','z');
  {$ENDIF}
  {$IFDEF WINDOWS}
const
  SSL_DLL_name         = 'ssleay32.dll';  {Do not localize}
  //The following is a workaround for an alternative name for
  //one of the OpenSSL .DLL's.  If you compile the .DLL's using
  //mingw32, the SSL .dll might be named 'libssl32.dll' instead of
  //ssleay32.dll like you would expect.
  SSL_DLL_name_alt     = 'libssl32.dll';  {Do not localize}
  SSLCLIB_DLL_name     = 'libeay32.dll';  {Do not localize}
  {$ENDIF}
{$ENDIF}


        比如Windows下,修改Indy10.6.2(D10)的头文件IdSSLOpenSSLHeaders.pas,如上改为:

  {$IFDEF WINDOWS}
const
  //SSL_DLL_name         = 'ssleay32.dll';  {Do not localize}//:屏蔽的
  SSL_DLL_name         = 'libssl-1_1.dll';  {Do not localize}//:改的
  //SSL_DLL_name_alt     = 'libssl32.dll';  {Do not localize}//:屏蔽的
  SSLCLIB_DLL_name     = 'libcrypto-1_1';  {Do not localize}//:改的
  {$ENDIF}
{$ENDIF}

        再如Windows下,修改D6的头文件IdSSLOpenSSLHeaders.pas,如上改为:

  {$IFDEF WINDOWS}
const
  //SSL_DLL_name         = 'ssleay32.dll';  {Do not localize}//:屏蔽的
  SSL_DLL_name         = 'libssl-1_1.dll';  {Do not localize}//:改的
  //SSL_DLL_name_alt     = 'libssl32.dll';  {Do not localize}//:屏蔽的
  SSLCLIB_DLL_name     = 'libcrypto-1_1';  {Do not localize}//:改的
  {$ENDIF}
{$ENDIF}

var
  hIdSSL: Integer = 0;
  hIdSSLLib: Integer = 0;

  hIdCrypto: Integer = 0;  //:加的

        3、按照最新的OpenSSL函数的调用说明,可能依赖调用对应的函数:

         比如,可能依赖调用这些函数:

SSL_CTX_new、TLS_client_method、OPENSSL_init_ssl等等

        4、对Indy8.0.25以下版本(对应D6及其以下),其它函数本质类似IdSsl***、ssl_***,其前加了前缀Indy_,可进行更名:

        比如,D6,第2541行次:

  IdSslSetFd: function(s: PSSL; fd: Integer): Integer cdecl = nil;

        可改为:

  Indy_IdSslSetFd: function(s: PSSL; fd: Integer): Integer cdecl = nil;