TLS/HTTPS Sockets
Spectranext supports TLS/SSL (Transport Layer Security) for secure, encrypted network connections. This allows ZX Spectrum programs to access modern HTTPS websites and other encrypted services.
Automatic TLS(HTTPS) Decryption
Spectranext automatically handles TLS encryption for any socket connection to port 443 (the standard HTTPS port). When your program opens a socket and connects to port 443, Spectranext transparently:
- Detects port 443: Automatically recognizes HTTPS connections
- Establishes TLS handshake: Performs the TLS/SSL handshake with the server
- Encrypts/decrypts data: All data sent and received is automatically encrypted and decrypted
- Verifies certificates: Validates server certificates to ensure secure connections
No special code required - simply connect to port 443 and use the socket normally. The encryption happens transparently.
ESP receive-task memory
The ESP keeps eight socket slots, but allocates each 4 KiB receive-task stack
only when that socket opens. CLOSE shuts down I/O, waits for the receiver to
park, then deletes its task and frees the TLS/SSH session. A listening socket
also needs its descriptor closed to cancel a blocked accept() in lwIP.
The descriptor belongs to the socket slot; TLS cleanup must not close an old
copy of a descriptor that might already have been reused.
If a receiver fails to park within ten seconds, cleanup logs an error and retains its resources rather than deleting a task inside library code. A later close/open can retry cleanup. This is a safety fallback, not a successful close.
self-test tls lifecycle exercises repeated open/close of four controller
sockets using idle TCP, UDP reception and blocked TCP listeners. Each round
waits for an ESP metadata response so queued commands cannot masquerade as
completed operations. Follow it with self-test tls and compare wifi-status
heap readings after all sockets close. ESP-IDF stack high-water marks are bytes.
Using HTTPS
From BASIC
10 REM Connect to HTTPS website
20 %connect #4, "www.example.com", 443
30 PRINT #4; "GET / HTTP/1.1"
40 PRINT #4; "Host: www.example.com"
50 PRINT #4
60 INPUT #4, a$
70 PRINT a$
80 %close #4
Custom ports: TLS and SSH
For a TLS or SSH server on a nonstandard port, explicitly select the protocol
when creating the socket. The SDK's <sys/socket.h> provides:
#define SPECTRANET_PROTO_TLS 0xFFF0
#define SPECTRANET_PROTO_SSH 0xFFF1
Pass the selector as the third argument to
socket(AF_INET, SOCK_STREAM, protocol), then pass the server's actual port to
connect(). These selectors require AF_INET and SOCK_STREAM. They take
precedence over port detection; protocol 0 keeps automatic TLS on port 443 and
SSH on port 22, with ordinary TCP on other ports.
From C (z88dk)
This helper returns a connected socket or -1. Call it with the Spectranet ROM
paged in, as for other SDK socket operations:
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
int open_secure_socket(char *hostname, unsigned int port, int protocol)
{
struct hostent *he;
struct sockaddr_in addr;
int sockfd;
he = gethostbyname(hostname);
if (!he)
return -1;
sockfd = socket(AF_INET, SOCK_STREAM, protocol);
if (sockfd < 0)
return -1;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = he->h_addr;
if (connect(sockfd, &addr, sizeof(addr)) < 0)
{
sockclose(sockfd);
return -1;
}
return sockfd;
}
/* Example calls: replace these names and ports with your servers. */
/* int tls_fd = open_secure_socket("example.com", 8443, SPECTRANET_PROTO_TLS); */
/* int ssh_fd = open_secure_socket("user@example.com", 2222, SPECTRANET_PROTO_SSH); */
Resolve the hostname through gethostbyname() so the firmware can associate it
with the connection (including the TLS hostname and optional SSH username).
The SDK's htons() is a compatibility no-op: its socket wrappers handle the
port representation for the ROM.
For TLS, use send() / recv() to exchange plaintext application data; the
firmware handles TLS on the selected port. HTTPS requests still need the usual
HTTP request and Host header. Close the socket with sockclose() when done.
For SSH, a successful connect() starts session setup. Read the line-oriented
control messages, handle TRUST?, USER?, and PASSWORD? as requested, and wait
for CONNECTED before sending shell input. Replies end in \r\n; ERROR
indicates setup failure. After CONNECTED, send() / recv() carry terminal
input/output. See SSH control protocol for the complete
message and authentication flow.
From Assembly (direct ROM calls)
The ROM SOCKET entry takes the socket type in C and the full 16-bit protocol
selector in DE, returning the descriptor in A. Both bytes matter:
D = 0xFF, with E = 0xF0 for TLS or E = 0xF1 for SSH. Initialize DE = 0
for default port-based behavior.
; ROM must be paged in; SOCKET and CONNECT are ROM entry symbols.
; remote_ip points to a resolved four-byte destination IPv4 address.
ld c, 1 ; SOCK_STREAM
ld de, 0xFFF0 ; TLS; use 0xFFF1 for SSH
call SOCKET
jr c, socket_error
ld (secure_fd), a ; Save the one-byte descriptor for I/O and close
ld bc, 8443 ; Actual server port; e.g. 2222 for SSH
ld de, remote_ip ; Pointer to four-byte IP, not sockaddr_in
call CONNECT ; A = socket descriptor, BC = port, DE = IP pointer
jr c, connect_error ; Close the saved socket on failure
The selectors start TLS or SSH as part of connection setup; they are not an in-place upgrade of an already connected plaintext socket.
How It Works
When you connect to port 443 with protocol 0, or explicitly select TLS on a custom port:
- TCP Connection: First, a normal TCP connection is established
- TLS Detection: Spectranext uses the explicit TLS selector or detects the port 443 destination
- TLS Handshake: Automatically performs TLS handshake with the server
- Certificate Verification: Validates the server's SSL certificate
- Encrypted Communication: All subsequent data is encrypted/decrypted transparently
Your program uses the socket exactly as it would for a normal TCP connection - the encryption is handled automatically by Spectranext.
Certificate Authority (CA) Certificates
Spectranext includes a curated list of trusted Certificate Authority (CA) certificates for validating server certificates. The CA store includes the following root CAs:
Included CA Certificates
- ISRG Root X1 - Internet Security Research Group (Let's Encrypt)
- ISRG Root X2 - Internet Security Research Group (Let's Encrypt)
- DigiCert Global Root G2 - DigiCert Inc.
- DigiCert TLS RSA Root CA - DigiCert Inc.
- DigiCert TLS ECC Root CA - DigiCert Inc.
- GTS Root R1/R2/R3/R4 - Google Trust Services LLC
- Amazon Root CA 1/2 - Amazon
- GlobalSign Root R3 - GlobalSign
- USERTrust RSA Certification Authority - The USERTRUST Network
- USERTrust ECC Certification Authority - The USERTRUST Network
- Microsoft RSA Root Certificate Authority 2017 - Microsoft Corporation
- Entrust Root Certification Authority - G2 - Entrust, Inc.
- Baltimore CyberTrust Root - Baltimore CyberTrust
These CAs cover the majority of modern HTTPS websites, including:
- Let's Encrypt certificates (used by many free SSL certificate providers)
- Google services (Gmail, YouTube, etc.)
- Amazon Web Services (AWS)
- Microsoft services
- Major commercial certificate providers (DigiCert, GlobalSign, etc.)
Certificate Validation
For both automatic and explicitly selected TLS connections, Spectranext:
- Receives the server's SSL certificate
- Validates the certificate chain against the included CA store
- Verifies the certificate hasn't expired
- Checks the certificate matches the hostname (SNI)
- Only allows the connection if validation succeeds
If certificate validation fails, the TLS connection will be rejected and your program will receive an error.
Limitations
- Port selection: Automatic TLS uses port 443; other TLS ports require
SPECTRANET_PROTO_TLSwhen creating the socket - Client mode: Only client-side TLS is supported (connecting to servers)
- Certificate validation: Server certificates are validated automatically against the included CA certificate store
- TLS 1.2: Supports TLS 1.2 protocol
- Limited CA list: Only certificates issued by the included CAs are trusted
Next Steps
- Socket APIs - Understanding underlying socket API
- Memory Architecture - Understanding TLS buffer management
- Check TLS library documentation for advanced features