Dissecting TLS Using Wireshark
In this post, we take an in-depth look at how to analyze a Transport Layer Security (TLS) handshake using a network protocol analyzer tool called Wireshark.
Join the DZone community and get the full member experience.Join For Free
the primary goal of the transport layer security protocol as defined in rfc 2246 (tls version 1.0) is “to provide privacy and data integrity between two communicating applications.” the tls protocol ensures this by encrypting data so that any third party is unable to intercept the communication; it also authenticates the peers to verify their identity. by providing a secure channel of communication between two peers, tls protocol protects the integrity of the message and ensures it is not being tampered.
tls and ssl are used interchangeably. tls evolved from ssl protocol (ssl 3.0) that is no longer considered secure; vulnerabilities such as the poodle attack has demonstrated this. tls has gone through two iterations, rfc 4346 (tls 1.1) and rfc 5246 (tls 1.2), with the latest update tls 1.3 being a working draft.
tls lies in between the application and the transport layer. it is designed to work on top of a reliable transport protocol such as tcp (but has been adapted to udp, as well) and is divided into two sub-layers:
tcp record protocol layer – this is the lower layer which lies on top of the tcp layer and is responsible for:
- fragmenting the message to be transmitted into manageable blocks.
- encrypting/decrypting the hashed data.
- compressing/decompressing the outgoing/incoming data.
- applying the message authentication code (mac), a hash to maintain the data integrity.
- transmitting the data from the upper application layer to the lower transport layer and vice versa.
the higher layer consists of the following sub-protocols:
- alert: this sub-protocol defines the alert levels and provides a description of the alerts. it is used to notify the peer of any error condition that has occurred.
- change cipher spec: it defines changes in ciphering strategies. the change cipher spec message, transmitted by both the client and the server, defines the re-negotiated cipher spec and keys that will be used for all the messages exchanged henceforth.
- application data: this protocol ensures that messages are fragmented, compressed, encrypted and transmitted in a secure manner.
- handshake: to communicate over a secure channel, two peers must agree on the cryptographic keys and encryption algorithms for that session. tls protocol describes the steps to authenticate the peers and set up a secure connection with defined parameters. the entire sequence which involves setting up the session identifier, tls protocol version, negotiating the cipher suite, certificate authentication of the peers and cryptographic key exchange between peers is called a tls handshake. the steps involved in the tls handshake are shown below:
analyzing tls handshake using wireshark
the below diagram is a snapshot of the tls handshake between a client and a server captured using the wireshark, a popular network protocol analyzer tool.
let’s analyze each step.
1. initial client to server communication
typically, the first message in the tls handshake is the client hello message which is sent by the client to initiate a session with the server.
the message contains:
- version: the tls protocol version number that the client wants to use for communication with the server. this is the highest version supported by the client.
- client random: a 32-byte pseudorandom number that is used to calculate the master secret (used in the creation of the encryption key).
- session identifier: a unique number used by the client to identify a session.
- cipher suite: the list of cipher suites supported by the client ordered by the client’s preference. the cipher suite consists of a key exchange algorithm, bulk encryption algorithm, mac algorithm and a pseudorandom function. an example of a single cipher suite (one of the 28 suites mentioned in the above diagram) is as follows:
where tls = protocol version rsa = key exchange algorithm determining the peer authentication 3des_ede_cbc = bulk encryption algorithm used for data encryption sha-1 = message authentication code which is a cryptographic hash.
2. server response to client
the server responds to the client with multiple messages:
the server hello contains the following information:
- server version : the highest tls protocol version supported by the server which is also supported by the client.
- server random : 32-byte pseudorandom number used to generate the master secret.
: unique number to identify the session for the corresponding connection with the client.
- if the session id in the client hello message is not empty, the server will find a match in the session cache. if a match is found and the server wants to use the same session state, it returns the same id as sent by the client. if the server doesn’t want to resume the same session, then a new id is generated. the server can also send an empty id, indicating the session cannot be resumed.
- cipher suite : the single strongest cipher suite that both the server and the client support. if there is no supporting cipher suite, then a handshake failure alert is created.
- compression method : the compression algorithm agreed by both the server and the client. this is optional.
the server sends the client a list of x.509 certificates to authenticate itself. the server’s certificate contains its public key. this certificate authentication is either done by a third party (certificate authority) that is trusted by the peers, the operating system, and the browser which contains the list of well-known certificate authorities or by manually importing certificates that the user trusts.
this message validates whether the server’s x.509 digital certificate is revoked or not, it is ascertained by contacting a designated ocsp (online certificate status protocol) server. the ocsp response, which is dated and signed, contains the certificate status. the client can ask the server to send the “certificate status” message which contains the ocsp response. this approach is known as ocsp stapling. the process saves bandwidth on constrained networks as it prevents ocsp servers from getting overwhelmed with too many client requests.
the client, to indicate that it wants a certificate status message, includes an extension of the type “status_request” in the client hello request.
server key exchange
the message is optional and sent when the public key present in the server’s certificate is not suitable for key exchange or if the cipher suite places a restriction requiring a temporary key. this key is used by the client to encrypt client key exchange later in the process.
client certificate request
this is optional and is sent when the server wants to authenticate the client, for, let's say, a website where the server needs to confirm the client’s identity before providing access to private information. the message contains the type of certificate required and the list of acceptable certificate authorities.
server hello done
this message indicates the server is done and is awaiting the client’s response.
3. client response to server
client key exchange
this message contains:
- the protocol version of the client which the server verifies if it matches with the original client hello message.
- pre-master secret – a random number generated by the client and encrypted with the server public key. this along with the client and server random number is used to create the master secret. if the server can decrypt the message using the private key and can create the master secret locally, then the client is assured that the server has authenticated itself.
change cipher spec
this message notifies the server that all the future messages will be encrypted using the algorithm and keys that were just negotiated.
finished (encrypted handshake)
the finished message is complicated as it is a hash of all the messages exchanged previously along with a label (“client finished”). this message indicates that the tls negotiation is completed for the client.
note: wireshark displays the finished message as encrypted handshake since, unlike the previous messages, this message has been encrypted with the just negotiated keys/algorithms.
4. server response to client
change cipher spec
the server informs the client that the messages will be encrypted with the existing algorithms and keys. the record layer now changes its state to use the symmetric key encryption.
like the client finished message but contains a different label (“server finished”). once the client successfully decrypts and validates the message, the server is successfully authenticated.
5. application data flow
once the entire tls handshake is successfully completed and the peers validated, the applications on the peers can begin communicating with each other.
the article gives a brief explanation of how the tls protocol works and the analysis of the tls handshake using a powerful tool like wireshark. one important thing to note is applications should not rely on tls to create the strongest secure connection between the peers as it is possible for a hacker to make the peers drop down to the least secure connection. also, the use of tsl/ssl could impact performance. hence, a clear understanding of the protocol will help evaluate its advantages and vulnerabilities.
Published at DZone with permission of Vidhatha Vivekananda, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.