tls_internal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. * TLS interface functions and an internal TLS implementation
  3. * Copyright (c) 2004-2011, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. *
  8. * This file interface functions for hostapd/wpa_supplicant to use the
  9. * integrated TLSv1 implementation.
  10. */
  11. #include "includes.h"
  12. #include "common.h"
  13. #include "tls.h"
  14. #include "tls/tlsv1_client.h"
  15. #include "tls/tlsv1_server.h"
  16. static int tls_ref_count = 0;
  17. struct tls_global {
  18. int server;
  19. struct tlsv1_credentials *server_cred;
  20. int check_crl;
  21. };
  22. struct tls_connection {
  23. struct tlsv1_client *client;
  24. struct tlsv1_server *server;
  25. struct tls_global *global;
  26. };
  27. void * tls_init(const struct tls_config *conf)
  28. {
  29. struct tls_global *global;
  30. if (tls_ref_count == 0) {
  31. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  32. if (tlsv1_client_global_init())
  33. return NULL;
  34. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  35. #ifdef CONFIG_TLS_INTERNAL_SERVER
  36. if (tlsv1_server_global_init())
  37. return NULL;
  38. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  39. }
  40. tls_ref_count++;
  41. global = os_zalloc(sizeof(*global));
  42. if (global == NULL)
  43. return NULL;
  44. return global;
  45. }
  46. void tls_deinit(void *ssl_ctx)
  47. {
  48. struct tls_global *global = ssl_ctx;
  49. tls_ref_count--;
  50. if (tls_ref_count == 0) {
  51. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  52. tlsv1_client_global_deinit();
  53. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  54. #ifdef CONFIG_TLS_INTERNAL_SERVER
  55. tlsv1_cred_free(global->server_cred);
  56. tlsv1_server_global_deinit();
  57. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  58. }
  59. os_free(global);
  60. }
  61. int tls_get_errors(void *tls_ctx)
  62. {
  63. return 0;
  64. }
  65. struct tls_connection * tls_connection_init(void *tls_ctx)
  66. {
  67. struct tls_connection *conn;
  68. struct tls_global *global = tls_ctx;
  69. conn = os_zalloc(sizeof(*conn));
  70. if (conn == NULL)
  71. return NULL;
  72. conn->global = global;
  73. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  74. if (!global->server) {
  75. conn->client = tlsv1_client_init();
  76. if (conn->client == NULL) {
  77. os_free(conn);
  78. return NULL;
  79. }
  80. }
  81. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  82. #ifdef CONFIG_TLS_INTERNAL_SERVER
  83. if (global->server) {
  84. conn->server = tlsv1_server_init(global->server_cred);
  85. if (conn->server == NULL) {
  86. os_free(conn);
  87. return NULL;
  88. }
  89. }
  90. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  91. return conn;
  92. }
  93. #ifdef CONFIG_TESTING_OPTIONS
  94. #ifdef CONFIG_TLS_INTERNAL_SERVER
  95. void tls_connection_set_test_flags(struct tls_connection *conn, u32 flags)
  96. {
  97. if (conn->server)
  98. tlsv1_server_set_test_flags(conn->server, flags);
  99. }
  100. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  101. #endif /* CONFIG_TESTING_OPTIONS */
  102. void tls_connection_set_log_cb(struct tls_connection *conn,
  103. void (*log_cb)(void *ctx, const char *msg),
  104. void *ctx)
  105. {
  106. #ifdef CONFIG_TLS_INTERNAL_SERVER
  107. if (conn->server)
  108. tlsv1_server_set_log_cb(conn->server, log_cb, ctx);
  109. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  110. }
  111. void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn)
  112. {
  113. if (conn == NULL)
  114. return;
  115. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  116. if (conn->client)
  117. tlsv1_client_deinit(conn->client);
  118. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  119. #ifdef CONFIG_TLS_INTERNAL_SERVER
  120. if (conn->server)
  121. tlsv1_server_deinit(conn->server);
  122. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  123. os_free(conn);
  124. }
  125. int tls_connection_established(void *tls_ctx, struct tls_connection *conn)
  126. {
  127. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  128. if (conn->client)
  129. return tlsv1_client_established(conn->client);
  130. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  131. #ifdef CONFIG_TLS_INTERNAL_SERVER
  132. if (conn->server)
  133. return tlsv1_server_established(conn->server);
  134. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  135. return 0;
  136. }
  137. int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn)
  138. {
  139. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  140. if (conn->client)
  141. return tlsv1_client_shutdown(conn->client);
  142. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  143. #ifdef CONFIG_TLS_INTERNAL_SERVER
  144. if (conn->server)
  145. return tlsv1_server_shutdown(conn->server);
  146. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  147. return -1;
  148. }
  149. int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
  150. const struct tls_connection_params *params)
  151. {
  152. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  153. struct tlsv1_credentials *cred;
  154. if (conn->client == NULL)
  155. return -1;
  156. cred = tlsv1_cred_alloc();
  157. if (cred == NULL)
  158. return -1;
  159. if (tlsv1_set_ca_cert(cred, params->ca_cert,
  160. params->ca_cert_blob, params->ca_cert_blob_len,
  161. params->ca_path)) {
  162. wpa_printf(MSG_INFO, "TLS: Failed to configure trusted CA "
  163. "certificates");
  164. tlsv1_cred_free(cred);
  165. return -1;
  166. }
  167. if (tlsv1_set_cert(cred, params->client_cert,
  168. params->client_cert_blob,
  169. params->client_cert_blob_len)) {
  170. wpa_printf(MSG_INFO, "TLS: Failed to configure client "
  171. "certificate");
  172. tlsv1_cred_free(cred);
  173. return -1;
  174. }
  175. if (tlsv1_set_private_key(cred, params->private_key,
  176. params->private_key_passwd,
  177. params->private_key_blob,
  178. params->private_key_blob_len)) {
  179. wpa_printf(MSG_INFO, "TLS: Failed to load private key");
  180. tlsv1_cred_free(cred);
  181. return -1;
  182. }
  183. if (tlsv1_set_dhparams(cred, params->dh_file, params->dh_blob,
  184. params->dh_blob_len)) {
  185. wpa_printf(MSG_INFO, "TLS: Failed to load DH parameters");
  186. tlsv1_cred_free(cred);
  187. return -1;
  188. }
  189. if (tlsv1_client_set_cred(conn->client, cred) < 0) {
  190. tlsv1_cred_free(cred);
  191. return -1;
  192. }
  193. tlsv1_client_set_time_checks(
  194. conn->client, !(params->flags & TLS_CONN_DISABLE_TIME_CHECKS));
  195. return 0;
  196. #else /* CONFIG_TLS_INTERNAL_CLIENT */
  197. return -1;
  198. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  199. }
  200. int tls_global_set_params(void *tls_ctx,
  201. const struct tls_connection_params *params)
  202. {
  203. #ifdef CONFIG_TLS_INTERNAL_SERVER
  204. struct tls_global *global = tls_ctx;
  205. struct tlsv1_credentials *cred;
  206. /* Currently, global parameters are only set when running in server
  207. * mode. */
  208. global->server = 1;
  209. tlsv1_cred_free(global->server_cred);
  210. global->server_cred = cred = tlsv1_cred_alloc();
  211. if (cred == NULL)
  212. return -1;
  213. if (tlsv1_set_ca_cert(cred, params->ca_cert, params->ca_cert_blob,
  214. params->ca_cert_blob_len, params->ca_path)) {
  215. wpa_printf(MSG_INFO, "TLS: Failed to configure trusted CA "
  216. "certificates");
  217. return -1;
  218. }
  219. if (tlsv1_set_cert(cred, params->client_cert, params->client_cert_blob,
  220. params->client_cert_blob_len)) {
  221. wpa_printf(MSG_INFO, "TLS: Failed to configure server "
  222. "certificate");
  223. return -1;
  224. }
  225. if (tlsv1_set_private_key(cred, params->private_key,
  226. params->private_key_passwd,
  227. params->private_key_blob,
  228. params->private_key_blob_len)) {
  229. wpa_printf(MSG_INFO, "TLS: Failed to load private key");
  230. return -1;
  231. }
  232. if (tlsv1_set_dhparams(cred, params->dh_file, params->dh_blob,
  233. params->dh_blob_len)) {
  234. wpa_printf(MSG_INFO, "TLS: Failed to load DH parameters");
  235. return -1;
  236. }
  237. return 0;
  238. #else /* CONFIG_TLS_INTERNAL_SERVER */
  239. return -1;
  240. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  241. }
  242. int tls_global_set_verify(void *tls_ctx, int check_crl)
  243. {
  244. struct tls_global *global = tls_ctx;
  245. global->check_crl = check_crl;
  246. return 0;
  247. }
  248. int tls_connection_set_verify(void *tls_ctx, struct tls_connection *conn,
  249. int verify_peer)
  250. {
  251. #ifdef CONFIG_TLS_INTERNAL_SERVER
  252. if (conn->server)
  253. return tlsv1_server_set_verify(conn->server, verify_peer);
  254. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  255. return -1;
  256. }
  257. int tls_connection_get_keys(void *tls_ctx, struct tls_connection *conn,
  258. struct tls_keys *keys)
  259. {
  260. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  261. if (conn->client)
  262. return tlsv1_client_get_keys(conn->client, keys);
  263. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  264. #ifdef CONFIG_TLS_INTERNAL_SERVER
  265. if (conn->server)
  266. return tlsv1_server_get_keys(conn->server, keys);
  267. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  268. return -1;
  269. }
  270. int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
  271. const char *label, int server_random_first,
  272. u8 *out, size_t out_len)
  273. {
  274. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  275. if (conn->client) {
  276. return tlsv1_client_prf(conn->client, label,
  277. server_random_first,
  278. out, out_len);
  279. }
  280. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  281. #ifdef CONFIG_TLS_INTERNAL_SERVER
  282. if (conn->server) {
  283. return tlsv1_server_prf(conn->server, label,
  284. server_random_first,
  285. out, out_len);
  286. }
  287. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  288. return -1;
  289. }
  290. struct wpabuf * tls_connection_handshake(void *tls_ctx,
  291. struct tls_connection *conn,
  292. const struct wpabuf *in_data,
  293. struct wpabuf **appl_data)
  294. {
  295. return tls_connection_handshake2(tls_ctx, conn, in_data, appl_data,
  296. NULL);
  297. }
  298. struct wpabuf * tls_connection_handshake2(void *tls_ctx,
  299. struct tls_connection *conn,
  300. const struct wpabuf *in_data,
  301. struct wpabuf **appl_data,
  302. int *need_more_data)
  303. {
  304. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  305. u8 *res, *ad;
  306. size_t res_len, ad_len;
  307. struct wpabuf *out;
  308. if (conn->client == NULL)
  309. return NULL;
  310. ad = NULL;
  311. res = tlsv1_client_handshake(conn->client,
  312. in_data ? wpabuf_head(in_data) : NULL,
  313. in_data ? wpabuf_len(in_data) : 0,
  314. &res_len, &ad, &ad_len, need_more_data);
  315. if (res == NULL)
  316. return NULL;
  317. out = wpabuf_alloc_ext_data(res, res_len);
  318. if (out == NULL) {
  319. os_free(res);
  320. os_free(ad);
  321. return NULL;
  322. }
  323. if (appl_data) {
  324. if (ad) {
  325. *appl_data = wpabuf_alloc_ext_data(ad, ad_len);
  326. if (*appl_data == NULL)
  327. os_free(ad);
  328. } else
  329. *appl_data = NULL;
  330. } else
  331. os_free(ad);
  332. return out;
  333. #else /* CONFIG_TLS_INTERNAL_CLIENT */
  334. return NULL;
  335. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  336. }
  337. struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
  338. struct tls_connection *conn,
  339. const struct wpabuf *in_data,
  340. struct wpabuf **appl_data)
  341. {
  342. #ifdef CONFIG_TLS_INTERNAL_SERVER
  343. u8 *res;
  344. size_t res_len;
  345. struct wpabuf *out;
  346. if (conn->server == NULL)
  347. return NULL;
  348. if (appl_data)
  349. *appl_data = NULL;
  350. res = tlsv1_server_handshake(conn->server, wpabuf_head(in_data),
  351. wpabuf_len(in_data), &res_len);
  352. if (res == NULL && tlsv1_server_established(conn->server))
  353. return wpabuf_alloc(0);
  354. if (res == NULL)
  355. return NULL;
  356. out = wpabuf_alloc_ext_data(res, res_len);
  357. if (out == NULL) {
  358. os_free(res);
  359. return NULL;
  360. }
  361. return out;
  362. #else /* CONFIG_TLS_INTERNAL_SERVER */
  363. return NULL;
  364. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  365. }
  366. struct wpabuf * tls_connection_encrypt(void *tls_ctx,
  367. struct tls_connection *conn,
  368. const struct wpabuf *in_data)
  369. {
  370. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  371. if (conn->client) {
  372. struct wpabuf *buf;
  373. int res;
  374. buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
  375. if (buf == NULL)
  376. return NULL;
  377. res = tlsv1_client_encrypt(conn->client, wpabuf_head(in_data),
  378. wpabuf_len(in_data),
  379. wpabuf_mhead(buf),
  380. wpabuf_size(buf));
  381. if (res < 0) {
  382. wpabuf_free(buf);
  383. return NULL;
  384. }
  385. wpabuf_put(buf, res);
  386. return buf;
  387. }
  388. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  389. #ifdef CONFIG_TLS_INTERNAL_SERVER
  390. if (conn->server) {
  391. struct wpabuf *buf;
  392. int res;
  393. buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
  394. if (buf == NULL)
  395. return NULL;
  396. res = tlsv1_server_encrypt(conn->server, wpabuf_head(in_data),
  397. wpabuf_len(in_data),
  398. wpabuf_mhead(buf),
  399. wpabuf_size(buf));
  400. if (res < 0) {
  401. wpabuf_free(buf);
  402. return NULL;
  403. }
  404. wpabuf_put(buf, res);
  405. return buf;
  406. }
  407. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  408. return NULL;
  409. }
  410. struct wpabuf * tls_connection_decrypt(void *tls_ctx,
  411. struct tls_connection *conn,
  412. const struct wpabuf *in_data)
  413. {
  414. return tls_connection_decrypt2(tls_ctx, conn, in_data, NULL);
  415. }
  416. struct wpabuf * tls_connection_decrypt2(void *tls_ctx,
  417. struct tls_connection *conn,
  418. const struct wpabuf *in_data,
  419. int *need_more_data)
  420. {
  421. if (need_more_data)
  422. *need_more_data = 0;
  423. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  424. if (conn->client) {
  425. return tlsv1_client_decrypt(conn->client, wpabuf_head(in_data),
  426. wpabuf_len(in_data),
  427. need_more_data);
  428. }
  429. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  430. #ifdef CONFIG_TLS_INTERNAL_SERVER
  431. if (conn->server) {
  432. struct wpabuf *buf;
  433. int res;
  434. buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
  435. if (buf == NULL)
  436. return NULL;
  437. res = tlsv1_server_decrypt(conn->server, wpabuf_head(in_data),
  438. wpabuf_len(in_data),
  439. wpabuf_mhead(buf),
  440. wpabuf_size(buf));
  441. if (res < 0) {
  442. wpabuf_free(buf);
  443. return NULL;
  444. }
  445. wpabuf_put(buf, res);
  446. return buf;
  447. }
  448. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  449. return NULL;
  450. }
  451. int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn)
  452. {
  453. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  454. if (conn->client)
  455. return tlsv1_client_resumed(conn->client);
  456. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  457. #ifdef CONFIG_TLS_INTERNAL_SERVER
  458. if (conn->server)
  459. return tlsv1_server_resumed(conn->server);
  460. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  461. return -1;
  462. }
  463. int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
  464. u8 *ciphers)
  465. {
  466. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  467. if (conn->client)
  468. return tlsv1_client_set_cipher_list(conn->client, ciphers);
  469. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  470. #ifdef CONFIG_TLS_INTERNAL_SERVER
  471. if (conn->server)
  472. return tlsv1_server_set_cipher_list(conn->server, ciphers);
  473. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  474. return -1;
  475. }
  476. int tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
  477. char *buf, size_t buflen)
  478. {
  479. if (conn == NULL)
  480. return -1;
  481. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  482. if (conn->client)
  483. return tlsv1_client_get_cipher(conn->client, buf, buflen);
  484. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  485. #ifdef CONFIG_TLS_INTERNAL_SERVER
  486. if (conn->server)
  487. return tlsv1_server_get_cipher(conn->server, buf, buflen);
  488. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  489. return -1;
  490. }
  491. int tls_connection_enable_workaround(void *tls_ctx,
  492. struct tls_connection *conn)
  493. {
  494. return -1;
  495. }
  496. int tls_connection_client_hello_ext(void *tls_ctx, struct tls_connection *conn,
  497. int ext_type, const u8 *data,
  498. size_t data_len)
  499. {
  500. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  501. if (conn->client) {
  502. return tlsv1_client_hello_ext(conn->client, ext_type,
  503. data, data_len);
  504. }
  505. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  506. return -1;
  507. }
  508. int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn)
  509. {
  510. return 0;
  511. }
  512. int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn)
  513. {
  514. return 0;
  515. }
  516. int tls_connection_get_write_alerts(void *tls_ctx,
  517. struct tls_connection *conn)
  518. {
  519. return 0;
  520. }
  521. int tls_connection_get_keyblock_size(void *tls_ctx,
  522. struct tls_connection *conn)
  523. {
  524. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  525. if (conn->client)
  526. return tlsv1_client_get_keyblock_size(conn->client);
  527. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  528. #ifdef CONFIG_TLS_INTERNAL_SERVER
  529. if (conn->server)
  530. return tlsv1_server_get_keyblock_size(conn->server);
  531. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  532. return -1;
  533. }
  534. unsigned int tls_capabilities(void *tls_ctx)
  535. {
  536. return 0;
  537. }
  538. int tls_connection_set_session_ticket_cb(void *tls_ctx,
  539. struct tls_connection *conn,
  540. tls_session_ticket_cb cb,
  541. void *ctx)
  542. {
  543. #ifdef CONFIG_TLS_INTERNAL_CLIENT
  544. if (conn->client) {
  545. tlsv1_client_set_session_ticket_cb(conn->client, cb, ctx);
  546. return 0;
  547. }
  548. #endif /* CONFIG_TLS_INTERNAL_CLIENT */
  549. #ifdef CONFIG_TLS_INTERNAL_SERVER
  550. if (conn->server) {
  551. tlsv1_server_set_session_ticket_cb(conn->server, cb, ctx);
  552. return 0;
  553. }
  554. #endif /* CONFIG_TLS_INTERNAL_SERVER */
  555. return -1;
  556. }