% $Id$\chapter{Case Study: Verifying a Security Protocol}\label{chap:crypto}\index{protocols!security|(}%crypto primitives \def\lbb{\mathopen{\{\kern-.30em|}}\def\rbb{\mathclose{|\kern-.32em\}}}\def\comp#1{\lbb#1\rbb}Communications security is an ancient art. Julius Caesar is said to haveencrypted his messages, shifting each letter three places along thealphabet. Mary Queen of Scots was convicted of treason after a cipher usedin her letters was broken. Today's postal systemincorporates security features. The envelope provides a degree of\emph{secrecy}. The signature provides \emph{authenticity} (proof oforigin), as do departmental stamps and letterheads.Networks are vulnerable: messages pass through many computers, any of whichmight be controlled by an adversary, who thus can capture or redirectmessages. People who wish to communicate securely over such a network canuse cryptography, but if they are to understand each other, they need tofollow a\emph{protocol}: a pre-arranged sequence of message formats. Protocols can be attacked in many ways, even if encryption is unbreakable. A \emph{splicing attack} involves an adversary's sending a message composedof parts of several old messages. This fake message may have the correctformat, fooling an honest party. The adversary might be able to masqueradeas somebody else, or he might obtain a secret key.\emph{Nonces} help prevent splicing attacks. A typical nonce is a 20-byterandom number. Each message that requires a reply incorporates a nonce. Thereply must include a copy of that nonce, to prove that it is not a replay ofa past message. The nonce in the reply must be cryptographicallyprotected, since otherwise an adversary could easily replace it by adifferent one. You should be starting to see that protocol design istricky!Researchers are developing methods for proving the correctness of securityprotocols. The Needham-Schroeder public-keyprotocol~\cite{needham-schroeder} has become a standard test case. Proposed in 1978, it was found to be defective nearly two decadeslater~\cite{lowe-fdr}. This toy protocol will be useful in demonstratinghow to verify protocols using Isabelle.\section{The Needham-Schroeder Public-Key Protocol}\label{sec:ns-protocol}\index{Needham-Schroeder protocol|(}%This protocol uses public-key cryptography. Each person has a private key, known only tohimself, and a public key, known to everybody. If Alice wants to send Bob a secret message, sheencrypts it using Bob's public key (which everybody knows), and sends it to Bob. Only Bob has thematching private key, which is needed in order to decrypt Alice's message.The core of the Needham-Schroeder protocol consists of three messages:\begin{alignat*}{2} &1.&\quad A\to B &: \comp{Na,A}\sb{Kb} \\ &2.&\quad B\to A &: \comp{Na,Nb}\sb{Ka} \\ &3.&\quad A\to B &: \comp{Nb}\sb{Kb}\end{alignat*}First, let's understand the notation. In the first message, Alicesends Bob a message consisting of a nonce generated by Alice~($Na$)paired with Alice's name~($A$) and encrypted using Bob's publickey~($Kb$). In the second message, Bob sends Alice a messageconsisting of $Na$ paired with a nonce generated by Bob~($Nb$), encrypted using Alice's public key~($Ka$). In the last message, Alicereturns $Nb$ to Bob, encrypted using his public key.When Alice receives Message~2, she knows that Bob has acted on hermessage, since only he could have decrypted$\comp{Na,A}\sb{Kb}$ and extracted~$Na$. That is precisely whatnonces are for. Similarly, message~3 assures Bob that Alice isactive. But the protocol was widely believed~\cite{ban89} to satisfy afurther property: that$Na$ and~$Nb$ were secrets shared by Alice and Bob. (Manyprotocols generate such shared secrets, which can be usedto lessen the reliance on slow public-key operations.) Lowe\index{Lowe, Gavin|(} found thisclaim to be false: if Alice runs the protocol with someone untrustworthy(Charlie say), then he can start a new run with another agent (Bob say). Charlie uses Alice as an oracle, masquerading asAlice to Bob~\cite{lowe-fdr}.\begin{alignat*}{4} &1.&\quad A\to C &: \comp{Na,A}\sb{Kc} && \qquad 1'.&\quad C\to B &: \comp{Na,A}\sb{Kb} \\ &2.&\quad B\to A &: \comp{Na,Nb}\sb{Ka} \\ &3.&\quad A\to C &: \comp{Nb}\sb{Kc} && \qquad 3'.&\quad C\to B &: \comp{Nb}\sb{Kb}\end{alignat*}In messages~1 and~3, Charlie removes the encryption using his privatekey and re-encrypts Alice's messages using Bob's public key. Bob isleft thinking he has run the protocol with Alice, which was notAlice's intention, and Bob is unaware that the ``secret'' nonces areknown to Charlie. This is a typical man-in-the-middle attack launchedby an insider.Whether this counts as an attack has been disputed. In protocols of thistype, we normally assume that the other party is honest. To be honestmeans to obey the protocol rules, so Alice's running the protocol withCharlie does not make her dishonest, just careless. After Lowe'sattack, Alice has no grounds for complaint: this protocol does not have toguarantee anything if you run it with a bad person. Bob does havegrounds for complaint, however: the protocol tells him that he iscommunicating with Alice (who is honest) but it does not guaranteesecrecy of the nonces.Lowe also suggested a correction, namely to include Bob's name inmessage~2:\begin{alignat*}{2} &1.&\quad A\to B &: \comp{Na,A}\sb{Kb} \\ &2.&\quad B\to A &: \comp{Na,Nb,B}\sb{Ka} \\ &3.&\quad A\to B &: \comp{Nb}\sb{Kb}\end{alignat*}If Charlie tries the same attack, Alice will receive the message$\comp{Na,Nb,B}\sb{Ka}$ when she was expecting to receive$\comp{Na,Nb,C}\sb{Ka}$. She will abandon the run, and eventually sowill Bob. Below, we shall look at parts of this protocol's correctnessproof. In ground-breaking work, Lowe~\cite{lowe-fdr}\index{Lowe, Gavin|)}showed how such attackscould be found automatically using a model checker. An alternative,which we shall examine below, is to prove protocols correct. Proofscan be done under more realistic assumptions because our model doesnot have to be finite. The strategy is to formalize the operationalsemantics of the system and to prove security properties using ruleinduction.%\index{Needham-Schroeder protocol|)}\input{Protocol/document/Message}\input{Protocol/document/Event}\input{Protocol/document/Public}\input{Protocol/document/NS_Public}