139beb93cSSam Leffler /*
239beb93cSSam Leffler * IKEv2 responder (RFC 4306) for EAP-IKEV2
339beb93cSSam Leffler * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
439beb93cSSam Leffler *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
739beb93cSSam Leffler */
839beb93cSSam Leffler
939beb93cSSam Leffler #include "includes.h"
1039beb93cSSam Leffler
1139beb93cSSam Leffler #include "common.h"
12e28a4053SRui Paulo #include "crypto/dh_groups.h"
13f05cddf9SRui Paulo #include "crypto/random.h"
1439beb93cSSam Leffler #include "ikev2.h"
1539beb93cSSam Leffler
1639beb93cSSam Leffler
ikev2_responder_deinit(struct ikev2_responder_data * data)1739beb93cSSam Leffler void ikev2_responder_deinit(struct ikev2_responder_data *data)
1839beb93cSSam Leffler {
1939beb93cSSam Leffler ikev2_free_keys(&data->keys);
2039beb93cSSam Leffler wpabuf_free(data->i_dh_public);
2139beb93cSSam Leffler wpabuf_free(data->r_dh_private);
2239beb93cSSam Leffler os_free(data->IDi);
2339beb93cSSam Leffler os_free(data->IDr);
2439beb93cSSam Leffler os_free(data->shared_secret);
2539beb93cSSam Leffler wpabuf_free(data->i_sign_msg);
2639beb93cSSam Leffler wpabuf_free(data->r_sign_msg);
2739beb93cSSam Leffler os_free(data->key_pad);
2839beb93cSSam Leffler }
2939beb93cSSam Leffler
3039beb93cSSam Leffler
ikev2_derive_keys(struct ikev2_responder_data * data)3139beb93cSSam Leffler static int ikev2_derive_keys(struct ikev2_responder_data *data)
3239beb93cSSam Leffler {
3339beb93cSSam Leffler u8 *buf, *pos, *pad, skeyseed[IKEV2_MAX_HASH_LEN];
3439beb93cSSam Leffler size_t buf_len, pad_len;
3539beb93cSSam Leffler struct wpabuf *shared;
3639beb93cSSam Leffler const struct ikev2_integ_alg *integ;
3739beb93cSSam Leffler const struct ikev2_prf_alg *prf;
3839beb93cSSam Leffler const struct ikev2_encr_alg *encr;
3939beb93cSSam Leffler int ret;
4039beb93cSSam Leffler const u8 *addr[2];
4139beb93cSSam Leffler size_t len[2];
4239beb93cSSam Leffler
4339beb93cSSam Leffler /* RFC 4306, Sect. 2.14 */
4439beb93cSSam Leffler
4539beb93cSSam Leffler integ = ikev2_get_integ(data->proposal.integ);
4639beb93cSSam Leffler prf = ikev2_get_prf(data->proposal.prf);
4739beb93cSSam Leffler encr = ikev2_get_encr(data->proposal.encr);
4839beb93cSSam Leffler if (integ == NULL || prf == NULL || encr == NULL) {
4939beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unsupported proposal");
5039beb93cSSam Leffler return -1;
5139beb93cSSam Leffler }
5239beb93cSSam Leffler
5339beb93cSSam Leffler shared = dh_derive_shared(data->i_dh_public, data->r_dh_private,
5439beb93cSSam Leffler data->dh);
5539beb93cSSam Leffler if (shared == NULL)
5639beb93cSSam Leffler return -1;
5739beb93cSSam Leffler
5839beb93cSSam Leffler /* Construct Ni | Nr | SPIi | SPIr */
5939beb93cSSam Leffler
6039beb93cSSam Leffler buf_len = data->i_nonce_len + data->r_nonce_len + 2 * IKEV2_SPI_LEN;
6139beb93cSSam Leffler buf = os_malloc(buf_len);
6239beb93cSSam Leffler if (buf == NULL) {
6339beb93cSSam Leffler wpabuf_free(shared);
6439beb93cSSam Leffler return -1;
6539beb93cSSam Leffler }
6639beb93cSSam Leffler
6739beb93cSSam Leffler pos = buf;
6839beb93cSSam Leffler os_memcpy(pos, data->i_nonce, data->i_nonce_len);
6939beb93cSSam Leffler pos += data->i_nonce_len;
7039beb93cSSam Leffler os_memcpy(pos, data->r_nonce, data->r_nonce_len);
7139beb93cSSam Leffler pos += data->r_nonce_len;
7239beb93cSSam Leffler os_memcpy(pos, data->i_spi, IKEV2_SPI_LEN);
7339beb93cSSam Leffler pos += IKEV2_SPI_LEN;
7439beb93cSSam Leffler os_memcpy(pos, data->r_spi, IKEV2_SPI_LEN);
7539beb93cSSam Leffler
7639beb93cSSam Leffler /* SKEYSEED = prf(Ni | Nr, g^ir) */
7739beb93cSSam Leffler /* Use zero-padding per RFC 4306, Sect. 2.14 */
7839beb93cSSam Leffler pad_len = data->dh->prime_len - wpabuf_len(shared);
7939beb93cSSam Leffler pad = os_zalloc(pad_len ? pad_len : 1);
8039beb93cSSam Leffler if (pad == NULL) {
8139beb93cSSam Leffler wpabuf_free(shared);
8239beb93cSSam Leffler os_free(buf);
8339beb93cSSam Leffler return -1;
8439beb93cSSam Leffler }
8539beb93cSSam Leffler
8639beb93cSSam Leffler addr[0] = pad;
8739beb93cSSam Leffler len[0] = pad_len;
8839beb93cSSam Leffler addr[1] = wpabuf_head(shared);
8939beb93cSSam Leffler len[1] = wpabuf_len(shared);
9039beb93cSSam Leffler if (ikev2_prf_hash(prf->id, buf, data->i_nonce_len + data->r_nonce_len,
9139beb93cSSam Leffler 2, addr, len, skeyseed) < 0) {
9239beb93cSSam Leffler wpabuf_free(shared);
9339beb93cSSam Leffler os_free(buf);
9439beb93cSSam Leffler os_free(pad);
9539beb93cSSam Leffler return -1;
9639beb93cSSam Leffler }
9739beb93cSSam Leffler os_free(pad);
9839beb93cSSam Leffler wpabuf_free(shared);
9939beb93cSSam Leffler
10039beb93cSSam Leffler /* DH parameters are not needed anymore, so free them */
10139beb93cSSam Leffler wpabuf_free(data->i_dh_public);
10239beb93cSSam Leffler data->i_dh_public = NULL;
10339beb93cSSam Leffler wpabuf_free(data->r_dh_private);
10439beb93cSSam Leffler data->r_dh_private = NULL;
10539beb93cSSam Leffler
10639beb93cSSam Leffler wpa_hexdump_key(MSG_DEBUG, "IKEV2: SKEYSEED",
10739beb93cSSam Leffler skeyseed, prf->hash_len);
10839beb93cSSam Leffler
10939beb93cSSam Leffler ret = ikev2_derive_sk_keys(prf, integ, encr, skeyseed, buf, buf_len,
11039beb93cSSam Leffler &data->keys);
11139beb93cSSam Leffler os_free(buf);
11239beb93cSSam Leffler return ret;
11339beb93cSSam Leffler }
11439beb93cSSam Leffler
11539beb93cSSam Leffler
ikev2_parse_transform(struct ikev2_proposal_data * prop,const u8 * pos,const u8 * end)11639beb93cSSam Leffler static int ikev2_parse_transform(struct ikev2_proposal_data *prop,
11739beb93cSSam Leffler const u8 *pos, const u8 *end)
11839beb93cSSam Leffler {
11939beb93cSSam Leffler int transform_len;
12039beb93cSSam Leffler const struct ikev2_transform *t;
12139beb93cSSam Leffler u16 transform_id;
12239beb93cSSam Leffler const u8 *tend;
12339beb93cSSam Leffler
12439beb93cSSam Leffler if (end - pos < (int) sizeof(*t)) {
12539beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Too short transform");
12639beb93cSSam Leffler return -1;
12739beb93cSSam Leffler }
12839beb93cSSam Leffler
12939beb93cSSam Leffler t = (const struct ikev2_transform *) pos;
13039beb93cSSam Leffler transform_len = WPA_GET_BE16(t->transform_length);
131780fb4a2SCy Schubert if (transform_len < (int) sizeof(*t) || transform_len > end - pos) {
13239beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Invalid transform length %d",
13339beb93cSSam Leffler transform_len);
13439beb93cSSam Leffler return -1;
13539beb93cSSam Leffler }
13639beb93cSSam Leffler tend = pos + transform_len;
13739beb93cSSam Leffler
13839beb93cSSam Leffler transform_id = WPA_GET_BE16(t->transform_id);
13939beb93cSSam Leffler
14039beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Transform:");
14139beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Type: %d Transform Length: %d "
14239beb93cSSam Leffler "Transform Type: %d Transform ID: %d",
14339beb93cSSam Leffler t->type, transform_len, t->transform_type, transform_id);
14439beb93cSSam Leffler
14539beb93cSSam Leffler if (t->type != 0 && t->type != 3) {
14639beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected Transform type");
14739beb93cSSam Leffler return -1;
14839beb93cSSam Leffler }
14939beb93cSSam Leffler
15039beb93cSSam Leffler pos = (const u8 *) (t + 1);
15139beb93cSSam Leffler if (pos < tend) {
15239beb93cSSam Leffler wpa_hexdump(MSG_DEBUG, "IKEV2: Transform Attributes",
15339beb93cSSam Leffler pos, tend - pos);
15439beb93cSSam Leffler }
15539beb93cSSam Leffler
15639beb93cSSam Leffler switch (t->transform_type) {
15739beb93cSSam Leffler case IKEV2_TRANSFORM_ENCR:
15839beb93cSSam Leffler if (ikev2_get_encr(transform_id)) {
15939beb93cSSam Leffler if (transform_id == ENCR_AES_CBC) {
16039beb93cSSam Leffler if (tend - pos != 4) {
16139beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: No "
16239beb93cSSam Leffler "Transform Attr for AES");
16339beb93cSSam Leffler break;
16439beb93cSSam Leffler }
16539beb93cSSam Leffler if (WPA_GET_BE16(pos) != 0x800e) {
16639beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Not a "
16739beb93cSSam Leffler "Key Size attribute for "
16839beb93cSSam Leffler "AES");
16939beb93cSSam Leffler break;
17039beb93cSSam Leffler }
17139beb93cSSam Leffler if (WPA_GET_BE16(pos + 2) != 128) {
17239beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: "
17339beb93cSSam Leffler "Unsupported AES key size "
17439beb93cSSam Leffler "%d bits",
17539beb93cSSam Leffler WPA_GET_BE16(pos + 2));
17639beb93cSSam Leffler break;
17739beb93cSSam Leffler }
17839beb93cSSam Leffler }
17939beb93cSSam Leffler prop->encr = transform_id;
18039beb93cSSam Leffler }
18139beb93cSSam Leffler break;
18239beb93cSSam Leffler case IKEV2_TRANSFORM_PRF:
18339beb93cSSam Leffler if (ikev2_get_prf(transform_id))
18439beb93cSSam Leffler prop->prf = transform_id;
18539beb93cSSam Leffler break;
18639beb93cSSam Leffler case IKEV2_TRANSFORM_INTEG:
18739beb93cSSam Leffler if (ikev2_get_integ(transform_id))
18839beb93cSSam Leffler prop->integ = transform_id;
18939beb93cSSam Leffler break;
19039beb93cSSam Leffler case IKEV2_TRANSFORM_DH:
19139beb93cSSam Leffler if (dh_groups_get(transform_id))
19239beb93cSSam Leffler prop->dh = transform_id;
19339beb93cSSam Leffler break;
19439beb93cSSam Leffler }
19539beb93cSSam Leffler
19639beb93cSSam Leffler return transform_len;
19739beb93cSSam Leffler }
19839beb93cSSam Leffler
19939beb93cSSam Leffler
ikev2_parse_proposal(struct ikev2_proposal_data * prop,const u8 * pos,const u8 * end)20039beb93cSSam Leffler static int ikev2_parse_proposal(struct ikev2_proposal_data *prop,
20139beb93cSSam Leffler const u8 *pos, const u8 *end)
20239beb93cSSam Leffler {
20339beb93cSSam Leffler const u8 *pend, *ppos;
204*c1d255d3SCy Schubert int proposal_len;
205*c1d255d3SCy Schubert unsigned int i, num;
20639beb93cSSam Leffler const struct ikev2_proposal *p;
20739beb93cSSam Leffler
20839beb93cSSam Leffler if (end - pos < (int) sizeof(*p)) {
20939beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Too short proposal");
21039beb93cSSam Leffler return -1;
21139beb93cSSam Leffler }
21239beb93cSSam Leffler
21339beb93cSSam Leffler /* FIX: AND processing if multiple proposals use the same # */
21439beb93cSSam Leffler
21539beb93cSSam Leffler p = (const struct ikev2_proposal *) pos;
21639beb93cSSam Leffler proposal_len = WPA_GET_BE16(p->proposal_length);
2175b9c547cSRui Paulo if (proposal_len < (int) sizeof(*p) || proposal_len > end - pos) {
21839beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Invalid proposal length %d",
21939beb93cSSam Leffler proposal_len);
22039beb93cSSam Leffler return -1;
22139beb93cSSam Leffler }
22239beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: SAi1 Proposal # %d",
22339beb93cSSam Leffler p->proposal_num);
22439beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Type: %d Proposal Length: %d "
22539beb93cSSam Leffler " Protocol ID: %d",
22639beb93cSSam Leffler p->type, proposal_len, p->protocol_id);
22739beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: SPI Size: %d Transforms: %d",
22839beb93cSSam Leffler p->spi_size, p->num_transforms);
22939beb93cSSam Leffler
23039beb93cSSam Leffler if (p->type != 0 && p->type != 2) {
23139beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected Proposal type");
23239beb93cSSam Leffler return -1;
23339beb93cSSam Leffler }
23439beb93cSSam Leffler
23539beb93cSSam Leffler if (p->protocol_id != IKEV2_PROTOCOL_IKE) {
23639beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Unexpected Protocol ID "
23739beb93cSSam Leffler "(only IKE allowed for EAP-IKEv2)");
23839beb93cSSam Leffler return -1;
23939beb93cSSam Leffler }
24039beb93cSSam Leffler
24139beb93cSSam Leffler if (p->proposal_num != prop->proposal_num) {
24239beb93cSSam Leffler if (p->proposal_num == prop->proposal_num + 1)
24339beb93cSSam Leffler prop->proposal_num = p->proposal_num;
24439beb93cSSam Leffler else {
24539beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected Proposal #");
24639beb93cSSam Leffler return -1;
24739beb93cSSam Leffler }
24839beb93cSSam Leffler }
24939beb93cSSam Leffler
25039beb93cSSam Leffler ppos = (const u8 *) (p + 1);
25139beb93cSSam Leffler pend = pos + proposal_len;
252780fb4a2SCy Schubert if (p->spi_size > pend - ppos) {
25339beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Not enough room for SPI "
25439beb93cSSam Leffler "in proposal");
25539beb93cSSam Leffler return -1;
25639beb93cSSam Leffler }
25739beb93cSSam Leffler if (p->spi_size) {
25839beb93cSSam Leffler wpa_hexdump(MSG_DEBUG, "IKEV2: SPI",
25939beb93cSSam Leffler ppos, p->spi_size);
26039beb93cSSam Leffler ppos += p->spi_size;
26139beb93cSSam Leffler }
26239beb93cSSam Leffler
26339beb93cSSam Leffler /*
26439beb93cSSam Leffler * For initial IKE_SA negotiation, SPI Size MUST be zero; for
26539beb93cSSam Leffler * subsequent negotiations, it must be 8 for IKE. We only support
26639beb93cSSam Leffler * initial case for now.
26739beb93cSSam Leffler */
26839beb93cSSam Leffler if (p->spi_size != 0) {
26939beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected SPI Size");
27039beb93cSSam Leffler return -1;
27139beb93cSSam Leffler }
27239beb93cSSam Leffler
273*c1d255d3SCy Schubert num = p->num_transforms;
274*c1d255d3SCy Schubert if (num == 0 || num > 255) {
27539beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: At least one transform required");
27639beb93cSSam Leffler return -1;
27739beb93cSSam Leffler }
27839beb93cSSam Leffler
279*c1d255d3SCy Schubert for (i = 0; i < num; i++) {
28039beb93cSSam Leffler int tlen = ikev2_parse_transform(prop, ppos, pend);
28139beb93cSSam Leffler if (tlen < 0)
28239beb93cSSam Leffler return -1;
28339beb93cSSam Leffler ppos += tlen;
28439beb93cSSam Leffler }
28539beb93cSSam Leffler
28639beb93cSSam Leffler if (ppos != pend) {
28739beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected data after "
28839beb93cSSam Leffler "transforms");
28939beb93cSSam Leffler return -1;
29039beb93cSSam Leffler }
29139beb93cSSam Leffler
29239beb93cSSam Leffler return proposal_len;
29339beb93cSSam Leffler }
29439beb93cSSam Leffler
29539beb93cSSam Leffler
ikev2_process_sai1(struct ikev2_responder_data * data,const u8 * sai1,size_t sai1_len)29639beb93cSSam Leffler static int ikev2_process_sai1(struct ikev2_responder_data *data,
29739beb93cSSam Leffler const u8 *sai1, size_t sai1_len)
29839beb93cSSam Leffler {
29939beb93cSSam Leffler struct ikev2_proposal_data prop;
30039beb93cSSam Leffler const u8 *pos, *end;
30139beb93cSSam Leffler int found = 0;
30239beb93cSSam Leffler
30339beb93cSSam Leffler /* Security Association Payloads: <Proposals> */
30439beb93cSSam Leffler
30539beb93cSSam Leffler if (sai1 == NULL) {
30639beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: SAi1 not received");
30739beb93cSSam Leffler return -1;
30839beb93cSSam Leffler }
30939beb93cSSam Leffler
31039beb93cSSam Leffler os_memset(&prop, 0, sizeof(prop));
31139beb93cSSam Leffler prop.proposal_num = 1;
31239beb93cSSam Leffler
31339beb93cSSam Leffler pos = sai1;
31439beb93cSSam Leffler end = sai1 + sai1_len;
31539beb93cSSam Leffler
31639beb93cSSam Leffler while (pos < end) {
31739beb93cSSam Leffler int plen;
31839beb93cSSam Leffler
31939beb93cSSam Leffler prop.integ = -1;
32039beb93cSSam Leffler prop.prf = -1;
32139beb93cSSam Leffler prop.encr = -1;
32239beb93cSSam Leffler prop.dh = -1;
32339beb93cSSam Leffler plen = ikev2_parse_proposal(&prop, pos, end);
32439beb93cSSam Leffler if (plen < 0)
32539beb93cSSam Leffler return -1;
32639beb93cSSam Leffler
32739beb93cSSam Leffler if (!found && prop.integ != -1 && prop.prf != -1 &&
32839beb93cSSam Leffler prop.encr != -1 && prop.dh != -1) {
32939beb93cSSam Leffler os_memcpy(&data->proposal, &prop, sizeof(prop));
33039beb93cSSam Leffler data->dh = dh_groups_get(prop.dh);
33139beb93cSSam Leffler found = 1;
33239beb93cSSam Leffler }
33339beb93cSSam Leffler
33439beb93cSSam Leffler pos += plen;
33539beb93cSSam Leffler }
33639beb93cSSam Leffler
33739beb93cSSam Leffler if (pos != end) {
33839beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected data after proposals");
33939beb93cSSam Leffler return -1;
34039beb93cSSam Leffler }
34139beb93cSSam Leffler
34239beb93cSSam Leffler if (!found) {
34339beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: No acceptable proposal found");
34439beb93cSSam Leffler return -1;
34539beb93cSSam Leffler }
34639beb93cSSam Leffler
34739beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Accepted proposal #%d: ENCR:%d PRF:%d "
34839beb93cSSam Leffler "INTEG:%d D-H:%d", data->proposal.proposal_num,
34939beb93cSSam Leffler data->proposal.encr, data->proposal.prf,
35039beb93cSSam Leffler data->proposal.integ, data->proposal.dh);
35139beb93cSSam Leffler
35239beb93cSSam Leffler return 0;
35339beb93cSSam Leffler }
35439beb93cSSam Leffler
35539beb93cSSam Leffler
ikev2_process_kei(struct ikev2_responder_data * data,const u8 * kei,size_t kei_len)35639beb93cSSam Leffler static int ikev2_process_kei(struct ikev2_responder_data *data,
35739beb93cSSam Leffler const u8 *kei, size_t kei_len)
35839beb93cSSam Leffler {
35939beb93cSSam Leffler u16 group;
36039beb93cSSam Leffler
36139beb93cSSam Leffler /*
36239beb93cSSam Leffler * Key Exchange Payload:
36339beb93cSSam Leffler * DH Group # (16 bits)
36439beb93cSSam Leffler * RESERVED (16 bits)
36539beb93cSSam Leffler * Key Exchange Data (Diffie-Hellman public value)
36639beb93cSSam Leffler */
36739beb93cSSam Leffler
36839beb93cSSam Leffler if (kei == NULL) {
36939beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: KEi not received");
37039beb93cSSam Leffler return -1;
37139beb93cSSam Leffler }
37239beb93cSSam Leffler
37339beb93cSSam Leffler if (kei_len < 4 + 96) {
3745b9c547cSRui Paulo wpa_printf(MSG_INFO, "IKEV2: Too short Key Exchange Payload");
37539beb93cSSam Leffler return -1;
37639beb93cSSam Leffler }
37739beb93cSSam Leffler
37839beb93cSSam Leffler group = WPA_GET_BE16(kei);
37939beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: KEi DH Group #%u", group);
38039beb93cSSam Leffler
38139beb93cSSam Leffler if (group != data->proposal.dh) {
38239beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: KEi DH Group #%u does not match "
38339beb93cSSam Leffler "with the selected proposal (%u)",
38439beb93cSSam Leffler group, data->proposal.dh);
38539beb93cSSam Leffler /* Reject message with Notify payload of type
38639beb93cSSam Leffler * INVALID_KE_PAYLOAD (RFC 4306, Sect. 3.4) */
38739beb93cSSam Leffler data->error_type = INVALID_KE_PAYLOAD;
38839beb93cSSam Leffler data->state = NOTIFY;
38939beb93cSSam Leffler return -1;
39039beb93cSSam Leffler }
39139beb93cSSam Leffler
39239beb93cSSam Leffler if (data->dh == NULL) {
39339beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unsupported DH group");
39439beb93cSSam Leffler return -1;
39539beb93cSSam Leffler }
39639beb93cSSam Leffler
39739beb93cSSam Leffler /* RFC 4306, Section 3.4:
398f05cddf9SRui Paulo * The length of DH public value MUST be equal to the length of the
39939beb93cSSam Leffler * prime modulus.
40039beb93cSSam Leffler */
40139beb93cSSam Leffler if (kei_len - 4 != data->dh->prime_len) {
40239beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Invalid DH public value length "
40339beb93cSSam Leffler "%ld (expected %ld)",
40439beb93cSSam Leffler (long) (kei_len - 4), (long) data->dh->prime_len);
40539beb93cSSam Leffler return -1;
40639beb93cSSam Leffler }
40739beb93cSSam Leffler
40839beb93cSSam Leffler wpabuf_free(data->i_dh_public);
40939beb93cSSam Leffler data->i_dh_public = wpabuf_alloc(kei_len - 4);
41039beb93cSSam Leffler if (data->i_dh_public == NULL)
41139beb93cSSam Leffler return -1;
41239beb93cSSam Leffler wpabuf_put_data(data->i_dh_public, kei + 4, kei_len - 4);
41339beb93cSSam Leffler
41439beb93cSSam Leffler wpa_hexdump_buf(MSG_DEBUG, "IKEV2: KEi Diffie-Hellman Public Value",
41539beb93cSSam Leffler data->i_dh_public);
41639beb93cSSam Leffler
41739beb93cSSam Leffler return 0;
41839beb93cSSam Leffler }
41939beb93cSSam Leffler
42039beb93cSSam Leffler
ikev2_process_ni(struct ikev2_responder_data * data,const u8 * ni,size_t ni_len)42139beb93cSSam Leffler static int ikev2_process_ni(struct ikev2_responder_data *data,
42239beb93cSSam Leffler const u8 *ni, size_t ni_len)
42339beb93cSSam Leffler {
42439beb93cSSam Leffler if (ni == NULL) {
42539beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Ni not received");
42639beb93cSSam Leffler return -1;
42739beb93cSSam Leffler }
42839beb93cSSam Leffler
42939beb93cSSam Leffler if (ni_len < IKEV2_NONCE_MIN_LEN || ni_len > IKEV2_NONCE_MAX_LEN) {
43039beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Invalid Ni length %ld",
43139beb93cSSam Leffler (long) ni_len);
43239beb93cSSam Leffler return -1;
43339beb93cSSam Leffler }
43439beb93cSSam Leffler
43539beb93cSSam Leffler data->i_nonce_len = ni_len;
43639beb93cSSam Leffler os_memcpy(data->i_nonce, ni, ni_len);
43739beb93cSSam Leffler wpa_hexdump(MSG_MSGDUMP, "IKEV2: Ni",
43839beb93cSSam Leffler data->i_nonce, data->i_nonce_len);
43939beb93cSSam Leffler
44039beb93cSSam Leffler return 0;
44139beb93cSSam Leffler }
44239beb93cSSam Leffler
44339beb93cSSam Leffler
ikev2_process_sa_init(struct ikev2_responder_data * data,const struct ikev2_hdr * hdr,struct ikev2_payloads * pl)44439beb93cSSam Leffler static int ikev2_process_sa_init(struct ikev2_responder_data *data,
44539beb93cSSam Leffler const struct ikev2_hdr *hdr,
44639beb93cSSam Leffler struct ikev2_payloads *pl)
44739beb93cSSam Leffler {
44839beb93cSSam Leffler if (ikev2_process_sai1(data, pl->sa, pl->sa_len) < 0 ||
44939beb93cSSam Leffler ikev2_process_kei(data, pl->ke, pl->ke_len) < 0 ||
45039beb93cSSam Leffler ikev2_process_ni(data, pl->nonce, pl->nonce_len) < 0)
45139beb93cSSam Leffler return -1;
45239beb93cSSam Leffler
45339beb93cSSam Leffler os_memcpy(data->i_spi, hdr->i_spi, IKEV2_SPI_LEN);
45439beb93cSSam Leffler
45539beb93cSSam Leffler return 0;
45639beb93cSSam Leffler }
45739beb93cSSam Leffler
45839beb93cSSam Leffler
ikev2_process_idi(struct ikev2_responder_data * data,const u8 * idi,size_t idi_len)45939beb93cSSam Leffler static int ikev2_process_idi(struct ikev2_responder_data *data,
46039beb93cSSam Leffler const u8 *idi, size_t idi_len)
46139beb93cSSam Leffler {
46239beb93cSSam Leffler u8 id_type;
46339beb93cSSam Leffler
46439beb93cSSam Leffler if (idi == NULL) {
46539beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: No IDi received");
46639beb93cSSam Leffler return -1;
46739beb93cSSam Leffler }
46839beb93cSSam Leffler
46939beb93cSSam Leffler if (idi_len < 4) {
47039beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Too short IDi payload");
47139beb93cSSam Leffler return -1;
47239beb93cSSam Leffler }
47339beb93cSSam Leffler
47439beb93cSSam Leffler id_type = idi[0];
47539beb93cSSam Leffler idi += 4;
47639beb93cSSam Leffler idi_len -= 4;
47739beb93cSSam Leffler
47839beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: IDi ID Type %d", id_type);
47939beb93cSSam Leffler wpa_hexdump_ascii(MSG_DEBUG, "IKEV2: IDi", idi, idi_len);
48039beb93cSSam Leffler os_free(data->IDi);
48185732ac8SCy Schubert data->IDi = os_memdup(idi, idi_len);
48239beb93cSSam Leffler if (data->IDi == NULL)
48339beb93cSSam Leffler return -1;
48439beb93cSSam Leffler data->IDi_len = idi_len;
48539beb93cSSam Leffler data->IDi_type = id_type;
48639beb93cSSam Leffler
48739beb93cSSam Leffler return 0;
48839beb93cSSam Leffler }
48939beb93cSSam Leffler
49039beb93cSSam Leffler
ikev2_process_cert(struct ikev2_responder_data * data,const u8 * cert,size_t cert_len)49139beb93cSSam Leffler static int ikev2_process_cert(struct ikev2_responder_data *data,
49239beb93cSSam Leffler const u8 *cert, size_t cert_len)
49339beb93cSSam Leffler {
49439beb93cSSam Leffler u8 cert_encoding;
49539beb93cSSam Leffler
49639beb93cSSam Leffler if (cert == NULL) {
49739beb93cSSam Leffler if (data->peer_auth == PEER_AUTH_CERT) {
49839beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: No Certificate received");
49939beb93cSSam Leffler return -1;
50039beb93cSSam Leffler }
50139beb93cSSam Leffler return 0;
50239beb93cSSam Leffler }
50339beb93cSSam Leffler
50439beb93cSSam Leffler if (cert_len < 1) {
50539beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: No Cert Encoding field");
50639beb93cSSam Leffler return -1;
50739beb93cSSam Leffler }
50839beb93cSSam Leffler
50939beb93cSSam Leffler cert_encoding = cert[0];
51039beb93cSSam Leffler cert++;
51139beb93cSSam Leffler cert_len--;
51239beb93cSSam Leffler
51339beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Cert Encoding %d", cert_encoding);
51439beb93cSSam Leffler wpa_hexdump(MSG_MSGDUMP, "IKEV2: Certificate Data", cert, cert_len);
51539beb93cSSam Leffler
51639beb93cSSam Leffler /* TODO: validate certificate */
51739beb93cSSam Leffler
51839beb93cSSam Leffler return 0;
51939beb93cSSam Leffler }
52039beb93cSSam Leffler
52139beb93cSSam Leffler
ikev2_process_auth_cert(struct ikev2_responder_data * data,u8 method,const u8 * auth,size_t auth_len)52239beb93cSSam Leffler static int ikev2_process_auth_cert(struct ikev2_responder_data *data,
52339beb93cSSam Leffler u8 method, const u8 *auth, size_t auth_len)
52439beb93cSSam Leffler {
52539beb93cSSam Leffler if (method != AUTH_RSA_SIGN) {
52639beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unsupported authentication "
52739beb93cSSam Leffler "method %d", method);
52839beb93cSSam Leffler return -1;
52939beb93cSSam Leffler }
53039beb93cSSam Leffler
53139beb93cSSam Leffler /* TODO: validate AUTH */
53239beb93cSSam Leffler return 0;
53339beb93cSSam Leffler }
53439beb93cSSam Leffler
53539beb93cSSam Leffler
ikev2_process_auth_secret(struct ikev2_responder_data * data,u8 method,const u8 * auth,size_t auth_len)53639beb93cSSam Leffler static int ikev2_process_auth_secret(struct ikev2_responder_data *data,
53739beb93cSSam Leffler u8 method, const u8 *auth,
53839beb93cSSam Leffler size_t auth_len)
53939beb93cSSam Leffler {
54039beb93cSSam Leffler u8 auth_data[IKEV2_MAX_HASH_LEN];
54139beb93cSSam Leffler const struct ikev2_prf_alg *prf;
54239beb93cSSam Leffler
54339beb93cSSam Leffler if (method != AUTH_SHARED_KEY_MIC) {
54439beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unsupported authentication "
54539beb93cSSam Leffler "method %d", method);
54639beb93cSSam Leffler return -1;
54739beb93cSSam Leffler }
54839beb93cSSam Leffler
54939beb93cSSam Leffler /* msg | Nr | prf(SK_pi,IDi') */
55039beb93cSSam Leffler if (ikev2_derive_auth_data(data->proposal.prf, data->i_sign_msg,
55139beb93cSSam Leffler data->IDi, data->IDi_len, data->IDi_type,
55239beb93cSSam Leffler &data->keys, 1, data->shared_secret,
55339beb93cSSam Leffler data->shared_secret_len,
55439beb93cSSam Leffler data->r_nonce, data->r_nonce_len,
55539beb93cSSam Leffler data->key_pad, data->key_pad_len,
55639beb93cSSam Leffler auth_data) < 0) {
55739beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Could not derive AUTH data");
55839beb93cSSam Leffler return -1;
55939beb93cSSam Leffler }
56039beb93cSSam Leffler
56139beb93cSSam Leffler wpabuf_free(data->i_sign_msg);
56239beb93cSSam Leffler data->i_sign_msg = NULL;
56339beb93cSSam Leffler
56439beb93cSSam Leffler prf = ikev2_get_prf(data->proposal.prf);
56539beb93cSSam Leffler if (prf == NULL)
56639beb93cSSam Leffler return -1;
56739beb93cSSam Leffler
56839beb93cSSam Leffler if (auth_len != prf->hash_len ||
5695b9c547cSRui Paulo os_memcmp_const(auth, auth_data, auth_len) != 0) {
57039beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Invalid Authentication Data");
57139beb93cSSam Leffler wpa_hexdump(MSG_DEBUG, "IKEV2: Received Authentication Data",
57239beb93cSSam Leffler auth, auth_len);
57339beb93cSSam Leffler wpa_hexdump(MSG_DEBUG, "IKEV2: Expected Authentication Data",
57439beb93cSSam Leffler auth_data, prf->hash_len);
57539beb93cSSam Leffler data->error_type = AUTHENTICATION_FAILED;
57639beb93cSSam Leffler data->state = NOTIFY;
57739beb93cSSam Leffler return -1;
57839beb93cSSam Leffler }
57939beb93cSSam Leffler
58039beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Server authenticated successfully "
58139beb93cSSam Leffler "using shared keys");
58239beb93cSSam Leffler
58339beb93cSSam Leffler return 0;
58439beb93cSSam Leffler }
58539beb93cSSam Leffler
58639beb93cSSam Leffler
ikev2_process_auth(struct ikev2_responder_data * data,const u8 * auth,size_t auth_len)58739beb93cSSam Leffler static int ikev2_process_auth(struct ikev2_responder_data *data,
58839beb93cSSam Leffler const u8 *auth, size_t auth_len)
58939beb93cSSam Leffler {
59039beb93cSSam Leffler u8 auth_method;
59139beb93cSSam Leffler
59239beb93cSSam Leffler if (auth == NULL) {
59339beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: No Authentication Payload");
59439beb93cSSam Leffler return -1;
59539beb93cSSam Leffler }
59639beb93cSSam Leffler
59739beb93cSSam Leffler if (auth_len < 4) {
59839beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Too short Authentication "
59939beb93cSSam Leffler "Payload");
60039beb93cSSam Leffler return -1;
60139beb93cSSam Leffler }
60239beb93cSSam Leffler
60339beb93cSSam Leffler auth_method = auth[0];
60439beb93cSSam Leffler auth += 4;
60539beb93cSSam Leffler auth_len -= 4;
60639beb93cSSam Leffler
60739beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Auth Method %d", auth_method);
60839beb93cSSam Leffler wpa_hexdump(MSG_MSGDUMP, "IKEV2: Authentication Data", auth, auth_len);
60939beb93cSSam Leffler
61039beb93cSSam Leffler switch (data->peer_auth) {
61139beb93cSSam Leffler case PEER_AUTH_CERT:
61239beb93cSSam Leffler return ikev2_process_auth_cert(data, auth_method, auth,
61339beb93cSSam Leffler auth_len);
61439beb93cSSam Leffler case PEER_AUTH_SECRET:
61539beb93cSSam Leffler return ikev2_process_auth_secret(data, auth_method, auth,
61639beb93cSSam Leffler auth_len);
61739beb93cSSam Leffler }
61839beb93cSSam Leffler
61939beb93cSSam Leffler return -1;
62039beb93cSSam Leffler }
62139beb93cSSam Leffler
62239beb93cSSam Leffler
ikev2_process_sa_auth_decrypted(struct ikev2_responder_data * data,u8 next_payload,u8 * payload,size_t payload_len)62339beb93cSSam Leffler static int ikev2_process_sa_auth_decrypted(struct ikev2_responder_data *data,
62439beb93cSSam Leffler u8 next_payload,
62539beb93cSSam Leffler u8 *payload, size_t payload_len)
62639beb93cSSam Leffler {
62739beb93cSSam Leffler struct ikev2_payloads pl;
62839beb93cSSam Leffler
62939beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Processing decrypted payloads");
63039beb93cSSam Leffler
63139beb93cSSam Leffler if (ikev2_parse_payloads(&pl, next_payload, payload, payload +
63239beb93cSSam Leffler payload_len) < 0) {
63339beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Failed to parse decrypted "
63439beb93cSSam Leffler "payloads");
63539beb93cSSam Leffler return -1;
63639beb93cSSam Leffler }
63739beb93cSSam Leffler
63839beb93cSSam Leffler if (ikev2_process_idi(data, pl.idi, pl.idi_len) < 0 ||
63939beb93cSSam Leffler ikev2_process_cert(data, pl.cert, pl.cert_len) < 0 ||
64039beb93cSSam Leffler ikev2_process_auth(data, pl.auth, pl.auth_len) < 0)
64139beb93cSSam Leffler return -1;
64239beb93cSSam Leffler
64339beb93cSSam Leffler return 0;
64439beb93cSSam Leffler }
64539beb93cSSam Leffler
64639beb93cSSam Leffler
ikev2_process_sa_auth(struct ikev2_responder_data * data,const struct ikev2_hdr * hdr,struct ikev2_payloads * pl)64739beb93cSSam Leffler static int ikev2_process_sa_auth(struct ikev2_responder_data *data,
64839beb93cSSam Leffler const struct ikev2_hdr *hdr,
64939beb93cSSam Leffler struct ikev2_payloads *pl)
65039beb93cSSam Leffler {
65139beb93cSSam Leffler u8 *decrypted;
65239beb93cSSam Leffler size_t decrypted_len;
65339beb93cSSam Leffler int ret;
65439beb93cSSam Leffler
65539beb93cSSam Leffler decrypted = ikev2_decrypt_payload(data->proposal.encr,
65639beb93cSSam Leffler data->proposal.integ,
65739beb93cSSam Leffler &data->keys, 1, hdr, pl->encrypted,
65839beb93cSSam Leffler pl->encrypted_len, &decrypted_len);
65939beb93cSSam Leffler if (decrypted == NULL)
66039beb93cSSam Leffler return -1;
66139beb93cSSam Leffler
66239beb93cSSam Leffler ret = ikev2_process_sa_auth_decrypted(data, pl->encr_next_payload,
66339beb93cSSam Leffler decrypted, decrypted_len);
66439beb93cSSam Leffler os_free(decrypted);
66539beb93cSSam Leffler
66639beb93cSSam Leffler return ret;
66739beb93cSSam Leffler }
66839beb93cSSam Leffler
66939beb93cSSam Leffler
ikev2_validate_rx_state(struct ikev2_responder_data * data,u8 exchange_type,u32 message_id)67039beb93cSSam Leffler static int ikev2_validate_rx_state(struct ikev2_responder_data *data,
67139beb93cSSam Leffler u8 exchange_type, u32 message_id)
67239beb93cSSam Leffler {
67339beb93cSSam Leffler switch (data->state) {
67439beb93cSSam Leffler case SA_INIT:
67539beb93cSSam Leffler /* Expect to receive IKE_SA_INIT: HDR, SAi1, KEi, Ni */
67639beb93cSSam Leffler if (exchange_type != IKE_SA_INIT) {
67739beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected Exchange Type "
67839beb93cSSam Leffler "%u in SA_INIT state", exchange_type);
67939beb93cSSam Leffler return -1;
68039beb93cSSam Leffler }
68139beb93cSSam Leffler if (message_id != 0) {
68239beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected Message ID %u "
68339beb93cSSam Leffler "in SA_INIT state", message_id);
68439beb93cSSam Leffler return -1;
68539beb93cSSam Leffler }
68639beb93cSSam Leffler break;
68739beb93cSSam Leffler case SA_AUTH:
68839beb93cSSam Leffler /* Expect to receive IKE_SA_AUTH:
68939beb93cSSam Leffler * HDR, SK {IDi, [CERT,] [CERTREQ,] [IDr,]
69039beb93cSSam Leffler * AUTH, SAi2, TSi, TSr}
69139beb93cSSam Leffler */
69239beb93cSSam Leffler if (exchange_type != IKE_SA_AUTH) {
69339beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected Exchange Type "
69439beb93cSSam Leffler "%u in SA_AUTH state", exchange_type);
69539beb93cSSam Leffler return -1;
69639beb93cSSam Leffler }
69739beb93cSSam Leffler if (message_id != 1) {
69839beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected Message ID %u "
69939beb93cSSam Leffler "in SA_AUTH state", message_id);
70039beb93cSSam Leffler return -1;
70139beb93cSSam Leffler }
70239beb93cSSam Leffler break;
70339beb93cSSam Leffler case CHILD_SA:
70439beb93cSSam Leffler if (exchange_type != CREATE_CHILD_SA) {
70539beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected Exchange Type "
70639beb93cSSam Leffler "%u in CHILD_SA state", exchange_type);
70739beb93cSSam Leffler return -1;
70839beb93cSSam Leffler }
70939beb93cSSam Leffler if (message_id != 2) {
71039beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected Message ID %u "
71139beb93cSSam Leffler "in CHILD_SA state", message_id);
71239beb93cSSam Leffler return -1;
71339beb93cSSam Leffler }
71439beb93cSSam Leffler break;
71539beb93cSSam Leffler case NOTIFY:
71639beb93cSSam Leffler case IKEV2_DONE:
71739beb93cSSam Leffler case IKEV2_FAILED:
71839beb93cSSam Leffler return -1;
71939beb93cSSam Leffler }
72039beb93cSSam Leffler
72139beb93cSSam Leffler return 0;
72239beb93cSSam Leffler }
72339beb93cSSam Leffler
72439beb93cSSam Leffler
ikev2_responder_process(struct ikev2_responder_data * data,const struct wpabuf * buf)72539beb93cSSam Leffler int ikev2_responder_process(struct ikev2_responder_data *data,
72639beb93cSSam Leffler const struct wpabuf *buf)
72739beb93cSSam Leffler {
72839beb93cSSam Leffler const struct ikev2_hdr *hdr;
72939beb93cSSam Leffler u32 length, message_id;
73039beb93cSSam Leffler const u8 *pos, *end;
73139beb93cSSam Leffler struct ikev2_payloads pl;
73239beb93cSSam Leffler
73339beb93cSSam Leffler wpa_printf(MSG_MSGDUMP, "IKEV2: Received message (len %lu)",
73439beb93cSSam Leffler (unsigned long) wpabuf_len(buf));
73539beb93cSSam Leffler
73639beb93cSSam Leffler if (wpabuf_len(buf) < sizeof(*hdr)) {
73739beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Too short frame to include HDR");
73839beb93cSSam Leffler return -1;
73939beb93cSSam Leffler }
74039beb93cSSam Leffler
74139beb93cSSam Leffler data->error_type = 0;
74239beb93cSSam Leffler hdr = (const struct ikev2_hdr *) wpabuf_head(buf);
74339beb93cSSam Leffler end = wpabuf_head_u8(buf) + wpabuf_len(buf);
74439beb93cSSam Leffler message_id = WPA_GET_BE32(hdr->message_id);
74539beb93cSSam Leffler length = WPA_GET_BE32(hdr->length);
74639beb93cSSam Leffler
74739beb93cSSam Leffler wpa_hexdump(MSG_DEBUG, "IKEV2: IKE_SA Initiator's SPI",
74839beb93cSSam Leffler hdr->i_spi, IKEV2_SPI_LEN);
74939beb93cSSam Leffler wpa_hexdump(MSG_DEBUG, "IKEV2: IKE_SA Responder's SPI",
75039beb93cSSam Leffler hdr->r_spi, IKEV2_SPI_LEN);
75139beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Next Payload: %u Version: 0x%x "
75239beb93cSSam Leffler "Exchange Type: %u",
75339beb93cSSam Leffler hdr->next_payload, hdr->version, hdr->exchange_type);
75439beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Message ID: %u Length: %u",
75539beb93cSSam Leffler message_id, length);
75639beb93cSSam Leffler
75739beb93cSSam Leffler if (hdr->version != IKEV2_VERSION) {
75839beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unsupported HDR version 0x%x "
75939beb93cSSam Leffler "(expected 0x%x)", hdr->version, IKEV2_VERSION);
76039beb93cSSam Leffler return -1;
76139beb93cSSam Leffler }
76239beb93cSSam Leffler
76339beb93cSSam Leffler if (length != wpabuf_len(buf)) {
76439beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Invalid length (HDR: %lu != "
76539beb93cSSam Leffler "RX: %lu)", (unsigned long) length,
76639beb93cSSam Leffler (unsigned long) wpabuf_len(buf));
76739beb93cSSam Leffler return -1;
76839beb93cSSam Leffler }
76939beb93cSSam Leffler
77039beb93cSSam Leffler if (ikev2_validate_rx_state(data, hdr->exchange_type, message_id) < 0)
77139beb93cSSam Leffler return -1;
77239beb93cSSam Leffler
77339beb93cSSam Leffler if ((hdr->flags & (IKEV2_HDR_INITIATOR | IKEV2_HDR_RESPONSE)) !=
77439beb93cSSam Leffler IKEV2_HDR_INITIATOR) {
77539beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected Flags value 0x%x",
77639beb93cSSam Leffler hdr->flags);
77739beb93cSSam Leffler return -1;
77839beb93cSSam Leffler }
77939beb93cSSam Leffler
78039beb93cSSam Leffler if (data->state != SA_INIT) {
78139beb93cSSam Leffler if (os_memcmp(data->i_spi, hdr->i_spi, IKEV2_SPI_LEN) != 0) {
78239beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected IKE_SA "
78339beb93cSSam Leffler "Initiator's SPI");
78439beb93cSSam Leffler return -1;
78539beb93cSSam Leffler }
78639beb93cSSam Leffler if (os_memcmp(data->r_spi, hdr->r_spi, IKEV2_SPI_LEN) != 0) {
78739beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected IKE_SA "
78839beb93cSSam Leffler "Responder's SPI");
78939beb93cSSam Leffler return -1;
79039beb93cSSam Leffler }
79139beb93cSSam Leffler }
79239beb93cSSam Leffler
79339beb93cSSam Leffler pos = (const u8 *) (hdr + 1);
79439beb93cSSam Leffler if (ikev2_parse_payloads(&pl, hdr->next_payload, pos, end) < 0)
79539beb93cSSam Leffler return -1;
79639beb93cSSam Leffler
79739beb93cSSam Leffler if (data->state == SA_INIT) {
79839beb93cSSam Leffler data->last_msg = LAST_MSG_SA_INIT;
79939beb93cSSam Leffler if (ikev2_process_sa_init(data, hdr, &pl) < 0) {
80039beb93cSSam Leffler if (data->state == NOTIFY)
80139beb93cSSam Leffler return 0;
80239beb93cSSam Leffler return -1;
80339beb93cSSam Leffler }
80439beb93cSSam Leffler wpabuf_free(data->i_sign_msg);
80539beb93cSSam Leffler data->i_sign_msg = wpabuf_dup(buf);
80639beb93cSSam Leffler }
80739beb93cSSam Leffler
80839beb93cSSam Leffler if (data->state == SA_AUTH) {
80939beb93cSSam Leffler data->last_msg = LAST_MSG_SA_AUTH;
81039beb93cSSam Leffler if (ikev2_process_sa_auth(data, hdr, &pl) < 0) {
81139beb93cSSam Leffler if (data->state == NOTIFY)
81239beb93cSSam Leffler return 0;
81339beb93cSSam Leffler return -1;
81439beb93cSSam Leffler }
81539beb93cSSam Leffler }
81639beb93cSSam Leffler
81739beb93cSSam Leffler return 0;
81839beb93cSSam Leffler }
81939beb93cSSam Leffler
82039beb93cSSam Leffler
ikev2_build_hdr(struct ikev2_responder_data * data,struct wpabuf * msg,u8 exchange_type,u8 next_payload,u32 message_id)82139beb93cSSam Leffler static void ikev2_build_hdr(struct ikev2_responder_data *data,
82239beb93cSSam Leffler struct wpabuf *msg, u8 exchange_type,
82339beb93cSSam Leffler u8 next_payload, u32 message_id)
82439beb93cSSam Leffler {
82539beb93cSSam Leffler struct ikev2_hdr *hdr;
82639beb93cSSam Leffler
82739beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Adding HDR");
82839beb93cSSam Leffler
82939beb93cSSam Leffler /* HDR - RFC 4306, Sect. 3.1 */
83039beb93cSSam Leffler hdr = wpabuf_put(msg, sizeof(*hdr));
83139beb93cSSam Leffler os_memcpy(hdr->i_spi, data->i_spi, IKEV2_SPI_LEN);
83239beb93cSSam Leffler os_memcpy(hdr->r_spi, data->r_spi, IKEV2_SPI_LEN);
83339beb93cSSam Leffler hdr->next_payload = next_payload;
83439beb93cSSam Leffler hdr->version = IKEV2_VERSION;
83539beb93cSSam Leffler hdr->exchange_type = exchange_type;
83639beb93cSSam Leffler hdr->flags = IKEV2_HDR_RESPONSE;
83739beb93cSSam Leffler WPA_PUT_BE32(hdr->message_id, message_id);
83839beb93cSSam Leffler }
83939beb93cSSam Leffler
84039beb93cSSam Leffler
ikev2_build_sar1(struct ikev2_responder_data * data,struct wpabuf * msg,u8 next_payload)84139beb93cSSam Leffler static int ikev2_build_sar1(struct ikev2_responder_data *data,
84239beb93cSSam Leffler struct wpabuf *msg, u8 next_payload)
84339beb93cSSam Leffler {
84439beb93cSSam Leffler struct ikev2_payload_hdr *phdr;
84539beb93cSSam Leffler size_t plen;
84639beb93cSSam Leffler struct ikev2_proposal *p;
84739beb93cSSam Leffler struct ikev2_transform *t;
84839beb93cSSam Leffler
84939beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Adding SAr1 payload");
85039beb93cSSam Leffler
85139beb93cSSam Leffler /* SAr1 - RFC 4306, Sect. 2.7 and 3.3 */
85239beb93cSSam Leffler phdr = wpabuf_put(msg, sizeof(*phdr));
85339beb93cSSam Leffler phdr->next_payload = next_payload;
85439beb93cSSam Leffler phdr->flags = 0;
85539beb93cSSam Leffler
85639beb93cSSam Leffler p = wpabuf_put(msg, sizeof(*p));
85739beb93cSSam Leffler p->proposal_num = data->proposal.proposal_num;
85839beb93cSSam Leffler p->protocol_id = IKEV2_PROTOCOL_IKE;
85939beb93cSSam Leffler p->num_transforms = 4;
86039beb93cSSam Leffler
86139beb93cSSam Leffler t = wpabuf_put(msg, sizeof(*t));
86239beb93cSSam Leffler t->type = 3;
86339beb93cSSam Leffler t->transform_type = IKEV2_TRANSFORM_ENCR;
86439beb93cSSam Leffler WPA_PUT_BE16(t->transform_id, data->proposal.encr);
86539beb93cSSam Leffler if (data->proposal.encr == ENCR_AES_CBC) {
86639beb93cSSam Leffler /* Transform Attribute: Key Len = 128 bits */
86739beb93cSSam Leffler wpabuf_put_be16(msg, 0x800e); /* AF=1, AttrType=14 */
86839beb93cSSam Leffler wpabuf_put_be16(msg, 128); /* 128-bit key */
86939beb93cSSam Leffler }
87039beb93cSSam Leffler plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) t;
87139beb93cSSam Leffler WPA_PUT_BE16(t->transform_length, plen);
87239beb93cSSam Leffler
87339beb93cSSam Leffler t = wpabuf_put(msg, sizeof(*t));
87439beb93cSSam Leffler t->type = 3;
87539beb93cSSam Leffler WPA_PUT_BE16(t->transform_length, sizeof(*t));
87639beb93cSSam Leffler t->transform_type = IKEV2_TRANSFORM_PRF;
87739beb93cSSam Leffler WPA_PUT_BE16(t->transform_id, data->proposal.prf);
87839beb93cSSam Leffler
87939beb93cSSam Leffler t = wpabuf_put(msg, sizeof(*t));
88039beb93cSSam Leffler t->type = 3;
88139beb93cSSam Leffler WPA_PUT_BE16(t->transform_length, sizeof(*t));
88239beb93cSSam Leffler t->transform_type = IKEV2_TRANSFORM_INTEG;
88339beb93cSSam Leffler WPA_PUT_BE16(t->transform_id, data->proposal.integ);
88439beb93cSSam Leffler
88539beb93cSSam Leffler t = wpabuf_put(msg, sizeof(*t));
88639beb93cSSam Leffler WPA_PUT_BE16(t->transform_length, sizeof(*t));
88739beb93cSSam Leffler t->transform_type = IKEV2_TRANSFORM_DH;
88839beb93cSSam Leffler WPA_PUT_BE16(t->transform_id, data->proposal.dh);
88939beb93cSSam Leffler
89039beb93cSSam Leffler plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) p;
89139beb93cSSam Leffler WPA_PUT_BE16(p->proposal_length, plen);
89239beb93cSSam Leffler
89339beb93cSSam Leffler plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
89439beb93cSSam Leffler WPA_PUT_BE16(phdr->payload_length, plen);
89539beb93cSSam Leffler
89639beb93cSSam Leffler return 0;
89739beb93cSSam Leffler }
89839beb93cSSam Leffler
89939beb93cSSam Leffler
ikev2_build_ker(struct ikev2_responder_data * data,struct wpabuf * msg,u8 next_payload)90039beb93cSSam Leffler static int ikev2_build_ker(struct ikev2_responder_data *data,
90139beb93cSSam Leffler struct wpabuf *msg, u8 next_payload)
90239beb93cSSam Leffler {
90339beb93cSSam Leffler struct ikev2_payload_hdr *phdr;
90439beb93cSSam Leffler size_t plen;
90539beb93cSSam Leffler struct wpabuf *pv;
90639beb93cSSam Leffler
90739beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Adding KEr payload");
90839beb93cSSam Leffler
90939beb93cSSam Leffler pv = dh_init(data->dh, &data->r_dh_private);
91039beb93cSSam Leffler if (pv == NULL) {
91139beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Failed to initialize DH");
91239beb93cSSam Leffler return -1;
91339beb93cSSam Leffler }
91439beb93cSSam Leffler
91539beb93cSSam Leffler /* KEr - RFC 4306, Sect. 3.4 */
91639beb93cSSam Leffler phdr = wpabuf_put(msg, sizeof(*phdr));
91739beb93cSSam Leffler phdr->next_payload = next_payload;
91839beb93cSSam Leffler phdr->flags = 0;
91939beb93cSSam Leffler
92039beb93cSSam Leffler wpabuf_put_be16(msg, data->proposal.dh); /* DH Group # */
92139beb93cSSam Leffler wpabuf_put(msg, 2); /* RESERVED */
92239beb93cSSam Leffler /*
92339beb93cSSam Leffler * RFC 4306, Sect. 3.4: possible zero padding for public value to
92439beb93cSSam Leffler * match the length of the prime.
92539beb93cSSam Leffler */
92639beb93cSSam Leffler wpabuf_put(msg, data->dh->prime_len - wpabuf_len(pv));
92739beb93cSSam Leffler wpabuf_put_buf(msg, pv);
92839beb93cSSam Leffler wpabuf_free(pv);
92939beb93cSSam Leffler
93039beb93cSSam Leffler plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
93139beb93cSSam Leffler WPA_PUT_BE16(phdr->payload_length, plen);
93239beb93cSSam Leffler return 0;
93339beb93cSSam Leffler }
93439beb93cSSam Leffler
93539beb93cSSam Leffler
ikev2_build_nr(struct ikev2_responder_data * data,struct wpabuf * msg,u8 next_payload)93639beb93cSSam Leffler static int ikev2_build_nr(struct ikev2_responder_data *data,
93739beb93cSSam Leffler struct wpabuf *msg, u8 next_payload)
93839beb93cSSam Leffler {
93939beb93cSSam Leffler struct ikev2_payload_hdr *phdr;
94039beb93cSSam Leffler size_t plen;
94139beb93cSSam Leffler
94239beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Adding Nr payload");
94339beb93cSSam Leffler
94439beb93cSSam Leffler /* Nr - RFC 4306, Sect. 3.9 */
94539beb93cSSam Leffler phdr = wpabuf_put(msg, sizeof(*phdr));
94639beb93cSSam Leffler phdr->next_payload = next_payload;
94739beb93cSSam Leffler phdr->flags = 0;
94839beb93cSSam Leffler wpabuf_put_data(msg, data->r_nonce, data->r_nonce_len);
94939beb93cSSam Leffler plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
95039beb93cSSam Leffler WPA_PUT_BE16(phdr->payload_length, plen);
95139beb93cSSam Leffler return 0;
95239beb93cSSam Leffler }
95339beb93cSSam Leffler
95439beb93cSSam Leffler
ikev2_build_idr(struct ikev2_responder_data * data,struct wpabuf * msg,u8 next_payload)95539beb93cSSam Leffler static int ikev2_build_idr(struct ikev2_responder_data *data,
95639beb93cSSam Leffler struct wpabuf *msg, u8 next_payload)
95739beb93cSSam Leffler {
95839beb93cSSam Leffler struct ikev2_payload_hdr *phdr;
95939beb93cSSam Leffler size_t plen;
96039beb93cSSam Leffler
96139beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Adding IDr payload");
96239beb93cSSam Leffler
96339beb93cSSam Leffler if (data->IDr == NULL) {
96439beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: No IDr available");
96539beb93cSSam Leffler return -1;
96639beb93cSSam Leffler }
96739beb93cSSam Leffler
96839beb93cSSam Leffler /* IDr - RFC 4306, Sect. 3.5 */
96939beb93cSSam Leffler phdr = wpabuf_put(msg, sizeof(*phdr));
97039beb93cSSam Leffler phdr->next_payload = next_payload;
97139beb93cSSam Leffler phdr->flags = 0;
97239beb93cSSam Leffler wpabuf_put_u8(msg, ID_KEY_ID);
97339beb93cSSam Leffler wpabuf_put(msg, 3); /* RESERVED */
97439beb93cSSam Leffler wpabuf_put_data(msg, data->IDr, data->IDr_len);
97539beb93cSSam Leffler plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
97639beb93cSSam Leffler WPA_PUT_BE16(phdr->payload_length, plen);
97739beb93cSSam Leffler return 0;
97839beb93cSSam Leffler }
97939beb93cSSam Leffler
98039beb93cSSam Leffler
ikev2_build_auth(struct ikev2_responder_data * data,struct wpabuf * msg,u8 next_payload)98139beb93cSSam Leffler static int ikev2_build_auth(struct ikev2_responder_data *data,
98239beb93cSSam Leffler struct wpabuf *msg, u8 next_payload)
98339beb93cSSam Leffler {
98439beb93cSSam Leffler struct ikev2_payload_hdr *phdr;
98539beb93cSSam Leffler size_t plen;
98639beb93cSSam Leffler const struct ikev2_prf_alg *prf;
98739beb93cSSam Leffler
98839beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Adding AUTH payload");
98939beb93cSSam Leffler
99039beb93cSSam Leffler prf = ikev2_get_prf(data->proposal.prf);
99139beb93cSSam Leffler if (prf == NULL)
99239beb93cSSam Leffler return -1;
99339beb93cSSam Leffler
99439beb93cSSam Leffler /* Authentication - RFC 4306, Sect. 3.8 */
99539beb93cSSam Leffler phdr = wpabuf_put(msg, sizeof(*phdr));
99639beb93cSSam Leffler phdr->next_payload = next_payload;
99739beb93cSSam Leffler phdr->flags = 0;
99839beb93cSSam Leffler wpabuf_put_u8(msg, AUTH_SHARED_KEY_MIC);
99939beb93cSSam Leffler wpabuf_put(msg, 3); /* RESERVED */
100039beb93cSSam Leffler
100139beb93cSSam Leffler /* msg | Ni | prf(SK_pr,IDr') */
100239beb93cSSam Leffler if (ikev2_derive_auth_data(data->proposal.prf, data->r_sign_msg,
100339beb93cSSam Leffler data->IDr, data->IDr_len, ID_KEY_ID,
100439beb93cSSam Leffler &data->keys, 0, data->shared_secret,
100539beb93cSSam Leffler data->shared_secret_len,
100639beb93cSSam Leffler data->i_nonce, data->i_nonce_len,
100739beb93cSSam Leffler data->key_pad, data->key_pad_len,
100839beb93cSSam Leffler wpabuf_put(msg, prf->hash_len)) < 0) {
100939beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Could not derive AUTH data");
101039beb93cSSam Leffler return -1;
101139beb93cSSam Leffler }
101239beb93cSSam Leffler wpabuf_free(data->r_sign_msg);
101339beb93cSSam Leffler data->r_sign_msg = NULL;
101439beb93cSSam Leffler
101539beb93cSSam Leffler plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
101639beb93cSSam Leffler WPA_PUT_BE16(phdr->payload_length, plen);
101739beb93cSSam Leffler return 0;
101839beb93cSSam Leffler }
101939beb93cSSam Leffler
102039beb93cSSam Leffler
ikev2_build_notification(struct ikev2_responder_data * data,struct wpabuf * msg,u8 next_payload)102139beb93cSSam Leffler static int ikev2_build_notification(struct ikev2_responder_data *data,
102239beb93cSSam Leffler struct wpabuf *msg, u8 next_payload)
102339beb93cSSam Leffler {
102439beb93cSSam Leffler struct ikev2_payload_hdr *phdr;
102539beb93cSSam Leffler size_t plen;
102639beb93cSSam Leffler
102739beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Adding Notification payload");
102839beb93cSSam Leffler
102939beb93cSSam Leffler if (data->error_type == 0) {
103039beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: No Notify Message Type "
103139beb93cSSam Leffler "available");
103239beb93cSSam Leffler return -1;
103339beb93cSSam Leffler }
103439beb93cSSam Leffler
103539beb93cSSam Leffler /* Notify - RFC 4306, Sect. 3.10 */
103639beb93cSSam Leffler phdr = wpabuf_put(msg, sizeof(*phdr));
103739beb93cSSam Leffler phdr->next_payload = next_payload;
103839beb93cSSam Leffler phdr->flags = 0;
103939beb93cSSam Leffler wpabuf_put_u8(msg, 0); /* Protocol ID: no existing SA */
104039beb93cSSam Leffler wpabuf_put_u8(msg, 0); /* SPI Size */
104139beb93cSSam Leffler wpabuf_put_be16(msg, data->error_type);
104239beb93cSSam Leffler
104339beb93cSSam Leffler switch (data->error_type) {
104439beb93cSSam Leffler case INVALID_KE_PAYLOAD:
104539beb93cSSam Leffler if (data->proposal.dh == -1) {
104639beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: No DH Group selected for "
104739beb93cSSam Leffler "INVALID_KE_PAYLOAD notifications");
104839beb93cSSam Leffler return -1;
104939beb93cSSam Leffler }
105039beb93cSSam Leffler wpabuf_put_be16(msg, data->proposal.dh);
105139beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: INVALID_KE_PAYLOAD - request "
105239beb93cSSam Leffler "DH Group #%d", data->proposal.dh);
105339beb93cSSam Leffler break;
105439beb93cSSam Leffler case AUTHENTICATION_FAILED:
105539beb93cSSam Leffler /* no associated data */
105639beb93cSSam Leffler break;
105739beb93cSSam Leffler default:
105839beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unsupported Notify Message Type "
105939beb93cSSam Leffler "%d", data->error_type);
106039beb93cSSam Leffler return -1;
106139beb93cSSam Leffler }
106239beb93cSSam Leffler
106339beb93cSSam Leffler plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
106439beb93cSSam Leffler WPA_PUT_BE16(phdr->payload_length, plen);
106539beb93cSSam Leffler return 0;
106639beb93cSSam Leffler }
106739beb93cSSam Leffler
106839beb93cSSam Leffler
ikev2_build_sa_init(struct ikev2_responder_data * data)106939beb93cSSam Leffler static struct wpabuf * ikev2_build_sa_init(struct ikev2_responder_data *data)
107039beb93cSSam Leffler {
107139beb93cSSam Leffler struct wpabuf *msg;
107239beb93cSSam Leffler
107339beb93cSSam Leffler /* build IKE_SA_INIT: HDR, SAr1, KEr, Nr, [CERTREQ], [SK{IDr}] */
107439beb93cSSam Leffler
107539beb93cSSam Leffler if (os_get_random(data->r_spi, IKEV2_SPI_LEN))
107639beb93cSSam Leffler return NULL;
107739beb93cSSam Leffler wpa_hexdump(MSG_DEBUG, "IKEV2: IKE_SA Responder's SPI",
107839beb93cSSam Leffler data->r_spi, IKEV2_SPI_LEN);
107939beb93cSSam Leffler
108039beb93cSSam Leffler data->r_nonce_len = IKEV2_NONCE_MIN_LEN;
1081f05cddf9SRui Paulo if (random_get_bytes(data->r_nonce, data->r_nonce_len))
108239beb93cSSam Leffler return NULL;
108339beb93cSSam Leffler wpa_hexdump(MSG_DEBUG, "IKEV2: Nr", data->r_nonce, data->r_nonce_len);
108439beb93cSSam Leffler
108539beb93cSSam Leffler msg = wpabuf_alloc(sizeof(struct ikev2_hdr) + data->IDr_len + 1500);
108639beb93cSSam Leffler if (msg == NULL)
108739beb93cSSam Leffler return NULL;
108839beb93cSSam Leffler
108939beb93cSSam Leffler ikev2_build_hdr(data, msg, IKE_SA_INIT, IKEV2_PAYLOAD_SA, 0);
109039beb93cSSam Leffler if (ikev2_build_sar1(data, msg, IKEV2_PAYLOAD_KEY_EXCHANGE) ||
109139beb93cSSam Leffler ikev2_build_ker(data, msg, IKEV2_PAYLOAD_NONCE) ||
109239beb93cSSam Leffler ikev2_build_nr(data, msg, data->peer_auth == PEER_AUTH_SECRET ?
109339beb93cSSam Leffler IKEV2_PAYLOAD_ENCRYPTED :
109439beb93cSSam Leffler IKEV2_PAYLOAD_NO_NEXT_PAYLOAD)) {
109539beb93cSSam Leffler wpabuf_free(msg);
109639beb93cSSam Leffler return NULL;
109739beb93cSSam Leffler }
109839beb93cSSam Leffler
109939beb93cSSam Leffler if (ikev2_derive_keys(data)) {
110039beb93cSSam Leffler wpabuf_free(msg);
110139beb93cSSam Leffler return NULL;
110239beb93cSSam Leffler }
110339beb93cSSam Leffler
110439beb93cSSam Leffler if (data->peer_auth == PEER_AUTH_CERT) {
110539beb93cSSam Leffler /* TODO: CERTREQ with SHA-1 hashes of Subject Public Key Info
110639beb93cSSam Leffler * for trust agents */
110739beb93cSSam Leffler }
110839beb93cSSam Leffler
110939beb93cSSam Leffler if (data->peer_auth == PEER_AUTH_SECRET) {
111039beb93cSSam Leffler struct wpabuf *plain = wpabuf_alloc(data->IDr_len + 1000);
111139beb93cSSam Leffler if (plain == NULL) {
111239beb93cSSam Leffler wpabuf_free(msg);
111339beb93cSSam Leffler return NULL;
111439beb93cSSam Leffler }
111539beb93cSSam Leffler if (ikev2_build_idr(data, plain,
111639beb93cSSam Leffler IKEV2_PAYLOAD_NO_NEXT_PAYLOAD) ||
111739beb93cSSam Leffler ikev2_build_encrypted(data->proposal.encr,
111839beb93cSSam Leffler data->proposal.integ,
111939beb93cSSam Leffler &data->keys, 0, msg, plain,
112039beb93cSSam Leffler IKEV2_PAYLOAD_IDr)) {
112139beb93cSSam Leffler wpabuf_free(plain);
112239beb93cSSam Leffler wpabuf_free(msg);
112339beb93cSSam Leffler return NULL;
112439beb93cSSam Leffler }
112539beb93cSSam Leffler wpabuf_free(plain);
112639beb93cSSam Leffler }
112739beb93cSSam Leffler
112839beb93cSSam Leffler ikev2_update_hdr(msg);
112939beb93cSSam Leffler
113039beb93cSSam Leffler wpa_hexdump_buf(MSG_MSGDUMP, "IKEV2: Sending message (SA_INIT)", msg);
113139beb93cSSam Leffler
113239beb93cSSam Leffler data->state = SA_AUTH;
113339beb93cSSam Leffler
113439beb93cSSam Leffler wpabuf_free(data->r_sign_msg);
113539beb93cSSam Leffler data->r_sign_msg = wpabuf_dup(msg);
113639beb93cSSam Leffler
113739beb93cSSam Leffler return msg;
113839beb93cSSam Leffler }
113939beb93cSSam Leffler
114039beb93cSSam Leffler
ikev2_build_sa_auth(struct ikev2_responder_data * data)114139beb93cSSam Leffler static struct wpabuf * ikev2_build_sa_auth(struct ikev2_responder_data *data)
114239beb93cSSam Leffler {
114339beb93cSSam Leffler struct wpabuf *msg, *plain;
114439beb93cSSam Leffler
114539beb93cSSam Leffler /* build IKE_SA_AUTH: HDR, SK {IDr, [CERT,] AUTH} */
114639beb93cSSam Leffler
114739beb93cSSam Leffler msg = wpabuf_alloc(sizeof(struct ikev2_hdr) + data->IDr_len + 1000);
114839beb93cSSam Leffler if (msg == NULL)
114939beb93cSSam Leffler return NULL;
115039beb93cSSam Leffler ikev2_build_hdr(data, msg, IKE_SA_AUTH, IKEV2_PAYLOAD_ENCRYPTED, 1);
115139beb93cSSam Leffler
115239beb93cSSam Leffler plain = wpabuf_alloc(data->IDr_len + 1000);
115339beb93cSSam Leffler if (plain == NULL) {
115439beb93cSSam Leffler wpabuf_free(msg);
115539beb93cSSam Leffler return NULL;
115639beb93cSSam Leffler }
115739beb93cSSam Leffler
115839beb93cSSam Leffler if (ikev2_build_idr(data, plain, IKEV2_PAYLOAD_AUTHENTICATION) ||
115939beb93cSSam Leffler ikev2_build_auth(data, plain, IKEV2_PAYLOAD_NO_NEXT_PAYLOAD) ||
116039beb93cSSam Leffler ikev2_build_encrypted(data->proposal.encr, data->proposal.integ,
116139beb93cSSam Leffler &data->keys, 0, msg, plain,
116239beb93cSSam Leffler IKEV2_PAYLOAD_IDr)) {
116339beb93cSSam Leffler wpabuf_free(plain);
116439beb93cSSam Leffler wpabuf_free(msg);
116539beb93cSSam Leffler return NULL;
116639beb93cSSam Leffler }
116739beb93cSSam Leffler wpabuf_free(plain);
116839beb93cSSam Leffler
116939beb93cSSam Leffler wpa_hexdump_buf(MSG_MSGDUMP, "IKEV2: Sending message (SA_AUTH)", msg);
117039beb93cSSam Leffler
117139beb93cSSam Leffler data->state = IKEV2_DONE;
117239beb93cSSam Leffler
117339beb93cSSam Leffler return msg;
117439beb93cSSam Leffler }
117539beb93cSSam Leffler
117639beb93cSSam Leffler
ikev2_build_notify(struct ikev2_responder_data * data)117739beb93cSSam Leffler static struct wpabuf * ikev2_build_notify(struct ikev2_responder_data *data)
117839beb93cSSam Leffler {
117939beb93cSSam Leffler struct wpabuf *msg;
118039beb93cSSam Leffler
118139beb93cSSam Leffler msg = wpabuf_alloc(sizeof(struct ikev2_hdr) + 1000);
118239beb93cSSam Leffler if (msg == NULL)
118339beb93cSSam Leffler return NULL;
118439beb93cSSam Leffler if (data->last_msg == LAST_MSG_SA_AUTH) {
118539beb93cSSam Leffler /* HDR, SK{N} */
118639beb93cSSam Leffler struct wpabuf *plain = wpabuf_alloc(100);
118739beb93cSSam Leffler if (plain == NULL) {
118839beb93cSSam Leffler wpabuf_free(msg);
118939beb93cSSam Leffler return NULL;
119039beb93cSSam Leffler }
119139beb93cSSam Leffler ikev2_build_hdr(data, msg, IKE_SA_AUTH,
119239beb93cSSam Leffler IKEV2_PAYLOAD_ENCRYPTED, 1);
119339beb93cSSam Leffler if (ikev2_build_notification(data, plain,
119439beb93cSSam Leffler IKEV2_PAYLOAD_NO_NEXT_PAYLOAD) ||
119539beb93cSSam Leffler ikev2_build_encrypted(data->proposal.encr,
119639beb93cSSam Leffler data->proposal.integ,
119739beb93cSSam Leffler &data->keys, 0, msg, plain,
119839beb93cSSam Leffler IKEV2_PAYLOAD_NOTIFICATION)) {
119939beb93cSSam Leffler wpabuf_free(plain);
120039beb93cSSam Leffler wpabuf_free(msg);
120139beb93cSSam Leffler return NULL;
120239beb93cSSam Leffler }
12035b9c547cSRui Paulo wpabuf_free(plain);
120439beb93cSSam Leffler data->state = IKEV2_FAILED;
120539beb93cSSam Leffler } else {
120639beb93cSSam Leffler /* HDR, N */
120739beb93cSSam Leffler ikev2_build_hdr(data, msg, IKE_SA_INIT,
120839beb93cSSam Leffler IKEV2_PAYLOAD_NOTIFICATION, 0);
120939beb93cSSam Leffler if (ikev2_build_notification(data, msg,
121039beb93cSSam Leffler IKEV2_PAYLOAD_NO_NEXT_PAYLOAD)) {
121139beb93cSSam Leffler wpabuf_free(msg);
121239beb93cSSam Leffler return NULL;
121339beb93cSSam Leffler }
121439beb93cSSam Leffler data->state = SA_INIT;
121539beb93cSSam Leffler }
121639beb93cSSam Leffler
121739beb93cSSam Leffler ikev2_update_hdr(msg);
121839beb93cSSam Leffler
121939beb93cSSam Leffler wpa_hexdump_buf(MSG_MSGDUMP, "IKEV2: Sending message (Notification)",
122039beb93cSSam Leffler msg);
122139beb93cSSam Leffler
122239beb93cSSam Leffler return msg;
122339beb93cSSam Leffler }
122439beb93cSSam Leffler
122539beb93cSSam Leffler
ikev2_responder_build(struct ikev2_responder_data * data)122639beb93cSSam Leffler struct wpabuf * ikev2_responder_build(struct ikev2_responder_data *data)
122739beb93cSSam Leffler {
122839beb93cSSam Leffler switch (data->state) {
122939beb93cSSam Leffler case SA_INIT:
123039beb93cSSam Leffler return ikev2_build_sa_init(data);
123139beb93cSSam Leffler case SA_AUTH:
123239beb93cSSam Leffler return ikev2_build_sa_auth(data);
123339beb93cSSam Leffler case CHILD_SA:
123439beb93cSSam Leffler return NULL;
123539beb93cSSam Leffler case NOTIFY:
123639beb93cSSam Leffler return ikev2_build_notify(data);
123739beb93cSSam Leffler case IKEV2_DONE:
123839beb93cSSam Leffler case IKEV2_FAILED:
123939beb93cSSam Leffler return NULL;
124039beb93cSSam Leffler }
124139beb93cSSam Leffler return NULL;
124239beb93cSSam Leffler }
1243