In order to use the various cryptographic algorithms, need to have or be able to negotiate the right keys. This means knowing the identity of the parties adn performing a suitable key distribution. Have to be careful that the protocol is not subject to a "replay" attack, whereby the exchange is recorded by an attacker, and then subsequently reused to fool the recipient into believing they're communicating with someone else.
A commonly used technique in network protocols is to include a sequence number on each message. This works fine in limited time periods for a limited number of conversations, but doesn't scale well to long period many party use.
The "obvious" way to overcome replays is to include the "time" in the message. Unfortunately time is a fluid value, and not all computer clocks are the same. Which means that some way of keeping clocks in sync is needed, and even then there must be a small window to allow for drift. So its not a perfect solution.
This is the core of many authentication protocols, its use convinces one party that the other must have the same secret info (key) it knows, without having to actually send that key, which could be snooped. If repeated the other way, both parties can be convinced that the other knows the info.
The original, basic key exchange protocol. Used by 2 parties who both trusted a common key server, it gives one party the info needed to establish a session key with the other. Note that since the key server chooses the session key, it is capable of reading/forging any messages between A&B, which is why they need to trust it absolutely!
Message 1 A -> S A, B, Na
Message 2 S -> A EKas{Na , B, Kab, EKbs{Kab, A} }
Message 3 A -> B EKbs{Kab, A}
Message 4 B -> A EKab{Nb}
Message 5 A-> B EKab{Nb-1}
nb: Na is a random value chosen by Alice,
Nb random chosen by Bob
Note that all communications is between A&S and A&B, B&S don't talk directly (though indirectly a message passes from S via A to B, encrypted in B's key so that A is unable to read or alter it). Other variations of key distribution protocols can involve direct communications between B&S.
There is a critical flaw in the protocol, as shown. This emphasises the need to be extremely careful in codifying assumptions, and tracking the timeliness of the flow of info in protocols. Designing secure protocols is not easy, and should not be done lightly. Great care and analysis is needed.
Here use a trusted intermediary, whom all parties trust, to mediate the establishment of secure communications between them. Must trust intermediary not to abuse the knowledge of all session keys.
One of the best known and most widely implemented trusted third party key distribution systems. Still mostly limited to use within a single organisation though.