Annotation of embedaddon/strongswan/src/libcharon/sa/ikev1/tasks/aggressive_mode.c, revision 1.1.1.2

1.1       misho       1: /*
                      2:  * Copyright (C) 2012 Tobias Brunner
                      3:  * HSR Hochschule fuer Technik Rapperswil
                      4:  *
                      5:  * Copyright (C) 2012 Martin Willi
                      6:  * Copyright (C) 2012 revosec AG
                      7:  *
                      8:  * This program is free software; you can redistribute it and/or modify it
                      9:  * under the terms of the GNU General Public License as published by the
                     10:  * Free Software Foundation; either version 2 of the License, or (at your
                     11:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
                     12:  *
                     13:  * This program is distributed in the hope that it will be useful, but
                     14:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     15:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
                     16:  * for more details.
                     17:  */
                     18: 
                     19: #include "aggressive_mode.h"
                     20: 
                     21: #include <string.h>
                     22: 
                     23: #include <daemon.h>
                     24: #include <sa/ikev1/phase1.h>
                     25: #include <encoding/payloads/sa_payload.h>
                     26: #include <encoding/payloads/id_payload.h>
                     27: #include <encoding/payloads/hash_payload.h>
                     28: #include <sa/ikev1/tasks/xauth.h>
                     29: #include <sa/ikev1/tasks/mode_config.h>
                     30: #include <sa/ikev1/tasks/informational.h>
                     31: #include <sa/ikev1/tasks/isakmp_delete.h>
                     32: #include <processing/jobs/adopt_children_job.h>
                     33: #include <processing/jobs/delete_ike_sa_job.h>
                     34: 
                     35: typedef struct private_aggressive_mode_t private_aggressive_mode_t;
                     36: 
                     37: /**
                     38:  * Private members of a aggressive_mode_t task.
                     39:  */
                     40: struct private_aggressive_mode_t {
                     41: 
                     42:        /**
                     43:         * Public methods and task_t interface.
                     44:         */
                     45:        aggressive_mode_t public;
                     46: 
                     47:        /**
                     48:         * Assigned IKE_SA.
                     49:         */
                     50:        ike_sa_t *ike_sa;
                     51: 
                     52:        /**
                     53:         * Are we the initiator?
                     54:         */
                     55:        bool initiator;
                     56: 
                     57:        /**
                     58:         * Common phase 1 helper class
                     59:         */
                     60:        phase1_t *ph1;
                     61: 
                     62:        /**
                     63:         * IKE config to establish
                     64:         */
                     65:        ike_cfg_t *ike_cfg;
                     66: 
                     67:        /**
                     68:         * Peer config to use
                     69:         */
                     70:        peer_cfg_t *peer_cfg;
                     71: 
                     72:        /**
                     73:         * selected IKE proposal
                     74:         */
                     75:        proposal_t *proposal;
                     76: 
                     77:        /**
                     78:         * Negotiated SA lifetime
                     79:         */
                     80:        uint32_t lifetime;
                     81: 
                     82:        /**
                     83:         * Negotiated authentication method
                     84:         */
                     85:        auth_method_t method;
                     86: 
                     87:        /**
                     88:         * Encoded ID payload, without fixed header
                     89:         */
                     90:        chunk_t id_data;
                     91: 
                     92:        /** states of aggressive mode */
                     93:        enum {
                     94:                AM_INIT,
                     95:                AM_AUTH,
                     96:        } state;
                     97: };
                     98: 
                     99: /**
                    100:  * Set IKE_SA to established state
                    101:  */
                    102: static bool establish(private_aggressive_mode_t *this)
                    103: {
                    104:        if (!charon->bus->authorize(charon->bus, TRUE))
                    105:        {
                    106:                DBG1(DBG_IKE, "final authorization hook forbids IKE_SA, cancelling");
                    107:                return FALSE;
                    108:        }
                    109: 
                    110:        DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
                    111:                 this->ike_sa->get_name(this->ike_sa),
                    112:                 this->ike_sa->get_unique_id(this->ike_sa),
                    113:                 this->ike_sa->get_my_host(this->ike_sa),
                    114:                 this->ike_sa->get_my_id(this->ike_sa),
                    115:                 this->ike_sa->get_other_host(this->ike_sa),
                    116:                 this->ike_sa->get_other_id(this->ike_sa));
                    117: 
                    118:        this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
                    119:        charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
                    120: 
                    121:        return TRUE;
                    122: }
                    123: 
                    124: /**
                    125:  * Check for notify errors, return TRUE if error found
                    126:  */
                    127: static bool has_notify_errors(private_aggressive_mode_t *this, message_t *message)
                    128: {
                    129:        enumerator_t *enumerator;
                    130:        payload_t *payload;
                    131:        bool err = FALSE;
                    132: 
                    133:        enumerator = message->create_payload_enumerator(message);
                    134:        while (enumerator->enumerate(enumerator, &payload))
                    135:        {
                    136:                if (payload->get_type(payload) == PLV1_NOTIFY)
                    137:                {
                    138:                        notify_payload_t *notify;
                    139:                        notify_type_t type;
                    140: 
                    141:                        notify = (notify_payload_t*)payload;
                    142:                        type = notify->get_notify_type(notify);
                    143:                        if (type < 16384)
                    144:                        {
                    145:                                DBG1(DBG_IKE, "received %N error notify",
                    146:                                         notify_type_names, type);
                    147:                                err = TRUE;
                    148:                        }
                    149:                        else
                    150:                        {
                    151:                                DBG1(DBG_IKE, "received %N notify", notify_type_names, type);
                    152:                        }
                    153:                }
                    154:        }
                    155:        enumerator->destroy(enumerator);
                    156: 
                    157:        return err;
                    158: }
                    159: 
                    160: /**
                    161:  * Queue a task sending a notify in an INFORMATIONAL exchange
                    162:  */
                    163: static status_t send_notify(private_aggressive_mode_t *this, notify_type_t type)
                    164: {
                    165:        notify_payload_t *notify;
                    166:        ike_sa_id_t *ike_sa_id;
                    167:        uint64_t spi_i, spi_r;
                    168:        chunk_t spi;
                    169: 
                    170:        notify = notify_payload_create_from_protocol_and_type(PLV1_NOTIFY,
                    171:                                                                                                                  PROTO_IKE, type);
                    172:        ike_sa_id = this->ike_sa->get_id(this->ike_sa);
                    173:        spi_i = ike_sa_id->get_initiator_spi(ike_sa_id);
                    174:        spi_r = ike_sa_id->get_responder_spi(ike_sa_id);
                    175:        spi = chunk_cata("cc", chunk_from_thing(spi_i), chunk_from_thing(spi_r));
                    176:        notify->set_spi_data(notify, spi);
                    177: 
                    178:        this->ike_sa->queue_task(this->ike_sa,
                    179:                                                (task_t*)informational_create(this->ike_sa, notify));
                    180:        /* cancel all active/passive tasks in favour of informational */
                    181:        this->ike_sa->flush_queue(this->ike_sa,
                    182:                                        this->initiator ? TASK_QUEUE_ACTIVE : TASK_QUEUE_PASSIVE);
                    183:        return ALREADY_DONE;
                    184: }
                    185: 
                    186: /**
                    187:  * Queue a delete task if authentication failed as initiator
                    188:  */
                    189: static status_t send_delete(private_aggressive_mode_t *this)
                    190: {
                    191:        this->ike_sa->queue_task(this->ike_sa,
                    192:                                                (task_t*)isakmp_delete_create(this->ike_sa, TRUE));
                    193:        /* cancel all active tasks in favour of informational */
                    194:        this->ike_sa->flush_queue(this->ike_sa,
                    195:                                        this->initiator ? TASK_QUEUE_ACTIVE : TASK_QUEUE_PASSIVE);
                    196:        return ALREADY_DONE;
                    197: }
                    198: 
                    199: /**
                    200:  * Schedule a timeout for the IKE_SA should it not establish
                    201:  */
                    202: static void schedule_timeout(ike_sa_t *ike_sa)
                    203: {
                    204:        job_t *job;
                    205: 
                    206:        job = (job_t*)delete_ike_sa_job_create(ike_sa->get_id(ike_sa), FALSE);
                    207:        lib->scheduler->schedule_job(lib->scheduler, job, HALF_OPEN_IKE_SA_TIMEOUT);
                    208: }
                    209: 
                    210: METHOD(task_t, build_i, status_t,
                    211:        private_aggressive_mode_t *this, message_t *message)
                    212: {
                    213:        switch (this->state)
                    214:        {
                    215:                case AM_INIT:
                    216:                {
                    217:                        sa_payload_t *sa_payload;
                    218:                        id_payload_t *id_payload;
                    219:                        linked_list_t *proposals;
                    220:                        identification_t *id;
                    221:                        packet_t *packet;
                    222:                        uint16_t group;
                    223: 
                    224:                        DBG0(DBG_IKE, "initiating Aggressive Mode IKE_SA %s[%d] to %H",
                    225:                                 this->ike_sa->get_name(this->ike_sa),
                    226:                                 this->ike_sa->get_unique_id(this->ike_sa),
                    227:                                 this->ike_sa->get_other_host(this->ike_sa));
                    228:                        this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
                    229: 
                    230:                        this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
                    231:                        this->peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
                    232:                        this->peer_cfg->get_ref(this->peer_cfg);
                    233: 
                    234:                        this->method = this->ph1->get_auth_method(this->ph1, this->peer_cfg);
                    235:                        if (this->method == AUTH_NONE)
                    236:                        {
                    237:                                DBG1(DBG_CFG, "configuration uses unsupported authentication");
                    238:                                return FAILED;
                    239:                        }
                    240:                        this->lifetime = this->peer_cfg->get_reauth_time(this->peer_cfg,
                    241:                                                                                                                         FALSE);
                    242:                        if (!this->lifetime)
                    243:                        {       /* fall back to rekey time of no rekey time configured */
                    244:                                this->lifetime = this->peer_cfg->get_rekey_time(this->peer_cfg,
                    245:                                                                                                                                 FALSE);
                    246:                        }
                    247:                        this->lifetime += this->peer_cfg->get_over_time(this->peer_cfg);
                    248:                        proposals = this->ike_cfg->get_proposals(this->ike_cfg);
                    249:                        sa_payload = sa_payload_create_from_proposals_v1(proposals,
                    250:                                                                        this->lifetime, 0, this->method, MODE_NONE,
                    251:                                                                        ENCAP_NONE, 0);
                    252:                        proposals->destroy_offset(proposals, offsetof(proposal_t, destroy));
                    253: 
                    254:                        message->add_payload(message, &sa_payload->payload_interface);
                    255: 
                    256:                        group = this->ike_cfg->get_dh_group(this->ike_cfg);
                    257:                        if (group == MODP_NONE)
                    258:                        {
                    259:                                DBG1(DBG_IKE, "DH group selection failed");
                    260:                                return FAILED;
                    261:                        }
                    262:                        if (!this->ph1->create_dh(this->ph1, group))
                    263:                        {
                    264:                                DBG1(DBG_IKE, "DH group %N not supported",
                    265:                                         diffie_hellman_group_names, group);
                    266:                                return FAILED;
                    267:                        }
                    268:                        if (!this->ph1->add_nonce_ke(this->ph1, message))
                    269:                        {
                    270:                                return FAILED;
                    271:                        }
                    272:                        id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
                    273:                        this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
                    274:                        id_payload = id_payload_create_from_identification(PLV1_ID, id);
                    275:                        this->id_data = id_payload->get_encoded(id_payload);
                    276:                        message->add_payload(message, &id_payload->payload_interface);
                    277: 
                    278:                        /* pregenerate message to store SA payload */
                    279:                        if (this->ike_sa->generate_message(this->ike_sa, message,
                    280:                                                                                           &packet) != SUCCESS)
                    281:                        {
                    282:                                DBG1(DBG_IKE, "pregenerating SA payload failed");
                    283:                                return FAILED;
                    284:                        }
                    285:                        packet->destroy(packet);
                    286:                        if (!this->ph1->save_sa_payload(this->ph1, message))
                    287:                        {
                    288:                                DBG1(DBG_IKE, "SA payload invalid");
                    289:                                return FAILED;
                    290:                        }
                    291:                        this->state = AM_AUTH;
                    292:                        return NEED_MORE;
                    293:                }
                    294:                case AM_AUTH:
                    295:                {
                    296:                        if (!this->ph1->build_auth(this->ph1, this->method, message,
                    297:                                                                           this->id_data))
                    298:                        {
                    299:                                this->id_data = chunk_empty;
                    300:                                charon->bus->alert(charon->bus, ALERT_LOCAL_AUTH_FAILED);
                    301:                                return send_notify(this, AUTHENTICATION_FAILED);
                    302:                        }
                    303:                        this->id_data = chunk_empty;
                    304: 
                    305:                        switch (this->method)
                    306:                        {
                    307:                                case AUTH_XAUTH_INIT_PSK:
                    308:                                case AUTH_XAUTH_INIT_RSA:
                    309:                                case AUTH_HYBRID_INIT_RSA:
                    310:                                        /* wait for XAUTH request */
                    311:                                        schedule_timeout(this->ike_sa);
                    312:                                        break;
                    313:                                case AUTH_XAUTH_RESP_PSK:
                    314:                                case AUTH_XAUTH_RESP_RSA:
                    315:                                case AUTH_HYBRID_RESP_RSA:
                    316:                                        this->ike_sa->queue_task(this->ike_sa,
                    317:                                                                        (task_t*)xauth_create(this->ike_sa, TRUE));
                    318:                                        break;
                    319:                                default:
                    320:                                        if (charon->ike_sa_manager->check_uniqueness(
                    321:                                                                charon->ike_sa_manager, this->ike_sa, FALSE))
                    322:                                        {
                    323:                                                DBG1(DBG_IKE, "cancelling Aggressive Mode due to "
                    324:                                                         "uniqueness policy");
                    325:                                                return send_notify(this, AUTHENTICATION_FAILED);
                    326:                                        }
                    327:                                        if (!establish(this))
                    328:                                        {
                    329:                                                charon->bus->alert(charon->bus, ALERT_PEER_AUTH_FAILED);
                    330:                                                return send_notify(this, AUTHENTICATION_FAILED);
                    331:                                        }
                    332:                                        break;
                    333:                        }
                    334:                        /* check for and prepare mode config push/pull */
                    335:                        if (this->ph1->has_virtual_ip(this->ph1, this->peer_cfg))
                    336:                        {
                    337:                                if (this->peer_cfg->use_pull_mode(this->peer_cfg))
                    338:                                {
                    339:                                        this->ike_sa->queue_task(this->ike_sa,
                    340:                                                (task_t*)mode_config_create(this->ike_sa, TRUE, TRUE));
                    341:                                }
                    342:                                else
                    343:                                {
                    344:                                        schedule_timeout(this->ike_sa);
                    345:                                }
                    346:                        }
                    347:                        else if (this->ph1->has_pool(this->ph1, this->peer_cfg))
                    348:                        {
                    349:                                if (this->peer_cfg->use_pull_mode(this->peer_cfg))
                    350:                                {
                    351:                                        schedule_timeout(this->ike_sa);
                    352:                                }
                    353:                                else
                    354:                                {
                    355:                                        this->ike_sa->queue_task(this->ike_sa,
                    356:                                                (task_t*)mode_config_create(this->ike_sa, TRUE, FALSE));
                    357:                                }
                    358:                        }
                    359:                        return SUCCESS;
                    360:                }
                    361:                default:
                    362:                        return FAILED;
                    363:        }
                    364: }
                    365: 
                    366: METHOD(task_t, process_r, status_t,
                    367:        private_aggressive_mode_t *this, message_t *message)
                    368: {
                    369:        switch (this->state)
                    370:        {
                    371:                case AM_INIT:
                    372:                {
                    373:                        sa_payload_t *sa_payload;
                    374:                        id_payload_t *id_payload;
                    375:                        identification_t *id;
                    376:                        linked_list_t *list;
                    377:                        proposal_selection_flag_t flags = PROPOSAL_SKIP_PRIVATE;
                    378:                        uint16_t group;
                    379: 
                    380:                        this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
                    381:                        DBG0(DBG_IKE, "%H is initiating a Aggressive Mode IKE_SA",
                    382:                                 message->get_source(message));
                    383:                        this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
                    384: 
                    385:                        this->ike_sa->update_hosts(this->ike_sa,
                    386:                                                                           message->get_destination(message),
1.1.1.2 ! misho     387:                                                                           message->get_source(message),
        !           388:                                                                           UPDATE_HOSTS_FORCE_ADDRS);
1.1       misho     389: 
                    390:                        sa_payload = (sa_payload_t*)message->get_payload(message,
                    391:                                                                                                        PLV1_SECURITY_ASSOCIATION);
                    392:                        if (!sa_payload)
                    393:                        {
                    394:                                DBG1(DBG_IKE, "SA payload missing");
                    395:                                return send_notify(this, INVALID_PAYLOAD_TYPE);
                    396:                        }
                    397:                        if (!this->ph1->save_sa_payload(this->ph1, message))
                    398:                        {
                    399:                                return send_notify(this, INVALID_PAYLOAD_TYPE);
                    400:                        }
                    401: 
                    402:                        list = sa_payload->get_proposals(sa_payload);
                    403:                        if (!lib->settings->get_bool(lib->settings,
                    404:                                                "%s.prefer_configured_proposals", TRUE, lib->ns))
                    405:                        {
                    406:                                flags = PROPOSAL_PREFER_SUPPLIED;
                    407:                        }
                    408:                        this->proposal = this->ike_cfg->select_proposal(this->ike_cfg, list,
                    409:                                                                                                                        flags);
                    410:                        list->destroy_offset(list, offsetof(proposal_t, destroy));
                    411:                        if (!this->proposal)
                    412:                        {
                    413:                                DBG1(DBG_IKE, "no proposal found");
                    414:                                return send_notify(this, NO_PROPOSAL_CHOSEN);
                    415:                        }
                    416:                        this->ike_sa->set_proposal(this->ike_sa, this->proposal);
                    417: 
                    418:                        this->method = sa_payload->get_auth_method(sa_payload);
                    419:                        this->lifetime = sa_payload->get_lifetime(sa_payload,
                    420:                                                                                                          this->proposal);
                    421: 
                    422:                        switch (this->method)
                    423:                        {
                    424:                                case AUTH_XAUTH_INIT_PSK:
                    425:                                case AUTH_XAUTH_RESP_PSK:
                    426:                                case AUTH_PSK:
                    427:                                        if (!lib->settings->get_bool(lib->settings, "%s.i_dont_care"
                    428:                                                "_about_security_and_use_aggressive_mode_psk",
                    429:                                                FALSE, lib->ns))
                    430:                                        {
                    431:                                                DBG1(DBG_IKE, "Aggressive Mode PSK disabled for "
                    432:                                                         "security reasons");
                    433:                                                charon->bus->alert(charon->bus, ALERT_PEER_AUTH_FAILED);
                    434:                                                return send_notify(this, AUTHENTICATION_FAILED);
                    435:                                        }
                    436:                                        break;
                    437:                                default:
                    438:                                        break;
                    439:                        }
                    440: 
                    441:                        if (!this->proposal->get_algorithm(this->proposal,
                    442:                                                                                DIFFIE_HELLMAN_GROUP, &group, NULL))
                    443:                        {
                    444:                                DBG1(DBG_IKE, "DH group selection failed");
                    445:                                return send_notify(this, INVALID_KEY_INFORMATION);
                    446:                        }
                    447:                        if (!this->ph1->create_dh(this->ph1, group))
                    448:                        {
                    449:                                DBG1(DBG_IKE, "negotiated DH group not supported");
                    450:                                return send_notify(this, INVALID_KEY_INFORMATION);
                    451:                        }
                    452:                        if (!this->ph1->get_nonce_ke(this->ph1, message))
                    453:                        {
                    454:                                return send_notify(this, INVALID_PAYLOAD_TYPE);
                    455:                        }
                    456: 
                    457:                        id_payload = (id_payload_t*)message->get_payload(message, PLV1_ID);
                    458:                        if (!id_payload)
                    459:                        {
                    460:                                DBG1(DBG_IKE, "IDii payload missing");
                    461:                                charon->bus->alert(charon->bus, ALERT_PEER_AUTH_FAILED);
                    462:                                return send_notify(this, INVALID_PAYLOAD_TYPE);
                    463:                        }
                    464: 
                    465:                        id = id_payload->get_identification(id_payload);
                    466:                        this->id_data = id_payload->get_encoded(id_payload);
                    467:                        this->ike_sa->set_other_id(this->ike_sa, id);
                    468:                        this->peer_cfg = this->ph1->select_config(this->ph1,
                    469:                                                                                                          this->method, TRUE, id);
                    470:                        if (!this->peer_cfg)
                    471:                        {
                    472:                                charon->bus->alert(charon->bus, ALERT_PEER_AUTH_FAILED);
                    473:                                return send_notify(this, AUTHENTICATION_FAILED);
                    474:                        }
                    475:                        this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
                    476: 
                    477:                        this->state = AM_AUTH;
                    478:                        if (has_notify_errors(this, message))
                    479:                        {
                    480:                                return FAILED;
                    481:                        }
                    482:                        return NEED_MORE;
                    483:                }
                    484:                case AM_AUTH:
                    485:                {
                    486:                        adopt_children_job_t *job = NULL;
                    487:                        xauth_t *xauth = NULL;
                    488: 
                    489:                        while (TRUE)
                    490:                        {
                    491:                                if (this->ph1->verify_auth(this->ph1, this->method, message,
                    492:                                                                                   chunk_clone(this->id_data)))
                    493:                                {
                    494:                                        break;
                    495:                                }
                    496:                                this->peer_cfg->destroy(this->peer_cfg);
                    497:                                this->peer_cfg = this->ph1->select_config(this->ph1,
                    498:                                                                                                        this->method, TRUE, NULL);
                    499:                                if (!this->peer_cfg)
                    500:                                {
                    501:                                        charon->bus->alert(charon->bus, ALERT_PEER_AUTH_FAILED);
                    502:                                        return send_delete(this);
                    503:                                }
                    504:                                this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
                    505:                        }
                    506: 
                    507:                        if (!charon->bus->authorize(charon->bus, FALSE))
                    508:                        {
                    509:                                DBG1(DBG_IKE, "Aggressive Mode authorization hook forbids "
                    510:                                         "IKE_SA, cancelling");
                    511:                                charon->bus->alert(charon->bus, ALERT_PEER_AUTH_FAILED);
                    512:                                return send_delete(this);
                    513:                        }
                    514: 
                    515:                        switch (this->method)
                    516:                        {
                    517:                                case AUTH_XAUTH_INIT_PSK:
                    518:                                case AUTH_XAUTH_INIT_RSA:
                    519:                                case AUTH_HYBRID_INIT_RSA:
                    520:                                        xauth = xauth_create(this->ike_sa, TRUE);
                    521:                                        this->ike_sa->queue_task(this->ike_sa, (task_t*)xauth);
                    522:                                        break;
                    523:                                case AUTH_XAUTH_RESP_PSK:
                    524:                                case AUTH_XAUTH_RESP_RSA:
                    525:                                case AUTH_HYBRID_RESP_RSA:
                    526:                                        /* wait for XAUTH request */
                    527:                                        break;
                    528:                                default:
                    529:                                        if (charon->ike_sa_manager->check_uniqueness(
                    530:                                                                charon->ike_sa_manager, this->ike_sa, FALSE))
                    531:                                        {
                    532:                                                DBG1(DBG_IKE, "cancelling Aggressive Mode due to "
                    533:                                                         "uniqueness policy");
                    534:                                                return send_delete(this);
                    535:                                        }
                    536:                                        if (!establish(this))
                    537:                                        {
                    538:                                                charon->bus->alert(charon->bus, ALERT_PEER_AUTH_FAILED);
                    539:                                                return send_delete(this);
                    540:                                        }
                    541:                                        job = adopt_children_job_create(
                    542:                                                                                        this->ike_sa->get_id(this->ike_sa));
                    543:                                        break;
                    544:                        }
                    545:                        /* check for and prepare mode config push/pull */
                    546:                        if (this->ph1->has_virtual_ip(this->ph1, this->peer_cfg))
                    547:                        {
                    548:                                if (this->peer_cfg->use_pull_mode(this->peer_cfg))
                    549:                                {
                    550:                                        this->ike_sa->queue_task(this->ike_sa,
                    551:                                                (task_t*)mode_config_create(this->ike_sa, TRUE, TRUE));
                    552:                                }
                    553:                        }
                    554:                        else if (this->ph1->has_pool(this->ph1, this->peer_cfg))
                    555:                        {
                    556:                                if (!this->peer_cfg->use_pull_mode(this->peer_cfg))
                    557:                                {
                    558:                                        if (job)
                    559:                                        {
                    560:                                                job->queue_task(job, (task_t*)
                    561:                                                                mode_config_create(this->ike_sa, TRUE, FALSE));
                    562:                                        }
                    563:                                        else if (xauth)
                    564:                                        {
                    565:                                                xauth->queue_mode_config_push(xauth);
                    566:                                        }
                    567:                                        else
                    568:                                        {
                    569:                                                this->ike_sa->queue_task(this->ike_sa, (task_t*)
                    570:                                                                mode_config_create(this->ike_sa, TRUE, FALSE));
                    571:                                        }
                    572:                                }
                    573:                        }
                    574:                        if (job)
                    575:                        {
                    576:                                lib->processor->queue_job(lib->processor, (job_t*)job);
                    577:                        }
                    578:                        return SUCCESS;
                    579:                }
                    580:                default:
                    581:                        return FAILED;
                    582:        }
                    583: }
                    584: 
                    585: METHOD(task_t, build_r, status_t,
                    586:        private_aggressive_mode_t *this, message_t *message)
                    587: {
                    588:        if (this->state == AM_AUTH)
                    589:        {
                    590:                sa_payload_t *sa_payload;
                    591:                id_payload_t *id_payload;
                    592:                identification_t *id;
                    593: 
                    594:                sa_payload = sa_payload_create_from_proposal_v1(this->proposal,
                    595:                                                                        this->lifetime, 0, this->method, MODE_NONE,
                    596:                                                                        ENCAP_NONE, 0);
                    597:                message->add_payload(message, &sa_payload->payload_interface);
                    598: 
                    599:                if (!this->ph1->add_nonce_ke(this->ph1, message))
                    600:                {
                    601:                        return send_notify(this, INVALID_KEY_INFORMATION);
                    602:                }
                    603:                if (!this->ph1->create_hasher(this->ph1))
                    604:                {
                    605:                        return send_notify(this, NO_PROPOSAL_CHOSEN);
                    606:                }
                    607:                if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
                    608:                {
                    609:                        return send_notify(this, INVALID_KEY_INFORMATION);
                    610:                }
                    611: 
                    612:                id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
                    613:                this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
                    614: 
                    615:                id_payload = id_payload_create_from_identification(PLV1_ID, id);
                    616:                message->add_payload(message, &id_payload->payload_interface);
                    617: 
                    618:                if (!this->ph1->build_auth(this->ph1, this->method, message,
                    619:                                                                   id_payload->get_encoded(id_payload)))
                    620:                {
                    621:                        charon->bus->alert(charon->bus, ALERT_LOCAL_AUTH_FAILED);
                    622:                        return send_notify(this, AUTHENTICATION_FAILED);
                    623:                }
                    624:                return NEED_MORE;
                    625:        }
                    626:        return FAILED;
                    627: }
                    628: 
                    629: METHOD(task_t, process_i, status_t,
                    630:        private_aggressive_mode_t *this, message_t *message)
                    631: {
                    632:        if (this->state == AM_AUTH)
                    633:        {
                    634:                auth_method_t method;
                    635:                sa_payload_t *sa_payload;
                    636:                id_payload_t *id_payload;
                    637:                identification_t *id, *cid;
                    638:                linked_list_t *list;
                    639:                uint32_t lifetime;
                    640: 
                    641:                sa_payload = (sa_payload_t*)message->get_payload(message,
                    642:                                                                                                PLV1_SECURITY_ASSOCIATION);
                    643:                if (!sa_payload)
                    644:                {
                    645:                        DBG1(DBG_IKE, "SA payload missing");
                    646:                        return send_notify(this, INVALID_PAYLOAD_TYPE);
                    647:                }
                    648:                list = sa_payload->get_proposals(sa_payload);
                    649:                this->proposal = this->ike_cfg->select_proposal(this->ike_cfg, list, 0);
                    650:                list->destroy_offset(list, offsetof(proposal_t, destroy));
                    651:                if (!this->proposal)
                    652:                {
                    653:                        DBG1(DBG_IKE, "no proposal found");
                    654:                        return send_notify(this, NO_PROPOSAL_CHOSEN);
                    655:                }
                    656:                this->ike_sa->set_proposal(this->ike_sa, this->proposal);
                    657: 
                    658:                lifetime = sa_payload->get_lifetime(sa_payload, this->proposal);
                    659:                if (lifetime != this->lifetime)
                    660:                {
                    661:                        DBG1(DBG_IKE, "received lifetime %us does not match configured "
                    662:                                 "lifetime %us", lifetime, this->lifetime);
                    663:                }
                    664:                this->lifetime = lifetime;
                    665:                method = sa_payload->get_auth_method(sa_payload);
                    666:                if (method != this->method)
                    667:                {
                    668:                        DBG1(DBG_IKE, "received %N authentication, but configured %N, "
                    669:                                 "continue with configured", auth_method_names, method,
                    670:                                 auth_method_names, this->method);
                    671:                }
                    672:                if (!this->ph1->get_nonce_ke(this->ph1, message))
                    673:                {
                    674:                        return send_notify(this, INVALID_PAYLOAD_TYPE);
                    675:                }
                    676:                if (!this->ph1->create_hasher(this->ph1))
                    677:                {
                    678:                        return send_notify(this, NO_PROPOSAL_CHOSEN);
                    679:                }
                    680: 
                    681:                id_payload = (id_payload_t*)message->get_payload(message, PLV1_ID);
                    682:                if (!id_payload)
                    683:                {
                    684:                        DBG1(DBG_IKE, "IDir payload missing");
                    685:                        charon->bus->alert(charon->bus, ALERT_PEER_AUTH_FAILED);
                    686:                        return send_delete(this);
                    687:                }
                    688:                id = id_payload->get_identification(id_payload);
                    689:                cid = this->ph1->get_id(this->ph1, this->peer_cfg, FALSE);
                    690:                if (cid && !id->matches(id, cid))
                    691:                {
                    692:                        DBG1(DBG_IKE, "IDir '%Y' does not match to '%Y'", id, cid);
                    693:                        id->destroy(id);
                    694:                        charon->bus->alert(charon->bus, ALERT_PEER_AUTH_FAILED);
                    695:                        return send_notify(this, INVALID_ID_INFORMATION);
                    696:                }
                    697:                this->ike_sa->set_other_id(this->ike_sa, id);
                    698: 
                    699:                if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
                    700:                {
                    701:                        return send_notify(this, INVALID_KEY_INFORMATION);
                    702:                }
                    703:                if (!this->ph1->verify_auth(this->ph1, this->method, message,
                    704:                                                                        id_payload->get_encoded(id_payload)))
                    705:                {
                    706:                        charon->bus->alert(charon->bus, ALERT_PEER_AUTH_FAILED);
                    707:                        return send_notify(this, AUTHENTICATION_FAILED);
                    708:                }
                    709:                if (!charon->bus->authorize(charon->bus, FALSE))
                    710:                {
                    711:                        DBG1(DBG_IKE, "Aggressive Mode authorization hook forbids IKE_SA, "
                    712:                                 "cancelling");
                    713:                        return send_notify(this, AUTHENTICATION_FAILED);
                    714:                }
                    715: 
                    716:                return NEED_MORE;
                    717:        }
                    718:        return FAILED;
                    719: }
                    720: 
                    721: METHOD(task_t, get_type, task_type_t,
                    722:        private_aggressive_mode_t *this)
                    723: {
                    724:        return TASK_AGGRESSIVE_MODE;
                    725: }
                    726: 
                    727: METHOD(task_t, migrate, void,
                    728:        private_aggressive_mode_t *this, ike_sa_t *ike_sa)
                    729: {
                    730:        DESTROY_IF(this->peer_cfg);
                    731:        DESTROY_IF(this->proposal);
                    732:        this->ph1->destroy(this->ph1);
                    733:        chunk_free(&this->id_data);
                    734: 
                    735:        this->ike_sa = ike_sa;
                    736:        this->state = AM_INIT;
                    737:        this->peer_cfg = NULL;
                    738:        this->proposal = NULL;
                    739:        this->ph1 = phase1_create(ike_sa, this->initiator);
                    740: }
                    741: 
                    742: METHOD(task_t, destroy, void,
                    743:        private_aggressive_mode_t *this)
                    744: {
                    745:        DESTROY_IF(this->peer_cfg);
                    746:        DESTROY_IF(this->proposal);
                    747:        this->ph1->destroy(this->ph1);
                    748:        chunk_free(&this->id_data);
                    749:        free(this);
                    750: }
                    751: 
                    752: /*
                    753:  * Described in header.
                    754:  */
                    755: aggressive_mode_t *aggressive_mode_create(ike_sa_t *ike_sa, bool initiator)
                    756: {
                    757:        private_aggressive_mode_t *this;
                    758: 
                    759:        INIT(this,
                    760:                .public = {
                    761:                        .task = {
                    762:                                .get_type = _get_type,
                    763:                                .migrate = _migrate,
                    764:                                .destroy = _destroy,
                    765:                        },
                    766:                },
                    767:                .ike_sa = ike_sa,
                    768:                .ph1 = phase1_create(ike_sa, initiator),
                    769:                .initiator = initiator,
                    770:                .state = AM_INIT,
                    771:        );
                    772: 
                    773:        if (initiator)
                    774:        {
                    775:                this->public.task.build = _build_i;
                    776:                this->public.task.process = _process_i;
                    777:        }
                    778:        else
                    779:        {
                    780:                this->public.task.build = _build_r;
                    781:                this->public.task.process = _process_r;
                    782:        }
                    783: 
                    784:        return &this->public;
                    785: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>