ikev2_common.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * IKEv2 common routines for initiator and responder
  3. * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto/crypto.h"
  11. #include "crypto/md5.h"
  12. #include "crypto/sha1.h"
  13. #include "crypto/random.h"
  14. #include "ikev2_common.h"
  15. static struct ikev2_integ_alg ikev2_integ_algs[] = {
  16. { AUTH_HMAC_SHA1_96, 20, 12 },
  17. { AUTH_HMAC_MD5_96, 16, 12 }
  18. };
  19. #define NUM_INTEG_ALGS ARRAY_SIZE(ikev2_integ_algs)
  20. static struct ikev2_prf_alg ikev2_prf_algs[] = {
  21. { PRF_HMAC_SHA1, 20, 20 },
  22. { PRF_HMAC_MD5, 16, 16 }
  23. };
  24. #define NUM_PRF_ALGS ARRAY_SIZE(ikev2_prf_algs)
  25. static struct ikev2_encr_alg ikev2_encr_algs[] = {
  26. { ENCR_AES_CBC, 16, 16 }, /* only 128-bit keys supported for now */
  27. { ENCR_3DES, 24, 8 }
  28. };
  29. #define NUM_ENCR_ALGS ARRAY_SIZE(ikev2_encr_algs)
  30. const struct ikev2_integ_alg * ikev2_get_integ(int id)
  31. {
  32. size_t i;
  33. for (i = 0; i < NUM_INTEG_ALGS; i++) {
  34. if (ikev2_integ_algs[i].id == id)
  35. return &ikev2_integ_algs[i];
  36. }
  37. return NULL;
  38. }
  39. int ikev2_integ_hash(int alg, const u8 *key, size_t key_len, const u8 *data,
  40. size_t data_len, u8 *hash)
  41. {
  42. u8 tmphash[IKEV2_MAX_HASH_LEN];
  43. switch (alg) {
  44. case AUTH_HMAC_SHA1_96:
  45. if (key_len != 20)
  46. return -1;
  47. hmac_sha1(key, key_len, data, data_len, tmphash);
  48. os_memcpy(hash, tmphash, 12);
  49. break;
  50. case AUTH_HMAC_MD5_96:
  51. if (key_len != 16)
  52. return -1;
  53. hmac_md5(key, key_len, data, data_len, tmphash);
  54. os_memcpy(hash, tmphash, 12);
  55. break;
  56. default:
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. const struct ikev2_prf_alg * ikev2_get_prf(int id)
  62. {
  63. size_t i;
  64. for (i = 0; i < NUM_PRF_ALGS; i++) {
  65. if (ikev2_prf_algs[i].id == id)
  66. return &ikev2_prf_algs[i];
  67. }
  68. return NULL;
  69. }
  70. int ikev2_prf_hash(int alg, const u8 *key, size_t key_len,
  71. size_t num_elem, const u8 *addr[], const size_t *len,
  72. u8 *hash)
  73. {
  74. switch (alg) {
  75. case PRF_HMAC_SHA1:
  76. hmac_sha1_vector(key, key_len, num_elem, addr, len, hash);
  77. break;
  78. case PRF_HMAC_MD5:
  79. hmac_md5_vector(key, key_len, num_elem, addr, len, hash);
  80. break;
  81. default:
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. int ikev2_prf_plus(int alg, const u8 *key, size_t key_len,
  87. const u8 *data, size_t data_len,
  88. u8 *out, size_t out_len)
  89. {
  90. u8 hash[IKEV2_MAX_HASH_LEN];
  91. size_t hash_len;
  92. u8 iter, *pos, *end;
  93. const u8 *addr[3];
  94. size_t len[3];
  95. const struct ikev2_prf_alg *prf;
  96. int res;
  97. prf = ikev2_get_prf(alg);
  98. if (prf == NULL)
  99. return -1;
  100. hash_len = prf->hash_len;
  101. addr[0] = hash;
  102. len[0] = hash_len;
  103. addr[1] = data;
  104. len[1] = data_len;
  105. addr[2] = &iter;
  106. len[2] = 1;
  107. pos = out;
  108. end = out + out_len;
  109. iter = 1;
  110. while (pos < end) {
  111. size_t clen;
  112. if (iter == 1)
  113. res = ikev2_prf_hash(alg, key, key_len, 2, &addr[1],
  114. &len[1], hash);
  115. else
  116. res = ikev2_prf_hash(alg, key, key_len, 3, addr, len,
  117. hash);
  118. if (res < 0)
  119. return -1;
  120. clen = hash_len;
  121. if ((int) clen > end - pos)
  122. clen = end - pos;
  123. os_memcpy(pos, hash, clen);
  124. pos += clen;
  125. iter++;
  126. }
  127. return 0;
  128. }
  129. const struct ikev2_encr_alg * ikev2_get_encr(int id)
  130. {
  131. size_t i;
  132. for (i = 0; i < NUM_ENCR_ALGS; i++) {
  133. if (ikev2_encr_algs[i].id == id)
  134. return &ikev2_encr_algs[i];
  135. }
  136. return NULL;
  137. }
  138. #ifdef CCNS_PL
  139. /* from des.c */
  140. struct des3_key_s {
  141. u32 ek[3][32];
  142. u32 dk[3][32];
  143. };
  144. void des3_key_setup(const u8 *key, struct des3_key_s *dkey);
  145. void des3_encrypt(const u8 *plain, const struct des3_key_s *key, u8 *crypt);
  146. void des3_decrypt(const u8 *crypt, const struct des3_key_s *key, u8 *plain);
  147. #endif /* CCNS_PL */
  148. int ikev2_encr_encrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
  149. const u8 *plain, u8 *crypt, size_t len)
  150. {
  151. struct crypto_cipher *cipher;
  152. int encr_alg;
  153. #ifdef CCNS_PL
  154. if (alg == ENCR_3DES) {
  155. struct des3_key_s des3key;
  156. size_t i, blocks;
  157. u8 *pos;
  158. /* ECB mode is used incorrectly for 3DES!? */
  159. if (key_len != 24) {
  160. wpa_printf(MSG_INFO, "IKEV2: Invalid encr key length");
  161. return -1;
  162. }
  163. des3_key_setup(key, &des3key);
  164. blocks = len / 8;
  165. pos = crypt;
  166. for (i = 0; i < blocks; i++) {
  167. des3_encrypt(pos, &des3key, pos);
  168. pos += 8;
  169. }
  170. } else {
  171. #endif /* CCNS_PL */
  172. switch (alg) {
  173. case ENCR_3DES:
  174. encr_alg = CRYPTO_CIPHER_ALG_3DES;
  175. break;
  176. case ENCR_AES_CBC:
  177. encr_alg = CRYPTO_CIPHER_ALG_AES;
  178. break;
  179. default:
  180. wpa_printf(MSG_DEBUG, "IKEV2: Unsupported encr alg %d", alg);
  181. return -1;
  182. }
  183. cipher = crypto_cipher_init(encr_alg, iv, key, key_len);
  184. if (cipher == NULL) {
  185. wpa_printf(MSG_INFO, "IKEV2: Failed to initialize cipher");
  186. return -1;
  187. }
  188. if (crypto_cipher_encrypt(cipher, plain, crypt, len) < 0) {
  189. wpa_printf(MSG_INFO, "IKEV2: Encryption failed");
  190. crypto_cipher_deinit(cipher);
  191. return -1;
  192. }
  193. crypto_cipher_deinit(cipher);
  194. #ifdef CCNS_PL
  195. }
  196. #endif /* CCNS_PL */
  197. return 0;
  198. }
  199. int ikev2_encr_decrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
  200. const u8 *crypt, u8 *plain, size_t len)
  201. {
  202. struct crypto_cipher *cipher;
  203. int encr_alg;
  204. #ifdef CCNS_PL
  205. if (alg == ENCR_3DES) {
  206. struct des3_key_s des3key;
  207. size_t i, blocks;
  208. /* ECB mode is used incorrectly for 3DES!? */
  209. if (key_len != 24) {
  210. wpa_printf(MSG_INFO, "IKEV2: Invalid encr key length");
  211. return -1;
  212. }
  213. des3_key_setup(key, &des3key);
  214. if (len % 8) {
  215. wpa_printf(MSG_INFO, "IKEV2: Invalid encrypted "
  216. "length");
  217. return -1;
  218. }
  219. blocks = len / 8;
  220. for (i = 0; i < blocks; i++) {
  221. des3_decrypt(crypt, &des3key, plain);
  222. plain += 8;
  223. crypt += 8;
  224. }
  225. } else {
  226. #endif /* CCNS_PL */
  227. switch (alg) {
  228. case ENCR_3DES:
  229. encr_alg = CRYPTO_CIPHER_ALG_3DES;
  230. break;
  231. case ENCR_AES_CBC:
  232. encr_alg = CRYPTO_CIPHER_ALG_AES;
  233. break;
  234. default:
  235. wpa_printf(MSG_DEBUG, "IKEV2: Unsupported encr alg %d", alg);
  236. return -1;
  237. }
  238. cipher = crypto_cipher_init(encr_alg, iv, key, key_len);
  239. if (cipher == NULL) {
  240. wpa_printf(MSG_INFO, "IKEV2: Failed to initialize cipher");
  241. return -1;
  242. }
  243. if (crypto_cipher_decrypt(cipher, crypt, plain, len) < 0) {
  244. wpa_printf(MSG_INFO, "IKEV2: Decryption failed");
  245. crypto_cipher_deinit(cipher);
  246. return -1;
  247. }
  248. crypto_cipher_deinit(cipher);
  249. #ifdef CCNS_PL
  250. }
  251. #endif /* CCNS_PL */
  252. return 0;
  253. }
  254. int ikev2_parse_payloads(struct ikev2_payloads *payloads,
  255. u8 next_payload, const u8 *pos, const u8 *end)
  256. {
  257. const struct ikev2_payload_hdr *phdr;
  258. os_memset(payloads, 0, sizeof(*payloads));
  259. while (next_payload != IKEV2_PAYLOAD_NO_NEXT_PAYLOAD) {
  260. int plen, pdatalen;
  261. const u8 *pdata;
  262. wpa_printf(MSG_DEBUG, "IKEV2: Processing payload %u",
  263. next_payload);
  264. if (end - pos < (int) sizeof(*phdr)) {
  265. wpa_printf(MSG_INFO, "IKEV2: Too short message for "
  266. "payload header (left=%ld)",
  267. (long) (end - pos));
  268. }
  269. phdr = (const struct ikev2_payload_hdr *) pos;
  270. plen = WPA_GET_BE16(phdr->payload_length);
  271. if (plen < (int) sizeof(*phdr) || pos + plen > end) {
  272. wpa_printf(MSG_INFO, "IKEV2: Invalid payload header "
  273. "length %d", plen);
  274. return -1;
  275. }
  276. wpa_printf(MSG_DEBUG, "IKEV2: Next Payload: %u Flags: 0x%x"
  277. " Payload Length: %d",
  278. phdr->next_payload, phdr->flags, plen);
  279. pdata = (const u8 *) (phdr + 1);
  280. pdatalen = plen - sizeof(*phdr);
  281. switch (next_payload) {
  282. case IKEV2_PAYLOAD_SA:
  283. wpa_printf(MSG_DEBUG, "IKEV2: Payload: Security "
  284. "Association");
  285. payloads->sa = pdata;
  286. payloads->sa_len = pdatalen;
  287. break;
  288. case IKEV2_PAYLOAD_KEY_EXCHANGE:
  289. wpa_printf(MSG_DEBUG, "IKEV2: Payload: Key "
  290. "Exchange");
  291. payloads->ke = pdata;
  292. payloads->ke_len = pdatalen;
  293. break;
  294. case IKEV2_PAYLOAD_IDi:
  295. wpa_printf(MSG_DEBUG, "IKEV2: Payload: IDi");
  296. payloads->idi = pdata;
  297. payloads->idi_len = pdatalen;
  298. break;
  299. case IKEV2_PAYLOAD_IDr:
  300. wpa_printf(MSG_DEBUG, "IKEV2: Payload: IDr");
  301. payloads->idr = pdata;
  302. payloads->idr_len = pdatalen;
  303. break;
  304. case IKEV2_PAYLOAD_CERTIFICATE:
  305. wpa_printf(MSG_DEBUG, "IKEV2: Payload: Certificate");
  306. payloads->cert = pdata;
  307. payloads->cert_len = pdatalen;
  308. break;
  309. case IKEV2_PAYLOAD_AUTHENTICATION:
  310. wpa_printf(MSG_DEBUG, "IKEV2: Payload: "
  311. "Authentication");
  312. payloads->auth = pdata;
  313. payloads->auth_len = pdatalen;
  314. break;
  315. case IKEV2_PAYLOAD_NONCE:
  316. wpa_printf(MSG_DEBUG, "IKEV2: Payload: Nonce");
  317. payloads->nonce = pdata;
  318. payloads->nonce_len = pdatalen;
  319. break;
  320. case IKEV2_PAYLOAD_ENCRYPTED:
  321. wpa_printf(MSG_DEBUG, "IKEV2: Payload: Encrypted");
  322. payloads->encrypted = pdata;
  323. payloads->encrypted_len = pdatalen;
  324. break;
  325. case IKEV2_PAYLOAD_NOTIFICATION:
  326. wpa_printf(MSG_DEBUG, "IKEV2: Payload: "
  327. "Notification");
  328. payloads->notification = pdata;
  329. payloads->notification_len = pdatalen;
  330. break;
  331. default:
  332. if (phdr->flags & IKEV2_PAYLOAD_FLAGS_CRITICAL) {
  333. wpa_printf(MSG_INFO, "IKEV2: Unsupported "
  334. "critical payload %u - reject the "
  335. "entire message", next_payload);
  336. return -1;
  337. } else {
  338. wpa_printf(MSG_DEBUG, "IKEV2: Skipped "
  339. "unsupported payload %u",
  340. next_payload);
  341. }
  342. }
  343. if (next_payload == IKEV2_PAYLOAD_ENCRYPTED &&
  344. pos + plen == end) {
  345. /*
  346. * Next Payload in the case of Encrypted Payload is
  347. * actually the payload type for the first embedded
  348. * payload.
  349. */
  350. payloads->encr_next_payload = phdr->next_payload;
  351. next_payload = IKEV2_PAYLOAD_NO_NEXT_PAYLOAD;
  352. } else
  353. next_payload = phdr->next_payload;
  354. pos += plen;
  355. }
  356. if (pos != end) {
  357. wpa_printf(MSG_INFO, "IKEV2: Unexpected extra data after "
  358. "payloads");
  359. return -1;
  360. }
  361. return 0;
  362. }
  363. int ikev2_derive_auth_data(int prf_alg, const struct wpabuf *sign_msg,
  364. const u8 *ID, size_t ID_len, u8 ID_type,
  365. struct ikev2_keys *keys, int initiator,
  366. const u8 *shared_secret, size_t shared_secret_len,
  367. const u8 *nonce, size_t nonce_len,
  368. const u8 *key_pad, size_t key_pad_len,
  369. u8 *auth_data)
  370. {
  371. size_t sign_len, buf_len;
  372. u8 *sign_data, *pos, *buf, hash[IKEV2_MAX_HASH_LEN];
  373. const struct ikev2_prf_alg *prf;
  374. const u8 *SK_p = initiator ? keys->SK_pi : keys->SK_pr;
  375. prf = ikev2_get_prf(prf_alg);
  376. if (sign_msg == NULL || ID == NULL || SK_p == NULL ||
  377. shared_secret == NULL || nonce == NULL || prf == NULL)
  378. return -1;
  379. /* prf(SK_pi/r,IDi/r') */
  380. buf_len = 4 + ID_len;
  381. buf = os_zalloc(buf_len);
  382. if (buf == NULL)
  383. return -1;
  384. buf[0] = ID_type;
  385. os_memcpy(buf + 4, ID, ID_len);
  386. if (ikev2_prf_hash(prf->id, SK_p, keys->SK_prf_len,
  387. 1, (const u8 **) &buf, &buf_len, hash) < 0) {
  388. os_free(buf);
  389. return -1;
  390. }
  391. os_free(buf);
  392. /* sign_data = msg | Nr/i | prf(SK_pi/r,IDi/r') */
  393. sign_len = wpabuf_len(sign_msg) + nonce_len + prf->hash_len;
  394. sign_data = os_malloc(sign_len);
  395. if (sign_data == NULL)
  396. return -1;
  397. pos = sign_data;
  398. os_memcpy(pos, wpabuf_head(sign_msg), wpabuf_len(sign_msg));
  399. pos += wpabuf_len(sign_msg);
  400. os_memcpy(pos, nonce, nonce_len);
  401. pos += nonce_len;
  402. os_memcpy(pos, hash, prf->hash_len);
  403. /* AUTH = prf(prf(Shared Secret, key pad, sign_data) */
  404. if (ikev2_prf_hash(prf->id, shared_secret, shared_secret_len, 1,
  405. &key_pad, &key_pad_len, hash) < 0 ||
  406. ikev2_prf_hash(prf->id, hash, prf->hash_len, 1,
  407. (const u8 **) &sign_data, &sign_len, auth_data) < 0)
  408. {
  409. os_free(sign_data);
  410. return -1;
  411. }
  412. os_free(sign_data);
  413. return 0;
  414. }
  415. u8 * ikev2_decrypt_payload(int encr_id, int integ_id,
  416. struct ikev2_keys *keys, int initiator,
  417. const struct ikev2_hdr *hdr,
  418. const u8 *encrypted, size_t encrypted_len,
  419. size_t *res_len)
  420. {
  421. size_t iv_len;
  422. const u8 *pos, *end, *iv, *integ;
  423. u8 hash[IKEV2_MAX_HASH_LEN], *decrypted;
  424. size_t decrypted_len, pad_len;
  425. const struct ikev2_integ_alg *integ_alg;
  426. const struct ikev2_encr_alg *encr_alg;
  427. const u8 *SK_e = initiator ? keys->SK_ei : keys->SK_er;
  428. const u8 *SK_a = initiator ? keys->SK_ai : keys->SK_ar;
  429. if (encrypted == NULL) {
  430. wpa_printf(MSG_INFO, "IKEV2: No Encrypted payload in SA_AUTH");
  431. return NULL;
  432. }
  433. encr_alg = ikev2_get_encr(encr_id);
  434. if (encr_alg == NULL) {
  435. wpa_printf(MSG_INFO, "IKEV2: Unsupported encryption type");
  436. return NULL;
  437. }
  438. iv_len = encr_alg->block_size;
  439. integ_alg = ikev2_get_integ(integ_id);
  440. if (integ_alg == NULL) {
  441. wpa_printf(MSG_INFO, "IKEV2: Unsupported intergrity type");
  442. return NULL;
  443. }
  444. if (encrypted_len < iv_len + 1 + integ_alg->hash_len) {
  445. wpa_printf(MSG_INFO, "IKEV2: No room for IV or Integrity "
  446. "Checksum");
  447. return NULL;
  448. }
  449. iv = encrypted;
  450. pos = iv + iv_len;
  451. end = encrypted + encrypted_len;
  452. integ = end - integ_alg->hash_len;
  453. if (SK_a == NULL) {
  454. wpa_printf(MSG_INFO, "IKEV2: No SK_a available");
  455. return NULL;
  456. }
  457. if (ikev2_integ_hash(integ_id, SK_a, keys->SK_integ_len,
  458. (const u8 *) hdr,
  459. integ - (const u8 *) hdr, hash) < 0) {
  460. wpa_printf(MSG_INFO, "IKEV2: Failed to calculate integrity "
  461. "hash");
  462. return NULL;
  463. }
  464. if (os_memcmp(integ, hash, integ_alg->hash_len) != 0) {
  465. wpa_printf(MSG_INFO, "IKEV2: Incorrect Integrity Checksum "
  466. "Data");
  467. return NULL;
  468. }
  469. if (SK_e == NULL) {
  470. wpa_printf(MSG_INFO, "IKEV2: No SK_e available");
  471. return NULL;
  472. }
  473. decrypted_len = integ - pos;
  474. decrypted = os_malloc(decrypted_len);
  475. if (decrypted == NULL)
  476. return NULL;
  477. if (ikev2_encr_decrypt(encr_alg->id, SK_e, keys->SK_encr_len, iv, pos,
  478. decrypted, decrypted_len) < 0) {
  479. os_free(decrypted);
  480. return NULL;
  481. }
  482. pad_len = decrypted[decrypted_len - 1];
  483. if (decrypted_len < pad_len + 1) {
  484. wpa_printf(MSG_INFO, "IKEV2: Invalid padding in encrypted "
  485. "payload");
  486. os_free(decrypted);
  487. return NULL;
  488. }
  489. decrypted_len -= pad_len + 1;
  490. *res_len = decrypted_len;
  491. return decrypted;
  492. }
  493. void ikev2_update_hdr(struct wpabuf *msg)
  494. {
  495. struct ikev2_hdr *hdr;
  496. /* Update lenth field in HDR */
  497. hdr = wpabuf_mhead(msg);
  498. WPA_PUT_BE32(hdr->length, wpabuf_len(msg));
  499. }
  500. int ikev2_build_encrypted(int encr_id, int integ_id, struct ikev2_keys *keys,
  501. int initiator, struct wpabuf *msg,
  502. struct wpabuf *plain, u8 next_payload)
  503. {
  504. struct ikev2_payload_hdr *phdr;
  505. size_t plen;
  506. size_t iv_len, pad_len;
  507. u8 *icv, *iv;
  508. const struct ikev2_integ_alg *integ_alg;
  509. const struct ikev2_encr_alg *encr_alg;
  510. const u8 *SK_e = initiator ? keys->SK_ei : keys->SK_er;
  511. const u8 *SK_a = initiator ? keys->SK_ai : keys->SK_ar;
  512. wpa_printf(MSG_DEBUG, "IKEV2: Adding Encrypted payload");
  513. /* Encr - RFC 4306, Sect. 3.14 */
  514. encr_alg = ikev2_get_encr(encr_id);
  515. if (encr_alg == NULL) {
  516. wpa_printf(MSG_INFO, "IKEV2: Unsupported encryption type");
  517. return -1;
  518. }
  519. iv_len = encr_alg->block_size;
  520. integ_alg = ikev2_get_integ(integ_id);
  521. if (integ_alg == NULL) {
  522. wpa_printf(MSG_INFO, "IKEV2: Unsupported intergrity type");
  523. return -1;
  524. }
  525. if (SK_e == NULL) {
  526. wpa_printf(MSG_INFO, "IKEV2: No SK_e available");
  527. return -1;
  528. }
  529. if (SK_a == NULL) {
  530. wpa_printf(MSG_INFO, "IKEV2: No SK_a available");
  531. return -1;
  532. }
  533. phdr = wpabuf_put(msg, sizeof(*phdr));
  534. phdr->next_payload = next_payload;
  535. phdr->flags = 0;
  536. iv = wpabuf_put(msg, iv_len);
  537. if (random_get_bytes(iv, iv_len)) {
  538. wpa_printf(MSG_INFO, "IKEV2: Could not generate IV");
  539. return -1;
  540. }
  541. pad_len = iv_len - (wpabuf_len(plain) + 1) % iv_len;
  542. if (pad_len == iv_len)
  543. pad_len = 0;
  544. wpabuf_put(plain, pad_len);
  545. wpabuf_put_u8(plain, pad_len);
  546. if (ikev2_encr_encrypt(encr_alg->id, SK_e, keys->SK_encr_len, iv,
  547. wpabuf_head(plain), wpabuf_mhead(plain),
  548. wpabuf_len(plain)) < 0)
  549. return -1;
  550. wpabuf_put_buf(msg, plain);
  551. /* Need to update all headers (Length fields) prior to hash func */
  552. icv = wpabuf_put(msg, integ_alg->hash_len);
  553. plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
  554. WPA_PUT_BE16(phdr->payload_length, plen);
  555. ikev2_update_hdr(msg);
  556. return ikev2_integ_hash(integ_id, SK_a, keys->SK_integ_len,
  557. wpabuf_head(msg),
  558. wpabuf_len(msg) - integ_alg->hash_len, icv);
  559. return 0;
  560. }
  561. int ikev2_keys_set(struct ikev2_keys *keys)
  562. {
  563. return keys->SK_d && keys->SK_ai && keys->SK_ar && keys->SK_ei &&
  564. keys->SK_er && keys->SK_pi && keys->SK_pr;
  565. }
  566. void ikev2_free_keys(struct ikev2_keys *keys)
  567. {
  568. os_free(keys->SK_d);
  569. os_free(keys->SK_ai);
  570. os_free(keys->SK_ar);
  571. os_free(keys->SK_ei);
  572. os_free(keys->SK_er);
  573. os_free(keys->SK_pi);
  574. os_free(keys->SK_pr);
  575. keys->SK_d = keys->SK_ai = keys->SK_ar = keys->SK_ei = keys->SK_er =
  576. keys->SK_pi = keys->SK_pr = NULL;
  577. }
  578. int ikev2_derive_sk_keys(const struct ikev2_prf_alg *prf,
  579. const struct ikev2_integ_alg *integ,
  580. const struct ikev2_encr_alg *encr,
  581. const u8 *skeyseed, const u8 *data, size_t data_len,
  582. struct ikev2_keys *keys)
  583. {
  584. u8 *keybuf, *pos;
  585. size_t keybuf_len;
  586. /*
  587. * {SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr } =
  588. * prf+(SKEYSEED, Ni | Nr | SPIi | SPIr )
  589. */
  590. ikev2_free_keys(keys);
  591. keys->SK_d_len = prf->key_len;
  592. keys->SK_integ_len = integ->key_len;
  593. keys->SK_encr_len = encr->key_len;
  594. keys->SK_prf_len = prf->key_len;
  595. #ifdef CCNS_PL
  596. /* Uses encryption key length for SK_d; should be PRF length */
  597. keys->SK_d_len = keys->SK_encr_len;
  598. #endif /* CCNS_PL */
  599. keybuf_len = keys->SK_d_len + 2 * keys->SK_integ_len +
  600. 2 * keys->SK_encr_len + 2 * keys->SK_prf_len;
  601. keybuf = os_malloc(keybuf_len);
  602. if (keybuf == NULL)
  603. return -1;
  604. if (ikev2_prf_plus(prf->id, skeyseed, prf->hash_len,
  605. data, data_len, keybuf, keybuf_len)) {
  606. os_free(keybuf);
  607. return -1;
  608. }
  609. pos = keybuf;
  610. keys->SK_d = os_malloc(keys->SK_d_len);
  611. if (keys->SK_d) {
  612. os_memcpy(keys->SK_d, pos, keys->SK_d_len);
  613. wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_d",
  614. keys->SK_d, keys->SK_d_len);
  615. }
  616. pos += keys->SK_d_len;
  617. keys->SK_ai = os_malloc(keys->SK_integ_len);
  618. if (keys->SK_ai) {
  619. os_memcpy(keys->SK_ai, pos, keys->SK_integ_len);
  620. wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ai",
  621. keys->SK_ai, keys->SK_integ_len);
  622. }
  623. pos += keys->SK_integ_len;
  624. keys->SK_ar = os_malloc(keys->SK_integ_len);
  625. if (keys->SK_ar) {
  626. os_memcpy(keys->SK_ar, pos, keys->SK_integ_len);
  627. wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ar",
  628. keys->SK_ar, keys->SK_integ_len);
  629. }
  630. pos += keys->SK_integ_len;
  631. keys->SK_ei = os_malloc(keys->SK_encr_len);
  632. if (keys->SK_ei) {
  633. os_memcpy(keys->SK_ei, pos, keys->SK_encr_len);
  634. wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ei",
  635. keys->SK_ei, keys->SK_encr_len);
  636. }
  637. pos += keys->SK_encr_len;
  638. keys->SK_er = os_malloc(keys->SK_encr_len);
  639. if (keys->SK_er) {
  640. os_memcpy(keys->SK_er, pos, keys->SK_encr_len);
  641. wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_er",
  642. keys->SK_er, keys->SK_encr_len);
  643. }
  644. pos += keys->SK_encr_len;
  645. keys->SK_pi = os_malloc(keys->SK_prf_len);
  646. if (keys->SK_pi) {
  647. os_memcpy(keys->SK_pi, pos, keys->SK_prf_len);
  648. wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_pi",
  649. keys->SK_pi, keys->SK_prf_len);
  650. }
  651. pos += keys->SK_prf_len;
  652. keys->SK_pr = os_malloc(keys->SK_prf_len);
  653. if (keys->SK_pr) {
  654. os_memcpy(keys->SK_pr, pos, keys->SK_prf_len);
  655. wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_pr",
  656. keys->SK_pr, keys->SK_prf_len);
  657. }
  658. os_free(keybuf);
  659. if (!ikev2_keys_set(keys)) {
  660. ikev2_free_keys(keys);
  661. return -1;
  662. }
  663. return 0;
  664. }