SecureSockets2 2.01 Beta - Secure_* interface
=============================================
Copyright (c) RISC OS Developments 2026. All rights reserved.
Maintained by RISC OS Technologies 2026. Licensed under CDDL 1.0.

SecureSockets2 is one module. The binary file is SecureSockets2, and it
registers the historic module title SecureSockets 2.01. It directly provides
the Secure_* SWIs at &54280; there is no second compatibility module and no
public TLSManager API.

The distributed module and its statically linked OpenSSL engine are compiled
for the ARMv4 instruction-set baseline. OpenSSL assembler is disabled, so the
same module runs on ARMv4 RISC OS 4 systems and later ARM machines.

Loading
-------
Install !SecureSockets2 in !Boot.Resources. Its !Run uses:
  RMEnsure SecureSockets 2.01 RMLoad <SecureSockets2$Dir>.SecureSockets2

Security
--------
Client contexts always use TLS 1.2 or TLS 1.3. Original clients can pass
context type 0, 2, 4 or 6 without a hostname; their certificate chain is
checked against InetDBase:CertData, but the DNS name cannot be checked.
Updated clients add &100 and pass the host name in R2:
  SYS "Secure_Connect",4+&100,sock%,"www.example.com" TO ssl%
This enables SNI and DNS-name verification as well as chain verification.

The caller owns the connected Internet socket and closes it after
Secure_Disconnect. Server contexts and Secure_Accept are not supported.
Descriptors are tagged handles rather than OpenSSL pointers.

SWIs
----
&54280 Secure_Connect
  R0=context type, R1=connected socket, optional R2=hostname; returns SSL.
&54282 Secure_Read
&54283 Secure_Peek
&54284 Secure_Write
  R0=SSL, R1=buffer, R2=size; returns bytes, zero at EOF, or -1.
&54285 Secure_GetError
  Returns 0 none, 1 TLS, 2 wants read, 3 wants write, 6 peer closed.
&54286 Secure_Disconnect
  Sends the TLS close alert and releases the TLS handle.
&54287 Secure_NewCTX
  R0=context type; returns context handle.
&54288 Secure_ConnectCTXToSocket
  R0=context, R1=socket, optional R2=hostname; returns SSL handle.
&5428D Secure_GetPeerCert
&5428E Secure_FreeCert
&5428F Secure_X509_getsubjectname
&54290 Secure_X509_getissuername
&54291 Secure_X509_NameOneline
  Access the saved subject and issuer names of the verified peer certificate.
&54292 Secure_State
  Returns &1000 connecting, 3 connected, or 0 failed.
&54293 Secure_PollConnect
  Returns 1 connected or -1 not ready.
&54295 Secure_InstallCerts
  R0=flags, R1=CertData path, R2=signed manifest path. Returns certificate
  count and manifest sequence after verification and installation.

Reading and writing large amounts of data
-----------------------------------------
Secure_Read returns data one TLS record at a time. A record holds at most
16384 bytes, so a single call never returns more than 16384 bytes, however
large the buffer in R2 is. It returns less when the buffer is smaller than
the rest of the record, or when only part of a record has arrived. A result
smaller than R2 does not mean the data has ended: always call Secure_Read in
a loop until you have all the data you expect. Reading 200000 bytes takes at
least 13 calls even with a 300000 byte buffer.

If the data is several times the size of the buffer nothing is lost. What
the buffer cannot hold stays queued, in order, for the next call: the rest of
the current record is already decrypted inside SecureSockets2 and later
records wait in the socket's receive buffer, where TCP flow control slows the
sender if it fills.

What each result means:
  1 to R2  that many bytes are in your buffer; add them up and read again.
  0        the peer closed the TLS stream (Secure_GetError gives 6).
  -1       call Secure_GetError: 2 means no complete record has arrived yet
           (non-blocking sockets only; a blocking socket waits instead),
           3 means it wants to write, 1 is a TLS error.

Waiting for the socket: after a short read the rest of that record has
already been taken from the socket and decrypted, so the socket can show
nothing to read while data is still waiting inside SecureSockets2.
Socket_Select then reports nothing readable and the data stays unread until
the peer sends more. Do not read once each time the socket becomes readable.
Either
  - call Secure_Read until it returns -1 with Secure_GetError 2, and only
    then wait on the socket; or
  - use a buffer of at least 16384 bytes, so each call takes a whole record.

Secure_Peek returns the same data as the next Secure_Read, also at most the
rest of the current record. Secure_Write sends the whole buffer on a
blocking socket; on a non-blocking socket it may send only part (add the
result to what you have sent and call again) or return -1 with
Secure_GetError 3.

Secure_Accept, Secure_AcceptCTXToSocket, Secure_PollAccept and the
Secure_CTX_* server certificate/key calls return an unsupported error.
