tls_gnutls.c 33 KB

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