Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Send a replay-safe request with TLS 1.3 0-RTT

Send a read-only status query with the first TLS flight when reconnecting to a known service. The example uses the status/1 application protocol. Its STATUS command reads the session's status and can safely be repeated. A fresh session replies READY.

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.

Keep the ticket with the authenticated service

uses
  System.SysUtils, StreamSec.Mobile.TLSClient, StreamSec.Mobile.TlsClass,
  TaskTlsResume;

type
  tEarlyStatusClient = class(tTlsTicketClient)
  private
    fStatusOnConnect: Boolean;
  protected
    procedure ConfigureLayer(const aLayer: TCustomTLS_ContentLayer); override;
  public
    function ConnectAndReadStatus(out aDelivery: TTLSEarlyDataStatus): string;
  end;

The base class comes from Resume a TLS 1.3 connection with a session ticket. Create one tEarlyStatusClient with CreateForService, supplying the address, expected server name, port, trusted CA file and status/1 as the ALPN protocol. Reuse that instance for connections to the same service.

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.

Offer the complete query when it fits

const
  cStatusLine = 'STATUS'#13#10;

procedure tEarlyStatusClient.ConfigureLayer(const aLayer: TCustomTLS_ContentLayer);
var
  lRequest: TBytes;
begin
  inherited ConfigureLayer(aLayer);
  if not fStatusOnConnect then Exit;
  if (ALPNProtocols.Count <> 1) or (ALPNProtocols[0] <> 'status/1') then
    raise eTLSClientError.Create('The status query requires status/1');
  if aLayer.ResumptionTicket = nil then Exit;

  lRequest := TEncoding.ASCII.GetBytes(cStatusLine);
  if UInt32(Length(lRequest)) <= aLayer.ResumptionTicket.MaxEarlyData then
    aLayer.EarlyData := lRequest;
end;

The inherited layer configuration offers a ticket obtained from the same service. MaxEarlyData is a byte limit. The encoded query occupies eight bytes, including its CRLF terminator. When the ticket is absent or its allowance is smaller, the query waits for the handshake.

TLS early data can be replayed. The method offers only the fixed status query. Send operations that change state after the authenticated handshake.

Decide whether to send after the handshake

function tEarlyStatusClient.ConnectAndReadStatus(out aDelivery: TTLSEarlyDataStatus): string;
begin
  fStatusOnConnect := True;
  try
    Connect;
  finally
    fStatusOnConnect := False;
  end;
  try
    aDelivery := ContentLayer.EarlyDataStatus;
    if aDelivery in [edsNone, edsRejected] then
      Send(cStatusLine)
    else if aDelivery <> edsEnded then
      raise eTLSClientError.Create('The early-data outcome is unknown');
    if not ReadLine(Result, IOTimeout, 64) then
      raise eTLSClientError.Create('No complete status reply');
  except
    Disconnect;
    raise;
  end;
end;

Call ConnectAndReadStatus on the disconnected client. After Connect succeeds, edsEnded means TLS accepted the early data. Read the reply without sending another query. With edsRejected the server declined the early data, and with edsNone the client sent none. In either case the method sends the query once over the established connection.

The first call uses a full handshake and can collect a ticket while reading the reply. Call Disconnect before the next query and free the client when finished. A resumed handshake can still decline early data. If the connection fails before the reply arrives, the application cannot infer from the missing reply whether the peer processed the query.

See Handle TLS 1.3 early data on an asynchronous server for the listener's early-data policy and the separate callback for an accepted early query.

Reference

TsmTLSClient · StreamSec.Mobile.TLSClient · StreamSec.Mobile.TlsClass

Related tasks