tlsv1_client.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * TLS v1.0/v1.1/v1.2 client (RFC 2246, RFC 4346, RFC 5246)
  3. * Copyright (c) 2006-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. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto/sha1.h"
  11. #include "crypto/tls.h"
  12. #include "tlsv1_common.h"
  13. #include "tlsv1_record.h"
  14. #include "tlsv1_client.h"
  15. #include "tlsv1_client_i.h"
  16. /* TODO:
  17. * Support for a message fragmented across several records (RFC 2246, 6.2.1)
  18. */
  19. void tls_alert(struct tlsv1_client *conn, u8 level, u8 description)
  20. {
  21. conn->alert_level = level;
  22. conn->alert_description = description;
  23. }
  24. void tlsv1_client_free_dh(struct tlsv1_client *conn)
  25. {
  26. os_free(conn->dh_p);
  27. os_free(conn->dh_g);
  28. os_free(conn->dh_ys);
  29. conn->dh_p = conn->dh_g = conn->dh_ys = NULL;
  30. }
  31. int tls_derive_pre_master_secret(u8 *pre_master_secret)
  32. {
  33. WPA_PUT_BE16(pre_master_secret, TLS_VERSION);
  34. if (os_get_random(pre_master_secret + 2,
  35. TLS_PRE_MASTER_SECRET_LEN - 2))
  36. return -1;
  37. return 0;
  38. }
  39. int tls_derive_keys(struct tlsv1_client *conn,
  40. const u8 *pre_master_secret, size_t pre_master_secret_len)
  41. {
  42. u8 seed[2 * TLS_RANDOM_LEN];
  43. u8 key_block[TLS_MAX_KEY_BLOCK_LEN];
  44. u8 *pos;
  45. size_t key_block_len;
  46. if (pre_master_secret) {
  47. wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: pre_master_secret",
  48. pre_master_secret, pre_master_secret_len);
  49. os_memcpy(seed, conn->client_random, TLS_RANDOM_LEN);
  50. os_memcpy(seed + TLS_RANDOM_LEN, conn->server_random,
  51. TLS_RANDOM_LEN);
  52. if (tls_prf(conn->rl.tls_version,
  53. pre_master_secret, pre_master_secret_len,
  54. "master secret", seed, 2 * TLS_RANDOM_LEN,
  55. conn->master_secret, TLS_MASTER_SECRET_LEN)) {
  56. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive "
  57. "master_secret");
  58. return -1;
  59. }
  60. wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: master_secret",
  61. conn->master_secret, TLS_MASTER_SECRET_LEN);
  62. }
  63. os_memcpy(seed, conn->server_random, TLS_RANDOM_LEN);
  64. os_memcpy(seed + TLS_RANDOM_LEN, conn->client_random, TLS_RANDOM_LEN);
  65. key_block_len = 2 * (conn->rl.hash_size + conn->rl.key_material_len);
  66. if (conn->rl.tls_version == TLS_VERSION_1)
  67. key_block_len += 2 * conn->rl.iv_size;
  68. if (tls_prf(conn->rl.tls_version,
  69. conn->master_secret, TLS_MASTER_SECRET_LEN,
  70. "key expansion", seed, 2 * TLS_RANDOM_LEN,
  71. key_block, key_block_len)) {
  72. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive key_block");
  73. return -1;
  74. }
  75. wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: key_block",
  76. key_block, key_block_len);
  77. pos = key_block;
  78. /* client_write_MAC_secret */
  79. os_memcpy(conn->rl.write_mac_secret, pos, conn->rl.hash_size);
  80. pos += conn->rl.hash_size;
  81. /* server_write_MAC_secret */
  82. os_memcpy(conn->rl.read_mac_secret, pos, conn->rl.hash_size);
  83. pos += conn->rl.hash_size;
  84. /* client_write_key */
  85. os_memcpy(conn->rl.write_key, pos, conn->rl.key_material_len);
  86. pos += conn->rl.key_material_len;
  87. /* server_write_key */
  88. os_memcpy(conn->rl.read_key, pos, conn->rl.key_material_len);
  89. pos += conn->rl.key_material_len;
  90. if (conn->rl.tls_version == TLS_VERSION_1) {
  91. /* client_write_IV */
  92. os_memcpy(conn->rl.write_iv, pos, conn->rl.iv_size);
  93. pos += conn->rl.iv_size;
  94. /* server_write_IV */
  95. os_memcpy(conn->rl.read_iv, pos, conn->rl.iv_size);
  96. pos += conn->rl.iv_size;
  97. } else {
  98. /*
  99. * Use IV field to set the mask value for TLS v1.1. A fixed
  100. * mask of zero is used per the RFC 4346, 6.2.3.2 CBC Block
  101. * Cipher option 2a.
  102. */
  103. os_memset(conn->rl.write_iv, 0, conn->rl.iv_size);
  104. }
  105. return 0;
  106. }
  107. /**
  108. * tlsv1_client_handshake - Process TLS handshake
  109. * @conn: TLSv1 client connection data from tlsv1_client_init()
  110. * @in_data: Input data from TLS peer
  111. * @in_len: Input data length
  112. * @out_len: Length of the output buffer.
  113. * @appl_data: Pointer to application data pointer, or %NULL if dropped
  114. * @appl_data_len: Pointer to variable that is set to appl_data length
  115. * @need_more_data: Set to 1 if more data would be needed to complete
  116. * processing
  117. * Returns: Pointer to output data, %NULL on failure
  118. */
  119. u8 * tlsv1_client_handshake(struct tlsv1_client *conn,
  120. const u8 *in_data, size_t in_len,
  121. size_t *out_len, u8 **appl_data,
  122. size_t *appl_data_len, int *need_more_data)
  123. {
  124. const u8 *pos, *end;
  125. u8 *msg = NULL, *in_msg = NULL, *in_pos, *in_end, alert, ct;
  126. size_t in_msg_len;
  127. int no_appl_data;
  128. int used;
  129. if (need_more_data)
  130. *need_more_data = 0;
  131. if (conn->state == CLIENT_HELLO) {
  132. if (in_len)
  133. return NULL;
  134. return tls_send_client_hello(conn, out_len);
  135. }
  136. if (conn->partial_input) {
  137. if (wpabuf_resize(&conn->partial_input, in_len) < 0) {
  138. wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
  139. "memory for pending record");
  140. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  141. TLS_ALERT_INTERNAL_ERROR);
  142. goto failed;
  143. }
  144. wpabuf_put_data(conn->partial_input, in_data, in_len);
  145. in_data = wpabuf_head(conn->partial_input);
  146. in_len = wpabuf_len(conn->partial_input);
  147. }
  148. if (in_data == NULL || in_len == 0)
  149. return NULL;
  150. pos = in_data;
  151. end = in_data + in_len;
  152. in_msg = os_malloc(in_len);
  153. if (in_msg == NULL)
  154. return NULL;
  155. /* Each received packet may include multiple records */
  156. while (pos < end) {
  157. in_msg_len = in_len;
  158. used = tlsv1_record_receive(&conn->rl, pos, end - pos,
  159. in_msg, &in_msg_len, &alert);
  160. if (used < 0) {
  161. wpa_printf(MSG_DEBUG, "TLSv1: Processing received "
  162. "record failed");
  163. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, alert);
  164. goto failed;
  165. }
  166. if (used == 0) {
  167. struct wpabuf *partial;
  168. wpa_printf(MSG_DEBUG, "TLSv1: Need more data");
  169. partial = wpabuf_alloc_copy(pos, end - pos);
  170. wpabuf_free(conn->partial_input);
  171. conn->partial_input = partial;
  172. if (conn->partial_input == NULL) {
  173. wpa_printf(MSG_DEBUG, "TLSv1: Failed to "
  174. "allocate memory for pending "
  175. "record");
  176. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  177. TLS_ALERT_INTERNAL_ERROR);
  178. goto failed;
  179. }
  180. os_free(in_msg);
  181. if (need_more_data)
  182. *need_more_data = 1;
  183. return NULL;
  184. }
  185. ct = pos[0];
  186. in_pos = in_msg;
  187. in_end = in_msg + in_msg_len;
  188. /* Each received record may include multiple messages of the
  189. * same ContentType. */
  190. while (in_pos < in_end) {
  191. in_msg_len = in_end - in_pos;
  192. if (tlsv1_client_process_handshake(conn, ct, in_pos,
  193. &in_msg_len,
  194. appl_data,
  195. appl_data_len) < 0)
  196. goto failed;
  197. in_pos += in_msg_len;
  198. }
  199. pos += used;
  200. }
  201. os_free(in_msg);
  202. in_msg = NULL;
  203. no_appl_data = appl_data == NULL || *appl_data == NULL;
  204. msg = tlsv1_client_handshake_write(conn, out_len, no_appl_data);
  205. failed:
  206. os_free(in_msg);
  207. if (conn->alert_level) {
  208. wpabuf_free(conn->partial_input);
  209. conn->partial_input = NULL;
  210. conn->state = FAILED;
  211. os_free(msg);
  212. msg = tlsv1_client_send_alert(conn, conn->alert_level,
  213. conn->alert_description,
  214. out_len);
  215. } else if (msg == NULL) {
  216. msg = os_zalloc(1);
  217. *out_len = 0;
  218. }
  219. if (need_more_data == NULL || !(*need_more_data)) {
  220. wpabuf_free(conn->partial_input);
  221. conn->partial_input = NULL;
  222. }
  223. return msg;
  224. }
  225. /**
  226. * tlsv1_client_encrypt - Encrypt data into TLS tunnel
  227. * @conn: TLSv1 client connection data from tlsv1_client_init()
  228. * @in_data: Pointer to plaintext data to be encrypted
  229. * @in_len: Input buffer length
  230. * @out_data: Pointer to output buffer (encrypted TLS data)
  231. * @out_len: Maximum out_data length
  232. * Returns: Number of bytes written to out_data, -1 on failure
  233. *
  234. * This function is used after TLS handshake has been completed successfully to
  235. * send data in the encrypted tunnel.
  236. */
  237. int tlsv1_client_encrypt(struct tlsv1_client *conn,
  238. const u8 *in_data, size_t in_len,
  239. u8 *out_data, size_t out_len)
  240. {
  241. size_t rlen;
  242. wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: Plaintext AppData",
  243. in_data, in_len);
  244. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_APPLICATION_DATA,
  245. out_data, out_len, in_data, in_len, &rlen) < 0) {
  246. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
  247. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  248. TLS_ALERT_INTERNAL_ERROR);
  249. return -1;
  250. }
  251. return rlen;
  252. }
  253. /**
  254. * tlsv1_client_decrypt - Decrypt data from TLS tunnel
  255. * @conn: TLSv1 client connection data from tlsv1_client_init()
  256. * @in_data: Pointer to input buffer (encrypted TLS data)
  257. * @in_len: Input buffer length
  258. * @need_more_data: Set to 1 if more data would be needed to complete
  259. * processing
  260. * Returns: Decrypted data or %NULL on failure
  261. *
  262. * This function is used after TLS handshake has been completed successfully to
  263. * receive data from the encrypted tunnel.
  264. */
  265. struct wpabuf * tlsv1_client_decrypt(struct tlsv1_client *conn,
  266. const u8 *in_data, size_t in_len,
  267. int *need_more_data)
  268. {
  269. const u8 *in_end, *pos;
  270. int used;
  271. u8 alert, *out_pos, ct;
  272. size_t olen;
  273. struct wpabuf *buf = NULL;
  274. if (need_more_data)
  275. *need_more_data = 0;
  276. if (conn->partial_input) {
  277. if (wpabuf_resize(&conn->partial_input, in_len) < 0) {
  278. wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
  279. "memory for pending record");
  280. alert = TLS_ALERT_INTERNAL_ERROR;
  281. goto fail;
  282. }
  283. wpabuf_put_data(conn->partial_input, in_data, in_len);
  284. in_data = wpabuf_head(conn->partial_input);
  285. in_len = wpabuf_len(conn->partial_input);
  286. }
  287. pos = in_data;
  288. in_end = in_data + in_len;
  289. while (pos < in_end) {
  290. ct = pos[0];
  291. if (wpabuf_resize(&buf, in_end - pos) < 0) {
  292. alert = TLS_ALERT_INTERNAL_ERROR;
  293. goto fail;
  294. }
  295. out_pos = wpabuf_put(buf, 0);
  296. olen = wpabuf_tailroom(buf);
  297. used = tlsv1_record_receive(&conn->rl, pos, in_end - pos,
  298. out_pos, &olen, &alert);
  299. if (used < 0) {
  300. wpa_printf(MSG_DEBUG, "TLSv1: Record layer processing "
  301. "failed");
  302. goto fail;
  303. }
  304. if (used == 0) {
  305. struct wpabuf *partial;
  306. wpa_printf(MSG_DEBUG, "TLSv1: Need more data");
  307. partial = wpabuf_alloc_copy(pos, in_end - pos);
  308. wpabuf_free(conn->partial_input);
  309. conn->partial_input = partial;
  310. if (conn->partial_input == NULL) {
  311. wpa_printf(MSG_DEBUG, "TLSv1: Failed to "
  312. "allocate memory for pending "
  313. "record");
  314. alert = TLS_ALERT_INTERNAL_ERROR;
  315. goto fail;
  316. }
  317. if (need_more_data)
  318. *need_more_data = 1;
  319. return buf;
  320. }
  321. if (ct == TLS_CONTENT_TYPE_ALERT) {
  322. if (olen < 2) {
  323. wpa_printf(MSG_DEBUG, "TLSv1: Alert "
  324. "underflow");
  325. alert = TLS_ALERT_DECODE_ERROR;
  326. goto fail;
  327. }
  328. wpa_printf(MSG_DEBUG, "TLSv1: Received alert %d:%d",
  329. out_pos[0], out_pos[1]);
  330. if (out_pos[0] == TLS_ALERT_LEVEL_WARNING) {
  331. /* Continue processing */
  332. pos += used;
  333. continue;
  334. }
  335. alert = out_pos[1];
  336. goto fail;
  337. }
  338. if (ct != TLS_CONTENT_TYPE_APPLICATION_DATA) {
  339. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected content type "
  340. "0x%x when decrypting application data",
  341. pos[0]);
  342. alert = TLS_ALERT_UNEXPECTED_MESSAGE;
  343. goto fail;
  344. }
  345. wpabuf_put(buf, olen);
  346. pos += used;
  347. }
  348. wpabuf_free(conn->partial_input);
  349. conn->partial_input = NULL;
  350. return buf;
  351. fail:
  352. wpabuf_free(buf);
  353. wpabuf_free(conn->partial_input);
  354. conn->partial_input = NULL;
  355. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, alert);
  356. return NULL;
  357. }
  358. /**
  359. * tlsv1_client_global_init - Initialize TLSv1 client
  360. * Returns: 0 on success, -1 on failure
  361. *
  362. * This function must be called before using any other TLSv1 client functions.
  363. */
  364. int tlsv1_client_global_init(void)
  365. {
  366. return crypto_global_init();
  367. }
  368. /**
  369. * tlsv1_client_global_deinit - Deinitialize TLSv1 client
  370. *
  371. * This function can be used to deinitialize the TLSv1 client that was
  372. * initialized by calling tlsv1_client_global_init(). No TLSv1 client functions
  373. * can be called after this before calling tlsv1_client_global_init() again.
  374. */
  375. void tlsv1_client_global_deinit(void)
  376. {
  377. crypto_global_deinit();
  378. }
  379. /**
  380. * tlsv1_client_init - Initialize TLSv1 client connection
  381. * Returns: Pointer to TLSv1 client connection data or %NULL on failure
  382. */
  383. struct tlsv1_client * tlsv1_client_init(void)
  384. {
  385. struct tlsv1_client *conn;
  386. size_t count;
  387. u16 *suites;
  388. conn = os_zalloc(sizeof(*conn));
  389. if (conn == NULL)
  390. return NULL;
  391. conn->state = CLIENT_HELLO;
  392. if (tls_verify_hash_init(&conn->verify) < 0) {
  393. wpa_printf(MSG_DEBUG, "TLSv1: Failed to initialize verify "
  394. "hash");
  395. os_free(conn);
  396. return NULL;
  397. }
  398. count = 0;
  399. suites = conn->cipher_suites;
  400. #ifndef CONFIG_CRYPTO_INTERNAL
  401. suites[count++] = TLS_RSA_WITH_AES_256_CBC_SHA256;
  402. suites[count++] = TLS_RSA_WITH_AES_256_CBC_SHA;
  403. #endif /* CONFIG_CRYPTO_INTERNAL */
  404. suites[count++] = TLS_RSA_WITH_AES_128_CBC_SHA256;
  405. suites[count++] = TLS_RSA_WITH_AES_128_CBC_SHA;
  406. suites[count++] = TLS_RSA_WITH_3DES_EDE_CBC_SHA;
  407. suites[count++] = TLS_RSA_WITH_RC4_128_SHA;
  408. suites[count++] = TLS_RSA_WITH_RC4_128_MD5;
  409. conn->num_cipher_suites = count;
  410. conn->rl.tls_version = TLS_VERSION;
  411. return conn;
  412. }
  413. /**
  414. * tlsv1_client_deinit - Deinitialize TLSv1 client connection
  415. * @conn: TLSv1 client connection data from tlsv1_client_init()
  416. */
  417. void tlsv1_client_deinit(struct tlsv1_client *conn)
  418. {
  419. crypto_public_key_free(conn->server_rsa_key);
  420. tlsv1_record_set_cipher_suite(&conn->rl, TLS_NULL_WITH_NULL_NULL);
  421. tlsv1_record_change_write_cipher(&conn->rl);
  422. tlsv1_record_change_read_cipher(&conn->rl);
  423. tls_verify_hash_free(&conn->verify);
  424. os_free(conn->client_hello_ext);
  425. tlsv1_client_free_dh(conn);
  426. tlsv1_cred_free(conn->cred);
  427. wpabuf_free(conn->partial_input);
  428. os_free(conn);
  429. }
  430. /**
  431. * tlsv1_client_established - Check whether connection has been established
  432. * @conn: TLSv1 client connection data from tlsv1_client_init()
  433. * Returns: 1 if connection is established, 0 if not
  434. */
  435. int tlsv1_client_established(struct tlsv1_client *conn)
  436. {
  437. return conn->state == ESTABLISHED;
  438. }
  439. /**
  440. * tlsv1_client_prf - Use TLS-PRF to derive keying material
  441. * @conn: TLSv1 client connection data from tlsv1_client_init()
  442. * @label: Label (e.g., description of the key) for PRF
  443. * @server_random_first: seed is 0 = client_random|server_random,
  444. * 1 = server_random|client_random
  445. * @out: Buffer for output data from TLS-PRF
  446. * @out_len: Length of the output buffer
  447. * Returns: 0 on success, -1 on failure
  448. */
  449. int tlsv1_client_prf(struct tlsv1_client *conn, const char *label,
  450. int server_random_first, u8 *out, size_t out_len)
  451. {
  452. u8 seed[2 * TLS_RANDOM_LEN];
  453. if (conn->state != ESTABLISHED)
  454. return -1;
  455. if (server_random_first) {
  456. os_memcpy(seed, conn->server_random, TLS_RANDOM_LEN);
  457. os_memcpy(seed + TLS_RANDOM_LEN, conn->client_random,
  458. TLS_RANDOM_LEN);
  459. } else {
  460. os_memcpy(seed, conn->client_random, TLS_RANDOM_LEN);
  461. os_memcpy(seed + TLS_RANDOM_LEN, conn->server_random,
  462. TLS_RANDOM_LEN);
  463. }
  464. return tls_prf(conn->rl.tls_version,
  465. conn->master_secret, TLS_MASTER_SECRET_LEN,
  466. label, seed, 2 * TLS_RANDOM_LEN, out, out_len);
  467. }
  468. /**
  469. * tlsv1_client_get_cipher - Get current cipher name
  470. * @conn: TLSv1 client connection data from tlsv1_client_init()
  471. * @buf: Buffer for the cipher name
  472. * @buflen: buf size
  473. * Returns: 0 on success, -1 on failure
  474. *
  475. * Get the name of the currently used cipher.
  476. */
  477. int tlsv1_client_get_cipher(struct tlsv1_client *conn, char *buf,
  478. size_t buflen)
  479. {
  480. char *cipher;
  481. switch (conn->rl.cipher_suite) {
  482. case TLS_RSA_WITH_RC4_128_MD5:
  483. cipher = "RC4-MD5";
  484. break;
  485. case TLS_RSA_WITH_RC4_128_SHA:
  486. cipher = "RC4-SHA";
  487. break;
  488. case TLS_RSA_WITH_DES_CBC_SHA:
  489. cipher = "DES-CBC-SHA";
  490. break;
  491. case TLS_RSA_WITH_3DES_EDE_CBC_SHA:
  492. cipher = "DES-CBC3-SHA";
  493. break;
  494. case TLS_DH_anon_WITH_AES_128_CBC_SHA256:
  495. cipher = "ADH-AES-128-SHA256";
  496. break;
  497. case TLS_DH_anon_WITH_AES_128_CBC_SHA:
  498. cipher = "ADH-AES-128-SHA";
  499. break;
  500. case TLS_RSA_WITH_AES_256_CBC_SHA:
  501. cipher = "AES-256-SHA";
  502. break;
  503. case TLS_RSA_WITH_AES_256_CBC_SHA256:
  504. cipher = "AES-256-SHA256";
  505. break;
  506. case TLS_RSA_WITH_AES_128_CBC_SHA:
  507. cipher = "AES-128-SHA";
  508. break;
  509. case TLS_RSA_WITH_AES_128_CBC_SHA256:
  510. cipher = "AES-128-SHA256";
  511. break;
  512. default:
  513. return -1;
  514. }
  515. if (os_strlcpy(buf, cipher, buflen) >= buflen)
  516. return -1;
  517. return 0;
  518. }
  519. /**
  520. * tlsv1_client_shutdown - Shutdown TLS connection
  521. * @conn: TLSv1 client connection data from tlsv1_client_init()
  522. * Returns: 0 on success, -1 on failure
  523. */
  524. int tlsv1_client_shutdown(struct tlsv1_client *conn)
  525. {
  526. conn->state = CLIENT_HELLO;
  527. if (tls_verify_hash_init(&conn->verify) < 0) {
  528. wpa_printf(MSG_DEBUG, "TLSv1: Failed to re-initialize verify "
  529. "hash");
  530. return -1;
  531. }
  532. tlsv1_record_set_cipher_suite(&conn->rl, TLS_NULL_WITH_NULL_NULL);
  533. tlsv1_record_change_write_cipher(&conn->rl);
  534. tlsv1_record_change_read_cipher(&conn->rl);
  535. conn->certificate_requested = 0;
  536. crypto_public_key_free(conn->server_rsa_key);
  537. conn->server_rsa_key = NULL;
  538. conn->session_resumed = 0;
  539. return 0;
  540. }
  541. /**
  542. * tlsv1_client_resumed - Was session resumption used
  543. * @conn: TLSv1 client connection data from tlsv1_client_init()
  544. * Returns: 1 if current session used session resumption, 0 if not
  545. */
  546. int tlsv1_client_resumed(struct tlsv1_client *conn)
  547. {
  548. return !!conn->session_resumed;
  549. }
  550. /**
  551. * tlsv1_client_hello_ext - Set TLS extension for ClientHello
  552. * @conn: TLSv1 client connection data from tlsv1_client_init()
  553. * @ext_type: Extension type
  554. * @data: Extension payload (%NULL to remove extension)
  555. * @data_len: Extension payload length
  556. * Returns: 0 on success, -1 on failure
  557. */
  558. int tlsv1_client_hello_ext(struct tlsv1_client *conn, int ext_type,
  559. const u8 *data, size_t data_len)
  560. {
  561. u8 *pos;
  562. conn->session_ticket_included = 0;
  563. os_free(conn->client_hello_ext);
  564. conn->client_hello_ext = NULL;
  565. conn->client_hello_ext_len = 0;
  566. if (data == NULL || data_len == 0)
  567. return 0;
  568. pos = conn->client_hello_ext = os_malloc(6 + data_len);
  569. if (pos == NULL)
  570. return -1;
  571. WPA_PUT_BE16(pos, 4 + data_len);
  572. pos += 2;
  573. WPA_PUT_BE16(pos, ext_type);
  574. pos += 2;
  575. WPA_PUT_BE16(pos, data_len);
  576. pos += 2;
  577. os_memcpy(pos, data, data_len);
  578. conn->client_hello_ext_len = 6 + data_len;
  579. if (ext_type == TLS_EXT_PAC_OPAQUE) {
  580. conn->session_ticket_included = 1;
  581. wpa_printf(MSG_DEBUG, "TLSv1: Using session ticket");
  582. }
  583. return 0;
  584. }
  585. /**
  586. * tlsv1_client_get_keys - Get master key and random data from TLS connection
  587. * @conn: TLSv1 client connection data from tlsv1_client_init()
  588. * @keys: Structure of key/random data (filled on success)
  589. * Returns: 0 on success, -1 on failure
  590. */
  591. int tlsv1_client_get_keys(struct tlsv1_client *conn, struct tls_keys *keys)
  592. {
  593. os_memset(keys, 0, sizeof(*keys));
  594. if (conn->state == CLIENT_HELLO)
  595. return -1;
  596. keys->client_random = conn->client_random;
  597. keys->client_random_len = TLS_RANDOM_LEN;
  598. if (conn->state != SERVER_HELLO) {
  599. keys->server_random = conn->server_random;
  600. keys->server_random_len = TLS_RANDOM_LEN;
  601. keys->master_key = conn->master_secret;
  602. keys->master_key_len = TLS_MASTER_SECRET_LEN;
  603. }
  604. return 0;
  605. }
  606. /**
  607. * tlsv1_client_get_keyblock_size - Get TLS key_block size
  608. * @conn: TLSv1 client connection data from tlsv1_client_init()
  609. * Returns: Size of the key_block for the negotiated cipher suite or -1 on
  610. * failure
  611. */
  612. int tlsv1_client_get_keyblock_size(struct tlsv1_client *conn)
  613. {
  614. if (conn->state == CLIENT_HELLO || conn->state == SERVER_HELLO)
  615. return -1;
  616. return 2 * (conn->rl.hash_size + conn->rl.key_material_len +
  617. conn->rl.iv_size);
  618. }
  619. /**
  620. * tlsv1_client_set_cipher_list - Configure acceptable cipher suites
  621. * @conn: TLSv1 client connection data from tlsv1_client_init()
  622. * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
  623. * (TLS_CIPHER_*).
  624. * Returns: 0 on success, -1 on failure
  625. */
  626. int tlsv1_client_set_cipher_list(struct tlsv1_client *conn, u8 *ciphers)
  627. {
  628. size_t count;
  629. u16 *suites;
  630. /* TODO: implement proper configuration of cipher suites */
  631. if (ciphers[0] == TLS_CIPHER_ANON_DH_AES128_SHA) {
  632. count = 0;
  633. suites = conn->cipher_suites;
  634. #ifndef CONFIG_CRYPTO_INTERNAL
  635. suites[count++] = TLS_DH_anon_WITH_AES_256_CBC_SHA256;
  636. suites[count++] = TLS_DH_anon_WITH_AES_256_CBC_SHA;
  637. #endif /* CONFIG_CRYPTO_INTERNAL */
  638. suites[count++] = TLS_DH_anon_WITH_AES_128_CBC_SHA256;
  639. suites[count++] = TLS_DH_anon_WITH_AES_128_CBC_SHA;
  640. suites[count++] = TLS_DH_anon_WITH_3DES_EDE_CBC_SHA;
  641. suites[count++] = TLS_DH_anon_WITH_RC4_128_MD5;
  642. suites[count++] = TLS_DH_anon_WITH_DES_CBC_SHA;
  643. /*
  644. * Cisco AP (at least 350 and 1200 series) local authentication
  645. * server does not know how to search cipher suites from the
  646. * list and seem to require that the last entry in the list is
  647. * the one that it wants to use. However, TLS specification
  648. * requires the list to be in the client preference order. As a
  649. * workaround, add anon-DH AES-128-SHA1 again at the end of the
  650. * list to allow the Cisco code to find it.
  651. */
  652. suites[count++] = TLS_DH_anon_WITH_AES_128_CBC_SHA;
  653. conn->num_cipher_suites = count;
  654. }
  655. return 0;
  656. }
  657. /**
  658. * tlsv1_client_set_cred - Set client credentials
  659. * @conn: TLSv1 client connection data from tlsv1_client_init()
  660. * @cred: Credentials from tlsv1_cred_alloc()
  661. * Returns: 0 on success, -1 on failure
  662. *
  663. * On success, the client takes ownership of the credentials block and caller
  664. * must not free it. On failure, caller is responsible for freeing the
  665. * credential block.
  666. */
  667. int tlsv1_client_set_cred(struct tlsv1_client *conn,
  668. struct tlsv1_credentials *cred)
  669. {
  670. tlsv1_cred_free(conn->cred);
  671. conn->cred = cred;
  672. return 0;
  673. }
  674. void tlsv1_client_set_time_checks(struct tlsv1_client *conn, int enabled)
  675. {
  676. conn->disable_time_checks = !enabled;
  677. }
  678. void tlsv1_client_set_session_ticket_cb(struct tlsv1_client *conn,
  679. tlsv1_client_session_ticket_cb cb,
  680. void *ctx)
  681. {
  682. wpa_printf(MSG_DEBUG, "TLSv1: SessionTicket callback set %p (ctx %p)",
  683. cb, ctx);
  684. conn->session_ticket_cb = cb;
  685. conn->session_ticket_cb_ctx = ctx;
  686. }