A familiar OpenSSL style interface for secure connections, on top of Salam's own TLS.
import ssl
This package gives an OpenSSL style context and connection interface (SSL_CTX, SSL_connect, SSL_read and friends, under Salam names), but underneath it is Salam's own tls and it needs no outside library. Use it when you port C code written against OpenSSL, or when you like that split between a shared context and many connections. For new code tls is shorter, and for web requests net.http does all of it for you.
The flow: make one context, hand each TCP socket to it with Wrap, for a client set the hostname and call Connect (a server calls Accept), then Read and Write. Things to know:
•
A client context checks certificates against the system roots from the start, with TLS 1.2 as the minimum. LoadVerifyLocationsFile replaces the system roots with your file; SetDefaultVerifyPaths switches back.
•
SetHostname is required before Connect while verification is on; without it the handshake is refused.
•
Shutdown and Close do not close the socket. As in OpenSSL, the TCP connection you wrapped is yours; close it with net.tcp afterwards.
•
SetMinVersion(ctx, TLS1_3) is checked after the handshake: a server that only offers 1.2 is connected and then refused. Read returns a str, which stops at a zero byte.
A complete client
The steps are always the same: one context for the whole program, then for each connection a TCP socket, Wrap, SetHostname and Connect. A context made with NewClientContext already trusts the system roots and stops the handshake when the check fails; the calls to SetDefaultVerifyPaths and SetVerify(ctx, VERIFY_PEER) in the program only make that explicit, the way OpenSSL code usually reads. Read returns an empty string when the server closes the connection, which is the end of a Connection: close response.
It needs network access, so it is only checked here. Run against the real hosts, it printed:
HTTP/1.1 200 OK
handshake failed: certificate verification failed: certificate expired
Trusting only your own certificate authority
Services inside a company often use certificates from a private CA. Load that CA with LoadVerifyLocationsFile instead of the system roots: the client then accepts only servers whose certificate your CA signed, which is stricter than the public web. SetMinVersion(ctx, TLS1_3) refuses anything older than TLS 1.3, and after the handshake PeerVerified and VerifyResult tell you how the check went.
VerifyResult is CERT_OK (zero) when the certificate was accepted. Keep the CA file readable by the program but not writable, since anyone who can replace it can impersonate your servers.
STARTTLS: encrypt a connection that is already open
Mail, IMAP, XMPP and some database protocols start in plain text and switch to TLS on the same connection when the client asks. This is why Wrap takes a socket descriptor rather than a host: talk to the server with net.tcp until it agrees, then wrap the very same fd and carry on through ssl.Read and ssl.Write.
Against Gmail's submission port it printed the server's greeting in plain text and then its first encrypted reply; the ids at the end of each line change with every run:
plain: 220 smtp.gmail.com ESMTP ... - gsmtp
encrypted: 250-smtp.gmail.com at your service, [...]
A real mail client should check every reply code, not only the one for STARTTLS, and should send EHLO again after the switch, as the program does, because the server forgets everything it was told in plain text.
When something fails
There are three places to look. LastErrorString describes the last failure that has no connection yet, such as a context that could not read its certificate. ConnErrorString describes what went wrong on one connection, for example a failed certificate check. And when Read or Write returns zero or less, ErrorCode(conn, n) tells a clean close by the other side apart from a protocol or socket error, the same split OpenSSL's SSL_get_error makes.