Sub topic 7
Plain-text protocols and their TLS variants
This chapter assumes you know what a port is, how TCP and UDP differ, and how to read a short Python script.
Why many protocols have two ports
Early protocols such as HTTP, SMTP and POP3 send commands and data as readable text. Anyone on the network path can read passwords and messages. Transport Layer Security (TLS) is a protocol that encrypts a TCP connection and lets the client check the server's identity. Adding TLS does not change the application protocol. It wraps the same bytes in an encrypted layer.
Two ways to add TLS
- Implicit TLS: The server listens on a separate port, and the TLS handshake happens immediately after the TCP connection opens. HTTPS on 443 works this way.
- STARTTLS: The client connects to the plain-text port, then sends a command asking to upgrade the same connection to TLS. SMTP submission on 587 works this way.
Common pairs
| Plain-text protocol | Port | Secure variant | Port | Method |
|---|---|---|---|---|
| HTTP | 80 | HTTPS | 443 | Implicit |
| IMAP | 143 | IMAPS | 993 | Implicit (STARTTLS also on 143) |
| POP3 | 110 | POP3S | 995 | Implicit (STARTTLS also on 110) |
| SMTP submission | 587 | SMTPS | 465 | Implicit on 465, STARTTLS on 587 |
| LDAP | 389 | LDAPS | 636 | Implicit (STARTTLS also on 389) |
| FTP | 21 | FTPS | 990 | Implicit (explicit mode uses 21) |
| DNS | 53 | DNS over TLS | 853 | Implicit |
| Telnet | 23 | SSH | 22 | Different protocol, not TLS |
SMTP on port 25 carries mail between servers. Servers often use STARTTLS there when both sides support it, but the upgrade is optional.
Seeing TLS from Python
This script opens a connection to port 443, completes the TLS handshake, and prints the negotiated version and the certificate subject.
import socket
import ssl
host = "example.com"
context = ssl.create_default_context()
with socket.create_connection((host, 443), timeout=5) as raw:
with context.wrap_socket(raw, server_hostname=host) as tls:
print("version:", tls.version())
print("subject:", tls.getpeercert()["subject"])create_default_context() turns on certificate and hostname checks. server_hostname is needed for both the hostname check and the server name indication (SNI), which tells the server which certificate to present.
Common errors
- Wrong mode for the port. Sending a TLS handshake to port 80, or plain HTTP to port 443, fails with a protocol error or a garbled response.
- Mixing up 465 and 587. Mail clients must use implicit TLS on 465 and STARTTLS on 587. A mismatch causes a hang or a handshake failure.
- Downgrade risk with STARTTLS. An attacker who strips the
STARTTLSoffer can force plain text unless the client requires the upgrade. - Disabling certificate checks. Setting verification off to silence an error removes the identity protection that TLS provides.
Exercise
A mail client must send outgoing mail through a provider that supports implicit TLS. Which port should you choose, and what error would you likely see if you configured STARTTLS mode on that same port?
Show answer
Use port 465. A client in STARTTLS mode waits for a plain-text greeting, but the server expects a TLS handshake first, so the connection hangs or fails with a handshake or protocol error.