tls_gnutls.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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. if (params->flags & TLS_CONN_ALLOW_SIGN_RSA_MD5) {
  497. gnutls_certificate_set_verify_flags(
  498. conn->xcred, GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5);
  499. }
  500. if (params->flags & TLS_CONN_DISABLE_TIME_CHECKS) {
  501. gnutls_certificate_set_verify_flags(
  502. conn->xcred,
  503. GNUTLS_VERIFY_DISABLE_TIME_CHECKS);
  504. }
  505. }
  506. if (params->client_cert && params->private_key) {
  507. /* TODO: private_key_passwd? */
  508. ret = gnutls_certificate_set_x509_key_file(
  509. conn->xcred, params->client_cert, params->private_key,
  510. GNUTLS_X509_FMT_PEM);
  511. if (ret < 0) {
  512. wpa_printf(MSG_DEBUG, "Failed to read client cert/key "
  513. "in PEM format: %s", gnutls_strerror(ret));
  514. ret = gnutls_certificate_set_x509_key_file(
  515. conn->xcred, params->client_cert,
  516. params->private_key, GNUTLS_X509_FMT_DER);
  517. if (ret < 0) {
  518. wpa_printf(MSG_DEBUG, "Failed to read client "
  519. "cert/key in DER format: %s",
  520. gnutls_strerror(ret));
  521. return ret;
  522. }
  523. }
  524. } else if (params->private_key) {
  525. int pkcs12_ok = 0;
  526. #ifdef PKCS12_FUNCS
  527. /* Try to load in PKCS#12 format */
  528. #if LIBGNUTLS_VERSION_NUMBER >= 0x010302
  529. ret = gnutls_certificate_set_x509_simple_pkcs12_file(
  530. conn->xcred, params->private_key, GNUTLS_X509_FMT_DER,
  531. params->private_key_passwd);
  532. if (ret != 0) {
  533. wpa_printf(MSG_DEBUG, "Failed to load private_key in "
  534. "PKCS#12 format: %s", gnutls_strerror(ret));
  535. return -1;
  536. } else
  537. pkcs12_ok = 1;
  538. #endif /* LIBGNUTLS_VERSION_NUMBER >= 0x010302 */
  539. #endif /* PKCS12_FUNCS */
  540. if (!pkcs12_ok) {
  541. wpa_printf(MSG_DEBUG, "GnuTLS: PKCS#12 support not "
  542. "included");
  543. return -1;
  544. }
  545. }
  546. conn->tls_ia = params->tls_ia;
  547. conn->params_set = 1;
  548. ret = gnutls_credentials_set(conn->session, GNUTLS_CRD_CERTIFICATE,
  549. conn->xcred);
  550. if (ret < 0) {
  551. wpa_printf(MSG_INFO, "Failed to configure credentials: %s",
  552. gnutls_strerror(ret));
  553. }
  554. #ifdef GNUTLS_IA
  555. if (conn->iacred_cli)
  556. gnutls_ia_free_client_credentials(conn->iacred_cli);
  557. ret = gnutls_ia_allocate_client_credentials(&conn->iacred_cli);
  558. if (ret) {
  559. wpa_printf(MSG_DEBUG, "Failed to allocate IA credentials: %s",
  560. gnutls_strerror(ret));
  561. return -1;
  562. }
  563. ret = gnutls_credentials_set(conn->session, GNUTLS_CRD_IA,
  564. conn->iacred_cli);
  565. if (ret) {
  566. wpa_printf(MSG_DEBUG, "Failed to configure IA credentials: %s",
  567. gnutls_strerror(ret));
  568. gnutls_ia_free_client_credentials(conn->iacred_cli);
  569. conn->iacred_cli = NULL;
  570. return -1;
  571. }
  572. #endif /* GNUTLS_IE */
  573. return ret;
  574. }
  575. int tls_global_set_params(void *tls_ctx,
  576. const struct tls_connection_params *params)
  577. {
  578. struct tls_global *global = tls_ctx;
  579. int ret;
  580. /* Currently, global parameters are only set when running in server
  581. * mode. */
  582. global->server = 1;
  583. if (global->params_set) {
  584. gnutls_certificate_free_credentials(global->xcred);
  585. global->params_set = 0;
  586. }
  587. ret = gnutls_certificate_allocate_credentials(&global->xcred);
  588. if (ret) {
  589. wpa_printf(MSG_DEBUG, "Failed to allocate global credentials "
  590. "%s", gnutls_strerror(ret));
  591. return -1;
  592. }
  593. if (params->ca_cert) {
  594. ret = gnutls_certificate_set_x509_trust_file(
  595. global->xcred, params->ca_cert, GNUTLS_X509_FMT_PEM);
  596. if (ret < 0) {
  597. wpa_printf(MSG_DEBUG, "Failed to read CA cert '%s' "
  598. "in PEM format: %s", params->ca_cert,
  599. gnutls_strerror(ret));
  600. ret = gnutls_certificate_set_x509_trust_file(
  601. global->xcred, params->ca_cert,
  602. GNUTLS_X509_FMT_DER);
  603. if (ret < 0) {
  604. wpa_printf(MSG_DEBUG, "Failed to read CA cert "
  605. "'%s' in DER format: %s",
  606. params->ca_cert,
  607. gnutls_strerror(ret));
  608. goto fail;
  609. }
  610. }
  611. if (params->flags & TLS_CONN_ALLOW_SIGN_RSA_MD5) {
  612. gnutls_certificate_set_verify_flags(
  613. global->xcred,
  614. GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5);
  615. }
  616. if (params->flags & TLS_CONN_DISABLE_TIME_CHECKS) {
  617. gnutls_certificate_set_verify_flags(
  618. global->xcred,
  619. GNUTLS_VERIFY_DISABLE_TIME_CHECKS);
  620. }
  621. }
  622. if (params->client_cert && params->private_key) {
  623. /* TODO: private_key_passwd? */
  624. ret = gnutls_certificate_set_x509_key_file(
  625. global->xcred, params->client_cert,
  626. params->private_key, GNUTLS_X509_FMT_PEM);
  627. if (ret < 0) {
  628. wpa_printf(MSG_DEBUG, "Failed to read client cert/key "
  629. "in PEM format: %s", gnutls_strerror(ret));
  630. ret = gnutls_certificate_set_x509_key_file(
  631. global->xcred, params->client_cert,
  632. params->private_key, GNUTLS_X509_FMT_DER);
  633. if (ret < 0) {
  634. wpa_printf(MSG_DEBUG, "Failed to read client "
  635. "cert/key in DER format: %s",
  636. gnutls_strerror(ret));
  637. goto fail;
  638. }
  639. }
  640. } else if (params->private_key) {
  641. int pkcs12_ok = 0;
  642. #ifdef PKCS12_FUNCS
  643. /* Try to load in PKCS#12 format */
  644. #if LIBGNUTLS_VERSION_NUMBER >= 0x010302
  645. ret = gnutls_certificate_set_x509_simple_pkcs12_file(
  646. global->xcred, params->private_key,
  647. GNUTLS_X509_FMT_DER, params->private_key_passwd);
  648. if (ret != 0) {
  649. wpa_printf(MSG_DEBUG, "Failed to load private_key in "
  650. "PKCS#12 format: %s", gnutls_strerror(ret));
  651. goto fail;
  652. } else
  653. pkcs12_ok = 1;
  654. #endif /* LIBGNUTLS_VERSION_NUMBER >= 0x010302 */
  655. #endif /* PKCS12_FUNCS */
  656. if (!pkcs12_ok) {
  657. wpa_printf(MSG_DEBUG, "GnuTLS: PKCS#12 support not "
  658. "included");
  659. goto fail;
  660. }
  661. }
  662. global->params_set = 1;
  663. return 0;
  664. fail:
  665. gnutls_certificate_free_credentials(global->xcred);
  666. return -1;
  667. }
  668. int tls_global_set_verify(void *ssl_ctx, int check_crl)
  669. {
  670. /* TODO */
  671. return 0;
  672. }
  673. int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
  674. int verify_peer)
  675. {
  676. if (conn == NULL || conn->session == NULL)
  677. return -1;
  678. conn->verify_peer = verify_peer;
  679. gnutls_certificate_server_set_request(conn->session,
  680. verify_peer ? GNUTLS_CERT_REQUIRE
  681. : GNUTLS_CERT_REQUEST);
  682. return 0;
  683. }
  684. int tls_connection_get_keys(void *ssl_ctx, struct tls_connection *conn,
  685. struct tls_keys *keys)
  686. {
  687. #ifdef GNUTLS_INTERNAL_STRUCTURE_HACK
  688. security_parameters_st *sec;
  689. #endif /* GNUTLS_INTERNAL_STRUCTURE_HACK */
  690. if (conn == NULL || conn->session == NULL || keys == NULL)
  691. return -1;
  692. os_memset(keys, 0, sizeof(*keys));
  693. #ifdef GNUTLS_INTERNAL_STRUCTURE_HACK
  694. sec = &conn->session->security_parameters;
  695. keys->master_key = sec->master_secret;
  696. keys->master_key_len = TLS_MASTER_SIZE;
  697. keys->client_random = sec->client_random;
  698. keys->server_random = sec->server_random;
  699. #else /* GNUTLS_INTERNAL_STRUCTURE_HACK */
  700. keys->client_random =
  701. (u8 *) gnutls_session_get_client_random(conn->session);
  702. keys->server_random =
  703. (u8 *) gnutls_session_get_server_random(conn->session);
  704. /* No access to master_secret */
  705. #endif /* GNUTLS_INTERNAL_STRUCTURE_HACK */
  706. #ifdef GNUTLS_IA
  707. gnutls_ia_extract_inner_secret(conn->session,
  708. (char *) conn->inner_secret);
  709. keys->inner_secret = conn->inner_secret;
  710. keys->inner_secret_len = TLS_MASTER_SIZE;
  711. #endif /* GNUTLS_IA */
  712. keys->client_random_len = TLS_RANDOM_SIZE;
  713. keys->server_random_len = TLS_RANDOM_SIZE;
  714. return 0;
  715. }
  716. int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
  717. const char *label, int server_random_first,
  718. u8 *out, size_t out_len)
  719. {
  720. #if LIBGNUTLS_VERSION_NUMBER >= 0x010302
  721. if (conn == NULL || conn->session == NULL)
  722. return -1;
  723. return gnutls_prf(conn->session, os_strlen(label), label,
  724. server_random_first, 0, NULL, out_len, (char *) out);
  725. #else /* LIBGNUTLS_VERSION_NUMBER >= 0x010302 */
  726. return -1;
  727. #endif /* LIBGNUTLS_VERSION_NUMBER >= 0x010302 */
  728. }
  729. static int tls_connection_verify_peer(struct tls_connection *conn,
  730. gnutls_alert_description_t *err)
  731. {
  732. unsigned int status, num_certs, i;
  733. struct os_time now;
  734. const gnutls_datum_t *certs;
  735. gnutls_x509_crt_t cert;
  736. if (gnutls_certificate_verify_peers2(conn->session, &status) < 0) {
  737. wpa_printf(MSG_INFO, "TLS: Failed to verify peer "
  738. "certificate chain");
  739. *err = GNUTLS_A_INTERNAL_ERROR;
  740. return -1;
  741. }
  742. if (conn->verify_peer && (status & GNUTLS_CERT_INVALID)) {
  743. wpa_printf(MSG_INFO, "TLS: Peer certificate not trusted");
  744. if (status & GNUTLS_CERT_INSECURE_ALGORITHM) {
  745. wpa_printf(MSG_INFO, "TLS: Certificate uses insecure "
  746. "algorithm");
  747. *err = GNUTLS_A_INSUFFICIENT_SECURITY;
  748. }
  749. if (status & GNUTLS_CERT_NOT_ACTIVATED) {
  750. wpa_printf(MSG_INFO, "TLS: Certificate not yet "
  751. "activated");
  752. *err = GNUTLS_A_CERTIFICATE_EXPIRED;
  753. }
  754. if (status & GNUTLS_CERT_EXPIRED) {
  755. wpa_printf(MSG_INFO, "TLS: Certificate expired");
  756. *err = GNUTLS_A_CERTIFICATE_EXPIRED;
  757. }
  758. return -1;
  759. }
  760. if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) {
  761. wpa_printf(MSG_INFO, "TLS: Peer certificate does not have a "
  762. "known issuer");
  763. *err = GNUTLS_A_UNKNOWN_CA;
  764. return -1;
  765. }
  766. if (status & GNUTLS_CERT_REVOKED) {
  767. wpa_printf(MSG_INFO, "TLS: Peer certificate has been revoked");
  768. *err = GNUTLS_A_CERTIFICATE_REVOKED;
  769. return -1;
  770. }
  771. os_get_time(&now);
  772. certs = gnutls_certificate_get_peers(conn->session, &num_certs);
  773. if (certs == NULL) {
  774. wpa_printf(MSG_INFO, "TLS: No peer certificate chain "
  775. "received");
  776. *err = GNUTLS_A_UNKNOWN_CA;
  777. return -1;
  778. }
  779. for (i = 0; i < num_certs; i++) {
  780. char *buf;
  781. size_t len;
  782. if (gnutls_x509_crt_init(&cert) < 0) {
  783. wpa_printf(MSG_INFO, "TLS: Certificate initialization "
  784. "failed");
  785. *err = GNUTLS_A_BAD_CERTIFICATE;
  786. return -1;
  787. }
  788. if (gnutls_x509_crt_import(cert, &certs[i],
  789. GNUTLS_X509_FMT_DER) < 0) {
  790. wpa_printf(MSG_INFO, "TLS: Could not parse peer "
  791. "certificate %d/%d", i + 1, num_certs);
  792. gnutls_x509_crt_deinit(cert);
  793. *err = GNUTLS_A_BAD_CERTIFICATE;
  794. return -1;
  795. }
  796. gnutls_x509_crt_get_dn(cert, NULL, &len);
  797. len++;
  798. buf = os_malloc(len + 1);
  799. if (buf) {
  800. buf[0] = buf[len] = '\0';
  801. gnutls_x509_crt_get_dn(cert, buf, &len);
  802. }
  803. wpa_printf(MSG_DEBUG, "TLS: Peer cert chain %d/%d: %s",
  804. i + 1, num_certs, buf);
  805. if (i == 0) {
  806. /* TODO: validate subject_match and altsubject_match */
  807. }
  808. os_free(buf);
  809. if (gnutls_x509_crt_get_expiration_time(cert) < now.sec ||
  810. gnutls_x509_crt_get_activation_time(cert) > now.sec) {
  811. wpa_printf(MSG_INFO, "TLS: Peer certificate %d/%d is "
  812. "not valid at this time",
  813. i + 1, num_certs);
  814. gnutls_x509_crt_deinit(cert);
  815. *err = GNUTLS_A_CERTIFICATE_EXPIRED;
  816. return -1;
  817. }
  818. gnutls_x509_crt_deinit(cert);
  819. }
  820. return 0;
  821. }
  822. static struct wpabuf * gnutls_get_appl_data(struct tls_connection *conn)
  823. {
  824. int res;
  825. struct wpabuf *ad;
  826. wpa_printf(MSG_DEBUG, "GnuTLS: Check for possible Application Data");
  827. ad = wpabuf_alloc((wpabuf_len(conn->pull_buf) + 500) * 3);
  828. if (ad == NULL)
  829. return NULL;
  830. res = gnutls_record_recv(conn->session, wpabuf_mhead(ad),
  831. wpabuf_size(ad));
  832. wpa_printf(MSG_DEBUG, "GnuTLS: gnutls_record_recv: %d", res);
  833. if (res < 0) {
  834. wpa_printf(MSG_DEBUG, "%s - gnutls_ia_recv failed: %d "
  835. "(%s)", __func__, (int) res,
  836. gnutls_strerror(res));
  837. wpabuf_free(ad);
  838. return NULL;
  839. }
  840. wpabuf_put(ad, res);
  841. wpa_printf(MSG_DEBUG, "GnuTLS: Received %d bytes of Application Data",
  842. res);
  843. return ad;
  844. }
  845. struct wpabuf * tls_connection_handshake(void *tls_ctx,
  846. struct tls_connection *conn,
  847. const struct wpabuf *in_data,
  848. struct wpabuf **appl_data)
  849. {
  850. struct tls_global *global = tls_ctx;
  851. struct wpabuf *out_data;
  852. int ret;
  853. if (appl_data)
  854. *appl_data = NULL;
  855. if (in_data && wpabuf_len(in_data) > 0) {
  856. if (conn->pull_buf) {
  857. wpa_printf(MSG_DEBUG, "%s - %lu bytes remaining in "
  858. "pull_buf", __func__,
  859. (unsigned long) wpabuf_len(conn->pull_buf));
  860. wpabuf_free(conn->pull_buf);
  861. }
  862. conn->pull_buf = wpabuf_dup(in_data);
  863. if (conn->pull_buf == NULL)
  864. return NULL;
  865. conn->pull_buf_offset = wpabuf_head(conn->pull_buf);
  866. }
  867. ret = gnutls_handshake(conn->session);
  868. if (ret < 0) {
  869. switch (ret) {
  870. case GNUTLS_E_AGAIN:
  871. if (global->server && conn->established &&
  872. conn->push_buf == NULL) {
  873. /* Need to return something to trigger
  874. * completion of EAP-TLS. */
  875. conn->push_buf = wpabuf_alloc(0);
  876. }
  877. break;
  878. case GNUTLS_E_FATAL_ALERT_RECEIVED:
  879. wpa_printf(MSG_DEBUG, "%s - received fatal '%s' alert",
  880. __func__, gnutls_alert_get_name(
  881. gnutls_alert_get(conn->session)));
  882. conn->read_alerts++;
  883. /* continue */
  884. default:
  885. wpa_printf(MSG_DEBUG, "%s - gnutls_handshake failed "
  886. "-> %s", __func__, gnutls_strerror(ret));
  887. conn->failed++;
  888. }
  889. } else {
  890. size_t size;
  891. gnutls_alert_description_t err;
  892. if (conn->verify_peer &&
  893. tls_connection_verify_peer(conn, &err)) {
  894. wpa_printf(MSG_INFO, "TLS: Peer certificate chain "
  895. "failed validation");
  896. conn->failed++;
  897. gnutls_alert_send(conn->session, GNUTLS_AL_FATAL, err);
  898. goto out;
  899. }
  900. #ifdef CONFIG_GNUTLS_EXTRA
  901. if (conn->tls_ia && !gnutls_ia_handshake_p(conn->session)) {
  902. wpa_printf(MSG_INFO, "TLS: No TLS/IA negotiation");
  903. conn->failed++;
  904. return NULL;
  905. }
  906. #endif /* CONFIG_GNUTLS_EXTRA */
  907. if (conn->tls_ia)
  908. wpa_printf(MSG_DEBUG, "TLS: Start TLS/IA handshake");
  909. else {
  910. wpa_printf(MSG_DEBUG, "TLS: Handshake completed "
  911. "successfully");
  912. }
  913. conn->established = 1;
  914. if (conn->push_buf == NULL) {
  915. /* Need to return something to get final TLS ACK. */
  916. conn->push_buf = wpabuf_alloc(0);
  917. }
  918. gnutls_session_get_data(conn->session, NULL, &size);
  919. if (global->session_data == NULL ||
  920. global->session_data_size < size) {
  921. os_free(global->session_data);
  922. global->session_data = os_malloc(size);
  923. }
  924. if (global->session_data) {
  925. global->session_data_size = size;
  926. gnutls_session_get_data(conn->session,
  927. global->session_data,
  928. &global->session_data_size);
  929. }
  930. if (conn->pull_buf && appl_data)
  931. *appl_data = gnutls_get_appl_data(conn);
  932. }
  933. out:
  934. out_data = conn->push_buf;
  935. conn->push_buf = NULL;
  936. return out_data;
  937. }
  938. struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
  939. struct tls_connection *conn,
  940. const struct wpabuf *in_data,
  941. struct wpabuf **appl_data)
  942. {
  943. return tls_connection_handshake(tls_ctx, conn, in_data, appl_data);
  944. }
  945. struct wpabuf * tls_connection_encrypt(void *tls_ctx,
  946. struct tls_connection *conn,
  947. const struct wpabuf *in_data)
  948. {
  949. ssize_t res;
  950. struct wpabuf *buf;
  951. #ifdef GNUTLS_IA
  952. if (conn->tls_ia)
  953. res = gnutls_ia_send(conn->session, wpabuf_head(in_data),
  954. wpabuf_len(in_data));
  955. else
  956. #endif /* GNUTLS_IA */
  957. res = gnutls_record_send(conn->session, wpabuf_head(in_data),
  958. wpabuf_len(in_data));
  959. if (res < 0) {
  960. wpa_printf(MSG_INFO, "%s: Encryption failed: %s",
  961. __func__, gnutls_strerror(res));
  962. return NULL;
  963. }
  964. buf = conn->push_buf;
  965. conn->push_buf = NULL;
  966. return buf;
  967. }
  968. struct wpabuf * tls_connection_decrypt(void *tls_ctx,
  969. struct tls_connection *conn,
  970. const struct wpabuf *in_data)
  971. {
  972. ssize_t res;
  973. struct wpabuf *out;
  974. if (conn->pull_buf) {
  975. wpa_printf(MSG_DEBUG, "%s - %lu bytes remaining in "
  976. "pull_buf", __func__,
  977. (unsigned long) wpabuf_len(conn->pull_buf));
  978. wpabuf_free(conn->pull_buf);
  979. }
  980. conn->pull_buf = wpabuf_dup(in_data);
  981. if (conn->pull_buf == NULL)
  982. return NULL;
  983. conn->pull_buf_offset = wpabuf_head(conn->pull_buf);
  984. /*
  985. * Even though we try to disable TLS compression, it is possible that
  986. * this cannot be done with all TLS libraries. Add extra buffer space
  987. * to handle the possibility of the decrypted data being longer than
  988. * input data.
  989. */
  990. out = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
  991. if (out == NULL)
  992. return NULL;
  993. #ifdef GNUTLS_IA
  994. if (conn->tls_ia) {
  995. res = gnutls_ia_recv(conn->session, wpabuf_mhead(out),
  996. wpabuf_size(out));
  997. if (res == GNUTLS_E_WARNING_IA_IPHF_RECEIVED ||
  998. res == GNUTLS_E_WARNING_IA_FPHF_RECEIVED) {
  999. int final = res == GNUTLS_E_WARNING_IA_FPHF_RECEIVED;
  1000. wpa_printf(MSG_DEBUG, "%s: Received %sPhaseFinished",
  1001. __func__, final ? "Final" : "Intermediate");
  1002. res = gnutls_ia_permute_inner_secret(
  1003. conn->session, conn->session_keys_len,
  1004. (char *) conn->session_keys);
  1005. if (conn->session_keys) {
  1006. os_memset(conn->session_keys, 0,
  1007. conn->session_keys_len);
  1008. os_free(conn->session_keys);
  1009. }
  1010. conn->session_keys = NULL;
  1011. conn->session_keys_len = 0;
  1012. if (res) {
  1013. wpa_printf(MSG_DEBUG, "%s: Failed to permute "
  1014. "inner secret: %s",
  1015. __func__, gnutls_strerror(res));
  1016. wpabuf_free(out);
  1017. return NULL;
  1018. }
  1019. res = gnutls_ia_verify_endphase(conn->session,
  1020. wpabuf_head(out));
  1021. if (res == 0) {
  1022. wpa_printf(MSG_DEBUG, "%s: Correct endphase "
  1023. "checksum", __func__);
  1024. } else {
  1025. wpa_printf(MSG_INFO, "%s: Endphase "
  1026. "verification failed: %s",
  1027. __func__, gnutls_strerror(res));
  1028. wpabuf_free(out);
  1029. return NULL;
  1030. }
  1031. if (final)
  1032. conn->final_phase_finished = 1;
  1033. return out;
  1034. }
  1035. if (res < 0) {
  1036. wpa_printf(MSG_DEBUG, "%s - gnutls_ia_recv failed: %d "
  1037. "(%s)", __func__, (int) res,
  1038. gnutls_strerror(res));
  1039. wpabuf_free(out);
  1040. return NULL;
  1041. }
  1042. wpabuf_put(out, res);
  1043. return out;
  1044. }
  1045. #endif /* GNUTLS_IA */
  1046. res = gnutls_record_recv(conn->session, wpabuf_mhead(out),
  1047. wpabuf_size(out));
  1048. if (res < 0) {
  1049. wpa_printf(MSG_DEBUG, "%s - gnutls_record_recv failed: %d "
  1050. "(%s)", __func__, (int) res, gnutls_strerror(res));
  1051. wpabuf_free(out);
  1052. return NULL;
  1053. }
  1054. wpabuf_put(out, res);
  1055. return out;
  1056. }
  1057. int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
  1058. {
  1059. if (conn == NULL)
  1060. return 0;
  1061. return gnutls_session_is_resumed(conn->session);
  1062. }
  1063. int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
  1064. u8 *ciphers)
  1065. {
  1066. /* TODO */
  1067. return -1;
  1068. }
  1069. int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
  1070. char *buf, size_t buflen)
  1071. {
  1072. /* TODO */
  1073. buf[0] = '\0';
  1074. return 0;
  1075. }
  1076. int tls_connection_enable_workaround(void *ssl_ctx,
  1077. struct tls_connection *conn)
  1078. {
  1079. /* TODO: set SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS */
  1080. return 0;
  1081. }
  1082. int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
  1083. int ext_type, const u8 *data,
  1084. size_t data_len)
  1085. {
  1086. /* TODO */
  1087. return -1;
  1088. }
  1089. int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
  1090. {
  1091. if (conn == NULL)
  1092. return -1;
  1093. return conn->failed;
  1094. }
  1095. int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
  1096. {
  1097. if (conn == NULL)
  1098. return -1;
  1099. return conn->read_alerts;
  1100. }
  1101. int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
  1102. {
  1103. if (conn == NULL)
  1104. return -1;
  1105. return conn->write_alerts;
  1106. }
  1107. int tls_connection_get_keyblock_size(void *tls_ctx,
  1108. struct tls_connection *conn)
  1109. {
  1110. /* TODO */
  1111. return -1;
  1112. }
  1113. unsigned int tls_capabilities(void *tls_ctx)
  1114. {
  1115. unsigned int capa = 0;
  1116. #ifdef GNUTLS_IA
  1117. capa |= TLS_CAPABILITY_IA;
  1118. #endif /* GNUTLS_IA */
  1119. return capa;
  1120. }
  1121. int tls_connection_set_ia(void *tls_ctx, struct tls_connection *conn,
  1122. int tls_ia)
  1123. {
  1124. #ifdef GNUTLS_IA
  1125. int ret;
  1126. if (conn == NULL)
  1127. return -1;
  1128. conn->tls_ia = tls_ia;
  1129. if (!tls_ia)
  1130. return 0;
  1131. ret = gnutls_ia_allocate_server_credentials(&conn->iacred_srv);
  1132. if (ret) {
  1133. wpa_printf(MSG_DEBUG, "Failed to allocate IA credentials: %s",
  1134. gnutls_strerror(ret));
  1135. return -1;
  1136. }
  1137. ret = gnutls_credentials_set(conn->session, GNUTLS_CRD_IA,
  1138. conn->iacred_srv);
  1139. if (ret) {
  1140. wpa_printf(MSG_DEBUG, "Failed to configure IA credentials: %s",
  1141. gnutls_strerror(ret));
  1142. gnutls_ia_free_server_credentials(conn->iacred_srv);
  1143. conn->iacred_srv = NULL;
  1144. return -1;
  1145. }
  1146. return 0;
  1147. #else /* GNUTLS_IA */
  1148. return -1;
  1149. #endif /* GNUTLS_IA */
  1150. }
  1151. struct wpabuf * tls_connection_ia_send_phase_finished(
  1152. void *tls_ctx, struct tls_connection *conn, int final)
  1153. {
  1154. #ifdef GNUTLS_IA
  1155. int ret;
  1156. struct wpabuf *buf;
  1157. if (conn == NULL || conn->session == NULL || !conn->tls_ia)
  1158. return NULL;
  1159. ret = gnutls_ia_permute_inner_secret(conn->session,
  1160. conn->session_keys_len,
  1161. (char *) conn->session_keys);
  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 = NULL;
  1167. conn->session_keys_len = 0;
  1168. if (ret) {
  1169. wpa_printf(MSG_DEBUG, "%s: Failed to permute inner secret: %s",
  1170. __func__, gnutls_strerror(ret));
  1171. return NULL;
  1172. }
  1173. ret = gnutls_ia_endphase_send(conn->session, final);
  1174. if (ret) {
  1175. wpa_printf(MSG_DEBUG, "%s: Failed to send endphase: %s",
  1176. __func__, gnutls_strerror(ret));
  1177. return NULL;
  1178. }
  1179. buf = conn->push_buf;
  1180. conn->push_buf = NULL;
  1181. return buf;
  1182. #else /* GNUTLS_IA */
  1183. return NULL;
  1184. #endif /* GNUTLS_IA */
  1185. }
  1186. int tls_connection_ia_final_phase_finished(void *tls_ctx,
  1187. struct tls_connection *conn)
  1188. {
  1189. if (conn == NULL)
  1190. return -1;
  1191. return conn->final_phase_finished;
  1192. }
  1193. int tls_connection_ia_permute_inner_secret(void *tls_ctx,
  1194. struct tls_connection *conn,
  1195. const u8 *key, size_t key_len)
  1196. {
  1197. #ifdef GNUTLS_IA
  1198. if (conn == NULL || !conn->tls_ia)
  1199. return -1;
  1200. if (conn->session_keys) {
  1201. os_memset(conn->session_keys, 0, conn->session_keys_len);
  1202. os_free(conn->session_keys);
  1203. }
  1204. conn->session_keys_len = 0;
  1205. if (key) {
  1206. conn->session_keys = os_malloc(key_len);
  1207. if (conn->session_keys == NULL)
  1208. return -1;
  1209. os_memcpy(conn->session_keys, key, key_len);
  1210. conn->session_keys_len = key_len;
  1211. } else {
  1212. conn->session_keys = NULL;
  1213. conn->session_keys_len = 0;
  1214. }
  1215. return 0;
  1216. #else /* GNUTLS_IA */
  1217. return -1;
  1218. #endif /* GNUTLS_IA */
  1219. }
  1220. int tls_connection_set_session_ticket_cb(void *tls_ctx,
  1221. struct tls_connection *conn,
  1222. tls_session_ticket_cb cb, void *ctx)
  1223. {
  1224. return -1;
  1225. }