Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Connect with X25519MLKEM768 in TLS 1.3

Use X25519 and ML-KEM-768 together to establish a TLS 1.3 connection. TLS combines their shared secrets. The server still needs a certificate for its DNS name, and the client needs a trust anchor for that certificate.

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.

Set the group on both peers

uses
  StreamSec.Mobile.TLSClient, StreamSec.Mobile.TlsInternalServer,
  StreamSec.Mobile.TlsClass, StreamSec.Mobile.TlsConst,
  StreamSec.Mobile.Tls13Handshake;

type
  eHybridTLSRequired = class(eTLSClientError);
procedure UseHybridGroup(aEngine: TsmSimpleTLSInternalServer);
begin
  aEngine.Options.SupportedGroups := [ecsX25519MLKEM768];
end;

On a TsmAsyncTLSServer, call UseHybridGroup(aEngine) from OnConfigureEngine. The event runs once for each engine when the listener starts. Load the server's certificate and private key and configure its application protocol as in Run an asynchronous TLS server.

On the client, call the same procedure on TsmTLSClient.TLSServer before Connect. Both components offer TLS 1.3 by default. The group setting selects X25519MLKEM768 for the client's initial key share and enables the server to select that hybrid group. The client also advertises classical curves. Check the negotiated group on each connection for which your application requires a hybrid exchange.

Connect and check the negotiated group

function OpenHybridTLS(const aHost, aServerName, aCAFile: string;
  aPort: Word): TsmTLSClient;
var
  lLayer: TCustomTLS_ContentLayer;
begin
  Result := TsmTLSClient.Create(nil);
  try
    Result.Host := aHost;
    Result.Port := aPort;
    Result.ServerName := aServerName;
    if Result.TLSServer.LoadRootCertsFromFile(aCAFile) = 0 then
      raise eTLSClientError.CreateFmt('No trust anchors in %s', [aCAFile]);
    UseHybridGroup(Result.TLSServer);
    Result.Connect;
    if not Result.Secure then
      raise eTLSClientError.Create('TLS handshake did not complete');
    lLayer := Result.ContentLayer;
    if (lLayer.Version.major <> 3) or (lLayer.Version.minor <> cTLS13vmm) or
       (lLayer.NegotiatedGroup <> grp_x25519mlkem768) then
      raise eHybridTLSRequired.CreateFmt(
        'Expected TLS 1.3 with X25519MLKEM768. Negotiated version $%.2x%.2x, group $%.4x',
        [lLayer.Version.major, lLayer.Version.minor, lLayer.NegotiatedGroup]);
  except
    Result.Free;
    raise;
  end;
end;

Pass the address and port to dial, the expected DNS name, and the CA file used to verify the server. When dialing a local service at 127.0.0.1 with a certificate for localhost, pass localhost as aServerName. The client checks the certificate chain and the expected name during Connect. See Connect a TLS client with certificate checks.

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.

After the handshake, ContentLayer.Version identifies the negotiated TLS version and NegotiatedGroup identifies the group selected by the peers. The function checks both before returning a connected client. Send application data only after it returns, and free the client when finished.

A server supporting only classical X25519 can request a new key share. If the handshake selects a classical group or an older TLS version, OpenHybridTLS closes the connection and raises eHybridTLSRequired with the negotiated version and group codes. The TLS notes describe the other supported hybrid groups.

Reference

TsmTLSClient · TsmAsyncTLSServer · StreamSec.Mobile.TLSClient · StreamSec.Mobile.TlsInternalServer · StreamSec.Mobile.TlsClass · StreamSec.Mobile.TlsConst · StreamSec.Mobile.Tls13Handshake

Related tasks