67fa04
diff --git a/programs/pluto/ikev1.c b/programs/pluto/ikev1.c
67fa04
index e0615323ed..401618b6dd 100644
67fa04
--- a/programs/pluto/ikev1.c
67fa04
+++ b/programs/pluto/ikev1.c
67fa04
@@ -1101,10 +1101,20 @@ void process_v1_packet(struct msg_digest *md)
67fa04
 	struct state *st = NULL;
67fa04
 	enum state_kind from_state = STATE_UNDEFINED;   /* state we started in */
67fa04
 
67fa04
+	/*
67fa04
+	 * For the initial responses, don't leak the responder's SPI.
67fa04
+	 * Hence the use of send_v1_notification_from_md().
67fa04
+	 *
67fa04
+	 * AGGR mode is a mess in that the R0->R1 transition happens
67fa04
+	 * well before the transition succeeds.
67fa04
+	 */
67fa04
 #define SEND_NOTIFICATION(t)						\
67fa04
 	{								\
67fa04
 		pstats(ikev1_sent_notifies_e, t);			\
67fa04
-		if (st != NULL)						\
67fa04
+		if (st != NULL &&					\
67fa04
+		    st->st_state->kind != STATE_AGGR_R0 &&		\
67fa04
+		    st->st_state->kind != STATE_AGGR_R1 &&		\
67fa04
+		    st->st_state->kind != STATE_MAIN_R0)		\
67fa04
 			send_v1_notification_from_state(st, from_state, t); \
67fa04
 		else							\
67fa04
 			send_v1_notification_from_md(md, t);		\
67fa04
@@ -1168,17 +1178,26 @@ void process_v1_packet(struct msg_digest *md)
67fa04
 			from_state = (md->hdr.isa_xchg == ISAKMP_XCHG_IDPROT ?
67fa04
 				      STATE_MAIN_R0 : STATE_AGGR_R0);
67fa04
 		} else {
67fa04
-			/* not an initial message */
67fa04
+			/*
67fa04
+			 * Possibly not an initial message.  Possibly
67fa04
+			 * from initiator.  Possibly from responder.
67fa04
+			 *
67fa04
+			 * Possibly.  Which is probably hopeless.
67fa04
+			 */
67fa04
 
67fa04
 			st = find_state_ikev1(&md->hdr.isa_ike_spis,
67fa04
 					      md->hdr.isa_msgid);
67fa04
 
67fa04
 			if (st == NULL) {
67fa04
 				/*
67fa04
-				 * perhaps this is a first message
67fa04
+				 * Perhaps this is a first message
67fa04
 				 * from the responder and contains a
67fa04
 				 * responder cookie that we've not yet
67fa04
 				 * seen.
67fa04
+				 *
67fa04
+				 * Perhaps this is a random message
67fa04
+				 * with a bogus non-zero responder IKE
67fa04
+				 * SPI.
67fa04
 				 */
67fa04
 				st = find_state_ikev1_init(&md->hdr.isa_ike_initiator_spi,
67fa04
 							   md->hdr.isa_msgid);
67fa04
@@ -1189,6 +1208,21 @@ void process_v1_packet(struct msg_digest *md)
67fa04
 					/* XXX Could send notification back */
67fa04
 					return;
67fa04
 				}
67fa04
+				if (st->st_state->kind == STATE_AGGR_R0) {
67fa04
+					/*
67fa04
+					 * The only way for this to
67fa04
+					 * happen is for the attacker
67fa04
+					 * to guess the responder's
67fa04
+					 * IKE SPI that hasn't been
67fa04
+					 * sent over the wire?
67fa04
+					 *
67fa04
+					 * Well that or played 1/2^32
67fa04
+					 * odds.
67fa04
+					 */
67fa04
+					llog_pexpect(md->md_logger, HERE,
67fa04
+						     "phase 1 message matching AGGR_R0 state");
67fa04
+					return;
67fa04
+				}
67fa04
 			}
67fa04
 			from_state = st->st_state->kind;
67fa04
 		}
67fa04
@@ -2870,7 +2904,28 @@ void complete_v1_state_transition(struct state *st, struct msg_digest *md, stf_s
67fa04
 			delete_state(st);
67fa04
 			/* wipe out dangling pointer to st */
67fa04
 			md->v1_st = NULL;
67fa04
+		} else if  (st->st_state->kind == STATE_AGGR_R0 ||
67fa04
+			    st->st_state->kind == STATE_AGGR_R1 ||
67fa04
+			    st->st_state->kind == STATE_MAIN_R0) {
67fa04
+			/*
67fa04
+			 *
67fa04
+			 * Wipe out the incomplete larval state.
67fa04
+			 *
67fa04
+			 * ARGH! In <=v4.10, the aggr code flipped the
67fa04
+			 * larval state to R1 right at the start of
67fa04
+			 * the transition and not the end, so using
67fa04
+			 * state to figure things out is close to
67fa04
+			 * useless.
67fa04
+			 *
67fa04
+			 * Deleting the state means that pluto has no
67fa04
+			 * way to detect and ignore amplification
67fa04
+			 * attacks.
67fa04
+			 */
67fa04
+			delete_state(st);
67fa04
+			/* wipe out dangling pointer to st */
67fa04
+			md->v1_st = NULL;
67fa04
 		}
67fa04
+
67fa04
 		break;
67fa04
 	}
67fa04
 	}
67fa04
diff --git a/programs/pluto/ikev1_aggr.c b/programs/pluto/ikev1_aggr.c
67fa04
index 2732951beb..87be80cb6c 100644
67fa04
--- a/programs/pluto/ikev1_aggr.c
67fa04
+++ b/programs/pluto/ikev1_aggr.c
67fa04
@@ -169,7 +169,7 @@ stf_status aggr_inI1_outR1(struct state *null_st UNUSED,
67fa04
 	/* Set up state */
67fa04
 	struct ike_sa *ike = new_v1_rstate(c, md);
67fa04
 	md->v1_st = &ike->sa;  /* (caller will reset cur_state) */
67fa04
-	change_v1_state(&ike->sa, STATE_AGGR_R1);
67fa04
+	change_v1_state(&ike->sa, STATE_AGGR_R0);
67fa04
 
67fa04
 	/*
67fa04
 	 * Warn when peer is expected to use especially dangerous
67fa04
@@ -197,7 +197,8 @@ stf_status aggr_inI1_outR1(struct state *null_st UNUSED,
67fa04
 
67fa04
 	if (!v1_decode_certs(md)) {
67fa04
 		llog_sa(RC_LOG, ike, "X509: CERT payload bogus or revoked");
67fa04
-		return false;
67fa04
+		/* XXX notification is in order! */
67fa04
+		return STF_FAIL_v1N + v1N_INVALID_ID_INFORMATION;
67fa04
 	}
67fa04
 
67fa04
 	/*