About
StartTLS or Opportunistic TLS is a communication pattern that secures the traffic in the middle of a plaintext connection.
Usage
It's used in cases where the server supports both:
- and clear connections.
For instance, SMTP is not secured by default, which means that if you were to send an email over SMTP without StartTLS the email could be intercepted and easily interpreted.
Steps
StartTLS is composed of three steps:
- A Client sends a StartTLS request to a server
- A Server sends a StartTLS response to the client.
- Client begins SSL/TLS handshake.
Example
Example of SMTP scenario
- with the StartTLS command of SMTP
- where S is the server and C is the client
S: 220 mail.example.org ESMTP service ready # <-- The server presents itself
C: EHLO client.example.org # <-- The client presents itself
S: 250-mail.example.org offers a warm hug of welcome # <-- The server welcomes
S: 250 STARTTLS # <-- The server makes StartTls mandatory
C: STARTTLS # <-- The StartTls client request
S: 220 Go ahead # <-- The server is ready for the handshake
C: <starts TLS negotiation>
C & S: <negotiate a TLS session>
C & S: <check result of negotiation>
C: EHLO client.example.org # <-- The connection is secured
Implementation
If you implement a server, you need to:
- Create a new SSL/TLS Handler,
- Insert it into your network traffic handler, and
- Write a StartTLS response so that the client can begin the SSL handshake
The client-side implementation is much simpler.
- Write a StartTLS request,
- Wait for the StartTLS response,
- Create a new SSL/TLS Handler and Initiate SSL handshake.