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