eap_tls_common.c 33 KB

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