tls_gnutls.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /*
  2. * SSL/TLS interface functions for GnuTLS
  3. * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include <gnutls/gnutls.h>
  16. #include <gnutls/x509.h>
  17. #ifdef PKCS12_FUNCS
  18. #include <gnutls/pkcs12.h>
  19. #endif /* PKCS12_FUNCS */
  20. #ifdef CONFIG_GNUTLS_EXTRA
  21. #if LIBGNUTLS_VERSION_NUMBER >= 0x010302
  22. #define GNUTLS_IA
  23. #include <gnutls/extra.h>
  24. #if LIBGNUTLS_VERSION_NUMBER == 0x010302
  25. /* This function is not included in the current gnutls/extra.h even though it
  26. * should be, so define it here as a workaround for the time being. */
  27. int gnutls_ia_verify_endphase(gnutls_session_t session, char *checksum);
  28. #endif /* LIBGNUTLS_VERSION_NUMBER == 0x010302 */
  29. #endif /* LIBGNUTLS_VERSION_NUMBER >= 0x010302 */
  30. #endif /* CONFIG_GNUTLS_EXTRA */
  31. #include "common.h"
  32. #include "tls.h"
  33. #ifndef TLS_RANDOM_SIZE
  34. #define TLS_RANDOM_SIZE 32
  35. #endif
  36. #ifndef TLS_MASTER_SIZE
  37. #define TLS_MASTER_SIZE 48
  38. #endif
  39. #if LIBGNUTLS_VERSION_NUMBER < 0x010302
  40. /* GnuTLS 1.3.2 added functions for using master secret. Older versions require
  41. * use of internal structures to get the master_secret and
  42. * {server,client}_random.
  43. */
  44. #define GNUTLS_INTERNAL_STRUCTURE_HACK
  45. #endif /* LIBGNUTLS_VERSION_NUMBER < 0x010302 */
  46. #ifdef GNUTLS_INTERNAL_STRUCTURE_HACK
  47. /*
  48. * It looks like gnutls does not provide access to client/server_random and
  49. * master_key. This is somewhat unfortunate since these are needed for key
  50. * derivation in EAP-{TLS,TTLS,PEAP,FAST}. Workaround for now is a horrible
  51. * hack that copies the gnutls_session_int definition from gnutls_int.h so that
  52. * we can get the needed information.
  53. */
  54. typedef u8 uint8;
  55. typedef unsigned char opaque;
  56. typedef struct {
  57. uint8 suite[2];
  58. } cipher_suite_st;
  59. typedef struct {
  60. gnutls_connection_end_t entity;
  61. gnutls_kx_algorithm_t kx_algorithm;
  62. gnutls_cipher_algorithm_t read_bulk_cipher_algorithm;
  63. gnutls_mac_algorithm_t read_mac_algorithm;
  64. gnutls_compression_method_t read_compression_algorithm;
  65. gnutls_cipher_algorithm_t write_bulk_cipher_algorithm;
  66. gnutls_mac_algorithm_t write_mac_algorithm;
  67. gnutls_compression_method_t write_compression_algorithm;
  68. cipher_suite_st current_cipher_suite;
  69. opaque master_secret[TLS_MASTER_SIZE];
  70. opaque client_random[TLS_RANDOM_SIZE];
  71. opaque server_random[TLS_RANDOM_SIZE];
  72. /* followed by stuff we are not interested in */
  73. } security_parameters_st;
  74. struct gnutls_session_int {
  75. security_parameters_st security_parameters;
  76. /* followed by things we are not interested in */
  77. };
  78. #endif /* LIBGNUTLS_VERSION_NUMBER < 0x010302 */
  79. static int tls_gnutls_ref_count = 0;
  80. struct tls_global {
  81. /* Data for session resumption */
  82. void *session_data;
  83. size_t session_data_size;
  84. int server;
  85. int params_set;
  86. gnutls_certificate_credentials_t xcred;
  87. };
  88. struct tls_connection {
  89. gnutls_session session;
  90. char *subject_match, *altsubject_match;
  91. int read_alerts, write_alerts, failed;
  92. u8 *pre_shared_secret;
  93. size_t pre_shared_secret_len;
  94. int established;
  95. int verify_peer;
  96. struct wpabuf *push_buf;
  97. struct wpabuf *pull_buf;
  98. const u8 *pull_buf_offset;
  99. int params_set;
  100. gnutls_certificate_credentials_t xcred;
  101. int tls_ia;
  102. int final_phase_finished;
  103. #ifdef GNUTLS_IA
  104. gnutls_ia_server_credentials_t iacred_srv;
  105. gnutls_ia_client_credentials_t iacred_cli;
  106. /* Session keys generated in the current phase for inner secret
  107. * permutation before generating/verifying PhaseFinished. */
  108. u8 *session_keys;
  109. size_t session_keys_len;
  110. u8 inner_secret[TLS_MASTER_SIZE];
  111. #endif /* GNUTLS_IA */
  112. };
  113. static void tls_log_func(int level, const char *msg)
  114. {
  115. char *s, *pos;
  116. if (level == 6 || level == 7) {
  117. /* These levels seem to be mostly I/O debug and msg dumps */
  118. return;
  119. }
  120. s = os_strdup(msg);
  121. if (s == NULL)
  122. return;
  123. pos = s;
  124. while (*pos != '\0') {
  125. if (*pos == '\n') {
  126. *pos = '\0';
  127. break;
  128. }
  129. pos++;
  130. }
  131. wpa_printf(level > 3 ? MSG_MSGDUMP : MSG_DEBUG,
  132. "gnutls<%d> %s", level, s);
  133. os_free(s);
  134. }
  135. extern int wpa_debug_show_keys;
  136. void * tls_init(const struct tls_config *conf)
  137. {
  138. struct tls_global *global;
  139. #ifdef GNUTLS_INTERNAL_STRUCTURE_HACK
  140. /* Because of the horrible hack to get master_secret and client/server
  141. * random, we need to make sure that the gnutls version is something
  142. * that is expected to have same structure definition for the session
  143. * data.. */
  144. const char *ver;
  145. const char *ok_ver[] = { "1.2.3", "1.2.4", "1.2.5", "1.2.6", "1.2.9",
  146. "1.3.2",
  147. NULL };
  148. int i;
  149. #endif /* GNUTLS_INTERNAL_STRUCTURE_HACK */
  150. global = os_zalloc(sizeof(*global));
  151. if (global == NULL)
  152. return NULL;
  153. if (tls_gnutls_ref_count == 0 && gnutls_global_init() < 0) {
  154. os_free(global);
  155. return NULL;
  156. }
  157. tls_gnutls_ref_count++;
  158. #ifdef GNUTLS_INTERNAL_STRUCTURE_HACK
  159. ver = gnutls_check_version(NULL);
  160. if (ver == NULL) {
  161. tls_deinit(global);
  162. return NULL;
  163. }
  164. wpa_printf(MSG_DEBUG, "%s - gnutls version %s", __func__, ver);
  165. for (i = 0; ok_ver[i]; i++) {
  166. if (strcmp(ok_ver[i], ver) == 0)
  167. break;
  168. }
  169. if (ok_ver[i] == NULL) {
  170. wpa_printf(MSG_INFO, "Untested gnutls version %s - this needs "
  171. "to be tested and enabled in tls_gnutls.c", ver);
  172. tls_deinit(global);
  173. return NULL;
  174. }
  175. #endif /* GNUTLS_INTERNAL_STRUCTURE_HACK */
  176. gnutls_global_set_log_function(tls_log_func);
  177. if (wpa_debug_show_keys)
  178. gnutls_global_set_log_level(11);
  179. return global;
  180. }
  181. void tls_deinit(void *ssl_ctx)
  182. {
  183. struct tls_global *global = ssl_ctx;
  184. if (global) {
  185. if (global->params_set)
  186. gnutls_certificate_free_credentials(global->xcred);
  187. os_free(global->session_data);
  188. os_free(global);
  189. }
  190. tls_gnutls_ref_count--;
  191. if (tls_gnutls_ref_count == 0)
  192. gnutls_global_deinit();
  193. }
  194. int tls_get_errors(void *ssl_ctx)
  195. {
  196. return 0;
  197. }
  198. static ssize_t tls_pull_func(gnutls_transport_ptr ptr, void *buf,
  199. size_t len)
  200. {
  201. struct tls_connection *conn = (struct tls_connection *) ptr;
  202. const u8 *end;
  203. if (conn->pull_buf == NULL) {
  204. errno = EWOULDBLOCK;
  205. return -1;
  206. }
  207. end = wpabuf_head_u8(conn->pull_buf) + wpabuf_len(conn->pull_buf);
  208. if ((size_t) (end - conn->pull_buf_offset) < len)
  209. len = end - conn->pull_buf_offset;
  210. os_memcpy(buf, conn->pull_buf_offset, len);
  211. conn->pull_buf_offset += len;
  212. if (conn->pull_buf_offset == end) {
  213. wpa_printf(MSG_DEBUG, "%s - pull_buf consumed", __func__);
  214. wpabuf_free(conn->pull_buf);
  215. conn->pull_buf = NULL;
  216. conn->pull_buf_offset = NULL;
  217. } else {
  218. wpa_printf(MSG_DEBUG, "%s - %lu bytes remaining in pull_buf",
  219. __func__,
  220. (unsigned long) (end - conn->pull_buf_offset));
  221. }
  222. return len;
  223. }
  224. static ssize_t tls_push_func(gnutls_transport_ptr ptr, const void *buf,
  225. size_t len)
  226. {
  227. struct tls_connection *conn = (struct tls_connection *) ptr;
  228. if (wpabuf_resize(&conn->push_buf, len) < 0) {
  229. errno = ENOMEM;
  230. return -1;
  231. }
  232. wpabuf_put_data(conn->push_buf, buf, len);
  233. return len;
  234. }
  235. static int tls_gnutls_init_session(struct tls_global *global,
  236. struct tls_connection *conn)
  237. {
  238. const int cert_types[2] = { GNUTLS_CRT_X509, 0 };
  239. const int protos[2] = { GNUTLS_TLS1, 0 };
  240. int ret;
  241. ret = gnutls_init(&conn->session,
  242. global->server ? GNUTLS_SERVER : GNUTLS_CLIENT);
  243. if (ret < 0) {
  244. wpa_printf(MSG_INFO, "TLS: Failed to initialize new TLS "
  245. "connection: %s", gnutls_strerror(ret));
  246. return -1;
  247. }
  248. ret = gnutls_set_default_priority(conn->session);
  249. if (ret < 0)
  250. goto fail;
  251. ret = gnutls_certificate_type_set_priority(conn->session, cert_types);
  252. if (ret < 0)
  253. goto fail;
  254. ret = gnutls_protocol_set_priority(conn->session, protos);
  255. if (ret < 0)
  256. goto fail;
  257. gnutls_transport_set_pull_function(conn->session, tls_pull_func);
  258. gnutls_transport_set_push_function(conn->session, tls_push_func);
  259. gnutls_transport_set_ptr(conn->session, (gnutls_transport_ptr) conn);
  260. return 0;
  261. fail:
  262. wpa_printf(MSG_INFO, "TLS: Failed to setup new TLS connection: %s",
  263. gnutls_strerror(ret));
  264. gnutls_deinit(conn->session);
  265. return -1;
  266. }
  267. struct tls_connection * tls_connection_init(void *ssl_ctx)
  268. {
  269. struct tls_global *global = ssl_ctx;
  270. struct tls_connection *conn;
  271. int ret;
  272. conn = os_zalloc(sizeof(*conn));
  273. if (conn == NULL)
  274. return NULL;
  275. if (tls_gnutls_init_session(global, conn)) {
  276. os_free(conn);
  277. return NULL;
  278. }
  279. if (global->params_set) {
  280. ret = gnutls_credentials_set(conn->session,
  281. GNUTLS_CRD_CERTIFICATE,
  282. global->xcred);
  283. if (ret < 0) {
  284. wpa_printf(MSG_INFO, "Failed to configure "
  285. "credentials: %s", gnutls_strerror(ret));
  286. os_free(conn);
  287. return NULL;
  288. }
  289. }
  290. if (gnutls_certificate_allocate_credentials(&conn->xcred)) {
  291. os_free(conn);
  292. return NULL;
  293. }
  294. return conn;
  295. }
  296. void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
  297. {
  298. if (conn == NULL)
  299. return;
  300. #ifdef GNUTLS_IA
  301. if (conn->iacred_srv)
  302. gnutls_ia_free_server_credentials(conn->iacred_srv);
  303. if (conn->iacred_cli)
  304. gnutls_ia_free_client_credentials(conn->iacred_cli);
  305. if (conn->session_keys) {
  306. os_memset(conn->session_keys, 0, conn->session_keys_len);
  307. os_free(conn->session_keys);
  308. }
  309. #endif /* GNUTLS_IA */
  310. gnutls_certificate_free_credentials(conn->xcred);
  311. gnutls_deinit(conn->session);
  312. os_free(conn->pre_shared_secret);
  313. os_free(conn->subject_match);
  314. os_free(conn->altsubject_match);
  315. wpabuf_free(conn->push_buf);
  316. wpabuf_free(conn->pull_buf);
  317. os_free(conn);
  318. }
  319. int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
  320. {
  321. return conn ? conn->established : 0;
  322. }
  323. int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
  324. {
  325. struct tls_global *global = ssl_ctx;
  326. int ret;
  327. if (conn == NULL)
  328. return -1;
  329. /* Shutdown previous TLS connection without notifying the peer
  330. * because the connection was already terminated in practice
  331. * and "close notify" shutdown alert would confuse AS. */
  332. gnutls_bye(conn->session, GNUTLS_SHUT_RDWR);
  333. wpabuf_free(conn->push_buf);
  334. conn->push_buf = NULL;
  335. conn->established = 0;
  336. conn->final_phase_finished = 0;
  337. #ifdef GNUTLS_IA
  338. if (conn->session_keys) {
  339. os_memset(conn->session_keys, 0, conn->session_keys_len);
  340. os_free(conn->session_keys);
  341. }
  342. conn->session_keys_len = 0;
  343. #endif /* GNUTLS_IA */
  344. gnutls_deinit(conn->session);
  345. if (tls_gnutls_init_session(global, conn)) {
  346. wpa_printf(MSG_INFO, "GnuTLS: Failed to preparare new session "
  347. "for session resumption use");
  348. return -1;
  349. }
  350. ret = gnutls_credentials_set(conn->session, GNUTLS_CRD_CERTIFICATE,
  351. conn->params_set ? conn->xcred :
  352. global->xcred);
  353. if (ret < 0) {
  354. wpa_printf(MSG_INFO, "GnuTLS: Failed to configure credentials "
  355. "for session resumption: %s", gnutls_strerror(ret));
  356. return -1;
  357. }
  358. if (global->session_data) {
  359. ret = gnutls_session_set_data(conn->session,
  360. global->session_data,
  361. global->session_data_size);
  362. if (ret < 0) {
  363. wpa_printf(MSG_INFO, "GnuTLS: Failed to set session "
  364. "data: %s", gnutls_strerror(ret));
  365. return -1;
  366. }
  367. }
  368. return 0;
  369. }
  370. #if 0
  371. static int tls_match_altsubject(X509 *cert, const char *match)
  372. {
  373. GENERAL_NAME *gen;
  374. char *field, *tmp;
  375. void *ext;
  376. int i, found = 0;
  377. size_t len;
  378. ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
  379. for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
  380. gen = sk_GENERAL_NAME_value(ext, i);
  381. switch (gen->type) {
  382. case GEN_EMAIL:
  383. field = "EMAIL";
  384. break;
  385. case GEN_DNS:
  386. field = "DNS";
  387. break;
  388. case GEN_URI:
  389. field = "URI";
  390. break;
  391. default:
  392. field = NULL;
  393. wpa_printf(MSG_DEBUG, "TLS: altSubjectName: "
  394. "unsupported type=%d", gen->type);
  395. break;
  396. }
  397. if (!field)
  398. continue;
  399. wpa_printf(MSG_DEBUG, "TLS: altSubjectName: %s:%s",
  400. field, gen->d.ia5->data);
  401. len = os_strlen(field) + 1 +
  402. strlen((char *) gen->d.ia5->data) + 1;
  403. tmp = os_malloc(len);
  404. if (tmp == NULL)
  405. continue;
  406. snprintf(tmp, len, "%s:%s", field, gen->d.ia5->data);
  407. if (strstr(tmp, match))
  408. found++;
  409. os_free(tmp);
  410. }
  411. return found;
  412. }
  413. #endif
  414. #if 0
  415. static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
  416. {
  417. char buf[256];
  418. X509 *err_cert;
  419. int err, depth;
  420. SSL *ssl;
  421. struct tls_connection *conn;
  422. char *match, *altmatch;
  423. err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
  424. err = X509_STORE_CTX_get_error(x509_ctx);
  425. depth = X509_STORE_CTX_get_error_depth(x509_ctx);
  426. ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
  427. SSL_get_ex_data_X509_STORE_CTX_idx());
  428. X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
  429. conn = SSL_get_app_data(ssl);
  430. match = conn ? conn->subject_match : NULL;
  431. altmatch = conn ? conn->altsubject_match : NULL;
  432. if (!preverify_ok) {
  433. wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
  434. " error %d (%s) depth %d for '%s'", err,
  435. X509_verify_cert_error_string(err), depth, buf);
  436. } else {
  437. wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - "
  438. "preverify_ok=%d err=%d (%s) depth=%d buf='%s'",
  439. preverify_ok, err,
  440. X509_verify_cert_error_string(err), depth, buf);
  441. if (depth == 0 && match && strstr(buf, match) == NULL) {
  442. wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
  443. "match with '%s'", buf, match);
  444. preverify_ok = 0;
  445. } else if (depth == 0 && altmatch &&
  446. !tls_match_altsubject(err_cert, altmatch)) {
  447. wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
  448. "'%s' not found", altmatch);
  449. preverify_ok = 0;
  450. }
  451. }
  452. return preverify_ok;
  453. }
  454. #endif
  455. int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
  456. const struct tls_connection_params *params)
  457. {
  458. int ret;
  459. if (conn == NULL || params == NULL)
  460. return -1;
  461. os_free(conn->subject_match);
  462. conn->subject_match = NULL;
  463. if (params->subject_match) {
  464. conn->subject_match = os_strdup(params->subject_match);
  465. if (conn->subject_match == NULL)
  466. return -1;
  467. }
  468. os_free(conn->altsubject_match);
  469. conn->altsubject_match = NULL;
  470. if (params->altsubject_match) {
  471. conn->altsubject_match = os_strdup(params->altsubject_match);
  472. if (conn->altsubject_match == NULL)
  473. return -1;
  474. }
  475. /* TODO: gnutls_certificate_set_verify_flags(xcred, flags);
  476. * to force peer validation(?) */
  477. if (params->ca_cert) {
  478. conn->verify_peer = 1;
  479. ret = gnutls_certificate_set_x509_trust_file(
  480. conn->xcred, params->ca_cert, GNUTLS_X509_FMT_PEM);
  481. if (ret < 0) {
  482. wpa_printf(MSG_DEBUG, "Failed to read CA cert '%s' "
  483. "in PEM format: %s", params->ca_cert,
  484. gnutls_strerror(ret));
  485. ret = gnutls_certificate_set_x509_trust_file(
  486. conn->xcred, params->ca_cert,
  487. GNUTLS_X509_FMT_DER);
  488. if (ret < 0) {
  489. wpa_printf(MSG_DEBUG, "Failed to read CA cert "
  490. "'%s' in DER format: %s",
  491. params->ca_cert,
  492. gnutls_strerror(ret));
  493. return -1;
  494. }
  495. }
  496. }
  497. if (params->client_cert && params->private_key) {
  498. /* TODO: private_key_passwd? */
  499. ret = gnutls_certificate_set_x509_key_file(
  500. conn->xcred, params->client_cert, params->private_key,
  501. GNUTLS_X509_FMT_PEM);
  502. if (ret < 0) {
  503. wpa_printf(MSG_DEBUG, "Failed to read client cert/key "
  504. "in PEM format: %s", gnutls_strerror(ret));
  505. ret = gnutls_certificate_set_x509_key_file(
  506. conn->xcred, params->client_cert,
  507. params->private_key, GNUTLS_X509_FMT_DER);
  508. if (ret < 0) {
  509. wpa_printf(MSG_DEBUG, "Failed to read client "
  510. "cert/key in DER format: %s",
  511. gnutls_strerror(ret));
  512. return ret;
  513. }
  514. }
  515. } else if (params->private_key) {
  516. int pkcs12_ok = 0;
  517. #ifdef PKCS12_FUNCS
  518. /* Try to load in PKCS#12 format */
  519. #if LIBGNUTLS_VERSION_NUMBER >= 0x010302
  520. ret = gnutls_certificate_set_x509_simple_pkcs12_file(
  521. conn->xcred, params->private_key, GNUTLS_X509_FMT_DER,
  522. params->private_key_passwd);
  523. if (ret != 0) {
  524. wpa_printf(MSG_DEBUG, "Failed to load private_key in "
  525. "PKCS#12 format: %s", gnutls_strerror(ret));
  526. return -1;
  527. } else
  528. pkcs12_ok = 1;
  529. #endif /* LIBGNUTLS_VERSION_NUMBER >= 0x010302 */
  530. #endif /* PKCS12_FUNCS */
  531. if (!pkcs12_ok) {
  532. wpa_printf(MSG_DEBUG, "GnuTLS: PKCS#12 support not "
  533. "included");
  534. return -1;
  535. }
  536. }
  537. conn->tls_ia = params->tls_ia;
  538. conn->params_set = 1;
  539. ret = gnutls_credentials_set(conn->session, GNUTLS_CRD_CERTIFICATE,
  540. conn->xcred);
  541. if (ret < 0) {
  542. wpa_printf(MSG_INFO, "Failed to configure credentials: %s",
  543. gnutls_strerror(ret));
  544. }
  545. #ifdef GNUTLS_IA
  546. if (conn->iacred_cli)
  547. gnutls_ia_free_client_credentials(conn->iacred_cli);
  548. ret = gnutls_ia_allocate_client_credentials(&conn->iacred_cli);
  549. if (ret) {
  550. wpa_printf(MSG_DEBUG, "Failed to allocate IA credentials: %s",
  551. gnutls_strerror(ret));
  552. return -1;
  553. }
  554. ret = gnutls_credentials_set(conn->session, GNUTLS_CRD_IA,
  555. conn->iacred_cli);
  556. if (ret) {
  557. wpa_printf(MSG_DEBUG, "Failed to configure IA credentials: %s",
  558. gnutls_strerror(ret));
  559. gnutls_ia_free_client_credentials(conn->iacred_cli);
  560. conn->iacred_cli = NULL;
  561. return -1;
  562. }
  563. #endif /* GNUTLS_IE */
  564. return ret;
  565. }
  566. int tls_global_set_params(void *tls_ctx,
  567. const struct tls_connection_params *params)
  568. {
  569. struct tls_global *global = tls_ctx;
  570. int ret;
  571. /* Currently, global parameters are only set when running in server
  572. * mode. */
  573. global->server = 1;
  574. if (global->params_set) {
  575. gnutls_certificate_free_credentials(global->xcred);
  576. global->params_set = 0;
  577. }
  578. ret = gnutls_certificate_allocate_credentials(&global->xcred);
  579. if (ret) {
  580. wpa_printf(MSG_DEBUG, "Failed to allocate global credentials "
  581. "%s", gnutls_strerror(ret));
  582. return -1;
  583. }
  584. if (params->ca_cert) {
  585. ret = gnutls_certificate_set_x509_trust_file(
  586. global->xcred, params->ca_cert, GNUTLS_X509_FMT_PEM);
  587. if (ret < 0) {
  588. wpa_printf(MSG_DEBUG, "Failed to read CA cert '%s' "
  589. "in PEM format: %s", params->ca_cert,
  590. gnutls_strerror(ret));
  591. ret = gnutls_certificate_set_x509_trust_file(
  592. global->xcred, params->ca_cert,
  593. GNUTLS_X509_FMT_DER);
  594. if (ret < 0) {
  595. wpa_printf(MSG_DEBUG, "Failed to read CA cert "
  596. "'%s' in DER format: %s",
  597. params->ca_cert,
  598. gnutls_strerror(ret));
  599. goto fail;
  600. }
  601. }
  602. }
  603. if (params->client_cert && params->private_key) {
  604. /* TODO: private_key_passwd? */
  605. ret = gnutls_certificate_set_x509_key_file(
  606. global->xcred, params->client_cert,
  607. params->private_key, GNUTLS_X509_FMT_PEM);
  608. if (ret < 0) {
  609. wpa_printf(MSG_DEBUG, "Failed to read client cert/key "
  610. "in PEM format: %s", gnutls_strerror(ret));
  611. ret = gnutls_certificate_set_x509_key_file(
  612. global->xcred, params->client_cert,
  613. params->private_key, GNUTLS_X509_FMT_DER);
  614. if (ret < 0) {
  615. wpa_printf(MSG_DEBUG, "Failed to read client "
  616. "cert/key in DER format: %s",
  617. gnutls_strerror(ret));
  618. goto fail;
  619. }
  620. }
  621. } else if (params->private_key) {
  622. int pkcs12_ok = 0;
  623. #ifdef PKCS12_FUNCS
  624. /* Try to load in PKCS#12 format */
  625. #if LIBGNUTLS_VERSION_NUMBER >= 0x010302
  626. ret = gnutls_certificate_set_x509_simple_pkcs12_file(
  627. global->xcred, params->private_key,
  628. GNUTLS_X509_FMT_DER, params->private_key_passwd);
  629. if (ret != 0) {
  630. wpa_printf(MSG_DEBUG, "Failed to load private_key in "
  631. "PKCS#12 format: %s", gnutls_strerror(ret));
  632. goto fail;
  633. } else
  634. pkcs12_ok = 1;
  635. #endif /* LIBGNUTLS_VERSION_NUMBER >= 0x010302 */
  636. #endif /* PKCS12_FUNCS */
  637. if (!pkcs12_ok) {
  638. wpa_printf(MSG_DEBUG, "GnuTLS: PKCS#12 support not "
  639. "included");
  640. goto fail;
  641. }
  642. }
  643. global->params_set = 1;
  644. return 0;
  645. fail:
  646. gnutls_certificate_free_credentials(global->xcred);
  647. return -1;
  648. }
  649. int tls_global_set_verify(void *ssl_ctx, int check_crl)
  650. {
  651. /* TODO */
  652. return 0;
  653. }
  654. int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
  655. int verify_peer)
  656. {
  657. if (conn == NULL || conn->session == NULL)
  658. return -1;
  659. conn->verify_peer = verify_peer;
  660. gnutls_certificate_server_set_request(conn->session,
  661. verify_peer ? GNUTLS_CERT_REQUIRE
  662. : GNUTLS_CERT_REQUEST);
  663. return 0;
  664. }
  665. int tls_connection_get_keys(void *ssl_ctx, struct tls_connection *conn,
  666. struct tls_keys *keys)
  667. {
  668. #ifdef GNUTLS_INTERNAL_STRUCTURE_HACK
  669. security_parameters_st *sec;
  670. #endif /* GNUTLS_INTERNAL_STRUCTURE_HACK */
  671. if (conn == NULL || conn->session == NULL || keys == NULL)
  672. return -1;
  673. os_memset(keys, 0, sizeof(*keys));
  674. #ifdef GNUTLS_INTERNAL_STRUCTURE_HACK
  675. sec = &conn->session->security_parameters;
  676. keys->master_key = sec->master_secret;
  677. keys->master_key_len = TLS_MASTER_SIZE;
  678. keys->client_random = sec->client_random;
  679. keys->server_random = sec->server_random;
  680. #else /* GNUTLS_INTERNAL_STRUCTURE_HACK */
  681. keys->client_random =
  682. (u8 *) gnutls_session_get_client_random(conn->session);
  683. keys->server_random =
  684. (u8 *) gnutls_session_get_server_random(conn->session);
  685. /* No access to master_secret */
  686. #endif /* GNUTLS_INTERNAL_STRUCTURE_HACK */
  687. #ifdef GNUTLS_IA
  688. gnutls_ia_extract_inner_secret(conn->session,
  689. (char *) conn->inner_secret);
  690. keys->inner_secret = conn->inner_secret;
  691. keys->inner_secret_len = TLS_MASTER_SIZE;
  692. #endif /* GNUTLS_IA */
  693. keys->client_random_len = TLS_RANDOM_SIZE;
  694. keys->server_random_len = TLS_RANDOM_SIZE;
  695. return 0;
  696. }
  697. int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
  698. const char *label, int server_random_first,
  699. u8 *out, size_t out_len)
  700. {
  701. #if LIBGNUTLS_VERSION_NUMBER >= 0x010302
  702. if (conn == NULL || conn->session == NULL)
  703. return -1;
  704. return gnutls_prf(conn->session, os_strlen(label), label,
  705. server_random_first, 0, NULL, out_len, (char *) out);
  706. #else /* LIBGNUTLS_VERSION_NUMBER >= 0x010302 */
  707. return -1;
  708. #endif /* LIBGNUTLS_VERSION_NUMBER >= 0x010302 */
  709. }
  710. static int tls_connection_verify_peer(struct tls_connection *conn,
  711. gnutls_alert_description_t *err)
  712. {
  713. unsigned int status, num_certs, i;
  714. struct os_time now;
  715. const gnutls_datum_t *certs;
  716. gnutls_x509_crt_t cert;
  717. if (gnutls_certificate_verify_peers2(conn->session, &status) < 0) {
  718. wpa_printf(MSG_INFO, "TLS: Failed to verify peer "
  719. "certificate chain");
  720. *err = GNUTLS_A_INTERNAL_ERROR;
  721. return -1;
  722. }
  723. if (conn->verify_peer && (status & GNUTLS_CERT_INVALID)) {
  724. wpa_printf(MSG_INFO, "TLS: Peer certificate not trusted");
  725. if (status & GNUTLS_CERT_INSECURE_ALGORITHM) {
  726. wpa_printf(MSG_INFO, "TLS: Certificate uses insecure "
  727. "algorithm");
  728. *err = GNUTLS_A_INSUFFICIENT_SECURITY;
  729. }
  730. if (status & GNUTLS_CERT_NOT_ACTIVATED) {
  731. wpa_printf(MSG_INFO, "TLS: Certificate not yet "
  732. "activated");
  733. *err = GNUTLS_A_CERTIFICATE_EXPIRED;
  734. }
  735. if (status & GNUTLS_CERT_EXPIRED) {
  736. wpa_printf(MSG_INFO, "TLS: Certificate expired");
  737. *err = GNUTLS_A_CERTIFICATE_EXPIRED;
  738. }
  739. return -1;
  740. }
  741. if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) {
  742. wpa_printf(MSG_INFO, "TLS: Peer certificate does not have a "
  743. "known issuer");
  744. *err = GNUTLS_A_UNKNOWN_CA;
  745. return -1;
  746. }
  747. if (status & GNUTLS_CERT_REVOKED) {
  748. wpa_printf(MSG_INFO, "TLS: Peer certificate has been revoked");
  749. *err = GNUTLS_A_CERTIFICATE_REVOKED;
  750. return -1;
  751. }
  752. os_get_time(&now);
  753. certs = gnutls_certificate_get_peers(conn->session, &num_certs);
  754. if (certs == NULL) {
  755. wpa_printf(MSG_INFO, "TLS: No peer certificate chain "
  756. "received");
  757. *err = GNUTLS_A_UNKNOWN_CA;
  758. return -1;
  759. }
  760. for (i = 0; i < num_certs; i++) {
  761. char *buf;
  762. size_t len;
  763. if (gnutls_x509_crt_init(&cert) < 0) {
  764. wpa_printf(MSG_INFO, "TLS: Certificate initialization "
  765. "failed");
  766. *err = GNUTLS_A_BAD_CERTIFICATE;
  767. return -1;
  768. }
  769. if (gnutls_x509_crt_import(cert, &certs[i],
  770. GNUTLS_X509_FMT_DER) < 0) {
  771. wpa_printf(MSG_INFO, "TLS: Could not parse peer "
  772. "certificate %d/%d", i + 1, num_certs);
  773. gnutls_x509_crt_deinit(cert);
  774. *err = GNUTLS_A_BAD_CERTIFICATE;
  775. return -1;
  776. }
  777. gnutls_x509_crt_get_dn(cert, NULL, &len);
  778. len++;
  779. buf = os_malloc(len + 1);
  780. if (buf) {
  781. buf[0] = buf[len] = '\0';
  782. gnutls_x509_crt_get_dn(cert, buf, &len);
  783. }
  784. wpa_printf(MSG_DEBUG, "TLS: Peer cert chain %d/%d: %s",
  785. i + 1, num_certs, buf);
  786. if (i == 0) {
  787. /* TODO: validate subject_match and altsubject_match */
  788. }
  789. os_free(buf);
  790. if (gnutls_x509_crt_get_expiration_time(cert) < now.sec ||
  791. gnutls_x509_crt_get_activation_time(cert) > now.sec) {
  792. wpa_printf(MSG_INFO, "TLS: Peer certificate %d/%d is "
  793. "not valid at this time",
  794. i + 1, num_certs);
  795. gnutls_x509_crt_deinit(cert);
  796. *err = GNUTLS_A_CERTIFICATE_EXPIRED;
  797. return -1;
  798. }
  799. gnutls_x509_crt_deinit(cert);
  800. }
  801. return 0;
  802. }
  803. struct wpabuf * tls_connection_handshake(void *tls_ctx,
  804. struct tls_connection *conn,
  805. const struct wpabuf *in_data,
  806. struct wpabuf **appl_data)
  807. {
  808. struct tls_global *global = tls_ctx;
  809. struct wpabuf *out_data;
  810. int ret;
  811. if (appl_data)
  812. *appl_data = NULL;
  813. if (in_data && wpabuf_len(in_data) > 0) {
  814. if (conn->pull_buf) {
  815. wpa_printf(MSG_DEBUG, "%s - %lu bytes remaining in "
  816. "pull_buf", __func__,
  817. (unsigned long) wpabuf_len(conn->pull_buf));
  818. wpabuf_free(conn->pull_buf);
  819. }
  820. conn->pull_buf = wpabuf_dup(in_data);
  821. if (conn->pull_buf == NULL)
  822. return NULL;
  823. conn->pull_buf_offset = wpabuf_head(conn->pull_buf);
  824. }
  825. ret = gnutls_handshake(conn->session);
  826. if (ret < 0) {
  827. switch (ret) {
  828. case GNUTLS_E_AGAIN:
  829. if (global->server && conn->established &&
  830. conn->push_buf == NULL) {
  831. /* Need to return something to trigger
  832. * completion of EAP-TLS. */
  833. conn->push_buf = wpabuf_alloc(0);
  834. }
  835. break;
  836. case GNUTLS_E_FATAL_ALERT_RECEIVED:
  837. wpa_printf(MSG_DEBUG, "%s - received fatal '%s' alert",
  838. __func__, gnutls_alert_get_name(
  839. gnutls_alert_get(conn->session)));
  840. conn->read_alerts++;
  841. /* continue */
  842. default:
  843. wpa_printf(MSG_DEBUG, "%s - gnutls_handshake failed "
  844. "-> %s", __func__, gnutls_strerror(ret));
  845. conn->failed++;
  846. }
  847. } else {
  848. size_t size;
  849. gnutls_alert_description_t err;
  850. if (conn->verify_peer &&
  851. tls_connection_verify_peer(conn, &err)) {
  852. wpa_printf(MSG_INFO, "TLS: Peer certificate chain "
  853. "failed validation");
  854. conn->failed++;
  855. gnutls_alert_send(conn->session, GNUTLS_AL_FATAL, err);
  856. goto out;
  857. }
  858. #ifdef CONFIG_GNUTLS_EXTRA
  859. if (conn->tls_ia && !gnutls_ia_handshake_p(conn->session)) {
  860. wpa_printf(MSG_INFO, "TLS: No TLS/IA negotiation");
  861. conn->failed++;
  862. return NULL;
  863. }
  864. #endif /* CONFIG_GNUTLS_EXTRA */
  865. if (conn->tls_ia)
  866. wpa_printf(MSG_DEBUG, "TLS: Start TLS/IA handshake");
  867. else {
  868. wpa_printf(MSG_DEBUG, "TLS: Handshake completed "
  869. "successfully");
  870. }
  871. conn->established = 1;
  872. if (conn->push_buf == NULL) {
  873. /* Need to return something to get final TLS ACK. */
  874. conn->push_buf = wpabuf_alloc(0);
  875. }
  876. gnutls_session_get_data(conn->session, NULL, &size);
  877. if (global->session_data == NULL ||
  878. global->session_data_size < size) {
  879. os_free(global->session_data);
  880. global->session_data = os_malloc(size);
  881. }
  882. if (global->session_data) {
  883. global->session_data_size = size;
  884. gnutls_session_get_data(conn->session,
  885. global->session_data,
  886. &global->session_data_size);
  887. }
  888. }
  889. out:
  890. out_data = conn->push_buf;
  891. conn->push_buf = NULL;
  892. return out_data;
  893. }
  894. struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
  895. struct tls_connection *conn,
  896. const struct wpabuf *in_data,
  897. struct wpabuf **appl_data)
  898. {
  899. return tls_connection_handshake(tls_ctx, conn, in_data, appl_data);
  900. }
  901. struct wpabuf * tls_connection_encrypt(void *tls_ctx,
  902. struct tls_connection *conn,
  903. const struct wpabuf *in_data)
  904. {
  905. ssize_t res;
  906. struct wpabuf *buf;
  907. #ifdef GNUTLS_IA
  908. if (conn->tls_ia)
  909. res = gnutls_ia_send(conn->session, wpabuf_head(in_data),
  910. wpabuf_len(in_data));
  911. else
  912. #endif /* GNUTLS_IA */
  913. res = gnutls_record_send(conn->session, wpabuf_head(in_data),
  914. wpabuf_len(in_data));
  915. if (res < 0) {
  916. wpa_printf(MSG_INFO, "%s: Encryption failed: %s",
  917. __func__, gnutls_strerror(res));
  918. return NULL;
  919. }
  920. buf = conn->push_buf;
  921. conn->push_buf = NULL;
  922. return buf;
  923. }
  924. struct wpabuf * tls_connection_decrypt(void *tls_ctx,
  925. struct tls_connection *conn,
  926. const struct wpabuf *in_data)
  927. {
  928. ssize_t res;
  929. struct wpabuf *out;
  930. if (conn->pull_buf) {
  931. wpa_printf(MSG_DEBUG, "%s - %lu bytes remaining in "
  932. "pull_buf", __func__,
  933. (unsigned long) wpabuf_len(conn->pull_buf));
  934. wpabuf_free(conn->pull_buf);
  935. }
  936. conn->pull_buf = wpabuf_dup(in_data);
  937. if (conn->pull_buf == NULL)
  938. return NULL;
  939. conn->pull_buf_offset = wpabuf_head(conn->pull_buf);
  940. /*
  941. * Even though we try to disable TLS compression, it is possible that
  942. * this cannot be done with all TLS libraries. Add extra buffer space
  943. * to handle the possibility of the decrypted data being longer than
  944. * input data.
  945. */
  946. out = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
  947. if (out == NULL)
  948. return NULL;
  949. #ifdef GNUTLS_IA
  950. if (conn->tls_ia) {
  951. res = gnutls_ia_recv(conn->session, wpabuf_mhead(out),
  952. wpabuf_size(out));
  953. if (res == GNUTLS_E_WARNING_IA_IPHF_RECEIVED ||
  954. res == GNUTLS_E_WARNING_IA_FPHF_RECEIVED) {
  955. int final = res == GNUTLS_E_WARNING_IA_FPHF_RECEIVED;
  956. wpa_printf(MSG_DEBUG, "%s: Received %sPhaseFinished",
  957. __func__, final ? "Final" : "Intermediate");
  958. res = gnutls_ia_permute_inner_secret(
  959. conn->session, conn->session_keys_len,
  960. (char *) conn->session_keys);
  961. if (conn->session_keys) {
  962. os_memset(conn->session_keys, 0,
  963. conn->session_keys_len);
  964. os_free(conn->session_keys);
  965. }
  966. conn->session_keys = NULL;
  967. conn->session_keys_len = 0;
  968. if (res) {
  969. wpa_printf(MSG_DEBUG, "%s: Failed to permute "
  970. "inner secret: %s",
  971. __func__, gnutls_strerror(res));
  972. wpabuf_free(out);
  973. return NULL;
  974. }
  975. res = gnutls_ia_verify_endphase(conn->session,
  976. wpabuf_head(out));
  977. if (res == 0) {
  978. wpa_printf(MSG_DEBUG, "%s: Correct endphase "
  979. "checksum", __func__);
  980. } else {
  981. wpa_printf(MSG_INFO, "%s: Endphase "
  982. "verification failed: %s",
  983. __func__, gnutls_strerror(res));
  984. wpabuf_free(out);
  985. return NULL;
  986. }
  987. if (final)
  988. conn->final_phase_finished = 1;
  989. return out;
  990. }
  991. if (res < 0) {
  992. wpa_printf(MSG_DEBUG, "%s - gnutls_ia_recv failed: %d "
  993. "(%s)", __func__, (int) res,
  994. gnutls_strerror(res));
  995. wpabuf_free(out);
  996. return NULL;
  997. }
  998. wpabuf_put(out, res);
  999. return out;
  1000. }
  1001. #endif /* GNUTLS_IA */
  1002. res = gnutls_record_recv(conn->session, wpabuf_mhead(out),
  1003. wpabuf_size(out));
  1004. if (res < 0) {
  1005. wpa_printf(MSG_DEBUG, "%s - gnutls_record_recv failed: %d "
  1006. "(%s)", __func__, (int) res, gnutls_strerror(res));
  1007. wpabuf_free(out);
  1008. return NULL;
  1009. }
  1010. wpabuf_put(out, res);
  1011. return out;
  1012. }
  1013. int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
  1014. {
  1015. if (conn == NULL)
  1016. return 0;
  1017. return gnutls_session_is_resumed(conn->session);
  1018. }
  1019. int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
  1020. u8 *ciphers)
  1021. {
  1022. /* TODO */
  1023. return -1;
  1024. }
  1025. int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
  1026. char *buf, size_t buflen)
  1027. {
  1028. /* TODO */
  1029. buf[0] = '\0';
  1030. return 0;
  1031. }
  1032. int tls_connection_enable_workaround(void *ssl_ctx,
  1033. struct tls_connection *conn)
  1034. {
  1035. /* TODO: set SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS */
  1036. return 0;
  1037. }
  1038. int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
  1039. int ext_type, const u8 *data,
  1040. size_t data_len)
  1041. {
  1042. /* TODO */
  1043. return -1;
  1044. }
  1045. int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
  1046. {
  1047. if (conn == NULL)
  1048. return -1;
  1049. return conn->failed;
  1050. }
  1051. int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
  1052. {
  1053. if (conn == NULL)
  1054. return -1;
  1055. return conn->read_alerts;
  1056. }
  1057. int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
  1058. {
  1059. if (conn == NULL)
  1060. return -1;
  1061. return conn->write_alerts;
  1062. }
  1063. int tls_connection_get_keyblock_size(void *tls_ctx,
  1064. struct tls_connection *conn)
  1065. {
  1066. /* TODO */
  1067. return -1;
  1068. }
  1069. unsigned int tls_capabilities(void *tls_ctx)
  1070. {
  1071. unsigned int capa = 0;
  1072. #ifdef GNUTLS_IA
  1073. capa |= TLS_CAPABILITY_IA;
  1074. #endif /* GNUTLS_IA */
  1075. return capa;
  1076. }
  1077. int tls_connection_set_ia(void *tls_ctx, struct tls_connection *conn,
  1078. int tls_ia)
  1079. {
  1080. #ifdef GNUTLS_IA
  1081. int ret;
  1082. if (conn == NULL)
  1083. return -1;
  1084. conn->tls_ia = tls_ia;
  1085. if (!tls_ia)
  1086. return 0;
  1087. ret = gnutls_ia_allocate_server_credentials(&conn->iacred_srv);
  1088. if (ret) {
  1089. wpa_printf(MSG_DEBUG, "Failed to allocate IA credentials: %s",
  1090. gnutls_strerror(ret));
  1091. return -1;
  1092. }
  1093. ret = gnutls_credentials_set(conn->session, GNUTLS_CRD_IA,
  1094. conn->iacred_srv);
  1095. if (ret) {
  1096. wpa_printf(MSG_DEBUG, "Failed to configure IA credentials: %s",
  1097. gnutls_strerror(ret));
  1098. gnutls_ia_free_server_credentials(conn->iacred_srv);
  1099. conn->iacred_srv = NULL;
  1100. return -1;
  1101. }
  1102. return 0;
  1103. #else /* GNUTLS_IA */
  1104. return -1;
  1105. #endif /* GNUTLS_IA */
  1106. }
  1107. int tls_connection_ia_send_phase_finished(void *tls_ctx,
  1108. struct tls_connection *conn,
  1109. int final,
  1110. u8 *out_data, size_t out_len)
  1111. {
  1112. #ifdef GNUTLS_IA
  1113. int ret;
  1114. if (conn == NULL || conn->session == NULL || !conn->tls_ia)
  1115. return -1;
  1116. ret = gnutls_ia_permute_inner_secret(conn->session,
  1117. conn->session_keys_len,
  1118. (char *) conn->session_keys);
  1119. if (conn->session_keys) {
  1120. os_memset(conn->session_keys, 0, conn->session_keys_len);
  1121. os_free(conn->session_keys);
  1122. }
  1123. conn->session_keys = NULL;
  1124. conn->session_keys_len = 0;
  1125. if (ret) {
  1126. wpa_printf(MSG_DEBUG, "%s: Failed to permute inner secret: %s",
  1127. __func__, gnutls_strerror(ret));
  1128. return -1;
  1129. }
  1130. ret = gnutls_ia_endphase_send(conn->session, final);
  1131. if (ret) {
  1132. wpa_printf(MSG_DEBUG, "%s: Failed to send endphase: %s",
  1133. __func__, gnutls_strerror(ret));
  1134. return -1;
  1135. }
  1136. if (conn->push_buf == NULL)
  1137. return -1;
  1138. if (wpabuf_len(conn->push_buf) < out_len)
  1139. out_len = wpabuf_len(conn->push_buf);
  1140. os_memcpy(out_data, wpabuf_head(conn->push_buf), out_len);
  1141. wpabuf_free(conn->push_buf);
  1142. conn->push_buf = NULL;
  1143. return out_len;
  1144. #else /* GNUTLS_IA */
  1145. return -1;
  1146. #endif /* GNUTLS_IA */
  1147. }
  1148. int tls_connection_ia_final_phase_finished(void *tls_ctx,
  1149. struct tls_connection *conn)
  1150. {
  1151. if (conn == NULL)
  1152. return -1;
  1153. return conn->final_phase_finished;
  1154. }
  1155. int tls_connection_ia_permute_inner_secret(void *tls_ctx,
  1156. struct tls_connection *conn,
  1157. const u8 *key, size_t key_len)
  1158. {
  1159. #ifdef GNUTLS_IA
  1160. if (conn == NULL || !conn->tls_ia)
  1161. return -1;
  1162. if (conn->session_keys) {
  1163. os_memset(conn->session_keys, 0, conn->session_keys_len);
  1164. os_free(conn->session_keys);
  1165. }
  1166. conn->session_keys_len = 0;
  1167. if (key) {
  1168. conn->session_keys = os_malloc(key_len);
  1169. if (conn->session_keys == NULL)
  1170. return -1;
  1171. os_memcpy(conn->session_keys, key, key_len);
  1172. conn->session_keys_len = key_len;
  1173. } else {
  1174. conn->session_keys = NULL;
  1175. conn->session_keys_len = 0;
  1176. }
  1177. return 0;
  1178. #else /* GNUTLS_IA */
  1179. return -1;
  1180. #endif /* GNUTLS_IA */
  1181. }