eap_tls_common.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /*
  2. * EAP peer: EAP-TLS/PEAP/TTLS/FAST common functions
  3. * Copyright (c) 2004-2008, 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 "eap_i.h"
  17. #include "eap_tls_common.h"
  18. #include "eap_config.h"
  19. #include "sha1.h"
  20. #include "tls.h"
  21. static int eap_tls_check_blob(struct eap_sm *sm, const char **name,
  22. const u8 **data, size_t *data_len)
  23. {
  24. const struct wpa_config_blob *blob;
  25. if (*name == NULL || os_strncmp(*name, "blob://", 7) != 0)
  26. return 0;
  27. blob = eap_get_config_blob(sm, *name + 7);
  28. if (blob == NULL) {
  29. wpa_printf(MSG_ERROR, "%s: Named configuration blob '%s' not "
  30. "found", __func__, *name + 7);
  31. return -1;
  32. }
  33. *name = NULL;
  34. *data = blob->data;
  35. *data_len = blob->len;
  36. return 0;
  37. }
  38. static void eap_tls_params_from_conf1(struct tls_connection_params *params,
  39. struct eap_peer_config *config)
  40. {
  41. params->ca_cert = (char *) config->ca_cert;
  42. params->ca_path = (char *) config->ca_path;
  43. params->client_cert = (char *) config->client_cert;
  44. params->private_key = (char *) config->private_key;
  45. params->private_key_passwd = (char *) config->private_key_passwd;
  46. params->dh_file = (char *) config->dh_file;
  47. params->subject_match = (char *) config->subject_match;
  48. params->altsubject_match = (char *) config->altsubject_match;
  49. params->engine = config->engine;
  50. params->engine_id = config->engine_id;
  51. params->pin = config->pin;
  52. params->key_id = config->key_id;
  53. params->cert_id = config->cert_id;
  54. params->ca_cert_id = config->ca_cert_id;
  55. }
  56. static void eap_tls_params_from_conf2(struct tls_connection_params *params,
  57. struct eap_peer_config *config)
  58. {
  59. params->ca_cert = (char *) config->ca_cert2;
  60. params->ca_path = (char *) config->ca_path2;
  61. params->client_cert = (char *) config->client_cert2;
  62. params->private_key = (char *) config->private_key2;
  63. params->private_key_passwd = (char *) config->private_key2_passwd;
  64. params->dh_file = (char *) config->dh_file2;
  65. params->subject_match = (char *) config->subject_match2;
  66. params->altsubject_match = (char *) config->altsubject_match2;
  67. params->engine = config->engine2;
  68. params->engine_id = config->engine2_id;
  69. params->pin = config->pin2;
  70. params->key_id = config->key2_id;
  71. params->cert_id = config->cert2_id;
  72. params->ca_cert_id = config->ca_cert2_id;
  73. }
  74. static int eap_tls_params_from_conf(struct eap_sm *sm,
  75. struct eap_ssl_data *data,
  76. struct tls_connection_params *params,
  77. struct eap_peer_config *config, int phase2)
  78. {
  79. os_memset(params, 0, sizeof(*params));
  80. if (phase2) {
  81. wpa_printf(MSG_DEBUG, "TLS: using phase2 config options");
  82. eap_tls_params_from_conf2(params, config);
  83. } else {
  84. wpa_printf(MSG_DEBUG, "TLS: using phase1 config options");
  85. eap_tls_params_from_conf1(params, config);
  86. }
  87. params->tls_ia = data->tls_ia;
  88. /*
  89. * Use blob data, if available. Otherwise, leave reference to external
  90. * file as-is.
  91. */
  92. if (eap_tls_check_blob(sm, &params->ca_cert, &params->ca_cert_blob,
  93. &params->ca_cert_blob_len) ||
  94. eap_tls_check_blob(sm, &params->client_cert,
  95. &params->client_cert_blob,
  96. &params->client_cert_blob_len) ||
  97. eap_tls_check_blob(sm, &params->private_key,
  98. &params->private_key_blob,
  99. &params->private_key_blob_len) ||
  100. eap_tls_check_blob(sm, &params->dh_file, &params->dh_blob,
  101. &params->dh_blob_len)) {
  102. wpa_printf(MSG_INFO, "SSL: Failed to get configuration blobs");
  103. return -1;
  104. }
  105. return 0;
  106. }
  107. static int eap_tls_init_connection(struct eap_sm *sm,
  108. struct eap_ssl_data *data,
  109. struct eap_peer_config *config,
  110. struct tls_connection_params *params)
  111. {
  112. int res;
  113. data->conn = tls_connection_init(sm->ssl_ctx);
  114. if (data->conn == NULL) {
  115. wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
  116. "connection");
  117. return -1;
  118. }
  119. res = tls_connection_set_params(sm->ssl_ctx, data->conn, params);
  120. if (res == TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED) {
  121. /*
  122. * At this point with the pkcs11 engine the PIN might be wrong.
  123. * We reset the PIN in the configuration to be sure to not use
  124. * it again and the calling function must request a new one.
  125. */
  126. os_free(config->pin);
  127. config->pin = NULL;
  128. } else if (res == TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED) {
  129. wpa_printf(MSG_INFO, "TLS: Failed to load private key");
  130. /*
  131. * We do not know exactly but maybe the PIN was wrong,
  132. * so ask for a new one.
  133. */
  134. os_free(config->pin);
  135. config->pin = NULL;
  136. eap_sm_request_pin(sm);
  137. sm->ignore = TRUE;
  138. return -1;
  139. } else if (res) {
  140. wpa_printf(MSG_INFO, "TLS: Failed to set TLS connection "
  141. "parameters");
  142. return -1;
  143. }
  144. return 0;
  145. }
  146. /**
  147. * eap_peer_tls_ssl_init - Initialize shared TLS functionality
  148. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  149. * @data: Data for TLS processing
  150. * @config: Pointer to the network configuration
  151. * Returns: 0 on success, -1 on failure
  152. *
  153. * This function is used to initialize shared TLS functionality for EAP-TLS,
  154. * EAP-PEAP, EAP-TTLS, and EAP-FAST.
  155. */
  156. int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
  157. struct eap_peer_config *config)
  158. {
  159. struct tls_connection_params params;
  160. if (config == NULL)
  161. return -1;
  162. data->eap = sm;
  163. data->phase2 = sm->init_phase2;
  164. if (eap_tls_params_from_conf(sm, data, &params, config, data->phase2) <
  165. 0)
  166. return -1;
  167. if (eap_tls_init_connection(sm, data, config, &params) < 0)
  168. return -1;
  169. data->tls_out_limit = config->fragment_size;
  170. if (data->phase2) {
  171. /* Limit the fragment size in the inner TLS authentication
  172. * since the outer authentication with EAP-PEAP does not yet
  173. * support fragmentation */
  174. if (data->tls_out_limit > 100)
  175. data->tls_out_limit -= 100;
  176. }
  177. if (config->phase1 &&
  178. os_strstr(config->phase1, "include_tls_length=1")) {
  179. wpa_printf(MSG_DEBUG, "TLS: Include TLS Message Length in "
  180. "unfragmented packets");
  181. data->include_tls_length = 1;
  182. }
  183. return 0;
  184. }
  185. /**
  186. * eap_peer_tls_ssl_deinit - Deinitialize shared TLS functionality
  187. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  188. * @data: Data for TLS processing
  189. *
  190. * This function deinitializes shared TLS functionality that was initialized
  191. * with eap_peer_tls_ssl_init().
  192. */
  193. void eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
  194. {
  195. tls_connection_deinit(sm->ssl_ctx, data->conn);
  196. eap_peer_tls_reset_input(data);
  197. eap_peer_tls_reset_output(data);
  198. }
  199. /**
  200. * eap_peer_tls_derive_key - Derive a key based on TLS session data
  201. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  202. * @data: Data for TLS processing
  203. * @label: Label string for deriving the keys, e.g., "client EAP encryption"
  204. * @len: Length of the key material to generate (usually 64 for MSK)
  205. * Returns: Pointer to allocated key on success or %NULL on failure
  206. *
  207. * This function uses TLS-PRF to generate pseudo-random data based on the TLS
  208. * session data (client/server random and master key). Each key type may use a
  209. * different label to bind the key usage into the generated material.
  210. *
  211. * The caller is responsible for freeing the returned buffer.
  212. */
  213. u8 * eap_peer_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
  214. const char *label, size_t len)
  215. {
  216. struct tls_keys keys;
  217. u8 *rnd = NULL, *out;
  218. out = os_malloc(len);
  219. if (out == NULL)
  220. return NULL;
  221. /* First, try to use TLS library function for PRF, if available. */
  222. if (tls_connection_prf(sm->ssl_ctx, data->conn, label, 0, out, len) ==
  223. 0)
  224. return out;
  225. /*
  226. * TLS library did not support key generation, so get the needed TLS
  227. * session parameters and use an internal implementation of TLS PRF to
  228. * derive the key.
  229. */
  230. if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
  231. goto fail;
  232. if (keys.client_random == NULL || keys.server_random == NULL ||
  233. keys.master_key == NULL)
  234. goto fail;
  235. rnd = os_malloc(keys.client_random_len + keys.server_random_len);
  236. if (rnd == NULL)
  237. goto fail;
  238. os_memcpy(rnd, keys.client_random, keys.client_random_len);
  239. os_memcpy(rnd + keys.client_random_len, keys.server_random,
  240. keys.server_random_len);
  241. if (tls_prf(keys.master_key, keys.master_key_len,
  242. label, rnd, keys.client_random_len +
  243. keys.server_random_len, out, len))
  244. goto fail;
  245. os_free(rnd);
  246. return out;
  247. fail:
  248. os_free(out);
  249. os_free(rnd);
  250. return NULL;
  251. }
  252. /**
  253. * eap_peer_tls_reassemble_fragment - Reassemble a received fragment
  254. * @data: Data for TLS processing
  255. * @in_data: Next incoming TLS segment
  256. * @in_len: Length of in_data
  257. * Returns: 0 on success, 1 if more data is needed for the full message, or
  258. * -1 on error
  259. */
  260. static int eap_peer_tls_reassemble_fragment(struct eap_ssl_data *data,
  261. const u8 *in_data, size_t in_len)
  262. {
  263. u8 *buf;
  264. if (data->tls_in_len + in_len == 0) {
  265. /* No message data received?! */
  266. wpa_printf(MSG_WARNING, "SSL: Invalid reassembly state: "
  267. "tls_in_left=%lu tls_in_len=%lu in_len=%lu",
  268. (unsigned long) data->tls_in_left,
  269. (unsigned long) data->tls_in_len,
  270. (unsigned long) in_len);
  271. eap_peer_tls_reset_input(data);
  272. return -1;
  273. }
  274. if (data->tls_in_len + in_len > 65536) {
  275. /*
  276. * Limit length to avoid rogue servers from causing large
  277. * memory allocations.
  278. */
  279. wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size over "
  280. "64 kB)");
  281. eap_peer_tls_reset_input(data);
  282. return -1;
  283. }
  284. if (in_len > data->tls_in_left) {
  285. /* Sender is doing something odd - reject message */
  286. wpa_printf(MSG_INFO, "SSL: more data than TLS message length "
  287. "indicated");
  288. eap_peer_tls_reset_input(data);
  289. return -1;
  290. }
  291. buf = os_realloc(data->tls_in, data->tls_in_len + in_len);
  292. if (buf == NULL) {
  293. wpa_printf(MSG_INFO, "SSL: Could not allocate memory for TLS "
  294. "data");
  295. eap_peer_tls_reset_input(data);
  296. return -1;
  297. }
  298. os_memcpy(buf + data->tls_in_len, in_data, in_len);
  299. data->tls_in = buf;
  300. data->tls_in_len += in_len;
  301. data->tls_in_left -= in_len;
  302. if (data->tls_in_left > 0) {
  303. wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input "
  304. "data", (unsigned long) data->tls_in_left);
  305. return 1;
  306. }
  307. return 0;
  308. }
  309. /**
  310. * eap_peer_tls_data_reassemble - Reassemble TLS data
  311. * @data: Data for TLS processing
  312. * @in_data: Next incoming TLS segment
  313. * @in_len: Length of in_data
  314. * @out_len: Variable for returning length of the reassembled message
  315. * @need_more_input: Variable for returning whether more input data is needed
  316. * to reassemble this TLS packet
  317. * Returns: Pointer to output data, %NULL on error or when more data is needed
  318. * for the full message (in which case, *need_more_input is also set to 1).
  319. *
  320. * This function reassembles TLS fragments. Caller must not free the returned
  321. * data buffer since an internal pointer to it is maintained.
  322. */
  323. const u8 * eap_peer_tls_data_reassemble(
  324. struct eap_ssl_data *data, const u8 *in_data, size_t in_len,
  325. size_t *out_len, int *need_more_input)
  326. {
  327. *need_more_input = 0;
  328. if (data->tls_in_left > in_len || data->tls_in) {
  329. /* Message has fragments */
  330. int res = eap_peer_tls_reassemble_fragment(data, in_data,
  331. in_len);
  332. if (res) {
  333. if (res == 1)
  334. *need_more_input = 1;
  335. return NULL;
  336. }
  337. /* Message is now fully reassembled. */
  338. } else {
  339. /* No fragments in this message, so just make a copy of it. */
  340. data->tls_in_left = 0;
  341. data->tls_in = os_malloc(in_len ? in_len : 1);
  342. if (data->tls_in == NULL)
  343. return NULL;
  344. os_memcpy(data->tls_in, in_data, in_len);
  345. data->tls_in_len = in_len;
  346. }
  347. *out_len = data->tls_in_len;
  348. return data->tls_in;
  349. }
  350. /**
  351. * eap_tls_process_input - Process incoming TLS message
  352. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  353. * @data: Data for TLS processing
  354. * @in_data: Message received from the server
  355. * @in_len: Length of in_data
  356. * @out_data: Buffer for returning a pointer to application data (if available)
  357. * Returns: 0 on success, 1 if more input data is needed, 2 if application data
  358. * is available, -1 on failure
  359. */
  360. static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data,
  361. const u8 *in_data, size_t in_len,
  362. struct wpabuf **out_data)
  363. {
  364. const u8 *msg;
  365. size_t msg_len;
  366. int need_more_input;
  367. u8 *appl_data;
  368. size_t appl_data_len;
  369. msg = eap_peer_tls_data_reassemble(data, in_data, in_len,
  370. &msg_len, &need_more_input);
  371. if (msg == NULL)
  372. return need_more_input ? 1 : -1;
  373. /* Full TLS message reassembled - continue handshake processing */
  374. if (data->tls_out) {
  375. /* This should not happen.. */
  376. wpa_printf(MSG_INFO, "SSL: eap_tls_process_input - pending "
  377. "tls_out data even though tls_out_len = 0");
  378. os_free(data->tls_out);
  379. WPA_ASSERT(data->tls_out == NULL);
  380. }
  381. appl_data = NULL;
  382. data->tls_out = tls_connection_handshake(sm->ssl_ctx, data->conn,
  383. msg, msg_len,
  384. &data->tls_out_len,
  385. &appl_data, &appl_data_len);
  386. eap_peer_tls_reset_input(data);
  387. if (appl_data &&
  388. tls_connection_established(sm->ssl_ctx, data->conn) &&
  389. !tls_connection_get_failed(sm->ssl_ctx, data->conn)) {
  390. wpa_hexdump_key(MSG_MSGDUMP, "SSL: Application data",
  391. appl_data, appl_data_len);
  392. *out_data = wpabuf_alloc_ext_data(appl_data, appl_data_len);
  393. if (*out_data == NULL) {
  394. os_free(appl_data);
  395. return -1;
  396. }
  397. return 2;
  398. }
  399. os_free(appl_data);
  400. return 0;
  401. }
  402. /**
  403. * eap_tls_process_output - Process outgoing TLS message
  404. * @data: Data for TLS processing
  405. * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
  406. * @peap_version: Version number for EAP-PEAP/TTLS
  407. * @id: EAP identifier for the response
  408. * @ret: Return value to use on success
  409. * @out_data: Buffer for returning the allocated output buffer
  410. * Returns: ret (0 or 1) on success, -1 on failure
  411. */
  412. static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type,
  413. int peap_version, u8 id, int ret,
  414. struct wpabuf **out_data)
  415. {
  416. size_t len;
  417. u8 *flags;
  418. int more_fragments, length_included;
  419. len = data->tls_out_len - data->tls_out_pos;
  420. wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
  421. "%lu bytes)",
  422. (unsigned long) len, (unsigned long) data->tls_out_len);
  423. /*
  424. * Limit outgoing message to the configured maximum size. Fragment
  425. * message if needed.
  426. */
  427. if (len > data->tls_out_limit) {
  428. more_fragments = 1;
  429. len = data->tls_out_limit;
  430. wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
  431. "will follow", (unsigned long) len);
  432. } else
  433. more_fragments = 0;
  434. length_included = data->tls_out_pos == 0 &&
  435. (data->tls_out_len > data->tls_out_limit ||
  436. data->include_tls_length);
  437. *out_data = eap_msg_alloc(EAP_VENDOR_IETF, eap_type,
  438. 1 + length_included * 4 + len,
  439. EAP_CODE_RESPONSE, id);
  440. if (*out_data == NULL)
  441. return -1;
  442. flags = wpabuf_put(*out_data, 1);
  443. *flags = peap_version;
  444. if (more_fragments)
  445. *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
  446. if (length_included) {
  447. *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
  448. wpabuf_put_be32(*out_data, data->tls_out_len);
  449. }
  450. wpabuf_put_data(*out_data, &data->tls_out[data->tls_out_pos], len);
  451. data->tls_out_pos += len;
  452. if (!more_fragments)
  453. eap_peer_tls_reset_output(data);
  454. return ret;
  455. }
  456. /**
  457. * eap_peer_tls_process_helper - Process TLS handshake message
  458. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  459. * @data: Data for TLS processing
  460. * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
  461. * @peap_version: Version number for EAP-PEAP/TTLS
  462. * @id: EAP identifier for the response
  463. * @in_data: Message received from the server
  464. * @in_len: Length of in_data
  465. * @out_data: Buffer for returning a pointer to the response message
  466. * Returns: 0 on success, 1 if more input data is needed, 2 if application data
  467. * is available, or -1 on failure
  468. *
  469. * This function can be used to process TLS handshake messages. It reassembles
  470. * the received fragments and uses a TLS library to process the messages. The
  471. * response data from the TLS library is fragmented to suitable output messages
  472. * that the caller can send out.
  473. *
  474. * out_data is used to return the response message if the return value of this
  475. * function is 0, 2, or -1. In case of failure, the message is likely a TLS
  476. * alarm message. The caller is responsible for freeing the allocated buffer if
  477. * *out_data is not %NULL.
  478. *
  479. * This function is called for each received TLS message during the TLS
  480. * handshake after eap_peer_tls_process_init() call and possible processing of
  481. * TLS Flags field. Once the handshake has been completed, i.e., when
  482. * tls_connection_established() returns 1, EAP method specific decrypting of
  483. * the tunneled data is used.
  484. */
  485. int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
  486. EapType eap_type, int peap_version,
  487. u8 id, const u8 *in_data, size_t in_len,
  488. struct wpabuf **out_data)
  489. {
  490. int ret = 0;
  491. *out_data = NULL;
  492. if (data->tls_out_len > 0 && in_len > 0) {
  493. wpa_printf(MSG_DEBUG, "SSL: Received non-ACK when output "
  494. "fragments are waiting to be sent out");
  495. return -1;
  496. }
  497. if (data->tls_out_len == 0) {
  498. /*
  499. * No more data to send out - expect to receive more data from
  500. * the AS.
  501. */
  502. int res = eap_tls_process_input(sm, data, in_data, in_len,
  503. out_data);
  504. if (res) {
  505. /*
  506. * Input processing failed (res = -1) or more data is
  507. * needed (res = 1).
  508. */
  509. return res;
  510. }
  511. /*
  512. * The incoming message has been reassembled and processed. The
  513. * response was allocated into data->tls_out buffer.
  514. */
  515. }
  516. if (data->tls_out == NULL) {
  517. /*
  518. * No outgoing fragments remaining from the previous message
  519. * and no new message generated. This indicates an error in TLS
  520. * processing.
  521. */
  522. eap_peer_tls_reset_output(data);
  523. return -1;
  524. }
  525. if (tls_connection_get_failed(sm->ssl_ctx, data->conn)) {
  526. /* TLS processing has failed - return error */
  527. wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
  528. "report error");
  529. ret = -1;
  530. /* TODO: clean pin if engine used? */
  531. }
  532. if (data->tls_out_len == 0) {
  533. /*
  534. * TLS negotiation should now be complete since all other cases
  535. * needing more data should have been caught above based on
  536. * the TLS Message Length field.
  537. */
  538. wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
  539. os_free(data->tls_out);
  540. data->tls_out = NULL;
  541. return 1;
  542. }
  543. /* Send the pending message (in fragments, if needed). */
  544. return eap_tls_process_output(data, eap_type, peap_version, id, ret,
  545. out_data);
  546. }
  547. /**
  548. * eap_peer_tls_build_ack - Build a TLS ACK frame
  549. * @id: EAP identifier for the response
  550. * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
  551. * @peap_version: Version number for EAP-PEAP/TTLS
  552. * Returns: Pointer to the allocated ACK frame or %NULL on failure
  553. */
  554. struct wpabuf * eap_peer_tls_build_ack(u8 id, EapType eap_type,
  555. int peap_version)
  556. {
  557. struct wpabuf *resp;
  558. resp = eap_msg_alloc(EAP_VENDOR_IETF, eap_type, 1, EAP_CODE_RESPONSE,
  559. id);
  560. if (resp == NULL)
  561. return NULL;
  562. wpa_printf(MSG_DEBUG, "SSL: Building ACK (type=%d id=%d ver=%d)",
  563. (int) eap_type, id, peap_version);
  564. wpabuf_put_u8(resp, peap_version); /* Flags */
  565. return resp;
  566. }
  567. /**
  568. * eap_peer_tls_reauth_init - Re-initialize shared TLS for session resumption
  569. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  570. * @data: Data for TLS processing
  571. * Returns: 0 on success, -1 on failure
  572. */
  573. int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data)
  574. {
  575. eap_peer_tls_reset_input(data);
  576. eap_peer_tls_reset_output(data);
  577. return tls_connection_shutdown(sm->ssl_ctx, data->conn);
  578. }
  579. /**
  580. * eap_peer_tls_status - Get TLS status
  581. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  582. * @data: Data for TLS processing
  583. * @buf: Buffer for status information
  584. * @buflen: Maximum buffer length
  585. * @verbose: Whether to include verbose status information
  586. * Returns: Number of bytes written to buf.
  587. */
  588. int eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data,
  589. char *buf, size_t buflen, int verbose)
  590. {
  591. char name[128];
  592. int len = 0, ret;
  593. if (tls_get_cipher(sm->ssl_ctx, data->conn, name, sizeof(name)) == 0) {
  594. ret = os_snprintf(buf + len, buflen - len,
  595. "EAP TLS cipher=%s\n", name);
  596. if (ret < 0 || (size_t) ret >= buflen - len)
  597. return len;
  598. len += ret;
  599. }
  600. return len;
  601. }
  602. /**
  603. * eap_peer_tls_process_init - Initial validation/processing of EAP requests
  604. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  605. * @data: Data for TLS processing
  606. * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
  607. * @ret: Return values from EAP request validation and processing
  608. * @reqData: EAP request to be processed (eapReqData)
  609. * @len: Buffer for returning length of the remaining payload
  610. * @flags: Buffer for returning TLS flags
  611. * Returns: Pointer to payload after TLS flags and length or %NULL on failure
  612. *
  613. * This function validates the EAP header and processes the optional TLS
  614. * Message Length field. If this is the first fragment of a TLS message, the
  615. * TLS reassembly code is initialized to receive the indicated number of bytes.
  616. *
  617. * EAP-TLS, EAP-PEAP, EAP-TTLS, and EAP-FAST methods are expected to use this
  618. * function as the first step in processing received messages. They will need
  619. * to process the flags (apart from Message Length Included) that are returned
  620. * through the flags pointer and the message payload that will be returned (and
  621. * the length is returned through the len pointer). Return values (ret) are set
  622. * for continuation of EAP method processing. The caller is responsible for
  623. * setting these to indicate completion (either success or failure) based on
  624. * the authentication result.
  625. */
  626. const u8 * eap_peer_tls_process_init(struct eap_sm *sm,
  627. struct eap_ssl_data *data,
  628. EapType eap_type,
  629. struct eap_method_ret *ret,
  630. const struct wpabuf *reqData,
  631. size_t *len, u8 *flags)
  632. {
  633. const u8 *pos;
  634. size_t left;
  635. unsigned int tls_msg_len;
  636. if (tls_get_errors(sm->ssl_ctx)) {
  637. wpa_printf(MSG_INFO, "SSL: TLS errors detected");
  638. ret->ignore = TRUE;
  639. return NULL;
  640. }
  641. pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData, &left);
  642. if (pos == NULL) {
  643. ret->ignore = TRUE;
  644. return NULL;
  645. }
  646. *flags = *pos++;
  647. left--;
  648. wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - "
  649. "Flags 0x%02x", (unsigned long) wpabuf_len(reqData),
  650. *flags);
  651. if (*flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
  652. if (left < 4) {
  653. wpa_printf(MSG_INFO, "SSL: Short frame with TLS "
  654. "length");
  655. ret->ignore = TRUE;
  656. return NULL;
  657. }
  658. tls_msg_len = WPA_GET_BE32(pos);
  659. wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d",
  660. tls_msg_len);
  661. if (data->tls_in_left == 0) {
  662. data->tls_in_total = tls_msg_len;
  663. data->tls_in_left = tls_msg_len;
  664. os_free(data->tls_in);
  665. data->tls_in = NULL;
  666. data->tls_in_len = 0;
  667. }
  668. pos += 4;
  669. left -= 4;
  670. }
  671. ret->ignore = FALSE;
  672. ret->methodState = METHOD_MAY_CONT;
  673. ret->decision = DECISION_FAIL;
  674. ret->allowNotifications = TRUE;
  675. *len = left;
  676. return pos;
  677. }
  678. /**
  679. * eap_peer_tls_reset_input - Reset input buffers
  680. * @data: Data for TLS processing
  681. *
  682. * This function frees any allocated memory for input buffers and resets input
  683. * state.
  684. */
  685. void eap_peer_tls_reset_input(struct eap_ssl_data *data)
  686. {
  687. data->tls_in_left = data->tls_in_total = data->tls_in_len = 0;
  688. os_free(data->tls_in);
  689. data->tls_in = NULL;
  690. }
  691. /**
  692. * eap_peer_tls_reset_output - Reset output buffers
  693. * @data: Data for TLS processing
  694. *
  695. * This function frees any allocated memory for output buffers and resets
  696. * output state.
  697. */
  698. void eap_peer_tls_reset_output(struct eap_ssl_data *data)
  699. {
  700. data->tls_out_len = 0;
  701. data->tls_out_pos = 0;
  702. os_free(data->tls_out);
  703. data->tls_out = NULL;
  704. }
  705. /**
  706. * eap_peer_tls_decrypt - Decrypt received phase 2 TLS message
  707. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  708. * @data: Data for TLS processing
  709. * @in_data: Message received from the server
  710. * @in_decrypted: Buffer for returning a pointer to the decrypted message
  711. * Returns: 0 on success, 1 if more input data is needed, or -1 on failure
  712. */
  713. int eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data,
  714. const struct wpabuf *in_data,
  715. struct wpabuf **in_decrypted)
  716. {
  717. int res;
  718. const u8 *msg;
  719. size_t msg_len, buf_len;
  720. int need_more_input;
  721. msg = eap_peer_tls_data_reassemble(data, wpabuf_head(in_data),
  722. wpabuf_len(in_data), &msg_len,
  723. &need_more_input);
  724. if (msg == NULL)
  725. return need_more_input ? 1 : -1;
  726. buf_len = wpabuf_len(in_data);
  727. if (data->tls_in_total > buf_len)
  728. buf_len = data->tls_in_total;
  729. /*
  730. * Even though we try to disable TLS compression, it is possible that
  731. * this cannot be done with all TLS libraries. Add extra buffer space
  732. * to handle the possibility of the decrypted data being longer than
  733. * input data.
  734. */
  735. buf_len += 500;
  736. buf_len *= 3;
  737. *in_decrypted = wpabuf_alloc(buf_len ? buf_len : 1);
  738. if (*in_decrypted == NULL) {
  739. eap_peer_tls_reset_input(data);
  740. wpa_printf(MSG_WARNING, "SSL: Failed to allocate memory for "
  741. "decryption");
  742. return -1;
  743. }
  744. res = tls_connection_decrypt(sm->ssl_ctx, data->conn, msg, msg_len,
  745. wpabuf_mhead(*in_decrypted), buf_len);
  746. eap_peer_tls_reset_input(data);
  747. if (res < 0) {
  748. wpa_printf(MSG_INFO, "SSL: Failed to decrypt Phase 2 data");
  749. return -1;
  750. }
  751. wpabuf_put(*in_decrypted, res);
  752. return 0;
  753. }
  754. /**
  755. * eap_peer_tls_encrypt - Encrypt phase 2 TLS message
  756. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  757. * @data: Data for TLS processing
  758. * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
  759. * @peap_version: Version number for EAP-PEAP/TTLS
  760. * @id: EAP identifier for the response
  761. * @in_data: Plaintext phase 2 data to encrypt or %NULL to continue fragments
  762. * @out_data: Buffer for returning a pointer to the encrypted response message
  763. * Returns: 0 on success, -1 on failure
  764. */
  765. int eap_peer_tls_encrypt(struct eap_sm *sm, struct eap_ssl_data *data,
  766. EapType eap_type, int peap_version, u8 id,
  767. const struct wpabuf *in_data,
  768. struct wpabuf **out_data)
  769. {
  770. int res;
  771. size_t len;
  772. if (in_data) {
  773. eap_peer_tls_reset_output(data);
  774. len = wpabuf_len(in_data) + 100;
  775. data->tls_out = os_malloc(len);
  776. if (data->tls_out == NULL)
  777. return -1;
  778. res = tls_connection_encrypt(sm->ssl_ctx, data->conn,
  779. wpabuf_head(in_data),
  780. wpabuf_len(in_data),
  781. data->tls_out, len);
  782. if (res < 0) {
  783. wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 "
  784. "data (in_len=%lu)",
  785. (unsigned long) wpabuf_len(in_data));
  786. eap_peer_tls_reset_output(data);
  787. return -1;
  788. }
  789. data->tls_out_len = res;
  790. }
  791. return eap_tls_process_output(data, eap_type, peap_version, id, 0,
  792. out_data);
  793. }
  794. /**
  795. * eap_peer_select_phase2_methods - Select phase 2 EAP method
  796. * @config: Pointer to the network configuration
  797. * @prefix: 'phase2' configuration prefix, e.g., "auth="
  798. * @types: Buffer for returning allocated list of allowed EAP methods
  799. * @num_types: Buffer for returning number of allocated EAP methods
  800. * Returns: 0 on success, -1 on failure
  801. *
  802. * This function is used to parse EAP method list and select allowed methods
  803. * for Phase2 authentication.
  804. */
  805. int eap_peer_select_phase2_methods(struct eap_peer_config *config,
  806. const char *prefix,
  807. struct eap_method_type **types,
  808. size_t *num_types)
  809. {
  810. char *start, *pos, *buf;
  811. struct eap_method_type *methods = NULL, *_methods;
  812. u8 method;
  813. size_t num_methods = 0, prefix_len;
  814. if (config == NULL || config->phase2 == NULL)
  815. goto get_defaults;
  816. start = buf = os_strdup(config->phase2);
  817. if (buf == NULL)
  818. return -1;
  819. prefix_len = os_strlen(prefix);
  820. while (start && *start != '\0') {
  821. int vendor;
  822. pos = os_strstr(start, prefix);
  823. if (pos == NULL)
  824. break;
  825. if (start != pos && *(pos - 1) != ' ') {
  826. start = pos + prefix_len;
  827. continue;
  828. }
  829. start = pos + prefix_len;
  830. pos = os_strchr(start, ' ');
  831. if (pos)
  832. *pos++ = '\0';
  833. method = eap_get_phase2_type(start, &vendor);
  834. if (vendor == EAP_VENDOR_IETF && method == EAP_TYPE_NONE) {
  835. wpa_printf(MSG_ERROR, "TLS: Unsupported Phase2 EAP "
  836. "method '%s'", start);
  837. } else {
  838. num_methods++;
  839. _methods = os_realloc(methods,
  840. num_methods * sizeof(*methods));
  841. if (_methods == NULL) {
  842. os_free(methods);
  843. os_free(buf);
  844. return -1;
  845. }
  846. methods = _methods;
  847. methods[num_methods - 1].vendor = vendor;
  848. methods[num_methods - 1].method = method;
  849. }
  850. start = pos;
  851. }
  852. os_free(buf);
  853. get_defaults:
  854. if (methods == NULL)
  855. methods = eap_get_phase2_types(config, &num_methods);
  856. if (methods == NULL) {
  857. wpa_printf(MSG_ERROR, "TLS: No Phase2 EAP methods available");
  858. return -1;
  859. }
  860. wpa_hexdump(MSG_DEBUG, "TLS: Phase2 EAP types",
  861. (u8 *) methods,
  862. num_methods * sizeof(struct eap_method_type));
  863. *types = methods;
  864. *num_types = num_methods;
  865. return 0;
  866. }
  867. /**
  868. * eap_peer_tls_phase2_nak - Generate EAP-Nak for Phase 2
  869. * @types: Buffer for returning allocated list of allowed EAP methods
  870. * @num_types: Buffer for returning number of allocated EAP methods
  871. * @hdr: EAP-Request header (and the following EAP type octet)
  872. * @resp: Buffer for returning the EAP-Nak message
  873. * Returns: 0 on success, -1 on failure
  874. */
  875. int eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types,
  876. struct eap_hdr *hdr, struct wpabuf **resp)
  877. {
  878. u8 *pos = (u8 *) (hdr + 1);
  879. size_t i;
  880. /* TODO: add support for expanded Nak */
  881. wpa_printf(MSG_DEBUG, "TLS: Phase 2 Request: Nak type=%d", *pos);
  882. wpa_hexdump(MSG_DEBUG, "TLS: Allowed Phase2 EAP types",
  883. (u8 *) types, num_types * sizeof(struct eap_method_type));
  884. *resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK, num_types,
  885. EAP_CODE_RESPONSE, hdr->identifier);
  886. if (*resp == NULL)
  887. return -1;
  888. for (i = 0; i < num_types; i++) {
  889. if (types[i].vendor == EAP_VENDOR_IETF &&
  890. types[i].method < 256)
  891. wpabuf_put_u8(*resp, types[i].method);
  892. }
  893. eap_update_len(*resp);
  894. return 0;
  895. }