Sub topic 7
Plain-text protocols and their TLS variants
This chapter assumes you know what a port is, how TCP carries bytes between two sockets, and the default ports for web, email and directory protocols from earlier chapters. It also assumes you can read a short Python script and run commands in a terminal.
Why plain-text protocols need a secure variant
Many classic protocols were designed when the network was a trusted place. HTTP, SMTP, POP3, IMAP, FTP, Telnet and LDAP all send commands and data as readable bytes. Passwords, session cookies and message bodies cross every router in the path unprotected. Anyone who can capture the traffic can read it, and anyone who can sit in the path can alter it.
Transport Layer Security (TLS) fixes this by wrapping a TCP connection in an encrypted, authenticated channel. TLS provides three things:
- Confidentiality: observers see only ciphertext.
- Integrity: tampering with the stream is detected.
- Server authentication: a certificate proves the server is the host you meant to reach.
TLS does not change the application protocol. The same commands run inside the tunnel. Only the way the connection starts changes.
Two ways to add TLS
Protocols adopted TLS in two different ways. You need to tell them apart because they use different ports and fail in different ways.
Implicit TLS uses a dedicated port. The client opens TCP and starts the TLS handshake immediately, before any application bytes. HTTPS on 443 works this way. If the client sends plain text to an implicit-TLS port, the server rejects it, because it expects a TLS handshake.
Explicit TLS (STARTTLS) keeps one port. The connection starts in plain text. The client asks to upgrade, the server agrees, and both sides run a TLS handshake on the same TCP connection. SMTP on port 587 and IMAP on port 143 can work this way.
Port pairs to know
| Protocol | Plain-text port | Secure form | Secure port | TLS style |
|---|---|---|---|---|
| HTTP | 80/tcp | HTTPS | 443/tcp | Implicit |
| SMTP (server to server) | 25/tcp | SMTP with STARTTLS | 25/tcp | Explicit |
| SMTP (mail submission) | 587/tcp | Submission with STARTTLS | 587/tcp | Explicit |
| SMTP | n/a | SMTPS | 465/tcp | Implicit |
| IMAP | 143/tcp | IMAPS | 993/tcp | Implicit |
| POP3 | 110/tcp | POP3S | 995/tcp | Implicit |
| LDAP | 389/tcp | LDAPS | 636/tcp | Implicit |
| FTP control | 21/tcp | FTPS (implicit) | 990/tcp | Implicit |
| Syslog | 514/udp | Syslog over TLS | 6514/tcp | Implicit |
| DNS | 53/udp and tcp | DNS over TLS | 853/tcp | Implicit |
| DNS | 53/udp and tcp | DNS over HTTPS | 443/tcp | Inside HTTPS |
Some entries need extra explanation:
- IMAP and POP3 can also use STARTTLS on 143 and 110. Many providers now prefer the implicit ports 993 and 995, because there is no plain-text phase to attack.
- LDAP supports both models: LDAPS on 636, or the StartTLS operation on 389.
- FTPS is FTP wrapped in TLS. It is not SFTP, which is a different protocol running inside SSH.
- DNS over HTTPS shares port 443 with ordinary web traffic. That is a design choice, and it makes DoH hard to tell apart from web browsing by port alone.
Not every secure replacement is TLS
Some secure protocols replaced the plain-text one entirely instead of wrapping it:
- SSH (22/tcp) replaced Telnet (23/tcp). SSH has its own handshake and key exchange, not TLS.
- SFTP runs over SSH on port 22. It shares nothing with FTP except the name.
- HTTPS is HTTP inside TLS, so it does count as a TLS variant.
When someone says "secure FTP", ask which one they mean. FTPS and SFTP use different ports, different clients and different firewall rules.
Seeing the difference in code
This script performs an implicit TLS connection to port 443. It prints the negotiated TLS version, the cipher and the certificate details.
import socket
import ssl
host = "example.com"
ctx = ssl.create_default_context()
with socket.create_connection((host, 443), timeout=5) as raw:
with ctx.wrap_socket(raw, server_hostname=host) as tls:
print("version:", tls.version())
print("cipher :", tls.cipher()[0])
cert = tls.getpeercert()
print("subject:", dict(item[0] for item in cert["subject"]))
print("expires:", cert["notAfter"])create_default_context() turns on certificate verification and hostname checking. server_hostname serves two purposes. It checks the certificate against the name, and it is sent as the Server Name Indication (SNI) value, which lets one IP address and port host many certificates.
Next is an explicit TLS (STARTTLS) example against a mail submission server. Replace mail.example.net with a server you are allowed to use. The script does not log in or send mail.
import smtplib
import ssl
host = "mail.example.net"
ctx = ssl.create_default_context()
with smtplib.SMTP(host, 587, timeout=10) as smtp:
print("greeting:", smtp.ehlo()[1].decode().splitlines()[0])
print("offers STARTTLS:", smtp.has_extn("starttls"))
smtp.starttls(context=ctx)
smtp.ehlo()
print("TLS version:", smtp.sock.version())The ehlo() call before starttls() is required. The server advertises its capabilities, including STARTTLS, in that reply. The second ehlo() is also required. After the upgrade the client must discard what it learned in plain text, because an attacker could have altered it.
To inspect either style from a shell, openssl s_client handles both:
# Implicit TLS: handshake starts immediately
openssl s_client -connect example.com:443 -servername example.com </dev/null
# Explicit TLS: speak SMTP, then upgrade
openssl s_client -connect mail.example.net:587 -starttls smtp </dev/nullCommon errors and what they mean
- Plain text sent to a TLS port. A browser pointed at
http://host:443gets an error such as "400 Bad Request: plain HTTP request sent to HTTPS port", or the connection is simply closed. - TLS handshake sent to a plain port. A client that expects TLS on 143 or 587 will see a readable greeting such as
* OK IMAP readywhere it expected handshake bytes. The error is usually "wrong version number". - Certificate name mismatch. You connected by IP address or an alias that is not in the certificate. Verification fails even though the encryption itself would work.
- Expired or untrusted certificate. The handshake completes only if the chain validates against your trust store. Disabling verification to "make it work" removes the authentication that TLS exists to provide.
Operational guidance
- Match the client setting to the server. Choose "SSL/TLS" for implicit ports (465, 993, 995, 636) and "STARTTLS" for upgrade ports (587, 143, 110, 389).
- Do not run plain and secure ports side by side without a reason. Leaving 143 and 110 open next to 993 and 995 invites clients to use the weaker option. Disable the plain ports or require STARTTLS on them.
- Use port 25 for server-to-server mail only. Users' mail clients should submit through 587 or 465 with authentication.
- Redirect HTTP to HTTPS rather than closing port 80, so that users who type a bare hostname still reach your site.
- Write firewall rules per port pair. Allowing 993 does not allow 143. Each is a separate rule, and each needs its own justification.
Exercise
A mail client is configured for server mail.example.net, port 993, with the security setting "STARTTLS". Connections fail with "wrong version number" or a hung handshake. What is wrong, and what are two valid corrected configurations?
Show answer
Port 993 is IMAPS, which uses implicit TLS, so the server expects a TLS handshake as soon as the connection opens. The client instead opens a plain-text session and sends an IMAP command or a STARTTLS request, which the server cannot parse. Fix 1: keep port 993 and change the security setting to "SSL/TLS" (implicit). Fix 2: change the port to 143 and keep "STARTTLS" (explicit), which upgrades the plain-text connection after the greeting.
Exercise
Why does a firewall rule that allows only 443/tcp not tell you whether the traffic inside is ordinary web browsing, an API, or DNS over HTTPS?
Show answer
HTTPS, DNS over HTTPS and many API protocols all use implicit TLS on 443, and the port number alone does not identify the application protocol. The payload is encrypted after the handshake, so a port-based rule cannot separate them. Telling them apart needs other signals, such as the SNI hostname, destination IP address or application-layer inspection.