eap_tls_common.c 31 KB

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