(* 
Needham-Schroeder public key protocol
Corrected version by Lowe
Proof that if the responder terminates, then the
whole protocol has run as expected.
*)

free c: channel.

type host.
type nonce.
type pkey.
type skey.
type spkey.
type sskey.

fun nonce_to_bitstring(nonce): bitstring [data,typeConverter].

(* Public key encryption *)

fun pk(skey): pkey.
fun encrypt(bitstring, pkey): bitstring.
reduc forall x: bitstring, y: skey; decrypt(encrypt(x,pk(y)),y) = x.

(* Signatures *)

fun spk(sskey): spkey.
fun sign(bitstring, sskey): bitstring.
reduc forall m: bitstring, k: sskey; getmess(sign(m,k)) = m.
reduc forall m: bitstring, k: sskey; checksign(sign(m,k), spk(k)) = m.

(* Shared key encryption *)

fun sencrypt(bitstring,nonce): bitstring.
reduc forall x: bitstring, y: nonce; sdecrypt(sencrypt(x,y),y) = x.

(* Secrecy assumptions *)

not attacker(new skA).
not attacker(new skB).
not attacker(new skS).

(* 2 honest host names A and B *)

free A, B: host.

(* the table host names/keys 
   The key table consists of pairs (host, public key) *)
table keys(host, pkey).

(* Queries *)
event endB(host, host, pkey, pkey, nonce, nonce).
event e3(host, host, pkey, pkey, nonce, nonce).
event e2(host, host, pkey, pkey, nonce, nonce).
event e1(host, host, pkey, pkey, nonce).

query y: host, pkx: pkey, pky: pkey, nx: nonce, ny: nonce; 
      inj-event(endB(A, y, pkx, pky, nx, ny)) ==> 
      (inj-event(e3(A, y, pkx, pky, nx, ny)) ==>
      (inj-event(e2(A, y, pkx, pky, nx, ny)) ==>
       inj-event(e1(A, y, pkx, pky, nx)))).

(* Role of the initiator with identity xA and secret key skxA *)
let processInitiator(pkS: spkey, skA: skey, skB: skey) =
	(* The attacker starts the initiator by choosing identity xA,
	   and its interlocutor xB0.
	   We check that xA is honest (i.e. is A or B)
	   and get its corresponding key.
	*)
	in(c, (xA: host, hostX: host));
	if xA = A || xA = B then
	let skxA = if xA = A then skA else skB in
	let pkxA = pk(skxA) in
	(* Real start of the role *)
	(* Message 1: Get the public key certificate for the other host *)
	out(c, (xA, hostX));
	(* Message 2 *)
	in(c, ms: bitstring); 
	let (pkX: pkey, =hostX) = checksign(ms,pkS) in
	(* Message 3 *)
	new Na: nonce;
	event e1(xA, hostX, pkxA, pkX, Na);
	out(c, encrypt((Na, xA), pkX));
	(* Message 6 *)
	in(c, m: bitstring); 
	let (=Na, NX2: nonce, =hostX) = decrypt(m, skA) in
	let m7 = encrypt(nonce_to_bitstring(NX2), pkX) in
	event e3(xA, hostX, pkxA, pkX, Na, NX2);
	(* Message 7 *)
	out(c, m7).

(* Role of the responder with identity xB and secret key skxB *)
let processResponder(pkS: spkey, skA: skey, skB: skey) =
	(* The attacker starts the responder by choosing identity xB.
	   We check that xB is honest (i.e. is A or B). *)
	in(c, xB: host);
	if xB = A || xB = B then
	let skxB = if xB = A then skA else skB in
	let pkxB = pk(skxB) in
	(* Real start of the role *)
	(* Message 3 *)
	in(c, m: bitstring);
	let (NY: nonce, hostY: host) = decrypt(m, skxB) in
	(* Message 4: Get the public key certificate for the other host *)
	out(c, (xB, hostY));
	(* Message 5 *)
	in(c,ms: bitstring);
	let (pkY: pkey,=hostY) = checksign(ms,pkS) in
	(* Message 6 *)
	new Nb: nonce;
	event e2(hostY, xB, pkY, pkxB, NY, Nb);
	out(c, encrypt((NY, Nb, xB), pkY));
	(* Message 7 *)
	in(c, m3: bitstring);
	if nonce_to_bitstring(Nb) = decrypt(m3, skB) then
	event endB(hostY, xB, pkY, pkxB, NY, Nb).

(* Server *)
let processS(skS: sskey) =  
	in(c,(a: host, b: host)); 
	get keys(=b, sb) in
	out(c,sign((sb,b),skS)).

(* Key registration *)
let processK =
	in(c, (h: host, k: pkey));
	if h <> A && h <> B then insert keys(h,k).

(* Start process *)
process 
	new skA: skey; let pkA = pk(skA) in out(c, pkA); insert keys(A, pkA);
	new skB: skey; let pkB = pk(skB) in out(c, pkB); insert keys(B, pkB);
	new skS: sskey; let pkS = spk(skS) in out(c, pkS);
	(
	  (* Launch an unbounded number of sessions of the initiator *)
	  (!processInitiator(pkS, skA, skB)) | 
	  (* Launch an unbounded number of sessions of the responder *)
	  (!processResponder(pkS, skA, skB)) |
	  (* Launch an unbounded number of sessions of the server *)
	  (!processS(skS)) |
	  (* Key registration process *)
	  (!processK)
	)
