Agreeing a key in public, and the man in the middle

9 min 5 sections 4 practice questions 5 sources citedISC2 objective 3.7

Two computers that have never met can agree a secret key while an eavesdropper hears every word they exchange. The method is called Diffie-Hellman, and with public key cryptography it lets a slow method deliver the key that a fast method then uses. The same agreement has a gap: an attacker placed between the two sides can agree one key with each. No background beyond public key cryptography is assumed. Every secure web connection depends on closing that gap.

By the end of this unit you will be able to:

  • Explain, step by step, how two parties agree a secret key over an open network
  • Explain how a man-in-the-middle attack works and choose the controls that stop it
  • Tell apart symmetric, asymmetric and hybrid encryption by speed, key handling and use
  • Put the main steps of a protected web connection in order
  • Tell apart replay and downgrade attacks and name the defence for each

The free network in the coffee shop

An illustrative example

An accountant opens her laptop in a coffee shop and joins a network called "Coffee Guest". She signs in to her firm's online ledger. The browser shows a warning that the site's certificate is not trusted. The warning has appeared before, and she clicks to carry on.

The page looks normal and the padlock symbol is there. A week later the firm finds that someone else has signed in to the ledger with her password.

The connection was encrypted from end to end, so how could anyone have read her password?

Agreeing a secret in front of an eavesdropper

is fast, and it needs both sides to hold the same key first. Diffie-Hellman is a method by which two parties reach the same secret number by exchanging only public values. Nobody listening can work the number out.

Mixing paint

Two decorators agree in public on a starting colour: yellow. Each picks a secret colour and mixes it into a pot of yellow. They swap the mixed pots in full view.

Each now adds their own secret colour to the pot they received. Both pots end up holding yellow plus both secret colours: the same shade. An onlooker saw yellow and the two mixtures, and cannot separate mixed paint back into its parts.

  1. The two parties agree public values in the open. These are the yellow paint.
  2. Each party makes a private value and keeps it. From it, each works out a public value and sends it to the other.
  3. Each party combines its own private value with the public value it received.
  4. Both arrive at the same shared secret. An eavesdropper holds only the public values, and undoing the calculation is the discrete logarithm problem.

This is key agreement: both sides contribute, and the key is never sent at all. The shared secret is turned into a session key, a symmetric key used for one connection and then thrown away.

Diffie-Hellman agrees a key and does nothing else. It does not a message, sign one, or prove who is on the other end.

The man in the middle: two sessions, one victim

A man-in-the-middle attack, also called an on-path or adversary-in-the-middle attack, places the attacker between two parties so that each talks to the attacker while believing it talks to the other. The coffee shop network was run by the attacker, so every message passed through the attacker's laptop.

  1. The accountant's browser starts a key agreement with what it thinks is the ledger site. The attacker answers instead.
  2. The attacker agrees one session key with the browser and a second session key with the real site.
  3. Each message from the browser is with the first key, read, perhaps changed, and encrypted again with the second.
  4. Replies travel the same way back. Both connections are encrypted, and the attacker holds both keys.

A man in the middle holds two separate session keys

A man in the middle holds two separate session keysA browser, a certificate check, the attacker's laptop holding session key 1 and session key 2, and the real site, in a row. Each message is decrypted with key 1 and encrypted again with key 2.Certificatecheckfalse onefails hereBrowserthe victimReal sitethe genuineserverAttacker's laptopreads every message,then re-encrypts itSessionkey 1agreed withthe browserSessionkey 2agreed withthe real siteA man in the middle holds two separate session keysA browser, a certificate check, the attacker's laptop holding session key 1 and session key 2, and the real site, in a row. Each message is decrypted with key 1 and encrypted again with key 2.Certificatecheckfalse onefails hereBrowserthe victimReal sitethe genuineserverAttacker's laptopreads every message,then re-encrypts itSessionkey 1agreed withthe browserSessionkey 2agreed withthe real site
Read this figure as text

On a network the attacker runs, such as coffee shop Wi-Fi, the browser's key agreement is answered by the attacker. Both connections are encrypted, and the attacker holds both keys, so neither end can see the attack from the encryption alone.

  1. The browser starts a key agreement with what it believes is the real site. The attacker answers in the site's place and offers a false certificate for the site.
  2. The certificate check is where the attack fails: a browser that validates the certificate rejects it. Only a user who clicks past the warning lets the attack go on.
  3. Past the check, the browser agrees session key 1 with the attacker. Each message from the browser is decrypted with key 1 on the attacker's laptop, read and perhaps changed.
  4. The attacker has agreed session key 2 with the real site, which believes the attacker is the browser. Each message is encrypted again with key 2 and sent on, and replies travel back the same way.
  5. Unauthenticated key agreement, such as plain Diffie-Hellman, cannot detect this. The defences prove who holds the other end: certificate validation, mutual authentication, pinning and HSTS.

Unauthenticated key agreement cannot detect this, because the attacker follows the method correctly with each side. The defence is to prove who holds the other end before trusting the key.

Certificate validation
The server proves it owns the key in a certificate that a trusted authority has signed for that site's name. The browser rejects a certificate that fails, and the user does not override the warning.
Mutual authentication
Both sides prove their identity, the client as well as the server, for example each with its own certificate.
Pinning
An application remembers the key or certificate it expects from a site and refuses any other, even one a trusted authority has signed.
HTTP Strict Transport Security (HSTS)
A site tells browsers to use only protected connections to it for a set period. The browser then refuses to continue past a certificate error, and offers no button to click through. It works once the browser has visited the site before, or when the site is on the browser's built-in preload list.

Common misconception

“If the connection is encrypted, nobody in between can read it.”

Encryption protects data only from those who lack the key. A man in the middle holds the key of each session. Encryption without authentication of the other end does not stop the attack.

Hybrid encryption: the slow key protects the fast key

Public key operations are slow and suit small items. Symmetric encryption is fast and needs a shared key. Hybrid encryption combines them: a public key method delivers or agrees a symmetric session key, and the session key encrypts the data.

The classic form is the digital envelope. The sender makes a random session key, encrypts the message with it, and encrypts the session key with the recipient's public key. Only the recipient's private key can open the envelope and recover the session key. Secure email works this way. One party choosing the key and sending it protected is called key transport, in contrast to key agreement, where both contribute.

Symmetric, asymmetric and hybrid encryption compared
PointSymmetricAsymmetricHybrid
KeysOne shared keyA key pair for each partyA key pair to set up, then a session key
SpeedFastSlowFast for the data
Key deliveryNeeds a secret routePublic keys travel openlySolved by the public key step
Typical useFiles, disks, bulk trafficSmall items: keys, signaturesWeb connections, secure email

How a protected web connection puts the pieces together

Transport Layer Security (TLS) is the protocol that protects web traffic, shown by the padlock in a browser. Its current version, TLS 1.3, builds a connection in a fixed order.

  1. The browser says which methods it supports and sends its half of a fresh Diffie-Hellman agreement.
  2. The server picks the methods and sends its half. Both now compute the same shared secret, and everything after this point is encrypted.
  3. The server sends its and a signature, made with its private key, over every message exchanged so far.
  4. The browser checks the certificate and the signature. This is the step that defeats a man in the middle.
  5. Both sides send a check value over the whole exchange, then the data flows under symmetric keys in an authenticated mode.

The Diffie-Hellman values are an ephemeral key: made new for each connection and destroyed after it. This gives forward secrecy, also called (PFS): if the server's long-term private key is stolen later, recorded past sessions still cannot be decrypted, because their session keys never depended on it.

Older versions of TLS also allowed a digital envelope sent under the server's RSA public key. Stealing that one private key opened every recorded session, so TLS 1.3 removed the option.

Replay and downgrade: two more attacks on the exchange

In a replay attack the attacker records a valid message and sends it again later. The attacker need not read it. A recorded instruction to pay 500 pounds, sent ten times, pays 5,000.

The defences make every message unique, so that a copy is recognised and refused.

  • A : a random value the receiver issues, used once, which the reply must contain.
  • A timestamp: a message older than a few minutes is refused.
  • A sequence number: a message whose number has been seen before is refused.

In a downgrade attack a man in the middle removes the strong options from the opening messages. Both sides then fall back to an older, weaker version or method that the attacker can break. Stripping a site from a protected to an unprotected connection is one form.

Against a fall back to an older version or weaker method, the defences are to switch off old versions on servers and clients and to protect the opening messages with the final check values. TLS 1.3 also places a fixed marker in its reply when it is pushed to an older version, and the browser abandons the connection when it sees it.

Stripping has a different defence. HSTS makes the browser insist on a protected connection to the site, so it cannot be led to an unprotected one. HSTS does nothing to stop a protected connection falling back to an older version.

Worked example: The paint exchange with a man in the middle

Two decorators swap paint pots through a courier. The courier is dishonest and has a secret colour of his own.

  1. The first decorator sends yellow plus her secret colour. The courier keeps it and passes on yellow plus his own secret colour.
  2. The second decorator sends yellow plus his secret colour. The courier keeps that too and passes on yellow plus the courier's colour.
  3. The first decorator mixes in her secret and gets a shade she shares with the courier. The second does the same and gets a different shade, which he also shares with the courier.
  4. Each decorator believes the shade is shared with the other decorator. Neither can tell, because each pot looked like ordinary mixed paint.
  5. If each decorator had signed the pot's label, and each knew the other's signature, the courier's swapped pots would have been rejected.

Key agreement on its own produces two keys and one listener. Authenticating the exchanged values is what turns two sessions back into one.

A question, worked through

A security manager at a firm of accountants learns that a member of staff signed in to the online ledger over a coffee shop network after clicking past a certificate warning. Her password was later used by someone else. The ledger site already uses TLS 1.3 with Diffie-Hellman key agreement, and staff must be able to reach it from anywhere.

Which change would BEST have prevented the password from being captured?

  1. Turn on HSTS for the ledger site so browsers refuse to pass certificate errors
  2. Encrypt the ledger database on the server with AES and a 256-bit key
  3. Require Diffie-Hellman key agreement for every connection to the ledger
  4. Change the TLS session keys every few minutes during each connection
Decide on your answer, then open the reasoning
  1. The question asks for the control that stops a captured password. The clicked-through warning shows a man-in-the-middle attack with a false certificate.
  2. Rule out b. It protects data stored on the server. The password was taken on the way there.
  3. Rule out d. The attacker agreed both session keys, so changing them more often gives the attacker new keys each time.
  4. Between a and c: the site already uses Diffie-Hellman, and the attacker simply carried out the agreement with each side. Agreement without authentication is the weakness.
  5. Option a removes the step that let the attack succeed: a browser that refuses to continue past a certificate error never talks to the impostor.

Answer: A. Turn on HSTS for the ledger site so browsers refuse to pass certificate errors

Choosing between good answers

An attacker relays and alters messages between two parties who each think they talk to the other
it is a man-in-the-middle attack. Choose authentication of the ends, not stronger encryption.
An option offers Diffie-Hellman as the defence against a man in the middle
reject it. Diffie-Hellman without authentication is what the attack exploits.
A question asks what protects past sessions if a server's private key is later stolen
choose ephemeral keys, which give forward secrecy.
A captured message is sent again unchanged to repeat its effect
it is replay. Choose nonces, timestamps or sequence numbers.
Two parties end up using an old, weak version that both support but neither prefers
it is a downgrade attack. Choose switching off old versions.

Check your understanding

4 questions on this unit, easiest first. Confirm an answer to see the reasoning for it, then the right answer, the other options and the rule to reuse. Your answers are kept in this browser and feed readiness and review.

These questions are drafts that no named reviewer has approved yet.

Question 1 of 4

A network engineer is tracing how a browser opens a TLS 1.3 connection to a web server. Put the steps in the order they happen.

Put the steps in order with the buttons beside each one. The whole order has to be right.

  1. 1The server picks the methods and sends its half, and both compute the same shared secret
  2. 2Both sides send a check value over the whole exchange, and data flows under session keys
  3. 3The browser checks the certificate and the signature
  4. 4The browser lists the methods it supports and sends its half of a Diffie-Hellman agreement
  5. 5The server sends its certificate and a signature over the messages exchanged so far

What to remember

  • Diffie-Hellman lets two parties agree a secret key by exchanging only public values. It does not encrypt, sign or authenticate.
  • A man in the middle runs a separate key agreement with each side and relays between them. Both connections are encrypted and the attacker holds both keys.
  • The defences are authentication: certificate validation, mutual authentication, pinning and HSTS.
  • Hybrid encryption uses a public key method to deliver or agree a symmetric session key, which then encrypts the data. The digital envelope is the classic form.
  • Ephemeral keys give forward secrecy: stealing a long-term key later does not open recorded sessions.
  • Replay is answered by nonces, timestamps and sequence numbers. Downgrade is answered by switching off old versions.

Say it from memory

  1. How does a man-in-the-middle attack defeat an encrypted connection, and which controls stop it?

    The attacker sits between the two parties and runs a separate key agreement with each, so it holds both session keys and decrypts, reads and re-encrypts every message. It is also called on-path or adversary-in-the-middle. Encryption alone does not stop it. Authentication does: certificate validation with no clicking past warnings, mutual authentication, pinning and HSTS. Do not confuse it with meet-in-the-middle, an attack on double encryption.

    Read this part again

  2. What does Diffie-Hellman do, and what does it not do?

    It lets two parties reach the same secret number by exchanging only public values, which is key agreement: both contribute and the key is never sent. The shared secret becomes a session key. It does not encrypt a message, sign one, or prove who is on the other end.

    Read this part again

  3. What is a digital envelope, and how does key transport differ from key agreement?

    The sender makes a random session key, encrypts the message with it, and encrypts the session key with the recipient's public key; only the recipient's private key can open it. In key transport one party chooses the key and sends it protected. In key agreement both parties contribute.

    Read this part again

  4. What is forward secrecy, and why did TLS 1.3 remove the RSA digital envelope?

    Forward secrecy comes from ephemeral Diffie-Hellman keys, made new for each connection and destroyed after it. If the server's long-term private key is stolen later, recorded past sessions still cannot be decrypted. With the RSA envelope, stealing that one private key opened every recorded session.

    Read this part again

  5. What are a replay attack and a downgrade attack, and what defends against each?

    A replay attack records a valid message and sends it again later; a nonce, a timestamp or a sequence number makes each message unique so copies are refused. A downgrade attack strips the strong options so both sides fall back to a weaker version. Switch off old versions and protect the opening messages with the final check values; TLS 1.3 adds a marker that exposes the fall back. Against stripping a site to an unprotected connection, use HSTS.

    Read this part again

Sources

  • RFC 2631: Diffie-Hellman Key Agreement Method, IETFDiffie-Hellman as key agreement in which each party combines its own private key with the other's public key and both compute the same shared secret.
  • NIST SP 800-57 Part 1 Rev. 5: Recommendation for Key Management, NISTKey agreement, in which both parties contribute, against key transport, in which one party chooses the key and sends it protected, and ephemeral keys generated for each key establishment.
  • RFC 8446: The Transport Layer Security (TLS) Protocol Version 1.3, IETFThe TLS 1.3 handshake with (EC)DHE key exchange, the server's Certificate and CertificateVerify signature, the removal of static RSA and Diffie-Hellman suites so that key exchange gives forward secrecy, and the downgrade marker in the server's random value, which a TLS 1.3 client checks and aborts on (section 4.1.3).
  • RFC 6797: HTTP Strict Transport Security (HSTS), IETFHSTS: a site declares it is reached only over protected connections, and the browser then refuses to proceed past a certificate error with no option to click through; the policy protects only after a first visit unless the site is on a browser's preloaded list (sections 12.3 and 14.6). The browser turns insecure links to the site into protected ones (section 8.3); nothing in it governs which TLS version is used.
  • RFC 7469: Public Key Pinning Extension for HTTP, IETFPinning a host's key so that a later certificate chain without the pinned key is refused, reducing man-in-the-middle attacks through a compromised certificate authority.