Einzelnen Beitrag anzeigen

Martin W

Registriert seit: 29. Mai 2004
Ort: Augsburg
220 Beiträge
 
Delphi XE3 Enterprise
 
#1

Indy - Client / Server - TCP über SSL - Verbindungsproblem

  Alt 20. Aug 2009, 12:52
Hallo!

Ich habe vor kurzem versucht mit hilfe der Indykomponenten und OpenSSL eine TCP / IP Verbindung über SSL hinzubekommen. Dazu habe ich folgende Units erstellt:

Der Server lässt sich dabei auf aktiv stellen, wenn der Client versucht sich zu verbinden, steht in der Server log das der Client sich verbunden hat. Der Client bleibt anschließend fehlermeldungslos hängen.

Der Quellcode des Servers

Delphi-Quellcode:
unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdServerIOHandler, IdSSL, IdSSLOpenSSL, IdBaseComponent, IdComponent,
  IdCustomTCPServer, IdTCPServer, idContext, StdCtrls;

type
  TForm1 = class(TForm)
    IdTCPServer1: TIdTCPServer;
    IdServerIOHandlerSSLOpenSSL1: TIdServerIOHandlerSSLOpenSSL;
    Memo1: TMemo;
    CheckBox1: TCheckBox;
    procedure CheckBox1Click(Sender: TObject);
    procedure IdTCPServer1Connect(AContext: TIdContext);
    procedure IdTCPServer1Disconnect(AContext: TIdContext);
    procedure IdTCPServer1Execute(AContext: TIdContext);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  IdTCPServer1.Active := Checkbox1.Checked;
end;

procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
  Memo1.Lines.Add('Connect: ' + AContext.Binding.PeerIP);
end;

procedure TForm1.IdTCPServer1Disconnect(AContext: TIdContext);
begin
  Memo1.Lines.Add('Disconnect: ' + AContext.Binding.PeerIP);
end;

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
begin
  Memo1.Lines.Add('Message: ' + AContext.Connection.IOHandler.ReadLn);
end;

end.
Die Konfiguration der Serverkomponenten:

Delphi-Quellcode:
  object IdTCPServer1: TIdTCPServer
    Bindings = <
      item
        IP = '127.0.0.1'
        Port = 51203
      end>
    DefaultPort = 51203
    IOHandler = IdServerIOHandlerSSLOpenSSL1
    OnConnect = IdTCPServer1Connect
    OnDisconnect = IdTCPServer1Disconnect
    OnExecute = IdTCPServer1Execute

  end
  object IdServerIOHandlerSSLOpenSSL1: TIdServerIOHandlerSSLOpenSSL
    SSLOptions.RootCertFile = 'ca.crt'
    SSLOptions.CertFile = 'Server.crt'
    SSLOptions.KeyFile = 'Server.key'
    SSLOptions.Method = sslvSSLv23
    SSLOptions.Mode = sslmUnassigned
    SSLOptions.VerifyMode = []
    SSLOptions.VerifyDepth = 0
  end

Der Quellcode des Clients

Delphi-Quellcode:
unit CltProject;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL;

type
  TForm1 = class(TForm)
    IdTCPClient1: TIdTCPClient;
    Edit1: TEdit;
    Button1: TButton;
    IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  IdTCPClient1.Connect;
end;

end.
Die Konfiguration der Clientkomponenten:

Delphi-Quellcode:
  object IdTCPClient1: TIdTCPClient
    IOHandler = IdSSLIOHandlerSocketOpenSSL1
    ConnectTimeout = 5000
    Host = '127.0.0.1'
    IPVersion = Id_IPv4
    Port = 51303
    ReadTimeout = -1
    Left = 216
    Top = 200
  end
  object IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL
    Destination = '127.0.0.1:51203'
    Host = '127.0.0.1'
    MaxLineAction = maException
    Port = 513203
    DefaultPort = 0
    UseNagle = False
    SSLOptions.Method = sslvSSLv23
    SSLOptions.Mode = sslmUnassigned
    SSLOptions.VerifyMode = []
    SSLOptions.VerifyDepth = 0
    SSLOptions.RootCertFile = 'ca.crt'
    SSLOptions.CertFile = 'Client.crt'
    SSLOptions.KeyFile = 'Client.key'
  end

Die verwendeten Zertifkate:

ca.crt > Öffentlicher Schlüssel der Root CA
Server.crt / Client.crt > Zertifikat des Servers / Clients
Server.key / Client.key > Privater Schlüssel des Servers / Clients

Warum bleibt die Verbindung nun immer hängen? In der Serverlog steht anschließend folgendes:

Zitat:
Connect: 127.0.0.1
Message: €z
Logfile:

Zitat:
127.0.0.1:51257 Stat Connected.
127.0.0.1:51257 Recv 20.08.2009 15:28:52:  Y UJO”µÈ°ÝÍêçŠuÀ‰„`?çlHA°*ãK.“z½ ( 9 8 5  
3 2 /            #
  Mit Zitat antworten Zitat