TFPHTTPClient SSL Error / Debian 12 "Bookworm" lazarus / 21-General ---------------------------------------------------------------------- cris75 · 2024-06-25 09:04 I searched a lot in the forum but I could not find what I hoped for, a sort of "definitive" answer I found this example code that uploads a file to the internet and show a progressbar (it uses TFPHTTPClient) Works OK in Win10_x64, libraries libeay32.dll and ssleay32.dll (ver. 1.0.2p) are in project folder It raises instead and exception on line 116 in Debian 12 "BookWorm", "Could not initialize OpenSSL library" 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 unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs,  StdCtrls, ComCtrls, ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)    bUpload: TButton;    lb0: TLabel;    lb100: TLabel;    lbProgressCap: TLabel;    lbProgress: TLabel;    pb: TProgressBar;    procedure bUploadClick(Sender: TObject);  private    FTotSize: Int64;    procedure DoOnReadStream(Sender: TObject; APos: Int64);    function UploadFile(const AURL, AFieldName, ASrcFileName, ADstFileName: String;          out AError: String): Boolean;    function FormatSize(Size: Int64): String;  public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } uses fphttpclient, openssl, opensslsockets; { TDownloadStream } type  TOnReadStream = procedure(Sender: TObject; APos: Int64) of object;  TUploadStream = class(TStream)  private    FOnReadStream: TOnReadStream;    FStream: TStream;  public    constructor Create(AStream: TStream);    destructor Destroy; override;    function Read(var Buffer; Count: LongInt): LongInt; override;    function Write(const Buffer; Count: LongInt): LongInt; override;    function Seek(Offset: LongInt; Origin: Word): LongInt; override;    procedure DoProgress;  published    property OnReadStream: TOnReadStream read FOnReadStream write FOnReadStream;  end; constructor TUploadStream.Create(AStream: TStream);begin  inherited Create;  FStream:= AStream;  FStream.Position:= 0;end; destructor TUploadStream.Destroy;begin  FStream.Free;  inherited Destroy;end; function TUploadStream.Read(var Buffer; Count: LongInt): LongInt;begin  Result:= FStream.Read(Buffer, Count);  DoProgress;end; function TUploadStream.Write(const Buffer; Count: LongInt): LongInt;begin  Result:= FStream.Write(Buffer, Count);end; function TUploadStream.Seek(Offset: LongInt; Origin: Word): LongInt;begin  Result:= FStream.Seek(Offset, Origin);end; procedure TUploadStream.DoProgress;begin  if Assigned(FOnReadStream) then    FOnReadStream(Self, Self.Position);end; function TForm1.UploadFile(const AURL, AFieldName, ASrcFileName, ADstFileName: String;  out AError: String): Boolean;var  SS: TStringStream;  HTTPClient: TFPHTTPClient;  US: TUploadStream;begin  Result:= False;  SS:= TStringStream.Create('');  try    HTTPClient:= TFPHTTPClient.Create(nil);    try      HTTPClient.AllowRedirect:= True;      US:= TUploadStream.Create(TFileStream.Create(ASrcFileName, fmOpenRead or fmShareDenyWrite));      try        FTotSize:= US.Size;        US.FOnReadStream:= @DoOnReadStream;        try          HTTPClient.StreamFormPost(AURL, AFieldName, ADstFileName, US, SS);      except          on E: Exception do            AError:= E.Message;      end;      finally        US.Free      end;      Result:= SS.DataString = 'zipok';      if (not Result) and (Trim(SS.DataString) <> '') then        AError:= SS.DataString;    finally      HTTPClient.Free;      HTTPClient:= nil;    end;  finally    SS.Free;  end;end; function TForm1.FormatSize(Size: Int64): String;const  KB = 1024;  MB = 1024 * KB;  GB = 1024 * MB;begin  if Size < KB then    Result:= FormatFloat('#,##0 Bytes', Size)  else if Size < MB then    Result:= FormatFloat('#,##0.0 KB', Size / KB)  else if Size < GB then    Result:= FormatFloat('#,##0.0 MB', Size / MB)  else    Result:= FormatFloat('#,##0.0 GB', Size / GB);end; procedure TForm1.DoOnReadStream(Sender: TObject; APos: Int64);var  Value: Integer;begin  if FTotSize = 0 then    Exit;  lbProgress.Caption:= FormatSize(APos) + '/' + FormatSize(FTotSize);  Value:= Round(100*APos/FTotSize);  //workaround for pb animation bug(vista+) --> pb.Postion:= Value;  if Value < pb.Max  then  begin    pb.Position:= Value + 1;    pb.Position:= pb.Position - 1;  end  else  begin    pb.Max:= pb.Max + 1;    pb.Position:= pb.Max;    pb.Position:= pb.Position - 1;    pb.Max:= pb.Max - 1;  end;  //end workaround  Application.ProcessMessages;  Sleep(10);end; procedure TForm1.bUploadClick(Sender: TObject);var  ErrMsg: String;begin  if UploadFile('https://xxxxxx.xxx/upload.php', 'bin', 'file.ext', 'file.ext', ErrMsg) then    ShowMessage('Successfully uploaded!')  else    ShowMessage('Cannot upload file: ' + sLineBreak + ErrMsg);end; end.  From what I read I seem to understand that the problem is correlated to the version of openssl package installed in Debian 12 Bookworm: 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 root@testmachine:/home/user# apt list opensslListing... Doneopenssl/stable,stable-security,now 3.0.11-1~deb12u2 amd64 [installed,automatic] I feel dumb that I can't solve this by myself, please help me BTW I installed Lazarus from the official repository, version 3.4 (FPC 3.2.2) - Same versions for Windows and Debian PS I'd prefer a solution that avoid to make things "not standard" (e.g. be forced to use devel version of FPC or messing with Debian packages/libraries etc..) Thank you ---------------------------------------------------------------------- zeljko · 2024-06-25 09:19 fpc-3.2.2 does not have support for openssl3. Try to find and install openssl 1.1 or use fpc trunk (not sure if fixes_3 supports openssl3). ---------------------------------------------------------------------- paweld · 2024-06-25 10:32 FPC 3.2-fixes support OpenSSL 3 as of 2024-01-16 ---------------------------------------------------------------------- cris75 · 2024-06-26 13:58 ok, I'm doing it a reason.. I'm not very seasoned in Linux, how should I proceed with FPC 3.2-fixes? Are there instructions around? e.g. should I have to remove both current Laz and FPC packages? I'm on Debian 12 BookWorm, thanks ---------------------------------------------------------------------- Thaddy · 2024-06-26 15:39 Quote from: cris75 on June 26, 2024, 03:58:19 pmok, I'm doing it a reason.. I'm not very seasoned in Linux, how should I proceed with FPC 3.2-fixes? Are there instructions around? e.g. should I have to remove both current Laz and FPC packages? I'm on Debian 12 BookWorm, thanks In this particular case you can simply copy the  ssl related code from the source tree (fixes) and it will work. It does not use any new features, just updates. ---------------------------------------------------------------------- paweld · 2024-06-26 17:32 @cris75: I think it's best if you install Lazarus + FPC using fpcupdeluxe - see this video for more information on how to do it: https://www.blaisepascalmagazine.eu/en/fpcupdeluxe-install-lazarus- stable-trunk-cross-compiling-raspberrypi-versions/ Select the "fixes" version for both FPC and Lazarus ---------------------------------------------------------------------- Jurassic Pork · 2024-06-27 00:07 Hello, @cris75, what is the result of the commands on a terminal ? : Quotesudo ldconfig -p | grep crypto sudo ldconfig -p | grep ssl friendly, J.P ---------------------------------------------------------------------- TRon · 2024-06-27 01:05 @JP: My name not cris75 but I do run the same openssl on my bookworm. Code: [Select]sudo ldconfig -p | grep crypto libmbedcrypto.so.7 (libc6,x86-64) => /lib/x86_64-linux- gnu/libmbedcrypto.so.7 libk5crypto.so.3 (libc6,x86-64) => /lib/x86_64-linux- gnu/libk5crypto.so.3 libcrypto.so.3 (libc6,x86-64) => /lib/x86_64-linux- gnu/libcrypto.so.3 libcrypto.so.1.1 (libc6,x86-64) => /lib/x86_64-linux- gnu/libcrypto.so.1.1 libcrypto.so (libc6,x86-64) => /lib/x86_64-linux- gnu/libcrypto.so libbd_crypto.so.2 (libc6,x86-64) => /lib/x86_64-linux- gnu/libbd_crypto.so.2 sudo ldconfig -p | grep ssl libssl3.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libssl3.so libssl.so.3 (libc6,x86-64) => /lib/x86_64-linux- gnu/libssl.so.3 libssl.so.1.1 (libc6,x86-64) => /lib/x86_64-linux- gnu/libssl.so.1.1 libssl.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libssl.so ---------------------------------------------------------------------- cris75 · 2024-06-27 07:39 @Jurassic Pork: For me it is the following Code: [Select]sudo ldconfig -p | grep crypto         libmbedcrypto.so.7 (libc6,x86-64) => /lib/x86_64-linux- gnu/libmbedcrypto.so.7         libk5crypto.so.3 (libc6,x86-64) => /lib/x86_64-linux- gnu/libk5crypto.so.3         libcrypto.so.3 (libc6,x86-64) => /lib/x86_64-linux- gnu/libcrypto.so.3         libbd_crypto.so.2 (libc6,x86-64) => /lib/x86_64-linux- gnu/libbd_crypto.so.2 sudo ldconfig -p | grep ssl         libxmlsec1-openssl.so.1 (libc6,x86-64) => /lib/x86_64-linux- gnu/libxmlsec1-openssl.so.1         libssl3.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libssl3.so         libssl.so.3 (libc6,x86-64) => /lib/x86_64-linux- gnu/libssl.so.3 ---------------------------------------------------------------------- Jurassic Pork · 2024-06-27 08:06 try to install libssl-dev to get libssl.so and libcrypto.so ( on my xubuntu 22.04 the dlls are in this package) and retry : Quotesudo ldconfig sudo ldconfig -p | grep crypto sudo ldconfig -p | grep ssl if you see libssl.so and libcrypto.so run you lazarus program ---------------------------------------------------------------------- cris75 · 2024-06-27 09:09 @Jurassic Pork: Done install libssl-dev. Now I have Code: [Select]sudo ldconfig -p | grep crypto         libmbedcrypto.so.7 (libc6,x86-64) => /lib/x86_64-linux- gnu/libmbedcrypto.so.7         libk5crypto.so.3 (libc6,x86-64) => /lib/x86_64-linux- gnu/libk5crypto.so.3         libcrypto.so.3 (libc6,x86-64) => /lib/x86_64-linux- gnu/libcrypto.so.3         libcrypto.so (libc6,x86-64) => /lib/x86_64-linux- gnu/libcrypto.so         libbd_crypto.so.2 (libc6,x86-64) => /lib/x86_64-linux- gnu/libbd_crypto.so.2 sudo ldconfig -p | grep ssl         libxmlsec1-openssl.so.1 (libc6,x86-64) => /lib/x86_64-linux- gnu/libxmlsec1-openssl.so.1         libssl3.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libssl3.so         libssl.so.3 (libc6,x86-64) => /lib/x86_64-linux- gnu/libssl.so.3         libssl.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libssl.so And IT WORKS! Thanks a bunch!  P.s. with libssl-dev install it added support for OpenSSL 1, right? Or if not, please explain me, thanks again ---------------------------------------------------------------------- Thaddy · 2024-06-27 09:18 It *should* support just SSL 1.1.x and SSL3 or higher. Mind you, using the wrong protocols will still not work, because they are dropped from the core libraries. (as I have explained many times) ---------------------------------------------------------------------- cris75 · 2024-06-27 11:52 @Thaddy Quote from: Thaddy on June 26, 2024, 05:39:22 pmIn this particular case you can simply copy the  ssl related code from the source tree (fixes) and it will work. It does not use any new features, just updates. if one of the next days I decide to do some experiments for myself and test that solution on a new VM I'll setup (same Debian 12 BookWorm and official/current Lazarus/FPC), what do I need to take from fixes and copy? It's just some specific file(s)? Thanks