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