(*************************************************************
 *                                                           *
 *  Cryptographic protocol verifier                          *
 *                                                           *
 *  Bruno Blanchet, Vincent Cheval, and Marc Sylvestre       *
 *                                                           *
 *  Copyright (C) INRIA, CNRS 2000-2018                      *
 *                                                           *
 *************************************************************)

(*

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details (in file LICENSE).

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*)
(* Otway Rees protocol.
   Flawed version, quoted by Paulson TR443 Inductive... *)

free c.

(* Shared-key cryptography *)

fun encrypt/2.
reduc decrypt(encrypt(m,k),k) = m.

(* Host name / key *)

fun host/1.
private reduc getkey(host(x)) = x.

(* Secrecy assumptions *)

not kA.
not kB.

authquery Aparam/1.
authquery Akey/3.
authquery Bparam/1.
authquery Bkey/3.

data cons/2.
data signatureElement/1.

(* Clauses *)

pred member/2 memberOptim.
pred mypred/2.

clauses

member:x,cons(x,l);
member:x,l -> member:x,cons(y,l);

x = signatureElement(y) -> mypred:x,y.



private free secretA, secretB.
query secretA.
query secretB.

let processA = 
	in(c, hostB0);
	begin Bparam(hostB0);
	new Na;
        out(c, (Na, hostA, hostB0, encrypt((Na, hostA, hostB0), kA)));
	in(c, (=Na, m2));
	let (=Na, k) = decrypt(m2, kA) in
	let x suchthat member:x,k in
	begin Bkey(hostA, hostB0, k);
	(* OK *)
	if hostB0 = hostB then 
        end Aparam(hostA);
	end Akey(hostA, hostB0, k);
	out(c, encrypt(secretA, k)).

let processB = 
	in(c, (na, hostA1, =hostB, z));
	begin Aparam(hostA1);
	new Nb;
	out(c, (na, hostA1, hostB, z, Nb, encrypt((na, hostA1, hostB), kB)));
	in(c, (=na, m3, m4));
	let (=Nb, k) = decrypt(m4, kB) in
	begin Akey(hostA1, hostB, k);
	out(c, (na, m3));
	(* OK *)
	if hostA1 = hostA then
	end Bparam(hostB);
	end Bkey(hostA1, hostB, k);
	out(c, encrypt(secretB, k)).

let processS = in(c, (Na1, hostA1, hostB1, m2, Nb1, m3));
	       let (=Na1, =hostA1, =hostB1) = decrypt(m2, getkey(hostA1)) in
	       let (=Na1, =hostA1, =hostB1) = decrypt(m3, getkey(hostB1)) in
	       new k;
               out(c, (Na1, encrypt((Na1, k), getkey(hostA1)), 
                            encrypt((Nb1, k), getkey(hostB1)))).


process new kA; new kB;
	let hostA = host(kA) in 
	let hostB = host(kB) in
	out(c, hostA); out(c, hostB);
	((!processA) | (!processB) | (!processS))
