[Solved] Lazarus fphttpclient and ssl lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- JohnnieK · 2018-11-20 10:18 Hi I am trying to do a simple web page download using fphttpclient. I am running it on OpenSUSE Leap 15 (64bit). The code below: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject); Var S    : string;     HTTP : TFPHttpClient;  begin   HTTP := TFPHttpClient.Create(nil);   S := HTTP.Get('https://www.google.com');   HTTP.Free;   Memo1.Text:= S;  end;   crashes with an exception that GDB cannot catch. I traced it to this piece of code inside fpopenssl. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Constructor TSSLContext.Create(AType: TSSLType); Var  C : PSSL_CTX; begin  C := nil;  Case AType of    stAny:  C := SslCtxNew(SslMethodV23);    stSSLv2: C := SslCtxNew(SslMethodV2);    stSSLv3: C := SslCtxNew(SslMethodV3);    stTLSv1: C := SslCtxNew(SslMethodTLSV1);  stTLSv1_1: C := SslCtxNew(SslMethodTLSV1_1);    stTLSv1_2: C := SslCtxNew(SslMethodTLSV1_2);  end;  if (C=Nil) then     Raise ESSL.Create(SErrCountNotGetContext);  Create(C);end; The above code return C=nil and then the exception is thrown. The installed version of openssl is 1.1 Any ideas on what could be wrong ? Thanx ---------------------------------------------------------------------- upsidasi · 2018-11-20 11:41 make sure you have openssl 1.0 installed. You can check it running   ldconfig -pv | grep 'libssl\.\|libcrypto\.' ---------------------------------------------------------------------- Thaddy · 2018-11-20 11:49 This is enough: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$H+}uses fphttpclient, fpopenssl;Var S    : string;  HTTP : TFPHttpClient;begin  HTTP := TFPHttpClient.Create(nil);  try    S := HTTP.Get('https://www.google.com');    writeln(s);  finally    http.free;  end;  end. ---------------------------------------------------------------------- JohnnieK · 2018-11-20 11:59 Output of ldconfig -pv | grep 'libssl\.\|libcrypto\.' Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i         libssl.so.1.1 (libc6,x86-64) => /usr/lib64/libssl.so.1.1        libssl.so.1.1 (libc6) => /usr/lib/libssl.so.1.1        libssl.so.1.0.0 (libc6,x86-64) => /usr/lib64/libssl.so.1.0.0        libssl.so (libc6,x86-64) => /usr/lib64/libssl.so        libcrypto.so.1.1 (libc6,x86-64) => /usr/lib64/libcrypto.so.1.1        libcrypto.so.1.1 (libc6) => /usr/lib/libcrypto.so.1.1        libcrypto.so.1.0.0 (libc6,x86-64) => /usr/lib64/libcrypto.so.1.0.0        libcrypto.so (libc6,x86-64) => /usr/lib64/libcrypto.so ---------------------------------------------------------------------- Thaddy · 2018-11-20 12:01 Try my code. works. ---------------------------------------------------------------------- CCRDude · 2018-11-20 12:21 Depending on the server on the other side, I had the experience that on Linux and Mac, I had to set the TLS version manually to be able to connect to https sources. That was Synapse, but that uses OpenSSL as well... see this thread. ---------------------------------------------------------------------- Thaddy · 2018-11-20 12:43 Note I came across some major sites (like a lot of google) that even dropped t;ls1.1. So best practice is at least tls 1.2 if you have to set it manually. I didn't have to do that with the above code. ---------------------------------------------------------------------- JohnnieK · 2018-11-20 13:06 I have made some progress. On OpenSUSE Leap 15.0 openssl 1.1 is the default. libssl.so is a softlink to libssl.so.1.1 and the same for libcrypto. I created a softlink libssl1.so -> libssl.s0.1.0 and the same for libcrypto. I then modified openssl.pas lines 106 and 107 as follows: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   DLLSSLName: string = 'libssl1';  DLLUtilName: string = 'libcrypto1'; Now I can connect to some https sites (like my home server running apache), but not to Google. This must be related to the TLS versions - I just don't know where to set the TLS version in fphttpclient. ---------------------------------------------------------------------- Thaddy · 2018-11-20 15:04 Try the    AllowRedirect := true property from TFPHTTPClient. In general protocol handshakes are made with TLS1.2 first, so that is not likely the issue. 403 ---------------------------------------------------------------------- guest58172 · 2018-11-20 17:55 Maybe the SSL context ? There's this annoyinh issue : https://bugs.freepascal.org/view.php?id=32789 Basically this means that you cant do secure request out of the box, i.e using fcl-web ---------------------------------------------------------------------- JohnnieK · 2018-11-21 10:20 OK, so finally solved the issue. I had to do the following: 1. OpenSUSE Leap 15 ships with both openssl 1.0 and 1.1. openssl 1.1 is the default and libssl.so is soft linked to openssl 1.1. I created 2 new softlinks for libssl and libcrypto and changed the dll names in openssl.pas. 2. I had to allow redirects and add a useragent to get https://www.google.com to work. Code below works now: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject); Var S    : string;     HTTP : TFPHttpClient;  begin   HTTP := TFPHttpClient.Create(nil);//   HTTP. .Sock.SSL.SSLType := LT_TLSv1_2;   HTTP.AllowRedirect:=True;   HTTP.AddHeader('User-Agent','Mozilla/5.0 (compatible; fpweb)');   S := HTTP.Get('https://www.google.com');   HTTP.Free;   Memo1.Text:= S;  end;   Thanx for all who assisted