DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Establishing a TLS Connection, Part 4

Establishing a TLS Connection, Part 4

In order to better understand TLS protocol, in this article, we are going to explore how you go about establishing a TLS connection.

Agathoklis Prodromou user avatar by
Agathoklis Prodromou
·
May. 04, 17 · Tutorial
Like (6)
Save
Tweet
Share
5.09K Views

Join the DZone community and get the full member experience.

Join For Free

In Parts 1, 2, and 3 of this series we looked at What is TLS/SSL?, TLS/SSL terminology and basics and TLS/SSL Certificates. In this, the fourth, part in the series, we will be looking at establishing a TLS connection.

To get a better understanding of the TLS protocol, we will now see what exactly takes place for a secure connection to be established.

The most important process of the connection establishment is the so-called “Handshake”. During the Handshake, the server and client will exchange important information regarding the properties under which the connection will be established.

During the Handshake, we will see how a hybrid of asymmetric and symmetric encryption is used in order to ensure security.

For this article, we will refer to a browser as the Client. In the following example, Diffie-Hellman is used for the key exchange.

Client Hello

A standard negotiation between client-server (the handshake) starts off with a Client Hello message.

The client sends a Client Hello to the server and includes the following.

Client hello

client_version

The client will send a list of all the SSL/TLS protocol versions it supports, with the preferred one being first on the list. The preferred one is usually and should be, the latest available version.

random

A 32-byte piece of random data of which 4 bytes represent the client’s current datetime (in epoch format) and the remaining 28 bytes, a randomly generated number. The Client’s random and the Server’s random will later be used to generate the key with which the data will be encrypted.

session_id

This is the session id which will be used for the connection. If the session_id is not empty, the server will search for previously cached sessions and resume that session if a match is found.

compression_methods

This is the method that is going to be used for compressing the SSL packets. By using compression, we can achieve lower bandwidth usage and therefore, faster transfer speeds. Later on this article, we will see why using compression is risky.

cipher_suites

Cipher suites are a combination of cryptographic algorithms which are used to define the overall security of the connection to be established. Typically, each cipher suite contains one cryptographic algorithm for each of the following: Key Exchange, Authentication, Bulk (data) Encryption, and Message Authentication.

The following is a sample cipher suit:
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256

Let’s break this down so that it makes more sense.

  • TLS is the protocol being used.
  • ECDHE is the key exchange algorithm (Elliptic curve Diffie–Hellman).
  • ECDSA is the authentication algorithm (Elliptic Curve Digital Signature Algorithm).
  • AES_128_GCM is the data encryption algorithm (Advanced Encryption Standard 128 bit Galois/Counter Mode).
  • SHA256 is the Message Authentication Code (MAC) algorithm (Secure Hash Algorithm 256 bit).

Here’s what an actual Client Hello looks like in a Wireshark capture:

Wireshark capture

The client can request additional functionality for the connection and this can be done via “extensions.” If the server cannot provide the additional functionality, then the client can abort the handshake if needed.

Server Hello

Server Hello

After the server receives the Client Hello, the second step will be to reply with a Server Hello. If it finds an acceptable set of algorithms in the Client’s request, it will respond by accepting those options and provide its certificate. If the server does not meet the requirements of the Client it will respond with a handshake failure message.

The server sends a Server Hello to the client and includes the following.

server_version

The server will (not blindly) select the Client’s preferred version of SSL/TLS protocol.

random

32 bytes of random data, out of which 4 bytes represent the server’s current datetime (in epoch format), and the rest of the 28 bytes are a randomly generated number that will be sent to the client. The Server’s random and the Client’s random will, later on, be used to generate the key with which the data will be encrypted.

session_id

This is the session id which will be used for the connection. If the session_id sent by the Client is not empty, the server will search for previously cached sessions and if a match is found, that session id will be used, thus resuming. If the session_id is empty, a new session will be created for the connection.

compression_methods

If supported, the server will agree on the Client’s preferred compression method.

cipher_suites

If supported, the server will agree on the Client’s preferred cipher suite.

certificate

The signed certificate of the server which proves it's identity to the client. It also contains the public key of the server.

Client Certificate (Optional)

Not often, the server may require the client to be authenticated with a Client Certificate as well. In cases like this, the client will provide its signed certificate to the server.

Server Key Exchange

The server key exchange message is sent only if the certificate provided by the server is not sufficient to allow the client to exchange a pre-master secret. (This is true for DHE_DSS, DHE_RSA, and DH_anon)

Server Hello Done

This is sent to the client as a confirmation that the Server Hello message is completed.

The following is what a Server Hello looks like in a Wireshark capture:

server hello wireshark capture

SSL TLS

Client Key Exchange

Key Exchange

The Client Key Exchange message is sent right after the Server Hello Done is received from the server. If the server requests a Client Certificate, the Client Key Exchange will be sent after that.

During this stage, the client will create a pre-master key.

Pre-Master Secret

The pre-master secret is created by the client (the method of creation depends on the cipher suite that will be used) and then it is shared with the server.

Before sending the pre-master secret to the server, the client encrypts it using the server’s public key which was extracted from the certificate provided by the server. This means that only the server can decrypt the message since asymmetric encryption is being used for the pre-master secret exchange.

The following is what the key-exchange looks like in a Wireshark capture (using DH):

key exchange

Master Secret

After the server receives the pre-master secret key, it uses its private key to decrypt it. Now both client and server will compute the master-secret key based on the random values exchanged earlier (ClientRandom and ServerRandom) using a Pseudorandom Function (PRF). A PRF is a function used to generate arbitrary amounts of pseudorandom data.

master_secret = PRF(pre_master_secret, "master secret", ClientHello.random + ServerHello.random) [0..47];

The master-secret key, which is 48 bytes in length, will then be used by both client and server to symmetrically encrypt the data for the rest of the communication.

Each of the parties (Client and Server), will create 3 sets of keys.

  • client_write_MAC_key – Authentication and Integrity check.
  • server_write_MAC_key – Authentication and Integrity check.
  • client_write_key – Message encryption using a symmetric key.
  • server_write_key – Message encryption using a symmetric key.
  • client_write_IV – Initialization Vector used by some AHEAD ciphers.
  • server_write_IV – Initialization Vector used by some AHEAD ciphers.

Both the Client and the Server will use the master secret to generate the sessions keys which will be able to encrypt/decrypt the data.

Change Cipher-Spec

Change Cypher Spec

Change Cypher Spec

At this point, both the Client and the Server are ready to switch to a secure, encrypted environment. The Change Cipher Spec protocol is used to change the encryption being used by the client and server. Any data being exchanged between the two parties will now be encrypted with the symmetrical shared key they have.

This is what the Change Cipher Spec looks like in a Wireshark capture:

 Change Cipher Spec Protocol

Finished

The last message of the handshake process and first encrypted one in the secure connection is Finished, which is exchanged by both parties.

Application Data

Application Data

To recap, the following illustrates a typical handshake.

Image title


TLS Connection (dance) Key exchange Data (computing) Cipher Session (web analytics) Algorithm

Published at DZone with permission of Agathoklis Prodromou, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Boot vs Eclipse Micro Profile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • Unlocking the Power of Elasticsearch: A Comprehensive Guide to Complex Search Use Cases
  • Multi-Cloud Integration
  • Create a REST API in C# Using ChatGPT

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: