Recent

Author Topic: TFPHTTPClient SSL Error / Debian 12 "Bookworm"  (Read 2232 times)

cris75

  • Jr. Member
  • **
  • Posts: 67
TFPHTTPClient SSL Error / Debian 12 "Bookworm"
« on: June 25, 2024, 11:04:18 am »
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][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
  9.   StdCtrls, ComCtrls, ExtCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     bUpload: TButton;
  17.     lb0: TLabel;
  18.     lb100: TLabel;
  19.     lbProgressCap: TLabel;
  20.     lbProgress: TLabel;
  21.     pb: TProgressBar;
  22.     procedure bUploadClick(Sender: TObject);
  23.   private
  24.     FTotSize: Int64;
  25.     procedure DoOnReadStream(Sender: TObject; APos: Int64);
  26.     function UploadFile(const AURL, AFieldName, ASrcFileName, ADstFileName: String;
  27.           out AError: String): Boolean;
  28.     function FormatSize(Size: Int64): String;
  29.   public
  30.  
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. { TForm1 }
  41.  
  42. uses fphttpclient, openssl, opensslsockets;
  43.  
  44. { TDownloadStream }
  45.  
  46. type
  47.   TOnReadStream = procedure(Sender: TObject; APos: Int64) of object;
  48.   TUploadStream = class(TStream)
  49.   private
  50.     FOnReadStream: TOnReadStream;
  51.     FStream: TStream;
  52.   public
  53.     constructor Create(AStream: TStream);
  54.     destructor Destroy; override;
  55.     function Read(var Buffer; Count: LongInt): LongInt; override;
  56.     function Write(const Buffer; Count: LongInt): LongInt; override;
  57.     function Seek(Offset: LongInt; Origin: Word): LongInt; override;
  58.     procedure DoProgress;
  59.   published
  60.     property OnReadStream: TOnReadStream read FOnReadStream write FOnReadStream;
  61.   end;
  62.  
  63. constructor TUploadStream.Create(AStream: TStream);
  64. begin
  65.   inherited Create;
  66.   FStream:= AStream;
  67.   FStream.Position:= 0;
  68. end;
  69.  
  70. destructor TUploadStream.Destroy;
  71. begin
  72.   FStream.Free;
  73.   inherited Destroy;
  74. end;
  75.  
  76. function TUploadStream.Read(var Buffer; Count: LongInt): LongInt;
  77. begin
  78.   Result:= FStream.Read(Buffer, Count);
  79.   DoProgress;
  80. end;
  81.  
  82. function TUploadStream.Write(const Buffer; Count: LongInt): LongInt;
  83. begin
  84.   Result:= FStream.Write(Buffer, Count);
  85. end;
  86.  
  87. function TUploadStream.Seek(Offset: LongInt; Origin: Word): LongInt;
  88. begin
  89.   Result:= FStream.Seek(Offset, Origin);
  90. end;
  91.  
  92. procedure TUploadStream.DoProgress;
  93. begin
  94.   if Assigned(FOnReadStream) then
  95.     FOnReadStream(Self, Self.Position);
  96. end;
  97.  
  98. function TForm1.UploadFile(const AURL, AFieldName, ASrcFileName, ADstFileName: String;
  99.   out AError: String): Boolean;
  100. var
  101.   SS: TStringStream;
  102.   HTTPClient: TFPHTTPClient;
  103.   US: TUploadStream;
  104. begin
  105.   Result:= False;
  106.   SS:= TStringStream.Create('');
  107.   try
  108.     HTTPClient:= TFPHTTPClient.Create(nil);
  109.     try
  110.       HTTPClient.AllowRedirect:= True;
  111.       US:= TUploadStream.Create(TFileStream.Create(ASrcFileName, fmOpenRead or fmShareDenyWrite));
  112.       try
  113.         FTotSize:= US.Size;
  114.         US.FOnReadStream:= @DoOnReadStream;
  115.         try
  116.           HTTPClient.StreamFormPost(AURL, AFieldName, ADstFileName, US, SS);
  117.         except
  118.           on E: Exception do
  119.             AError:= E.Message;
  120.         end;
  121.       finally
  122.         US.Free
  123.       end;
  124.       Result:= SS.DataString = 'zipok';
  125.       if (not Result) and (Trim(SS.DataString) <> '') then
  126.         AError:= SS.DataString;
  127.     finally
  128.       HTTPClient.Free;
  129.       HTTPClient:= nil;
  130.     end;
  131.   finally
  132.     SS.Free;
  133.   end;
  134. end;
  135.  
  136. function TForm1.FormatSize(Size: Int64): String;
  137. const
  138.   KB = 1024;
  139.   MB = 1024 * KB;
  140.   GB = 1024 * MB;
  141. begin
  142.   if Size < KB then
  143.     Result:= FormatFloat('#,##0 Bytes', Size)
  144.   else if Size < MB then
  145.     Result:= FormatFloat('#,##0.0 KB', Size / KB)
  146.   else if Size < GB then
  147.     Result:= FormatFloat('#,##0.0 MB', Size / MB)
  148.   else
  149.     Result:= FormatFloat('#,##0.0 GB', Size / GB);
  150. end;
  151.  
  152. procedure TForm1.DoOnReadStream(Sender: TObject; APos: Int64);
  153. var
  154.   Value: Integer;
  155. begin
  156.   if FTotSize = 0 then
  157.     Exit;
  158.   lbProgress.Caption:= FormatSize(APos) + '/' + FormatSize(FTotSize);
  159.   Value:= Round(100*APos/FTotSize);
  160.   //workaround for pb animation bug(vista+) --> pb.Postion:= Value;
  161.   if Value < pb.Max  then
  162.   begin
  163.     pb.Position:= Value + 1;
  164.     pb.Position:= pb.Position - 1;
  165.   end
  166.   else
  167.   begin
  168.     pb.Max:= pb.Max + 1;
  169.     pb.Position:= pb.Max;
  170.     pb.Position:= pb.Position - 1;
  171.     pb.Max:= pb.Max - 1;
  172.   end;
  173.   //end workaround
  174.   Application.ProcessMessages;
  175.   Sleep(10);
  176. end;
  177.  
  178. procedure TForm1.bUploadClick(Sender: TObject);
  179. var
  180.   ErrMsg: String;
  181. begin
  182.   if UploadFile('https://xxxxxx.xxx/upload.php', 'bin', 'file.ext', 'file.ext', ErrMsg) then
  183.     ShowMessage('Successfully uploaded!')
  184.   else
  185.     ShowMessage('Cannot upload file: ' + sLineBreak + ErrMsg);
  186. end;
  187.  
  188. end.
  189.  

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][+][-]
  1. root@testmachine:/home/user# apt list openssl
  2. Listing... Done
  3. openssl/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

Lazarus: 3.2 / FPC: 3.2.2 [x86_64-win64-win32/win64]
Win10 x64
Debian 12

zeljko

  • Hero Member
  • *****
  • Posts: 1720
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: TFPHTTPClient SSL Error / Debian 12 "Bookworm"
« Reply #1 on: June 25, 2024, 11:19:50 am »
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

  • Hero Member
  • *****
  • Posts: 1325
Re: TFPHTTPClient SSL Error / Debian 12 "Bookworm"
« Reply #2 on: June 25, 2024, 12:32:29 pm »
FPC 3.2-fixes support OpenSSL 3 as of 2024-01-16
Best regards / Pozdrawiam
paweld

cris75

  • Jr. Member
  • **
  • Posts: 67
Re: TFPHTTPClient SSL Error / Debian 12 "Bookworm"
« Reply #3 on: June 26, 2024, 03:58:19 pm »
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
Lazarus: 3.2 / FPC: 3.2.2 [x86_64-win64-win32/win64]
Win10 x64
Debian 12

Thaddy

  • Hero Member
  • *****
  • Posts: 16653
  • Kallstadt seems a good place to evict Trump to.
Re: TFPHTTPClient SSL Error / Debian 12 "Bookworm"
« Reply #4 on: June 26, 2024, 05:39:22 pm »
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
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.
But I am sure they don't want the Trumps back...

paweld

  • Hero Member
  • *****
  • Posts: 1325
Re: TFPHTTPClient SSL Error / Debian 12 "Bookworm"
« Reply #5 on: June 26, 2024, 07:32:45 pm »
@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
Best regards / Pozdrawiam
paweld

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1255
Re: TFPHTTPClient SSL Error / Debian 12 "Bookworm"
« Reply #6 on: June 27, 2024, 02:07:16 am »
Hello,
@cris75, what is the result of the commands on a terminal ? :
Quote
sudo ldconfig -p | grep crypto
sudo ldconfig -p | grep ssl

friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

TRon

  • Hero Member
  • *****
  • Posts: 4148
Re: TFPHTTPClient SSL Error / Debian 12 "Bookworm"
« Reply #7 on: June 27, 2024, 03:05:54 am »
@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
Today is tomorrow's yesterday.

cris75

  • Jr. Member
  • **
  • Posts: 67
Re: TFPHTTPClient SSL Error / Debian 12 "Bookworm"
« Reply #8 on: June 27, 2024, 09:39:44 am »
@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
Lazarus: 3.2 / FPC: 3.2.2 [x86_64-win64-win32/win64]
Win10 x64
Debian 12

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1255
Re: TFPHTTPClient SSL Error / Debian 12 "Bookworm"
« Reply #9 on: June 27, 2024, 10:06:43 am »
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 :
Quote
sudo ldconfig
sudo ldconfig -p | grep crypto
sudo ldconfig -p | grep ssl

if you see libssl.so and libcrypto.so run you lazarus program

Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

cris75

  • Jr. Member
  • **
  • Posts: 67
Re: TFPHTTPClient SSL Error / Debian 12 "Bookworm"
« Reply #10 on: June 27, 2024, 11:09:29 am »
@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
Lazarus: 3.2 / FPC: 3.2.2 [x86_64-win64-win32/win64]
Win10 x64
Debian 12

Thaddy

  • Hero Member
  • *****
  • Posts: 16653
  • Kallstadt seems a good place to evict Trump to.
Re: TFPHTTPClient SSL Error / Debian 12 "Bookworm"
« Reply #11 on: June 27, 2024, 11:18:20 am »
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)
« Last Edit: June 27, 2024, 11:30:38 am by Thaddy »
But I am sure they don't want the Trumps back...

cris75

  • Jr. Member
  • **
  • Posts: 67
Re: TFPHTTPClient SSL Error / Debian 12 "Bookworm"
« Reply #12 on: June 27, 2024, 01:52:07 pm »
@Thaddy
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.
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
Lazarus: 3.2 / FPC: 3.2.2 [x86_64-win64-win32/win64]
Win10 x64
Debian 12

 

TinyPortal © 2005-2018