free (ie->id_cr);
{ free (ie->id_cr); ie->id_cr = 0; }
initiator_recv_HASH_SA_NONCE (struct message *msg){  struct exchange *exchange = msg->exchange;  struct ipsec_exch *ie = exchange->data;  struct sa *sa;  struct proto *proto, *next_proto;  struct payload *sa_p = TAILQ_FIRST (&msg->payload[ISAKMP_PAYLOAD_SA]);  struct payload *xf, *idp;  struct payload *hashp = TAILQ_FIRST (&msg->payload[ISAKMP_PAYLOAD_HASH]);  struct payload *kep = TAILQ_FIRST (&msg->payload[ISAKMP_PAYLOAD_KEY_EXCH]);  struct prf *prf;  struct sa *isakmp_sa = msg->isakmp_sa;  struct ipsec_sa *isa = isakmp_sa->data;  struct hash *hash = hash_get (isa->hash);  size_t hashsize = hash->hashsize;  u_int8_t *rest;  size_t rest_len;  struct sockaddr *src, *dst;  /* Allocate the prf and start calculating our HASH(1).  XXX Share?  */  LOG_DBG_BUF ((LOG_NEGOTIATION, 90, "initiator_recv_HASH_SA_NONCE: SKEYID_a",		(u_int8_t *)isa->skeyid_a, isa->skeyid_len));  prf = prf_alloc (isa->prf_type, hash->type, isa->skeyid_a, isa->skeyid_len);  if (!prf)    return -1;  prf->Init (prf->prfctx);  LOG_DBG_BUF ((LOG_NEGOTIATION, 90,		"initiator_recv_HASH_SA_NONCE: message_id",		exchange->message_id, ISAKMP_HDR_MESSAGE_ID_LEN));  prf->Update (prf->prfctx, exchange->message_id, ISAKMP_HDR_MESSAGE_ID_LEN);  LOG_DBG_BUF ((LOG_NEGOTIATION, 90, "initiator_recv_HASH_SA_NONCE: NONCE_I_b",		exchange->nonce_i, exchange->nonce_i_len));  prf->Update (prf->prfctx, exchange->nonce_i, exchange->nonce_i_len);  rest = hashp->p + GET_ISAKMP_GEN_LENGTH (hashp->p);  rest_len = (GET_ISAKMP_HDR_LENGTH (msg->iov[0].iov_base)	      - (rest - (u_int8_t*)msg->iov[0].iov_base));  LOG_DBG_BUF ((LOG_NEGOTIATION, 90,		"initiator_recv_HASH_SA_NONCE: payloads after HASH(2)", rest,		rest_len));  prf->Update (prf->prfctx, rest, rest_len);  prf->Final (hash->digest, prf->prfctx);  prf_free (prf);  LOG_DBG_BUF ((LOG_NEGOTIATION, 80,		"initiator_recv_HASH_SA_NONCE: computed HASH(2)",		hash->digest, hashsize));  if (memcmp (hashp->p + ISAKMP_HASH_DATA_OFF, hash->digest, hashsize) != 0)    {      message_drop (msg, ISAKMP_NOTIFY_INVALID_HASH_INFORMATION, 0, 1, 0);      return -1;    }  /* Mark the HASH as handled.  */  hashp->flags |= PL_MARK;  /*   * As we are getting an answer on our transform offer, only one transform   * should be given.   *   * XXX Currently we only support negotiating one SA per quick mode run.   */  if (TAILQ_NEXT (sa_p, link))    {      log_print ("initiator_recv_HASH_SA_NONCE: "		 "multiple SA payloads in quick mode not supported yet");      return -1;    }  sa = TAILQ_FIRST (&exchange->sa_list);  /* This is here for the policy check */  if (kep)    ie->pfs = 1;  /* Handle optional client ID payloads.  */  idp = TAILQ_FIRST (&msg->payload[ISAKMP_PAYLOAD_ID]);  if (idp)    {      /* If IDci is there, IDcr must be too.  */      if (!TAILQ_NEXT (idp, link))	{	  /* XXX Is this a good notify type?  */	  message_drop (msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED, 0, 1, 0);	  return -1;	}      /* XXX We should really compare, not override.  */      ie->id_ci_sz = GET_ISAKMP_GEN_LENGTH (idp->p);      ie->id_ci = malloc (ie->id_ci_sz);      if (!ie->id_ci)	{	  log_error ("initiator_recv_HASH_SA_NONCE: malloc (%lu) failed",		     (unsigned long)ie->id_ci_sz);	  return -1;	}      memcpy (ie->id_ci, idp->p, ie->id_ci_sz);      idp->flags |= PL_MARK;      LOG_DBG_BUF ((LOG_NEGOTIATION, 90,		    "initiator_recv_HASH_SA_NONCE: IDci",		    ie->id_ci + ISAKMP_GEN_SZ, ie->id_ci_sz		    - ISAKMP_GEN_SZ));      idp = TAILQ_NEXT (idp, link);      ie->id_cr_sz = GET_ISAKMP_GEN_LENGTH (idp->p);      ie->id_cr = malloc (ie->id_cr_sz);      if (!ie->id_cr)	{	  log_error ("initiator_recv_HASH_SA_NONCE: malloc (%lu) failed",		     (unsigned long)ie->id_cr_sz);	  return -1;	}      memcpy (ie->id_cr, idp->p, ie->id_cr_sz);      idp->flags |= PL_MARK;      LOG_DBG_BUF ((LOG_NEGOTIATION, 90,		    "initiator_recv_HASH_SA_NONCE: IDcr",		    ie->id_cr + ISAKMP_GEN_SZ, ie->id_cr_sz		    - ISAKMP_GEN_SZ));    }  else    {      /*       * If client identifiers are not present in the exchange,       * we fake them. RFC 2409 states:       *    The identities of the SAs negotiated in Quick Mode are       *    implicitly assumed to be the IP addresses of the ISAKMP       *    peers, without any constraints on the protocol or port       *    numbers allowed, unless client identifiers are specified       *    in Quick Mode.       *       * -- Michael Paddon (mwp@aba.net.au)       */      ie->flags = IPSEC_EXCH_FLAG_NO_ID;      /* Get initiator and responder addresses.  */      msg->transport->vtbl->get_src (msg->transport, &src);      msg->transport->vtbl->get_dst (msg->transport, &dst);      ie->id_ci_sz = ISAKMP_ID_DATA_OFF + sockaddr_addrlen (src);      ie->id_cr_sz = ISAKMP_ID_DATA_OFF + sockaddr_addrlen (dst);      ie->id_ci = calloc (ie->id_ci_sz, sizeof (char));      ie->id_cr = calloc (ie->id_cr_sz, sizeof (char));      if (!ie->id_ci || !ie->id_cr)	{	  log_error ("initiator_recv_HASH_SA_NONCE: malloc (%lu) failed",		     (unsigned long)ie->id_cr_sz);	  if (ie->id_ci)	    free (ie->id_ci);	  if (ie->id_cr)	    free (ie->id_cr);	  return -1;	}      if (src->sa_family != dst->sa_family)	{	  log_error ("initiator_recv_HASH_SA_NONCE: sa_family mismatch");	  free (ie->id_ci);	  free (ie->id_cr);	  return -1;	}      switch (src->sa_family)	{	case AF_INET:	  SET_ISAKMP_ID_TYPE (ie->id_ci, IPSEC_ID_IPV4_ADDR);	  SET_ISAKMP_ID_TYPE (ie->id_cr, IPSEC_ID_IPV4_ADDR);	  break;	case AF_INET6:	  SET_ISAKMP_ID_TYPE (ie->id_ci, IPSEC_ID_IPV6_ADDR);	  SET_ISAKMP_ID_TYPE (ie->id_cr, IPSEC_ID_IPV6_ADDR);	  break;	default:	  log_error ("initiator_recv_HASH_SA_NONCE: unknown sa_family %d",		     src->sa_family);	  free (ie->id_ci);	  free (ie->id_cr);	  return -1;	}      memcpy (ie->id_ci + ISAKMP_ID_DATA_OFF, sockaddr_addrdata (src),	      sockaddr_addrlen (src));      memcpy (ie->id_cr + ISAKMP_ID_DATA_OFF, sockaddr_addrdata (dst),	      sockaddr_addrlen (dst));    }  /* Build the protection suite in our SA.  */  for (xf = TAILQ_FIRST (&msg->payload[ISAKMP_PAYLOAD_TRANSFORM]); xf;       xf = TAILQ_NEXT (xf, link))    {      /*       * XXX We could check that the proposal each transform belongs to       * is unique.       */      if (sa_add_transform (sa, xf, exchange->initiator, &proto))	return -1;      /* XXX Check that the chosen transform matches an offer.  */      ipsec_decode_transform (msg, sa, proto, xf->p);    }  /* Now remove offers that we don't need anymore.  */  for (proto = TAILQ_FIRST (&sa->protos); proto; proto = next_proto)    {      next_proto = TAILQ_NEXT (proto, link);      if (!proto->chosen)	proto_free (proto);    }#ifdef USE_POLICY  if (!check_policy (exchange, sa, msg->isakmp_sa))    {      message_drop (msg, ISAKMP_NOTIFY_NO_PROPOSAL_CHOSEN, 0, 1, 0);      log_print ("initiator_recv_HASH_SA_NONCE: policy check failed");      return -1;    }#endif  /* Mark the SA as handled.  */  sa_p->flags |= PL_MARK;  isa = sa->data;  if ((isa->group_desc && (!ie->group || ie->group->id != isa->group_desc))      || (!isa->group_desc && ie->group))    {      log_print ("initiator_recv_HASH_SA_NONCE: disagreement on PFS");      return -1;    }  /* Copy out the initiator's nonce.  */  if (exchange_save_nonce (msg))    return -1;  /* Handle the optional KEY_EXCH payload.  */  if (kep && ipsec_save_g_x (msg))    return -1;  return 0;}