2 * IKEv2 responder (RFC 4306) for EAP-IKEV2
3 * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/dh_groups.h"
13 #include "crypto/random.h"
17 void ikev2_responder_deinit(struct ikev2_responder_data *data)
19 ikev2_free_keys(&data->keys);
20 wpabuf_free(data->i_dh_public);
21 wpabuf_free(data->r_dh_private);
24 os_free(data->shared_secret);
25 wpabuf_free(data->i_sign_msg);
26 wpabuf_free(data->r_sign_msg);
27 os_free(data->key_pad);
31 static int ikev2_derive_keys(struct ikev2_responder_data *data)
33 u8 *buf, *pos, *pad, skeyseed[IKEV2_MAX_HASH_LEN];
34 size_t buf_len, pad_len;
35 struct wpabuf *shared;
36 const struct ikev2_integ_alg *integ;
37 const struct ikev2_prf_alg *prf;
38 const struct ikev2_encr_alg *encr;
43 /* RFC 4306, Sect. 2.14 */
45 integ = ikev2_get_integ(data->proposal.integ);
46 prf = ikev2_get_prf(data->proposal.prf);
47 encr = ikev2_get_encr(data->proposal.encr);
48 if (integ == NULL || prf == NULL || encr == NULL) {
49 wpa_printf(MSG_INFO, "IKEV2: Unsupported proposal");
53 shared = dh_derive_shared(data->i_dh_public, data->r_dh_private,
58 /* Construct Ni | Nr | SPIi | SPIr */
60 buf_len = data->i_nonce_len + data->r_nonce_len + 2 * IKEV2_SPI_LEN;
61 buf = os_malloc(buf_len);
68 os_memcpy(pos, data->i_nonce, data->i_nonce_len);
69 pos += data->i_nonce_len;
70 os_memcpy(pos, data->r_nonce, data->r_nonce_len);
71 pos += data->r_nonce_len;
72 os_memcpy(pos, data->i_spi, IKEV2_SPI_LEN);
74 os_memcpy(pos, data->r_spi, IKEV2_SPI_LEN);
76 #if __BYTE_ORDER == __LITTLE_ENDIAN
79 u8 *tmp = pos - IKEV2_SPI_LEN;
80 /* Incorrect byte re-ordering on little endian hosts.. */
81 for (i = 0; i < IKEV2_SPI_LEN; i++)
82 *tmp++ = data->i_spi[IKEV2_SPI_LEN - 1 - i];
83 for (i = 0; i < IKEV2_SPI_LEN; i++)
84 *tmp++ = data->r_spi[IKEV2_SPI_LEN - 1 - i];
89 /* SKEYSEED = prf(Ni | Nr, g^ir) */
90 /* Use zero-padding per RFC 4306, Sect. 2.14 */
91 pad_len = data->dh->prime_len - wpabuf_len(shared);
93 /* Shared secret is not zero-padded correctly */
96 pad = os_zalloc(pad_len ? pad_len : 1);
105 addr[1] = wpabuf_head(shared);
106 len[1] = wpabuf_len(shared);
107 if (ikev2_prf_hash(prf->id, buf, data->i_nonce_len + data->r_nonce_len,
108 2, addr, len, skeyseed) < 0) {
117 /* DH parameters are not needed anymore, so free them */
118 wpabuf_free(data->i_dh_public);
119 data->i_dh_public = NULL;
120 wpabuf_free(data->r_dh_private);
121 data->r_dh_private = NULL;
123 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SKEYSEED",
124 skeyseed, prf->hash_len);
126 ret = ikev2_derive_sk_keys(prf, integ, encr, skeyseed, buf, buf_len,
133 static int ikev2_parse_transform(struct ikev2_proposal_data *prop,
134 const u8 *pos, const u8 *end)
137 const struct ikev2_transform *t;
141 if (end - pos < (int) sizeof(*t)) {
142 wpa_printf(MSG_INFO, "IKEV2: Too short transform");
146 t = (const struct ikev2_transform *) pos;
147 transform_len = WPA_GET_BE16(t->transform_length);
148 if (transform_len < (int) sizeof(*t) || pos + transform_len > end) {
149 wpa_printf(MSG_INFO, "IKEV2: Invalid transform length %d",
153 tend = pos + transform_len;
155 transform_id = WPA_GET_BE16(t->transform_id);
157 wpa_printf(MSG_DEBUG, "IKEV2: Transform:");
158 wpa_printf(MSG_DEBUG, "IKEV2: Type: %d Transform Length: %d "
159 "Transform Type: %d Transform ID: %d",
160 t->type, transform_len, t->transform_type, transform_id);
162 if (t->type != 0 && t->type != 3) {
163 wpa_printf(MSG_INFO, "IKEV2: Unexpected Transform type");
167 pos = (const u8 *) (t + 1);
169 wpa_hexdump(MSG_DEBUG, "IKEV2: Transform Attributes",
173 switch (t->transform_type) {
174 case IKEV2_TRANSFORM_ENCR:
175 if (ikev2_get_encr(transform_id)) {
176 if (transform_id == ENCR_AES_CBC) {
177 if (tend - pos != 4) {
178 wpa_printf(MSG_DEBUG, "IKEV2: No "
179 "Transform Attr for AES");
183 if (WPA_GET_BE16(pos) != 0x001d /* ?? */) {
184 wpa_printf(MSG_DEBUG, "IKEV2: Not a "
185 "Key Size attribute for "
190 if (WPA_GET_BE16(pos) != 0x800e) {
191 wpa_printf(MSG_DEBUG, "IKEV2: Not a "
192 "Key Size attribute for "
197 if (WPA_GET_BE16(pos + 2) != 128) {
198 wpa_printf(MSG_DEBUG, "IKEV2: "
199 "Unsupported AES key size "
201 WPA_GET_BE16(pos + 2));
205 prop->encr = transform_id;
208 case IKEV2_TRANSFORM_PRF:
209 if (ikev2_get_prf(transform_id))
210 prop->prf = transform_id;
212 case IKEV2_TRANSFORM_INTEG:
213 if (ikev2_get_integ(transform_id))
214 prop->integ = transform_id;
216 case IKEV2_TRANSFORM_DH:
217 if (dh_groups_get(transform_id))
218 prop->dh = transform_id;
222 return transform_len;
226 static int ikev2_parse_proposal(struct ikev2_proposal_data *prop,
227 const u8 *pos, const u8 *end)
229 const u8 *pend, *ppos;
231 const struct ikev2_proposal *p;
233 if (end - pos < (int) sizeof(*p)) {
234 wpa_printf(MSG_INFO, "IKEV2: Too short proposal");
238 /* FIX: AND processing if multiple proposals use the same # */
240 p = (const struct ikev2_proposal *) pos;
241 proposal_len = WPA_GET_BE16(p->proposal_length);
242 if (proposal_len < (int) sizeof(*p) || pos + proposal_len > end) {
243 wpa_printf(MSG_INFO, "IKEV2: Invalid proposal length %d",
247 wpa_printf(MSG_DEBUG, "IKEV2: SAi1 Proposal # %d",
249 wpa_printf(MSG_DEBUG, "IKEV2: Type: %d Proposal Length: %d "
251 p->type, proposal_len, p->protocol_id);
252 wpa_printf(MSG_DEBUG, "IKEV2: SPI Size: %d Transforms: %d",
253 p->spi_size, p->num_transforms);
255 if (p->type != 0 && p->type != 2) {
256 wpa_printf(MSG_INFO, "IKEV2: Unexpected Proposal type");
260 if (p->protocol_id != IKEV2_PROTOCOL_IKE) {
261 wpa_printf(MSG_DEBUG, "IKEV2: Unexpected Protocol ID "
262 "(only IKE allowed for EAP-IKEv2)");
266 if (p->proposal_num != prop->proposal_num) {
267 if (p->proposal_num == prop->proposal_num + 1)
268 prop->proposal_num = p->proposal_num;
270 wpa_printf(MSG_INFO, "IKEV2: Unexpected Proposal #");
275 ppos = (const u8 *) (p + 1);
276 pend = pos + proposal_len;
277 if (ppos + p->spi_size > pend) {
278 wpa_printf(MSG_INFO, "IKEV2: Not enough room for SPI "
283 wpa_hexdump(MSG_DEBUG, "IKEV2: SPI",
289 * For initial IKE_SA negotiation, SPI Size MUST be zero; for
290 * subsequent negotiations, it must be 8 for IKE. We only support
291 * initial case for now.
293 if (p->spi_size != 0) {
294 wpa_printf(MSG_INFO, "IKEV2: Unexpected SPI Size");
298 if (p->num_transforms == 0) {
299 wpa_printf(MSG_INFO, "IKEV2: At least one transform required");
303 for (i = 0; i < (int) p->num_transforms; i++) {
304 int tlen = ikev2_parse_transform(prop, ppos, pend);
311 wpa_printf(MSG_INFO, "IKEV2: Unexpected data after "
320 static int ikev2_process_sai1(struct ikev2_responder_data *data,
321 const u8 *sai1, size_t sai1_len)
323 struct ikev2_proposal_data prop;
327 /* Security Association Payloads: <Proposals> */
330 wpa_printf(MSG_INFO, "IKEV2: SAi1 not received");
334 os_memset(&prop, 0, sizeof(prop));
335 prop.proposal_num = 1;
338 end = sai1 + sai1_len;
347 plen = ikev2_parse_proposal(&prop, pos, end);
351 if (!found && prop.integ != -1 && prop.prf != -1 &&
352 prop.encr != -1 && prop.dh != -1) {
353 os_memcpy(&data->proposal, &prop, sizeof(prop));
354 data->dh = dh_groups_get(prop.dh);
362 wpa_printf(MSG_INFO, "IKEV2: Unexpected data after proposals");
367 wpa_printf(MSG_INFO, "IKEV2: No acceptable proposal found");
371 wpa_printf(MSG_DEBUG, "IKEV2: Accepted proposal #%d: ENCR:%d PRF:%d "
372 "INTEG:%d D-H:%d", data->proposal.proposal_num,
373 data->proposal.encr, data->proposal.prf,
374 data->proposal.integ, data->proposal.dh);
380 static int ikev2_process_kei(struct ikev2_responder_data *data,
381 const u8 *kei, size_t kei_len)
386 * Key Exchange Payload:
387 * DH Group # (16 bits)
389 * Key Exchange Data (Diffie-Hellman public value)
393 wpa_printf(MSG_INFO, "IKEV2: KEi not received");
397 if (kei_len < 4 + 96) {
398 wpa_printf(MSG_INFO, "IKEV2: Too show Key Exchange Payload");
402 group = WPA_GET_BE16(kei);
403 wpa_printf(MSG_DEBUG, "IKEV2: KEi DH Group #%u", group);
405 if (group != data->proposal.dh) {
406 wpa_printf(MSG_DEBUG, "IKEV2: KEi DH Group #%u does not match "
407 "with the selected proposal (%u)",
408 group, data->proposal.dh);
409 /* Reject message with Notify payload of type
410 * INVALID_KE_PAYLOAD (RFC 4306, Sect. 3.4) */
411 data->error_type = INVALID_KE_PAYLOAD;
412 data->state = NOTIFY;
416 if (data->dh == NULL) {
417 wpa_printf(MSG_INFO, "IKEV2: Unsupported DH group");
421 /* RFC 4306, Section 3.4:
422 * The length of DH public value MUST be equal to the length of the
425 if (kei_len - 4 != data->dh->prime_len) {
426 wpa_printf(MSG_INFO, "IKEV2: Invalid DH public value length "
427 "%ld (expected %ld)",
428 (long) (kei_len - 4), (long) data->dh->prime_len);
432 wpabuf_free(data->i_dh_public);
433 data->i_dh_public = wpabuf_alloc(kei_len - 4);
434 if (data->i_dh_public == NULL)
436 wpabuf_put_data(data->i_dh_public, kei + 4, kei_len - 4);
438 wpa_hexdump_buf(MSG_DEBUG, "IKEV2: KEi Diffie-Hellman Public Value",
445 static int ikev2_process_ni(struct ikev2_responder_data *data,
446 const u8 *ni, size_t ni_len)
449 wpa_printf(MSG_INFO, "IKEV2: Ni not received");
453 if (ni_len < IKEV2_NONCE_MIN_LEN || ni_len > IKEV2_NONCE_MAX_LEN) {
454 wpa_printf(MSG_INFO, "IKEV2: Invalid Ni length %ld",
460 /* Zeros are removed incorrectly from the beginning of the nonces */
461 while (ni_len > 1 && *ni == 0) {
467 data->i_nonce_len = ni_len;
468 os_memcpy(data->i_nonce, ni, ni_len);
469 wpa_hexdump(MSG_MSGDUMP, "IKEV2: Ni",
470 data->i_nonce, data->i_nonce_len);
476 static int ikev2_process_sa_init(struct ikev2_responder_data *data,
477 const struct ikev2_hdr *hdr,
478 struct ikev2_payloads *pl)
480 if (ikev2_process_sai1(data, pl->sa, pl->sa_len) < 0 ||
481 ikev2_process_kei(data, pl->ke, pl->ke_len) < 0 ||
482 ikev2_process_ni(data, pl->nonce, pl->nonce_len) < 0)
485 os_memcpy(data->i_spi, hdr->i_spi, IKEV2_SPI_LEN);
491 static int ikev2_process_idi(struct ikev2_responder_data *data,
492 const u8 *idi, size_t idi_len)
497 wpa_printf(MSG_INFO, "IKEV2: No IDi received");
502 wpa_printf(MSG_INFO, "IKEV2: Too short IDi payload");
510 wpa_printf(MSG_DEBUG, "IKEV2: IDi ID Type %d", id_type);
511 wpa_hexdump_ascii(MSG_DEBUG, "IKEV2: IDi", idi, idi_len);
513 data->IDi = os_malloc(idi_len);
514 if (data->IDi == NULL)
516 os_memcpy(data->IDi, idi, idi_len);
517 data->IDi_len = idi_len;
518 data->IDi_type = id_type;
524 static int ikev2_process_cert(struct ikev2_responder_data *data,
525 const u8 *cert, size_t cert_len)
530 if (data->peer_auth == PEER_AUTH_CERT) {
531 wpa_printf(MSG_INFO, "IKEV2: No Certificate received");
538 wpa_printf(MSG_INFO, "IKEV2: No Cert Encoding field");
542 cert_encoding = cert[0];
546 wpa_printf(MSG_DEBUG, "IKEV2: Cert Encoding %d", cert_encoding);
547 wpa_hexdump(MSG_MSGDUMP, "IKEV2: Certificate Data", cert, cert_len);
549 /* TODO: validate certificate */
555 static int ikev2_process_auth_cert(struct ikev2_responder_data *data,
556 u8 method, const u8 *auth, size_t auth_len)
558 if (method != AUTH_RSA_SIGN) {
559 wpa_printf(MSG_INFO, "IKEV2: Unsupported authentication "
560 "method %d", method);
564 /* TODO: validate AUTH */
569 static int ikev2_process_auth_secret(struct ikev2_responder_data *data,
570 u8 method, const u8 *auth,
573 u8 auth_data[IKEV2_MAX_HASH_LEN];
574 const struct ikev2_prf_alg *prf;
576 if (method != AUTH_SHARED_KEY_MIC) {
577 wpa_printf(MSG_INFO, "IKEV2: Unsupported authentication "
578 "method %d", method);
582 /* msg | Nr | prf(SK_pi,IDi') */
583 if (ikev2_derive_auth_data(data->proposal.prf, data->i_sign_msg,
584 data->IDi, data->IDi_len, data->IDi_type,
585 &data->keys, 1, data->shared_secret,
586 data->shared_secret_len,
587 data->r_nonce, data->r_nonce_len,
588 data->key_pad, data->key_pad_len,
590 wpa_printf(MSG_INFO, "IKEV2: Could not derive AUTH data");
594 wpabuf_free(data->i_sign_msg);
595 data->i_sign_msg = NULL;
597 prf = ikev2_get_prf(data->proposal.prf);
601 if (auth_len != prf->hash_len ||
602 os_memcmp(auth, auth_data, auth_len) != 0) {
603 wpa_printf(MSG_INFO, "IKEV2: Invalid Authentication Data");
604 wpa_hexdump(MSG_DEBUG, "IKEV2: Received Authentication Data",
606 wpa_hexdump(MSG_DEBUG, "IKEV2: Expected Authentication Data",
607 auth_data, prf->hash_len);
608 data->error_type = AUTHENTICATION_FAILED;
609 data->state = NOTIFY;
613 wpa_printf(MSG_DEBUG, "IKEV2: Server authenticated successfully "
614 "using shared keys");
620 static int ikev2_process_auth(struct ikev2_responder_data *data,
621 const u8 *auth, size_t auth_len)
626 wpa_printf(MSG_INFO, "IKEV2: No Authentication Payload");
631 wpa_printf(MSG_INFO, "IKEV2: Too short Authentication "
636 auth_method = auth[0];
640 wpa_printf(MSG_DEBUG, "IKEV2: Auth Method %d", auth_method);
641 wpa_hexdump(MSG_MSGDUMP, "IKEV2: Authentication Data", auth, auth_len);
643 switch (data->peer_auth) {
645 return ikev2_process_auth_cert(data, auth_method, auth,
647 case PEER_AUTH_SECRET:
648 return ikev2_process_auth_secret(data, auth_method, auth,
656 static int ikev2_process_sa_auth_decrypted(struct ikev2_responder_data *data,
658 u8 *payload, size_t payload_len)
660 struct ikev2_payloads pl;
662 wpa_printf(MSG_DEBUG, "IKEV2: Processing decrypted payloads");
664 if (ikev2_parse_payloads(&pl, next_payload, payload, payload +
666 wpa_printf(MSG_INFO, "IKEV2: Failed to parse decrypted "
671 if (ikev2_process_idi(data, pl.idi, pl.idi_len) < 0 ||
672 ikev2_process_cert(data, pl.cert, pl.cert_len) < 0 ||
673 ikev2_process_auth(data, pl.auth, pl.auth_len) < 0)
680 static int ikev2_process_sa_auth(struct ikev2_responder_data *data,
681 const struct ikev2_hdr *hdr,
682 struct ikev2_payloads *pl)
685 size_t decrypted_len;
688 decrypted = ikev2_decrypt_payload(data->proposal.encr,
689 data->proposal.integ,
690 &data->keys, 1, hdr, pl->encrypted,
691 pl->encrypted_len, &decrypted_len);
692 if (decrypted == NULL)
695 ret = ikev2_process_sa_auth_decrypted(data, pl->encr_next_payload,
696 decrypted, decrypted_len);
703 static int ikev2_validate_rx_state(struct ikev2_responder_data *data,
704 u8 exchange_type, u32 message_id)
706 switch (data->state) {
708 /* Expect to receive IKE_SA_INIT: HDR, SAi1, KEi, Ni */
709 if (exchange_type != IKE_SA_INIT) {
710 wpa_printf(MSG_INFO, "IKEV2: Unexpected Exchange Type "
711 "%u in SA_INIT state", exchange_type);
714 if (message_id != 0) {
715 wpa_printf(MSG_INFO, "IKEV2: Unexpected Message ID %u "
716 "in SA_INIT state", message_id);
721 /* Expect to receive IKE_SA_AUTH:
722 * HDR, SK {IDi, [CERT,] [CERTREQ,] [IDr,]
723 * AUTH, SAi2, TSi, TSr}
725 if (exchange_type != IKE_SA_AUTH) {
726 wpa_printf(MSG_INFO, "IKEV2: Unexpected Exchange Type "
727 "%u in SA_AUTH state", exchange_type);
730 if (message_id != 1) {
731 wpa_printf(MSG_INFO, "IKEV2: Unexpected Message ID %u "
732 "in SA_AUTH state", message_id);
737 if (exchange_type != CREATE_CHILD_SA) {
738 wpa_printf(MSG_INFO, "IKEV2: Unexpected Exchange Type "
739 "%u in CHILD_SA state", exchange_type);
742 if (message_id != 2) {
743 wpa_printf(MSG_INFO, "IKEV2: Unexpected Message ID %u "
744 "in CHILD_SA state", message_id);
758 int ikev2_responder_process(struct ikev2_responder_data *data,
759 const struct wpabuf *buf)
761 const struct ikev2_hdr *hdr;
762 u32 length, message_id;
764 struct ikev2_payloads pl;
766 wpa_printf(MSG_MSGDUMP, "IKEV2: Received message (len %lu)",
767 (unsigned long) wpabuf_len(buf));
769 if (wpabuf_len(buf) < sizeof(*hdr)) {
770 wpa_printf(MSG_INFO, "IKEV2: Too short frame to include HDR");
774 data->error_type = 0;
775 hdr = (const struct ikev2_hdr *) wpabuf_head(buf);
776 end = wpabuf_head_u8(buf) + wpabuf_len(buf);
777 message_id = WPA_GET_BE32(hdr->message_id);
778 length = WPA_GET_BE32(hdr->length);
780 wpa_hexdump(MSG_DEBUG, "IKEV2: IKE_SA Initiator's SPI",
781 hdr->i_spi, IKEV2_SPI_LEN);
782 wpa_hexdump(MSG_DEBUG, "IKEV2: IKE_SA Responder's SPI",
783 hdr->r_spi, IKEV2_SPI_LEN);
784 wpa_printf(MSG_DEBUG, "IKEV2: Next Payload: %u Version: 0x%x "
786 hdr->next_payload, hdr->version, hdr->exchange_type);
787 wpa_printf(MSG_DEBUG, "IKEV2: Message ID: %u Length: %u",
790 if (hdr->version != IKEV2_VERSION) {
791 wpa_printf(MSG_INFO, "IKEV2: Unsupported HDR version 0x%x "
792 "(expected 0x%x)", hdr->version, IKEV2_VERSION);
796 if (length != wpabuf_len(buf)) {
797 wpa_printf(MSG_INFO, "IKEV2: Invalid length (HDR: %lu != "
798 "RX: %lu)", (unsigned long) length,
799 (unsigned long) wpabuf_len(buf));
803 if (ikev2_validate_rx_state(data, hdr->exchange_type, message_id) < 0)
806 if ((hdr->flags & (IKEV2_HDR_INITIATOR | IKEV2_HDR_RESPONSE)) !=
807 IKEV2_HDR_INITIATOR) {
808 wpa_printf(MSG_INFO, "IKEV2: Unexpected Flags value 0x%x",
813 if (data->state != SA_INIT) {
814 if (os_memcmp(data->i_spi, hdr->i_spi, IKEV2_SPI_LEN) != 0) {
815 wpa_printf(MSG_INFO, "IKEV2: Unexpected IKE_SA "
819 if (os_memcmp(data->r_spi, hdr->r_spi, IKEV2_SPI_LEN) != 0) {
820 wpa_printf(MSG_INFO, "IKEV2: Unexpected IKE_SA "
826 pos = (const u8 *) (hdr + 1);
827 if (ikev2_parse_payloads(&pl, hdr->next_payload, pos, end) < 0)
830 if (data->state == SA_INIT) {
831 data->last_msg = LAST_MSG_SA_INIT;
832 if (ikev2_process_sa_init(data, hdr, &pl) < 0) {
833 if (data->state == NOTIFY)
837 wpabuf_free(data->i_sign_msg);
838 data->i_sign_msg = wpabuf_dup(buf);
841 if (data->state == SA_AUTH) {
842 data->last_msg = LAST_MSG_SA_AUTH;
843 if (ikev2_process_sa_auth(data, hdr, &pl) < 0) {
844 if (data->state == NOTIFY)
854 static void ikev2_build_hdr(struct ikev2_responder_data *data,
855 struct wpabuf *msg, u8 exchange_type,
856 u8 next_payload, u32 message_id)
858 struct ikev2_hdr *hdr;
860 wpa_printf(MSG_DEBUG, "IKEV2: Adding HDR");
862 /* HDR - RFC 4306, Sect. 3.1 */
863 hdr = wpabuf_put(msg, sizeof(*hdr));
864 os_memcpy(hdr->i_spi, data->i_spi, IKEV2_SPI_LEN);
865 os_memcpy(hdr->r_spi, data->r_spi, IKEV2_SPI_LEN);
866 hdr->next_payload = next_payload;
867 hdr->version = IKEV2_VERSION;
868 hdr->exchange_type = exchange_type;
869 hdr->flags = IKEV2_HDR_RESPONSE;
870 WPA_PUT_BE32(hdr->message_id, message_id);
874 static int ikev2_build_sar1(struct ikev2_responder_data *data,
875 struct wpabuf *msg, u8 next_payload)
877 struct ikev2_payload_hdr *phdr;
879 struct ikev2_proposal *p;
880 struct ikev2_transform *t;
882 wpa_printf(MSG_DEBUG, "IKEV2: Adding SAr1 payload");
884 /* SAr1 - RFC 4306, Sect. 2.7 and 3.3 */
885 phdr = wpabuf_put(msg, sizeof(*phdr));
886 phdr->next_payload = next_payload;
889 p = wpabuf_put(msg, sizeof(*p));
891 /* Seems to require that the Proposal # is 1 even though RFC 4306
892 * Sect 3.3.1 has following requirement "When a proposal is accepted,
893 * all of the proposal numbers in the SA payload MUST be the same and
894 * MUST match the number on the proposal sent that was accepted.".
898 p->proposal_num = data->proposal.proposal_num;
900 p->protocol_id = IKEV2_PROTOCOL_IKE;
901 p->num_transforms = 4;
903 t = wpabuf_put(msg, sizeof(*t));
905 t->transform_type = IKEV2_TRANSFORM_ENCR;
906 WPA_PUT_BE16(t->transform_id, data->proposal.encr);
907 if (data->proposal.encr == ENCR_AES_CBC) {
908 /* Transform Attribute: Key Len = 128 bits */
910 wpabuf_put_be16(msg, 0x001d); /* ?? */
912 wpabuf_put_be16(msg, 0x800e); /* AF=1, AttrType=14 */
914 wpabuf_put_be16(msg, 128); /* 128-bit key */
916 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) t;
917 WPA_PUT_BE16(t->transform_length, plen);
919 t = wpabuf_put(msg, sizeof(*t));
921 WPA_PUT_BE16(t->transform_length, sizeof(*t));
922 t->transform_type = IKEV2_TRANSFORM_PRF;
923 WPA_PUT_BE16(t->transform_id, data->proposal.prf);
925 t = wpabuf_put(msg, sizeof(*t));
927 WPA_PUT_BE16(t->transform_length, sizeof(*t));
928 t->transform_type = IKEV2_TRANSFORM_INTEG;
929 WPA_PUT_BE16(t->transform_id, data->proposal.integ);
931 t = wpabuf_put(msg, sizeof(*t));
932 WPA_PUT_BE16(t->transform_length, sizeof(*t));
933 t->transform_type = IKEV2_TRANSFORM_DH;
934 WPA_PUT_BE16(t->transform_id, data->proposal.dh);
936 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) p;
937 WPA_PUT_BE16(p->proposal_length, plen);
939 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
940 WPA_PUT_BE16(phdr->payload_length, plen);
946 static int ikev2_build_ker(struct ikev2_responder_data *data,
947 struct wpabuf *msg, u8 next_payload)
949 struct ikev2_payload_hdr *phdr;
953 wpa_printf(MSG_DEBUG, "IKEV2: Adding KEr payload");
955 pv = dh_init(data->dh, &data->r_dh_private);
957 wpa_printf(MSG_DEBUG, "IKEV2: Failed to initialize DH");
961 /* KEr - RFC 4306, Sect. 3.4 */
962 phdr = wpabuf_put(msg, sizeof(*phdr));
963 phdr->next_payload = next_payload;
966 wpabuf_put_be16(msg, data->proposal.dh); /* DH Group # */
967 wpabuf_put(msg, 2); /* RESERVED */
969 * RFC 4306, Sect. 3.4: possible zero padding for public value to
970 * match the length of the prime.
972 wpabuf_put(msg, data->dh->prime_len - wpabuf_len(pv));
973 wpabuf_put_buf(msg, pv);
976 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
977 WPA_PUT_BE16(phdr->payload_length, plen);
982 static int ikev2_build_nr(struct ikev2_responder_data *data,
983 struct wpabuf *msg, u8 next_payload)
985 struct ikev2_payload_hdr *phdr;
988 wpa_printf(MSG_DEBUG, "IKEV2: Adding Nr payload");
990 /* Nr - RFC 4306, Sect. 3.9 */
991 phdr = wpabuf_put(msg, sizeof(*phdr));
992 phdr->next_payload = next_payload;
994 wpabuf_put_data(msg, data->r_nonce, data->r_nonce_len);
995 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
996 WPA_PUT_BE16(phdr->payload_length, plen);
1001 static int ikev2_build_idr(struct ikev2_responder_data *data,
1002 struct wpabuf *msg, u8 next_payload)
1004 struct ikev2_payload_hdr *phdr;
1007 wpa_printf(MSG_DEBUG, "IKEV2: Adding IDr payload");
1009 if (data->IDr == NULL) {
1010 wpa_printf(MSG_INFO, "IKEV2: No IDr available");
1014 /* IDr - RFC 4306, Sect. 3.5 */
1015 phdr = wpabuf_put(msg, sizeof(*phdr));
1016 phdr->next_payload = next_payload;
1018 wpabuf_put_u8(msg, ID_KEY_ID);
1019 wpabuf_put(msg, 3); /* RESERVED */
1020 wpabuf_put_data(msg, data->IDr, data->IDr_len);
1021 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
1022 WPA_PUT_BE16(phdr->payload_length, plen);
1027 static int ikev2_build_auth(struct ikev2_responder_data *data,
1028 struct wpabuf *msg, u8 next_payload)
1030 struct ikev2_payload_hdr *phdr;
1032 const struct ikev2_prf_alg *prf;
1034 wpa_printf(MSG_DEBUG, "IKEV2: Adding AUTH payload");
1036 prf = ikev2_get_prf(data->proposal.prf);
1040 /* Authentication - RFC 4306, Sect. 3.8 */
1041 phdr = wpabuf_put(msg, sizeof(*phdr));
1042 phdr->next_payload = next_payload;
1044 wpabuf_put_u8(msg, AUTH_SHARED_KEY_MIC);
1045 wpabuf_put(msg, 3); /* RESERVED */
1047 /* msg | Ni | prf(SK_pr,IDr') */
1048 if (ikev2_derive_auth_data(data->proposal.prf, data->r_sign_msg,
1049 data->IDr, data->IDr_len, ID_KEY_ID,
1050 &data->keys, 0, data->shared_secret,
1051 data->shared_secret_len,
1052 data->i_nonce, data->i_nonce_len,
1053 data->key_pad, data->key_pad_len,
1054 wpabuf_put(msg, prf->hash_len)) < 0) {
1055 wpa_printf(MSG_INFO, "IKEV2: Could not derive AUTH data");
1058 wpabuf_free(data->r_sign_msg);
1059 data->r_sign_msg = NULL;
1061 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
1062 WPA_PUT_BE16(phdr->payload_length, plen);
1067 static int ikev2_build_notification(struct ikev2_responder_data *data,
1068 struct wpabuf *msg, u8 next_payload)
1070 struct ikev2_payload_hdr *phdr;
1073 wpa_printf(MSG_DEBUG, "IKEV2: Adding Notification payload");
1075 if (data->error_type == 0) {
1076 wpa_printf(MSG_INFO, "IKEV2: No Notify Message Type "
1081 /* Notify - RFC 4306, Sect. 3.10 */
1082 phdr = wpabuf_put(msg, sizeof(*phdr));
1083 phdr->next_payload = next_payload;
1086 wpabuf_put_u8(msg, 1); /* Protocol ID: IKE_SA notification */
1088 wpabuf_put_u8(msg, 0); /* Protocol ID: no existing SA */
1089 #endif /* CCNS_PL */
1090 wpabuf_put_u8(msg, 0); /* SPI Size */
1091 wpabuf_put_be16(msg, data->error_type);
1093 switch (data->error_type) {
1094 case INVALID_KE_PAYLOAD:
1095 if (data->proposal.dh == -1) {
1096 wpa_printf(MSG_INFO, "IKEV2: No DH Group selected for "
1097 "INVALID_KE_PAYLOAD notifications");
1100 wpabuf_put_be16(msg, data->proposal.dh);
1101 wpa_printf(MSG_DEBUG, "IKEV2: INVALID_KE_PAYLOAD - request "
1102 "DH Group #%d", data->proposal.dh);
1104 case AUTHENTICATION_FAILED:
1105 /* no associated data */
1108 wpa_printf(MSG_INFO, "IKEV2: Unsupported Notify Message Type "
1109 "%d", data->error_type);
1113 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
1114 WPA_PUT_BE16(phdr->payload_length, plen);
1119 static struct wpabuf * ikev2_build_sa_init(struct ikev2_responder_data *data)
1123 /* build IKE_SA_INIT: HDR, SAr1, KEr, Nr, [CERTREQ], [SK{IDr}] */
1125 if (os_get_random(data->r_spi, IKEV2_SPI_LEN))
1127 wpa_hexdump(MSG_DEBUG, "IKEV2: IKE_SA Responder's SPI",
1128 data->r_spi, IKEV2_SPI_LEN);
1130 data->r_nonce_len = IKEV2_NONCE_MIN_LEN;
1131 if (random_get_bytes(data->r_nonce, data->r_nonce_len))
1134 /* Zeros are removed incorrectly from the beginning of the nonces in
1135 * key derivation; as a workaround, make sure Nr does not start with
1137 if (data->r_nonce[0] == 0)
1138 data->r_nonce[0] = 1;
1139 #endif /* CCNS_PL */
1140 wpa_hexdump(MSG_DEBUG, "IKEV2: Nr", data->r_nonce, data->r_nonce_len);
1142 msg = wpabuf_alloc(sizeof(struct ikev2_hdr) + data->IDr_len + 1500);
1146 ikev2_build_hdr(data, msg, IKE_SA_INIT, IKEV2_PAYLOAD_SA, 0);
1147 if (ikev2_build_sar1(data, msg, IKEV2_PAYLOAD_KEY_EXCHANGE) ||
1148 ikev2_build_ker(data, msg, IKEV2_PAYLOAD_NONCE) ||
1149 ikev2_build_nr(data, msg, data->peer_auth == PEER_AUTH_SECRET ?
1150 IKEV2_PAYLOAD_ENCRYPTED :
1151 IKEV2_PAYLOAD_NO_NEXT_PAYLOAD)) {
1156 if (ikev2_derive_keys(data)) {
1161 if (data->peer_auth == PEER_AUTH_CERT) {
1162 /* TODO: CERTREQ with SHA-1 hashes of Subject Public Key Info
1163 * for trust agents */
1166 if (data->peer_auth == PEER_AUTH_SECRET) {
1167 struct wpabuf *plain = wpabuf_alloc(data->IDr_len + 1000);
1168 if (plain == NULL) {
1172 if (ikev2_build_idr(data, plain,
1173 IKEV2_PAYLOAD_NO_NEXT_PAYLOAD) ||
1174 ikev2_build_encrypted(data->proposal.encr,
1175 data->proposal.integ,
1176 &data->keys, 0, msg, plain,
1177 IKEV2_PAYLOAD_IDr)) {
1185 ikev2_update_hdr(msg);
1187 wpa_hexdump_buf(MSG_MSGDUMP, "IKEV2: Sending message (SA_INIT)", msg);
1189 data->state = SA_AUTH;
1191 wpabuf_free(data->r_sign_msg);
1192 data->r_sign_msg = wpabuf_dup(msg);
1198 static struct wpabuf * ikev2_build_sa_auth(struct ikev2_responder_data *data)
1200 struct wpabuf *msg, *plain;
1202 /* build IKE_SA_AUTH: HDR, SK {IDr, [CERT,] AUTH} */
1204 msg = wpabuf_alloc(sizeof(struct ikev2_hdr) + data->IDr_len + 1000);
1207 ikev2_build_hdr(data, msg, IKE_SA_AUTH, IKEV2_PAYLOAD_ENCRYPTED, 1);
1209 plain = wpabuf_alloc(data->IDr_len + 1000);
1210 if (plain == NULL) {
1215 if (ikev2_build_idr(data, plain, IKEV2_PAYLOAD_AUTHENTICATION) ||
1216 ikev2_build_auth(data, plain, IKEV2_PAYLOAD_NO_NEXT_PAYLOAD) ||
1217 ikev2_build_encrypted(data->proposal.encr, data->proposal.integ,
1218 &data->keys, 0, msg, plain,
1219 IKEV2_PAYLOAD_IDr)) {
1226 wpa_hexdump_buf(MSG_MSGDUMP, "IKEV2: Sending message (SA_AUTH)", msg);
1228 data->state = IKEV2_DONE;
1234 static struct wpabuf * ikev2_build_notify(struct ikev2_responder_data *data)
1238 msg = wpabuf_alloc(sizeof(struct ikev2_hdr) + 1000);
1241 if (data->last_msg == LAST_MSG_SA_AUTH) {
1243 struct wpabuf *plain = wpabuf_alloc(100);
1244 if (plain == NULL) {
1248 ikev2_build_hdr(data, msg, IKE_SA_AUTH,
1249 IKEV2_PAYLOAD_ENCRYPTED, 1);
1250 if (ikev2_build_notification(data, plain,
1251 IKEV2_PAYLOAD_NO_NEXT_PAYLOAD) ||
1252 ikev2_build_encrypted(data->proposal.encr,
1253 data->proposal.integ,
1254 &data->keys, 0, msg, plain,
1255 IKEV2_PAYLOAD_NOTIFICATION)) {
1261 data->state = IKEV2_FAILED;
1264 ikev2_build_hdr(data, msg, IKE_SA_INIT,
1265 IKEV2_PAYLOAD_NOTIFICATION, 0);
1266 if (ikev2_build_notification(data, msg,
1267 IKEV2_PAYLOAD_NO_NEXT_PAYLOAD)) {
1271 data->state = SA_INIT;
1274 ikev2_update_hdr(msg);
1276 wpa_hexdump_buf(MSG_MSGDUMP, "IKEV2: Sending message (Notification)",
1283 struct wpabuf * ikev2_responder_build(struct ikev2_responder_data *data)
1285 switch (data->state) {
1287 return ikev2_build_sa_init(data);
1289 return ikev2_build_sa_auth(data);
1293 return ikev2_build_notify(data);