Cosa fa
- Suite AES-128-GCM, ChaCha20-Poly1305 e AES-256-GCM
- Gruppi x25519 e secp256r1
- Firme del server ECDSA P-256/P-384 e RSA-PSS
- SNI, ALPN, HelloRetryRequest, KeyUpdate, close_notify
- <code>CTlsStream</code> con I/O, e <code>CTlsEngine</code> senza I/O per codice con un proprio loop
- Catena, date e nome sempre verificati. Key pinning opzionale
- Root di sistema su Windows e Linux
- DER rigoroso e verifica del nome host secondo RFC 9525
- Nuove primitive aggiunte a <code>cryptography</code> per il TLS: SHA-384, HMAC e HKDF con SHA-256/384/512, AES-GCM (bitsliced a tempo costante, AES-NI con PCLMULQDQ quando disponibile), X25519, P-256 (ECDH ed ECDSA secondo RFC 6979), verifica P-384 e RSA
Misurato
- Primitive simmetriche: 47 vettori ufficiali e 127,502 controlli incrociati con OpenSSL 3.5 e Python <code>cryptography</code> 46, 0 divergenze
- Chiave pubblica: 59,001 controlli incrociati con l'oracolo, 0 divergenze. ECDSA P-256 identico byte per byte a OpenSSL
- X.509: 23,643 controlli incrociati su catene casuali. Fuzzing con 100,000 mutazioni sotto ASan, nessun certificato modificato verificato
- TLS 1.3: la trace RFC 8448 riprodotta byte per byte. 85,000 casi di fuzzing in Release senza crash
- Interoperabilità: 31 casi contro <code>openssl s_server</code>, <code>openssl s_client</code> e Python <code>ssl</code>, in entrambi i ruoli, 0 errori
Limiti noti
- Niente TLS 1.2, 0-RTT, ripresa di sessione, certificati client, OCSP o CRL
- Il server supporta solo chiavi P-256. AES-GCM va a circa 1.9 GB/s con AES-NI e a 55 MB/s in scalare
An in-house TLS 1.3 (RFC 8446), client and server, with the X.509 it needs: DER, certificate, chain, host name, PEM and the system roots. No OpenSSL and no external dependency at all. Always built with the library, like network: it has no CMake option of its own.
The HTTPS client of the http module (Ethereal::Web::CWeb) runs on top of it.
#include <ethereal/cpp/tls/stream.hpp>
#include <ethereal/cpp/tls/x509.hpp>
using namespace Ethereal::Tls;
CTrustStore Roots;
Roots.LoadSystem(); // system roots
STlsClientConfig Config;
Config.pTrustStore = &Roots; // chain, dates and name: always checked
CTlsStream Stream;
STlsStatus Status = Stream.Connect( "example.com", 443, Config );
if( !Status.IsOk() )
printf( "%s\n", Status.ToString().c_str() ); // "certificate_rejected: sent unknown_ca (unknown issuer)"
Stream.Send( Bytes );
Stream.Receive( Buffer, uReceived );
Stream.Close(); // close_notify---
TLS 1.3 only
There is no TLS 1.2 or earlier, on purpose. 1.2 doubles the surface: another handshake, another key schedule, renegotiation, CBC suites with the padding attacks, RSA for key exchange, and the downgrade defenses between the two. All current servers accept 1.3. A 1.2-only server is refused with protocol_version, and the client checks the DOWNGRD sentinel in the ServerHello random (illegal_parameter).
| What | Supported |
|---|---|
| Suites | TLS_AES_128_GCM_SHA256, TLS_CHACHA20_POLY1305_SHA256, TLS_AES_256_GCM_SHA384 |
| Groups | x25519, secp256r1 (key share only for the first, the other costs a HelloRetryRequest) |
| Signatures accepted from the server | ecdsa_secp256r1_sha256, ecdsa_secp384r1_sha384, rsa_pss_rsae_sha256/384/512 |
| Certificate signatures | the ones above and rsa_pkcs1_sha256/384/512 |
| Server | P-256 key, signs ecdsa_secp256r1_sha256 (RFC 6979) |
| Extensions | server_name, supported_versions, supported_groups, key_share, signature_algorithms(_cert), ALPN, cookie |
| Record | 2^14 limits, close_notify (half close), KeyUpdate (requested, answered and automatic every 2^24 records), NewSessionTicket checked and ignored |
| Out | TLS 1.2, 0-RTT, PSK and resumption, client certificate (the client sends an empty Certificate, the server does not request one), rsa_pss_pss_*, record_size_limit, OCSP and SCT, exporters |
---
The API
| File | Pieces |
|---|---|
tls_common.hpp | ETlsAlert, ETlsCipherSuite, ETlsGroup, ETlsSignatureScheme, ETlsError, STlsStatus (error, alert, certificate status, TCP result, ToString), ETlsVerifyMode, STlsClientConfig, STlsServerConfig (with LoadPem) |
stream.hpp | CTlsStream: Connect, ConnectOver, Accept, Send, Receive, Close, RequestKeyUpdate, and STlsStreamOptions (connection, handshake and per-I/O timeouts) |
engine.hpp | CTlsEngine: the protocol without I/O (Feed, PendingOutput, NextEvent, Read, Write), for those who have their own event loop |
key_schedule.hpp | CKeySchedule: the key schedule from RFC 8446 section 7 |
x509.hpp | CCertificate, CPublicKey, CTrustStore (AddPem, AddDer, LoadSystem), SVerifyOptions, VerifyChain, ECertStatus, ParseIpLiteral |
pem.hpp | PemDecodeAll, LoadP256PrivateKeyPem, LoadP256PrivateKeyDer |
der.hpp | Der::CReader and the strict DER reads |
The client verification mode: eVerifyChain (the default: chain up to the store, dates, name and the pin when there is one), eVerifyPinOnly (only the SHA-256 of the leaf's SubjectPublicKeyInfo, for a self-signed certificate with a known key) and eVerifyNone_InsecureTestOnly (tests only).
CTlsStream is not for two threads at the same time. Different streams share nothing. CTrustStore accepts several threads verifying, but adding is not safe while there are readers.
X.509 and host name
- Strict DER: minimal length, no indefinite length, minimal INTEGER, BOOLEAN 00/FF, unused bits zeroed, encoded default value refused. A repeated extension, an empty SAN, an empty keyUsage and an inner algorithm different from the outer one are malformed.
- Keys: RSA from 2048 to 8192 bits, P-256 and P-384. Ed25519, P-521, RSA smaller than 2048 and compressed points do not load. A certificate signed with SHA-1 or MD5 loads (so that old roots can serve as anchors) and fails verification.
- Chain: search with backtracking (store first, then the ones sent), resolves cross-signing, ceiling of 64 verifications and 32 certificates. The anchor is trusted because it is in the store, but its validity is checked. A self-issued leaf that is in the store is valid on its own.
- Leaf: keyUsage, when present, needs digitalSignature. extKeyUsage, when present, needs serverAuth (or anyExtendedKeyUsage).
- Name (RFC 9525): with a SAN, only dNSName and iPAddress. With no SAN at all, the CN. Wildcard only as the entire leftmost label, with 3 labels or more. An IP literal only against iPAddress.
System roots (CTrustStore::LoadSystem)
| Platform | From where |
|---|---|
| Windows | CertOpenSystemStoreW( 0, L"ROOT" ), each X.509 certificate in the store |
| Linux | SSL_CERT_FILE, otherwise the known distribution files (/etc/ssl/certs/ca-certificates.crt and similar) |
| macOS | SSL_CERT_FILE, otherwise /etc/ssl/cert.pem |
Certificates that do not load (unsupported key) are skipped and counted in SkippedCount(). On WSL Ubuntu: 120 of 121 (the missing one has a P-521 key). SSL_CERT_DIR is not read.
PEM
PemDecodeAll reads all the blocks of a text (garbage between blocks ignored, CRLF accepted, a block with an RFC 1421 header refused). The P-256 private key comes in PKCS #8 (v1 and v2) or SEC 1, the curve must be secp256r1, and the embedded public key must match the derived one. An encrypted key is not read. The copies of the key are wiped with SecureZero.
---
The new cryptography primitives
TLS only uses primitives from the library itself. These were added for it, in the Ethereal::Crypto namespace:
| Primitive | File | Constant time |
|---|---|---|
SHA-384, CSha384Stream, CSha512Stream | sha.hpp | no secret on the path |
CHmacSha256/384/512, CHkdfSha256/384/512 | hmac.hpp | yes |
CAes (encryption only), CAesGcm (96-bit nonce, 16-byte tag) | aes.hpp | yes: bitsliced AES (aes_ct64, Boyar-Peralta S-box) and GHASH by masked product in the scalar path, AES-NI with PCLMULQDQ when available |
X25519 | x25519.hpp | yes (ladder with conditional swap) |
P256: key, ECDH, ECDSA (RFC 6979) | ecc.hpp | yes (Renes-Costello-Batina complete formulas, fixed window with a selection that reads every entry) |
P384: verify only | ecc.hpp | not needed (public data) |
CRsaPublicKey: PKCS #1 v1.5 and PSS, verify only | rsa.hpp | not needed (public data) |
ChaCha20-Poly1305 and Ed25519 already existed.
---
How this was verified
TLS and the primitives are our own, because the library has no external dependency. That goes against the rule of not implementing cryptography by hand, and so the verification went beyond the usual. The standard is the one used for Ed25519 (docs/LICENCIAMENTO.md): official vectors embedded in the test, each one checked beforehand against an oracle that does not share a line of code (openssl 3.5 and Python's cryptography 46), random cross-checks with the oracle in both directions, and tampered cases that must be refused.
Symmetric primitives (tests/crypto_sym.cpp, ctest crypto.sym):
- 47 official vectors, checked in Python before going in, 0 divergences: FIPS 180-4 (SHA-384/512, including the one with 1 million 'a'), RFC 4231 cases 1 to 7 on the three hashes, RFC 5869 cases 1 to 3, FIPS 197 and McGrew-Viega GCM 1 to 4 and 13 to 16.
- 200 checks in ctest: splits at every point of the streams, each tampered bit of text, tag, AAD and nonce refused without releasing plaintext, the two AES paths agreeing on 1803 cases.
- Oracle (
tests/crypto_sym_oracle.py): 127,502 checks, 0 divergences, including 20,000 GCM cases per path (scalar and AES-NI).
Public key (tests/crypto_pk.cpp, ctest crypto.pk):
- RFC 7748 5.2 and 6.1, RFC 6979 A.2.5 (P-256, signature byte for byte) and A.2.6 (P-384). Two values written from memory were wrong, and the oracle corrected them: the oracle's value went in, which is the RFC's. The X25519 1,000,000-iteration vector passed outside ctest (11.5 min).
- Refusals: point off the curve, infinity, r or s outside [1, n-1], 14 forms of invalid DER, a small or even RSA modulus, and the 7 low-order points of X25519.
- 109 checks in ctest. Oracle (
tests/crypto_pk_oracle.py): 59,001 checks, 0 divergences (X25519 10,000, ECDH 6,000, ECDSA P-256 17,000 in both directions and identical byte for byte to OpenSSL, P-384 8,000, RSA 18,000 with 15 keys from 2048 to 4096 bits).
X.509 (tests/tls_x509.cpp, ctest tls.x509):
- 357 checks: DER rules, dates, IP literal, 31 parser cases built by surgery on the DER, 51 chain cases, 26 name cases, PEM, private key,
LoadSystem, 4 threads, fuzz. - Against
openssl verify -x509_strict: 47 of 51 chains identical. The remaining 4 are a chosen policy (CN with an IP-only SAN, keyEncipherment alone, Ed25519). - Oracle (
tests/tls_x509_oracle.py, random chains with 29 failure classes): 23,643 checks. Status equal to the expected one in 6000 of 6000, leaf fields equal to those ofcryptographyin 5643 of 5643, accept/refuse equal to OpenSSL in 5842 of 6000 (the 158 differences are the three policy classes). - Fuzz: 20,000 mutations in ctest and 100,000 under ASan, with no failure, and no modified certificate verified.
TLS 1.3 (tests/tls13.cpp, ctest tls.tls13, and tests/tls_rfc8448_oracle.py):
- RFC 8448 section 3, written from memory and checked in Python: 31 checks, 0 divergences, from the entire key schedule to the records. Two values from memory were wrong and the oracle's went in. In ctest, our client runs the RFC trace and the Finished, the data and the close_notify come out identical byte for byte, and our server produces the trace's ServerHello.
- 162 checks: 3 suites x 2 groups, HelloRetryRequest, 1 MiB, KeyUpdate, close_notify, ALPN, SNI, pin, each x509 refusal with the right alert, and each alert the RFC requires (version, sentinel, duplicated or unrequested extension, oversized record, tampered record, wrong Finished, plus 16 server refusals).
- Fuzz in 5 modes (mutated flights, messages mutated before encryption, mutated data records): 85,000 cases in Release and 12,750 under ASan, with no crash.
- Interoperability (
tests/tls_interop.py, ctesttls.interop): 31 cases, 0 failures, againstopenssl s_server,openssl s_clientand Python'sssl, in both roles, with RSA 2048, P-256, P-384 certificates and a chain with an intermediate, and the refusals with the right status.
HTTP (tests/http_web.cpp and tests/http_interop.py): 106 checks and 24 interoperability cases. See the [http module](http.md) page.
ASan and TSan: all the targets above pass in the linux-asan and linux-tsan trees, with zero compiler warnings.
To redo it (WSL, from the root, with the tree already configured):
bash build/estilo/fase2/compila.sh linux-release ethereal_crypto_sym ethereal_crypto_pk \
ethereal_tls_x509 ethereal_tls13 ethereal_tls_interop_cli -- '^(crypto\.|tls\.)'
python3 tests/crypto_sym_oracle.py build/linux-release/bin/ethereal_crypto_sym_cli 20000
python3 tests/crypto_pk_oracle.py build/linux-release/bin/ethereal_crypto_pk_cli 2000
python3 tests/tls_x509_oracle.py 6000 2
python3 tests/tls_rfc8448_oracle.py tests/tls13.cpp
python3 tests/tls_interop.py --cli build/linux-release/bin/ethereal_tls_interop_cli---
C layer
Header ethereal/c/tls/tls.h (source src/c/tls/tls.cpp), 18 ETLC_Tls* functions. A session (ETLC_TlsClientCreate / ETLC_TlsServerCreate) holds a CTlsEngine and runs over one of two transports:
- a TCP socket from the network C layer (
ethereal/c/network/network.h), borrowed: the session changes its mode and timeouts and never closes it. - no socket: the caller hands in what arrived with
ETLC_TlsFeedand takes out what is outgoing withETLC_TlsTakeOutput, for any transport.
Each call with I/O receives uTimeoutMs: 0 does not wait and returns ETLC_ERR_WOULD_BLOCK, ETLC_TLS_WAIT_FOREVER waits with no limit, any other value limits the entire call. That is what CTlsStream does not have (it only blocks), and it is what lets a process without a thread run the client and the server at the same time. Configuration: roots in PEM or the system ones, host name, pin, ALPN, time for the dates. On the server, the chain and the P-256 key in PEM. The error comes out in ETLC_TlsGetErrorInfo (the code, the alert, the certificate status, the socket code) and in ETLC_TlsDescribe.
Left out: the test hooks, RequestKeyUpdate, the list of suites and groups (the C++ ones apply), the server chain in DER, and standalone X.509, DER and PEM.
Tests tls.cabi.c11 and tls.cabi.c17 (tests/tls_c_abi.c): in-memory sessions, three refusals (unknown root, wrong host, wrong pin), and sessions over TCP on 127.0.0.1 with a non-blocking handshake, 300,000 bytes without waiting, a deadline that expires, close_notify and a cut TCP connection.
---
Known limits
- The ones in the "Out" table above. The server only has a P-256 key and never requests a client certificate.
- Handshake message up to 256 KiB. No limit on how many KeyUpdate messages the other side can send.
- No CRL and no OCSP: a revoked certificate only stops being valid when it expires. No nameConstraints (a critical one is refused as an unknown extension) and no policy processing.
- The store searches by subject linearly (the system roots number about 120).
- Simple performance: generic P-256 with 32-bit limbs, no special reduction for the NIST prime. AES-GCM uses AES-NI when available (about 1.9 GB/s), otherwise the bitsliced scalar path (about 55 MB/s with AES-128).
- The constant time of integer multiplication holds on x86-64 and ARM64, not on Cortex-M3 class cores.
- On Windows the platform-specific code is the
LoadSystembranch withwincrypt.h. Built with MSVC with zero warnings since 2026-10-12, with interop against OpenSSL 31/31.