eap_tls_common.c 33 KB

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