eap_tls_common.c 33 KB

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