Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Expose a Delphi class through Snif and call it from a client

A Delphi client can call an ordinary method on a server through a typed interface. In this example the client passes two integers to Add. The server authenticates the account, checks its role and returns the sum.

The components on this page are normally placed on a form or a data module at design time and set up in the Object Inspector. The sample creates them in code instead, because code shows at a glance which properties are changed from their default values.

Declare the interface and server class

uses
  System.SysUtils, StreamSec.Snif.ServerAttributes, StreamSec.Snif.ClassServer,
  StreamSec.Snif.KAPA, StreamSec.Snif.AkeAuthentication,
  StreamSec.Snip.TLSClient, StreamSec.Snip.TLSServer, StreamSec.Mobile.AsyncTLSServer,
  StreamSec.DSI.CompositeKEM, StreamSec.DSI.MLC, StreamSec.DSI.CurveXArith,
  stAESCT;
type
  {$M+}
  iTaskCalculator = interface
    ['{25713F90-6F35-4624-91CF-53EB012F3562}']
    function Add(A, B: Integer): Integer;
  end;
  {$M-}

  {$METHODINFO ON}
  [Roles('calculator')]
  TTaskCalculator = class
  public
    function Add(A, B: Integer): Integer;
  end;
  {$METHODINFO OFF}

Put these declarations in the interface section of TaskSnifCall.pas. The client needs the interface GUID and method RTTI to build a proxy. On the server, {$METHODINFO ON} emits the RTTI Snif uses to dispatch the call. Match the method name, parameter names and types, and return type on both sides. The Roles attribute names the role allowed to call this class.

Register the class and attach the Snif service

function TTaskCalculator.Add(A, B: Integer): Integer;
begin
  Result := A + B;
end;

procedure AttachSnifService(aPlugin: TsnipTLSServer; aListener: TsmAsyncTLSServer);
begin
  aPlugin.Server := aListener;
end;
initialization
  TsnifClassServer.RegisterServerClass(TTaskCalculator);

Link the registration unit into the server. Its initialization registers the class once for the process. Create a TsnipTLSServer and pass it to AttachSnifService with your TsmAsyncTLSServer listener. The plugin supplies the Snif protocol and owns the class server.

Set the listener's bind address and port, and load the server certificate, its private key and the CA chain before activating the listener. This example authenticates the client with a Snif account. In the listener's OnConfigureEngine, set Options.RequestClientCertificate and Options.RequireClientCertificate to False for that policy. Run an asynchronous TLS server shows how to configure the listener and import the certificate and key.

Provision the account

function NewSnifUser(const aUser, aPassword: string;
  const aRoles: array of string): TsnifUserListItem;
var
  lFile: TsnifPwFile;
  i: Integer;
begin
  Result := Default(TsnifUserListItem);
  lFile := AkeRegister(TEncoding.UTF8.GetBytes(aUser), TEncoding.UTF8.GetBytes(aPassword),
    nil, TsnifAkeParams.Default, nil, nil);
  Result.fName := aUser;
  Result.fAuthData := lFile.Serialize;
  SetLength(Result.fRoles, Length(aRoles));
  for i := 0 to High(aRoles) do
    Result.fRoles[i] := aRoles[i];
end;

At enrollment, pass the account name, its password and ['calculator'] to NewSnifUser. Store the returned record in your TsnifUserList. Its fAuthData contains the serialized KAPA password file. fRoles lists the roles this account may request. Call Plugin.ClassServer.UpdateUserList(Users) with the full list, then set Listener.Active := True.

Connect and call Add

The client checks, on every connection, that the server's certificate is issued for the host name it connects to. Give it the DNS name the certificate carries, not the server's IP address: with an address the connection fails although the server's certificate is valid. To connect by address, set ServerName to the name in the certificate - see Download a page over HTTPS.
function OpenSnifClient(const aHost, aServerName: string; aPort: Word;
  const aCAFile, aUser, aPassword: string): TsnipTLSClient;
begin
  Result := TsnipTLSClient.Create(nil);
  try
    Result.Host := aHost;
    Result.ServerName := aServerName;
    Result.Port := aPort;
    if Result.TLSServer.LoadRootCertsFromFile(aCAFile) = 0 then
      raise EArgumentException.Create('No trusted certificate in the CA file');
    Result.SnifUsername := aUser;
    Result.SnifPassword := aPassword;
    Result.Username := aUser;
    Result.HandshakeTimeout := 15000;
    Result.ReadTimeout := 15000;
    Result.Start;
  except
    Result.Free;
    raise;
  end;
end;

Pass the TCP destination as aHost, the DNS name on the server certificate as aServerName, and the trusted CA file as aCAFile. Start establishes TLS and logs in with SnifUsername and SnifPassword. Set Username to the same account name for the Snif requests that follow.

function CallCalculator(aClient: TsnipTLSClient; A, B: Integer): Integer;
var
  lCalc: iTaskCalculator;
begin
  if not aClient.MakeInstance('TaskSnifCall.TTaskCalculator', TypeInfo(iTaskCalculator),
    lCalc, 'calculator') then
    raise Exception.Create('Could not create the calculator proxy');
  Result := lCalc.Add(A, B);
end;

MakeInstance takes the unit-qualified server class name, the client's interface type information and the requested role. With the connected client, CallCalculator(Client, 2, 3) returns 5. The local interface goes out of scope after the call. Free the client when you have finished using the connection. See the Snif notes for the remoting and authentication model.

Reference

TsnipTLSServer · TsnipTLSClient · TsmAsyncTLSServer · StreamSec.Snif.ClassServer · StreamSec.Snif.ServerAttributes · StreamSec.Snif.KAPA · StreamSec.Snif.AkeAuthentication · StreamSec.Snip.TLSServer · StreamSec.Snip.TLSClient · StreamSec.Mobile.AsyncTLSServer

Related tasks