simaka_message.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * Copyright (C) 2009 Martin Willi
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include "simaka_message.h"
  16. #include "simaka_manager.h"
  17. #include <utils/debug.h>
  18. #include <collections/linked_list.h>
  19. typedef struct private_simaka_message_t private_simaka_message_t;
  20. typedef struct hdr_t hdr_t;
  21. typedef struct attr_hdr_t attr_hdr_t;
  22. typedef struct attr_t attr_t;
  23. /**
  24. * packed EAP-SIM/AKA header struct
  25. */
  26. struct hdr_t {
  27. /** EAP code (REQUEST/RESPONSE) */
  28. uint8_t code;
  29. /** unique message identifier */
  30. uint8_t identifier;
  31. /** length of whole message */
  32. uint16_t length;
  33. /** EAP type => EAP_SIM/EAP_AKA */
  34. uint8_t type;
  35. /** SIM subtype */
  36. uint8_t subtype;
  37. /** reserved bytes */
  38. uint16_t reserved;
  39. } __attribute__((__packed__));
  40. /**
  41. * packed EAP-SIM/AKA attribute header struct
  42. */
  43. struct attr_hdr_t {
  44. /** attribute type */
  45. uint8_t type;
  46. /** attribute length */
  47. uint8_t length;
  48. } __attribute__((__packed__));
  49. /**
  50. * SIM/AKA attribute, parsed
  51. */
  52. struct attr_t {
  53. /** type of attribute */
  54. simaka_attribute_t type;
  55. /** length of data */
  56. size_t len;
  57. /** start of data, variable length */
  58. char data[];
  59. };
  60. ENUM_BEGIN(simaka_subtype_names, AKA_CHALLENGE, AKA_IDENTITY,
  61. "AKA_CHALLENGE",
  62. "AKA_AUTHENTICATION_REJECT",
  63. "AKA_3",
  64. "AKA_SYNCHRONIZATION_FAILURE",
  65. "AKA_IDENTITY");
  66. ENUM_NEXT(simaka_subtype_names, SIM_START, AKA_CLIENT_ERROR, AKA_IDENTITY,
  67. "SIM_START",
  68. "SIM_CHALLENGE",
  69. "SIM/AKA_NOTIFICATION",
  70. "SIM/AKA_REAUTHENTICATION",
  71. "SIM/AKA_CLIENT_ERROR");
  72. ENUM_END(simaka_subtype_names, AKA_CLIENT_ERROR);
  73. ENUM_BEGIN(simaka_attribute_names, AT_RAND, AT_CLIENT_ERROR_CODE,
  74. "AT_RAND",
  75. "AT_AUTN",
  76. "AT_RES",
  77. "AT_AUTS",
  78. "AT_5",
  79. "AT_PADDING",
  80. "AT_NONCE_MT",
  81. "AT_8",
  82. "AT_9",
  83. "AT_PERMANENT_ID_REQ",
  84. "AT_MAC",
  85. "AT_NOTIFICATION",
  86. "AT_ANY_ID_REQ",
  87. "AT_IDENTITY",
  88. "AT_VERSION_LIST",
  89. "AT_SELECTED_VERSION",
  90. "AT_FULLAUTH_ID_REQ",
  91. "AT_18",
  92. "AT_COUNTER",
  93. "AT_COUNTER_TOO_SMALL",
  94. "AT_NONCE_S",
  95. "AT_CLIENT_ERROR_CODE");
  96. ENUM_NEXT(simaka_attribute_names, AT_IV, AT_RESULT_IND, AT_CLIENT_ERROR_CODE,
  97. "AT_IV",
  98. "AT_ENCR_DATA",
  99. "AT_131",
  100. "AT_NEXT_PSEUDONYM",
  101. "AT_NEXT_REAUTH_ID",
  102. "AT_CHECKCODE",
  103. "AT_RESULT_IND");
  104. ENUM_END(simaka_attribute_names, AT_RESULT_IND);
  105. ENUM_BEGIN(simaka_notification_names, SIM_GENERAL_FAILURE_AA, SIM_GENERAL_FAILURE_AA,
  106. "General failure after authentication");
  107. ENUM_NEXT(simaka_notification_names, SIM_TEMP_DENIED, SIM_TEMP_DENIED, SIM_GENERAL_FAILURE_AA,
  108. "User has been temporarily denied access");
  109. ENUM_NEXT(simaka_notification_names, SIM_NOT_SUBSCRIBED, SIM_NOT_SUBSCRIBED, SIM_TEMP_DENIED,
  110. "User has not subscribed to the requested service");
  111. ENUM_NEXT(simaka_notification_names, SIM_GENERAL_FAILURE, SIM_GENERAL_FAILURE, SIM_NOT_SUBSCRIBED,
  112. "General failure");
  113. ENUM_NEXT(simaka_notification_names, SIM_SUCCESS, SIM_SUCCESS, SIM_GENERAL_FAILURE,
  114. "User has been successfully authenticated");
  115. ENUM_END(simaka_notification_names, SIM_SUCCESS);
  116. ENUM(simaka_client_error_names, SIM_UNABLE_TO_PROCESS, SIM_RANDS_NOT_FRESH,
  117. "unable to process packet",
  118. "unsupported version",
  119. "insufficient number of challenges",
  120. "RANDs are not fresh",
  121. );
  122. /**
  123. * Check if an EAP-SIM/AKA attribute is skippable
  124. */
  125. bool simaka_attribute_skippable(simaka_attribute_t attribute)
  126. {
  127. bool skippable = !((int)attribute >= 0 && attribute <= 127);
  128. DBG1(DBG_LIB, "%sskippable EAP-SIM/AKA attribute %N",
  129. skippable ? "ignoring " : "found non-",
  130. simaka_attribute_names, attribute);
  131. return skippable;
  132. }
  133. /**
  134. * Private data of an simaka_message_t object.
  135. */
  136. struct private_simaka_message_t {
  137. /**
  138. * Public simaka_message_t interface.
  139. */
  140. simaka_message_t public;
  141. /**
  142. * EAP message, starting with EAP header
  143. */
  144. hdr_t *hdr;
  145. /**
  146. * List of parsed attributes, attr_t
  147. */
  148. linked_list_t *attributes;
  149. /**
  150. * Currently parsing AT_ENCR_DATA wrapped attributes?
  151. */
  152. bool encrypted;
  153. /**
  154. * crypto helper
  155. */
  156. simaka_crypto_t *crypto;
  157. /**
  158. * Phase a NOTIFICATION is sent within
  159. */
  160. bool p_bit;
  161. /**
  162. * MAC value, pointing into message
  163. */
  164. chunk_t mac;
  165. /**
  166. * ENCR_DATA value, pointing into message
  167. */
  168. chunk_t encr;
  169. /**
  170. * IV value, pointing into message
  171. */
  172. chunk_t iv;
  173. };
  174. METHOD(simaka_message_t, is_request, bool,
  175. private_simaka_message_t *this)
  176. {
  177. return this->hdr->code == EAP_REQUEST;
  178. }
  179. METHOD(simaka_message_t, get_identifier, uint8_t,
  180. private_simaka_message_t *this)
  181. {
  182. return this->hdr->identifier;
  183. }
  184. METHOD(simaka_message_t, get_subtype, simaka_subtype_t,
  185. private_simaka_message_t *this)
  186. {
  187. return this->hdr->subtype;
  188. }
  189. METHOD(simaka_message_t, get_type, eap_type_t,
  190. private_simaka_message_t *this)
  191. {
  192. return this->hdr->type;
  193. }
  194. CALLBACK(attr_enum_filter, bool,
  195. void *null, enumerator_t *orig, va_list args)
  196. {
  197. attr_t *attr;
  198. simaka_attribute_t *type;
  199. chunk_t *data;
  200. VA_ARGS_VGET(args, type, data);
  201. if (orig->enumerate(orig, &attr))
  202. {
  203. *type = attr->type;
  204. *data = chunk_create(attr->data, attr->len);
  205. return TRUE;
  206. }
  207. return FALSE;
  208. }
  209. METHOD(simaka_message_t, create_attribute_enumerator, enumerator_t*,
  210. private_simaka_message_t *this)
  211. {
  212. return enumerator_create_filter(
  213. this->attributes->create_enumerator(this->attributes),
  214. attr_enum_filter, NULL, NULL);
  215. }
  216. METHOD(simaka_message_t, add_attribute, void,
  217. private_simaka_message_t *this, simaka_attribute_t type, chunk_t data)
  218. {
  219. attr_t *attr;
  220. attr = malloc(sizeof(attr_t) + data.len);
  221. attr->len = data.len;
  222. attr->type = type;
  223. memcpy(attr->data, data.ptr, data.len);
  224. this->attributes->insert_last(this->attributes, attr);
  225. }
  226. /**
  227. * Error handling for unencrypted attributes
  228. */
  229. static bool not_encrypted(simaka_attribute_t type)
  230. {
  231. DBG1(DBG_LIB, "received unencrypted %N", simaka_attribute_names, type);
  232. return FALSE;
  233. }
  234. /**
  235. * Error handling for invalid length
  236. */
  237. static bool invalid_length(simaka_attribute_t type)
  238. {
  239. DBG1(DBG_LIB, "invalid length of %N", simaka_attribute_names, type);
  240. return FALSE;
  241. }
  242. /**
  243. * Call SIM/AKA message hooks
  244. */
  245. static void call_hook(private_simaka_message_t *this,
  246. bool inbound, bool decrypted)
  247. {
  248. simaka_manager_t *mgr;
  249. switch (this->hdr->type)
  250. {
  251. case EAP_SIM:
  252. mgr = lib->get(lib, "sim-manager");
  253. break;
  254. case EAP_AKA:
  255. mgr = lib->get(lib, "aka-manager");
  256. break;
  257. default:
  258. return;
  259. }
  260. mgr->message_hook(mgr, &this->public, inbound, decrypted);
  261. }
  262. /**
  263. * Parse attributes from a chunk of data
  264. */
  265. static bool parse_attributes(private_simaka_message_t *this, chunk_t in)
  266. {
  267. while (in.len)
  268. {
  269. attr_hdr_t *hdr;
  270. chunk_t data;
  271. if (in.len < sizeof(attr_hdr_t))
  272. {
  273. DBG1(DBG_LIB, "found short %N attribute header",
  274. eap_type_names, this->hdr->type);
  275. return FALSE;
  276. }
  277. hdr = (attr_hdr_t*)in.ptr;
  278. switch (hdr->type)
  279. {
  280. /* attributes without data */
  281. case AT_COUNTER_TOO_SMALL:
  282. if (!this->encrypted)
  283. {
  284. return not_encrypted(hdr->type);
  285. }
  286. /* FALL */
  287. case AT_ANY_ID_REQ:
  288. case AT_PERMANENT_ID_REQ:
  289. case AT_FULLAUTH_ID_REQ:
  290. {
  291. if (hdr->length != 1 || in.len < 4)
  292. {
  293. return invalid_length(hdr->type);
  294. }
  295. data = chunk_empty;
  296. in = chunk_skip(in, 4);
  297. break;
  298. }
  299. /* attributes with two bytes data */
  300. case AT_COUNTER:
  301. if (!this->encrypted)
  302. {
  303. return not_encrypted(hdr->type);
  304. }
  305. /* FALL */
  306. case AT_CLIENT_ERROR_CODE:
  307. case AT_SELECTED_VERSION:
  308. case AT_NOTIFICATION:
  309. {
  310. if (hdr->length != 1 || in.len < 4)
  311. {
  312. return invalid_length(hdr->type);
  313. }
  314. data = chunk_create(in.ptr + 2, 2);
  315. in = chunk_skip(in, 4);
  316. break;
  317. }
  318. /* attributes with an additional actual-length in bits or bytes */
  319. case AT_NEXT_PSEUDONYM:
  320. case AT_NEXT_REAUTH_ID:
  321. if (!this->encrypted)
  322. {
  323. return not_encrypted(hdr->type);
  324. }
  325. /* FALL */
  326. case AT_RES:
  327. case AT_IDENTITY:
  328. case AT_VERSION_LIST:
  329. {
  330. uint16_t len;
  331. if (hdr->length < 1 || in.len < 4)
  332. {
  333. return invalid_length(hdr->type);
  334. }
  335. memcpy(&len, in.ptr + 2, 2);
  336. len = ntohs(len);
  337. if (hdr->type == AT_RES)
  338. { /* AT_RES uses length encoding in bits */
  339. len /= 8;
  340. }
  341. if (len > hdr->length * 4 || len > in.len)
  342. {
  343. return invalid_length(hdr->type);
  344. }
  345. data = chunk_create(in.ptr + 4, len);
  346. in = chunk_skip(in, hdr->length * 4);
  347. break;
  348. }
  349. /* attributes with two reserved bytes, 16 bytes length */
  350. case AT_NONCE_S:
  351. if (!this->encrypted)
  352. {
  353. return not_encrypted(hdr->type);
  354. }
  355. /* FALL */
  356. case AT_AUTN:
  357. case AT_NONCE_MT:
  358. case AT_IV:
  359. case AT_MAC:
  360. {
  361. if (hdr->length != 5 || in.len < 20)
  362. {
  363. return invalid_length(hdr->type);
  364. }
  365. data = chunk_create(in.ptr + 4, 16);
  366. in = chunk_skip(in, 20);
  367. break;
  368. }
  369. /* attributes with two reserved bytes, variable length */
  370. case AT_ENCR_DATA:
  371. case AT_RAND:
  372. {
  373. if (hdr->length * 4 > in.len || in.len < 4)
  374. {
  375. return invalid_length(hdr->type);
  376. }
  377. data = chunk_create(in.ptr + 4, hdr->length * 4 - 4);
  378. in = chunk_skip(in, hdr->length * 4);
  379. break;
  380. }
  381. /* attributes with no reserved bytes, 14 bytes length */
  382. case AT_AUTS:
  383. {
  384. if (hdr->length != 4 || in.len < 16)
  385. {
  386. return invalid_length(hdr->type);
  387. }
  388. data = chunk_create(in.ptr + 2, 14);
  389. in = chunk_skip(in, 16);
  390. break;
  391. }
  392. /* other attributes (with 4n + 2 length) */
  393. case AT_PADDING:
  394. default:
  395. {
  396. if (hdr->length * 4 > in.len || in.len < 4)
  397. {
  398. return invalid_length(hdr->type);
  399. }
  400. data = chunk_create(in.ptr + 2, hdr->length * 4 - 2);
  401. in = chunk_skip(in, hdr->length * 4);
  402. break;
  403. }
  404. }
  405. /* handle special attributes */
  406. switch (hdr->type)
  407. {
  408. case AT_MAC:
  409. this->mac = data;
  410. break;
  411. case AT_IV:
  412. this->iv = data;
  413. break;
  414. case AT_ENCR_DATA:
  415. this->encr = data;
  416. break;
  417. case AT_PADDING:
  418. break;
  419. case AT_NOTIFICATION:
  420. if (this->p_bit)
  421. { /* remember P bit for MAC verification */
  422. this->p_bit = !!(data.ptr[0] & 0x40);
  423. }
  424. else if (!this->encrypted)
  425. {
  426. DBG1(DBG_LIB, "found P-bit 0 notify in unencrypted message");
  427. return FALSE;
  428. }
  429. /* FALL */
  430. default:
  431. add_attribute(this, hdr->type, data);
  432. break;
  433. }
  434. }
  435. call_hook(this, TRUE, this->encrypted);
  436. return TRUE;
  437. }
  438. /**
  439. * Decrypt a message and parse the decrypted attributes
  440. */
  441. static bool decrypt(private_simaka_message_t *this)
  442. {
  443. bool success;
  444. crypter_t *crypter;
  445. chunk_t plain;
  446. crypter = this->crypto->get_crypter(this->crypto);
  447. if (!crypter || !this->iv.len || !this->encr.len || this->encrypted)
  448. {
  449. return TRUE;
  450. }
  451. if (this->encr.len % crypter->get_block_size(crypter))
  452. {
  453. DBG1(DBG_LIB, "%N ENCR_DATA not a multiple of block size",
  454. eap_type_names, this->hdr->type);
  455. return FALSE;
  456. }
  457. if (!crypter->decrypt(crypter, this->encr, this->iv, &plain))
  458. {
  459. return FALSE;
  460. }
  461. this->encrypted = TRUE;
  462. success = parse_attributes(this, plain);
  463. this->encrypted = FALSE;
  464. free(plain.ptr);
  465. return success;
  466. }
  467. METHOD(simaka_message_t, parse, bool,
  468. private_simaka_message_t *this)
  469. {
  470. chunk_t in;
  471. if (this->attributes->get_count(this->attributes))
  472. { /* Already parsed. Try to decrypt and parse AT_ENCR_DATA. */
  473. return decrypt(this);
  474. }
  475. in = chunk_create((char*)this->hdr, ntohs(this->hdr->length));
  476. if (!parse_attributes(this, chunk_skip(in, sizeof(hdr_t))))
  477. {
  478. return FALSE;
  479. }
  480. /* try to decrypt if we already have keys */
  481. return decrypt(this);
  482. }
  483. METHOD(simaka_message_t, verify, bool,
  484. private_simaka_message_t *this, chunk_t sigdata)
  485. {
  486. chunk_t data, backup;
  487. signer_t *signer;
  488. signer = this->crypto->get_signer(this->crypto);
  489. switch (this->hdr->subtype)
  490. {
  491. case SIM_START:
  492. case SIM_CLIENT_ERROR:
  493. /* AKA_CLIENT_ERROR: */
  494. case AKA_AUTHENTICATION_REJECT:
  495. case AKA_SYNCHRONIZATION_FAILURE:
  496. case AKA_IDENTITY:
  497. /* skip MAC if available */
  498. return TRUE;
  499. case SIM_CHALLENGE:
  500. case AKA_CHALLENGE:
  501. case SIM_REAUTHENTICATION:
  502. /* AKA_REAUTHENTICATION: */
  503. {
  504. if (!this->mac.ptr || !signer)
  505. { /* require MAC, but not found */
  506. DBG1(DBG_LIB, "%N message requires a MAC, but none found",
  507. simaka_subtype_names, this->hdr->subtype);
  508. return FALSE;
  509. }
  510. break;
  511. }
  512. case SIM_NOTIFICATION:
  513. /* AKA_NOTIFICATION: */
  514. {
  515. if (this->p_bit)
  516. { /* MAC not verified if in Phase 1 */
  517. return TRUE;
  518. }
  519. if (!this->mac.ptr || !signer)
  520. {
  521. DBG1(DBG_LIB, "%N message has a phase 0 notify, but "
  522. "no MAC found", simaka_subtype_names, this->hdr->subtype);
  523. return FALSE;
  524. }
  525. break;
  526. }
  527. default:
  528. /* unknown message? */
  529. DBG1(DBG_LIB, "signature rule for %N messages missing",
  530. simaka_subtype_names, this->hdr->subtype);
  531. return FALSE;
  532. }
  533. /* zero MAC for verification */
  534. backup = chunk_clonea(this->mac);
  535. memset(this->mac.ptr, 0, this->mac.len);
  536. data = chunk_create((char*)this->hdr, ntohs(this->hdr->length));
  537. if (sigdata.len)
  538. {
  539. data = chunk_cata("cc", data, sigdata);
  540. }
  541. if (!signer->verify_signature(signer, data, backup))
  542. {
  543. DBG1(DBG_LIB, "%N MAC verification failed",
  544. eap_type_names, this->hdr->type);
  545. return FALSE;
  546. }
  547. return TRUE;
  548. }
  549. METHOD(simaka_message_t, generate, bool,
  550. private_simaka_message_t *this, chunk_t sigdata, chunk_t *gen)
  551. {
  552. /* buffers large enough for messages we generate */
  553. char out_buf[1024], encr_buf[512];
  554. enumerator_t *enumerator;
  555. chunk_t out, encr, data, *target, mac = chunk_empty;
  556. simaka_attribute_t type;
  557. attr_hdr_t *hdr;
  558. uint16_t len;
  559. signer_t *signer;
  560. call_hook(this, FALSE, TRUE);
  561. out = chunk_create(out_buf, sizeof(out_buf));
  562. encr = chunk_create(encr_buf, sizeof(encr_buf));
  563. /* copy header */
  564. memcpy(out.ptr, this->hdr, sizeof(hdr_t));
  565. out = chunk_skip(out, sizeof(hdr_t));
  566. /* encode attributes */
  567. enumerator = create_attribute_enumerator(this);
  568. while (enumerator->enumerate(enumerator, &type, &data))
  569. {
  570. /* encrypt this attribute? */
  571. switch (type)
  572. {
  573. case AT_NONCE_S:
  574. case AT_NEXT_PSEUDONYM:
  575. case AT_NEXT_REAUTH_ID:
  576. case AT_COUNTER:
  577. case AT_COUNTER_TOO_SMALL:
  578. target = &encr;
  579. break;
  580. case AT_NOTIFICATION:
  581. /* P bit not set, encrypt */
  582. if (!(data.ptr[0] & 0x40))
  583. {
  584. target = &encr;
  585. break;
  586. }
  587. /* FALL */
  588. default:
  589. target = &out;
  590. break;
  591. }
  592. hdr = (attr_hdr_t*)target->ptr;
  593. hdr->type = type;
  594. /* encode type specific */
  595. switch (type)
  596. {
  597. /* attributes without data */
  598. case AT_COUNTER_TOO_SMALL:
  599. case AT_ANY_ID_REQ:
  600. case AT_PERMANENT_ID_REQ:
  601. case AT_FULLAUTH_ID_REQ:
  602. {
  603. hdr->length = 1;
  604. memset(target->ptr + 2, 0, 2);
  605. *target = chunk_skip(*target, 4);
  606. break;
  607. }
  608. /* attributes with two bytes data */
  609. case AT_COUNTER:
  610. case AT_CLIENT_ERROR_CODE:
  611. case AT_SELECTED_VERSION:
  612. case AT_NOTIFICATION:
  613. {
  614. hdr->length = 1;
  615. memcpy(target->ptr + 2, data.ptr, 2);
  616. *target = chunk_skip(*target, 4);
  617. break;
  618. }
  619. /* attributes with an additional actual-length in bits or bytes */
  620. case AT_NEXT_PSEUDONYM:
  621. case AT_NEXT_REAUTH_ID:
  622. case AT_IDENTITY:
  623. case AT_VERSION_LIST:
  624. case AT_RES:
  625. {
  626. uint16_t len, padding;
  627. len = htons(data.len);
  628. if (type == AT_RES)
  629. { /* AT_RES uses length encoding in bits */
  630. len *= 8;
  631. }
  632. memcpy(target->ptr + 2, &len, sizeof(len));
  633. memcpy(target->ptr + 4, data.ptr, data.len);
  634. hdr->length = data.len / 4 + 1;
  635. padding = (4 - (data.len % 4)) % 4;
  636. if (padding)
  637. {
  638. hdr->length++;
  639. memset(target->ptr + 4 + data.len, 0, padding);
  640. }
  641. *target = chunk_skip(*target, hdr->length * 4);
  642. break;
  643. }
  644. /* attributes with two reserved bytes, 16 bytes length */
  645. case AT_NONCE_S:
  646. case AT_NONCE_MT:
  647. case AT_AUTN:
  648. {
  649. hdr->length = 5;
  650. memset(target->ptr + 2, 0, 2);
  651. memcpy(target->ptr + 4, data.ptr, data.len);
  652. *target = chunk_skip(*target, 20);
  653. break;
  654. }
  655. /* attributes with two reserved bytes, variable length */
  656. case AT_RAND:
  657. {
  658. hdr->length = 1 + data.len / 4;
  659. memset(target->ptr + 2, 0, 2);
  660. memcpy(target->ptr + 4, data.ptr, data.len);
  661. *target = chunk_skip(*target, data.len + 4);
  662. break;
  663. }
  664. /* attributes with no reserved bytes, 14 bytes length */
  665. case AT_AUTS:
  666. {
  667. hdr->length = 4;
  668. memcpy(target->ptr + 2, data.ptr, data.len);
  669. *target = chunk_skip(*target, 16);
  670. break;
  671. }
  672. default:
  673. {
  674. DBG1(DBG_LIB, "no rule to encode %N, skipped",
  675. simaka_attribute_names, type);
  676. break;
  677. }
  678. }
  679. }
  680. enumerator->destroy(enumerator);
  681. /* encrypt attributes, if any */
  682. if (encr.len < sizeof(encr_buf))
  683. {
  684. chunk_t iv;
  685. size_t bs, padding;
  686. crypter_t *crypter;
  687. rng_t *rng;
  688. crypter = this->crypto->get_crypter(this->crypto);
  689. bs = crypter->get_block_size(crypter);
  690. iv.len = crypter->get_iv_size(crypter);
  691. /* add AT_PADDING attribute */
  692. padding = bs - ((sizeof(encr_buf) - encr.len) % bs);
  693. if (padding)
  694. {
  695. hdr = (attr_hdr_t*)encr.ptr;
  696. hdr->type = AT_PADDING;
  697. hdr->length = padding / 4;
  698. memset(encr.ptr + 2, 0, padding - 2);
  699. encr = chunk_skip(encr, padding);
  700. }
  701. encr = chunk_create(encr_buf, sizeof(encr_buf) - encr.len);
  702. /* add IV attribute */
  703. hdr = (attr_hdr_t*)out.ptr;
  704. hdr->type = AT_IV;
  705. hdr->length = iv.len / 4 + 1;
  706. memset(out.ptr + 2, 0, 2);
  707. out = chunk_skip(out, 4);
  708. rng = this->crypto->get_rng(this->crypto);
  709. if (!rng->get_bytes(rng, iv.len, out.ptr))
  710. {
  711. return FALSE;
  712. }
  713. iv = chunk_clonea(chunk_create(out.ptr, iv.len));
  714. out = chunk_skip(out, iv.len);
  715. /* inline encryption */
  716. if (!crypter->encrypt(crypter, encr, iv, NULL))
  717. {
  718. return FALSE;
  719. }
  720. /* add ENCR_DATA attribute */
  721. hdr = (attr_hdr_t*)out.ptr;
  722. hdr->type = AT_ENCR_DATA;
  723. hdr->length = encr.len / 4 + 1;
  724. memset(out.ptr + 2, 0, 2);
  725. memcpy(out.ptr + 4, encr.ptr, encr.len);
  726. out = chunk_skip(out, encr.len + 4);
  727. }
  728. /* include MAC ? */
  729. signer = this->crypto->get_signer(this->crypto);
  730. switch (this->hdr->subtype)
  731. {
  732. case SIM_CHALLENGE:
  733. case AKA_CHALLENGE:
  734. case SIM_REAUTHENTICATION:
  735. /* AKA_REAUTHENTICATION: */
  736. /* TODO: Notifications without P bit */
  737. {
  738. size_t bs;
  739. bs = signer->get_block_size(signer);
  740. hdr = (attr_hdr_t*)out.ptr;
  741. hdr->type = AT_MAC;
  742. hdr->length = bs / 4 + 1;
  743. memset(out.ptr + 2, 0, 2 + bs);
  744. mac = chunk_create(out.ptr + 4, bs);
  745. out = chunk_skip(out, bs + 4);
  746. break;
  747. }
  748. default:
  749. break;
  750. }
  751. /* calculate message length */
  752. out = chunk_create(out_buf, sizeof(out_buf) - out.len);
  753. len = htons(out.len);
  754. memcpy(out.ptr + 2, &len, sizeof(len));
  755. /* generate MAC */
  756. if (mac.len)
  757. {
  758. data = chunk_cata("cc", out, sigdata);
  759. if (!signer->get_signature(signer, data, mac.ptr))
  760. {
  761. return FALSE;
  762. }
  763. }
  764. call_hook(this, FALSE, FALSE);
  765. *gen = chunk_clone(out);
  766. return TRUE;
  767. }
  768. METHOD(simaka_message_t, destroy, void,
  769. private_simaka_message_t *this)
  770. {
  771. this->attributes->destroy_function(this->attributes, free);
  772. free(this->hdr);
  773. free(this);
  774. }
  775. /**
  776. * Generic constructor.
  777. */
  778. static simaka_message_t *simaka_message_create_data(chunk_t data,
  779. simaka_crypto_t *crypto)
  780. {
  781. private_simaka_message_t *this;
  782. hdr_t *hdr = (hdr_t*)data.ptr;
  783. if (data.len < sizeof(hdr_t) || hdr->length != htons(data.len))
  784. {
  785. DBG1(DBG_LIB, "EAP-SIM/AKA header has invalid length");
  786. return NULL;
  787. }
  788. if (hdr->code != EAP_REQUEST && hdr->code != EAP_RESPONSE)
  789. {
  790. DBG1(DBG_LIB, "invalid EAP code in EAP-SIM/AKA message",
  791. eap_type_names, hdr->type);
  792. return NULL;
  793. }
  794. if (hdr->type != EAP_SIM && hdr->type != EAP_AKA)
  795. {
  796. DBG1(DBG_LIB, "invalid EAP type in EAP-SIM/AKA message",
  797. eap_type_names, hdr->type);
  798. return NULL;
  799. }
  800. INIT(this,
  801. .public = {
  802. .is_request = _is_request,
  803. .get_identifier = _get_identifier,
  804. .get_type = _get_type,
  805. .get_subtype = _get_subtype,
  806. .create_attribute_enumerator = _create_attribute_enumerator,
  807. .add_attribute = _add_attribute,
  808. .parse = _parse,
  809. .verify = _verify,
  810. .generate = _generate,
  811. .destroy = _destroy,
  812. },
  813. .attributes = linked_list_create(),
  814. .crypto = crypto,
  815. .p_bit = TRUE,
  816. .hdr = malloc(data.len),
  817. );
  818. memcpy(this->hdr, hdr, data.len);
  819. return &this->public;
  820. }
  821. /**
  822. * See header.
  823. */
  824. simaka_message_t *simaka_message_create_from_payload(chunk_t data,
  825. simaka_crypto_t *crypto)
  826. {
  827. return simaka_message_create_data(data, crypto);
  828. }
  829. /**
  830. * See header.
  831. */
  832. simaka_message_t *simaka_message_create(bool request, uint8_t identifier,
  833. eap_type_t type, simaka_subtype_t subtype,
  834. simaka_crypto_t *crypto)
  835. {
  836. hdr_t hdr = {
  837. .code = request ? EAP_REQUEST : EAP_RESPONSE,
  838. .identifier = identifier,
  839. .length = htons(sizeof(hdr_t)),
  840. .type = type,
  841. .subtype = subtype,
  842. };
  843. return simaka_message_create_data(chunk_create((char*)&hdr, sizeof(hdr)),
  844. crypto);
  845. }