nl80211: Fix a typo
[hostap-gosc2009.git] / src / eap_server / ikev2.c
blob435ba26249332ff9d943885f8287de06f629d035
1 /*
2 * IKEv2 initiator (RFC 4306) for EAP-IKEV2
3 * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "crypto/dh_groups.h"
19 #include "ikev2.h"
22 static int ikev2_process_idr(struct ikev2_initiator_data *data,
23 const u8 *idr, size_t idr_len);
26 void ikev2_initiator_deinit(struct ikev2_initiator_data *data)
28 ikev2_free_keys(&data->keys);
29 wpabuf_free(data->r_dh_public);
30 wpabuf_free(data->i_dh_private);
31 os_free(data->IDi);
32 os_free(data->IDr);
33 os_free(data->shared_secret);
34 wpabuf_free(data->i_sign_msg);
35 wpabuf_free(data->r_sign_msg);
36 os_free(data->key_pad);
40 static int ikev2_derive_keys(struct ikev2_initiator_data *data)
42 u8 *buf, *pos, *pad, skeyseed[IKEV2_MAX_HASH_LEN];
43 size_t buf_len, pad_len;
44 struct wpabuf *shared;
45 const struct ikev2_integ_alg *integ;
46 const struct ikev2_prf_alg *prf;
47 const struct ikev2_encr_alg *encr;
48 int ret;
49 const u8 *addr[2];
50 size_t len[2];
52 /* RFC 4306, Sect. 2.14 */
54 integ = ikev2_get_integ(data->proposal.integ);
55 prf = ikev2_get_prf(data->proposal.prf);
56 encr = ikev2_get_encr(data->proposal.encr);
57 if (integ == NULL || prf == NULL || encr == NULL) {
58 wpa_printf(MSG_INFO, "IKEV2: Unsupported proposal");
59 return -1;
62 shared = dh_derive_shared(data->r_dh_public, data->i_dh_private,
63 data->dh);
64 if (shared == NULL)
65 return -1;
67 /* Construct Ni | Nr | SPIi | SPIr */
69 buf_len = data->i_nonce_len + data->r_nonce_len + 2 * IKEV2_SPI_LEN;
70 buf = os_malloc(buf_len);
71 if (buf == NULL) {
72 wpabuf_free(shared);
73 return -1;
76 pos = buf;
77 os_memcpy(pos, data->i_nonce, data->i_nonce_len);
78 pos += data->i_nonce_len;
79 os_memcpy(pos, data->r_nonce, data->r_nonce_len);
80 pos += data->r_nonce_len;
81 os_memcpy(pos, data->i_spi, IKEV2_SPI_LEN);
82 pos += IKEV2_SPI_LEN;
83 os_memcpy(pos, data->r_spi, IKEV2_SPI_LEN);
85 /* SKEYSEED = prf(Ni | Nr, g^ir) */
87 /* Use zero-padding per RFC 4306, Sect. 2.14 */
88 pad_len = data->dh->prime_len - wpabuf_len(shared);
89 pad = os_zalloc(pad_len ? pad_len : 1);
90 if (pad == NULL) {
91 wpabuf_free(shared);
92 os_free(buf);
93 return -1;
95 addr[0] = pad;
96 len[0] = pad_len;
97 addr[1] = wpabuf_head(shared);
98 len[1] = wpabuf_len(shared);
99 if (ikev2_prf_hash(prf->id, buf, data->i_nonce_len + data->r_nonce_len,
100 2, addr, len, skeyseed) < 0) {
101 wpabuf_free(shared);
102 os_free(buf);
103 os_free(pad);
104 return -1;
106 os_free(pad);
107 wpabuf_free(shared);
109 /* DH parameters are not needed anymore, so free them */
110 wpabuf_free(data->r_dh_public);
111 data->r_dh_public = NULL;
112 wpabuf_free(data->i_dh_private);
113 data->i_dh_private = NULL;
115 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SKEYSEED",
116 skeyseed, prf->hash_len);
118 ret = ikev2_derive_sk_keys(prf, integ, encr, skeyseed, buf, buf_len,
119 &data->keys);
120 os_free(buf);
121 return ret;
125 static int ikev2_parse_transform(struct ikev2_initiator_data *data,
126 struct ikev2_proposal_data *prop,
127 const u8 *pos, const u8 *end)
129 int transform_len;
130 const struct ikev2_transform *t;
131 u16 transform_id;
132 const u8 *tend;
134 if (end - pos < (int) sizeof(*t)) {
135 wpa_printf(MSG_INFO, "IKEV2: Too short transform");
136 return -1;
139 t = (const struct ikev2_transform *) pos;
140 transform_len = WPA_GET_BE16(t->transform_length);
141 if (transform_len < (int) sizeof(*t) || pos + transform_len > end) {
142 wpa_printf(MSG_INFO, "IKEV2: Invalid transform length %d",
143 transform_len);
144 return -1;
146 tend = pos + transform_len;
148 transform_id = WPA_GET_BE16(t->transform_id);
150 wpa_printf(MSG_DEBUG, "IKEV2: Transform:");
151 wpa_printf(MSG_DEBUG, "IKEV2: Type: %d Transform Length: %d "
152 "Transform Type: %d Transform ID: %d",
153 t->type, transform_len, t->transform_type, transform_id);
155 if (t->type != 0 && t->type != 3) {
156 wpa_printf(MSG_INFO, "IKEV2: Unexpected Transform type");
157 return -1;
160 pos = (const u8 *) (t + 1);
161 if (pos < tend) {
162 wpa_hexdump(MSG_DEBUG, "IKEV2: Transform Attributes",
163 pos, tend - pos);
166 switch (t->transform_type) {
167 case IKEV2_TRANSFORM_ENCR:
168 if (ikev2_get_encr(transform_id) &&
169 transform_id == data->proposal.encr) {
170 if (transform_id == ENCR_AES_CBC) {
171 if (tend - pos != 4) {
172 wpa_printf(MSG_DEBUG, "IKEV2: No "
173 "Transform Attr for AES");
174 break;
176 if (WPA_GET_BE16(pos) != 0x800e) {
177 wpa_printf(MSG_DEBUG, "IKEV2: Not a "
178 "Key Size attribute for "
179 "AES");
180 break;
182 if (WPA_GET_BE16(pos + 2) != 128) {
183 wpa_printf(MSG_DEBUG, "IKEV2: "
184 "Unsupported AES key size "
185 "%d bits",
186 WPA_GET_BE16(pos + 2));
187 break;
190 prop->encr = transform_id;
192 break;
193 case IKEV2_TRANSFORM_PRF:
194 if (ikev2_get_prf(transform_id) &&
195 transform_id == data->proposal.prf)
196 prop->prf = transform_id;
197 break;
198 case IKEV2_TRANSFORM_INTEG:
199 if (ikev2_get_integ(transform_id) &&
200 transform_id == data->proposal.integ)
201 prop->integ = transform_id;
202 break;
203 case IKEV2_TRANSFORM_DH:
204 if (dh_groups_get(transform_id) &&
205 transform_id == data->proposal.dh)
206 prop->dh = transform_id;
207 break;
210 return transform_len;
214 static int ikev2_parse_proposal(struct ikev2_initiator_data *data,
215 struct ikev2_proposal_data *prop,
216 const u8 *pos, const u8 *end)
218 const u8 *pend, *ppos;
219 int proposal_len, i;
220 const struct ikev2_proposal *p;
222 if (end - pos < (int) sizeof(*p)) {
223 wpa_printf(MSG_INFO, "IKEV2: Too short proposal");
224 return -1;
227 p = (const struct ikev2_proposal *) pos;
228 proposal_len = WPA_GET_BE16(p->proposal_length);
229 if (proposal_len < (int) sizeof(*p) || pos + proposal_len > end) {
230 wpa_printf(MSG_INFO, "IKEV2: Invalid proposal length %d",
231 proposal_len);
232 return -1;
234 wpa_printf(MSG_DEBUG, "IKEV2: SAi1 Proposal # %d",
235 p->proposal_num);
236 wpa_printf(MSG_DEBUG, "IKEV2: Type: %d Proposal Length: %d "
237 " Protocol ID: %d",
238 p->type, proposal_len, p->protocol_id);
239 wpa_printf(MSG_DEBUG, "IKEV2: SPI Size: %d Transforms: %d",
240 p->spi_size, p->num_transforms);
242 if (p->type != 0 && p->type != 2) {
243 wpa_printf(MSG_INFO, "IKEV2: Unexpected Proposal type");
244 return -1;
247 if (p->protocol_id != IKEV2_PROTOCOL_IKE) {
248 wpa_printf(MSG_DEBUG, "IKEV2: Unexpected Protocol ID "
249 "(only IKE allowed for EAP-IKEv2)");
250 return -1;
253 if (p->proposal_num != prop->proposal_num) {
254 if (p->proposal_num == prop->proposal_num + 1)
255 prop->proposal_num = p->proposal_num;
256 else {
257 wpa_printf(MSG_INFO, "IKEV2: Unexpected Proposal #");
258 return -1;
262 ppos = (const u8 *) (p + 1);
263 pend = pos + proposal_len;
264 if (ppos + p->spi_size > pend) {
265 wpa_printf(MSG_INFO, "IKEV2: Not enough room for SPI "
266 "in proposal");
267 return -1;
269 if (p->spi_size) {
270 wpa_hexdump(MSG_DEBUG, "IKEV2: SPI",
271 ppos, p->spi_size);
272 ppos += p->spi_size;
276 * For initial IKE_SA negotiation, SPI Size MUST be zero; for
277 * subsequent negotiations, it must be 8 for IKE. We only support
278 * initial case for now.
280 if (p->spi_size != 0) {
281 wpa_printf(MSG_INFO, "IKEV2: Unexpected SPI Size");
282 return -1;
285 if (p->num_transforms == 0) {
286 wpa_printf(MSG_INFO, "IKEV2: At least one transform required");
287 return -1;
290 for (i = 0; i < (int) p->num_transforms; i++) {
291 int tlen = ikev2_parse_transform(data, prop, ppos, pend);
292 if (tlen < 0)
293 return -1;
294 ppos += tlen;
297 if (ppos != pend) {
298 wpa_printf(MSG_INFO, "IKEV2: Unexpected data after "
299 "transforms");
300 return -1;
303 return proposal_len;
307 static int ikev2_process_sar1(struct ikev2_initiator_data *data,
308 const u8 *sar1, size_t sar1_len)
310 struct ikev2_proposal_data prop;
311 const u8 *pos, *end;
312 int found = 0;
314 /* Security Association Payloads: <Proposals> */
316 if (sar1 == NULL) {
317 wpa_printf(MSG_INFO, "IKEV2: SAr1 not received");
318 return -1;
321 os_memset(&prop, 0, sizeof(prop));
322 prop.proposal_num = 1;
324 pos = sar1;
325 end = sar1 + sar1_len;
327 while (pos < end) {
328 int plen;
330 prop.integ = -1;
331 prop.prf = -1;
332 prop.encr = -1;
333 prop.dh = -1;
334 plen = ikev2_parse_proposal(data, &prop, pos, end);
335 if (plen < 0)
336 return -1;
338 if (!found && prop.integ != -1 && prop.prf != -1 &&
339 prop.encr != -1 && prop.dh != -1) {
340 found = 1;
343 pos += plen;
345 /* Only one proposal expected in SAr */
346 break;
349 if (pos != end) {
350 wpa_printf(MSG_INFO, "IKEV2: Unexpected data after proposal");
351 return -1;
354 if (!found) {
355 wpa_printf(MSG_INFO, "IKEV2: No acceptable proposal found");
356 return -1;
359 wpa_printf(MSG_DEBUG, "IKEV2: Accepted proposal #%d: ENCR:%d PRF:%d "
360 "INTEG:%d D-H:%d", data->proposal.proposal_num,
361 data->proposal.encr, data->proposal.prf,
362 data->proposal.integ, data->proposal.dh);
364 return 0;
368 static int ikev2_process_ker(struct ikev2_initiator_data *data,
369 const u8 *ker, size_t ker_len)
371 u16 group;
374 * Key Exchange Payload:
375 * DH Group # (16 bits)
376 * RESERVED (16 bits)
377 * Key Exchange Data (Diffie-Hellman public value)
380 if (ker == NULL) {
381 wpa_printf(MSG_INFO, "IKEV2: KEr not received");
382 return -1;
385 if (ker_len < 4 + 96) {
386 wpa_printf(MSG_INFO, "IKEV2: Too show Key Exchange Payload");
387 return -1;
390 group = WPA_GET_BE16(ker);
391 wpa_printf(MSG_DEBUG, "IKEV2: KEr DH Group #%u", group);
393 if (group != data->proposal.dh) {
394 wpa_printf(MSG_DEBUG, "IKEV2: KEr DH Group #%u does not match "
395 "with the selected proposal (%u)",
396 group, data->proposal.dh);
397 return -1;
400 if (data->dh == NULL) {
401 wpa_printf(MSG_INFO, "IKEV2: Unsupported DH group");
402 return -1;
405 /* RFC 4306, Section 3.4:
406 * The length of DH public value MUST be equal to the lenght of the
407 * prime modulus.
409 if (ker_len - 4 != data->dh->prime_len) {
410 wpa_printf(MSG_INFO, "IKEV2: Invalid DH public value length "
411 "%ld (expected %ld)",
412 (long) (ker_len - 4), (long) data->dh->prime_len);
413 return -1;
416 wpabuf_free(data->r_dh_public);
417 data->r_dh_public = wpabuf_alloc_copy(ker + 4, ker_len - 4);
418 if (data->r_dh_public == NULL)
419 return -1;
421 wpa_hexdump_buf(MSG_DEBUG, "IKEV2: KEr Diffie-Hellman Public Value",
422 data->r_dh_public);
424 return 0;
428 static int ikev2_process_nr(struct ikev2_initiator_data *data,
429 const u8 *nr, size_t nr_len)
431 if (nr == NULL) {
432 wpa_printf(MSG_INFO, "IKEV2: Nr not received");
433 return -1;
436 if (nr_len < IKEV2_NONCE_MIN_LEN || nr_len > IKEV2_NONCE_MAX_LEN) {
437 wpa_printf(MSG_INFO, "IKEV2: Invalid Nr length %ld",
438 (long) nr_len);
439 return -1;
442 data->r_nonce_len = nr_len;
443 os_memcpy(data->r_nonce, nr, nr_len);
444 wpa_hexdump(MSG_MSGDUMP, "IKEV2: Nr",
445 data->r_nonce, data->r_nonce_len);
447 return 0;
451 static int ikev2_process_sa_init_encr(struct ikev2_initiator_data *data,
452 const struct ikev2_hdr *hdr,
453 const u8 *encrypted,
454 size_t encrypted_len, u8 next_payload)
456 u8 *decrypted;
457 size_t decrypted_len;
458 struct ikev2_payloads pl;
459 int ret = 0;
461 decrypted = ikev2_decrypt_payload(data->proposal.encr,
462 data->proposal.integ, &data->keys, 0,
463 hdr, encrypted, encrypted_len,
464 &decrypted_len);
465 if (decrypted == NULL)
466 return -1;
468 wpa_printf(MSG_DEBUG, "IKEV2: Processing decrypted payloads");
470 if (ikev2_parse_payloads(&pl, next_payload, decrypted,
471 decrypted + decrypted_len) < 0) {
472 wpa_printf(MSG_INFO, "IKEV2: Failed to parse decrypted "
473 "payloads");
474 return -1;
477 if (pl.idr)
478 ret = ikev2_process_idr(data, pl.idr, pl.idr_len);
480 os_free(decrypted);
482 return ret;
486 static int ikev2_process_sa_init(struct ikev2_initiator_data *data,
487 const struct ikev2_hdr *hdr,
488 struct ikev2_payloads *pl)
490 if (ikev2_process_sar1(data, pl->sa, pl->sa_len) < 0 ||
491 ikev2_process_ker(data, pl->ke, pl->ke_len) < 0 ||
492 ikev2_process_nr(data, pl->nonce, pl->nonce_len) < 0)
493 return -1;
495 os_memcpy(data->r_spi, hdr->r_spi, IKEV2_SPI_LEN);
497 if (ikev2_derive_keys(data) < 0)
498 return -1;
500 if (pl->encrypted) {
501 wpa_printf(MSG_DEBUG, "IKEV2: Encrypted payload in SA_INIT - "
502 "try to get IDr from it");
503 if (ikev2_process_sa_init_encr(data, hdr, pl->encrypted,
504 pl->encrypted_len,
505 pl->encr_next_payload) < 0) {
506 wpa_printf(MSG_INFO, "IKEV2: Failed to process "
507 "encrypted payload");
508 return -1;
512 data->state = SA_AUTH;
514 return 0;
518 static int ikev2_process_idr(struct ikev2_initiator_data *data,
519 const u8 *idr, size_t idr_len)
521 u8 id_type;
523 if (idr == NULL) {
524 wpa_printf(MSG_INFO, "IKEV2: No IDr received");
525 return -1;
528 if (idr_len < 4) {
529 wpa_printf(MSG_INFO, "IKEV2: Too short IDr payload");
530 return -1;
533 id_type = idr[0];
534 idr += 4;
535 idr_len -= 4;
537 wpa_printf(MSG_DEBUG, "IKEV2: IDr ID Type %d", id_type);
538 wpa_hexdump_ascii(MSG_DEBUG, "IKEV2: IDr", idr, idr_len);
539 if (data->IDr) {
540 if (id_type != data->IDr_type || idr_len != data->IDr_len ||
541 os_memcmp(idr, data->IDr, idr_len) != 0) {
542 wpa_printf(MSG_INFO, "IKEV2: IDr differs from the one "
543 "received earlier");
544 wpa_printf(MSG_DEBUG, "IKEV2: Previous IDr ID Type %d",
545 id_type);
546 wpa_hexdump_ascii(MSG_DEBUG, "Previous IKEV2: IDr",
547 data->IDr, data->IDr_len);
548 return -1;
550 os_free(data->IDr);
552 data->IDr = os_malloc(idr_len);
553 if (data->IDr == NULL)
554 return -1;
555 os_memcpy(data->IDr, idr, idr_len);
556 data->IDr_len = idr_len;
557 data->IDr_type = id_type;
559 return 0;
563 static int ikev2_process_cert(struct ikev2_initiator_data *data,
564 const u8 *cert, size_t cert_len)
566 u8 cert_encoding;
568 if (cert == NULL) {
569 if (data->peer_auth == PEER_AUTH_CERT) {
570 wpa_printf(MSG_INFO, "IKEV2: No Certificate received");
571 return -1;
573 return 0;
576 if (cert_len < 1) {
577 wpa_printf(MSG_INFO, "IKEV2: No Cert Encoding field");
578 return -1;
581 cert_encoding = cert[0];
582 cert++;
583 cert_len--;
585 wpa_printf(MSG_DEBUG, "IKEV2: Cert Encoding %d", cert_encoding);
586 wpa_hexdump(MSG_MSGDUMP, "IKEV2: Certificate Data", cert, cert_len);
588 /* TODO: validate certificate */
590 return 0;
594 static int ikev2_process_auth_cert(struct ikev2_initiator_data *data,
595 u8 method, const u8 *auth, size_t auth_len)
597 if (method != AUTH_RSA_SIGN) {
598 wpa_printf(MSG_INFO, "IKEV2: Unsupported authentication "
599 "method %d", method);
600 return -1;
603 /* TODO: validate AUTH */
604 return 0;
608 static int ikev2_process_auth_secret(struct ikev2_initiator_data *data,
609 u8 method, const u8 *auth,
610 size_t auth_len)
612 u8 auth_data[IKEV2_MAX_HASH_LEN];
613 const struct ikev2_prf_alg *prf;
615 if (method != AUTH_SHARED_KEY_MIC) {
616 wpa_printf(MSG_INFO, "IKEV2: Unsupported authentication "
617 "method %d", method);
618 return -1;
621 /* msg | Ni | prf(SK_pr,IDr') */
622 if (ikev2_derive_auth_data(data->proposal.prf, data->r_sign_msg,
623 data->IDr, data->IDr_len, data->IDr_type,
624 &data->keys, 0, data->shared_secret,
625 data->shared_secret_len,
626 data->i_nonce, data->i_nonce_len,
627 data->key_pad, data->key_pad_len,
628 auth_data) < 0) {
629 wpa_printf(MSG_INFO, "IKEV2: Could not derive AUTH data");
630 return -1;
633 wpabuf_free(data->r_sign_msg);
634 data->r_sign_msg = NULL;
636 prf = ikev2_get_prf(data->proposal.prf);
637 if (prf == NULL)
638 return -1;
640 if (auth_len != prf->hash_len ||
641 os_memcmp(auth, auth_data, auth_len) != 0) {
642 wpa_printf(MSG_INFO, "IKEV2: Invalid Authentication Data");
643 wpa_hexdump(MSG_DEBUG, "IKEV2: Received Authentication Data",
644 auth, auth_len);
645 wpa_hexdump(MSG_DEBUG, "IKEV2: Expected Authentication Data",
646 auth_data, prf->hash_len);
647 return -1;
650 wpa_printf(MSG_DEBUG, "IKEV2: Peer authenticated successfully "
651 "using shared keys");
653 return 0;
657 static int ikev2_process_auth(struct ikev2_initiator_data *data,
658 const u8 *auth, size_t auth_len)
660 u8 auth_method;
662 if (auth == NULL) {
663 wpa_printf(MSG_INFO, "IKEV2: No Authentication Payload");
664 return -1;
667 if (auth_len < 4) {
668 wpa_printf(MSG_INFO, "IKEV2: Too short Authentication "
669 "Payload");
670 return -1;
673 auth_method = auth[0];
674 auth += 4;
675 auth_len -= 4;
677 wpa_printf(MSG_DEBUG, "IKEV2: Auth Method %d", auth_method);
678 wpa_hexdump(MSG_MSGDUMP, "IKEV2: Authentication Data", auth, auth_len);
680 switch (data->peer_auth) {
681 case PEER_AUTH_CERT:
682 return ikev2_process_auth_cert(data, auth_method, auth,
683 auth_len);
684 case PEER_AUTH_SECRET:
685 return ikev2_process_auth_secret(data, auth_method, auth,
686 auth_len);
689 return -1;
693 static int ikev2_process_sa_auth_decrypted(struct ikev2_initiator_data *data,
694 u8 next_payload,
695 u8 *payload, size_t payload_len)
697 struct ikev2_payloads pl;
699 wpa_printf(MSG_DEBUG, "IKEV2: Processing decrypted payloads");
701 if (ikev2_parse_payloads(&pl, next_payload, payload, payload +
702 payload_len) < 0) {
703 wpa_printf(MSG_INFO, "IKEV2: Failed to parse decrypted "
704 "payloads");
705 return -1;
708 if (ikev2_process_idr(data, pl.idr, pl.idr_len) < 0 ||
709 ikev2_process_cert(data, pl.cert, pl.cert_len) < 0 ||
710 ikev2_process_auth(data, pl.auth, pl.auth_len) < 0)
711 return -1;
713 return 0;
717 static int ikev2_process_sa_auth(struct ikev2_initiator_data *data,
718 const struct ikev2_hdr *hdr,
719 struct ikev2_payloads *pl)
721 u8 *decrypted;
722 size_t decrypted_len;
723 int ret;
725 decrypted = ikev2_decrypt_payload(data->proposal.encr,
726 data->proposal.integ,
727 &data->keys, 0, hdr, pl->encrypted,
728 pl->encrypted_len, &decrypted_len);
729 if (decrypted == NULL)
730 return -1;
732 ret = ikev2_process_sa_auth_decrypted(data, pl->encr_next_payload,
733 decrypted, decrypted_len);
734 os_free(decrypted);
736 if (ret == 0 && !data->unknown_user) {
737 wpa_printf(MSG_DEBUG, "IKEV2: Authentication completed");
738 data->state = IKEV2_DONE;
741 return ret;
745 static int ikev2_validate_rx_state(struct ikev2_initiator_data *data,
746 u8 exchange_type, u32 message_id)
748 switch (data->state) {
749 case SA_INIT:
750 /* Expect to receive IKE_SA_INIT: HDR, SAr, KEr, Nr, [CERTREQ],
751 * [SK{IDr}] */
752 if (exchange_type != IKE_SA_INIT) {
753 wpa_printf(MSG_INFO, "IKEV2: Unexpected Exchange Type "
754 "%u in SA_INIT state", exchange_type);
755 return -1;
757 if (message_id != 0) {
758 wpa_printf(MSG_INFO, "IKEV2: Unexpected Message ID %u "
759 "in SA_INIT state", message_id);
760 return -1;
762 break;
763 case SA_AUTH:
764 /* Expect to receive IKE_SA_AUTH:
765 * HDR, SK {IDr, [CERT,] [CERTREQ,] [NFID,] AUTH}
767 if (exchange_type != IKE_SA_AUTH) {
768 wpa_printf(MSG_INFO, "IKEV2: Unexpected Exchange Type "
769 "%u in SA_AUTH state", exchange_type);
770 return -1;
772 if (message_id != 1) {
773 wpa_printf(MSG_INFO, "IKEV2: Unexpected Message ID %u "
774 "in SA_AUTH state", message_id);
775 return -1;
777 break;
778 case CHILD_SA:
779 if (exchange_type != CREATE_CHILD_SA) {
780 wpa_printf(MSG_INFO, "IKEV2: Unexpected Exchange Type "
781 "%u in CHILD_SA state", exchange_type);
782 return -1;
784 if (message_id != 2) {
785 wpa_printf(MSG_INFO, "IKEV2: Unexpected Message ID %u "
786 "in CHILD_SA state", message_id);
787 return -1;
789 break;
790 case IKEV2_DONE:
791 return -1;
794 return 0;
798 int ikev2_initiator_process(struct ikev2_initiator_data *data,
799 const struct wpabuf *buf)
801 const struct ikev2_hdr *hdr;
802 u32 length, message_id;
803 const u8 *pos, *end;
804 struct ikev2_payloads pl;
806 wpa_printf(MSG_MSGDUMP, "IKEV2: Received message (len %lu)",
807 (unsigned long) wpabuf_len(buf));
809 if (wpabuf_len(buf) < sizeof(*hdr)) {
810 wpa_printf(MSG_INFO, "IKEV2: Too short frame to include HDR");
811 return -1;
814 hdr = (const struct ikev2_hdr *) wpabuf_head(buf);
815 end = wpabuf_head_u8(buf) + wpabuf_len(buf);
816 message_id = WPA_GET_BE32(hdr->message_id);
817 length = WPA_GET_BE32(hdr->length);
819 wpa_hexdump(MSG_DEBUG, "IKEV2: IKE_SA Initiator's SPI",
820 hdr->i_spi, IKEV2_SPI_LEN);
821 wpa_hexdump(MSG_DEBUG, "IKEV2: IKE_SA Initiator's SPI",
822 hdr->r_spi, IKEV2_SPI_LEN);
823 wpa_printf(MSG_DEBUG, "IKEV2: Next Payload: %u Version: 0x%x "
824 "Exchange Type: %u",
825 hdr->next_payload, hdr->version, hdr->exchange_type);
826 wpa_printf(MSG_DEBUG, "IKEV2: Message ID: %u Length: %u",
827 message_id, length);
829 if (hdr->version != IKEV2_VERSION) {
830 wpa_printf(MSG_INFO, "IKEV2: Unsupported HDR version 0x%x "
831 "(expected 0x%x)", hdr->version, IKEV2_VERSION);
832 return -1;
835 if (length != wpabuf_len(buf)) {
836 wpa_printf(MSG_INFO, "IKEV2: Invalid length (HDR: %lu != "
837 "RX: %lu)", (unsigned long) length,
838 (unsigned long) wpabuf_len(buf));
839 return -1;
842 if (ikev2_validate_rx_state(data, hdr->exchange_type, message_id) < 0)
843 return -1;
845 if ((hdr->flags & (IKEV2_HDR_INITIATOR | IKEV2_HDR_RESPONSE)) !=
846 IKEV2_HDR_RESPONSE) {
847 wpa_printf(MSG_INFO, "IKEV2: Unexpected Flags value 0x%x",
848 hdr->flags);
849 return -1;
852 if (data->state != SA_INIT) {
853 if (os_memcmp(data->i_spi, hdr->i_spi, IKEV2_SPI_LEN) != 0) {
854 wpa_printf(MSG_INFO, "IKEV2: Unexpected IKE_SA "
855 "Initiator's SPI");
856 return -1;
858 if (os_memcmp(data->r_spi, hdr->r_spi, IKEV2_SPI_LEN) != 0) {
859 wpa_printf(MSG_INFO, "IKEV2: Unexpected IKE_SA "
860 "Responder's SPI");
861 return -1;
865 pos = (const u8 *) (hdr + 1);
866 if (ikev2_parse_payloads(&pl, hdr->next_payload, pos, end) < 0)
867 return -1;
869 switch (data->state) {
870 case SA_INIT:
871 if (ikev2_process_sa_init(data, hdr, &pl) < 0)
872 return -1;
873 wpabuf_free(data->r_sign_msg);
874 data->r_sign_msg = wpabuf_dup(buf);
875 break;
876 case SA_AUTH:
877 if (ikev2_process_sa_auth(data, hdr, &pl) < 0)
878 return -1;
879 break;
880 case CHILD_SA:
881 case IKEV2_DONE:
882 break;
885 return 0;
889 static void ikev2_build_hdr(struct ikev2_initiator_data *data,
890 struct wpabuf *msg, u8 exchange_type,
891 u8 next_payload, u32 message_id)
893 struct ikev2_hdr *hdr;
895 wpa_printf(MSG_DEBUG, "IKEV2: Adding HDR");
897 /* HDR - RFC 4306, Sect. 3.1 */
898 hdr = wpabuf_put(msg, sizeof(*hdr));
899 os_memcpy(hdr->i_spi, data->i_spi, IKEV2_SPI_LEN);
900 os_memcpy(hdr->r_spi, data->r_spi, IKEV2_SPI_LEN);
901 hdr->next_payload = next_payload;
902 hdr->version = IKEV2_VERSION;
903 hdr->exchange_type = exchange_type;
904 hdr->flags = IKEV2_HDR_INITIATOR;
905 WPA_PUT_BE32(hdr->message_id, message_id);
909 static int ikev2_build_sai(struct ikev2_initiator_data *data,
910 struct wpabuf *msg, u8 next_payload)
912 struct ikev2_payload_hdr *phdr;
913 size_t plen;
914 struct ikev2_proposal *p;
915 struct ikev2_transform *t;
917 wpa_printf(MSG_DEBUG, "IKEV2: Adding SAi payload");
919 /* SAi1 - RFC 4306, Sect. 2.7 and 3.3 */
920 phdr = wpabuf_put(msg, sizeof(*phdr));
921 phdr->next_payload = next_payload;
922 phdr->flags = 0;
924 /* TODO: support for multiple proposals */
925 p = wpabuf_put(msg, sizeof(*p));
926 p->proposal_num = data->proposal.proposal_num;
927 p->protocol_id = IKEV2_PROTOCOL_IKE;
928 p->num_transforms = 4;
930 t = wpabuf_put(msg, sizeof(*t));
931 t->type = 3;
932 t->transform_type = IKEV2_TRANSFORM_ENCR;
933 WPA_PUT_BE16(t->transform_id, data->proposal.encr);
934 if (data->proposal.encr == ENCR_AES_CBC) {
935 /* Transform Attribute: Key Len = 128 bits */
936 wpabuf_put_be16(msg, 0x800e); /* AF=1, AttrType=14 */
937 wpabuf_put_be16(msg, 128); /* 128-bit key */
939 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) t;
940 WPA_PUT_BE16(t->transform_length, plen);
942 t = wpabuf_put(msg, sizeof(*t));
943 t->type = 3;
944 WPA_PUT_BE16(t->transform_length, sizeof(*t));
945 t->transform_type = IKEV2_TRANSFORM_PRF;
946 WPA_PUT_BE16(t->transform_id, data->proposal.prf);
948 t = wpabuf_put(msg, sizeof(*t));
949 t->type = 3;
950 WPA_PUT_BE16(t->transform_length, sizeof(*t));
951 t->transform_type = IKEV2_TRANSFORM_INTEG;
952 WPA_PUT_BE16(t->transform_id, data->proposal.integ);
954 t = wpabuf_put(msg, sizeof(*t));
955 WPA_PUT_BE16(t->transform_length, sizeof(*t));
956 t->transform_type = IKEV2_TRANSFORM_DH;
957 WPA_PUT_BE16(t->transform_id, data->proposal.dh);
959 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) p;
960 WPA_PUT_BE16(p->proposal_length, plen);
962 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
963 WPA_PUT_BE16(phdr->payload_length, plen);
965 return 0;
969 static int ikev2_build_kei(struct ikev2_initiator_data *data,
970 struct wpabuf *msg, u8 next_payload)
972 struct ikev2_payload_hdr *phdr;
973 size_t plen;
974 struct wpabuf *pv;
976 wpa_printf(MSG_DEBUG, "IKEV2: Adding KEi payload");
978 data->dh = dh_groups_get(data->proposal.dh);
979 pv = dh_init(data->dh, &data->i_dh_private);
980 if (pv == NULL) {
981 wpa_printf(MSG_DEBUG, "IKEV2: Failed to initialize DH");
982 return -1;
985 /* KEi - RFC 4306, Sect. 3.4 */
986 phdr = wpabuf_put(msg, sizeof(*phdr));
987 phdr->next_payload = next_payload;
988 phdr->flags = 0;
990 wpabuf_put_be16(msg, data->proposal.dh); /* DH Group # */
991 wpabuf_put(msg, 2); /* RESERVED */
993 * RFC 4306, Sect. 3.4: possible zero padding for public value to
994 * match the length of the prime.
996 wpabuf_put(msg, data->dh->prime_len - wpabuf_len(pv));
997 wpabuf_put_buf(msg, pv);
998 os_free(pv);
1000 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
1001 WPA_PUT_BE16(phdr->payload_length, plen);
1002 return 0;
1006 static int ikev2_build_ni(struct ikev2_initiator_data *data,
1007 struct wpabuf *msg, u8 next_payload)
1009 struct ikev2_payload_hdr *phdr;
1010 size_t plen;
1012 wpa_printf(MSG_DEBUG, "IKEV2: Adding Ni payload");
1014 /* Ni - RFC 4306, Sect. 3.9 */
1015 phdr = wpabuf_put(msg, sizeof(*phdr));
1016 phdr->next_payload = next_payload;
1017 phdr->flags = 0;
1018 wpabuf_put_data(msg, data->i_nonce, data->i_nonce_len);
1019 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
1020 WPA_PUT_BE16(phdr->payload_length, plen);
1021 return 0;
1025 static int ikev2_build_idi(struct ikev2_initiator_data *data,
1026 struct wpabuf *msg, u8 next_payload)
1028 struct ikev2_payload_hdr *phdr;
1029 size_t plen;
1031 wpa_printf(MSG_DEBUG, "IKEV2: Adding IDi payload");
1033 if (data->IDi == NULL) {
1034 wpa_printf(MSG_INFO, "IKEV2: No IDi available");
1035 return -1;
1038 /* IDi - RFC 4306, Sect. 3.5 */
1039 phdr = wpabuf_put(msg, sizeof(*phdr));
1040 phdr->next_payload = next_payload;
1041 phdr->flags = 0;
1042 wpabuf_put_u8(msg, ID_KEY_ID);
1043 wpabuf_put(msg, 3); /* RESERVED */
1044 wpabuf_put_data(msg, data->IDi, data->IDi_len);
1045 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
1046 WPA_PUT_BE16(phdr->payload_length, plen);
1047 return 0;
1051 static int ikev2_build_auth(struct ikev2_initiator_data *data,
1052 struct wpabuf *msg, u8 next_payload)
1054 struct ikev2_payload_hdr *phdr;
1055 size_t plen;
1056 const struct ikev2_prf_alg *prf;
1058 wpa_printf(MSG_DEBUG, "IKEV2: Adding AUTH payload");
1060 prf = ikev2_get_prf(data->proposal.prf);
1061 if (prf == NULL)
1062 return -1;
1064 /* Authentication - RFC 4306, Sect. 3.8 */
1065 phdr = wpabuf_put(msg, sizeof(*phdr));
1066 phdr->next_payload = next_payload;
1067 phdr->flags = 0;
1068 wpabuf_put_u8(msg, AUTH_SHARED_KEY_MIC);
1069 wpabuf_put(msg, 3); /* RESERVED */
1071 /* msg | Nr | prf(SK_pi,IDi') */
1072 if (ikev2_derive_auth_data(data->proposal.prf, data->i_sign_msg,
1073 data->IDi, data->IDi_len, ID_KEY_ID,
1074 &data->keys, 1, data->shared_secret,
1075 data->shared_secret_len,
1076 data->r_nonce, data->r_nonce_len,
1077 data->key_pad, data->key_pad_len,
1078 wpabuf_put(msg, prf->hash_len)) < 0) {
1079 wpa_printf(MSG_INFO, "IKEV2: Could not derive AUTH data");
1080 return -1;
1082 wpabuf_free(data->i_sign_msg);
1083 data->i_sign_msg = NULL;
1085 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
1086 WPA_PUT_BE16(phdr->payload_length, plen);
1087 return 0;
1091 static struct wpabuf * ikev2_build_sa_init(struct ikev2_initiator_data *data)
1093 struct wpabuf *msg;
1095 /* build IKE_SA_INIT: HDR, SAi, KEi, Ni */
1097 if (os_get_random(data->i_spi, IKEV2_SPI_LEN))
1098 return NULL;
1099 wpa_hexdump(MSG_DEBUG, "IKEV2: IKE_SA Initiator's SPI",
1100 data->i_spi, IKEV2_SPI_LEN);
1102 data->i_nonce_len = IKEV2_NONCE_MIN_LEN;
1103 if (os_get_random(data->i_nonce, data->i_nonce_len))
1104 return NULL;
1105 wpa_hexdump(MSG_DEBUG, "IKEV2: Ni", data->i_nonce, data->i_nonce_len);
1107 msg = wpabuf_alloc(sizeof(struct ikev2_hdr) + 1000);
1108 if (msg == NULL)
1109 return NULL;
1111 ikev2_build_hdr(data, msg, IKE_SA_INIT, IKEV2_PAYLOAD_SA, 0);
1112 if (ikev2_build_sai(data, msg, IKEV2_PAYLOAD_KEY_EXCHANGE) ||
1113 ikev2_build_kei(data, msg, IKEV2_PAYLOAD_NONCE) ||
1114 ikev2_build_ni(data, msg, IKEV2_PAYLOAD_NO_NEXT_PAYLOAD)) {
1115 wpabuf_free(msg);
1116 return NULL;
1119 ikev2_update_hdr(msg);
1121 wpa_hexdump_buf(MSG_MSGDUMP, "IKEV2: Sending message (SA_INIT)", msg);
1123 wpabuf_free(data->i_sign_msg);
1124 data->i_sign_msg = wpabuf_dup(msg);
1126 return msg;
1130 static struct wpabuf * ikev2_build_sa_auth(struct ikev2_initiator_data *data)
1132 struct wpabuf *msg, *plain;
1133 const u8 *secret;
1134 size_t secret_len;
1136 secret = data->get_shared_secret(data->cb_ctx, data->IDr,
1137 data->IDr_len, &secret_len);
1138 if (secret == NULL) {
1139 wpa_printf(MSG_INFO, "IKEV2: Could not get shared secret - "
1140 "use fake value");
1141 /* RFC 5106, Sect. 7:
1142 * Use a random key to fake AUTH generation in order to prevent
1143 * probing of user identities.
1145 data->unknown_user = 1;
1146 os_free(data->shared_secret);
1147 data->shared_secret = os_malloc(16);
1148 if (data->shared_secret == NULL)
1149 return NULL;
1150 data->shared_secret_len = 16;
1151 if (os_get_random(data->shared_secret, 16))
1152 return NULL;
1153 } else {
1154 os_free(data->shared_secret);
1155 data->shared_secret = os_malloc(secret_len);
1156 if (data->shared_secret == NULL)
1157 return NULL;
1158 os_memcpy(data->shared_secret, secret, secret_len);
1159 data->shared_secret_len = secret_len;
1162 /* build IKE_SA_AUTH: HDR, SK {IDi, [CERT,] [CERTREQ,] AUTH} */
1164 msg = wpabuf_alloc(sizeof(struct ikev2_hdr) + data->IDr_len + 1000);
1165 if (msg == NULL)
1166 return NULL;
1167 ikev2_build_hdr(data, msg, IKE_SA_AUTH, IKEV2_PAYLOAD_ENCRYPTED, 1);
1169 plain = wpabuf_alloc(data->IDr_len + 1000);
1170 if (plain == NULL) {
1171 wpabuf_free(msg);
1172 return NULL;
1175 if (ikev2_build_idi(data, plain, IKEV2_PAYLOAD_AUTHENTICATION) ||
1176 ikev2_build_auth(data, plain, IKEV2_PAYLOAD_NO_NEXT_PAYLOAD) ||
1177 ikev2_build_encrypted(data->proposal.encr, data->proposal.integ,
1178 &data->keys, 1, msg, plain,
1179 IKEV2_PAYLOAD_IDi)) {
1180 wpabuf_free(plain);
1181 wpabuf_free(msg);
1182 return NULL;
1184 wpabuf_free(plain);
1186 wpa_hexdump_buf(MSG_MSGDUMP, "IKEV2: Sending message (SA_AUTH)", msg);
1188 return msg;
1192 struct wpabuf * ikev2_initiator_build(struct ikev2_initiator_data *data)
1194 switch (data->state) {
1195 case SA_INIT:
1196 return ikev2_build_sa_init(data);
1197 case SA_AUTH:
1198 return ikev2_build_sa_auth(data);
1199 case CHILD_SA:
1200 return NULL;
1201 case IKEV2_DONE:
1202 return NULL;
1204 return NULL;