Delphi XE5 : SSL / HTTPS on different platforms with TIdHTTP and TRESTClient

  

Using SSL/HTTPS with Indy’s TIdHttp (or through TRESTClient – which is effectively based on TIdHttp) requires external OpenSSL libraries, which is something most of us probably learned in the past.
With Delphi XE5 now supporting different platforms, such as Windows, Mac, iOS devices, iOS Simulator and Android devices, there are now a couple of different libraries and approaches to follow.
Btw, if you are using TRESTClient, then you can just assign a URL like https://www.example.com/api to TRESTClient.BaseURL – you don’t have to worry about assigning an SSL IO handler, as you would have to, if working with TIdHttp directly. TRESTClient detects that situation automatically.
In one of Marco Cantú’s recent posts you will find some very useful information about how to build your own OpenSSL binaries iOS – which is a good idea from a security standpoint.
Marco’s post is basically for iOS only, so I thought I should share an overview that covers all platforms and make this an easy to follow SSL with Delphi recipe.
As mentioned above building your own SSL libraries from source is a good idea, but many people would just like to download ready-to-use binaries. For us Delphi users there is a trustful mirror site which has recent builds available since many years:
http://indy.fulgan.com/SSL
There are dynamic libraries (a.k.a “DLLs”) and static libraries (a.k.a “.a files”)
Recent dynamic libraries (32-bit): openssl-1.0.1e-i386-win32.zip
Recent static libraries: OpenSSLStaticLibs.7z
Windows
Download dynamic libraries and put them into the same directory as your exe. You may also put them into your system’s windows\system32 directory.
Files (32 Bit)

libeay32.dll
ssleay32.dll

Mac OSX
No download required, recent versions of Mac OSX come with OpenSSL pre-installed, you only need to tell your app where the required dynamic libraries (a.k.a dylibs) can be found. On typical Mac installations they are in “/usr/lib/”

uses
IdSSLOpenSSLHeaders;

IdOpenSSLSetLibPath(‘/usr/lib’);

You could also deploy these files with your app by adding them to your apps deployment files list (and enable it for Mac OSX and iOS Simulator only)
Files

libcrypto.dylib
libssl.dylib

(Both files are usually symbolic links to actual binaries like /usr/lib/libssl.0.9.7.dylib)
iOS Simulator
No download required, recent versions of Mac OSX come with OpenSSL pre-installed (and which is used by the iOS Simulator as well), you only need to tell your app where the required dynamic libraries (a.k.a dylibs) can be found. On typical Mac installations they are in “/usr/lib/”

IdOpenSSLSetLibPath(‘/usr/lib’);

Files

libcrypto.dylib
libssl.dylib

(Both files are usually symbolic links to actual binaries like /usr/lib/libssl.0.9.7.dylib)
iOS Device
iOS does not allow to load any dynamic code, thus you need to download the static OpenSSL libraries and put them into the main source directory of your application, so that Delphi can find them while compiling/linking your app.
Files

libcrypto.a
libssl.a

Android Device
Info follows soon!
Making your project multi platform
As you can actually have one project compiling to all the mentioned platforms you probably want some ifdefs to handle all platforms and their units automatically. Below is a sample DPR file that works fine for me:
program SSLTest;

uses
System.StartUpCopy,
{$IF Defined(IOS) and Defined(CPUARM)}
IdSSLOpenSSLHeaders_Static,
{$ELSE}
IdSSLOpenSSLHeaders,
{$ENDIF}
FMX.Forms,
Unit5 in ‘Unit5.pas’ {Form5};

{$R *.res}

begin
{$IF Defined(IOS) and not Defined(CPUARM)}
IdOpenSSLSetLibPath(‘/usr/lib/’);
{$ENDIF}
Application.Initialize;
Application.CreateForm(TForm5, Form5);
Application.Run;
end.

Tweet This!

Share this on del.icio.us

Digg this!

Share this on Reddit

Get Shareaholic

Comments are closed.