Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Minimal SSL TCPClient and server, VCL
#3
(10-15-2019, 08:48 PM)logihouse Wrote: No errormessages, but only rubbish received in ServerExecute.

You are not setting the SSLIOHandler to PassThrough=False on the server side. It is True by default, in order to support STARTTLS-like protocols. As such, your OnExecute code ends up reading the client's raw SSL/TLS data. You need to set PassThrough=False on the server side, on a per-connection basis. In your example, the OnConnect event would be the appropriate place to do so, since the client is initiating the SSL/TLS handshake as soon as the TCP connection is established and before any application data is sent:

Code:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, uIdContext, IdContext,
  IdServerIOHandler, IdSSL, IdSSLOpenSSL, IdBaseComponent, IdComponent,
  IdCustomTCPServer, IdTCPServer, Vcl.StdCtrls, IdIOHandler, IdIOHandlerSocket,
  IdIOHandlerStack, IdTCPConnection, IdTCPClient;

type
  TForm1 = class(TForm)
    Server: TIdTCPServer;
    Client: TIdTCPClient;
    cSSL: TIdSSLIOHandlerSocketOpenSSL;
    Button1: TButton;
    SSL: TIdServerIOHandlerSSLOpenSSL;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ServerConnect(AContext: TIdContext);
    procedure ServerExecute(AContext: TIdContext);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
  UseSSL = True;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Client.Connect;
  try
    Client.IOHandler.WriteLn('test');
  finally
    Client.Disconnect;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Server := TIdTCPServer.Create(Self);
  Server.DefaultPort := 443;
  Client := TIdTCPClient.Create(Self);
  Client.Host := 'localhost';
  Client.Port := 443;
  Server.OnConnect := ServerConnect;
  Server.OnExecute := ServerExecute;
  if UseSSL then
  begin
    cSSL := TIdSSLIOHandlerSocketOpenSSL.Create(Client);
    cSSL.PassThrough := False;
    cSSL.SSLOptions.Mode := sslmClient;
    cSSL.SSLOptions.VerifyMode := [];
    cSSL.SSLOptions.VerifyDepth  := 0;
    cSSL.SSLOptions.SSLVersions := [sslvSSLv2..sslvTLSv1_2];    // Avoid using SSL}
    Client.IOHandler := cSSL;  // TIdSSLIOHandlerSocketOpenSSL

    SSL := TIdServerIOHandlerSSLOpenSSL.Create(Server);
    SSL.SSLOptions.Mode := sslmServer;
    SSL.SSLOptions.VerifyMode := [];
    SSL.SSLOptions.VerifyDepth  := 0;
    SSL.SSLOptions.SSLVersions := [sslvSSLv2..sslvTLSv1_2];
    Server.IOHandler := SSL;   // TIdServerIOHandlerSSLOpenSSL
  end;

  Server.Active := True;
end;

procedure TForm1.ServerConnect(AContext: TIdContext);
begin
  if AContext.Connection.IOHandler is TIdSSLIOHandlerSocketBase then
    TIdSSLIOHandlerSocketBase(AContext.Connection.IOHandler).PassThrough := False;
end;

procedure TForm1.ServerExecute(AContext: TIdContext);
var
  s : string;
begin
  s := AContext.Connection.IOHandler.AllData;
end;

end.

Reply


Messages In This Thread
RE: Minimal SSL TCPClient and server, VCL - by rlebeau - 10-15-2019, 11:54 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)