eap_tls_common.c 29 KB

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