ikev2.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. /*
  2. * IKEv2 initiator (RFC 4306) for EAP-IKEV2
  3. * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
  4. *
  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.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "crypto/dh_groups.h"
  17. #include "ikev2.h"
  18. static int ikev2_process_idr(struct ikev2_initiator_data *data,
  19. const u8 *idr, size_t idr_len);
  20. void ikev2_initiator_deinit(struct ikev2_initiator_data *data)
  21. {
  22. ikev2_free_keys(&data->keys);
  23. wpabuf_free(data->r_dh_public);
  24. wpabuf_free(data->i_dh_private);
  25. os_free(data->IDi);
  26. os_free(data->IDr);
  27. os_free(data->shared_secret);
  28. wpabuf_free(data->i_sign_msg);
  29. wpabuf_free(data->r_sign_msg);
  30. os_free(data->key_pad);
  31. }
  32. static int ikev2_derive_keys(struct ikev2_initiator_data *data)
  33. {
  34. u8 *buf, *pos, *pad, skeyseed[IKEV2_MAX_HASH_LEN];
  35. size_t buf_len, pad_len;
  36. struct wpabuf *shared;
  37. const struct ikev2_integ_alg *integ;
  38. const struct ikev2_prf_alg *prf;
  39. const struct ikev2_encr_alg *encr;
  40. int ret;
  41. const u8 *addr[2];
  42. size_t len[2];
  43. /* RFC 4306, Sect. 2.14 */
  44. integ = ikev2_get_integ(data->proposal.integ);
  45. prf = ikev2_get_prf(data->proposal.prf);
  46. encr = ikev2_get_encr(data->proposal.encr);
  47. if (integ == NULL || prf == NULL || encr == NULL) {
  48. wpa_printf(MSG_INFO, "IKEV2: Unsupported proposal");
  49. return -1;
  50. }
  51. shared = dh_derive_shared(data->r_dh_public, data->i_dh_private,
  52. data->dh);
  53. if (shared == NULL)
  54. return -1;
  55. /* Construct Ni | Nr | SPIi | SPIr */
  56. buf_len = data->i_nonce_len + data->r_nonce_len + 2 * IKEV2_SPI_LEN;
  57. buf = os_malloc(buf_len);
  58. if (buf == NULL) {
  59. wpabuf_free(shared);
  60. return -1;
  61. }
  62. pos = buf;
  63. os_memcpy(pos, data->i_nonce, data->i_nonce_len);
  64. pos += data->i_nonce_len;
  65. os_memcpy(pos, data->r_nonce, data->r_nonce_len);
  66. pos += data->r_nonce_len;
  67. os_memcpy(pos, data->i_spi, IKEV2_SPI_LEN);
  68. pos += IKEV2_SPI_LEN;
  69. os_memcpy(pos, data->r_spi, IKEV2_SPI_LEN);
  70. /* SKEYSEED = prf(Ni | Nr, g^ir) */
  71. /* Use zero-padding per RFC 4306, Sect. 2.14 */
  72. pad_len = data->dh->prime_len - wpabuf_len(shared);
  73. pad = os_zalloc(pad_len ? pad_len : 1);
  74. if (pad == NULL) {
  75. wpabuf_free(shared);
  76. os_free(buf);
  77. return -1;
  78. }
  79. addr[0] = pad;
  80. len[0] = pad_len;
  81. addr[1] = wpabuf_head(shared);
  82. len[1] = wpabuf_len(shared);
  83. if (ikev2_prf_hash(prf->id, buf, data->i_nonce_len + data->r_nonce_len,
  84. 2, addr, len, skeyseed) < 0) {
  85. wpabuf_free(shared);
  86. os_free(buf);
  87. os_free(pad);
  88. return -1;
  89. }
  90. os_free(pad);
  91. wpabuf_free(shared);
  92. /* DH parameters are not needed anymore, so free them */
  93. wpabuf_free(data->r_dh_public);
  94. data->r_dh_public = NULL;
  95. wpabuf_free(data->i_dh_private);
  96. data->i_dh_private = NULL;
  97. wpa_hexdump_key(MSG_DEBUG, "IKEV2: SKEYSEED",
  98. skeyseed, prf->hash_len);
  99. ret = ikev2_derive_sk_keys(prf, integ, encr, skeyseed, buf, buf_len,
  100. &data->keys);
  101. os_free(buf);
  102. return ret;
  103. }
  104. static int ikev2_parse_transform(struct ikev2_initiator_data *data,
  105. struct ikev2_proposal_data *prop,
  106. const u8 *pos, const u8 *end)
  107. {
  108. int transform_len;
  109. const struct ikev2_transform *t;
  110. u16 transform_id;
  111. const u8 *tend;
  112. if (end - pos < (int) sizeof(*t)) {
  113. wpa_printf(MSG_INFO, "IKEV2: Too short transform");
  114. return -1;
  115. }
  116. t = (const struct ikev2_transform *) pos;
  117. transform_len = WPA_GET_BE16(t->transform_length);
  118. if (transform_len < (int) sizeof(*t) || pos + transform_len > end) {
  119. wpa_printf(MSG_INFO, "IKEV2: Invalid transform length %d",
  120. transform_len);
  121. return -1;
  122. }
  123. tend = pos + transform_len;
  124. transform_id = WPA_GET_BE16(t->transform_id);
  125. wpa_printf(MSG_DEBUG, "IKEV2: Transform:");
  126. wpa_printf(MSG_DEBUG, "IKEV2: Type: %d Transform Length: %d "
  127. "Transform Type: %d Transform ID: %d",
  128. t->type, transform_len, t->transform_type, transform_id);
  129. if (t->type != 0 && t->type != 3) {
  130. wpa_printf(MSG_INFO, "IKEV2: Unexpected Transform type");
  131. return -1;
  132. }
  133. pos = (const u8 *) (t + 1);
  134. if (pos < tend) {
  135. wpa_hexdump(MSG_DEBUG, "IKEV2: Transform Attributes",
  136. pos, tend - pos);
  137. }
  138. switch (t->transform_type) {
  139. case IKEV2_TRANSFORM_ENCR:
  140. if (ikev2_get_encr(transform_id) &&
  141. transform_id == data->proposal.encr) {
  142. if (transform_id == ENCR_AES_CBC) {
  143. if (tend - pos != 4) {
  144. wpa_printf(MSG_DEBUG, "IKEV2: No "
  145. "Transform Attr for AES");
  146. break;
  147. }
  148. if (WPA_GET_BE16(pos) != 0x800e) {
  149. wpa_printf(MSG_DEBUG, "IKEV2: Not a "
  150. "Key Size attribute for "
  151. "AES");
  152. break;
  153. }
  154. if (WPA_GET_BE16(pos + 2) != 128) {
  155. wpa_printf(MSG_DEBUG, "IKEV2: "
  156. "Unsupported AES key size "
  157. "%d bits",
  158. WPA_GET_BE16(pos + 2));
  159. break;
  160. }
  161. }
  162. prop->encr = transform_id;
  163. }
  164. break;
  165. case IKEV2_TRANSFORM_PRF:
  166. if (ikev2_get_prf(transform_id) &&
  167. transform_id == data->proposal.prf)
  168. prop->prf = transform_id;
  169. break;
  170. case IKEV2_TRANSFORM_INTEG:
  171. if (ikev2_get_integ(transform_id) &&
  172. transform_id == data->proposal.integ)
  173. prop->integ = transform_id;
  174. break;
  175. case IKEV2_TRANSFORM_DH:
  176. if (dh_groups_get(transform_id) &&
  177. transform_id == data->proposal.dh)
  178. prop->dh = transform_id;
  179. break;
  180. }
  181. return transform_len;
  182. }
  183. static int ikev2_parse_proposal(struct ikev2_initiator_data *data,
  184. struct ikev2_proposal_data *prop,
  185. const u8 *pos, const u8 *end)
  186. {
  187. const u8 *pend, *ppos;
  188. int proposal_len, i;
  189. const struct ikev2_proposal *p;
  190. if (end - pos < (int) sizeof(*p)) {
  191. wpa_printf(MSG_INFO, "IKEV2: Too short proposal");
  192. return -1;
  193. }
  194. p = (const struct ikev2_proposal *) pos;
  195. proposal_len = WPA_GET_BE16(p->proposal_length);
  196. if (proposal_len < (int) sizeof(*p) || pos + proposal_len > end) {
  197. wpa_printf(MSG_INFO, "IKEV2: Invalid proposal length %d",
  198. proposal_len);
  199. return -1;
  200. }
  201. wpa_printf(MSG_DEBUG, "IKEV2: SAi1 Proposal # %d",
  202. p->proposal_num);
  203. wpa_printf(MSG_DEBUG, "IKEV2: Type: %d Proposal Length: %d "
  204. " Protocol ID: %d",
  205. p->type, proposal_len, p->protocol_id);
  206. wpa_printf(MSG_DEBUG, "IKEV2: SPI Size: %d Transforms: %d",
  207. p->spi_size, p->num_transforms);
  208. if (p->type != 0 && p->type != 2) {
  209. wpa_printf(MSG_INFO, "IKEV2: Unexpected Proposal type");
  210. return -1;
  211. }
  212. if (p->protocol_id != IKEV2_PROTOCOL_IKE) {
  213. wpa_printf(MSG_DEBUG, "IKEV2: Unexpected Protocol ID "
  214. "(only IKE allowed for EAP-IKEv2)");
  215. return -1;
  216. }
  217. if (p->proposal_num != prop->proposal_num) {
  218. if (p->proposal_num == prop->proposal_num + 1)
  219. prop->proposal_num = p->proposal_num;
  220. else {
  221. wpa_printf(MSG_INFO, "IKEV2: Unexpected Proposal #");
  222. return -1;
  223. }
  224. }
  225. ppos = (const u8 *) (p + 1);
  226. pend = pos + proposal_len;
  227. if (ppos + p->spi_size > pend) {
  228. wpa_printf(MSG_INFO, "IKEV2: Not enough room for SPI "
  229. "in proposal");
  230. return -1;
  231. }
  232. if (p->spi_size) {
  233. wpa_hexdump(MSG_DEBUG, "IKEV2: SPI",
  234. ppos, p->spi_size);
  235. ppos += p->spi_size;
  236. }
  237. /*
  238. * For initial IKE_SA negotiation, SPI Size MUST be zero; for
  239. * subsequent negotiations, it must be 8 for IKE. We only support
  240. * initial case for now.
  241. */
  242. if (p->spi_size != 0) {
  243. wpa_printf(MSG_INFO, "IKEV2: Unexpected SPI Size");
  244. return -1;
  245. }
  246. if (p->num_transforms == 0) {
  247. wpa_printf(MSG_INFO, "IKEV2: At least one transform required");
  248. return -1;
  249. }
  250. for (i = 0; i < (int) p->num_transforms; i++) {
  251. int tlen = ikev2_parse_transform(data, prop, ppos, pend);
  252. if (tlen < 0)
  253. return -1;
  254. ppos += tlen;
  255. }
  256. if (ppos != pend) {
  257. wpa_printf(MSG_INFO, "IKEV2: Unexpected data after "
  258. "transforms");
  259. return -1;
  260. }
  261. return proposal_len;
  262. }
  263. static int ikev2_process_sar1(struct ikev2_initiator_data *data,
  264. const u8 *sar1, size_t sar1_len)
  265. {
  266. struct ikev2_proposal_data prop;
  267. const u8 *pos, *end;
  268. int found = 0;
  269. /* Security Association Payloads: <Proposals> */
  270. if (sar1 == NULL) {
  271. wpa_printf(MSG_INFO, "IKEV2: SAr1 not received");
  272. return -1;
  273. }
  274. os_memset(&prop, 0, sizeof(prop));
  275. prop.proposal_num = 1;
  276. pos = sar1;
  277. end = sar1 + sar1_len;
  278. while (pos < end) {
  279. int plen;
  280. prop.integ = -1;
  281. prop.prf = -1;
  282. prop.encr = -1;
  283. prop.dh = -1;
  284. plen = ikev2_parse_proposal(data, &prop, pos, end);
  285. if (plen < 0)
  286. return -1;
  287. if (!found && prop.integ != -1 && prop.prf != -1 &&
  288. prop.encr != -1 && prop.dh != -1) {
  289. found = 1;
  290. }
  291. pos += plen;
  292. /* Only one proposal expected in SAr */
  293. break;
  294. }
  295. if (pos != end) {
  296. wpa_printf(MSG_INFO, "IKEV2: Unexpected data after proposal");
  297. return -1;
  298. }
  299. if (!found) {
  300. wpa_printf(MSG_INFO, "IKEV2: No acceptable proposal found");
  301. return -1;
  302. }
  303. wpa_printf(MSG_DEBUG, "IKEV2: Accepted proposal #%d: ENCR:%d PRF:%d "
  304. "INTEG:%d D-H:%d", data->proposal.proposal_num,
  305. data->proposal.encr, data->proposal.prf,
  306. data->proposal.integ, data->proposal.dh);
  307. return 0;
  308. }
  309. static int ikev2_process_ker(struct ikev2_initiator_data *data,
  310. const u8 *ker, size_t ker_len)
  311. {
  312. u16 group;
  313. /*
  314. * Key Exchange Payload:
  315. * DH Group # (16 bits)
  316. * RESERVED (16 bits)
  317. * Key Exchange Data (Diffie-Hellman public value)
  318. */
  319. if (ker == NULL) {
  320. wpa_printf(MSG_INFO, "IKEV2: KEr not received");
  321. return -1;
  322. }
  323. if (ker_len < 4 + 96) {
  324. wpa_printf(MSG_INFO, "IKEV2: Too show Key Exchange Payload");
  325. return -1;
  326. }
  327. group = WPA_GET_BE16(ker);
  328. wpa_printf(MSG_DEBUG, "IKEV2: KEr DH Group #%u", group);
  329. if (group != data->proposal.dh) {
  330. wpa_printf(MSG_DEBUG, "IKEV2: KEr DH Group #%u does not match "
  331. "with the selected proposal (%u)",
  332. group, data->proposal.dh);
  333. return -1;
  334. }
  335. if (data->dh == NULL) {
  336. wpa_printf(MSG_INFO, "IKEV2: Unsupported DH group");
  337. return -1;
  338. }
  339. /* RFC 4306, Section 3.4:
  340. * The length of DH public value MUST be equal to the lenght of the
  341. * prime modulus.
  342. */
  343. if (ker_len - 4 != data->dh->prime_len) {
  344. wpa_printf(MSG_INFO, "IKEV2: Invalid DH public value length "
  345. "%ld (expected %ld)",
  346. (long) (ker_len - 4), (long) data->dh->prime_len);
  347. return -1;
  348. }
  349. wpabuf_free(data->r_dh_public);
  350. data->r_dh_public = wpabuf_alloc_copy(ker + 4, ker_len - 4);
  351. if (data->r_dh_public == NULL)
  352. return -1;
  353. wpa_hexdump_buf(MSG_DEBUG, "IKEV2: KEr Diffie-Hellman Public Value",
  354. data->r_dh_public);
  355. return 0;
  356. }
  357. static int ikev2_process_nr(struct ikev2_initiator_data *data,
  358. const u8 *nr, size_t nr_len)
  359. {
  360. if (nr == NULL) {
  361. wpa_printf(MSG_INFO, "IKEV2: Nr not received");
  362. return -1;
  363. }
  364. if (nr_len < IKEV2_NONCE_MIN_LEN || nr_len > IKEV2_NONCE_MAX_LEN) {
  365. wpa_printf(MSG_INFO, "IKEV2: Invalid Nr length %ld",
  366. (long) nr_len);
  367. return -1;
  368. }
  369. data->r_nonce_len = nr_len;
  370. os_memcpy(data->r_nonce, nr, nr_len);
  371. wpa_hexdump(MSG_MSGDUMP, "IKEV2: Nr",
  372. data->r_nonce, data->r_nonce_len);
  373. return 0;
  374. }
  375. static int ikev2_process_sa_init_encr(struct ikev2_initiator_data *data,
  376. const struct ikev2_hdr *hdr,
  377. const u8 *encrypted,
  378. size_t encrypted_len, u8 next_payload)
  379. {
  380. u8 *decrypted;
  381. size_t decrypted_len;
  382. struct ikev2_payloads pl;
  383. int ret = 0;
  384. decrypted = ikev2_decrypt_payload(data->proposal.encr,
  385. data->proposal.integ, &data->keys, 0,
  386. hdr, encrypted, encrypted_len,
  387. &decrypted_len);
  388. if (decrypted == NULL)
  389. return -1;
  390. wpa_printf(MSG_DEBUG, "IKEV2: Processing decrypted payloads");
  391. if (ikev2_parse_payloads(&pl, next_payload, decrypted,
  392. decrypted + decrypted_len) < 0) {
  393. wpa_printf(MSG_INFO, "IKEV2: Failed to parse decrypted "
  394. "payloads");
  395. return -1;
  396. }
  397. if (pl.idr)
  398. ret = ikev2_process_idr(data, pl.idr, pl.idr_len);
  399. os_free(decrypted);
  400. return ret;
  401. }
  402. static int ikev2_process_sa_init(struct ikev2_initiator_data *data,
  403. const struct ikev2_hdr *hdr,
  404. struct ikev2_payloads *pl)
  405. {
  406. if (ikev2_process_sar1(data, pl->sa, pl->sa_len) < 0 ||
  407. ikev2_process_ker(data, pl->ke, pl->ke_len) < 0 ||
  408. ikev2_process_nr(data, pl->nonce, pl->nonce_len) < 0)
  409. return -1;
  410. os_memcpy(data->r_spi, hdr->r_spi, IKEV2_SPI_LEN);
  411. if (ikev2_derive_keys(data) < 0)
  412. return -1;
  413. if (pl->encrypted) {
  414. wpa_printf(MSG_DEBUG, "IKEV2: Encrypted payload in SA_INIT - "
  415. "try to get IDr from it");
  416. if (ikev2_process_sa_init_encr(data, hdr, pl->encrypted,
  417. pl->encrypted_len,
  418. pl->encr_next_payload) < 0) {
  419. wpa_printf(MSG_INFO, "IKEV2: Failed to process "
  420. "encrypted payload");
  421. return -1;
  422. }
  423. }
  424. data->state = SA_AUTH;
  425. return 0;
  426. }
  427. static int ikev2_process_idr(struct ikev2_initiator_data *data,
  428. const u8 *idr, size_t idr_len)
  429. {
  430. u8 id_type;
  431. if (idr == NULL) {
  432. wpa_printf(MSG_INFO, "IKEV2: No IDr received");
  433. return -1;
  434. }
  435. if (idr_len < 4) {
  436. wpa_printf(MSG_INFO, "IKEV2: Too short IDr payload");
  437. return -1;
  438. }
  439. id_type = idr[0];
  440. idr += 4;
  441. idr_len -= 4;
  442. wpa_printf(MSG_DEBUG, "IKEV2: IDr ID Type %d", id_type);
  443. wpa_hexdump_ascii(MSG_DEBUG, "IKEV2: IDr", idr, idr_len);
  444. if (data->IDr) {
  445. if (id_type != data->IDr_type || idr_len != data->IDr_len ||
  446. os_memcmp(idr, data->IDr, idr_len) != 0) {
  447. wpa_printf(MSG_INFO, "IKEV2: IDr differs from the one "
  448. "received earlier");
  449. wpa_printf(MSG_DEBUG, "IKEV2: Previous IDr ID Type %d",
  450. id_type);
  451. wpa_hexdump_ascii(MSG_DEBUG, "Previous IKEV2: IDr",
  452. data->IDr, data->IDr_len);
  453. return -1;
  454. }
  455. os_free(data->IDr);
  456. }
  457. data->IDr = os_malloc(idr_len);
  458. if (data->IDr == NULL)
  459. return -1;
  460. os_memcpy(data->IDr, idr, idr_len);
  461. data->IDr_len = idr_len;
  462. data->IDr_type = id_type;
  463. return 0;
  464. }
  465. static int ikev2_process_cert(struct ikev2_initiator_data *data,
  466. const u8 *cert, size_t cert_len)
  467. {
  468. u8 cert_encoding;
  469. if (cert == NULL) {
  470. if (data->peer_auth == PEER_AUTH_CERT) {
  471. wpa_printf(MSG_INFO, "IKEV2: No Certificate received");
  472. return -1;
  473. }
  474. return 0;
  475. }
  476. if (cert_len < 1) {
  477. wpa_printf(MSG_INFO, "IKEV2: No Cert Encoding field");
  478. return -1;
  479. }
  480. cert_encoding = cert[0];
  481. cert++;
  482. cert_len--;
  483. wpa_printf(MSG_DEBUG, "IKEV2: Cert Encoding %d", cert_encoding);
  484. wpa_hexdump(MSG_MSGDUMP, "IKEV2: Certificate Data", cert, cert_len);
  485. /* TODO: validate certificate */
  486. return 0;
  487. }
  488. static int ikev2_process_auth_cert(struct ikev2_initiator_data *data,
  489. u8 method, const u8 *auth, size_t auth_len)
  490. {
  491. if (method != AUTH_RSA_SIGN) {
  492. wpa_printf(MSG_INFO, "IKEV2: Unsupported authentication "
  493. "method %d", method);
  494. return -1;
  495. }
  496. /* TODO: validate AUTH */
  497. return 0;
  498. }
  499. static int ikev2_process_auth_secret(struct ikev2_initiator_data *data,
  500. u8 method, const u8 *auth,
  501. size_t auth_len)
  502. {
  503. u8 auth_data[IKEV2_MAX_HASH_LEN];
  504. const struct ikev2_prf_alg *prf;
  505. if (method != AUTH_SHARED_KEY_MIC) {
  506. wpa_printf(MSG_INFO, "IKEV2: Unsupported authentication "
  507. "method %d", method);
  508. return -1;
  509. }
  510. /* msg | Ni | prf(SK_pr,IDr') */
  511. if (ikev2_derive_auth_data(data->proposal.prf, data->r_sign_msg,
  512. data->IDr, data->IDr_len, data->IDr_type,
  513. &data->keys, 0, data->shared_secret,
  514. data->shared_secret_len,
  515. data->i_nonce, data->i_nonce_len,
  516. data->key_pad, data->key_pad_len,
  517. auth_data) < 0) {
  518. wpa_printf(MSG_INFO, "IKEV2: Could not derive AUTH data");
  519. return -1;
  520. }
  521. wpabuf_free(data->r_sign_msg);
  522. data->r_sign_msg = NULL;
  523. prf = ikev2_get_prf(data->proposal.prf);
  524. if (prf == NULL)
  525. return -1;
  526. if (auth_len != prf->hash_len ||
  527. os_memcmp(auth, auth_data, auth_len) != 0) {
  528. wpa_printf(MSG_INFO, "IKEV2: Invalid Authentication Data");
  529. wpa_hexdump(MSG_DEBUG, "IKEV2: Received Authentication Data",
  530. auth, auth_len);
  531. wpa_hexdump(MSG_DEBUG, "IKEV2: Expected Authentication Data",
  532. auth_data, prf->hash_len);
  533. return -1;
  534. }
  535. wpa_printf(MSG_DEBUG, "IKEV2: Peer authenticated successfully "
  536. "using shared keys");
  537. return 0;
  538. }
  539. static int ikev2_process_auth(struct ikev2_initiator_data *data,
  540. const u8 *auth, size_t auth_len)
  541. {
  542. u8 auth_method;
  543. if (auth == NULL) {
  544. wpa_printf(MSG_INFO, "IKEV2: No Authentication Payload");
  545. return -1;
  546. }
  547. if (auth_len < 4) {
  548. wpa_printf(MSG_INFO, "IKEV2: Too short Authentication "
  549. "Payload");
  550. return -1;
  551. }
  552. auth_method = auth[0];
  553. auth += 4;
  554. auth_len -= 4;
  555. wpa_printf(MSG_DEBUG, "IKEV2: Auth Method %d", auth_method);
  556. wpa_hexdump(MSG_MSGDUMP, "IKEV2: Authentication Data", auth, auth_len);
  557. switch (data->peer_auth) {
  558. case PEER_AUTH_CERT:
  559. return ikev2_process_auth_cert(data, auth_method, auth,
  560. auth_len);
  561. case PEER_AUTH_SECRET:
  562. return ikev2_process_auth_secret(data, auth_method, auth,
  563. auth_len);
  564. }
  565. return -1;
  566. }
  567. static int ikev2_process_sa_auth_decrypted(struct ikev2_initiator_data *data,
  568. u8 next_payload,
  569. u8 *payload, size_t payload_len)
  570. {
  571. struct ikev2_payloads pl;
  572. wpa_printf(MSG_DEBUG, "IKEV2: Processing decrypted payloads");
  573. if (ikev2_parse_payloads(&pl, next_payload, payload, payload +
  574. payload_len) < 0) {
  575. wpa_printf(MSG_INFO, "IKEV2: Failed to parse decrypted "
  576. "payloads");
  577. return -1;
  578. }
  579. if (ikev2_process_idr(data, pl.idr, pl.idr_len) < 0 ||
  580. ikev2_process_cert(data, pl.cert, pl.cert_len) < 0 ||
  581. ikev2_process_auth(data, pl.auth, pl.auth_len) < 0)
  582. return -1;
  583. return 0;
  584. }
  585. static int ikev2_process_sa_auth(struct ikev2_initiator_data *data,
  586. const struct ikev2_hdr *hdr,
  587. struct ikev2_payloads *pl)
  588. {
  589. u8 *decrypted;
  590. size_t decrypted_len;
  591. int ret;
  592. decrypted = ikev2_decrypt_payload(data->proposal.encr,
  593. data->proposal.integ,
  594. &data->keys, 0, hdr, pl->encrypted,
  595. pl->encrypted_len, &decrypted_len);
  596. if (decrypted == NULL)
  597. return -1;
  598. ret = ikev2_process_sa_auth_decrypted(data, pl->encr_next_payload,
  599. decrypted, decrypted_len);
  600. os_free(decrypted);
  601. if (ret == 0 && !data->unknown_user) {
  602. wpa_printf(MSG_DEBUG, "IKEV2: Authentication completed");
  603. data->state = IKEV2_DONE;
  604. }
  605. return ret;
  606. }
  607. static int ikev2_validate_rx_state(struct ikev2_initiator_data *data,
  608. u8 exchange_type, u32 message_id)
  609. {
  610. switch (data->state) {
  611. case SA_INIT:
  612. /* Expect to receive IKE_SA_INIT: HDR, SAr, KEr, Nr, [CERTREQ],
  613. * [SK{IDr}] */
  614. if (exchange_type != IKE_SA_INIT) {
  615. wpa_printf(MSG_INFO, "IKEV2: Unexpected Exchange Type "
  616. "%u in SA_INIT state", exchange_type);
  617. return -1;
  618. }
  619. if (message_id != 0) {
  620. wpa_printf(MSG_INFO, "IKEV2: Unexpected Message ID %u "
  621. "in SA_INIT state", message_id);
  622. return -1;
  623. }
  624. break;
  625. case SA_AUTH:
  626. /* Expect to receive IKE_SA_AUTH:
  627. * HDR, SK {IDr, [CERT,] [CERTREQ,] [NFID,] AUTH}
  628. */
  629. if (exchange_type != IKE_SA_AUTH) {
  630. wpa_printf(MSG_INFO, "IKEV2: Unexpected Exchange Type "
  631. "%u in SA_AUTH state", exchange_type);
  632. return -1;
  633. }
  634. if (message_id != 1) {
  635. wpa_printf(MSG_INFO, "IKEV2: Unexpected Message ID %u "
  636. "in SA_AUTH state", message_id);
  637. return -1;
  638. }
  639. break;
  640. case CHILD_SA:
  641. if (exchange_type != CREATE_CHILD_SA) {
  642. wpa_printf(MSG_INFO, "IKEV2: Unexpected Exchange Type "
  643. "%u in CHILD_SA state", exchange_type);
  644. return -1;
  645. }
  646. if (message_id != 2) {
  647. wpa_printf(MSG_INFO, "IKEV2: Unexpected Message ID %u "
  648. "in CHILD_SA state", message_id);
  649. return -1;
  650. }
  651. break;
  652. case IKEV2_DONE:
  653. return -1;
  654. }
  655. return 0;
  656. }
  657. int ikev2_initiator_process(struct ikev2_initiator_data *data,
  658. const struct wpabuf *buf)
  659. {
  660. const struct ikev2_hdr *hdr;
  661. u32 length, message_id;
  662. const u8 *pos, *end;
  663. struct ikev2_payloads pl;
  664. wpa_printf(MSG_MSGDUMP, "IKEV2: Received message (len %lu)",
  665. (unsigned long) wpabuf_len(buf));
  666. if (wpabuf_len(buf) < sizeof(*hdr)) {
  667. wpa_printf(MSG_INFO, "IKEV2: Too short frame to include HDR");
  668. return -1;
  669. }
  670. hdr = (const struct ikev2_hdr *) wpabuf_head(buf);
  671. end = wpabuf_head_u8(buf) + wpabuf_len(buf);
  672. message_id = WPA_GET_BE32(hdr->message_id);
  673. length = WPA_GET_BE32(hdr->length);
  674. wpa_hexdump(MSG_DEBUG, "IKEV2: IKE_SA Initiator's SPI",
  675. hdr->i_spi, IKEV2_SPI_LEN);
  676. wpa_hexdump(MSG_DEBUG, "IKEV2: IKE_SA Initiator's SPI",
  677. hdr->r_spi, IKEV2_SPI_LEN);
  678. wpa_printf(MSG_DEBUG, "IKEV2: Next Payload: %u Version: 0x%x "
  679. "Exchange Type: %u",
  680. hdr->next_payload, hdr->version, hdr->exchange_type);
  681. wpa_printf(MSG_DEBUG, "IKEV2: Message ID: %u Length: %u",
  682. message_id, length);
  683. if (hdr->version != IKEV2_VERSION) {
  684. wpa_printf(MSG_INFO, "IKEV2: Unsupported HDR version 0x%x "
  685. "(expected 0x%x)", hdr->version, IKEV2_VERSION);
  686. return -1;
  687. }
  688. if (length != wpabuf_len(buf)) {
  689. wpa_printf(MSG_INFO, "IKEV2: Invalid length (HDR: %lu != "
  690. "RX: %lu)", (unsigned long) length,
  691. (unsigned long) wpabuf_len(buf));
  692. return -1;
  693. }
  694. if (ikev2_validate_rx_state(data, hdr->exchange_type, message_id) < 0)
  695. return -1;
  696. if ((hdr->flags & (IKEV2_HDR_INITIATOR | IKEV2_HDR_RESPONSE)) !=
  697. IKEV2_HDR_RESPONSE) {
  698. wpa_printf(MSG_INFO, "IKEV2: Unexpected Flags value 0x%x",
  699. hdr->flags);
  700. return -1;
  701. }
  702. if (data->state != SA_INIT) {
  703. if (os_memcmp(data->i_spi, hdr->i_spi, IKEV2_SPI_LEN) != 0) {
  704. wpa_printf(MSG_INFO, "IKEV2: Unexpected IKE_SA "
  705. "Initiator's SPI");
  706. return -1;
  707. }
  708. if (os_memcmp(data->r_spi, hdr->r_spi, IKEV2_SPI_LEN) != 0) {
  709. wpa_printf(MSG_INFO, "IKEV2: Unexpected IKE_SA "
  710. "Responder's SPI");
  711. return -1;
  712. }
  713. }
  714. pos = (const u8 *) (hdr + 1);
  715. if (ikev2_parse_payloads(&pl, hdr->next_payload, pos, end) < 0)
  716. return -1;
  717. switch (data->state) {
  718. case SA_INIT:
  719. if (ikev2_process_sa_init(data, hdr, &pl) < 0)
  720. return -1;
  721. wpabuf_free(data->r_sign_msg);
  722. data->r_sign_msg = wpabuf_dup(buf);
  723. break;
  724. case SA_AUTH:
  725. if (ikev2_process_sa_auth(data, hdr, &pl) < 0)
  726. return -1;
  727. break;
  728. case CHILD_SA:
  729. case IKEV2_DONE:
  730. break;
  731. }
  732. return 0;
  733. }
  734. static void ikev2_build_hdr(struct ikev2_initiator_data *data,
  735. struct wpabuf *msg, u8 exchange_type,
  736. u8 next_payload, u32 message_id)
  737. {
  738. struct ikev2_hdr *hdr;
  739. wpa_printf(MSG_DEBUG, "IKEV2: Adding HDR");
  740. /* HDR - RFC 4306, Sect. 3.1 */
  741. hdr = wpabuf_put(msg, sizeof(*hdr));
  742. os_memcpy(hdr->i_spi, data->i_spi, IKEV2_SPI_LEN);
  743. os_memcpy(hdr->r_spi, data->r_spi, IKEV2_SPI_LEN);
  744. hdr->next_payload = next_payload;
  745. hdr->version = IKEV2_VERSION;
  746. hdr->exchange_type = exchange_type;
  747. hdr->flags = IKEV2_HDR_INITIATOR;
  748. WPA_PUT_BE32(hdr->message_id, message_id);
  749. }
  750. static int ikev2_build_sai(struct ikev2_initiator_data *data,
  751. struct wpabuf *msg, u8 next_payload)
  752. {
  753. struct ikev2_payload_hdr *phdr;
  754. size_t plen;
  755. struct ikev2_proposal *p;
  756. struct ikev2_transform *t;
  757. wpa_printf(MSG_DEBUG, "IKEV2: Adding SAi payload");
  758. /* SAi1 - RFC 4306, Sect. 2.7 and 3.3 */
  759. phdr = wpabuf_put(msg, sizeof(*phdr));
  760. phdr->next_payload = next_payload;
  761. phdr->flags = 0;
  762. /* TODO: support for multiple proposals */
  763. p = wpabuf_put(msg, sizeof(*p));
  764. p->proposal_num = data->proposal.proposal_num;
  765. p->protocol_id = IKEV2_PROTOCOL_IKE;
  766. p->num_transforms = 4;
  767. t = wpabuf_put(msg, sizeof(*t));
  768. t->type = 3;
  769. t->transform_type = IKEV2_TRANSFORM_ENCR;
  770. WPA_PUT_BE16(t->transform_id, data->proposal.encr);
  771. if (data->proposal.encr == ENCR_AES_CBC) {
  772. /* Transform Attribute: Key Len = 128 bits */
  773. wpabuf_put_be16(msg, 0x800e); /* AF=1, AttrType=14 */
  774. wpabuf_put_be16(msg, 128); /* 128-bit key */
  775. }
  776. plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) t;
  777. WPA_PUT_BE16(t->transform_length, plen);
  778. t = wpabuf_put(msg, sizeof(*t));
  779. t->type = 3;
  780. WPA_PUT_BE16(t->transform_length, sizeof(*t));
  781. t->transform_type = IKEV2_TRANSFORM_PRF;
  782. WPA_PUT_BE16(t->transform_id, data->proposal.prf);
  783. t = wpabuf_put(msg, sizeof(*t));
  784. t->type = 3;
  785. WPA_PUT_BE16(t->transform_length, sizeof(*t));
  786. t->transform_type = IKEV2_TRANSFORM_INTEG;
  787. WPA_PUT_BE16(t->transform_id, data->proposal.integ);
  788. t = wpabuf_put(msg, sizeof(*t));
  789. WPA_PUT_BE16(t->transform_length, sizeof(*t));
  790. t->transform_type = IKEV2_TRANSFORM_DH;
  791. WPA_PUT_BE16(t->transform_id, data->proposal.dh);
  792. plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) p;
  793. WPA_PUT_BE16(p->proposal_length, plen);
  794. plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
  795. WPA_PUT_BE16(phdr->payload_length, plen);
  796. return 0;
  797. }
  798. static int ikev2_build_kei(struct ikev2_initiator_data *data,
  799. struct wpabuf *msg, u8 next_payload)
  800. {
  801. struct ikev2_payload_hdr *phdr;
  802. size_t plen;
  803. struct wpabuf *pv;
  804. wpa_printf(MSG_DEBUG, "IKEV2: Adding KEi payload");
  805. data->dh = dh_groups_get(data->proposal.dh);
  806. pv = dh_init(data->dh, &data->i_dh_private);
  807. if (pv == NULL) {
  808. wpa_printf(MSG_DEBUG, "IKEV2: Failed to initialize DH");
  809. return -1;
  810. }
  811. /* KEi - RFC 4306, Sect. 3.4 */
  812. phdr = wpabuf_put(msg, sizeof(*phdr));
  813. phdr->next_payload = next_payload;
  814. phdr->flags = 0;
  815. wpabuf_put_be16(msg, data->proposal.dh); /* DH Group # */
  816. wpabuf_put(msg, 2); /* RESERVED */
  817. /*
  818. * RFC 4306, Sect. 3.4: possible zero padding for public value to
  819. * match the length of the prime.
  820. */
  821. wpabuf_put(msg, data->dh->prime_len - wpabuf_len(pv));
  822. wpabuf_put_buf(msg, pv);
  823. os_free(pv);
  824. plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
  825. WPA_PUT_BE16(phdr->payload_length, plen);
  826. return 0;
  827. }
  828. static int ikev2_build_ni(struct ikev2_initiator_data *data,
  829. struct wpabuf *msg, u8 next_payload)
  830. {
  831. struct ikev2_payload_hdr *phdr;
  832. size_t plen;
  833. wpa_printf(MSG_DEBUG, "IKEV2: Adding Ni payload");
  834. /* Ni - RFC 4306, Sect. 3.9 */
  835. phdr = wpabuf_put(msg, sizeof(*phdr));
  836. phdr->next_payload = next_payload;
  837. phdr->flags = 0;
  838. wpabuf_put_data(msg, data->i_nonce, data->i_nonce_len);
  839. plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
  840. WPA_PUT_BE16(phdr->payload_length, plen);
  841. return 0;
  842. }
  843. static int ikev2_build_idi(struct ikev2_initiator_data *data,
  844. struct wpabuf *msg, u8 next_payload)
  845. {
  846. struct ikev2_payload_hdr *phdr;
  847. size_t plen;
  848. wpa_printf(MSG_DEBUG, "IKEV2: Adding IDi payload");
  849. if (data->IDi == NULL) {
  850. wpa_printf(MSG_INFO, "IKEV2: No IDi available");
  851. return -1;
  852. }
  853. /* IDi - RFC 4306, Sect. 3.5 */
  854. phdr = wpabuf_put(msg, sizeof(*phdr));
  855. phdr->next_payload = next_payload;
  856. phdr->flags = 0;
  857. wpabuf_put_u8(msg, ID_KEY_ID);
  858. wpabuf_put(msg, 3); /* RESERVED */
  859. wpabuf_put_data(msg, data->IDi, data->IDi_len);
  860. plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
  861. WPA_PUT_BE16(phdr->payload_length, plen);
  862. return 0;
  863. }
  864. static int ikev2_build_auth(struct ikev2_initiator_data *data,
  865. struct wpabuf *msg, u8 next_payload)
  866. {
  867. struct ikev2_payload_hdr *phdr;
  868. size_t plen;
  869. const struct ikev2_prf_alg *prf;
  870. wpa_printf(MSG_DEBUG, "IKEV2: Adding AUTH payload");
  871. prf = ikev2_get_prf(data->proposal.prf);
  872. if (prf == NULL)
  873. return -1;
  874. /* Authentication - RFC 4306, Sect. 3.8 */
  875. phdr = wpabuf_put(msg, sizeof(*phdr));
  876. phdr->next_payload = next_payload;
  877. phdr->flags = 0;
  878. wpabuf_put_u8(msg, AUTH_SHARED_KEY_MIC);
  879. wpabuf_put(msg, 3); /* RESERVED */
  880. /* msg | Nr | prf(SK_pi,IDi') */
  881. if (ikev2_derive_auth_data(data->proposal.prf, data->i_sign_msg,
  882. data->IDi, data->IDi_len, ID_KEY_ID,
  883. &data->keys, 1, data->shared_secret,
  884. data->shared_secret_len,
  885. data->r_nonce, data->r_nonce_len,
  886. data->key_pad, data->key_pad_len,
  887. wpabuf_put(msg, prf->hash_len)) < 0) {
  888. wpa_printf(MSG_INFO, "IKEV2: Could not derive AUTH data");
  889. return -1;
  890. }
  891. wpabuf_free(data->i_sign_msg);
  892. data->i_sign_msg = NULL;
  893. plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
  894. WPA_PUT_BE16(phdr->payload_length, plen);
  895. return 0;
  896. }
  897. static struct wpabuf * ikev2_build_sa_init(struct ikev2_initiator_data *data)
  898. {
  899. struct wpabuf *msg;
  900. /* build IKE_SA_INIT: HDR, SAi, KEi, Ni */
  901. if (os_get_random(data->i_spi, IKEV2_SPI_LEN))
  902. return NULL;
  903. wpa_hexdump(MSG_DEBUG, "IKEV2: IKE_SA Initiator's SPI",
  904. data->i_spi, IKEV2_SPI_LEN);
  905. data->i_nonce_len = IKEV2_NONCE_MIN_LEN;
  906. if (os_get_random(data->i_nonce, data->i_nonce_len))
  907. return NULL;
  908. wpa_hexdump(MSG_DEBUG, "IKEV2: Ni", data->i_nonce, data->i_nonce_len);
  909. msg = wpabuf_alloc(sizeof(struct ikev2_hdr) + 1000);
  910. if (msg == NULL)
  911. return NULL;
  912. ikev2_build_hdr(data, msg, IKE_SA_INIT, IKEV2_PAYLOAD_SA, 0);
  913. if (ikev2_build_sai(data, msg, IKEV2_PAYLOAD_KEY_EXCHANGE) ||
  914. ikev2_build_kei(data, msg, IKEV2_PAYLOAD_NONCE) ||
  915. ikev2_build_ni(data, msg, IKEV2_PAYLOAD_NO_NEXT_PAYLOAD)) {
  916. wpabuf_free(msg);
  917. return NULL;
  918. }
  919. ikev2_update_hdr(msg);
  920. wpa_hexdump_buf(MSG_MSGDUMP, "IKEV2: Sending message (SA_INIT)", msg);
  921. wpabuf_free(data->i_sign_msg);
  922. data->i_sign_msg = wpabuf_dup(msg);
  923. return msg;
  924. }
  925. static struct wpabuf * ikev2_build_sa_auth(struct ikev2_initiator_data *data)
  926. {
  927. struct wpabuf *msg, *plain;
  928. const u8 *secret;
  929. size_t secret_len;
  930. secret = data->get_shared_secret(data->cb_ctx, data->IDr,
  931. data->IDr_len, &secret_len);
  932. if (secret == NULL) {
  933. wpa_printf(MSG_INFO, "IKEV2: Could not get shared secret - "
  934. "use fake value");
  935. /* RFC 5106, Sect. 7:
  936. * Use a random key to fake AUTH generation in order to prevent
  937. * probing of user identities.
  938. */
  939. data->unknown_user = 1;
  940. os_free(data->shared_secret);
  941. data->shared_secret = os_malloc(16);
  942. if (data->shared_secret == NULL)
  943. return NULL;
  944. data->shared_secret_len = 16;
  945. if (os_get_random(data->shared_secret, 16))
  946. return NULL;
  947. } else {
  948. os_free(data->shared_secret);
  949. data->shared_secret = os_malloc(secret_len);
  950. if (data->shared_secret == NULL)
  951. return NULL;
  952. os_memcpy(data->shared_secret, secret, secret_len);
  953. data->shared_secret_len = secret_len;
  954. }
  955. /* build IKE_SA_AUTH: HDR, SK {IDi, [CERT,] [CERTREQ,] AUTH} */
  956. msg = wpabuf_alloc(sizeof(struct ikev2_hdr) + data->IDr_len + 1000);
  957. if (msg == NULL)
  958. return NULL;
  959. ikev2_build_hdr(data, msg, IKE_SA_AUTH, IKEV2_PAYLOAD_ENCRYPTED, 1);
  960. plain = wpabuf_alloc(data->IDr_len + 1000);
  961. if (plain == NULL) {
  962. wpabuf_free(msg);
  963. return NULL;
  964. }
  965. if (ikev2_build_idi(data, plain, IKEV2_PAYLOAD_AUTHENTICATION) ||
  966. ikev2_build_auth(data, plain, IKEV2_PAYLOAD_NO_NEXT_PAYLOAD) ||
  967. ikev2_build_encrypted(data->proposal.encr, data->proposal.integ,
  968. &data->keys, 1, msg, plain,
  969. IKEV2_PAYLOAD_IDi)) {
  970. wpabuf_free(plain);
  971. wpabuf_free(msg);
  972. return NULL;
  973. }
  974. wpabuf_free(plain);
  975. wpa_hexdump_buf(MSG_MSGDUMP, "IKEV2: Sending message (SA_AUTH)", msg);
  976. return msg;
  977. }
  978. struct wpabuf * ikev2_initiator_build(struct ikev2_initiator_data *data)
  979. {
  980. switch (data->state) {
  981. case SA_INIT:
  982. return ikev2_build_sa_init(data);
  983. case SA_AUTH:
  984. return ikev2_build_sa_auth(data);
  985. case CHILD_SA:
  986. return NULL;
  987. case IKEV2_DONE:
  988. return NULL;
  989. }
  990. return NULL;
  991. }