ike-cfg: Add option to prefer supplied proposals over locally configured ones

This commit is contained in:
Tobias Brunner 2016-06-01 11:44:33 +02:00
parent f5e8bc18fd
commit 3a40d572c6
5 changed files with 48 additions and 32 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2012-2015 Tobias Brunner * Copyright (C) 2012-2016 Tobias Brunner
* Copyright (C) 2005-2007 Martin Willi * Copyright (C) 2005-2007 Martin Willi
* Copyright (C) 2005 Jan Hutter * Copyright (C) 2005 Jan Hutter
* Hochschule fuer Technik Rapperswil * Hochschule fuer Technik Rapperswil
@ -310,42 +310,57 @@ METHOD(ike_cfg_t, get_proposals, linked_list_t*,
} }
METHOD(ike_cfg_t, select_proposal, proposal_t*, METHOD(ike_cfg_t, select_proposal, proposal_t*,
private_ike_cfg_t *this, linked_list_t *proposals, bool private) private_ike_cfg_t *this, linked_list_t *proposals, bool private,
bool prefer_self)
{ {
enumerator_t *stored_enum, *supplied_enum; enumerator_t *prefer_enum, *match_enum;
proposal_t *stored, *supplied, *selected; proposal_t *proposal, *match, *selected = NULL;
stored_enum = this->proposals->create_enumerator(this->proposals); if (prefer_self)
supplied_enum = proposals->create_enumerator(proposals);
/* compare all stored proposals with all supplied. Stored ones are preferred.*/
while (stored_enum->enumerate(stored_enum, (void**)&stored))
{ {
proposals->reset_enumerator(proposals, supplied_enum); prefer_enum = this->proposals->create_enumerator(this->proposals);
match_enum = proposals->create_enumerator(proposals);
while (supplied_enum->enumerate(supplied_enum, (void**)&supplied)) }
else
{ {
selected = stored->select(stored, supplied, private); prefer_enum = proposals->create_enumerator(proposals);
match_enum = this->proposals->create_enumerator(this->proposals);
}
while (prefer_enum->enumerate(prefer_enum, (void**)&proposal))
{
if (prefer_self)
{
proposals->reset_enumerator(proposals, match_enum);
}
else
{
this->proposals->reset_enumerator(this->proposals, match_enum);
}
while (match_enum->enumerate(match_enum, (void**)&match))
{
selected = proposal->select(proposal, match, private);
if (selected) if (selected)
{ {
/* they match, return */
stored_enum->destroy(stored_enum);
supplied_enum->destroy(supplied_enum);
DBG2(DBG_CFG, "received proposals: %#P", proposals); DBG2(DBG_CFG, "received proposals: %#P", proposals);
DBG2(DBG_CFG, "configured proposals: %#P", this->proposals); DBG2(DBG_CFG, "configured proposals: %#P", this->proposals);
DBG2(DBG_CFG, "selected proposal: %P", selected); DBG2(DBG_CFG, "selected proposal: %P", selected);
return selected; break;
} }
} }
if (selected)
{
break;
} }
/* no proposal match :-(, will result in a NO_PROPOSAL_CHOSEN... */ }
stored_enum->destroy(stored_enum); prefer_enum->destroy(prefer_enum);
supplied_enum->destroy(supplied_enum); match_enum->destroy(match_enum);
if (!selected)
{
DBG1(DBG_CFG, "received proposals: %#P", proposals); DBG1(DBG_CFG, "received proposals: %#P", proposals);
DBG1(DBG_CFG, "configured proposals: %#P", this->proposals); DBG1(DBG_CFG, "configured proposals: %#P", this->proposals);
}
return NULL; return selected;
} }
METHOD(ike_cfg_t, get_dh_group, diffie_hellman_group_t, METHOD(ike_cfg_t, get_dh_group, diffie_hellman_group_t,

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2012-2015 Tobias Brunner * Copyright (C) 2012-2016 Tobias Brunner
* Copyright (C) 2005-2007 Martin Willi * Copyright (C) 2005-2007 Martin Willi
* Copyright (C) 2005 Jan Hutter * Copyright (C) 2005 Jan Hutter
* Hochschule fuer Technik Rapperswil * Hochschule fuer Technik Rapperswil
@ -165,16 +165,17 @@ struct ike_cfg_t {
linked_list_t* (*get_proposals) (ike_cfg_t *this); linked_list_t* (*get_proposals) (ike_cfg_t *this);
/** /**
* Select a proposed from suggested proposals. * Select a proposal from a list of supplied proposals.
* *
* Returned proposal must be destroyed after use. * Returned proposal must be destroyed after use.
* *
* @param proposals list of proposals to select from * @param proposals list of proposals to select from
* @param private accept algorithms from a private range * @param private accept algorithms from a private range
* @param prefer_self whether to prefer configured or supplied proposals
* @return selected proposal, or NULL if none matches. * @return selected proposal, or NULL if none matches.
*/ */
proposal_t *(*select_proposal) (ike_cfg_t *this, linked_list_t *proposals, proposal_t *(*select_proposal) (ike_cfg_t *this, linked_list_t *proposals,
bool private); bool private, bool prefer_self);
/** /**
* Should we send a certificate request in IKE_SA_INIT? * Should we send a certificate request in IKE_SA_INIT?

View File

@ -402,7 +402,7 @@ METHOD(task_t, process_r, status_t,
list = sa_payload->get_proposals(sa_payload); list = sa_payload->get_proposals(sa_payload);
this->proposal = this->ike_cfg->select_proposal(this->ike_cfg, this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
list, FALSE); list, FALSE, TRUE);
list->destroy_offset(list, offsetof(proposal_t, destroy)); list->destroy_offset(list, offsetof(proposal_t, destroy));
if (!this->proposal) if (!this->proposal)
{ {
@ -640,7 +640,7 @@ METHOD(task_t, process_i, status_t,
} }
list = sa_payload->get_proposals(sa_payload); list = sa_payload->get_proposals(sa_payload);
this->proposal = this->ike_cfg->select_proposal(this->ike_cfg, this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
list, FALSE); list, FALSE, TRUE);
list->destroy_offset(list, offsetof(proposal_t, destroy)); list->destroy_offset(list, offsetof(proposal_t, destroy));
if (!this->proposal) if (!this->proposal)
{ {

View File

@ -394,7 +394,7 @@ METHOD(task_t, process_r, status_t,
private = this->ike_sa->supports_extension(this->ike_sa, private = this->ike_sa->supports_extension(this->ike_sa,
EXT_STRONGSWAN); EXT_STRONGSWAN);
this->proposal = this->ike_cfg->select_proposal(this->ike_cfg, this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
list, private); list, private, TRUE);
list->destroy_offset(list, offsetof(proposal_t, destroy)); list->destroy_offset(list, offsetof(proposal_t, destroy));
if (!this->proposal) if (!this->proposal)
{ {
@ -641,7 +641,7 @@ METHOD(task_t, process_i, status_t,
private = this->ike_sa->supports_extension(this->ike_sa, private = this->ike_sa->supports_extension(this->ike_sa,
EXT_STRONGSWAN); EXT_STRONGSWAN);
this->proposal = this->ike_cfg->select_proposal(this->ike_cfg, this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
list, private); list, private, TRUE);
list->destroy_offset(list, offsetof(proposal_t, destroy)); list->destroy_offset(list, offsetof(proposal_t, destroy));
if (!this->proposal) if (!this->proposal)
{ {

View File

@ -379,7 +379,7 @@ static void process_payloads(private_ike_init_t *this, message_t *message)
private = this->ike_sa->supports_extension(this->ike_sa, private = this->ike_sa->supports_extension(this->ike_sa,
EXT_STRONGSWAN); EXT_STRONGSWAN);
this->proposal = this->config->select_proposal(this->config, this->proposal = this->config->select_proposal(this->config,
proposal_list, private); proposal_list, private, TRUE);
if (!this->proposal) if (!this->proposal)
{ {
charon->bus->alert(charon->bus, ALERT_PROPOSAL_MISMATCH_IKE, charon->bus->alert(charon->bus, ALERT_PROPOSAL_MISMATCH_IKE,