src/Doc/Tutorial/Protocol/NS_Public.thy
changeset 69505 cc2d676d5395
parent 67406 23307fd33906
child 69597 ff784d5a5bfb
equal deleted inserted replaced
69504:bda7527ccf05 69505:cc2d676d5395
    56   &3.&\quad  A\to B  &: \comp{Nb}\sb{Kb}
    56   &3.&\quad  A\to B  &: \comp{Nb}\sb{Kb}
    57 \end{alignat*%
    57 \end{alignat*%
    58 }
    58 }
    59 
    59 
    60 Each protocol step is specified by a rule of an inductive definition.  An
    60 Each protocol step is specified by a rule of an inductive definition.  An
    61 event trace has type @{text "event list"}, so we declare the constant
    61 event trace has type \<open>event list\<close>, so we declare the constant
    62 @{text ns_public} to be a set of such traces.
    62 \<open>ns_public\<close> to be a set of such traces.
    63 
    63 
    64 Figure~\ref{fig:ns_public} presents the inductive definition.  The
    64 Figure~\ref{fig:ns_public} presents the inductive definition.  The
    65 @{text Nil} rule introduces the empty trace.  The @{text Fake} rule models the
    65 \<open>Nil\<close> rule introduces the empty trace.  The \<open>Fake\<close> rule models the
    66 adversary's sending a message built from components taken from past
    66 adversary's sending a message built from components taken from past
    67 traffic, expressed using the functions @{text synth} and
    67 traffic, expressed using the functions \<open>synth\<close> and
    68 @{text analz}. 
    68 \<open>analz\<close>. 
    69 The next three rules model how honest agents would perform the three
    69 The next three rules model how honest agents would perform the three
    70 protocol steps.  
    70 protocol steps.  
    71 
    71 
    72 Here is a detailed explanation of rule @{text NS2}.
    72 Here is a detailed explanation of rule \<open>NS2\<close>.
    73 A trace containing an event of the form
    73 A trace containing an event of the form
    74 @{term [display,indent=5] "Says A' B (Crypt (pubK B) \<lbrace>Nonce NA, Agent A\<rbrace>)"}
    74 @{term [display,indent=5] "Says A' B (Crypt (pubK B) \<lbrace>Nonce NA, Agent A\<rbrace>)"}
    75 may be extended by an event of the form
    75 may be extended by an event of the form
    76 @{term [display,indent=5] "Says B A (Crypt (pubK A) \<lbrace>Nonce NA, Nonce NB, Agent B\<rbrace>)"}
    76 @{term [display,indent=5] "Says B A (Crypt (pubK A) \<lbrace>Nonce NA, Nonce NB, Agent B\<rbrace>)"}
    77 where @{text NB} is a fresh nonce: @{term "Nonce NB \<notin> used evs2"}.
    77 where \<open>NB\<close> is a fresh nonce: @{term "Nonce NB \<notin> used evs2"}.
    78 Writing the sender as @{text A'} indicates that @{text B} does not 
    78 Writing the sender as \<open>A'\<close> indicates that \<open>B\<close> does not 
    79 know who sent the message.  Calling the trace variable @{text evs2} rather
    79 know who sent the message.  Calling the trace variable \<open>evs2\<close> rather
    80 than simply @{text evs} helps us know where we are in a proof after many
    80 than simply \<open>evs\<close> helps us know where we are in a proof after many
    81 case-splits: every subgoal mentioning @{text evs2} involves message~2 of the
    81 case-splits: every subgoal mentioning \<open>evs2\<close> involves message~2 of the
    82 protocol.
    82 protocol.
    83 
    83 
    84 Benefits of this approach are simplicity and clarity.  The semantic model
    84 Benefits of this approach are simplicity and clarity.  The semantic model
    85 is set theory, proofs are by induction and the translation from the informal
    85 is set theory, proofs are by induction and the translation from the informal
    86 notation to the inductive rules is straightforward. 
    86 notation to the inductive rules is straightforward. 
   111 
   111 
   112 text \<open>
   112 text \<open>
   113 Secrecy properties can be hard to prove.  The conclusion of a typical
   113 Secrecy properties can be hard to prove.  The conclusion of a typical
   114 secrecy theorem is 
   114 secrecy theorem is 
   115 @{term "X \<notin> analz (knows Spy evs)"}.  The difficulty arises from
   115 @{term "X \<notin> analz (knows Spy evs)"}.  The difficulty arises from
   116 having to reason about @{text analz}, or less formally, showing that the spy
   116 having to reason about \<open>analz\<close>, or less formally, showing that the spy
   117 can never learn~@{text X}.  Much easier is to prove that @{text X} can never
   117 can never learn~\<open>X\<close>.  Much easier is to prove that \<open>X\<close> can never
   118 occur at all.  Such \emph{regularity} properties are typically expressed
   118 occur at all.  Such \emph{regularity} properties are typically expressed
   119 using @{text parts} rather than @{text analz}.
   119 using \<open>parts\<close> rather than \<open>analz\<close>.
   120 
   120 
   121 The following lemma states that @{text A}'s private key is potentially
   121 The following lemma states that \<open>A\<close>'s private key is potentially
   122 known to the spy if and only if @{text A} belongs to the set @{text bad} of
   122 known to the spy if and only if \<open>A\<close> belongs to the set \<open>bad\<close> of
   123 compromised agents.  The statement uses @{text parts}: the very presence of
   123 compromised agents.  The statement uses \<open>parts\<close>: the very presence of
   124 @{text A}'s private key in a message, whether protected by encryption or
   124 \<open>A\<close>'s private key in a message, whether protected by encryption or
   125 not, is enough to confirm that @{text A} is compromised.  The proof, like
   125 not, is enough to confirm that \<open>A\<close> is compromised.  The proof, like
   126 nearly all protocol proofs, is by induction over traces.
   126 nearly all protocol proofs, is by induction over traces.
   127 \<close>
   127 \<close>
   128 
   128 
   129 lemma Spy_see_priK [simp]:
   129 lemma Spy_see_priK [simp]:
   130       "evs \<in> ns_public
   130       "evs \<in> ns_public
   131        \<Longrightarrow> (Key (priK A) \<in> parts (knows Spy evs)) = (A \<in> bad)"
   131        \<Longrightarrow> (Key (priK A) \<in> parts (knows Spy evs)) = (A \<in> bad)"
   132 apply (erule ns_public.induct, simp_all)
   132 apply (erule ns_public.induct, simp_all)
   133 txt \<open>
   133 txt \<open>
   134 The induction yields five subgoals, one for each rule in the definition of
   134 The induction yields five subgoals, one for each rule in the definition of
   135 @{text ns_public}.  The idea is to prove that the protocol property holds initially
   135 \<open>ns_public\<close>.  The idea is to prove that the protocol property holds initially
   136 (rule @{text Nil}), is preserved by each of the legitimate protocol steps (rules
   136 (rule \<open>Nil\<close>), is preserved by each of the legitimate protocol steps (rules
   137 @{text NS1}--@{text 3}), and even is preserved in the face of anything the
   137 \<open>NS1\<close>--\<open>3\<close>), and even is preserved in the face of anything the
   138 spy can do (rule @{text Fake}).  
   138 spy can do (rule \<open>Fake\<close>).  
   139 
   139 
   140 The proof is trivial.  No legitimate protocol rule sends any keys
   140 The proof is trivial.  No legitimate protocol rule sends any keys
   141 at all, so only @{text Fake} is relevant. Indeed, simplification leaves
   141 at all, so only \<open>Fake\<close> is relevant. Indeed, simplification leaves
   142 only the @{text Fake} case, as indicated by the variable name @{text evsf}:
   142 only the \<open>Fake\<close> case, as indicated by the variable name \<open>evsf\<close>:
   143 @{subgoals[display,indent=0,margin=65]}
   143 @{subgoals[display,indent=0,margin=65]}
   144 \<close>
   144 \<close>
   145 by blast
   145 by blast
   146 (*<*)
   146 (*<*)
   147 lemma Spy_analz_priK [simp]:
   147 lemma Spy_analz_priK [simp]:
   148       "evs \<in> ns_public \<Longrightarrow> (Key (priK A) \<in> analz (knows Spy evs)) = (A \<in> bad)"
   148       "evs \<in> ns_public \<Longrightarrow> (Key (priK A) \<in> analz (knows Spy evs)) = (A \<in> bad)"
   149 by auto
   149 by auto
   150 (*>*)
   150 (*>*)
   151 
   151 
   152 text \<open>
   152 text \<open>
   153 The @{text Fake} case is proved automatically.  If
   153 The \<open>Fake\<close> case is proved automatically.  If
   154 @{term "priK A"} is in the extended trace then either (1) it was already in the
   154 @{term "priK A"} is in the extended trace then either (1) it was already in the
   155 original trace or (2) it was
   155 original trace or (2) it was
   156 generated by the spy, who must have known this key already. 
   156 generated by the spy, who must have known this key already. 
   157 Either way, the induction hypothesis applies.
   157 Either way, the induction hypothesis applies.
   158 
   158 
   160 can occur only once in a trace.  The following lemma states that a nonce
   160 can occur only once in a trace.  The following lemma states that a nonce
   161 cannot be used both as $Na$ and as $Nb$ unless
   161 cannot be used both as $Na$ and as $Nb$ unless
   162 it is known to the spy.  Intuitively, it holds because honest agents
   162 it is known to the spy.  Intuitively, it holds because honest agents
   163 always choose fresh values as nonces; only the spy might reuse a value,
   163 always choose fresh values as nonces; only the spy might reuse a value,
   164 and he doesn't know this particular value.  The proof script is short:
   164 and he doesn't know this particular value.  The proof script is short:
   165 induction, simplification, @{text blast}.  The first line uses the rule
   165 induction, simplification, \<open>blast\<close>.  The first line uses the rule
   166 @{text rev_mp} to prepare the induction by moving two assumptions into the 
   166 \<open>rev_mp\<close> to prepare the induction by moving two assumptions into the 
   167 induction formula.
   167 induction formula.
   168 \<close>
   168 \<close>
   169 
   169 
   170 lemma no_nonce_NS1_NS2:
   170 lemma no_nonce_NS1_NS2:
   171     "\<lbrakk>Crypt (pubK C) \<lbrace>NA', Nonce NA, Agent D\<rbrace> \<in> parts (knows Spy evs);
   171     "\<lbrakk>Crypt (pubK C) \<lbrace>NA', Nonce NA, Agent D\<rbrace> \<in> parts (knows Spy evs);
   275  "\<lbrakk>Says B A (Crypt (pubK A) \<lbrace>Nonce NA, Nonce NB, Agent B\<rbrace>) \<in> set evs;
   275  "\<lbrakk>Says B A (Crypt (pubK A) \<lbrace>Nonce NA, Nonce NB, Agent B\<rbrace>) \<in> set evs;
   276    A \<notin> bad;  B \<notin> bad;  evs \<in> ns_public\<rbrakk>
   276    A \<notin> bad;  B \<notin> bad;  evs \<in> ns_public\<rbrakk>
   277   \<Longrightarrow> Nonce NB \<notin> analz (knows Spy evs)"
   277   \<Longrightarrow> Nonce NB \<notin> analz (knows Spy evs)"
   278 txt \<open>
   278 txt \<open>
   279 To prove it, we must formulate the induction properly (one of the
   279 To prove it, we must formulate the induction properly (one of the
   280 assumptions mentions~@{text evs}), apply induction, and simplify:
   280 assumptions mentions~\<open>evs\<close>), apply induction, and simplify:
   281 \<close>
   281 \<close>
   282 
   282 
   283 apply (erule rev_mp, erule ns_public.induct, simp_all)
   283 apply (erule rev_mp, erule ns_public.induct, simp_all)
   284 (*<*)
   284 (*<*)
   285 apply spy_analz
   285 apply spy_analz
   297 belongs to a different run from that referred to in the theorem
   297 belongs to a different run from that referred to in the theorem
   298 statement --- the theorem
   298 statement --- the theorem
   299 refers to a past instance of message~2, while this subgoal
   299 refers to a past instance of message~2, while this subgoal
   300 concerns message~1 being sent just now.
   300 concerns message~1 being sent just now.
   301 In the Isabelle subgoal, instead of primed variables like $B'$ and $Na'$
   301 In the Isabelle subgoal, instead of primed variables like $B'$ and $Na'$
   302 we have @{text Ba} and~@{text NAa}:
   302 we have \<open>Ba\<close> and~\<open>NAa\<close>:
   303 @{subgoals[display,indent=0]}
   303 @{subgoals[display,indent=0]}
   304 The simplifier has used a 
   304 The simplifier has used a 
   305 default simplification rule that does a case
   305 default simplification rule that does a case
   306 analysis for each encrypted message on whether or not the decryption key
   306 analysis for each encrypted message on whether or not the decryption key
   307 is compromised.
   307 is compromised.
   308 @{named_thms [display,indent=0,margin=50] analz_Crypt_if [no_vars] (analz_Crypt_if)}
   308 @{named_thms [display,indent=0,margin=50] analz_Crypt_if [no_vars] (analz_Crypt_if)}
   309 The simplifier has also used @{text Spy_see_priK}, proved in
   309 The simplifier has also used \<open>Spy_see_priK\<close>, proved in
   310 {\S}\ref{sec:regularity} above, to yield @{term "Ba \<in> bad"}.
   310 {\S}\ref{sec:regularity} above, to yield @{term "Ba \<in> bad"}.
   311 
   311 
   312 Recall that this subgoal concerns the case
   312 Recall that this subgoal concerns the case
   313 where the last message to be sent was
   313 where the last message to be sent was
   314 \[ 1.\quad  A'\to B'  : \comp{Na',A'}\sb{Kb'}. \]
   314 \[ 1.\quad  A'\to B'  : \comp{Na',A'}\sb{Kb'}. \]
   315 This message can compromise $Nb$ only if $Nb=Na'$ and $B'$ is compromised,
   315 This message can compromise $Nb$ only if $Nb=Na'$ and $B'$ is compromised,
   316 allowing the spy to decrypt the message.  The Isabelle subgoal says
   316 allowing the spy to decrypt the message.  The Isabelle subgoal says
   317 precisely this, if we allow for its choice of variable names.
   317 precisely this, if we allow for its choice of variable names.
   318 Proving @{term "NB \<noteq> NAa"} is easy: @{text NB} was
   318 Proving @{term "NB \<noteq> NAa"} is easy: \<open>NB\<close> was
   319 sent earlier, while @{text NAa} is fresh; formally, we have
   319 sent earlier, while \<open>NAa\<close> is fresh; formally, we have
   320 the assumption @{term "Nonce NAa \<notin> used evs1"}. 
   320 the assumption @{term "Nonce NAa \<notin> used evs1"}. 
   321 
   321 
   322 Note that our reasoning concerned @{text B}'s participation in another
   322 Note that our reasoning concerned \<open>B\<close>'s participation in another
   323 run.  Agents may engage in several runs concurrently, and some attacks work
   323 run.  Agents may engage in several runs concurrently, and some attacks work
   324 by interleaving the messages of two runs.  With model checking, this
   324 by interleaving the messages of two runs.  With model checking, this
   325 possibility can cause a state-space explosion, and for us it
   325 possibility can cause a state-space explosion, and for us it
   326 certainly complicates proofs.  The biggest subgoal concerns message~2.  It
   326 certainly complicates proofs.  The biggest subgoal concerns message~2.  It
   327 splits into several cases, such as whether or not the message just sent is
   327 splits into several cases, such as whether or not the message just sent is
   328 the very message mentioned in the theorem statement.
   328 the very message mentioned in the theorem statement.
   329 Some of the cases are proved by unicity, others by
   329 Some of the cases are proved by unicity, others by
   330 the induction hypothesis.  For all those complications, the proofs are
   330 the induction hypothesis.  For all those complications, the proofs are
   331 automatic by @{text blast} with the theorem @{text no_nonce_NS1_NS2}.
   331 automatic by \<open>blast\<close> with the theorem \<open>no_nonce_NS1_NS2\<close>.
   332 
   332 
   333 The remaining theorems about the protocol are not hard to prove.  The
   333 The remaining theorems about the protocol are not hard to prove.  The
   334 following one asserts a form of \emph{authenticity}: if
   334 following one asserts a form of \emph{authenticity}: if
   335 @{text B} has sent an instance of message~2 to~@{text A} and has received the
   335 \<open>B\<close> has sent an instance of message~2 to~\<open>A\<close> and has received the
   336 expected reply, then that reply really originated with~@{text A}.  The
   336 expected reply, then that reply really originated with~\<open>A\<close>.  The
   337 proof is a simple induction.
   337 proof is a simple induction.
   338 \<close>
   338 \<close>
   339 
   339 
   340 (*<*)
   340 (*<*)
   341 by (blast intro: no_nonce_NS1_NS2)
   341 by (blast intro: no_nonce_NS1_NS2)
   367       Says A B (Crypt (pubK B) \<lbrace>Nonce NA, Agent A\<rbrace>) \<in> set evs"
   367       Says A B (Crypt (pubK B) \<lbrace>Nonce NA, Agent A\<rbrace>) \<in> set evs"
   368 by (erule ns_public.induct, auto)
   368 by (erule ns_public.induct, auto)
   369 (*>*)
   369 (*>*)
   370 
   370 
   371 text \<open>
   371 text \<open>
   372 From similar assumptions, we can prove that @{text A} started the protocol
   372 From similar assumptions, we can prove that \<open>A\<close> started the protocol
   373 run by sending an instance of message~1 involving the nonce~@{text NA}\@. 
   373 run by sending an instance of message~1 involving the nonce~\<open>NA\<close>\@. 
   374 For this theorem, the conclusion is 
   374 For this theorem, the conclusion is 
   375 @{thm [display] (concl) B_trusts_protocol [no_vars]}
   375 @{thm [display] (concl) B_trusts_protocol [no_vars]}
   376 Analogous theorems can be proved for~@{text A}, stating that nonce~@{text NA}
   376 Analogous theorems can be proved for~\<open>A\<close>, stating that nonce~\<open>NA\<close>
   377 remains secret and that message~2 really originates with~@{text B}.  Even the
   377 remains secret and that message~2 really originates with~\<open>B\<close>.  Even the
   378 flawed protocol establishes these properties for~@{text A};
   378 flawed protocol establishes these properties for~\<open>A\<close>;
   379 the flaw only harms the second participant.
   379 the flaw only harms the second participant.
   380 
   380 
   381 \medskip
   381 \medskip
   382 
   382 
   383 Detailed information on this protocol verification technique can be found
   383 Detailed information on this protocol verification technique can be found