eap_tls_common.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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_derive_session_id - Derive a Session-Id based on TLS data
  297. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  298. * @data: Data for TLS processing
  299. * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
  300. * @len: Pointer to length of the session ID generated
  301. * Returns: Pointer to allocated Session-Id on success or %NULL on failure
  302. *
  303. * This function derive the Session-Id based on the TLS session data
  304. * (client/server random and method type).
  305. *
  306. * The caller is responsible for freeing the returned buffer.
  307. */
  308. u8 * eap_peer_tls_derive_session_id(struct eap_sm *sm,
  309. struct eap_ssl_data *data, u8 eap_type,
  310. size_t *len)
  311. {
  312. struct tls_keys keys;
  313. u8 *out;
  314. /*
  315. * TLS library did not support session ID generation,
  316. * so get the needed TLS session parameters
  317. */
  318. if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
  319. return NULL;
  320. if (keys.client_random == NULL || keys.server_random == NULL ||
  321. keys.master_key == NULL)
  322. return NULL;
  323. *len = 1 + keys.client_random_len + keys.server_random_len;
  324. out = os_malloc(*len);
  325. if (out == NULL)
  326. return NULL;
  327. /* Session-Id = EAP type || client.random || server.random */
  328. out[0] = eap_type;
  329. os_memcpy(out + 1, keys.client_random, keys.client_random_len);
  330. os_memcpy(out + 1 + keys.client_random_len, keys.server_random,
  331. keys.server_random_len);
  332. return out;
  333. }
  334. /**
  335. * eap_peer_tls_reassemble_fragment - Reassemble a received fragment
  336. * @data: Data for TLS processing
  337. * @in_data: Next incoming TLS segment
  338. * Returns: 0 on success, 1 if more data is needed for the full message, or
  339. * -1 on error
  340. */
  341. static int eap_peer_tls_reassemble_fragment(struct eap_ssl_data *data,
  342. const struct wpabuf *in_data)
  343. {
  344. size_t tls_in_len, in_len;
  345. tls_in_len = data->tls_in ? wpabuf_len(data->tls_in) : 0;
  346. in_len = in_data ? wpabuf_len(in_data) : 0;
  347. if (tls_in_len + in_len == 0) {
  348. /* No message data received?! */
  349. wpa_printf(MSG_WARNING, "SSL: Invalid reassembly state: "
  350. "tls_in_left=%lu tls_in_len=%lu in_len=%lu",
  351. (unsigned long) data->tls_in_left,
  352. (unsigned long) tls_in_len,
  353. (unsigned long) in_len);
  354. eap_peer_tls_reset_input(data);
  355. return -1;
  356. }
  357. if (tls_in_len + in_len > 65536) {
  358. /*
  359. * Limit length to avoid rogue servers from causing large
  360. * memory allocations.
  361. */
  362. wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size over "
  363. "64 kB)");
  364. eap_peer_tls_reset_input(data);
  365. return -1;
  366. }
  367. if (in_len > data->tls_in_left) {
  368. /* Sender is doing something odd - reject message */
  369. wpa_printf(MSG_INFO, "SSL: more data than TLS message length "
  370. "indicated");
  371. eap_peer_tls_reset_input(data);
  372. return -1;
  373. }
  374. if (wpabuf_resize(&data->tls_in, in_len) < 0) {
  375. wpa_printf(MSG_INFO, "SSL: Could not allocate memory for TLS "
  376. "data");
  377. eap_peer_tls_reset_input(data);
  378. return -1;
  379. }
  380. if (in_data)
  381. wpabuf_put_buf(data->tls_in, in_data);
  382. data->tls_in_left -= in_len;
  383. if (data->tls_in_left > 0) {
  384. wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input "
  385. "data", (unsigned long) data->tls_in_left);
  386. return 1;
  387. }
  388. return 0;
  389. }
  390. /**
  391. * eap_peer_tls_data_reassemble - Reassemble TLS data
  392. * @data: Data for TLS processing
  393. * @in_data: Next incoming TLS segment
  394. * @need_more_input: Variable for returning whether more input data is needed
  395. * to reassemble this TLS packet
  396. * Returns: Pointer to output data, %NULL on error or when more data is needed
  397. * for the full message (in which case, *need_more_input is also set to 1).
  398. *
  399. * This function reassembles TLS fragments. Caller must not free the returned
  400. * data buffer since an internal pointer to it is maintained.
  401. */
  402. static const struct wpabuf * eap_peer_tls_data_reassemble(
  403. struct eap_ssl_data *data, const struct wpabuf *in_data,
  404. int *need_more_input)
  405. {
  406. *need_more_input = 0;
  407. if (data->tls_in_left > wpabuf_len(in_data) || data->tls_in) {
  408. /* Message has fragments */
  409. int res = eap_peer_tls_reassemble_fragment(data, in_data);
  410. if (res) {
  411. if (res == 1)
  412. *need_more_input = 1;
  413. return NULL;
  414. }
  415. /* Message is now fully reassembled. */
  416. } else {
  417. /* No fragments in this message, so just make a copy of it. */
  418. data->tls_in_left = 0;
  419. data->tls_in = wpabuf_dup(in_data);
  420. if (data->tls_in == NULL)
  421. return NULL;
  422. }
  423. return data->tls_in;
  424. }
  425. /**
  426. * eap_tls_process_input - Process incoming TLS message
  427. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  428. * @data: Data for TLS processing
  429. * @in_data: Message received from the server
  430. * @in_len: Length of in_data
  431. * @out_data: Buffer for returning a pointer to application data (if available)
  432. * Returns: 0 on success, 1 if more input data is needed, 2 if application data
  433. * is available, -1 on failure
  434. */
  435. static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data,
  436. const u8 *in_data, size_t in_len,
  437. struct wpabuf **out_data)
  438. {
  439. const struct wpabuf *msg;
  440. int need_more_input;
  441. struct wpabuf *appl_data;
  442. struct wpabuf buf;
  443. wpabuf_set(&buf, in_data, in_len);
  444. msg = eap_peer_tls_data_reassemble(data, &buf, &need_more_input);
  445. if (msg == NULL)
  446. return need_more_input ? 1 : -1;
  447. /* Full TLS message reassembled - continue handshake processing */
  448. if (data->tls_out) {
  449. /* This should not happen.. */
  450. wpa_printf(MSG_INFO, "SSL: eap_tls_process_input - pending "
  451. "tls_out data even though tls_out_len = 0");
  452. wpabuf_free(data->tls_out);
  453. WPA_ASSERT(data->tls_out == NULL);
  454. }
  455. appl_data = NULL;
  456. data->tls_out = tls_connection_handshake(data->ssl_ctx, data->conn,
  457. msg, &appl_data);
  458. eap_peer_tls_reset_input(data);
  459. if (appl_data &&
  460. tls_connection_established(data->ssl_ctx, data->conn) &&
  461. !tls_connection_get_failed(data->ssl_ctx, data->conn)) {
  462. wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application data",
  463. appl_data);
  464. *out_data = appl_data;
  465. return 2;
  466. }
  467. wpabuf_free(appl_data);
  468. return 0;
  469. }
  470. /**
  471. * eap_tls_process_output - Process outgoing TLS message
  472. * @data: Data for TLS processing
  473. * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
  474. * @peap_version: Version number for EAP-PEAP/TTLS
  475. * @id: EAP identifier for the response
  476. * @ret: Return value to use on success
  477. * @out_data: Buffer for returning the allocated output buffer
  478. * Returns: ret (0 or 1) on success, -1 on failure
  479. */
  480. static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type,
  481. int peap_version, u8 id, int ret,
  482. struct wpabuf **out_data)
  483. {
  484. size_t len;
  485. u8 *flags;
  486. int more_fragments, length_included;
  487. if (data->tls_out == NULL)
  488. return -1;
  489. len = wpabuf_len(data->tls_out) - data->tls_out_pos;
  490. wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
  491. "%lu bytes)",
  492. (unsigned long) len,
  493. (unsigned long) wpabuf_len(data->tls_out));
  494. /*
  495. * Limit outgoing message to the configured maximum size. Fragment
  496. * message if needed.
  497. */
  498. if (len > data->tls_out_limit) {
  499. more_fragments = 1;
  500. len = data->tls_out_limit;
  501. wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
  502. "will follow", (unsigned long) len);
  503. } else
  504. more_fragments = 0;
  505. length_included = data->tls_out_pos == 0 &&
  506. (wpabuf_len(data->tls_out) > data->tls_out_limit ||
  507. data->include_tls_length);
  508. if (!length_included &&
  509. eap_type == EAP_TYPE_PEAP && peap_version == 0 &&
  510. !tls_connection_established(data->eap->ssl_ctx, data->conn)) {
  511. /*
  512. * Windows Server 2008 NPS really wants to have the TLS Message
  513. * length included in phase 0 even for unfragmented frames or
  514. * it will get very confused with Compound MAC calculation and
  515. * Outer TLVs.
  516. */
  517. length_included = 1;
  518. }
  519. *out_data = eap_tls_msg_alloc(eap_type, 1 + length_included * 4 + len,
  520. EAP_CODE_RESPONSE, id);
  521. if (*out_data == NULL)
  522. return -1;
  523. flags = wpabuf_put(*out_data, 1);
  524. *flags = peap_version;
  525. if (more_fragments)
  526. *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
  527. if (length_included) {
  528. *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
  529. wpabuf_put_be32(*out_data, wpabuf_len(data->tls_out));
  530. }
  531. wpabuf_put_data(*out_data,
  532. wpabuf_head_u8(data->tls_out) + data->tls_out_pos,
  533. len);
  534. data->tls_out_pos += len;
  535. if (!more_fragments)
  536. eap_peer_tls_reset_output(data);
  537. return ret;
  538. }
  539. /**
  540. * eap_peer_tls_process_helper - Process TLS handshake message
  541. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  542. * @data: Data for TLS processing
  543. * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
  544. * @peap_version: Version number for EAP-PEAP/TTLS
  545. * @id: EAP identifier for the response
  546. * @in_data: Message received from the server
  547. * @in_len: Length of in_data
  548. * @out_data: Buffer for returning a pointer to the response message
  549. * Returns: 0 on success, 1 if more input data is needed, 2 if application data
  550. * is available, or -1 on failure
  551. *
  552. * This function can be used to process TLS handshake messages. It reassembles
  553. * the received fragments and uses a TLS library to process the messages. The
  554. * response data from the TLS library is fragmented to suitable output messages
  555. * that the caller can send out.
  556. *
  557. * out_data is used to return the response message if the return value of this
  558. * function is 0, 2, or -1. In case of failure, the message is likely a TLS
  559. * alarm message. The caller is responsible for freeing the allocated buffer if
  560. * *out_data is not %NULL.
  561. *
  562. * This function is called for each received TLS message during the TLS
  563. * handshake after eap_peer_tls_process_init() call and possible processing of
  564. * TLS Flags field. Once the handshake has been completed, i.e., when
  565. * tls_connection_established() returns 1, EAP method specific decrypting of
  566. * the tunneled data is used.
  567. */
  568. int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
  569. EapType eap_type, int peap_version,
  570. u8 id, const u8 *in_data, size_t in_len,
  571. struct wpabuf **out_data)
  572. {
  573. int ret = 0;
  574. *out_data = NULL;
  575. if (data->tls_out && wpabuf_len(data->tls_out) > 0 && in_len > 0) {
  576. wpa_printf(MSG_DEBUG, "SSL: Received non-ACK when output "
  577. "fragments are waiting to be sent out");
  578. return -1;
  579. }
  580. if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
  581. /*
  582. * No more data to send out - expect to receive more data from
  583. * the AS.
  584. */
  585. int res = eap_tls_process_input(sm, data, in_data, in_len,
  586. out_data);
  587. if (res) {
  588. /*
  589. * Input processing failed (res = -1) or more data is
  590. * needed (res = 1).
  591. */
  592. return res;
  593. }
  594. /*
  595. * The incoming message has been reassembled and processed. The
  596. * response was allocated into data->tls_out buffer.
  597. */
  598. }
  599. if (data->tls_out == NULL) {
  600. /*
  601. * No outgoing fragments remaining from the previous message
  602. * and no new message generated. This indicates an error in TLS
  603. * processing.
  604. */
  605. eap_peer_tls_reset_output(data);
  606. return -1;
  607. }
  608. if (tls_connection_get_failed(data->ssl_ctx, data->conn)) {
  609. /* TLS processing has failed - return error */
  610. wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
  611. "report error");
  612. ret = -1;
  613. /* TODO: clean pin if engine used? */
  614. }
  615. if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
  616. /*
  617. * TLS negotiation should now be complete since all other cases
  618. * needing more data should have been caught above based on
  619. * the TLS Message Length field.
  620. */
  621. wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
  622. wpabuf_free(data->tls_out);
  623. data->tls_out = NULL;
  624. return 1;
  625. }
  626. /* Send the pending message (in fragments, if needed). */
  627. return eap_tls_process_output(data, eap_type, peap_version, id, ret,
  628. out_data);
  629. }
  630. /**
  631. * eap_peer_tls_build_ack - Build a TLS ACK frame
  632. * @id: EAP identifier for the response
  633. * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
  634. * @peap_version: Version number for EAP-PEAP/TTLS
  635. * Returns: Pointer to the allocated ACK frame or %NULL on failure
  636. */
  637. struct wpabuf * eap_peer_tls_build_ack(u8 id, EapType eap_type,
  638. int peap_version)
  639. {
  640. struct wpabuf *resp;
  641. resp = eap_tls_msg_alloc(eap_type, 1, EAP_CODE_RESPONSE, id);
  642. if (resp == NULL)
  643. return NULL;
  644. wpa_printf(MSG_DEBUG, "SSL: Building ACK (type=%d id=%d ver=%d)",
  645. (int) eap_type, id, peap_version);
  646. wpabuf_put_u8(resp, peap_version); /* Flags */
  647. return resp;
  648. }
  649. /**
  650. * eap_peer_tls_reauth_init - Re-initialize shared TLS for session resumption
  651. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  652. * @data: Data for TLS processing
  653. * Returns: 0 on success, -1 on failure
  654. */
  655. int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data)
  656. {
  657. eap_peer_tls_reset_input(data);
  658. eap_peer_tls_reset_output(data);
  659. return tls_connection_shutdown(data->ssl_ctx, data->conn);
  660. }
  661. /**
  662. * eap_peer_tls_status - Get TLS status
  663. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  664. * @data: Data for TLS processing
  665. * @buf: Buffer for status information
  666. * @buflen: Maximum buffer length
  667. * @verbose: Whether to include verbose status information
  668. * Returns: Number of bytes written to buf.
  669. */
  670. int eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data,
  671. char *buf, size_t buflen, int verbose)
  672. {
  673. char name[128];
  674. int len = 0, ret;
  675. if (tls_get_cipher(data->ssl_ctx, data->conn, name, sizeof(name)) == 0)
  676. {
  677. ret = os_snprintf(buf + len, buflen - len,
  678. "EAP TLS cipher=%s\n", name);
  679. if (ret < 0 || (size_t) ret >= buflen - len)
  680. return len;
  681. len += ret;
  682. }
  683. return len;
  684. }
  685. /**
  686. * eap_peer_tls_process_init - Initial validation/processing of EAP requests
  687. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  688. * @data: Data for TLS processing
  689. * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
  690. * @ret: Return values from EAP request validation and processing
  691. * @reqData: EAP request to be processed (eapReqData)
  692. * @len: Buffer for returning length of the remaining payload
  693. * @flags: Buffer for returning TLS flags
  694. * Returns: Pointer to payload after TLS flags and length or %NULL on failure
  695. *
  696. * This function validates the EAP header and processes the optional TLS
  697. * Message Length field. If this is the first fragment of a TLS message, the
  698. * TLS reassembly code is initialized to receive the indicated number of bytes.
  699. *
  700. * EAP-TLS, EAP-PEAP, EAP-TTLS, and EAP-FAST methods are expected to use this
  701. * function as the first step in processing received messages. They will need
  702. * to process the flags (apart from Message Length Included) that are returned
  703. * through the flags pointer and the message payload that will be returned (and
  704. * the length is returned through the len pointer). Return values (ret) are set
  705. * for continuation of EAP method processing. The caller is responsible for
  706. * setting these to indicate completion (either success or failure) based on
  707. * the authentication result.
  708. */
  709. const u8 * eap_peer_tls_process_init(struct eap_sm *sm,
  710. struct eap_ssl_data *data,
  711. EapType eap_type,
  712. struct eap_method_ret *ret,
  713. const struct wpabuf *reqData,
  714. size_t *len, u8 *flags)
  715. {
  716. const u8 *pos;
  717. size_t left;
  718. unsigned int tls_msg_len;
  719. if (tls_get_errors(data->ssl_ctx)) {
  720. wpa_printf(MSG_INFO, "SSL: TLS errors detected");
  721. ret->ignore = TRUE;
  722. return NULL;
  723. }
  724. if (eap_type == EAP_UNAUTH_TLS_TYPE)
  725. pos = eap_hdr_validate(EAP_VENDOR_UNAUTH_TLS,
  726. EAP_VENDOR_TYPE_UNAUTH_TLS, reqData,
  727. &left);
  728. else
  729. pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData,
  730. &left);
  731. if (pos == NULL) {
  732. ret->ignore = TRUE;
  733. return NULL;
  734. }
  735. if (left == 0) {
  736. wpa_printf(MSG_DEBUG, "SSL: Invalid TLS message: no Flags "
  737. "octet included");
  738. if (!sm->workaround) {
  739. ret->ignore = TRUE;
  740. return NULL;
  741. }
  742. wpa_printf(MSG_DEBUG, "SSL: Workaround - assume no Flags "
  743. "indicates ACK frame");
  744. *flags = 0;
  745. } else {
  746. *flags = *pos++;
  747. left--;
  748. }
  749. wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - "
  750. "Flags 0x%02x", (unsigned long) wpabuf_len(reqData),
  751. *flags);
  752. if (*flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
  753. if (left < 4) {
  754. wpa_printf(MSG_INFO, "SSL: Short frame with TLS "
  755. "length");
  756. ret->ignore = TRUE;
  757. return NULL;
  758. }
  759. tls_msg_len = WPA_GET_BE32(pos);
  760. wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d",
  761. tls_msg_len);
  762. if (data->tls_in_left == 0) {
  763. data->tls_in_total = tls_msg_len;
  764. data->tls_in_left = tls_msg_len;
  765. wpabuf_free(data->tls_in);
  766. data->tls_in = NULL;
  767. }
  768. pos += 4;
  769. left -= 4;
  770. if (left > tls_msg_len) {
  771. wpa_printf(MSG_INFO, "SSL: TLS Message Length (%d "
  772. "bytes) smaller than this fragment (%d "
  773. "bytes)", (int) tls_msg_len, (int) left);
  774. ret->ignore = TRUE;
  775. return NULL;
  776. }
  777. }
  778. ret->ignore = FALSE;
  779. ret->methodState = METHOD_MAY_CONT;
  780. ret->decision = DECISION_FAIL;
  781. ret->allowNotifications = TRUE;
  782. *len = left;
  783. return pos;
  784. }
  785. /**
  786. * eap_peer_tls_reset_input - Reset input buffers
  787. * @data: Data for TLS processing
  788. *
  789. * This function frees any allocated memory for input buffers and resets input
  790. * state.
  791. */
  792. void eap_peer_tls_reset_input(struct eap_ssl_data *data)
  793. {
  794. data->tls_in_left = data->tls_in_total = 0;
  795. wpabuf_free(data->tls_in);
  796. data->tls_in = NULL;
  797. }
  798. /**
  799. * eap_peer_tls_reset_output - Reset output buffers
  800. * @data: Data for TLS processing
  801. *
  802. * This function frees any allocated memory for output buffers and resets
  803. * output state.
  804. */
  805. void eap_peer_tls_reset_output(struct eap_ssl_data *data)
  806. {
  807. data->tls_out_pos = 0;
  808. wpabuf_free(data->tls_out);
  809. data->tls_out = NULL;
  810. }
  811. /**
  812. * eap_peer_tls_decrypt - Decrypt received phase 2 TLS message
  813. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  814. * @data: Data for TLS processing
  815. * @in_data: Message received from the server
  816. * @in_decrypted: Buffer for returning a pointer to the decrypted message
  817. * Returns: 0 on success, 1 if more input data is needed, or -1 on failure
  818. */
  819. int eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data,
  820. const struct wpabuf *in_data,
  821. struct wpabuf **in_decrypted)
  822. {
  823. const struct wpabuf *msg;
  824. int need_more_input;
  825. msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input);
  826. if (msg == NULL)
  827. return need_more_input ? 1 : -1;
  828. *in_decrypted = tls_connection_decrypt(data->ssl_ctx, data->conn, msg);
  829. eap_peer_tls_reset_input(data);
  830. if (*in_decrypted == NULL) {
  831. wpa_printf(MSG_INFO, "SSL: Failed to decrypt Phase 2 data");
  832. return -1;
  833. }
  834. return 0;
  835. }
  836. /**
  837. * eap_peer_tls_encrypt - Encrypt phase 2 TLS message
  838. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  839. * @data: Data for TLS processing
  840. * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
  841. * @peap_version: Version number for EAP-PEAP/TTLS
  842. * @id: EAP identifier for the response
  843. * @in_data: Plaintext phase 2 data to encrypt or %NULL to continue fragments
  844. * @out_data: Buffer for returning a pointer to the encrypted response message
  845. * Returns: 0 on success, -1 on failure
  846. */
  847. int eap_peer_tls_encrypt(struct eap_sm *sm, struct eap_ssl_data *data,
  848. EapType eap_type, int peap_version, u8 id,
  849. const struct wpabuf *in_data,
  850. struct wpabuf **out_data)
  851. {
  852. if (in_data) {
  853. eap_peer_tls_reset_output(data);
  854. data->tls_out = tls_connection_encrypt(data->ssl_ctx,
  855. data->conn, in_data);
  856. if (data->tls_out == NULL) {
  857. wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 "
  858. "data (in_len=%lu)",
  859. (unsigned long) wpabuf_len(in_data));
  860. eap_peer_tls_reset_output(data);
  861. return -1;
  862. }
  863. }
  864. return eap_tls_process_output(data, eap_type, peap_version, id, 0,
  865. out_data);
  866. }
  867. /**
  868. * eap_peer_select_phase2_methods - Select phase 2 EAP method
  869. * @config: Pointer to the network configuration
  870. * @prefix: 'phase2' configuration prefix, e.g., "auth="
  871. * @types: Buffer for returning allocated list of allowed EAP methods
  872. * @num_types: Buffer for returning number of allocated EAP methods
  873. * Returns: 0 on success, -1 on failure
  874. *
  875. * This function is used to parse EAP method list and select allowed methods
  876. * for Phase2 authentication.
  877. */
  878. int eap_peer_select_phase2_methods(struct eap_peer_config *config,
  879. const char *prefix,
  880. struct eap_method_type **types,
  881. size_t *num_types)
  882. {
  883. char *start, *pos, *buf;
  884. struct eap_method_type *methods = NULL, *_methods;
  885. u8 method;
  886. size_t num_methods = 0, prefix_len;
  887. if (config == NULL || config->phase2 == NULL)
  888. goto get_defaults;
  889. start = buf = os_strdup(config->phase2);
  890. if (buf == NULL)
  891. return -1;
  892. prefix_len = os_strlen(prefix);
  893. while (start && *start != '\0') {
  894. int vendor;
  895. pos = os_strstr(start, prefix);
  896. if (pos == NULL)
  897. break;
  898. if (start != pos && *(pos - 1) != ' ') {
  899. start = pos + prefix_len;
  900. continue;
  901. }
  902. start = pos + prefix_len;
  903. pos = os_strchr(start, ' ');
  904. if (pos)
  905. *pos++ = '\0';
  906. method = eap_get_phase2_type(start, &vendor);
  907. if (vendor == EAP_VENDOR_IETF && method == EAP_TYPE_NONE) {
  908. wpa_printf(MSG_ERROR, "TLS: Unsupported Phase2 EAP "
  909. "method '%s'", start);
  910. } else {
  911. num_methods++;
  912. _methods = os_realloc_array(methods, num_methods,
  913. sizeof(*methods));
  914. if (_methods == NULL) {
  915. os_free(methods);
  916. os_free(buf);
  917. return -1;
  918. }
  919. methods = _methods;
  920. methods[num_methods - 1].vendor = vendor;
  921. methods[num_methods - 1].method = method;
  922. }
  923. start = pos;
  924. }
  925. os_free(buf);
  926. get_defaults:
  927. if (methods == NULL)
  928. methods = eap_get_phase2_types(config, &num_methods);
  929. if (methods == NULL) {
  930. wpa_printf(MSG_ERROR, "TLS: No Phase2 EAP methods available");
  931. return -1;
  932. }
  933. wpa_hexdump(MSG_DEBUG, "TLS: Phase2 EAP types",
  934. (u8 *) methods,
  935. num_methods * sizeof(struct eap_method_type));
  936. *types = methods;
  937. *num_types = num_methods;
  938. return 0;
  939. }
  940. /**
  941. * eap_peer_tls_phase2_nak - Generate EAP-Nak for Phase 2
  942. * @types: Buffer for returning allocated list of allowed EAP methods
  943. * @num_types: Buffer for returning number of allocated EAP methods
  944. * @hdr: EAP-Request header (and the following EAP type octet)
  945. * @resp: Buffer for returning the EAP-Nak message
  946. * Returns: 0 on success, -1 on failure
  947. */
  948. int eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types,
  949. struct eap_hdr *hdr, struct wpabuf **resp)
  950. {
  951. u8 *pos = (u8 *) (hdr + 1);
  952. size_t i;
  953. /* TODO: add support for expanded Nak */
  954. wpa_printf(MSG_DEBUG, "TLS: Phase 2 Request: Nak type=%d", *pos);
  955. wpa_hexdump(MSG_DEBUG, "TLS: Allowed Phase2 EAP types",
  956. (u8 *) types, num_types * sizeof(struct eap_method_type));
  957. *resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK, num_types,
  958. EAP_CODE_RESPONSE, hdr->identifier);
  959. if (*resp == NULL)
  960. return -1;
  961. for (i = 0; i < num_types; i++) {
  962. if (types[i].vendor == EAP_VENDOR_IETF &&
  963. types[i].method < 256)
  964. wpabuf_put_u8(*resp, types[i].method);
  965. }
  966. eap_update_len(*resp);
  967. return 0;
  968. }